Files
commafeed/src/main/webapp/js/services.js

248 lines
4.5 KiB
JavaScript
Raw Normal View History

2013-03-22 09:29:30 +01:00
var module = angular.module('commafeed.services', [ 'ngResource' ]);
2013-03-31 11:30:52 +02:00
module.factory('SessionService', function($resource) {
2013-04-13 12:19:59 +02:00
var actions = {
get : {
method : 'GET',
params : {
_method : 'get'
}
},
save : {
method : 'POST',
params : {
_method : 'save'
}
}
};
return $resource('rest/session/:_method', {}, actions);
2013-03-31 08:17:28 +02:00
});
module.factory('SubscriptionService', function($resource, $http) {
2013-03-27 12:13:56 +01:00
2013-03-31 11:30:52 +02:00
var flatten = function(category, parentName, array) {
if (!array)
array = [];
var name = category.name;
if (parentName) {
name += (' (in ' + parentName + ')');
}
array.push({
id : category.id,
name : name
});
if (category.children) {
for ( var i = 0; i < category.children.length; i++) {
flatten(category.children[i], category.name, array);
2013-03-24 13:11:05 +01:00
}
2013-03-31 11:30:52 +02:00
}
return array;
};
var actions = {
fetch : {
method : 'GET',
params : {
_method : 'fetch'
}
},
get : {
method : 'GET',
params : {
_method : ''
}
},
subscribe : {
method : 'POST',
params : {
_method : 'subscribe'
}
},
unsubscribe : {
method : 'GET',
params : {
_method : 'unsubscribe'
}
},
2013-04-01 14:30:40 +02:00
rename : {
method : 'GET',
params : {
_method : 'rename'
}
},
2013-03-31 11:30:52 +02:00
collapse : {
method : 'GET',
params : {
_method : 'collapse'
}
},
addCategory : {
method : 'GET',
params : {
_method : 'addCategory'
}
},
deleteCategory : {
method : 'GET',
params : {
_method : 'deleteCategory'
}
}
};
var s = {};
s.subscriptions = {};
s.flatCategories = {};
2013-03-27 12:13:56 +01:00
2013-03-31 11:30:52 +02:00
var res = $resource('rest/subscriptions/:_method', {}, actions);
s.init = function(callback) {
2013-04-03 08:59:02 +02:00
res.get(function(data) {
s.subscriptions = data;
2013-03-31 11:30:52 +02:00
s.flatCategories = flatten(s.subscriptions);
if (callback)
callback(data);
});
};
s.fetch = res.fetch;
2013-04-01 14:30:40 +02:00
s.rename = res.rename;
2013-03-31 11:30:52 +02:00
s.subscribe = function(sub, callback) {
res.subscribe(sub, function(data) {
s.init();
if (callback)
callback(data);
});
};
2013-03-27 12:13:56 +01:00
2013-03-31 11:30:52 +02:00
var removeSubscription = function(node, subId) {
if (node.children) {
$.each(node.children, function(k, v) {
removeSubscription(v, subId);
});
}
if (node.feeds) {
var foundAtIndex = -1;
$.each(node.feeds, function(k, v) {
if (v.id == subId) {
foundAtIndex = k;
2013-03-27 12:13:56 +01:00
}
2013-03-31 11:30:52 +02:00
});
if (foundAtIndex > -1) {
node.feeds.splice(foundAtIndex, 1);
2013-03-27 12:13:56 +01:00
}
2013-03-31 11:30:52 +02:00
}
};
s.unsubscribe = function(id) {
removeSubscription(s.subscriptions, id);
res.unsubscribe({
id : id
});
};
s.addCategory = function(cat, callback) {
res.addCategory(cat, function(data) {
2013-03-24 13:11:05 +01:00
s.init();
2013-03-31 11:30:52 +02:00
if (callback)
callback(data);
2013-03-31 08:17:28 +02:00
});
2013-03-31 11:30:52 +02:00
};
2013-04-10 22:02:18 +02:00
s.deleteCategory = res.deleteCategory;
2013-03-31 11:30:52 +02:00
s.collapse = res.collapse;
s.init();
return s;
});
2013-03-22 20:51:22 +01:00
2013-03-31 11:30:52 +02:00
module.factory('EntryService', function($resource, $http) {
var actions = {
get : {
method : 'GET',
params : {
_method : 'get'
}
},
mark : {
method : 'GET',
params : {
_method : 'mark'
}
2013-04-03 13:33:33 +02:00
},
search : {
method : 'GET',
params : {
_method : 'search'
}
2013-03-31 11:30:52 +02:00
}
};
var res = $resource('rest/entries/:_method', {}, actions);
return res;
});
2013-03-23 23:14:14 +01:00
2013-03-29 22:18:27 +01:00
module.factory('SettingsService', function($resource) {
2013-03-31 11:30:52 +02:00
var s = {};
s.settings = {};
2013-04-04 11:36:24 +02:00
s.save = function(callback) {
$resource('rest/settings/save').save(s.settings, function(data) {
if (callback) {
callback(data);
}
});
2013-03-23 23:14:14 +01:00
};
2013-04-04 11:36:24 +02:00
s.init = function(callback) {
$resource('rest/settings/get').get(function(data) {
s.settings = data;
if (callback) {
callback(data);
}
});
};
s.init();
2013-03-23 23:14:14 +01:00
return s;
2013-03-29 17:17:36 +01:00
});
2013-03-29 22:18:27 +01:00
module.factory('AdminUsersService', function($resource) {
2013-03-29 17:17:36 +01:00
var actions = {
get : {
method : 'GET',
params : {
_method : 'get'
2013-03-30 09:22:49 +01:00
}
},
getAll : {
method : 'GET',
params : {
_method : 'getAll'
2013-03-29 17:17:36 +01:00
},
2013-03-30 09:22:49 +01:00
isArray : true
},
save : {
method : 'POST',
params : {
_method : 'save'
}
2013-03-30 11:37:57 +01:00
},
2013-03-31 11:30:52 +02:00
remove : {
2013-03-30 11:57:11 +01:00
method : 'GET',
2013-03-30 11:37:57 +01:00
params : {
_method : 'delete'
}
2013-03-29 17:17:36 +01:00
}
};
2013-03-29 22:18:27 +01:00
var res = $resource('rest/admin/users/:_method', {}, actions);
2013-03-29 17:17:36 +01:00
return res;
2013-04-05 16:31:42 +02:00
});
module.factory('AdminSettingsService', function($resource) {
var actions = {
get : {
method : 'GET',
params : {
_method : 'get'
}
},
save : {
method : 'POST',
params : {
_method : 'save'
}
}
};
var res = $resource('rest/admin/settings/:_method', {}, actions);
return res;
2013-03-29 22:18:27 +01:00
});