controllers.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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.persons)) {
  108. return;
  109. }
  110. $scope.event.persons = $scope.EventUpdates.data.persons;
  111. }
  112. );
  113. }
  114. }
  115. $scope.updateOrded = function(key) {
  116. var new_order = [key];
  117. var inv_key;
  118. if (key && key[0] === '-') {
  119. inv_key = key.substring(1);
  120. } else {
  121. inv_key = '-' + key;
  122. }
  123. angular.forEach($scope.personsOrder,
  124. function(value, idx) {
  125. if (value !== key && value !== inv_key) {
  126. new_order.push(value)
  127. }
  128. }
  129. );
  130. $scope.personsOrder = new_order;
  131. };
  132. // store a new Event or update an existing one
  133. $scope.save = function() {
  134. // avoid override of event.persons list.
  135. var this_event = angular.copy($scope.event);
  136. if (this_event.persons) {
  137. delete this_event.persons;
  138. }
  139. if (this_event.id === undefined) {
  140. $scope.event = Event.save(this_event);
  141. } else {
  142. $scope.event = Event.update(this_event);
  143. }
  144. $scope.eventForm.$setPristine(false);
  145. };
  146. $scope.calcAttendees = function() {
  147. if (!($scope.event && $scope.event.persons)) {
  148. return;
  149. }
  150. var attendees = 0;
  151. angular.forEach($scope.event.persons, function(value, key) {
  152. if (value.attended) {
  153. attendees += 1;
  154. }
  155. });
  156. $scope.countAttendees = attendees;
  157. };
  158. $scope._addPerson = function(person_data) {
  159. var original_data = angular.copy(person_data);
  160. person_data.person_id = person_data._id;
  161. person_data._id = $stateParams.id;
  162. Event.addPerson(person_data, function() {
  163. // This could be improved adding it only locally.
  164. //$scope.event.persons.push(person_data);
  165. $scope.setPersonAttribute(person_data, 'attended', true, function() {
  166. Event.get($stateParams, function(data) {
  167. $scope.event.persons = angular.fromJson(data).persons;
  168. });
  169. var idx = $scope.allPersons.indexOf(original_data);
  170. if (idx != -1) {
  171. $scope.allPersons.splice(idx, 1);
  172. } else {
  173. $scope.allPersons = Person.all();
  174. }
  175. $scope.newPerson = {};
  176. $translate('{{person_name}} {{person_surname}} successfully added to event {{event_title}}',
  177. {person_name: person_data.name, person_surname: person_data.surname, event_title: $scope.event.title}).then(
  178. function (translation) {
  179. $scope.showMessage({message: translation});
  180. }
  181. );
  182. });
  183. });
  184. $scope.query = '';
  185. };
  186. $scope.fastAddPerson = function(person, isNew) {
  187. $log.debug('EventDetailsCtrl.fastAddPerson.person:');
  188. $log.debug(person);
  189. if (isNew) {
  190. var personObj = new Person(person);
  191. personObj.$save(function(p) {
  192. $scope._addPerson(angular.copy(p));
  193. });
  194. } else {
  195. $scope._addPerson(angular.copy(person));
  196. }
  197. };
  198. $scope.setPersonAttribute = function(person, key, value, callback) {
  199. $log.debug('EventDetailsCtrl.setPersonAttribute.event_id: ' + $stateParams.id);
  200. $log.debug('EventDetailsCtrl.setPersonAttribute.person_id: ' + person.person_id);
  201. $log.debug('EventDetailsCtrl.setPersonAttribute.key: ' + key + ' value: ' + value);
  202. var data = {_id: $stateParams.id, person_id: person.person_id};
  203. data[key] = value;
  204. Event.updatePerson(data,
  205. function(data) {
  206. $scope.event.persons = data;
  207. if (callback) {
  208. callback(data);
  209. }
  210. if (key === 'attended') {
  211. var msg = {};
  212. if (value) {
  213. msg.message = '' + person.name + ' ' + person.surname + ' successfully added to event ' + $scope.event.title;
  214. } else {
  215. msg.message = '' + person.name + ' ' + person.surname + ' successfully removed from event ' + $scope.event.title;
  216. msg.isError = true;
  217. }
  218. $scope.showMessage(msg);
  219. }
  220. });
  221. };
  222. $scope.setPersonAttributeAndRefocus = function(person, key, value) {
  223. $scope.setPersonAttribute(person, key, value);
  224. $scope.query = '';
  225. };
  226. $scope.removeAttendee = function(person) {
  227. Event.deletePerson({
  228. _id: $stateParams.id,
  229. person_id: person.person_id
  230. },
  231. function(data) {
  232. $scope.event.persons = data;
  233. $scope.allPersons = Person.all();
  234. });
  235. };
  236. $scope.showMessage = function(cfg) {
  237. $scope.message.show(cfg);
  238. };
  239. $scope.$on('$destroy', function() {
  240. $scope.EventUpdates && $scope.EventUpdates.close();
  241. });
  242. }]
  243. );
  244. eventManControllers.controller('PersonsListCtrl', ['$scope', 'Person', 'Setting', '$modal', '$translate', '$rootScope',
  245. function ($scope, Person, Setting, $modal, $translate, $rootScope) {
  246. $scope.persons = Person.all();
  247. $scope.personsOrder = ["name", "surname"];
  248. $scope.customFields = Setting.query({setting: 'person_custom_field',
  249. in_persons_list: true});
  250. $scope.confirm_delete = 'Do you really want to delete this person?';
  251. $rootScope.$on('$translateChangeSuccess', function () {
  252. $translate('Do you really want to delete this person?').then(function (translation) {
  253. $scope.confirm_delete = translation;
  254. });
  255. });
  256. $scope.updateOrded = function(key) {
  257. var new_order = [key];
  258. var inv_key;
  259. if (key && key[0] === '-') {
  260. inv_key = key.substring(1);
  261. } else {
  262. inv_key = '-' + key;
  263. }
  264. angular.forEach($scope.personsOrder,
  265. function(value, idx) {
  266. if (value !== key && value !== inv_key) {
  267. new_order.push(value)
  268. }
  269. }
  270. );
  271. $scope.personsOrder = new_order;
  272. };
  273. $scope.setAttribute = function(person, key, value) {
  274. var data = {_id: person._id};
  275. data[key] = value;
  276. Person.update(data, function() {
  277. $scope.persons = Person.all();
  278. });
  279. };
  280. $scope.remove = function(_id) {
  281. var modalInstance = $modal.open({
  282. scope: $scope,
  283. templateUrl: 'modal-confirm-action.html',
  284. controller: 'ModalConfirmInstanceCtrl',
  285. resolve: {
  286. message: function() { return $scope.confirm_delete; }
  287. }
  288. });
  289. modalInstance.result.then(function() {
  290. console.debug('here');
  291. Person.remove({'id': _id}, function() {
  292. $scope.persons = Person.all();
  293. }
  294. );
  295. });
  296. };
  297. }]
  298. );
  299. eventManControllers.controller('PersonDetailsCtrl', ['$scope', '$stateParams', 'Person', 'Event', 'Setting', '$log',
  300. function ($scope, $stateParams, Person, Event, Setting, $log) {
  301. $scope.personsOrderProp = 'name';
  302. $scope.eventsOrderProp = '-begin-date';
  303. $scope.addToEvent = '';
  304. $scope.customFields = Setting.query({setting: 'person_custom_field',
  305. in_persons_list: true});
  306. if ($stateParams.id) {
  307. $scope.person = Person.get($stateParams);
  308. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  309. } else {
  310. $scope.events = Event.all();
  311. }
  312. // store a new Person or update an existing one
  313. $scope.save = function() {
  314. if ($scope.person.id === undefined) {
  315. $scope.person = new Person($scope.person);
  316. $scope.person.$save(function(person) {
  317. if ($scope.addToEvent) {
  318. var data = angular.copy(person);
  319. data.person_id = data._id;
  320. data._id = $scope.addToEvent;
  321. data.attended = false;
  322. Event.addPerson(data);
  323. }
  324. });
  325. } else {
  326. $scope.person = Person.update($scope.person, function(data) {
  327. if ($scope.addToEvent) {
  328. var data = angular.copy($scope.person);
  329. data._id = $scope.addToEvent;
  330. data.person_id = $scope.person._id;
  331. data.attended = false;
  332. Event.addPerson(data);
  333. }
  334. });
  335. }
  336. $scope.personForm.$setPristine(false);
  337. };
  338. $scope.setPersonAttributeAtEvent = function(evnt, key, value) {
  339. var attrs = {_id: evnt._id, person_id: $stateParams.id};
  340. attrs[key] = value;
  341. Event.updatePerson(attrs,
  342. function(data) {
  343. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  344. }
  345. );
  346. };
  347. $scope.switchRegistered = function(evnt, person, add) {
  348. $log.debug('PersonDetailsCtrl.switchRegistered.event_id: ' + evnt._id);
  349. $log.debug('PersonDetailsCtrl.switchRegistered.person_id: ' + person._id);
  350. $log.debug('PersonDetailsCtrl.switchRegistered.add: ' + add);
  351. if (add) {
  352. var data = angular.copy(person);
  353. data._id = evnt._id;
  354. data.person_id = person._id;
  355. data.attended = false;
  356. Event.addPerson(data,
  357. function(data) {
  358. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  359. }
  360. );
  361. } else {
  362. Event.deletePerson({_id: evnt._id, person_id: person._id},
  363. function(data) {
  364. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  365. }
  366. );
  367. }
  368. };
  369. }]
  370. );
  371. eventManControllers.controller('FileUploadCtrl', ['$scope', '$log', '$upload', 'Event',
  372. function ($scope, $log, $upload, Event) {
  373. $scope.file = null;
  374. $scope.reply = {};
  375. $scope.events = Event.all();
  376. $scope.upload = function(file, url) {
  377. $log.debug("FileUploadCtrl.upload");
  378. $upload.upload({
  379. url: url,
  380. file: file,
  381. fields: {targetEvent: $scope.targetEvent}
  382. }).progress(function(evt) {
  383. var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
  384. $log.debug('progress: ' + progressPercentage + '%');
  385. }).success(function(data, status, headers, config) {
  386. $scope.file = null;
  387. $scope.reply = angular.fromJson(data);
  388. });
  389. };
  390. }]
  391. );