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

321 lines
6.5 KiB
JavaScript
Raw Normal View History

2013-07-31 11:16:50 +02:00
var module = angular.module('commafeed.services', ['ngResource']);
2013-03-22 09:29:30 +01:00
2013-07-31 11:16:50 +02:00
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-07-31 11:16:50 +02:00
}]);
2013-05-26 13:34:31 +02:00
2013-07-31 11:16:50 +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
};
this.mobile = device.mobile() || device.tablet();
2013-05-26 13:34:31 +02:00
}]);
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;
var lang = s.settings.language || 'en';
if (lang === 'zh') {
lang = 'zh-cn';
2013-07-01 08:11:35 +02:00
} else if (lang === 'ms') {
lang = 'ms-my';
}
moment.lang(lang, {});
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-07-31 11:16:50 +02: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 : {
2013-07-11 09:08:39 +02:00
method : 'POST',
2013-03-31 11:30:52 +02:00
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-07-27 11:21:26 +02:00
refreshAll : {
method : 'GET',
params : {
_method : 'refreshAll'
}
},
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-07-31 11:16:50 +02:00
module.factory('CategoryService', ['$resource', '$http', function($resource, $http) {
var traverse = function(callback, category, parentName) {
callback(category, parentName);
var children = category.children;
if (children) {
2013-10-13 10:49:44 +02:00
for (var c = 0; c < children.length; c++) {
traverse(callback, children[c], category.name);
2013-04-18 12:50:44 +02:00
}
}
};
2013-07-31 11:16:50 +02:00
// flatten categories
var flatten = function(category) {
var array = [];
var callback = function(category, parentName) {
var name = category.name;
if (parentName) {
name += (' (in ' + parentName + ')');
}
array.push({
id : category.id,
2013-07-31 11:16:50 +02:00
name : name,
orig : category
});
};
traverse(callback, category);
return array;
};
// flatten feeds
var flatFeeds = function(category) {
var subs = [];
var callback = function(category) {
subs.push.apply(subs, category.feeds);
};
traverse(callback, category);
return subs;
};
2013-07-31 11:16:50 +02:00
// flatten everything
var flatAll = function(category, a) {
2013-07-31 11:16:50 +02:00
a.push([category.id, 'category']);
_.each(category.children, function(child) {
flatAll(child, a);
});
_.each(category.feeds, function(feed) {
2013-07-31 11:16:50 +02:00
a.push([feed.id, 'feed']);
});
};
2013-07-31 11:16:50 +02:00
2013-04-18 12:50:44 +02:00
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 = {};
res.feeds = [];
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);
res.feeds = flatFeeds(data);
2013-07-31 11:16:50 +02:00
res.flatAll = [];
flatAll(data, res.flatAll);
res.flatAll.splice(1, 0, ['starred', 'category']);
2013-03-31 11:30:52 +02:00
if (callback)
callback(data);
});
};
res.refresh = function(callback) {
res.get(function(data) {
_.merge(res.subscriptions, data);
if (callback)
callback(data);
});
};
2013-03-31 11:30:52 +02:00
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-07-31 11:16:50 +02: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
},
2013-07-08 01:27:58 +02:00
markMultiple : {
method : 'POST',
params : {
_method : 'markMultiple'
}
},
2013-04-30 11:29:02 +02:00
star : {
method : 'POST',
params : {
_method : 'star'
}
2013-10-13 10:49:44 +02:00
},
tag : {
method : 'POST',
params : {
_method : 'tag'
}
2013-03-31 11:30:52 +02:00
}
};
2013-04-18 12:50:44 +02:00
var res = $resource('rest/entry/:_method', {}, actions);
2013-10-13 10:49:44 +02:00
res.tags = [];
var initTags = function() {
$http.get('rest/entry/tags').success(function(data) {
res.tags = [];
res.tags.push.apply(res.tags, data);
});
};
var oldTag = res.tag;
res.tag = function(data) {
oldTag(data, function() {
initTags();
});
};
initTags();
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
}]);
2013-08-23 12:46:35 +02:00
module.factory('AdminMetricsService', ['$resource', function($resource) {
var res = $resource('rest/admin/metrics/');
return res;
}]);
2013-04-26 07:40:39 +02:00
module.factory('ServerService', ['$resource', function($resource) {
2013-07-31 11:16:50 +02:00
var res = $resource('rest/server/get');
2013-04-26 07:40:39 +02:00
return res;
2013-04-22 20:58:18 -05:00
}]);