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