fix load_symposium and make it more robust
[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.get({ id: id }, function(registration) {
32 console.debug('registration', id, registration);
33                                         self.last_saved = angular.copy(registration);
34                                         self.registration = registration; // needed for load_symposium below
35                                         self.load_symposium();
36                                 });
37                         }
38                         else this.reset();
39                 }
40         },
41         reset: function() {
42                 console.debug( this.Registration );
43                 var last = this.registration;
44                 if ( last && last.type == 'symposium' ) {
45                         if ( last.$id ) last.symposium.work_nr++; // only if saved
46                 }
47                 this.registration = new this.Registration( this.master );
48                 if ( last ) {
49                         this.registration.type      = last.type;
50                         this.registration.person    = last.person;
51
52                         if ( last.type == 'symposium' )
53                         this.registration.symposium = last.symposium;
54                 }
55                 this.last_saved = angular.copy( this.registration ); // FIXME was: {};
56 console.debug( 'reset', this.registration, this.$location.hashPath, last );
57         },
58         save: function(){
59                 var self = this;
60                 this.registration.$save(function(registration){
61                         self.$location.hashPath = registration.$id;
62
63                         // save symposium to separate resource
64                         if ( registration.type == 'symposium' ) {
65                                 if ( ! self.symposium ) { 
66                                         registration.symposium.$id = registration.$id; // reuse $id of first work for symposium
67                                         self.symposium = angular.copy( self.registration.symposium );
68                                         self.symposium.works = [];
69                                 }
70                                 registration.work.$id = registration.$id; // preserve $id
71                                 self.symposium.works[ registration.symposium.work_nr - 1 ] = registration.work;
72
73                                 //self.symposium.$save();
74                                 //self.load_symposium();
75                         }
76
77                         self.last_saved = angular.copy(registration);
78                 });
79         },
80         load_symposium: function() {
81                 var self = this;
82                 if ( self.registration.type != 'symposium' ) return;
83
84                 var s_id = self.registration.$id;
85                 if ( self.registration.symposium ) s_id = self.registration.symposium.$id;
86
87                 if ( self.symposium && self.symposium.$id == s_id ) {
88                         console.debug('load_symposium ', s_id, ' allready loaded');
89                         return;
90                 }
91
92                 self.symposium = angular.copy( self.registration.symposium );
93                 self.symposium.works = [];
94                 // first registration doesn't have symposium.$id, but we used same $id
95 console.debug( 'load_symposium ', s_id, self.symposium );
96
97 console.debug( self.$xhr );
98
99                 self.$xhr("JSON"
100                         , "http://localhost:5984/conference/_design/symposium/_view/works?callback=JSON_CALLBACK;key=" + s_id
101                         , function(code, response){ 
102 console.log('symposium/_view/works', code, response);
103                                 angular.foreach( response.rows, function(row) {
104                                         var work = row.value.work;
105                                         work.$id = row.value.$id; // copy $id so we can select correct one in list
106                                         self.symposium.works.push( work );
107                                 } );
108 console.debug( 'symposium', self.symposium );
109                         }
110                 ); 
111         }
112 };
113
114 angular.validator.max_length = function(input, len) {
115         var ok = input.length <= len;
116 console.debug( 'max_length', ok, input.length, len );
117         return ok ? '' : 'must be shorter than '+len+' characters';
118 }
119