controllers.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. $log.info($scope.event.persons.length);
  82. angular.forEach($scope.event.persons, function(value, key) {
  83. if (value.attended) {
  84. attendees += 1;
  85. }
  86. });
  87. $scope.countAttendees = attendees;
  88. };
  89. $scope._addPerson = function(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. $scope.event = Event.get($stateParams);
  95. $scope.allPersons = Person.all();
  96. $scope.newPerson = {};
  97. });
  98. };
  99. $scope.fastAddPerson = function(person, isNew) {
  100. $log.debug('EventDetailsCtrl.fastAddPerson.person:');
  101. $log.debug(person);
  102. if (isNew) {
  103. var personObj = new Person(person);
  104. personObj.$save(function(p) {
  105. $scope._addPerson(angular.copy(p));
  106. });
  107. } else {
  108. $scope._addPerson(angular.copy(person));
  109. }
  110. };
  111. $scope.setPersonAttribute = function(person, key, value) {
  112. $log.debug('EventDetailsCtrl.setPersonAttribute.event_id: ' + $stateParams.id);
  113. $log.debug('EventDetailsCtrl.setPersonAttribute.person_id: ' + person.person_id);
  114. $log.debug('EventDetailsCtrl.setPersonAttribute.key: ' + key + ' value: ' + value);
  115. var data = {_id: $stateParams.id, person_id: person.person_id};
  116. data[key] = value;
  117. Event.updatePerson(data,
  118. function(data) {
  119. $log.debug('EventDetailsCtrl.setPersonAttribute.data');
  120. $log.debug(data);
  121. $scope.event.persons = data;
  122. });
  123. };
  124. $scope.removeAttendee = function(person) {
  125. Event.deletePerson({
  126. _id: $stateParams.id,
  127. person_id: person.person_id
  128. },
  129. function(data) {
  130. $scope.event.persons = data;
  131. $scope.allPersons = Person.all();
  132. });
  133. };
  134. }]
  135. );
  136. eventManControllers.controller('PersonsListCtrl', ['$scope', 'Person', 'Setting',
  137. function ($scope, Person, Setting) {
  138. $scope.persons = Person.all();
  139. $scope.personsOrderProp = 'name';
  140. $scope.eventsOrderProp = '-begin-date';
  141. $scope.customFields = Setting.query({setting: 'person_custom_field',
  142. in_persons_list: true});
  143. $scope.setAttribute = function(person, key, value) {
  144. var data = {_id: person._id};
  145. data[key] = value;
  146. Person.update(data, function() {
  147. $scope.persons = Person.all();
  148. });
  149. };
  150. $scope.remove = function(_id) {
  151. Person.remove({'id': _id}, function() {
  152. $scope.persons = Person.all();
  153. });
  154. };
  155. }]
  156. );
  157. eventManControllers.controller('PersonDetailsCtrl', ['$scope', '$stateParams', 'Person', 'Event', 'Setting', '$log',
  158. function ($scope, $stateParams, Person, Event, Setting, $log) {
  159. $scope.personsOrderProp = 'name';
  160. $scope.eventsOrderProp = '-begin-date';
  161. $scope.addToEvent = '';
  162. $scope.customFields = Setting.query({setting: 'person_custom_field'});
  163. if ($stateParams.id) {
  164. $scope.person = Person.get($stateParams);
  165. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  166. } else {
  167. $scope.events = Event.all();
  168. }
  169. // store a new Person or update an existing one
  170. $scope.save = function() {
  171. if ($scope.person.id === undefined) {
  172. $scope.person = new Person($scope.person);
  173. $scope.person.$save(function(person) {
  174. if ($scope.addToEvent) {
  175. var data = angular.copy(person);
  176. data.person_id = data._id;
  177. data._id = $scope.addToEvent;
  178. data.attended = false;
  179. Event.addPerson(data);
  180. }
  181. });
  182. } else {
  183. $scope.person = Person.update($scope.person, function(data) {
  184. if ($scope.addToEvent) {
  185. var data = angular.copy($scope.person);
  186. data._id = $scope.addToEvent;
  187. data.person_id = $scope.person._id;
  188. data.attended = false;
  189. Event.addPerson(data);
  190. }
  191. });
  192. }
  193. $scope.personForm.$dirty = false;
  194. };
  195. $scope.setPersonAttributeAtEvent = function(evnt, key, value) {
  196. var attrs = {_id: evnt._id, person_id: $stateParams.id};
  197. attrs[key] = value;
  198. Event.updatePerson(attrs,
  199. function(data) {
  200. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  201. }
  202. );
  203. };
  204. $scope.switchRegistered = function(evnt, person, add) {
  205. $log.debug('PersonDetailsCtrl.switchRegistered.event_id: ' + evnt._id);
  206. $log.debug('PersonDetailsCtrl.switchRegistered.person_id: ' + person._id);
  207. $log.debug('PersonDetailsCtrl.switchRegistered.add: ' + add);
  208. if (add) {
  209. var data = angular.copy(person);
  210. data._id = evnt._id;
  211. data.person_id = person._id;
  212. data.attended = false;
  213. Event.addPerson(data,
  214. function(data) {
  215. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  216. }
  217. );
  218. } else {
  219. Event.deletePerson({_id: evnt._id, person_id: person._id},
  220. function(data) {
  221. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  222. }
  223. );
  224. }
  225. };
  226. }]
  227. );
  228. eventManControllers.controller('FileUploadCtrl', ['$scope', '$log', '$upload', 'Event',
  229. function ($scope, $log, $upload, Event) {
  230. $scope.file = null;
  231. $scope.reply = {};
  232. $scope.events = Event.all();
  233. $scope.upload = function(file, url) {
  234. $log.debug("FileUploadCtrl.upload");
  235. $upload.upload({
  236. url: url,
  237. file: file,
  238. fields: {targetEvent: $scope.targetEvent}
  239. }).progress(function(evt) {
  240. var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
  241. $log.debug('progress: ' + progressPercentage + '%');
  242. }).success(function(data, status, headers, config) {
  243. $scope.file = null;
  244. $scope.reply = angular.fromJson(data);
  245. });
  246. };
  247. }]
  248. );