implement registration state
[angular-drzb] / app / js / controllers.js
1 'use strict';
2
3 /* Controllers */
4
5
6 function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $route, View) {
7
8         $scope.$routeParams = $routeParams;
9         $scope.$location = $location;
10         $scope.organizations = [];
11
12         $scope.update = function(registration, state) {
13
14                 registration.state = state;
15
16                 if ( ! $scope.has_work && registration.work ) {
17                         delete( registration.work );
18                         $log.info("removed work");
19                 }
20
21                 registration.$save( function(registration) {
22                         $log.info("saved", registration);
23                         $log.info('id =', registration.id, 'state = ', registration.state);
24                         if ( registration.id && registration.state ) {
25                                 $location.path( '/' + registration.state + '/' + registration.id );
26                         } else {
27                                 $log.error("can't find id in ", registration);
28                         }
29                 });
30         }
31
32         $scope.reset = function() {
33                 if ( $routeParams.registrationId ) {
34                         $scope.registration = new Registration();
35                         $scope.registration.$get({ registrationId: $routeParams.registrationId }, function(registration) {
36                                 $log.info("get Registration", registration);
37                                 $scope.user = registration.user;
38                                 $scope.work = registration.work;
39                         });
40                 } else {
41                         $scope.registration = new Registration({ user: {}, work: { persons: [] } });
42                         $scope.user = $scope.registration.user;
43                         $scope.work = $scope.registration.work;
44                         $scope.work.persons = [ $scope.user ]; // first author is person submitting work
45                         $log.info("new Registration", $scope.registration);
46                 }
47
48                 $log.info( $routeParams.registrationId );
49
50                 $scope.view = new View();
51 if(0){//FIXME
52                 $scope.view.$get({ view: 'organizations' }, function(result) {
53                         $log.info('View organizations', result);
54                         angular.forEach( result.rows, function(value, key) {
55                                 this.push( value.key ); // name of organization
56                         }, $scope.organizations );
57                         $log.info('organizations', $scope.organizations);
58                 });
59 }else{//FIXME
60                 $scope.view.$key_array({ view: 'organizations' }, function(result) {
61                         $log.info('key_array', result);
62 //                      $scope.organizations = result.rows; // FIXME doesn't refresh bs-typeahead
63                         angular.forEach( result.rows, function(o) {
64                                 this.push( o );
65                         }, $scope.organizations );
66                         $log.info('organizations', $scope.organizations);
67                 });
68         }
69 }//FIXME
70
71         $scope.$watch('user.registration_type', function( oldValue, newValue ) {
72                 $log.info("registration_type watch", oldValue, newValue );
73
74                 if ( oldValue === newValue ) return; // triggers on resource, so ignore it
75                 if ( ! $scope.user ) { // resource triggers watch before loading json
76                         $log.error("no user in scope");
77                         return;
78                 }
79
80                 if (
81                         $scope.user.registration_type == 'lecture' ||
82                         $scope.user.registration_type == 'poster' ||
83                         $scope.user.registration_type == 'symposium' ||
84                         $scope.user.registration_type == 'round'
85                 ) {
86                         $scope.work.type = $scope.user.registration_type;
87                         $scope.has_work = true;
88                         $log.info( $scope.user.registration_type, " type updated");
89
90                         if ( $scope.user.registration_type == 'symposium' && ! $scope.work.symposium_works ) {
91                                 // create 4 empty symposium works
92                                 $scope.work.symposium_works = [
93                                         { persons: [{}] },
94                                         { persons: [{}] },
95                                         { persons: [{}] },
96                                         { persons: [{}] }
97                                 ];
98                                 $log.info('created symposium_works');
99                         }
100
101                 } else {
102                         $log.info( $scope.user.registration_type, "NO work" );
103                         $scope.has_work = false;
104                 }
105         });
106
107         $scope.change_student = function() {
108                 if ( $scope.user.student ) {
109                         $scope.user.hpd_member = false;
110                         $scope.user.dinner = false;
111                 }
112         }
113
114         $scope.addPerson = function(persons) {
115                 $log.info('addPerson', persons);
116                 persons.push({ firstname: '' });
117         };
118  
119         $scope.removePerson = function(persons,person) {
120                 for (var i = 0, ii = persons.length; i < ii; i++) {
121                         if (person === persons[i]) {
122                                 persons.splice(i, 1);
123                                 $log.info('removePerson', i, person);
124                         }
125                 }
126         };
127
128         $scope.add_symposium_work = function(works) {
129                 works.push({ persons: [{}] });
130         }
131
132         $scope.symposium_work_remove = function(work_index) {
133                 if ( ! angular.isNumber(work_index) ) {
134                         $log.error("work_index", work_index);
135                         return;
136                 }
137                 var works = $scope.work.symposium_works;
138                 $log.info('symposium_work_remove', works, work_index);
139                 var removed = works.splice( work_index, 1 );
140                 if ( angular.isArray(removed) && removed[0] ) {
141                         removed = removed[0];
142                         removed.deleted = true;
143                         if ( ! angular.isArray( $scope.work.symposium_works_deleted ) )
144                                 $scope.work.symposium_works_deleted = [];
145                         $scope.work.symposium_works_deleted.push( removed );
146                         $log.info("work deleted", works);
147                 } else {
148                         $log.warn("symposium_work_remove no work to remove", works, work);
149                 }
150         }
151
152         $scope.abstract_class = function(work) {
153                 var abstract = work.abstract;
154                 return angular.isString(abstract) && abstract.length <= 2000 ? 'ok' : 'ng-invalid';
155         }
156         $scope.abstract_length = function(work) {
157                 var abstract = work.abstract;
158                 if ( ! abstract ) return 0;
159                 return abstract.length <= 2000 ? abstract.length : 2000 - abstract.length;
160         }
161
162         $scope.reset();
163 }
164 //RegistrationCtrl.$inject = [ '$scope', '$log' ];
165
166 function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter) {
167
168         $scope.list = [];
169         $scope.search = {};
170         $scope.ready = false;
171
172         $scope.all_registrations = Registration.query( function() {
173                 var RegistrationTypeCount = { '': 0 };
174                 angular.forEach( $scope.all_registrations, function(value, key) {
175                         var type = value.user.registration_type;
176                         if ( ! angular.isNumber( RegistrationTypeCount[type] ) ) {
177                                 RegistrationTypeCount[type] = 1;
178                         } else {
179                                 RegistrationTypeCount[type]++;
180                         }
181                         RegistrationTypeCount['']++;
182
183                         value.registration_type = type; // for search
184                         
185 //                      $log.info( key, value.user.registration_type, RegistrationTypeCount[type]  );
186                 });
187                 $log.info('RegistrationTypeCount', RegistrationTypeCount);
188                 $scope.RegistrationTypeCount = RegistrationTypeCount;
189
190 //              $scope.list = $scope.all_registrations; // FIXME show all registrations on page load
191                 $scope.ready = true;
192         });
193
194         $scope.filter_list = function(newVal, oldVal) {
195                 $scope.list = $filter('filter')($scope.all_registrations, $scope.search);
196                 $log.info('filter_list', newVal, oldVal, 'search', $scope.search, 'results', $scope.list);
197         };
198         $scope.$watch('search.registration_type', $scope.filter_list);
199         $scope.$watch('search.$', $scope.filter_list); // $ is skipped by search watch!!
200
201         $scope.RegistrationTypes = RegistrationTypes;
202         $log.info( "RegistrationTypes", RegistrationTypes );
203 }
204
205
206 function MyCtrl2() {
207 }
208 MyCtrl2.$inject = [];