summaryrefslogtreecommitdiffstats
path: root/js/news.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/news.js')
-rw-r--r--js/news.js199
1 files changed, 137 insertions, 62 deletions
diff --git a/js/news.js b/js/news.js
index 2be7499ed..22eb7195a 100644
--- a/js/news.js
+++ b/js/news.js
@@ -175,59 +175,11 @@ News={
});
return false;
},
- markItem:function(itemid, feedid) {
- var currentitem = $('#feed_items [data-id="' + itemid + '"][data-feedid="' + feedid + '"]');
- if (currentitem.hasClass('title_unread')) {
- $.post(OC.filePath('news', 'ajax', 'markitem.php'),{'itemid':itemid},function(jsondata){
- if(jsondata.status == 'success'){
- currentitem.removeClass('title_unread');
- currentitem.addClass('title_read');
-
- // decrement counter
- var counterplace = $('li.feed[data-id="'+feedid+'"]').find('.unreaditemcounter');
- var title = $('li.feed[data-id="'+feedid+'"] > a');
- var oldcount = counterplace.html();
- counterplace.empty();
- if (oldcount <= 1) {
- counterplace.removeClass('nonzero').addClass('zero');
- title.removeClass('nonzero').addClass('zero');
- }
- else {
- counterplace.append(--oldcount);
- }
- //set a timeout for this
- }
- else{
- OC.dialogs.alert(jsondata.data.message, t('news', 'Error'));
- }
- })
- };
- },
- markAllItems:function() {
- $("#feed_items li.title_unread").each(function(){
+ setAllItemsRead:function() {
+ $("#feed_items li:not(.read)").each(function(){
var itemId = $(this).data('id');
- var feedId = $(this).data('feedid');
- News.Feed.markItem(itemId, feedId);
- });
- },
- setImportant:function(isImportant, itemId, feedId){
- var $currentItem = $('#feed_items [data-id="' + itemId + '"][data-feedid="' + feedId + '"]');
- var $currentStar = $currentItem.children('.item_utils').children('ul').children('li.star');
- data = {
- isImportant: isImportant,
- itemId: itemId,
- feedId: feedId
- };
- $.post(OC.filePath('news', 'ajax', 'importantitem.php'), data, function(jsondata){
- if(jsondata.status == 'success'){
- if(isImportant){
- $currentStar.removeClass('important');
- } else {
- $currentStar.addClass('important');
- }
- } else{
- OC.dialogs.alert(jsondata.data.message, t('news', 'Error'));
- }
+ var handler = new News.ItemStatusHandler(itemId);
+ handler.setRead(true);
});
},
load:function(feedid) {
@@ -302,7 +254,123 @@ News={
}
}
- }
+ },
+ // this handler handles changes in the ui when the itemstatus changes
+ ItemStatusHandler: function(itemId){
+ var _itemId = itemId;
+ var _$currentItem = $('#feed_items li[data-id="' + itemId + '"]');
+ var _$currentItemStar = _$currentItem.children('.item_utils').children('ul').children('.star');
+ var _$currentItemKeepUnread = _$currentItem.children('.item_utils').children('.hidden_item_utils').children('.keep_unread').children('input[type=checkbox]');
+ var _feedId = _$currentItem.data('feedid');
+ var _read = _$currentItem.hasClass('read');
+ var _important = _$currentItemStar.hasClass('important');
+ var _keepUnread = _$currentItemKeepUnread.prop('checked');
+
+ /**
+ * Switches important items to unimportant and vice versa
+ */
+ var _toggleImportant = function(){
+ if(_important){
+ status = 'unimportant';
+ } else {
+ status = 'important';
+ }
+
+ var data = {
+ itemId: itemId,
+ status: status
+ };
+
+ $.post(OC.filePath('news', 'ajax', 'setitemstatus.php'), data, function(jsondata){
+ if(jsondata.status == 'success'){
+ if(_important){
+ _$currentItemStar.removeClass('important');
+ } else {
+ _$currentItemStar.addClass('important');
+ }
+ } else{
+ OC.dialogs.alert(jsondata.data.message, t('news', 'Error'));
+ }
+ });
+ };
+
+ /**
+ * Toggles an item as "keep unread". This prevents all handlers to mark it as unread
+ * except the current one
+ */
+ var _toggleKeepUnread = function(){
+ if(_keepUnread){
+ _$currentItemKeepUnread.prop("checked", false);
+ } else {
+ _$currentItemKeepUnread.prop("checked", true);
+ _setRead(false);
+ }
+ };
+
+ /**
+ * Sets the current item as read or unread
+ * @param read true sets the item to read, false to unread
+ */
+ var _setRead = function(read){
+ var status;
+
+ // if we already have the status, do nothing
+ if(read === _read) return;
+ // check if the keep unread flag was set
+ if(read && _keepUnread) return;
+
+ if(read){
+ status = 'read';
+ } else {
+ status = 'unread';
+ }
+
+ var data = {
+ itemId: itemId,
+ status: status
+ };
+
+ $.post(OC.filePath('news', 'ajax', 'setitemstatus.php'), data, function(jsonData){
+ if(jsonData.status == 'success'){
+ var counterplace = $('li.feed[data-id="'+_feedId+'"]').find('.unreaditemcounter');
+ var title = $('li.feed[data-id="'+_feedId+'"] > a');
+ var oldcount = counterplace.html();
+ counterplace.empty();
+
+ if(read){
+ _$currentItem.addClass('read');
+ if (oldcount <= 1) {
+ counterplace.removeClass('nonzero').addClass('zero');
+ title.removeClass('nonzero').addClass('zero');
+ }
+ else {
+ counterplace.append(--oldcount);
+ }
+ } else {
+ _$currentItem.removeClass('read');
+ if (oldcount >= 1) {
+ counterplace.removeClass('zero').addClass('nonzero');
+ title.removeClass('zero').addClass('nonzero');
+ }
+ else {
+ counterplace.append(--oldcount);
+ }
+ }
+
+ } else {
+ OC.dialogs.alert(jsonData.data.message, t('news', 'Error'));
+ }
+ })
+
+ };
+
+ // set public methods
+ this.setRead = function(read){ _setRead(read); }
+ this.isRead = function(){ return _read; }
+ this.toggleImportant = function(){ _toggleImportant(); }
+ this.toggleKeepUnread = function(){ _toggleKeepUnread(); }
+ },
+
}
function transformCollapsableTrigger() {
@@ -378,12 +446,12 @@ function bindItemEventListeners(){
var scrollHeight = $(this).prop('scrollHeight');
var scrolled = $(this).scrollTop() + boxHeight;
- $(this).children('ul').children('li.title_unread').each(function(){
+ $(this).children('ul').children('li:not(.read)').each(function(){
var itemOffset = $(this).position().top;
if(itemOffset <= 0 || scrolled >= scrollHeight){
var itemId = $(this).data('id');
- var feedId = $(this).data('feedid');
- News.Feed.markItem(itemId, feedId);
+ var handler = new News.ItemStatusHandler(itemId);
+ handler.setRead(true);
}
})
});
@@ -392,22 +460,29 @@ function bindItemEventListeners(){
$('#feed_items h1.item_title a').click(function(){
var $item = $(this).parent().parent('.news_item');
var itemId = $item.data('id');
- var feedId = $item.data('feedid');
- News.Feed.markItem(itemId, feedId);
+ var handler = new News.ItemStatusHandler(itemId);
+ handler.setRead(true);
})
// mark or unmark as important
$('#feed_items li.star').click(function(){
- var important = $(this).hasClass('important');
var $item = $(this).parent().parent().parent('.news_item');
var itemId = $item.data('id');
- var feedId = $item.data('feedid');
- News.Feed.setImportant(important, itemId, feedId);
+ var handler = new News.ItemStatusHandler(itemId);
+ handler.toggleImportant();
+ })
+
+ // toggle logic for the keep unread handler
+ $('#feed_items .keep_unread').click(function(){
+ var $item = $(this).parent().parent().parent('.news_item');
+ var itemId = $item.data('id');
+ var handler = new News.ItemStatusHandler(itemId);
+ handler.toggleKeepUnread();
})
// bind the mark all as read button
$('#mark_all_as_read').click(function(){
- News.Feed.markAllItems();
+ News.Feed.setAllItemsRead();
});
// filter for newest or all items