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

309 lines
7.3 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-03-29 15:09:50 +01:00
$rootScope.$on('emitReload', function(event, args) {
$rootScope.$broadcast('reload');
});
2013-03-22 09:29:30 +01:00
});
2013-03-29 15:09:50 +01:00
module.controller('CategoryTreeCtrl', function($scope, $stateParams, $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-03-24 13:11:05 +01:00
$scope.SubscriptionService = SubscriptionService;
2013-03-22 20:51:22 +01:00
var unreadCount = function(category) {
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-03-22 20:51:22 +01:00
count = count + 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-03-22 07:31:15 +01:00
2013-03-22 20:51:22 +01:00
$scope.formatCategoryName = function(category) {
var count = unreadCount(category);
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
2013-03-29 15:09:50 +01:00
module.controller('FeedListCtrl', function($scope, $stateParams, $http, $route,
$window, EntryService, SettingsService) {
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-03-22 09:29:30 +01:00
$scope.name = null;
2013-03-31 18:47:17 +02:00
$scope.message = null;
$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-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-03-25 13:52:02 +01:00
EntryService.get({
2013-03-25 18:41:03 +01:00
type : $scope.selectedType,
id : $scope.selectedId,
readType : $scope.settingsService.settings.readingMode,
offset : $scope.entries.length,
limit : limit
2013-03-25 13:52:02 +01:00
}, 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-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-03-25 13:52:02 +01:00
});
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 08:55:16 +01:00
$scope.mark(entry, true);
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;
}
$scope.current = entry;
2013-03-29 08:55:16 +01:00
event.preventDefault();
event.stopPropagation();
}
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-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 = [];
$scope.busy = false;
$scope.hasMore = true;
$scope.loadMoreEntries();
});
2013-03-29 17:17:36 +01:00
});
2013-03-30 09:22:49 +01:00
module.controller('ManageUsersCtrl',
2013-03-30 18:18:47 +01:00
function($scope, $state, $location, AdminUsersService) {
2013-03-30 09:22:49 +01:00
$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
});
}
};
$scope.addUser = function() {
$state.transitionTo('admin.useradd');
};
2013-03-30 18:18:47 +01:00
$scope.back = function() {
$location.path('/');
};
2013-03-30 09:22:49 +01:00
});
module.controller('ManageUserCtrl', function($scope, $state, $stateParams,
AdminUsersService) {
$scope.user = $stateParams._id ? AdminUsersService.get({
id : $stateParams._id
2013-03-30 19:06:32 +01: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,
type: 'error'
});
};
2013-03-30 09:22:49 +01:00
$scope.cancel = function(){
$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() {
AdminUsersService.remove({id: $scope.user.id}, function() {
2013-03-30 11:37:57 +01:00
$state.transitionTo('admin.userlist');
2013-03-30 18:18:47 +01:00
},alertFunction);
2013-03-30 11:37:57 +01:00
};
2013-03-28 23:18:19 +01:00
});