eventman/angular_app/js/services.js

181 lines
5.4 KiB
JavaScript
Raw Normal View History

2015-03-22 08:57:57 +01:00
'use strict';
2015-03-22 09:36:32 +01:00
/* Services that are used to interact with the backend. */
2015-03-21 13:21:15 +01:00
var eventManServices = angular.module('eventManServices', ['ngResource']);
2015-03-22 09:19:30 +01:00
2016-04-25 13:40:03 +02:00
/* Modify, in place, an object to convert datetime. */
function convert_dates(obj) {
if (obj['begin-date']) {
obj['begin-date'] = obj['begin_date'] = obj['begin-date'].getTime();
}
if (obj['end-date']) {
obj['end-date'] = obj['end_date'] = obj['end-date'].getTime();
}
return obj;
}
eventManServices.factory('Event', ['$resource', '$rootScope',
function($resource, $rootScope) {
2015-04-05 00:55:59 +02:00
return $resource('events/:id', {id: '@_id', person_id: '@person_id'}, {
2015-03-21 15:45:40 +01:00
all: {
method: 'GET',
isArray: true,
transformResponse: function(data, headers) {
2016-04-25 13:40:03 +02:00
data = angular.fromJson(data);
angular.forEach(data.events || [], function(event_, event_idx) {
convert_dates(event_);
});
return data.events;
2015-03-21 15:45:40 +01:00
}
},
2015-03-23 23:06:44 +01:00
get: {method: 'GET',
transformResponse: function(data, headers) {
data = angular.fromJson(data);
2016-04-25 13:40:03 +02:00
convert_dates(data);
2016-04-17 19:14:52 +02:00
// strip empty keys.
angular.forEach(data.persons || [], function(person, person_idx) {
angular.forEach(person, function(value, key) {
if (value === "") {
delete person[key];
}
});
});
2015-03-23 23:06:44 +01:00
return data;
}
},
2015-04-05 00:55:59 +02:00
update: {method: 'PUT'},
2015-04-18 19:56:00 +02:00
updatePerson: {
2015-04-05 00:55:59 +02:00
method: 'PUT',
isArray: false,
2015-04-05 00:55:59 +02:00
url: 'events/:id/persons/:person_id',
params: {uuid: $rootScope.app_uuid},
2015-04-05 00:55:59 +02:00
transformResponse: function(data, headers) {
return angular.fromJson(data);
2015-04-05 00:55:59 +02:00
}
2015-04-05 11:20:57 +02:00
},
2015-04-18 19:58:15 +02:00
addPerson: {
method: 'POST',
isArray: false,
url: 'events/:id/persons/:person_id',
params: {uuid: $rootScope.app_uuid},
transformResponse: function(data, headers) {
return angular.fromJson(data);
}
},
2015-04-18 19:58:15 +02:00
deletePerson: {
2015-04-05 11:20:57 +02:00
method: 'DELETE',
isArray: false,
2015-04-05 11:20:57 +02:00
url: 'events/:_id/persons/:person_id',
params: {uuid: $rootScope.app_uuid},
2015-04-05 11:20:57 +02:00
transformResponse: function(data, headers) {
return angular.fromJson(data);
2015-04-05 11:20:57 +02:00
}
2015-04-05 00:55:59 +02:00
}
2015-03-21 15:45:40 +01:00
});
}]
2015-03-21 13:21:15 +01:00
);
eventManServices.factory('Person', ['$resource',
2015-03-21 15:45:40 +01:00
function($resource) {
2015-03-21 20:31:36 +01:00
return $resource('persons/:id', {id: '@_id'}, {
2015-03-21 15:45:40 +01:00
all: {
method: 'GET',
2015-03-21 20:31:36 +01:00
isArray: true,
2015-03-21 15:45:40 +01:00
transformResponse: function(data, headers) {
return angular.fromJson(data).persons;
}
2015-03-21 16:48:00 +01:00
},
2015-04-04 14:15:52 +02:00
update: {method: 'PUT'},
2015-04-04 14:15:52 +02:00
getEvents: {
method: 'GET',
url: 'persons/:_id/events',
2015-04-04 14:15:52 +02:00
isArray: true,
transformResponse: function(data, headers) {
2016-04-25 13:40:03 +02:00
data = angular.fromJson(data);
angular.forEach(data.events || [], function(event_, event_idx) {
convert_dates(event_);
});
return data.events;
2015-04-18 17:33:42 +02:00
}
}
});
}]
);
eventManServices.factory('Setting', ['$resource',
function($resource) {
return $resource('settings/', {}, {
query: {
method: 'GET',
isArray: true,
transformResponse: function(data, headers) {
return angular.fromJson(data).settings;
}
},
2016-04-24 16:31:07 +02:00
update: {method: 'PUT'}
2015-04-18 17:33:42 +02:00
});
}]
);
eventManServices.factory('Info', ['$resource',
function($resource) {
return $resource('info/', {}, {
get: {method: 'GET',
isArray: false,
transformResponse: function(data, headers) {
return angular.fromJson(data).info || {};
}
}
});
}]
);
/* WebSocket collection used to update the list of persons of an Event. */
eventManApp.factory('EventUpdates', ['$websocket', '$location', '$log',
function($websocket, $location, $log) {
2015-04-26 12:42:47 +02:00
var dataStream = null;
var data = {};
var methods = {
data: data,
2015-04-26 12:42:47 +02:00
close: function() {
$log.debug('close WebSocket connection');
dataStream.close();
},
open: function() {
$log.debug('open WebSocket connection');
dataStream && dataStream.close();
var proto = $location.protocol() == 'https' ? 'wss' : 'ws';
dataStream = $websocket(proto + '://' + $location.host() + ':' + $location.port() +
'/ws/' + $location.path() + '/updates');
dataStream.onMessage(function(message) {
$log.debug('EventUpdates message received');
data.update = angular.fromJson(message.data);
});
2015-04-26 12:42:47 +02:00
}
};
return methods;
}]
);