controllers.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. 'use strict';
  2. /* Controllers; their method are available where specified with the ng-controller
  3. * directive or for a given route (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.orderProp = 'begin-datetime';
  37. $scope.remove = function(_id) {
  38. Event.remove({'id': _id}, function() {
  39. $scope.events = Event.all();
  40. });
  41. };
  42. }]
  43. );
  44. eventManControllers.controller('EventDetailsCtrl', ['$scope', 'Event', '$stateParams', '$log',
  45. function ($scope, Event, $stateParams, $log) {
  46. if ($stateParams.id) {
  47. $scope.event = Event.get($stateParams);
  48. }
  49. // store a new Event or update an existing one
  50. $scope.save = function() {
  51. // avoid override of event.persons list.
  52. var this_event = angular.copy($scope.event);
  53. if (this_event.persons) {
  54. delete this_event.persons;
  55. }
  56. if (this_event.id === undefined) {
  57. $scope.event = Event.save(this_event);
  58. } else {
  59. $scope.event = Event.update(this_event);
  60. }
  61. $scope.eventForm.$dirty = false;
  62. };
  63. $scope.updateAttendee = function(person, attended) {
  64. $log.debug('EventDetailsCtrl.event_id: ' + $stateParams.id);
  65. $log.debug('EventDetailsCtrl.person_id: ' + person.person_id);
  66. $log.debug('EventDetailsCtrl.attended: ' + attended);
  67. Event.personAttended({
  68. _id: $stateParams.id,
  69. person_id: person.person_id,
  70. 'persons.$.attended': attended
  71. },
  72. function(data) {
  73. $log.debug('EventDetailsCtrl.personAttended.data');
  74. $log.debug(data);
  75. $scope.event.persons = data;
  76. });
  77. };
  78. $scope.removeAttendee = function(person) {
  79. Event.deleteAttendee({
  80. _id: $stateParams.id,
  81. person_id: person.person_id
  82. },
  83. function(data) {
  84. $scope.event.persons = data;
  85. });
  86. };
  87. }]
  88. );
  89. eventManControllers.controller('PersonsListCtrl', ['$scope', 'Person',
  90. function ($scope, Person) {
  91. $scope.persons = Person.all();
  92. $scope.orderProp = 'name';
  93. $scope.remove = function(_id) {
  94. Person.remove({'id': _id}, function() {
  95. $scope.persons = Person.all();
  96. });
  97. };
  98. }]
  99. );
  100. eventManControllers.controller('PersonDetailsCtrl', ['$scope', '$stateParams', 'Person', 'Event', '$log',
  101. function ($scope, $stateParams, Person, Event, $log) {
  102. if ($stateParams.id) {
  103. $scope.person = Person.get($stateParams);
  104. Person.getEvents($stateParams, function(data) {
  105. $scope.events = data;
  106. });
  107. }
  108. // store a new Person or update an existing one
  109. $scope.save = function() {
  110. if ($scope.person.id === undefined) {
  111. $scope.person = Person.save($scope.person);
  112. } else {
  113. $scope.person = Person.update($scope.person);
  114. }
  115. };
  116. $scope.updateAttendee = function(event, attended) {
  117. $log.debug('PersonDetailsCtrl.event_id: ' + $stateParams.id);
  118. $log.debug('PersonDetailsCtrl.event_id: ' + event.event_id);
  119. $log.debug('PersonDetailsCtrl.attended: ' + attended);
  120. Event.personAttended({
  121. _id: event._id,
  122. person_id: $stateParams.id,
  123. 'persons.$.attended': attended
  124. },
  125. function(data) {
  126. Person.getEvents($stateParams, function(data) {
  127. $log.debug('PersonDetailsCtrl.personAttended.data');
  128. $log.debug(data);
  129. $scope.events = data;
  130. });
  131. }
  132. );
  133. }
  134. }]
  135. );
  136. eventManControllers.controller('ImportPersonsCtrl', ['$scope', '$log',
  137. function ($scope, $log) {
  138. $scope.ebCSVimport = function() {
  139. $log.debug("ImportPersonsCtrl");
  140. $log.debug($scope);
  141. };
  142. }]
  143. );
  144. eventManControllers.controller('FileUploadCtrl', ['$scope', '$log', '$upload', 'Event',
  145. function ($scope, $log, $upload, Event) {
  146. $scope.file = null;
  147. $scope.reply = {};
  148. $scope.events = Event.all();
  149. $scope.upload = function(file, url) {
  150. $log.debug("FileUploadCtrl.upload");
  151. $upload.upload({
  152. url: url,
  153. file: file,
  154. fields: {targetEvent: $scope.targetEvent}
  155. }).progress(function(evt) {
  156. var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
  157. $log.debug('progress: ' + progressPercentage + '%');
  158. }).success(function(data, status, headers, config) {
  159. $scope.file = null;
  160. $scope.reply = angular.fromJson(data);
  161. });
  162. };
  163. }]
  164. );