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

169 lines
4.0 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
});
});
module.controller('CategoryTreeCtrl', function($scope, $routeParams, $location, $route,
2013-03-24 13:11:05 +01:00
SubscriptionService) {
2013-03-21 23:10:05 +01:00
$scope.$on('$routeChangeSuccess', function() {
$scope.selectedType = $routeParams._type;
$scope.selectedId = $routeParams._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;
if (category.children) {
for ( var i = 0; i < category.children.length; i++) {
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) {
for ( var i = 0; i < category.feeds.length; i++) {
var feed = category.feeds[i];
count = count + feed.unread;
}
}
return count;
}
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) {
label = label + " (" + count + ")";
}
return label;
}
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) {
label = label + " (" + feed.unread + ")";
}
return label;
}
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) {
$route.reload();
} else {
$location.path('/feeds/view/feed/' + id);
}
2013-03-22 20:51:22 +01:00
};
$scope.categoryClicked = function(id) {
if($scope.selectedType == 'category' && id == $scope.selectedId) {
$route.reload();
} else {
$location.path('/feeds/view/category/' + id);
}
2013-03-22 20:51:22 +01:00
};
2013-03-23 17:17:27 +01:00
var mark = function(node, entry) {
2013-03-22 22:20:17 +01:00
if (node.children) {
for ( var 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) {
for ( var i = 0; i < node.feeds.length; i++) {
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-24 13:11:05 +01: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-22 20:51:22 +01:00
module.controller('FeedListCtrl', function($scope, $routeParams, $http,
2013-03-23 23:14:14 +01:00
EntryService, SettingsService) {
2013-03-21 16:22:58 +01:00
2013-03-22 09:29:30 +01:00
$scope.selectedType = $routeParams._type;
$scope.selectedId = $routeParams._id;
2013-03-23 23:14:14 +01:00
$scope.settings = SettingsService.settings;
$scope.$watch('settings.readingMode', function() {
$scope.refreshList();
});
2013-03-22 20:51:22 +01:00
2013-03-26 09:54:59 +01:00
$scope.limit = 20;
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
2013-03-23 17:56:19 +01:00
$scope.refreshList = function() {
2013-03-23 23:14:14 +01:00
if ($scope.settings.readingMode) {
$scope.entryList = EntryService.get({
2013-03-25 18:41:03 +01:00
type : $scope.selectedType,
id : $scope.selectedId,
readType : $scope.settings.readingMode,
2013-03-25 14:58:15 +01:00
offset : 0,
2013-03-25 18:41:03 +01:00
limit : 30
2013-03-23 23:14:14 +01:00
});
}
2013-03-23 17:56:19 +01:00
};
2013-03-25 13:52:02 +01:00
$scope.loadMoreEntries = function() {
2013-03-25 18:41:03 +01:00
if(!$scope.hasMore)
return;
if (!$scope.entryList || !$scope.entryList.entries)
return;
2013-03-25 14:58:15 +01:00
if ($scope.busy)
2013-03-25 13:52:02 +01:00
return;
if (!$scope.settings.readingMode)
return;
$scope.busy = true;
EntryService.get({
2013-03-25 18:41:03 +01:00
type : $scope.selectedType,
id : $scope.selectedId,
readType : $scope.settings.readingMode,
2013-03-25 14:58:15 +01:00
offset : $scope.entryList.entries.length,
2013-03-25 13:52:02 +01:00
limit : $scope.limit
}, function(data) {
2013-03-25 14:58:15 +01:00
var entries = data.entries
for ( var i = 0; i < entries.length; i++) {
$scope.entryList.entries.push(entries[i]);
2013-03-25 13:52:02 +01:00
}
$scope.busy = false;
2013-03-25 18:41:03 +01:00
$scope.hasMore = entries.length == $scope.limit
2013-03-25 13:52:02 +01: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
};
2013-03-26 12:10:19 +01:00
$scope.isOpen = false
2013-03-26 12:10:19 +01:00
$scope.toggle = function(entry) {
if ($scope.current != entry) {
$scope.isOpen = true;
} else {
$scope.isOpen = !$scope.isOpen;
}
2013-03-26 12:10:19 +01:00
$scope.current = entry;
$scope.mark(entry, true);
}
2013-03-21 16:22:58 +01:00
});