controllers.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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', ['$scope', '$location', 'Setting',
  8. function ($scope, $location, Setting) {
  9. $scope.logo = {};
  10. $scope.go = function(url) {
  11. $location.url(url);
  12. };
  13. Setting.query({setting: 'logo'}, function(data) {
  14. if (data && data.length) {
  15. $scope.logo = data[0];
  16. }
  17. });
  18. $scope.isActive = function(view) {
  19. if (view === $location.path()) {
  20. return true;
  21. }
  22. if (view[view.length-1] !== '/') {
  23. view = view + '/';
  24. }
  25. return $location.path().indexOf(view) == 0;
  26. };
  27. }]
  28. );
  29. /* Controller for a group of date and time pickers. */
  30. eventManControllers.controller('DatetimePickerCtrl', ['$scope',
  31. function ($scope) {
  32. $scope.open = function($event) {
  33. $event.preventDefault();
  34. $event.stopPropagation();
  35. $scope.opened = true;
  36. };
  37. }]
  38. );
  39. /* Controller for modals. */
  40. eventManControllers.controller('ModalConfirmInstanceCtrl', ['$scope', '$modalInstance', 'message',
  41. function ($scope, $modalInstance, message) {
  42. $scope.message = message;
  43. $scope.ok = function () {
  44. $modalInstance.close($scope);
  45. };
  46. $scope.cancel = function () {
  47. $modalInstance.dismiss('cancel');
  48. };
  49. }]
  50. );
  51. eventManControllers.controller('EventsListCtrl', ['$scope', 'Event', '$modal', '$log', '$translate', '$rootScope',
  52. function ($scope, Event, $modal, $log, $translate, $rootScope) {
  53. $scope.events = Event.all();
  54. $scope.personsOrderProp = 'name';
  55. $scope.eventsOrderProp = "'-begin-date'";
  56. $scope.confirm_delete = 'Do you really want to delete this event?';
  57. $rootScope.$on('$translateChangeSuccess', function () {
  58. $translate('Do you really want to delete this event?').then(function (translation) {
  59. $scope.confirm_delete = translation;
  60. });
  61. });
  62. $scope.remove = function(_id) {
  63. var modalInstance = $modal.open({
  64. scope: $scope,
  65. templateUrl: 'modal-confirm-action.html',
  66. controller: 'ModalConfirmInstanceCtrl',
  67. resolve: {
  68. message: function() { return $scope.confirm_delete; }
  69. }
  70. });
  71. modalInstance.result.then(function() {
  72. console.debug('here');
  73. Event.remove({'id': _id}, function() {
  74. $scope.events = Event.all();
  75. }
  76. );
  77. });
  78. };
  79. }]
  80. );
  81. eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event', 'Person', 'EventUpdates', '$stateParams', 'Setting', '$log', '$translate',
  82. function ($scope, $state, Event, Person, EventUpdates, $stateParams, Setting, $log, $translate) {
  83. $scope.personsOrder = ["name", "surname"];
  84. $scope.countAttendees = 0;
  85. $scope.message = {};
  86. $scope.event = {};
  87. $scope.event.persons = [];
  88. $scope.customFields = Setting.query({setting: 'person_custom_field',
  89. in_event_details: true});
  90. if ($stateParams.id) {
  91. $scope.event = Event.get($stateParams, function() {
  92. $scope.$watchCollection(function() {
  93. return $scope.event.persons;
  94. }, function(prev, old) {
  95. $scope.calcAttendees();
  96. }
  97. );
  98. });
  99. $scope.allPersons = Person.all();
  100. if ($state.is('event.info')) {
  101. // Handle WebSocket connection used to update the list of persons.
  102. $scope.EventUpdates = EventUpdates;
  103. $scope.EventUpdates.open();
  104. $scope.$watchCollection(function() {
  105. return $scope.EventUpdates.data;
  106. }, function(prev, old) {
  107. if (!($scope.EventUpdates.data && $scope.EventUpdates.data.update)) {
  108. return;
  109. }
  110. var data = $scope.EventUpdates.data.update;
  111. var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
  112. return data.person_id == el.person_id;
  113. });
  114. if (person_idx != -1) {
  115. $log.debug('person_id ' + data.person_id + ' found');
  116. } else {
  117. $log.debug('person_id ' + data.person_id + ' not found');
  118. }
  119. if (data.action == 'update' && person_idx != -1 && $scope.event.persons[person_idx] != data.person) {
  120. $scope.event.persons[person_idx] = data.person;
  121. } else if (data.action == 'add' && person_idx == -1) {
  122. $scope.event.persons.push(data.person);
  123. }
  124. }
  125. );
  126. }
  127. }
  128. $scope.updateOrded = function(key) {
  129. var new_order = [key];
  130. var inv_key;
  131. if (key && key[0] === '-') {
  132. inv_key = key.substring(1);
  133. } else {
  134. inv_key = '-' + key;
  135. }
  136. angular.forEach($scope.personsOrder,
  137. function(value, idx) {
  138. if (value !== key && value !== inv_key) {
  139. new_order.push(value)
  140. }
  141. }
  142. );
  143. $scope.personsOrder = new_order;
  144. };
  145. // store a new Event or update an existing one
  146. $scope.save = function() {
  147. // avoid override of event.persons list.
  148. var this_event = angular.copy($scope.event);
  149. if (this_event.persons) {
  150. delete this_event.persons;
  151. }
  152. if (this_event.id === undefined) {
  153. $scope.event = Event.save(this_event);
  154. } else {
  155. $scope.event = Event.update(this_event);
  156. }
  157. $scope.eventForm.$setPristine(false);
  158. };
  159. $scope.calcAttendees = function() {
  160. if (!($scope.event && $scope.event.persons)) {
  161. return;
  162. }
  163. var attendees = 0;
  164. angular.forEach($scope.event.persons, function(value, key) {
  165. if (value.attended) {
  166. attendees += 1;
  167. }
  168. });
  169. $scope.countAttendees = attendees;
  170. };
  171. $scope._addPerson = function(person_data) {
  172. var original_data = angular.copy(person_data);
  173. person_data.person_id = person_data._id;
  174. person_data._id = $stateParams.id;
  175. Event.addPerson(person_data, function() {
  176. $scope.event.persons.push(person_data);
  177. $scope.setPersonAttribute(person_data, 'attended', true, function() {
  178. var idx = $scope.allPersons.indexOf(original_data);
  179. if (idx != -1) {
  180. $scope.allPersons.splice(idx, 1);
  181. } else {
  182. $scope.allPersons = Person.all();
  183. }
  184. $translate('{{person_name}} {{person_surname}} successfully added to event {{event_title}}',
  185. {person_name: person_data.name, person_surname: person_data.surname, event_title: $scope.event.title}).then(
  186. function (translation) {
  187. $scope.showMessage({message: translation});
  188. }
  189. );
  190. });
  191. });
  192. $scope.query = '';
  193. };
  194. $scope.fastAddPerson = function(person, isNew) {
  195. $log.debug('EventDetailsCtrl.fastAddPerson.person:');
  196. $log.debug(person);
  197. if (isNew) {
  198. var personObj = new Person(person);
  199. personObj.$save(function(p) {
  200. $scope._addPerson(angular.copy(p));
  201. });
  202. } else {
  203. $scope._addPerson(angular.copy(person));
  204. }
  205. };
  206. $scope.setPersonAttribute = function(person, key, value, callback) {
  207. $log.debug('EventDetailsCtrl.setPersonAttribute.event_id: ' + $stateParams.id);
  208. $log.debug('EventDetailsCtrl.setPersonAttribute.person_id: ' + person.person_id);
  209. $log.debug('EventDetailsCtrl.setPersonAttribute.key: ' + key + ' value: ' + value);
  210. var data = {_id: $stateParams.id, person_id: person.person_id};
  211. data[key] = value;
  212. Event.updatePerson(data,
  213. function(data) {
  214. if (!(data && data.person_id && data.person)) {
  215. return;
  216. }
  217. var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
  218. return data.person_id == el.person_id;
  219. });
  220. if (person_idx == -1) {
  221. $log.warn('unable to find person_id ' + person_id);
  222. return;
  223. }
  224. if ($scope.event.persons[person_idx] != data.person) {
  225. $scope.event.persons[person_idx] = data.person;
  226. }
  227. if (callback) {
  228. callback(data);
  229. }
  230. if (key === 'attended') {
  231. var msg = {};
  232. if (value) {
  233. msg.message = '' + person.name + ' ' + person.surname + ' successfully added to event ' + $scope.event.title;
  234. } else {
  235. msg.message = '' + person.name + ' ' + person.surname + ' successfully removed from event ' + $scope.event.title;
  236. msg.isError = true;
  237. }
  238. $scope.showMessage(msg);
  239. }
  240. });
  241. };
  242. $scope.setPersonAttributeAndRefocus = function(person, key, value) {
  243. $scope.setPersonAttribute(person, key, value);
  244. $scope.query = '';
  245. };
  246. $scope.removeAttendee = function(person) {
  247. Event.deletePerson({
  248. _id: $stateParams.id,
  249. person_id: person.person_id
  250. },
  251. function(data) {
  252. $scope.event.persons = data;
  253. $scope.allPersons = Person.all();
  254. });
  255. };
  256. $scope.showMessage = function(cfg) {
  257. $scope.message.show(cfg);
  258. };
  259. $scope.$on('$destroy', function() {
  260. $scope.EventUpdates && $scope.EventUpdates.close();
  261. });
  262. }]
  263. );
  264. eventManControllers.controller('PersonsListCtrl', ['$scope', 'Person', 'Setting', '$modal', '$translate', '$rootScope',
  265. function ($scope, Person, Setting, $modal, $translate, $rootScope) {
  266. $scope.persons = Person.all();
  267. $scope.personsOrder = ["name", "surname"];
  268. $scope.customFields = Setting.query({setting: 'person_custom_field',
  269. in_persons_list: true});
  270. $scope.confirm_delete = 'Do you really want to delete this person?';
  271. $rootScope.$on('$translateChangeSuccess', function () {
  272. $translate('Do you really want to delete this person?').then(function (translation) {
  273. $scope.confirm_delete = translation;
  274. });
  275. });
  276. $scope.updateOrded = function(key) {
  277. var new_order = [key];
  278. var inv_key;
  279. if (key && key[0] === '-') {
  280. inv_key = key.substring(1);
  281. } else {
  282. inv_key = '-' + key;
  283. }
  284. angular.forEach($scope.personsOrder,
  285. function(value, idx) {
  286. if (value !== key && value !== inv_key) {
  287. new_order.push(value)
  288. }
  289. }
  290. );
  291. $scope.personsOrder = new_order;
  292. };
  293. $scope.setAttribute = function(person, key, value) {
  294. var data = {_id: person._id};
  295. data[key] = value;
  296. Person.update(data, function() {
  297. $scope.persons = Person.all();
  298. });
  299. };
  300. $scope.remove = function(_id) {
  301. var modalInstance = $modal.open({
  302. scope: $scope,
  303. templateUrl: 'modal-confirm-action.html',
  304. controller: 'ModalConfirmInstanceCtrl',
  305. resolve: {
  306. message: function() { return $scope.confirm_delete; }
  307. }
  308. });
  309. modalInstance.result.then(function() {
  310. console.debug('here');
  311. Person.remove({'id': _id}, function() {
  312. $scope.persons = Person.all();
  313. }
  314. );
  315. });
  316. };
  317. }]
  318. );
  319. eventManControllers.controller('PersonDetailsCtrl', ['$scope', '$stateParams', 'Person', 'Event', 'Setting', '$log',
  320. function ($scope, $stateParams, Person, Event, Setting, $log) {
  321. $scope.personsOrderProp = 'name';
  322. $scope.eventsOrderProp = '-begin-date';
  323. $scope.addToEvent = '';
  324. $scope.customFields = Setting.query({setting: 'person_custom_field',
  325. in_persons_list: true});
  326. if ($stateParams.id) {
  327. $scope.person = Person.get($stateParams);
  328. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  329. } else {
  330. $scope.events = Event.all();
  331. }
  332. // store a new Person or update an existing one
  333. $scope.save = function() {
  334. if ($scope.person.id === undefined) {
  335. $scope.person = new Person($scope.person);
  336. $scope.person.$save(function(person) {
  337. if ($scope.addToEvent) {
  338. var data = angular.copy(person);
  339. data.person_id = data._id;
  340. data._id = $scope.addToEvent;
  341. data.attended = false;
  342. Event.addPerson(data);
  343. }
  344. });
  345. } else {
  346. $scope.person = Person.update($scope.person, function(data) {
  347. if ($scope.addToEvent) {
  348. var data = angular.copy($scope.person);
  349. data._id = $scope.addToEvent;
  350. data.person_id = $scope.person._id;
  351. data.attended = false;
  352. Event.addPerson(data);
  353. }
  354. });
  355. }
  356. $scope.personForm.$setPristine(false);
  357. };
  358. $scope.setPersonAttributeAtEvent = function(evnt, key, value) {
  359. var attrs = {_id: evnt._id, person_id: $stateParams.id};
  360. attrs[key] = value;
  361. Event.updatePerson(attrs,
  362. function(data) {
  363. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  364. }
  365. );
  366. };
  367. $scope.switchRegistered = function(evnt, person, add) {
  368. $log.debug('PersonDetailsCtrl.switchRegistered.event_id: ' + evnt._id);
  369. $log.debug('PersonDetailsCtrl.switchRegistered.person_id: ' + person._id);
  370. $log.debug('PersonDetailsCtrl.switchRegistered.add: ' + add);
  371. if (add) {
  372. var data = angular.copy(person);
  373. data._id = evnt._id;
  374. data.person_id = person._id;
  375. data.attended = false;
  376. Event.addPerson(data,
  377. function(data) {
  378. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  379. }
  380. );
  381. } else {
  382. Event.deletePerson({_id: evnt._id, person_id: person._id},
  383. function(data) {
  384. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  385. }
  386. );
  387. }
  388. };
  389. }]
  390. );
  391. eventManControllers.controller('FileUploadCtrl', ['$scope', '$log', '$upload', 'Event',
  392. function ($scope, $log, $upload, Event) {
  393. $scope.file = null;
  394. $scope.reply = {};
  395. $scope.events = Event.all();
  396. $scope.upload = function(file, url) {
  397. $log.debug("FileUploadCtrl.upload");
  398. $upload.upload({
  399. url: url,
  400. file: file,
  401. fields: {targetEvent: $scope.targetEvent}
  402. }).progress(function(evt) {
  403. var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
  404. $log.debug('progress: ' + progressPercentage + '%');
  405. }).success(function(data, status, headers, config) {
  406. $scope.file = null;
  407. $scope.reply = angular.fromJson(data);
  408. });
  409. };
  410. }]
  411. );