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

238 lines
4.8 KiB
JavaScript
Raw Normal View History

2013-03-22 09:29:30 +01:00
var module = angular.module('commafeed.services', [ 'ngResource' ]);
module.service('AnalyticsService', [ '$state', function($state) {
this.track = function(path) {
2013-04-27 06:43:07 +02:00
if (typeof ga === 'undefined') {
return;
}
2013-04-24 12:26:27 +02:00
path = path || $state.$current.url.prefix;
ga('send', 'pageview', {
page : path
});
};
} ]);
2013-05-26 13:34:31 +02:00
module.service('MobileService', [ '$state', function($state) {
2013-05-26 17:51:32 +02:00
this.leftMenu = false;
this.rightMenu = false;
2013-05-26 13:34:31 +02:00
this.toggleLeftMenu = function() {
2013-05-26 17:51:32 +02:00
this.leftMenu = !this.leftMenu;
2013-05-26 13:34:31 +02:00
$('body').toggleClass('left-menu-active');
2013-05-26 15:38:16 +02:00
};
2013-05-26 13:34:31 +02:00
this.toggleRightMenu = function() {
2013-05-26 17:51:32 +02:00
this.rightMenu = !this.rightMenu;
2013-05-26 13:34:31 +02:00
$('body').toggleClass('right-menu-active');
2013-05-26 15:38:16 +02:00
};
2013-05-26 13:34:31 +02:00
var width = (window.innerWidth > 0) ? window.innerWidth : screen.width;
this.mobile = width < 979;
}]);
2013-04-22 20:58:18 -05:00
module.factory('ProfileService', ['$resource', function($resource) {
var res = $resource('rest/user/profile/');
res.deleteAccount = $resource('rest/user/profile/deleteAccount').save;
return res;
2013-04-22 20:58:18 -05:00
}]);
2013-03-31 08:17:28 +02:00
2013-04-22 20:58:18 -05:00
module.factory('SettingsService', ['$resource', function($resource) {
2013-04-18 12:50:44 +02:00
var res = $resource('rest/user/settings');
2013-03-27 12:13:56 +01:00
2013-04-18 12:50:44 +02:00
var s = {};
s.settings = {};
s.save = function(callback) {
res.save(s.settings, function(data) {
if (callback) {
callback(data);
}
2013-03-31 11:30:52 +02:00
});
2013-04-18 12:50:44 +02:00
};
s.init = function(callback) {
res.get(function(data) {
s.settings = data;
2013-05-12 12:38:56 +02:00
moment.lang(s.settings.language || 'en');
2013-04-18 12:50:44 +02:00
if (callback) {
callback(data);
2013-03-24 13:11:05 +01:00
}
2013-04-18 12:50:44 +02:00
});
2013-03-31 11:30:52 +02:00
};
2013-04-18 12:50:44 +02:00
s.init();
return s;
2013-04-22 20:58:18 -05:00
}]);
2013-04-18 12:50:44 +02:00
2013-04-22 20:58:18 -05:00
module.factory('FeedService', ['$resource', '$http',
function($resource, $http) {
2013-03-31 11:30:52 +02:00
var actions = {
2013-04-18 12:50:44 +02:00
entries : {
method : 'GET',
params : {
_method : 'entries'
}
},
2013-03-31 11:30:52 +02:00
fetch : {
method : 'GET',
params : {
2013-04-16 12:36:36 +02:00
_method : 'fetch'
2013-03-31 11:30:52 +02:00
}
},
2013-04-18 12:50:44 +02:00
mark : {
method : 'POST',
params : {
_method : 'mark'
}
},
refresh : {
method : 'POST',
params : {
_method : 'refresh'
}
},
2013-03-31 11:30:52 +02:00
subscribe : {
method : 'POST',
params : {
2013-04-16 12:36:36 +02:00
_method : 'subscribe'
2013-03-31 11:30:52 +02:00
}
},
unsubscribe : {
method : 'POST',
2013-03-31 11:30:52 +02:00
params : {
2013-04-16 12:36:36 +02:00
_method : 'unsubscribe'
2013-03-31 11:30:52 +02:00
}
},
modify : {
method : 'POST',
2013-04-01 14:30:40 +02:00
params : {
_method : 'modify'
2013-04-01 14:30:40 +02:00
}
2013-04-18 12:50:44 +02:00
}
};
var res = $resource('rest/feed/:_method', {}, actions);
res.get = $resource('rest/feed/get/:id').get;
2013-04-18 12:50:44 +02:00
return res;
2013-04-22 20:58:18 -05:00
}]);
2013-04-18 12:50:44 +02:00
2013-04-22 20:58:18 -05:00
module.factory('CategoryService', ['$resource', '$http',
function($resource, $http) {
2013-04-18 12:50:44 +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,
2013-04-30 22:20:39 +02:00
orig: category
2013-04-18 12:50:44 +02: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 : 'get'
}
2013-04-01 14:30:40 +02:00
},
2013-04-18 12:50:44 +02:00
entries : {
method : 'GET',
params : {
_method : 'entries'
}
},
mark : {
method : 'POST',
2013-03-31 11:30:52 +02:00
params : {
2013-04-18 12:50:44 +02:00
_method : 'mark'
2013-03-31 11:30:52 +02:00
}
},
2013-04-18 12:50:44 +02:00
add : {
method : 'POST',
2013-03-31 11:30:52 +02:00
params : {
2013-04-16 12:36:36 +02:00
_method : 'add'
2013-03-31 11:30:52 +02:00
}
},
2013-04-18 12:50:44 +02:00
remove : {
method : 'POST',
2013-03-31 11:30:52 +02:00
params : {
2013-04-16 12:36:36 +02:00
_method : 'delete'
2013-04-16 12:36:18 +02:00
}
},
modify : {
method : 'POST',
2013-04-16 12:36:18 +02:00
params : {
_method : 'modify'
2013-03-31 11:30:52 +02:00
}
2013-04-18 12:50:44 +02:00
},
collapse : {
method : 'POST',
params : {
_method : 'collapse'
}
2013-03-31 11:30:52 +02:00
}
};
2013-04-18 12:50:44 +02:00
var res = $resource('rest/category/:_method', {}, actions);
res.subscriptions = {};
res.flatCategories = {};
2013-03-27 12:13:56 +01:00
2013-04-18 12:50:44 +02:00
res.init = function(callback) {
res.get(function(data) {
res.subscriptions = data;
res.flatCategories = flatten(data);
2013-03-31 11:30:52 +02:00
if (callback)
callback(data);
});
};
2013-04-18 12:50:44 +02:00
res.init();
return res;
2013-04-22 20:58:18 -05:00
}]);
2013-03-22 20:51:22 +01:00
2013-04-22 20:58:18 -05:00
module.factory('EntryService', ['$resource', '$http',
function($resource, $http) {
2013-03-31 11:30:52 +02:00
var actions = {
2013-04-18 12:50:44 +02:00
search : {
2013-03-31 11:30:52 +02:00
method : 'GET',
params : {
2013-04-18 12:50:44 +02:00
_method : 'search'
2013-03-31 11:30:52 +02:00
}
},
mark : {
method : 'POST',
2013-03-31 11:30:52 +02:00
params : {
_method : 'mark'
}
2013-04-30 11:29:02 +02:00
},
star : {
method : 'POST',
params : {
_method : 'star'
}
2013-03-31 11:30:52 +02:00
}
};
2013-04-18 12:50:44 +02:00
var res = $resource('rest/entry/:_method', {}, actions);
2013-03-31 11:30:52 +02:00
return res;
2013-04-22 20:58:18 -05:00
}]);
2013-03-23 23:14:14 +01:00
2013-04-22 20:58:18 -05:00
module.factory('AdminUsersService', ['$resource', function($resource) {
2013-04-18 12:50:44 +02:00
var res = {};
res.get = $resource('rest/admin/user/get/:id').get;
res.getAll = $resource('rest/admin/user/getAll').query;
res.save = $resource('rest/admin/user/save').save;
res.remove = $resource('rest/admin/user/delete').save;
2013-03-29 17:17:36 +01:00
return res;
2013-04-22 20:58:18 -05:00
}]);
2013-04-05 16:31:42 +02:00
2013-04-22 20:58:18 -05:00
module.factory('AdminSettingsService', ['$resource', function($resource) {
2013-04-18 12:50:44 +02:00
var res = $resource('rest/admin/settings/');
2013-04-05 16:31:42 +02:00
return res;
2013-04-26 07:40:39 +02:00
}]);
module.factory('ServerService', ['$resource', function($resource) {
var res = $resource('rest/server/get');
return res;
2013-04-22 20:58:18 -05:00
}]);