2014-09-24 22:06:12 +02:00
|
|
|
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2014-08-20 21:49:44 +02:00
|
|
|
|
2014-09-24 22:06:12 +02:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
2014-11-03 18:19:35 +01:00
|
|
|
/**
|
|
|
|
* @name Resource
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} $http - the Angular HTTP service
|
|
|
|
* @param {Object} $q - the Angular promise/deferred service
|
|
|
|
* @param {String} path - the base path of the external resource
|
|
|
|
* @param {Object} options - extra attributes to be associated to the object
|
|
|
|
*/
|
2014-11-10 21:17:36 +01:00
|
|
|
function Resource($http, $q, path, activeUser, options) {
|
2014-09-24 22:06:12 +02:00
|
|
|
angular.extend(this, {
|
|
|
|
_http: $http,
|
|
|
|
_q: $q,
|
2014-11-10 21:17:36 +01:00
|
|
|
_path: path,
|
|
|
|
_activeUser: activeUser
|
2014-09-24 22:06:12 +02:00
|
|
|
});
|
|
|
|
angular.extend(this, options);
|
2014-12-12 19:39:02 +01:00
|
|
|
// Trim trailing slash
|
|
|
|
this._path = this._path.replace(/\/$/, '');
|
2014-09-24 22:06:12 +02:00
|
|
|
}
|
|
|
|
|
2014-11-03 18:19:35 +01:00
|
|
|
/**
|
|
|
|
* @memberof Resource
|
|
|
|
* @desc The factory we'll use to register with Angular.
|
|
|
|
* @return a new Resource object
|
|
|
|
*/
|
2014-09-24 22:06:12 +02:00
|
|
|
Resource.$factory = ['$http', '$q', function($http, $q) {
|
2014-11-11 17:14:55 +01:00
|
|
|
return function(path, activeUser, options) {
|
|
|
|
return new Resource($http, $q, path, activeUser, options);
|
2014-08-20 21:49:44 +02:00
|
|
|
};
|
2014-09-24 22:06:12 +02:00
|
|
|
}];
|
|
|
|
|
2014-11-03 18:19:35 +01:00
|
|
|
/**
|
|
|
|
* @module SOGo.Common
|
|
|
|
* @desc Factory registration of Resource in Angular module.
|
|
|
|
*/
|
2014-09-24 22:06:12 +02:00
|
|
|
angular.module('SOGo.Common').factory('sgResource', Resource.$factory);
|
|
|
|
|
2014-11-10 21:17:36 +01:00
|
|
|
/**
|
|
|
|
* @function userResource
|
|
|
|
* @memberof Resource.prototype
|
|
|
|
* @desc Create a new Resource object associated to a username different than the active user.
|
|
|
|
* @param {String} uid - the user UID
|
|
|
|
* @return a new Resource object
|
|
|
|
*/
|
|
|
|
Resource.prototype.userResource = function(uid) {
|
|
|
|
var path = _.compact(this._activeUser.folderURL.split('/'));
|
|
|
|
path.splice(path.length - 1, 1, escape(uid));
|
|
|
|
|
|
|
|
return new Resource(this._http, this._q, '/' + path.join('/'), this._activeUser);
|
|
|
|
};
|
|
|
|
|
2014-11-03 18:19:35 +01:00
|
|
|
/**
|
|
|
|
* @function fetch
|
|
|
|
* @memberof Resource.prototype
|
|
|
|
* @desc Fetch resource using a specific folder, action and/or parameters.
|
|
|
|
* @param {string} folderId - the folder on which the action will be applied (ex: addressbook, calendar)
|
|
|
|
* @param {string} action - the action to be used in the URL
|
|
|
|
* @param {Object} params - Object parameters injected through the $http service
|
|
|
|
* @return a promise
|
|
|
|
*/
|
|
|
|
Resource.prototype.fetch = function(folderId, action, params) {
|
|
|
|
var deferred = this._q.defer(),
|
2014-11-10 21:17:36 +01:00
|
|
|
path = [this._path];
|
|
|
|
if (folderId) path.push(folderId.split('/'));
|
|
|
|
if (action) path.push(action);
|
|
|
|
path = _.compact(_.flatten(path)).join('/');
|
2014-09-24 22:06:12 +02:00
|
|
|
|
|
|
|
this._http({
|
|
|
|
method: 'GET',
|
2014-11-03 18:19:35 +01:00
|
|
|
url: path,
|
2014-09-24 22:06:12 +02:00
|
|
|
params: params
|
|
|
|
})
|
|
|
|
.success(deferred.resolve)
|
2015-01-08 22:11:07 +01:00
|
|
|
.error(function(data, status) {
|
|
|
|
if (status == 404)
|
|
|
|
return deferred.reject();
|
|
|
|
});
|
2014-09-24 22:06:12 +02:00
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2014-11-03 18:19:35 +01:00
|
|
|
/**
|
|
|
|
* @function newguid
|
|
|
|
* @memberof Resource.prototype
|
|
|
|
* @desc Fetch a new GUID on the specified folder ID.
|
|
|
|
* @return a promise of the new data structure
|
|
|
|
*/
|
|
|
|
Resource.prototype.newguid = function(folderId) {
|
2014-09-24 22:06:12 +02:00
|
|
|
var deferred = this._q.defer(),
|
2014-11-03 18:19:35 +01:00
|
|
|
path = this._path + '/' + folderId + '/newguid';
|
2014-09-24 22:06:12 +02:00
|
|
|
|
|
|
|
this._http
|
|
|
|
.get(path)
|
|
|
|
.success(deferred.resolve)
|
|
|
|
.error(deferred.reject);
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function create
|
2014-11-03 18:19:35 +01:00
|
|
|
* @memberof Resource.prototype
|
|
|
|
* @desc Create a new resource using a specific action (post).
|
2014-09-24 22:06:12 +02:00
|
|
|
* @param {string} action - the action to be used in the URL
|
|
|
|
* @param {string} name - the new resource's name
|
2014-11-03 18:19:35 +01:00
|
|
|
* @return a promise
|
2014-09-24 22:06:12 +02:00
|
|
|
*/
|
|
|
|
Resource.prototype.create = function(action, name) {
|
|
|
|
var deferred = this._q.defer(),
|
|
|
|
path = this._path + '/' + action;
|
|
|
|
|
|
|
|
this._http
|
|
|
|
.post(path, { name: name })
|
|
|
|
.success(deferred.resolve)
|
|
|
|
.error(deferred.reject);
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2014-11-03 18:19:35 +01:00
|
|
|
/**
|
2014-11-25 22:09:55 +01:00
|
|
|
* @function post
|
2014-11-03 18:19:35 +01:00
|
|
|
* @memberof Resource.prototype
|
2014-11-25 22:09:55 +01:00
|
|
|
* @desc Post a resource attributes on the server.
|
2014-11-03 18:19:35 +01:00
|
|
|
* @return a promise
|
|
|
|
*/
|
2014-11-25 22:09:55 +01:00
|
|
|
Resource.prototype.post = function(id, action, data) {
|
2014-09-24 22:06:12 +02:00
|
|
|
var deferred = this._q.defer(),
|
2014-10-08 20:08:54 +02:00
|
|
|
path = this._path + '/' + id + '/' + action;
|
2014-09-24 22:06:12 +02:00
|
|
|
|
|
|
|
this._http
|
2014-11-25 22:09:55 +01:00
|
|
|
.post(path, data)
|
2014-09-24 22:06:12 +02:00
|
|
|
.success(deferred.resolve)
|
|
|
|
.error(deferred.reject);
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2014-11-25 22:09:55 +01:00
|
|
|
/**
|
|
|
|
* @function save
|
|
|
|
* @memberof Resource.prototype
|
|
|
|
* @desc Save a resource attributes on the server (post /save).
|
|
|
|
* @return a promise
|
|
|
|
*/
|
|
|
|
Resource.prototype.save = function(id, newValue, options) {
|
|
|
|
var deferred = this._q.defer(),
|
|
|
|
action = (options && options.action)? options.action : 'save';
|
|
|
|
|
|
|
|
return this.post(id, action, newValue);
|
|
|
|
};
|
|
|
|
|
2014-11-03 18:19:35 +01:00
|
|
|
/**
|
|
|
|
* @function remove
|
|
|
|
* @memberof Resource.prototype
|
|
|
|
* @desc Delete a resource (get /delete).
|
|
|
|
* @return a promise
|
|
|
|
*/
|
2014-09-24 22:06:12 +02:00
|
|
|
Resource.prototype.remove = function(uid) {
|
|
|
|
var deferred = this._q.defer(),
|
|
|
|
path = this._path + '/' + uid + '/delete';
|
|
|
|
|
|
|
|
this._http
|
|
|
|
.get(path)
|
|
|
|
.success(deferred.resolve)
|
|
|
|
.error(deferred.reject);
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
2014-10-08 20:08:54 +02:00
|
|
|
|
2014-08-06 22:05:52 +02:00
|
|
|
})();
|