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

143 lines
3.0 KiB
JavaScript
Raw Normal View History

2013-03-22 09:29:30 +01:00
var module = angular.module('commafeed.services', [ 'ngResource' ]);
2013-03-27 12:13:56 +01:00
module.factory('SubscriptionService', [
'$resource',
'$http',
2013-03-22 09:29:30 +01:00
function($resource, $http) {
2013-03-27 12:13:56 +01:00
var flatten = function(category, parentName, array) {
if (!array)
array = [];
2013-03-24 13:11:05 +01:00
array.push({
id : category.id,
2013-03-27 12:13:56 +01:00
name : category.name
+ (parentName ? (' (in ' + parentName + ')') : '')
2013-03-24 13:11:05 +01:00
});
if (category.children) {
for ( var i = 0; i < category.children.length; i++) {
flatten(category.children[i], category.name, array);
}
}
return array;
}
var actions = {
get : {
method : 'GET',
params : {
_method : ''
}
},
subscribe : {
method : 'POST',
params : {
_method : 'subscribe'
}
},
unsubscribe : {
method : 'GET',
params : {
_method : 'unsubscribe'
}
2013-03-28 10:39:03 +01:00
},
collapse : {
method : 'GET',
params : {
_method : 'collapse'
}
2013-03-24 13:11:05 +01:00
}
};
var s = {};
s.subscriptions = {};
s.flatCategories = {};
2013-03-27 12:13:56 +01:00
2013-03-24 13:11:05 +01:00
var res = $resource('rest/subscriptions/:_method', {}, actions);
2013-03-25 21:01:29 +01:00
s.init = function(callback) {
s.subscriptions = res.get(function(data) {
2013-03-24 13:11:05 +01:00
s.flatCategories = flatten(s.subscriptions);
2013-03-27 12:13:56 +01:00
if (callback)
2013-03-25 23:14:25 +01:00
callback(data);
2013-03-24 13:11:05 +01:00
});
};
2013-03-24 14:42:17 +01:00
s.subscribe = function(sub, callback) {
res.subscribe(sub, function(data) {
2013-03-27 12:13:56 +01:00
s.init();
if (callback)
callback(data);
2013-03-27 12:13:56 +01:00
});
2013-03-24 13:11:05 +01:00
};
2013-03-27 12:13:56 +01: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;
}
});
if (foundAtIndex > -1) {
node.feeds.splice(foundAtIndex, 1);
}
}
}
s.unsubscribe = function(id) {
removeSubscription(s.subscriptions, id);
2013-03-24 13:11:05 +01:00
res.unsubscribe({
id : id
2013-03-27 12:13:56 +01:00
});
2013-03-24 13:11:05 +01:00
};
2013-03-28 10:39:03 +01:00
s.collapse = res.collapse;
2013-03-24 13:11:05 +01:00
s.init();
return s;
2013-03-22 20:51:22 +01:00
} ]);
2013-03-27 12:13:56 +01:00
module.factory('EntryService', [ '$resource', '$http',
2013-03-22 20:51:22 +01:00
function($resource, $http) {
var actions = {
2013-03-23 23:14:14 +01:00
get : {
2013-03-22 20:51:22 +01:00
method : 'GET',
params : {
_method : 'get'
2013-03-22 20:51:22 +01:00
}
2013-03-23 17:17:27 +01:00
},
mark : {
method : 'GET',
params : {
_method : 'mark'
}
2013-03-22 20:51:22 +01:00
}
2013-03-23 17:17:27 +01:00
};
2013-03-27 12:13:56 +01:00
res = $resource('rest/entries/:_method', {}, actions);
2013-03-23 17:17:27 +01:00
return res;
2013-03-23 23:14:14 +01:00
} ]);
module.service('SettingsService', function($resource) {
var s = {}
s.settings = {};
$resource('rest/settings/get').get(function(data) {
s.settings = data;
});
2013-03-23 23:14:14 +01:00
s.save = function() {
$resource('rest/settings/save').save(s.settings);
};
return s;
2013-03-29 17:17:36 +01:00
});
module.service('AdminUsersService', function($resource) {
var actions = {
get : {
method : 'GET',
params : {
_method : 'get'
},
isArray : true
}
};
var res = $resource('rest/admin/:_method', {}, actions);
return res;
})