controllers.js 11 KB

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