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