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