angular-strap bs-typeahead for organization
[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(1){//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;
61                         $log.info('organizations', $scope.organizations);
62                 });
63         }
64 }//FIXME
65
66         $scope.$watch('user.registration_type', function( oldValue, newValue ) {
67                 $log.info("registration_type watch", oldValue, newValue );
68
69                 if ( oldValue === newValue ) return; // triggers on resource, so ignore it
70                 if ( ! $scope.user ) { // resource triggers watch before loading json
71                         $log.error("no user in scope");
72                         return;
73                 }
74
75                 if (
76                         $scope.user.registration_type == 'lecture' ||
77                         $scope.user.registration_type == 'poster' ||
78                         $scope.user.registration_type == 'symposium' ||
79                         $scope.user.registration_type == 'round'
80                 ) {
81                         $scope.work.type = $scope.user.registration_type;
82                         $scope.has_work = true;
83                         $log.info( $scope.user.registration_type, " type updated");
84
85                         if ( $scope.user.registration_type == 'symposium' && ! $scope.work.symposium_works ) {
86                                 // create 4 empty symposium works
87                                 $scope.work.symposium_works = [
88                                         { persons: [{}] },
89                                         { persons: [{}] },
90                                         { persons: [{}] },
91                                         { persons: [{}] }
92                                 ];
93                                 $log.info('created symposium_works');
94                         }
95
96                 } else {
97                         $log.info( $scope.user.registration_type, "NO work" );
98                         $scope.has_work = false;
99                 }
100         });
101
102         $scope.change_student = function() {
103                 if ( $scope.user.student ) {
104                         $scope.user.hpd_member = false;
105                         $scope.user.reception = false;
106                         $scope.user.dinner = false;
107                 }
108         }
109
110         $scope.addPerson = function(persons) {
111                 $log.info('addPerson', persons);
112                 persons.push({ firstname: '' });
113         };
114  
115         $scope.removePerson = function(persons,person) {
116                 for (var i = 0, ii = persons.length; i < ii; i++) {
117                         if (person === persons[i]) {
118                                 persons.splice(i, 1);
119                                 $log.info('removePerson', i, person);
120                         }
121                 }
122         };
123
124         $scope.add_symposium_work = function(works) {
125                 works.push({ persons: [{}] });
126         }
127
128         $scope.reset();
129 }
130 //RegistrationCtrl.$inject = [ '$scope', '$log' ];
131
132 function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter) {
133
134         $scope.list = [];
135         $scope.search = {};
136         $scope.ready = false;
137
138         $scope.all_registrations = Registration.query( function() {
139                 var RegistrationTypeCount = { '': 0 };
140                 angular.forEach( $scope.all_registrations, function(value, key) {
141                         var type = value.user.registration_type;
142                         if ( ! angular.isNumber( RegistrationTypeCount[type] ) ) {
143                                 RegistrationTypeCount[type] = 1;
144                         } else {
145                                 RegistrationTypeCount[type]++;
146                         }
147                         RegistrationTypeCount['']++;
148
149                         value.registration_type = type; // for search
150                         
151 //                      $log.info( key, value.user.registration_type, RegistrationTypeCount[type]  );
152                 });
153                 $log.info('RegistrationTypeCount', RegistrationTypeCount);
154                 $scope.RegistrationTypeCount = RegistrationTypeCount;
155
156 //              $scope.list = $scope.all_registrations; // FIXME show all registrations on page load
157                 $scope.ready = true;
158         });
159
160         $scope.filter_list = function(newVal, oldVal) {
161                 $scope.list = $filter('filter')($scope.all_registrations, $scope.search);
162                 $log.info('filter_list', newVal, oldVal, 'search', $scope.search, 'results', $scope.list);
163         };
164         $scope.$watch('search.registration_type', $scope.filter_list);
165         $scope.$watch('search.$', $scope.filter_list); // $ is skipped by search watch!!
166
167         $scope.RegistrationTypes = RegistrationTypes;
168         $log.info( "RegistrationTypes", RegistrationTypes );
169 }
170
171
172 function MyCtrl2() {
173 }
174 MyCtrl2.$inject = [];