replace Symposium $resource with $xhr CouchDB view
[angular-mojolicious.git] / public / app / conference / registration.js
1
2 if (typeof (console) === 'undefined') console = { debug: function() {} }; // mock console.debug
3
4 function Registration($resource,$xhr){
5         this.master = {
6                 person: {
7                         name: '', surname: '', inst: '', email: ''
8                 },
9                 type: 'participant',
10                 work: {
11                         title: '',
12                         abstract: '',
13                         authors:[ { name:'', surname:'', inst:'', email:'' } ]
14                 },
15                 symposium: { organizers: [ {name:'', surname:'', inst:'', email:'' } ], work_nr: 1 }
16         };
17         this.Registration = $resource( '/data/conference/Registration/:id', { id:'' } );
18         this.reset();
19         this.$watch('$location.hashPath', this.hash_change);
20         this.$xhr = $xhr;
21 }
22 Registration.$inject=['$resource','$xhr'];
23
24 Registration.prototype = {
25         hash_change: function() {
26                 var id = this.$location.hashPath;
27 console.debug( 'hash_change', id, this.registration.$id );
28                 if ( id != this.registration.$id ) {
29                         if (id) {
30                                 var self = this;
31                                 this.registration = this.Registration.get({ id: id }, function(registration) {
32                                         self.last_saved = angular.copy(registration);
33                                         if ( registration.type == 'symposium' ) {
34                                                 //self.symposium = self.Symposium.get({ id: s_id });
35                                                 self.load_symposium();
36                                         }
37                                 });
38                         }
39                         else this.reset();
40                 }
41         },
42         reset: function() {
43                 console.debug( this.Registration );
44                 var last = this.registration;
45                 if ( last && last.type == 'symposium' ) {
46                         if ( last.$id ) last.symposium.work_nr++; // only if saved
47                 }
48                 this.registration = new this.Registration( this.master );
49                 if ( last ) {
50                         this.registration.type      = last.type;
51                         this.registration.person    = last.person;
52
53                         if ( last.type == 'symposium' )
54                         this.registration.symposium = last.symposium;
55                 }
56                 this.last_saved = angular.copy( this.registration ); // FIXME was: {};
57 console.debug( 'reset', this.registration, this.$location.hashPath, last );
58         },
59         save: function(){
60                 var self = this;
61                 this.registration.$save(function(registration){
62                         self.$location.hashPath = registration.$id;
63
64                         // save symposium to separate resource
65                         if ( registration.type == 'symposium' ) {
66                                 if ( ! self.symposium ) { 
67                                         registration.symposium.$id = registration.$id; // reuse $id of first work for symposium
68                                         self.symposium = angular.copy( self.registration.symposium );
69                                         self.symposium.works = [];
70                                 }
71                                 registration.work.$id = registration.$id; // preserve $id
72                                 self.symposium.works[ registration.symposium.work_nr - 1 ] = registration.work;
73
74                                 //self.symposium.$save();
75                                 //self.load_symposium();
76                         }
77
78                         self.last_saved = angular.copy(registration);
79                 });
80         },
81         load_symposium: function() {
82                 var self = this;
83                 var s_id = self.registration.symposium.$id || self.registration.$id;
84
85                 if ( s_id = self.symposium.$id ) {
86                         console.debug('load_symposium ', s_id, ' allready loaded');
87                         return;
88                 }
89
90                 self.symposium = angular.copy( self.registration.symposium );
91                 self.symposium.works = [];
92                 // first registration doesn't have symposium.$id, but we used same $id
93 console.debug( 'load_symposium ', s_id, self.symposium );
94
95 console.debug( self.$xhr );
96
97                 self.$xhr("JSON"
98                         , "http://localhost:5984/conference/_design/symposium/_view/works?callback=JSON_CALLBACK;key=" + s_id
99                         , function(code, response){ 
100 console.log('symposium/_view/works', code, response);
101                                 angular.foreach( response.rows, function(row) {
102                                         var work = row.value.work;
103                                         work.$id = row.value.$id; // copy $id so we can select correct one in list
104                                         self.symposium.works.push( work );
105                                 } );
106 console.debug( 'symposium', self.symposium );
107                         }
108                 ); 
109         }
110 };
111
112 angular.validator.max_length = function(input, len) {
113         var ok = input.length <= len;
114 console.debug( 'max_length', ok, input.length, len );
115         return ok ? '' : 'must be shorter than '+len+' characters';
116 }
117