ceda744f6ab3264f783e46ee2af02a9b406e7def
[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){
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.Symposium = $resource( '/data/conference/Symposium/:id', { id:'' } );
19         this.reset();
20         this.$watch('$location.hashPath', this.hash_change);
21 }
22 Registration.$inject=['$resource'];
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                                                 var s_id = registration.symposium.$id || registration.$id;
35                                                 // first registration doesn't have symposium.$id, but we used same $id
36 console.debug( 'load symposium ', s_id );
37                                                 self.symposium = self.Symposium.get({ id: s_id });
38                                         }
39                                 });
40                         }
41                         else this.reset();
42                 }
43         },
44         reset: function() {
45                 console.debug( this.Registration );
46                 var last = this.registration;
47                 if ( last && last.type == 'symposium' ) {
48                         if ( last.$id ) last.symposium.work_nr++; // only if saved
49                 }
50                 this.registration = new this.Registration( this.master );
51                 if ( last ) {
52                         this.registration.type      = last.type;
53                         this.registration.person    = last.person;
54
55                         if ( last.type == 'symposium' )
56                         this.registration.symposium = last.symposium;
57                 }
58                 this.last_saved = angular.copy( this.registration ); // FIXME was: {};
59 console.debug( 'reset', this.registration, this.$location.hashPath, last );
60         },
61         save: function(){
62                 var self = this;
63                 this.registration.$save(function(registration){
64                         self.$location.hashPath = registration.$id;
65
66                         // save symposium to separate resource
67                         if ( registration.type == 'symposium' ) {
68                                 if ( ! self.symposium ) { 
69                                         self.registration.symposium.$id = registration.$id; // reuse $id of first work for symposium
70                                         self.symposium = new self.Symposium( registration.symposium );
71                                         self.symposium.works = [];
72                                 }
73                                 registration.work.$id = registration.$id; // preserve $id
74                                 self.symposium.works[ registration.symposium.work_nr - 1 ] = registration.work;
75 console.debug('save_symposium', self.symposium );
76                                 self.symposium.$save();
77                         }
78
79                         self.last_saved = angular.copy(registration);
80                 });
81         }
82 };
83
84 angular.validator.max_length = function(input, len) {
85         var ok = input.length <= len;
86 console.debug( 'max_length', ok, input.length, len );
87         return ok ? '' : 'must be shorter than '+len+' characters';
88 }
89