services.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. 'use strict';
  2. /* Services that are used to interact with the backend. */
  3. var eventManServices = angular.module('eventManServices', ['ngResource']);
  4. /* Modify, in place, an object to convert datetime. */
  5. function convert_dates(obj) {
  6. if (obj['begin-date']) {
  7. obj['begin-date'] = obj['begin_date'] = obj['begin-date'].getTime();
  8. }
  9. if (obj['end-date']) {
  10. obj['end-date'] = obj['end_date'] = obj['end-date'].getTime();
  11. }
  12. return obj;
  13. }
  14. eventManServices.factory('Event', ['$resource', '$rootScope',
  15. function($resource, $rootScope) {
  16. return $resource('events/:id', {id: '@_id', person_id: '@person_id'}, {
  17. all: {
  18. method: 'GET',
  19. isArray: true,
  20. transformResponse: function(data, headers) {
  21. data = angular.fromJson(data);
  22. angular.forEach(data.events || [], function(event_, event_idx) {
  23. convert_dates(event_);
  24. });
  25. return data.events;
  26. }
  27. },
  28. get: {method: 'GET',
  29. transformResponse: function(data, headers) {
  30. data = angular.fromJson(data);
  31. convert_dates(data);
  32. // strip empty keys.
  33. angular.forEach(data.persons || [], function(person, person_idx) {
  34. angular.forEach(person, function(value, key) {
  35. if (value === "") {
  36. delete person[key];
  37. }
  38. });
  39. });
  40. return data;
  41. }
  42. },
  43. update: {method: 'PUT'},
  44. updatePerson: {
  45. method: 'PUT',
  46. isArray: false,
  47. url: 'events/:id/persons/:person_id',
  48. params: {uuid: $rootScope.app_uuid},
  49. transformResponse: function(data, headers) {
  50. return angular.fromJson(data);
  51. }
  52. },
  53. addPerson: {
  54. method: 'POST',
  55. isArray: false,
  56. url: 'events/:id/persons/:person_id',
  57. params: {uuid: $rootScope.app_uuid},
  58. transformResponse: function(data, headers) {
  59. return angular.fromJson(data);
  60. }
  61. },
  62. deletePerson: {
  63. method: 'DELETE',
  64. isArray: false,
  65. url: 'events/:_id/persons/:person_id',
  66. params: {uuid: $rootScope.app_uuid},
  67. transformResponse: function(data, headers) {
  68. return angular.fromJson(data);
  69. }
  70. }
  71. });
  72. }]
  73. );
  74. eventManServices.factory('Person', ['$resource',
  75. function($resource) {
  76. return $resource('persons/:id', {id: '@_id'}, {
  77. all: {
  78. method: 'GET',
  79. isArray: true,
  80. transformResponse: function(data, headers) {
  81. return angular.fromJson(data).persons;
  82. }
  83. },
  84. update: {method: 'PUT'},
  85. getEvents: {
  86. method: 'GET',
  87. url: 'persons/:_id/events',
  88. isArray: true,
  89. transformResponse: function(data, headers) {
  90. data = angular.fromJson(data);
  91. angular.forEach(data.events || [], function(event_, event_idx) {
  92. convert_dates(event_);
  93. });
  94. return data.events;
  95. }
  96. }
  97. });
  98. }]
  99. );
  100. eventManServices.factory('Setting', ['$resource',
  101. function($resource) {
  102. return $resource('settings/', {}, {
  103. query: {
  104. method: 'GET',
  105. isArray: true,
  106. transformResponse: function(data, headers) {
  107. return angular.fromJson(data).settings;
  108. }
  109. },
  110. update: {method: 'PUT'}
  111. });
  112. }]
  113. );
  114. /* WebSocket collection used to update the list of persons of an Event. */
  115. eventManApp.factory('EventUpdates', ['$websocket', '$location', '$log',
  116. function($websocket, $location, $log) {
  117. var dataStream = null;
  118. var data = {};
  119. var methods = {
  120. data: data,
  121. close: function() {
  122. $log.debug('close WebSocket connection');
  123. dataStream.close();
  124. },
  125. open: function() {
  126. $log.debug('open WebSocket connection');
  127. dataStream && dataStream.close();
  128. var proto = $location.protocol() == 'https' ? 'wss' : 'ws';
  129. dataStream = $websocket(proto + '://' + $location.host() + ':' + $location.port() +
  130. '/ws/' + $location.path() + '/updates');
  131. dataStream.onMessage(function(message) {
  132. $log.debug('EventUpdates message received');
  133. data.update = angular.fromJson(message.data);
  134. });
  135. }
  136. };
  137. return methods;
  138. }]
  139. );