controllers.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. 'use strict';
  2. /* Controllers; their method are available where specified with the ng-controller
  3. * directive or for a given route/state (see app.js). They use some services to
  4. * connect to the backend (see services.js). */
  5. var eventManControllers = angular.module('eventManControllers', []);
  6. /* A controller that can be used to navigate. */
  7. eventManControllers.controller('NavigationCtrl', ['$location',
  8. function ($location) {
  9. this.go = function(url) {
  10. $location.url(url);
  11. };
  12. this.isActive = function (view) {
  13. if (view === $location.path()) {
  14. return true;
  15. }
  16. if (view[view.length-1] !== '/') {
  17. view = view + '/';
  18. }
  19. return $location.path().indexOf(view) == 0;
  20. };
  21. }]
  22. );
  23. /* Controller for a group of date and time pickers. */
  24. eventManControllers.controller('DatetimePickerCtrl', ['$scope',
  25. function ($scope) {
  26. $scope.open = function($event) {
  27. $event.preventDefault();
  28. $event.stopPropagation();
  29. $scope.opened = true;
  30. };
  31. }]
  32. );
  33. eventManControllers.controller('EventsListCtrl', ['$scope', 'Event',
  34. function ($scope, Event) {
  35. $scope.events = Event.all();
  36. $scope.personsOrderProp = 'name';
  37. $scope.eventsOrderProp = "'-begin-date'";
  38. $scope.remove = function(_id) {
  39. Event.remove({'id': _id}, function() {
  40. $scope.events = Event.all();
  41. });
  42. };
  43. }]
  44. );
  45. eventManControllers.controller('EventDetailsCtrl', ['$scope', 'Event', 'Person', '$stateParams', 'Setting', '$log',
  46. function ($scope, Event, Person, $stateParams, Setting, $log) {
  47. $scope.personsOrderProp = 'name';
  48. $scope.eventsOrderProp = '-begin-date';
  49. $scope.countAttendees = 0;
  50. $scope.customFields = Setting.query({setting: 'person_custom_field',
  51. in_event_details: true});
  52. if ($stateParams.id) {
  53. $scope.event = Event.get($stateParams, function() {
  54. $scope.$watchCollection(function() {
  55. return $scope.event.persons;
  56. }, function(prev, old) {
  57. $scope.calcAttendees();
  58. });
  59. });
  60. $scope.allPersons = Person.all();
  61. }
  62. // store a new Event or update an existing one
  63. $scope.save = function() {
  64. // avoid override of event.persons list.
  65. var this_event = angular.copy($scope.event);
  66. if (this_event.persons) {
  67. delete this_event.persons;
  68. }
  69. if (this_event.id === undefined) {
  70. $scope.event = Event.save(this_event);
  71. } else {
  72. $scope.event = Event.update(this_event);
  73. }
  74. $scope.eventForm.$dirty = false;
  75. };
  76. $scope.calcAttendees = function() {
  77. if (!($scope.event && $scope.event.persons)) {
  78. return;
  79. }
  80. var attendees = 0;
  81. angular.forEach($scope.event.persons, function(value, key) {
  82. if (value.attended) {
  83. attendees += 1;
  84. }
  85. });
  86. $scope.countAttendees = attendees;
  87. };
  88. $scope._addPerson = function(person_data) {
  89. var original_data = angular.copy(person_data);
  90. person_data.person_id = person_data._id;
  91. person_data._id = $stateParams.id;
  92. person_data.attended = true;
  93. Event.addPerson(person_data, function() {
  94. // This could be improved adding it only locally.
  95. //$scope.event.persons.push(person_data);
  96. Event.get($stateParams, function(data) {
  97. $scope.event.persons = angular.fromJson(data).persons;
  98. });
  99. var idx = $scope.allPersons.indexOf(original_data);
  100. if (idx != -1) {
  101. $scope.allPersons.splice(idx, 1);
  102. } else {
  103. $scope.allPersons = Person.all();
  104. }
  105. $scope.newPerson = {};
  106. });
  107. };
  108. $scope.fastAddPerson = function(person, isNew) {
  109. $log.debug('EventDetailsCtrl.fastAddPerson.person:');
  110. $log.debug(person);
  111. if (isNew) {
  112. var personObj = new Person(person);
  113. personObj.$save(function(p) {
  114. $scope._addPerson(angular.copy(p));
  115. });
  116. } else {
  117. $scope._addPerson(angular.copy(person));
  118. }
  119. };
  120. $scope.setPersonAttribute = function(person, key, value) {
  121. $log.debug('EventDetailsCtrl.setPersonAttribute.event_id: ' + $stateParams.id);
  122. $log.debug('EventDetailsCtrl.setPersonAttribute.person_id: ' + person.person_id);
  123. $log.debug('EventDetailsCtrl.setPersonAttribute.key: ' + key + ' value: ' + value);
  124. var data = {_id: $stateParams.id, person_id: person.person_id};
  125. data[key] = value;
  126. Event.updatePerson(data,
  127. function(data) {
  128. $log.debug('EventDetailsCtrl.setPersonAttribute.data');
  129. $log.debug(data);
  130. $scope.event.persons = data;
  131. });
  132. };
  133. $scope.removeAttendee = function(person) {
  134. Event.deletePerson({
  135. _id: $stateParams.id,
  136. person_id: person.person_id
  137. },
  138. function(data) {
  139. $scope.event.persons = data;
  140. $scope.allPersons = Person.all();
  141. });
  142. };
  143. }]
  144. );
  145. eventManControllers.controller('PersonsListCtrl', ['$scope', 'Person', 'Setting',
  146. function ($scope, Person, Setting) {
  147. $scope.persons = Person.all();
  148. $scope.personsOrderProp = 'name';
  149. $scope.eventsOrderProp = '-begin-date';
  150. $scope.customFields = Setting.query({setting: 'person_custom_field',
  151. in_persons_list: true});
  152. $scope.setAttribute = function(person, key, value) {
  153. var data = {_id: person._id};
  154. data[key] = value;
  155. Person.update(data, function() {
  156. $scope.persons = Person.all();
  157. });
  158. };
  159. $scope.remove = function(_id) {
  160. Person.remove({'id': _id}, function() {
  161. $scope.persons = Person.all();
  162. });
  163. };
  164. }]
  165. );
  166. eventManControllers.controller('PersonDetailsCtrl', ['$scope', '$stateParams', 'Person', 'Event', 'Setting', '$log',
  167. function ($scope, $stateParams, Person, Event, Setting, $log) {
  168. $scope.personsOrderProp = 'name';
  169. $scope.eventsOrderProp = '-begin-date';
  170. $scope.addToEvent = '';
  171. $scope.customFields = Setting.query({setting: 'person_custom_field',
  172. in_persons_list: true});
  173. if ($stateParams.id) {
  174. $scope.person = Person.get($stateParams);
  175. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  176. } else {
  177. $scope.events = Event.all();
  178. }
  179. // store a new Person or update an existing one
  180. $scope.save = function() {
  181. if ($scope.person.id === undefined) {
  182. $scope.person = new Person($scope.person);
  183. $scope.person.$save(function(person) {
  184. if ($scope.addToEvent) {
  185. var data = angular.copy(person);
  186. data.person_id = data._id;
  187. data._id = $scope.addToEvent;
  188. data.attended = false;
  189. Event.addPerson(data);
  190. }
  191. });
  192. } else {
  193. $scope.person = Person.update($scope.person, function(data) {
  194. if ($scope.addToEvent) {
  195. var data = angular.copy($scope.person);
  196. data._id = $scope.addToEvent;
  197. data.person_id = $scope.person._id;
  198. data.attended = false;
  199. Event.addPerson(data);
  200. }
  201. });
  202. }
  203. $scope.personForm.$dirty = false;
  204. };
  205. $scope.setPersonAttributeAtEvent = function(evnt, key, value) {
  206. var attrs = {_id: evnt._id, person_id: $stateParams.id};
  207. attrs[key] = value;
  208. Event.updatePerson(attrs,
  209. function(data) {
  210. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  211. }
  212. );
  213. };
  214. $scope.switchRegistered = function(evnt, person, add) {
  215. $log.debug('PersonDetailsCtrl.switchRegistered.event_id: ' + evnt._id);
  216. $log.debug('PersonDetailsCtrl.switchRegistered.person_id: ' + person._id);
  217. $log.debug('PersonDetailsCtrl.switchRegistered.add: ' + add);
  218. if (add) {
  219. var data = angular.copy(person);
  220. data._id = evnt._id;
  221. data.person_id = person._id;
  222. data.attended = false;
  223. Event.addPerson(data,
  224. function(data) {
  225. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  226. }
  227. );
  228. } else {
  229. Event.deletePerson({_id: evnt._id, person_id: person._id},
  230. function(data) {
  231. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  232. }
  233. );
  234. }
  235. };
  236. }]
  237. );
  238. eventManControllers.controller('FileUploadCtrl', ['$scope', '$log', '$upload', 'Event',
  239. function ($scope, $log, $upload, Event) {
  240. $scope.file = null;
  241. $scope.reply = {};
  242. $scope.events = Event.all();
  243. $scope.upload = function(file, url) {
  244. $log.debug("FileUploadCtrl.upload");
  245. $upload.upload({
  246. url: url,
  247. file: file,
  248. fields: {targetEvent: $scope.targetEvent}
  249. }).progress(function(evt) {
  250. var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
  251. $log.debug('progress: ' + progressPercentage + '%');
  252. }).success(function(data, status, headers, config) {
  253. $scope.file = null;
  254. $scope.reply = angular.fromJson(data);
  255. });
  256. };
  257. }]
  258. );