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

562 lines
13 KiB
JavaScript
Raw Normal View History

2013-03-21 16:22:58 +01:00
var module = angular.module('commafeed.controllers', []);
2013-03-22 09:29:30 +01:00
module.run(function($rootScope) {
2013-03-23 17:17:27 +01:00
$rootScope.$on('emitMark', function(event, args) {
// args.entry - the entry
$rootScope.$broadcast('mark', args);
2013-03-22 09:29:30 +01:00
});
2013-04-09 11:10:26 +02:00
$rootScope.$on('emitMarkAll', function(event, args) {
// args.type
// args.id
// args.read
$rootScope.$broadcast('markAll', args);
});
2013-03-29 15:09:50 +01:00
$rootScope.$on('emitReload', function(event, args) {
$rootScope.$broadcast('reload');
});
2013-03-22 09:29:30 +01:00
});
module.controller('SubscribeCtrl', function($scope, SubscriptionService) {
$scope.opts = {
backdropFade : true,
dialogFade : true
};
$scope.isOpen = false;
$scope.isOpenImport = false;
$scope.sub = {};
$scope.SubscriptionService = SubscriptionService;
$scope.open = function() {
$scope.sub = {};
$scope.isOpen = true;
};
$scope.close = function() {
$scope.isOpen = false;
};
$scope.urlChanged = function() {
2013-04-09 10:20:39 +02:00
var msg = 'Loading...';
if ($scope.sub.url && (!$scope.sub.title || $scope.sub.title == msg)) {
$scope.sub.title = msg;
SubscriptionService.fetch({
url : $scope.sub.url
}, function(data) {
$scope.sub.title = data.title;
});
}
};
$scope.save = function() {
SubscriptionService.subscribe($scope.sub);
$scope.close();
};
$scope.openImport = function() {
$scope.isOpenImport = true;
};
$scope.closeImport = function() {
$scope.isOpenImport = false;
};
$scope.uploadComplete = function(contents, completed) {
SubscriptionService.init();
$scope.closeImport();
};
$scope.cat = {};
$scope.openCategory = function() {
$scope.isOpenCategory = true;
$scope.cat = {};
};
$scope.closeCategory = function() {
$scope.isOpenCategory = false;
};
$scope.saveCategory = function() {
SubscriptionService.addCategory($scope.cat);
$scope.closeCategory();
};
});
2013-04-03 12:18:17 +02:00
module.controller('CategoryTreeCtrl', function($scope, $timeout, $stateParams,
2013-04-06 17:29:27 +02:00
$window, $location, $state, $route, SubscriptionService) {
2013-03-21 23:10:05 +01:00
2013-03-29 15:09:50 +01:00
$scope.$on('$stateChangeSuccess', function() {
$scope.selectedType = $stateParams._type;
$scope.selectedId = $stateParams._id;
});
2013-03-21 23:10:05 +01:00
2013-04-03 09:07:54 +02:00
$timeout(function refreshTree() {
SubscriptionService.init(function() {
2013-04-07 12:01:50 +02:00
$timeout(refreshTree, 15000);
2013-04-03 09:07:54 +02:00
});
2013-04-09 15:02:36 +02:00
}, 15000);
2013-04-03 09:07:54 +02:00
2013-03-24 13:11:05 +01:00
$scope.SubscriptionService = SubscriptionService;
2013-03-22 20:51:22 +01:00
2013-04-09 10:39:02 +02:00
$scope.unreadCount = function(category) {
2013-03-22 20:51:22 +01:00
var count = 0;
2013-03-31 11:30:52 +02:00
var i;
2013-03-22 20:51:22 +01:00
if (category.children) {
2013-03-31 11:30:52 +02:00
for (i = 0; i < category.children.length; i++) {
2013-04-09 10:39:02 +02:00
count = count + $scope.unreadCount(category.children[i]);
2013-03-22 07:31:15 +01:00
}
2013-03-22 20:51:22 +01:00
}
if (category.feeds) {
2013-03-31 11:30:52 +02:00
for (i = 0; i < category.feeds.length; i++) {
2013-03-22 20:51:22 +01:00
var feed = category.feeds[i];
count = count + feed.unread;
}
}
return count;
2013-03-31 11:30:52 +02:00
};
2013-04-06 17:29:27 +02:00
var rootUnreadCount = function() {
2013-04-09 10:39:02 +02:00
return $scope.unreadCount($scope.SubscriptionService.subscriptions);
2013-04-06 17:29:27 +02:00
};
$scope.$watch(rootUnreadCount, function(value) {
var label = 'CommaFeed';
if (value > 0) {
2013-04-09 08:40:36 +02:00
label = value + ' - ' + label;
2013-04-06 17:29:27 +02:00
}
$window.document.title = label;
});
2013-03-22 07:31:15 +01:00
2013-03-22 20:51:22 +01:00
$scope.formatCategoryName = function(category) {
2013-04-09 10:39:02 +02:00
var count = $scope.unreadCount(category);
2013-03-22 20:51:22 +01:00
var label = category.name;
if (count > 0) {
2013-03-31 11:30:52 +02:00
label = label + ' (' + count + ')';
2013-03-22 20:51:22 +01:00
}
return label;
2013-03-31 11:30:52 +02:00
};
2013-03-21 16:22:58 +01:00
2013-03-22 20:51:22 +01:00
$scope.formatFeedName = function(feed) {
var label = feed.name;
if (feed.unread > 0) {
2013-03-31 11:30:52 +02:00
label = label + ' (' + feed.unread + ')';
2013-03-22 20:51:22 +01:00
}
return label;
2013-03-31 11:30:52 +02:00
};
2013-03-22 09:29:30 +01:00
2013-03-22 20:51:22 +01:00
$scope.feedClicked = function(id) {
if ($scope.selectedType == 'feed' && id == $scope.selectedId) {
2013-03-29 15:09:50 +01:00
$scope.$emit('emitReload');
} else {
2013-03-29 15:09:50 +01:00
$state.transitionTo('feeds.view', {
_type : 'feed',
_id : id
});
}
2013-03-22 20:51:22 +01:00
};
$scope.categoryClicked = function(id) {
if ($scope.selectedType == 'category' && id == $scope.selectedId) {
2013-03-29 15:09:50 +01:00
$scope.$emit('emitReload');
} else {
2013-03-29 15:09:50 +01:00
$state.transitionTo('feeds.view', {
_type : 'category',
_id : id
});
}
2013-03-22 20:51:22 +01:00
};
2013-03-23 17:17:27 +01:00
var mark = function(node, entry) {
2013-03-31 11:30:52 +02:00
var i;
2013-03-22 22:20:17 +01:00
if (node.children) {
2013-03-31 11:30:52 +02:00
for (i = 0; i < node.children.length; i++) {
2013-03-23 17:17:27 +01:00
mark(node.children[i], entry);
2013-03-22 20:51:22 +01:00
}
2013-03-22 22:20:17 +01:00
}
if (node.feeds) {
2013-03-31 11:30:52 +02:00
for (i = 0; i < node.feeds.length; i++) {
2013-03-22 22:20:17 +01:00
var feed = node.feeds[i];
if (feed.id == entry.feedId) {
2013-03-23 17:17:27 +01:00
var c = entry.read ? -1 : 1;
2013-03-22 22:20:17 +01:00
feed.unread = feed.unread + c;
2013-03-22 09:29:30 +01:00
}
2013-03-22 20:51:22 +01:00
}
}
};
2013-03-22 09:29:30 +01:00
2013-03-23 17:17:27 +01:00
$scope.$on('mark', function(event, args) {
2013-03-31 11:30:52 +02:00
mark($scope.SubscriptionService.subscriptions, args.entry);
2013-03-22 20:51:22 +01:00
});
});
2013-03-21 16:22:58 +01:00
module.controller('ToolbarCtrl', function($scope, $http, $state, $stateParams,
$route, $location, SettingsService, EntryService, SubscriptionService,
SessionService) {
function totalActiveAjaxRequests() {
return ($http.pendingRequests.length + $.active);
}
$scope.session = SessionService.get();
$scope.loading = true;
$scope.$watch(totalActiveAjaxRequests, function() {
$scope.loading = (totalActiveAjaxRequests() !== 0);
});
$scope.settingsService = SettingsService;
$scope.$watch('settingsService.settings.readingMode', function(newValue,
oldValue) {
if (newValue && oldValue && newValue != oldValue) {
SettingsService.save();
}
});
2013-04-10 10:28:48 +02:00
$scope.$watch('settingsService.settings.readingOrder', function(newValue,
oldValue) {
if (newValue && oldValue && newValue != oldValue) {
SettingsService.save();
}
});
$scope.refresh = function() {
$scope.$emit('emitReload');
};
$scope.markAllAsRead = function() {
2013-04-09 11:10:26 +02:00
$scope.$emit('emitMarkAll', {
type : $stateParams._type,
id : $stateParams._id,
read : true
});
};
$scope.keywords = $stateParams._keywords;
$scope.search = function() {
if ($scope.keywords == $stateParams._keywords) {
$scope.refresh();
} else {
$state.transitionTo('feeds.search', {
_keywords : $scope.keywords
});
}
};
$scope.showButtons = function() {
return !$stateParams._keywords;
};
$scope.toAdmin = function() {
$location.path('admin');
};
$scope.toSettings = function() {
$location.path('settings');
};
});
2013-03-29 15:09:50 +01:00
module.controller('FeedListCtrl', function($scope, $stateParams, $http, $route,
2013-04-09 11:10:26 +02:00
$window, EntryService, SettingsService, SubscriptionService) {
2013-03-21 16:22:58 +01:00
2013-03-29 15:09:50 +01:00
$scope.selectedType = $stateParams._type;
$scope.selectedId = $stateParams._id;
2013-04-03 13:33:33 +02:00
$scope.keywords = $stateParams._keywords;
2013-03-22 09:29:30 +01:00
$scope.name = null;
2013-03-31 18:47:17 +02:00
$scope.message = null;
2013-04-09 09:03:52 +02:00
$scope.errorCount = 0;
2013-04-09 11:10:26 +02:00
$scope.timestamp = 0;
$scope.entries = [];
$scope.settingsService = SettingsService;
$scope.$watch('settingsService.settings.readingMode', function(newValue,
oldValue) {
if (newValue && oldValue && newValue != oldValue) {
2013-03-29 15:09:50 +01:00
$scope.$emit('emitReload');
}
2013-03-23 23:14:14 +01:00
});
2013-04-10 10:28:48 +02:00
$scope.$watch('settingsService.settings.readingOrder', function(newValue,
oldValue) {
if (newValue && oldValue && newValue != oldValue) {
$scope.$emit('emitReload');
}
});
2013-03-22 20:51:22 +01:00
$scope.limit = 10;
2013-03-25 13:52:02 +01:00
$scope.busy = false;
2013-03-25 18:41:03 +01:00
$scope.hasMore = true;
2013-03-25 13:52:02 +01:00
$scope.loadMoreEntries = function() {
if (!$scope.hasMore)
2013-03-25 18:41:03 +01:00
return;
2013-03-25 14:58:15 +01:00
if ($scope.busy)
2013-03-25 13:52:02 +01:00
return;
$scope.busy = true;
var limit = $scope.limit;
2013-03-31 11:30:52 +02:00
if ($scope.entries.length === 0) {
$window = angular.element($window);
limit = $window.height() / 33;
2013-03-31 11:30:52 +02:00
limit = parseInt(limit, 10) + 5;
}
2013-04-03 13:33:33 +02:00
var callback = function(data) {
for ( var i = 0; i < data.entries.length; i++) {
$scope.entries.push(data.entries[i]);
2013-03-30 09:22:49 +01:00
}
$scope.name = data.name;
2013-03-31 18:47:17 +02:00
$scope.message = data.message;
2013-04-09 09:03:52 +02:00
$scope.errorCount = data.errorCount;
2013-04-09 11:10:26 +02:00
$scope.timestamp = data.timestamp;
2013-03-25 13:52:02 +01:00
$scope.busy = false;
2013-03-31 11:30:52 +02:00
$scope.hasMore = data.entries.length == limit;
2013-04-03 13:33:33 +02:00
};
if (!$scope.keywords) {
EntryService.get({
type : $scope.selectedType,
id : $scope.selectedId,
readType : $scope.settingsService.settings.readingMode,
2013-04-10 10:28:48 +02:00
order : $scope.settingsService.settings.readingOrder,
2013-04-03 13:33:33 +02:00
offset : $scope.entries.length,
limit : limit
}, callback);
} else {
EntryService.search({
keywords : $scope.keywords,
offset : $scope.entries.length,
limit : limit
}, callback);
}
2013-03-31 11:30:52 +02:00
};
2013-03-23 17:56:19 +01:00
2013-03-23 17:17:27 +01:00
$scope.mark = function(entry, read) {
2013-03-22 09:29:30 +01:00
if (entry.read != read) {
2013-03-23 17:17:27 +01:00
entry.read = read;
$scope.$emit('emitMark', {
entry : entry
});
EntryService.mark({
2013-03-25 21:01:29 +01:00
type : 'entry',
id : entry.id,
read : read
2013-03-22 09:29:30 +01:00
});
}
2013-03-21 19:51:00 +01:00
};
$scope.isOpen = false;
$scope.entryClicked = function(entry, event) {
2013-03-29 06:58:13 +01:00
if (!event.ctrlKey && event.which != 2) {
if ($scope.current != entry) {
$scope.isOpen = true;
} else {
$scope.isOpen = !$scope.isOpen;
}
2013-04-07 10:41:55 +02:00
if($scope.isOpen) {
$scope.mark(entry, true);
}
$scope.current = entry;
2013-03-29 08:55:16 +01:00
event.preventDefault();
event.stopPropagation();
2013-04-07 10:41:55 +02:00
} else {
$scope.mark(entry, true);
}
2013-03-31 11:30:52 +02:00
};
2013-03-28 23:18:19 +01:00
2013-03-29 06:58:13 +01:00
var openNextEntry = function(event) {
2013-03-28 23:18:19 +01:00
var entry = null;
if ($scope.current) {
var index;
for ( var i = 0; i < $scope.entries.length; i++) {
if ($scope.current == $scope.entries[i]) {
index = i;
break;
}
}
index = index + 1;
if (index < $scope.entries.length) {
entry = $scope.entries[index];
}
} else if ($scope.entries.length > 0) {
entry = $scope.entries[0];
}
if (entry) {
2013-03-29 06:58:13 +01:00
$scope.entryClicked(entry, event);
2013-03-28 23:18:19 +01:00
}
};
2013-03-29 06:58:13 +01:00
var openPreviousEntry = function(event) {
2013-03-28 23:18:19 +01:00
var entry = null;
if ($scope.current) {
var index;
for ( var i = 0; i < $scope.entries.length; i++) {
if ($scope.current == $scope.entries[i]) {
index = i;
break;
}
}
index = index - 1;
if (index >= 0) {
entry = $scope.entries[index];
}
}
if (entry) {
2013-03-29 06:58:13 +01:00
$scope.entryClicked(entry, event);
2013-03-28 23:18:19 +01:00
}
};
2013-03-29 06:58:13 +01:00
Mousetrap.bind('space', function(e) {
2013-03-29 08:55:16 +01:00
$scope.$apply(function() {
openNextEntry(e);
2013-03-31 11:30:52 +02:00
});
2013-03-29 06:58:13 +01:00
});
Mousetrap.bind('j', function(e) {
2013-03-29 08:55:16 +01:00
$scope.$apply(function() {
openNextEntry(e);
2013-03-31 11:30:52 +02:00
});
2013-03-29 06:58:13 +01:00
});
2013-03-29 15:09:50 +01:00
2013-03-29 09:35:54 +01:00
Mousetrap.bind('shift+space', function(e) {
$scope.$apply(function() {
openPreviousEntry(e);
2013-03-31 11:30:52 +02:00
});
2013-03-29 09:35:54 +01:00
});
2013-03-29 06:58:13 +01:00
Mousetrap.bind('k', function(e) {
2013-03-29 08:55:16 +01:00
$scope.$apply(function() {
openPreviousEntry(e);
2013-03-31 11:30:52 +02:00
});
2013-03-29 06:58:13 +01:00
});
2013-04-09 11:10:26 +02:00
$scope.$on('markAll', function(event, args) {
EntryService.mark({
type : $scope.selectedType,
id : $scope.selectedId,
olderThan : $scope.timestamp,
read : true
}, function() {
SubscriptionService.init(function() {
$scope.$emit('emitReload');
});
});
});
2013-03-29 17:17:36 +01:00
2013-03-29 15:09:50 +01:00
$scope.$on('reload', function(event, args) {
$scope.name = null;
$scope.entries = [];
2013-04-09 09:03:52 +02:00
$scope.message = null;
$scope.errorCount = 0;
2013-04-09 11:10:26 +02:00
$scope.timestamp = 0;
2013-03-29 15:09:50 +01:00
$scope.busy = false;
$scope.hasMore = true;
$scope.loadMoreEntries();
});
2013-03-29 17:17:36 +01:00
});
2013-04-03 09:07:54 +02:00
module.controller('ManageUsersCtrl', function($scope, $state, $location,
AdminUsersService) {
$scope.users = AdminUsersService.getAll();
$scope.selection = [];
$scope.gridOptions = {
data : 'users',
selectedItems : $scope.selection,
multiSelect : false,
afterSelectionChange : function(item) {
$state.transitionTo('admin.useredit', {
_id : item.entity.id
});
}
};
2013-03-30 09:22:49 +01:00
2013-04-03 09:07:54 +02:00
$scope.addUser = function() {
$state.transitionTo('admin.useradd');
};
$scope.back = function() {
$location.path('/');
};
});
2013-03-30 09:22:49 +01:00
2013-04-03 13:33:33 +02:00
module.controller('ManageUserCtrl', function($scope, $state, $stateParams,
$dialog, AdminUsersService) {
2013-03-30 09:22:49 +01:00
$scope.user = $stateParams._id ? AdminUsersService.get({
id : $stateParams._id
2013-04-03 09:07:54 +02:00
}) : {
enabled : true
};
2013-03-30 09:22:49 +01:00
$scope.alerts = [];
$scope.closeAlert = function(index) {
$scope.alerts.splice(index, 1);
};
2013-03-30 18:18:47 +01:00
var alertFunction = function(data) {
$scope.alerts.push({
msg : data.data,
2013-04-03 09:07:54 +02:00
type : 'error'
2013-03-30 18:18:47 +01:00
});
};
2013-03-30 09:22:49 +01:00
2013-04-03 09:07:54 +02:00
$scope.cancel = function() {
2013-03-30 09:22:49 +01:00
$state.transitionTo('admin.userlist');
2013-03-31 11:30:52 +02:00
};
2013-03-30 09:22:49 +01:00
$scope.save = function() {
$scope.alerts.splice(0, $scope.alerts.length);
2013-03-30 09:22:49 +01:00
AdminUsersService.save($scope.user, function() {
$state.transitionTo('admin.userlist');
2013-03-30 18:18:47 +01:00
}, alertFunction);
2013-03-29 17:17:36 +01:00
};
2013-03-31 11:30:52 +02:00
$scope.remove = function() {
2013-04-03 12:18:17 +02:00
var title = 'Delete user';
var msg = 'Delete user ' + $scope.user.name + ' ?';
var btns = [ {
result : 'cancel',
label : 'Cancel'
}, {
result : 'ok',
label : 'OK',
cssClass : 'btn-primary'
} ];
$dialog.messageBox(title, msg, btns).open().then(function(result) {
if (result == 'ok') {
AdminUsersService.remove({
id : $scope.user.id
}, function() {
$state.transitionTo('admin.userlist');
}, alertFunction);
}
});
2013-03-30 11:37:57 +01:00
};
2013-04-04 11:36:24 +02:00
});
module.controller('SettingsCtrl', function($scope, $location, SettingsService) {
$scope.settingsService = SettingsService;
2013-04-04 13:13:08 +02:00
$scope.$watch('settingsService.settings', function(value) {
$scope.settings = angular.copy(value);
});
2013-04-04 11:36:24 +02:00
$scope.codeMirrorConfig = {
mode : 'css',
lineNumbers : true
};
$scope.cancel = function() {
SettingsService.init(function() {
$location.path('/');
});
};
$scope.save = function() {
2013-04-04 13:13:08 +02:00
SettingsService.settings = $scope.settings;
2013-04-04 11:36:24 +02:00
SettingsService.save(function() {
window.location.href = window.location.href.substring(0,
window.location.href.lastIndexOf('#'));
2013-04-04 11:36:24 +02:00
});
};
2013-04-05 16:31:42 +02:00
});
2013-04-06 17:29:27 +02:00
module.controller('ManageSettingsCtrl', function($scope, $location,
AdminSettingsService) {
2013-04-05 16:31:42 +02:00
$scope.settings = AdminSettingsService.get();
2013-04-06 17:29:27 +02:00
2013-04-05 16:31:42 +02:00
$scope.cancel = function() {
$location.path('/');
};
$scope.save = function() {
AdminSettingsService.save({}, $scope.settings, function() {
$location.path('/');
});
};
2013-03-28 23:18:19 +01:00
});