2012-05-17 18:40:46 -07:00
|
|
|
NEWSBLUR.Models.Story = Backbone.Model.extend({
|
|
|
|
|
2012-06-05 16:18:30 -07:00
|
|
|
initialize: function() {
|
2012-07-21 16:38:37 -07:00
|
|
|
this.bind('change:shared_comments', this.populate_comments);
|
2012-06-06 20:37:20 -07:00
|
|
|
this.bind('change:comments', this.populate_comments);
|
2012-06-07 10:25:15 -07:00
|
|
|
this.bind('change:comment_count', this.populate_comments);
|
2013-08-13 13:15:36 -07:00
|
|
|
this.bind('change:starred', this.change_starred);
|
|
|
|
this.bind('change:user_tags', this.change_user_tags);
|
2016-12-08 16:32:59 -08:00
|
|
|
this.bind('change:selected', this.select_story);
|
2012-06-06 20:37:20 -07:00
|
|
|
this.populate_comments();
|
2012-08-02 19:04:20 -07:00
|
|
|
this.story_permalink = this.get('story_permalink');
|
|
|
|
this.story_title = this.get('story_title');
|
2012-06-06 20:37:20 -07:00
|
|
|
},
|
|
|
|
|
2016-12-08 16:32:59 -08:00
|
|
|
select_story: function(story, selected) {
|
2017-01-11 12:33:52 -08:00
|
|
|
// console.log(['select_story', this, this.collection, story, selected]);
|
2016-12-14 14:51:49 -08:00
|
|
|
if (this.collection) this.collection.detect_selected_story(this, selected);
|
2016-12-08 16:32:59 -08:00
|
|
|
},
|
|
|
|
|
2013-01-14 12:50:00 -08:00
|
|
|
populate_comments: function(story, collection) {
|
2012-07-11 10:52:50 -07:00
|
|
|
this.friend_comments = new NEWSBLUR.Collections.Comments(this.get('friend_comments'));
|
2015-06-01 18:55:50 -07:00
|
|
|
this.friend_shares = new NEWSBLUR.Collections.Comments(this.get('friend_shares'));
|
2012-07-11 10:52:50 -07:00
|
|
|
this.public_comments = new NEWSBLUR.Collections.Comments(this.get('public_comments'));
|
2012-06-05 16:18:30 -07:00
|
|
|
},
|
|
|
|
|
2012-05-24 17:32:01 -07:00
|
|
|
score: function() {
|
2013-09-04 14:09:35 -07:00
|
|
|
if (NEWSBLUR.reader.flags['starred_view']) {
|
2014-05-28 19:17:44 -07:00
|
|
|
return 2;
|
2012-05-24 17:32:01 -07:00
|
|
|
} else {
|
|
|
|
return NEWSBLUR.utils.compute_story_score(this);
|
|
|
|
}
|
|
|
|
},
|
2012-05-17 18:40:46 -07:00
|
|
|
|
2012-05-24 17:32:01 -07:00
|
|
|
score_name: function(score) {
|
|
|
|
score = !_.isUndefined(score) ? score : this.score();
|
|
|
|
var score_name = 'neutral';
|
|
|
|
if (score > 0) score_name = 'positive';
|
|
|
|
if (score < 0) score_name = 'negative';
|
|
|
|
return score_name;
|
2012-05-25 16:42:41 -07:00
|
|
|
},
|
|
|
|
|
2014-12-18 21:17:32 -08:00
|
|
|
content_preview: function(attribute, length) {
|
2014-04-08 10:27:14 -07:00
|
|
|
var content = this.get(attribute || 'story_content');
|
2014-04-07 16:55:08 -07:00
|
|
|
content = content && Inflector.stripTags(content);
|
|
|
|
|
2014-12-18 21:17:32 -08:00
|
|
|
return _.string.prune(_.string.trim(content), length || 150, "...");
|
|
|
|
},
|
|
|
|
|
2016-08-15 13:53:48 -07:00
|
|
|
image_url: function(index) {
|
|
|
|
if (!index) index = 0;
|
|
|
|
if (this.get('image_urls').length >= index+1) {
|
|
|
|
return this.get('image_urls')[index];
|
2014-12-18 21:17:32 -08:00
|
|
|
}
|
2014-04-07 16:55:08 -07:00
|
|
|
},
|
|
|
|
|
2016-02-25 15:31:07 -08:00
|
|
|
story_authors: function() {
|
|
|
|
return this.get('story_authors').replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>');
|
|
|
|
},
|
|
|
|
|
2014-01-30 16:39:10 -08:00
|
|
|
formatted_short_date: function() {
|
|
|
|
var timestamp = this.get('story_timestamp');
|
|
|
|
var dateformat = NEWSBLUR.assets.preference('dateformat');
|
|
|
|
var date = new Date(parseInt(timestamp, 10) * 1000);
|
|
|
|
var midnight_today = function() {
|
|
|
|
var midnight = new Date();
|
|
|
|
midnight.setHours(0);
|
|
|
|
midnight.setMinutes(0);
|
|
|
|
midnight.setSeconds(0);
|
|
|
|
return midnight;
|
|
|
|
};
|
2017-06-07 17:51:27 -07:00
|
|
|
var midnight_tomorrow = function(midnight, days) {
|
|
|
|
if (!days) days = 1;
|
|
|
|
return new Date(midnight + days*60*60*24*1000);
|
|
|
|
};
|
2014-01-30 16:39:10 -08:00
|
|
|
var midnight_yesterday = function(midnight) {
|
2017-06-07 17:51:27 -07:00
|
|
|
return midnight_tomorrow(midnight, -1);
|
2014-01-30 16:39:10 -08:00
|
|
|
};
|
|
|
|
var midnight = midnight_today();
|
|
|
|
var time = date.format(dateformat == "24" ? "H:i" : "g:ia");
|
|
|
|
|
|
|
|
if (date > midnight) {
|
2017-06-07 17:51:27 -07:00
|
|
|
if (date > midnight_tomorrow(midnight)) {
|
|
|
|
if (date < midnight_tomorrow(midnight, 2)) {
|
|
|
|
return "Tomorrow, " + time;
|
|
|
|
} else {
|
|
|
|
return date.format("d M Y, ") + time;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return time;
|
|
|
|
}
|
2014-01-30 16:39:10 -08:00
|
|
|
} else if (date > midnight_yesterday(midnight)) {
|
|
|
|
return "Yesterday, " + time;
|
|
|
|
} else {
|
|
|
|
return date.format("d M Y, ") + time;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
formatted_long_date: function() {
|
|
|
|
var timestamp = this.get('story_timestamp');
|
|
|
|
var dateformat = NEWSBLUR.assets.preference('dateformat');
|
|
|
|
var date = new Date(parseInt(timestamp, 10) * 1000);
|
|
|
|
var midnight_today = function() {
|
|
|
|
var midnight = new Date();
|
|
|
|
midnight.setHours(0);
|
|
|
|
midnight.setMinutes(0);
|
|
|
|
midnight.setSeconds(0);
|
|
|
|
return midnight;
|
|
|
|
};
|
|
|
|
var midnight_yesterday = function(midnight) {
|
|
|
|
return new Date(midnight - 60*60*24*1000);
|
|
|
|
};
|
|
|
|
var beginning_of_month = function() {
|
|
|
|
var month = new Date();
|
|
|
|
month.setHours(0);
|
|
|
|
month.setMinutes(0);
|
|
|
|
month.setSeconds(0);
|
|
|
|
month.setDate(1);
|
|
|
|
return month;
|
|
|
|
};
|
|
|
|
var midnight = midnight_today();
|
|
|
|
var time = date.format(dateformat == "24" ? "H:i" : "g:ia");
|
|
|
|
if (date > midnight) {
|
|
|
|
return "Today, " + date.format("F jS ") + time;
|
|
|
|
} else if (date > midnight_yesterday(midnight)) {
|
|
|
|
return "Yesterday, " + date.format("F jS ") + time;
|
|
|
|
} else if (date > beginning_of_month()) {
|
|
|
|
return date.format("l, F jS ") + time;
|
|
|
|
} else {
|
|
|
|
return date.format("l, F jS Y ") + time;
|
|
|
|
}
|
|
|
|
},
|
2016-02-26 20:01:41 -08:00
|
|
|
|
2012-06-14 16:09:30 -07:00
|
|
|
mark_read: function(options) {
|
|
|
|
return NEWSBLUR.assets.stories.mark_read(this, options);
|
2012-06-26 13:31:37 -07:00
|
|
|
},
|
|
|
|
|
2013-06-16 09:12:15 -07:00
|
|
|
open_story_in_new_tab: function(background) {
|
2013-02-12 16:07:01 -08:00
|
|
|
this.mark_read({skip_delay: true});
|
2013-06-16 09:12:15 -07:00
|
|
|
|
2015-09-06 15:35:10 -07:00
|
|
|
// Safari browser on Linux is an impossibility, and thus we're actually
|
2015-09-06 21:34:18 +02:00
|
|
|
// on a WebKit-based browser (WebKitGTK or QTWebKit). These can't handle
|
|
|
|
// background tabs. Work around it by disabling backgrounding if we
|
|
|
|
// think we're on Safari and we're also on X11 or Linux
|
2016-11-16 11:35:02 -08:00
|
|
|
if ($.browser.safari && (/(\(X11|Linux)/.test(navigator.userAgent))) {
|
2015-09-06 21:34:18 +02:00
|
|
|
background = false;
|
|
|
|
}
|
2016-09-27 12:11:41 -07:00
|
|
|
|
|
|
|
if (background && $.browser.webkit) {
|
|
|
|
var event = new CustomEvent("openInNewTab", {
|
|
|
|
bubbles: true,
|
|
|
|
detail: {background: background}
|
|
|
|
});
|
|
|
|
var success = !this.story_title_view.$st.find('a')[0].dispatchEvent(event);
|
|
|
|
if (success) {
|
|
|
|
// console.log(['Used safari extension to open link in background', success]);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// console.log(['Safari extension failed to open link in background', success, this.story_title_view.$st.find('a')[0]]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-07 08:23:53 +02:00
|
|
|
if (background && !$.browser.mozilla) {
|
2013-06-16 09:12:15 -07:00
|
|
|
var anchor, event;
|
|
|
|
|
|
|
|
anchor = document.createElement("a");
|
|
|
|
anchor.href = this.get('story_permalink');
|
|
|
|
event = document.createEvent("MouseEvents");
|
|
|
|
event.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, true, false, false, true, 0, null);
|
2016-09-27 12:11:41 -07:00
|
|
|
var success = anchor.dispatchEvent(event);
|
|
|
|
if (success) {
|
|
|
|
// console.log(['Opened link in background', anchor.href]);
|
|
|
|
return success;
|
|
|
|
} else {
|
|
|
|
// console.log(['Failed to open link in background', anchor.href]);
|
|
|
|
}
|
2013-06-16 09:12:15 -07:00
|
|
|
} else {
|
|
|
|
window.open(this.get('story_permalink'), '_blank');
|
|
|
|
window.focus();
|
|
|
|
}
|
2013-02-12 16:07:01 -08:00
|
|
|
},
|
2013-02-12 13:15:14 -08:00
|
|
|
|
2013-04-03 13:25:17 -07:00
|
|
|
open_share_dialog: function(e, view) {
|
|
|
|
if (view == 'title') {
|
|
|
|
var $story_title = this.story_title_view.$st;
|
|
|
|
this.story_title_view.mouseenter_manage_icon();
|
|
|
|
NEWSBLUR.reader.show_manage_menu('story', $story_title, {story_id: this.id});
|
|
|
|
NEWSBLUR.reader.show_confirm_story_share_menu_item(this.id);
|
|
|
|
} else {
|
|
|
|
var $story = this.latest_story_detail_view.$el;
|
|
|
|
this.latest_story_detail_view.share_view.toggle_feed_story_share_dialog({
|
|
|
|
animate_scroll: true
|
|
|
|
});
|
|
|
|
}
|
2013-02-25 14:45:59 -08:00
|
|
|
},
|
2016-12-01 19:23:38 -08:00
|
|
|
|
2013-08-13 13:15:36 -07:00
|
|
|
// =================
|
|
|
|
// = Saved Stories =
|
|
|
|
// =================
|
|
|
|
|
|
|
|
toggle_starred: function() {
|
|
|
|
this.set('user_tags', this.existing_tags(), {silent: true});
|
|
|
|
|
|
|
|
if (!this.get('starred')) {
|
|
|
|
NEWSBLUR.assets.starred_count += 1;
|
|
|
|
this.set('starred', true);
|
|
|
|
} else {
|
|
|
|
NEWSBLUR.assets.starred_count -= 1;
|
|
|
|
this.set('starred', false);
|
|
|
|
}
|
|
|
|
NEWSBLUR.reader.update_starred_count();
|
|
|
|
},
|
|
|
|
|
|
|
|
change_starred: function() {
|
|
|
|
if (this.get('starred')) {
|
|
|
|
NEWSBLUR.assets.mark_story_as_starred(this.id);
|
|
|
|
} else {
|
|
|
|
NEWSBLUR.assets.mark_story_as_unstarred(this.id);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
change_user_tags: function(tags, options, etc) {
|
|
|
|
NEWSBLUR.assets.mark_story_as_starred(this.id);
|
|
|
|
},
|
|
|
|
|
|
|
|
existing_tags: function() {
|
|
|
|
var tags = this.get('user_tags');
|
|
|
|
|
|
|
|
if (!tags) {
|
|
|
|
tags = this.folder_tags();
|
|
|
|
}
|
|
|
|
|
|
|
|
return tags || [];
|
|
|
|
},
|
2014-04-07 16:55:08 -07:00
|
|
|
|
2013-08-13 13:15:36 -07:00
|
|
|
unused_story_tags: function() {
|
|
|
|
var tags = _.reduce(this.get('user_tags') || [], function(m, t) {
|
|
|
|
return _.without(m, t);
|
|
|
|
}, this.get('story_tags'));
|
|
|
|
return tags;
|
|
|
|
},
|
|
|
|
|
|
|
|
folder_tags: function() {
|
|
|
|
var folder_tags = [];
|
|
|
|
var feed_id = this.get('story_feed_id');
|
|
|
|
var feed = NEWSBLUR.assets.get_feed(feed_id);
|
|
|
|
if (feed) {
|
|
|
|
folder_tags = feed.parent_folder_names();
|
|
|
|
}
|
|
|
|
return folder_tags;
|
|
|
|
},
|
|
|
|
|
|
|
|
all_tags: function() {
|
|
|
|
var tags = [];
|
|
|
|
var story_tags = this.get('story_tags') || [];
|
|
|
|
var user_tags = this.get('user_tags') || [];
|
|
|
|
var folder_tags = this.folder_tags();
|
2013-08-16 18:26:56 -07:00
|
|
|
var existing_tags = NEWSBLUR.assets.starred_feeds.all_tags();
|
|
|
|
var all_tags = _.unique(_.compact(_.reduce([
|
|
|
|
story_tags, user_tags, folder_tags, existing_tags
|
|
|
|
], function(x, m) {
|
2013-08-15 19:04:14 -07:00
|
|
|
return m.concat(x);
|
|
|
|
}, [])));
|
2013-08-13 13:15:36 -07:00
|
|
|
|
|
|
|
return all_tags;
|
2012-05-24 17:32:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
NEWSBLUR.Collections.Stories = Backbone.Collection.extend({
|
|
|
|
|
2012-05-25 16:42:41 -07:00
|
|
|
model: NEWSBLUR.Models.Story,
|
2016-12-13 16:29:42 -08:00
|
|
|
|
2012-05-25 16:42:41 -07:00
|
|
|
read_stories: [],
|
|
|
|
|
2012-06-14 15:30:34 -07:00
|
|
|
previous_stories_stack: [],
|
|
|
|
|
2012-05-25 18:54:04 -07:00
|
|
|
active_story: null,
|
|
|
|
|
|
|
|
initialize: function() {
|
2016-12-08 16:32:59 -08:00
|
|
|
// this.bind('change:selected', this.detect_selected_story, this); // Handled in the Story model so it fires first
|
2012-06-14 15:30:34 -07:00
|
|
|
this.bind('reset', this.clear_previous_stories_stack, this);
|
2016-12-01 19:23:38 -08:00
|
|
|
// this.bind('change:selected', this.change_selected);
|
2012-05-25 18:54:04 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// ===========
|
|
|
|
// = Actions =
|
|
|
|
// ===========
|
|
|
|
|
2012-09-21 11:36:27 -07:00
|
|
|
deselect_other_stories: function(selected_story) {
|
2012-05-29 11:48:40 -07:00
|
|
|
this.any(function(story) {
|
2012-09-21 11:36:27 -07:00
|
|
|
if (story.get('selected') && story.id != selected_story.id) {
|
2012-05-25 20:52:30 -07:00
|
|
|
story.set('selected', false);
|
2012-05-29 11:48:40 -07:00
|
|
|
return true;
|
2012-05-25 20:52:30 -07:00
|
|
|
}
|
2012-05-25 16:42:41 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-05-25 20:52:30 -07:00
|
|
|
mark_read: function(story, options) {
|
|
|
|
options = options || {};
|
|
|
|
var delay = NEWSBLUR.assets.preference('read_story_delay');
|
|
|
|
if (options.skip_delay) {
|
|
|
|
delay = 0;
|
2013-07-13 11:06:26 -07:00
|
|
|
} else if (options.force) {
|
|
|
|
delay = 0;
|
2012-05-25 20:52:30 -07:00
|
|
|
} else if (delay == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
clearTimeout(this.read_story_delay);
|
|
|
|
|
2014-12-09 12:41:40 -08:00
|
|
|
var _mark_read = _.bind(function() {
|
2012-06-26 11:26:45 -07:00
|
|
|
if (!delay || (delay && this.active_story.id == story.id)) {
|
2012-06-14 17:38:09 -07:00
|
|
|
var feed = NEWSBLUR.assets.get_feed(NEWSBLUR.reader.active_feed);
|
2012-06-14 17:53:39 -07:00
|
|
|
if (!feed) {
|
|
|
|
feed = NEWSBLUR.assets.get_feed(story.get('story_feed_id'));
|
|
|
|
}
|
2013-09-06 12:42:39 -07:00
|
|
|
NEWSBLUR.assets.mark_story_hash_as_read(story, _.bind(function(read) {
|
2012-06-13 11:28:32 -07:00
|
|
|
this.update_read_count(story, {previously_read: read});
|
|
|
|
}, this));
|
2012-05-25 20:52:30 -07:00
|
|
|
}
|
2014-12-09 12:41:40 -08:00
|
|
|
}, this);
|
|
|
|
|
|
|
|
if (delay) {
|
|
|
|
this.read_story_delay = _.delay(_mark_read, delay * 1000);
|
|
|
|
} else {
|
|
|
|
_mark_read();
|
|
|
|
}
|
2012-05-25 20:52:30 -07:00
|
|
|
},
|
|
|
|
|
2016-12-05 17:10:47 -08:00
|
|
|
mark_read_pubsub: function(story_hash) {
|
|
|
|
var story = this.get_by_story_hash(story_hash);
|
|
|
|
if (!story) return;
|
|
|
|
story.set('read_status', 1);
|
|
|
|
},
|
|
|
|
|
|
|
|
mark_unread_pubsub: function(story_hash) {
|
|
|
|
var story = this.get_by_story_hash(story_hash);
|
|
|
|
if (!story) return;
|
|
|
|
story.set('read_status', 0);
|
|
|
|
},
|
|
|
|
|
2012-05-25 20:52:30 -07:00
|
|
|
mark_unread: function(story, options) {
|
|
|
|
options = options || {};
|
2012-06-13 11:28:32 -07:00
|
|
|
NEWSBLUR.assets.mark_story_as_unread(story.id, story.get('story_feed_id'), _.bind(function(read) {
|
|
|
|
this.update_read_count(story, {unread: true});
|
2013-01-23 10:02:14 -08:00
|
|
|
}, this), _.bind(function(data) {
|
|
|
|
story.set('read_status', 1, {'error_marking_unread': true, 'message': data.message});
|
|
|
|
this.update_read_count(story, {unread: false});
|
2012-06-13 11:28:32 -07:00
|
|
|
}, this));
|
2012-05-25 20:52:30 -07:00
|
|
|
story.set('read_status', 0);
|
|
|
|
},
|
|
|
|
|
2012-06-13 11:28:32 -07:00
|
|
|
update_read_count: function(story, options) {
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
if (options.previously_read) return;
|
|
|
|
|
|
|
|
var story_unread_counter = NEWSBLUR.app.story_unread_counter;
|
|
|
|
var unread_view = NEWSBLUR.reader.get_unread_view_name();
|
2012-06-14 17:38:09 -07:00
|
|
|
var active_feed = NEWSBLUR.assets.get_feed(NEWSBLUR.reader.active_feed);
|
|
|
|
var story_feed = NEWSBLUR.assets.get_feed(story.get('story_feed_id'));
|
2012-08-08 11:52:59 -07:00
|
|
|
var friend_feeds = NEWSBLUR.assets.get_friend_feeds(story);
|
2012-08-10 10:02:43 -07:00
|
|
|
|
2012-06-14 17:53:39 -07:00
|
|
|
if (!active_feed) {
|
|
|
|
// River of News does not have an active feed.
|
|
|
|
active_feed = story_feed;
|
2017-03-24 00:54:57 -07:00
|
|
|
} else if (active_feed && active_feed.is_feed() && active_feed.is_social()) {
|
2012-08-10 10:02:43 -07:00
|
|
|
friend_feeds = _.without(friend_feeds, active_feed);
|
2012-06-14 17:53:39 -07:00
|
|
|
}
|
2012-08-10 10:02:43 -07:00
|
|
|
|
2012-06-13 11:28:32 -07:00
|
|
|
if (story.score() > 0) {
|
2012-12-13 17:58:19 -08:00
|
|
|
var active_count = active_feed && Math.max(active_feed.get('ps') + (options.unread?1:-1), 0);
|
2012-09-27 10:44:55 -07:00
|
|
|
var story_count = story_feed && Math.max(story_feed.get('ps') + (options.unread?1:-1), 0);
|
2012-12-13 17:58:19 -08:00
|
|
|
if (active_feed) active_feed.set('ps', active_count, {instant: true});
|
2012-09-27 10:44:55 -07:00
|
|
|
if (story_feed) story_feed.set('ps', story_count, {instant: true});
|
2012-08-08 11:52:59 -07:00
|
|
|
_.each(friend_feeds, function(socialsub) {
|
|
|
|
var socialsub_count = Math.max(socialsub.get('ps') + (options.unread?1:-1), 0);
|
|
|
|
socialsub.set('ps', socialsub_count, {instant: true});
|
|
|
|
});
|
2012-06-13 11:28:32 -07:00
|
|
|
} else if (story.score() == 0) {
|
2012-12-13 17:58:19 -08:00
|
|
|
var active_count = active_feed && Math.max(active_feed.get('nt') + (options.unread?1:-1), 0);
|
2012-09-27 10:44:55 -07:00
|
|
|
var story_count = story_feed && Math.max(story_feed.get('nt') + (options.unread?1:-1), 0);
|
2012-12-13 17:58:19 -08:00
|
|
|
if (active_feed) active_feed.set('nt', active_count, {instant: true});
|
2012-09-27 10:44:55 -07:00
|
|
|
if (story_feed) story_feed.set('nt', story_count, {instant: true});
|
2012-08-08 11:52:59 -07:00
|
|
|
_.each(friend_feeds, function(socialsub) {
|
|
|
|
var socialsub_count = Math.max(socialsub.get('nt') + (options.unread?1:-1), 0);
|
|
|
|
socialsub.set('nt', socialsub_count, {instant: true});
|
|
|
|
});
|
2012-06-13 11:28:32 -07:00
|
|
|
} else if (story.score() < 0) {
|
2012-12-13 17:58:19 -08:00
|
|
|
var active_count = active_feed && Math.max(active_feed.get('ng') + (options.unread?1:-1), 0);
|
2012-09-27 10:44:55 -07:00
|
|
|
var story_count = story_feed && Math.max(story_feed.get('ng') + (options.unread?1:-1), 0);
|
2012-12-13 17:58:19 -08:00
|
|
|
if (active_feed) active_feed.set('ng', active_count, {instant: true});
|
2012-09-27 10:44:55 -07:00
|
|
|
if (story_feed) story_feed.set('ng', story_count, {instant: true});
|
2012-08-08 11:52:59 -07:00
|
|
|
_.each(friend_feeds, function(socialsub) {
|
|
|
|
var socialsub_count = Math.max(socialsub.get('ng') + (options.unread?1:-1), 0);
|
|
|
|
socialsub.set('ng', socialsub_count, {instant: true});
|
|
|
|
});
|
2012-06-13 11:28:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (story_unread_counter) {
|
|
|
|
story_unread_counter.flash();
|
|
|
|
}
|
|
|
|
|
|
|
|
// if ((unread_view == 'positive' && feed.get('ps') == 0) ||
|
|
|
|
// (unread_view == 'neutral' && feed.get('ps') == 0 && feed.get('nt') == 0) ||
|
|
|
|
// (unread_view == 'negative' && feed.get('ps') == 0 && feed.get('nt') == 0 && feed.get('ng') == 0)) {
|
|
|
|
// story_unread_counter.fall();
|
|
|
|
// }
|
|
|
|
},
|
|
|
|
|
2012-06-14 15:30:34 -07:00
|
|
|
clear_previous_stories_stack: function() {
|
|
|
|
this.previous_stories_stack = [];
|
2014-05-28 19:17:44 -07:00
|
|
|
this.active_story = null;
|
2012-06-14 15:30:34 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
select_previous_story: function() {
|
|
|
|
if (this.previous_stories_stack.length) {
|
|
|
|
var previous_story = this.previous_stories_stack.pop();
|
|
|
|
if (previous_story.get('selected') ||
|
|
|
|
previous_story.score() < NEWSBLUR.reader.get_unread_view_score()) {
|
|
|
|
this.select_previous_story();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
previous_story.set('selected', true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-05-25 18:54:04 -07:00
|
|
|
// ==================
|
|
|
|
// = Model Managers =
|
|
|
|
// ==================
|
|
|
|
|
2012-06-13 17:34:26 -07:00
|
|
|
visible: function(score) {
|
|
|
|
score = _.isUndefined(score) ? NEWSBLUR.reader.get_unread_view_score() : score;
|
|
|
|
|
|
|
|
return this.select(function(story) {
|
2012-07-01 12:49:08 -07:00
|
|
|
return story.score() >= score || story.get('visible');
|
2012-06-13 17:34:26 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-02-08 11:26:29 -08:00
|
|
|
last_visible: function(score) {
|
|
|
|
score = _.isUndefined(score) ? NEWSBLUR.reader.get_unread_view_score() : score;
|
|
|
|
|
|
|
|
for (var i=this.size(); i >= 0; i--) {
|
|
|
|
var story = this.at(i);
|
|
|
|
if (story.score() >= score || story.get('visible')) {
|
|
|
|
return story;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-06-13 17:34:26 -07:00
|
|
|
visible_and_unread: function(score, include_active_story) {
|
2012-06-13 17:07:15 -07:00
|
|
|
var active_story_id = this.active_story && this.active_story.id;
|
|
|
|
score = _.isUndefined(score) ? NEWSBLUR.reader.get_unread_view_score() : score;
|
2012-05-25 16:42:41 -07:00
|
|
|
|
|
|
|
return this.select(function(story) {
|
2012-07-01 12:49:08 -07:00
|
|
|
var visible = story.score() >= score || story.get('visible');
|
2012-06-13 17:34:26 -07:00
|
|
|
var same_story = include_active_story && story.id == active_story_id;
|
|
|
|
var read = !!story.get('read_status');
|
|
|
|
|
|
|
|
return visible && (!read || same_story);
|
2012-05-25 16:42:41 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-06-14 15:03:20 -07:00
|
|
|
hidden: function(score) {
|
|
|
|
score = _.isUndefined(score) ? NEWSBLUR.reader.get_unread_view_score() : score;
|
|
|
|
|
2012-05-25 16:42:41 -07:00
|
|
|
return this.select(function(story) {
|
2012-07-01 12:49:08 -07:00
|
|
|
return story.score() < score && !story.get('visible');
|
2012-05-25 16:42:41 -07:00
|
|
|
});
|
2012-05-25 18:54:04 -07:00
|
|
|
},
|
|
|
|
|
2016-12-13 16:29:42 -08:00
|
|
|
limit: function(count) {
|
|
|
|
this.models = this.models.slice(0, count);
|
|
|
|
},
|
|
|
|
|
2012-05-25 18:54:04 -07:00
|
|
|
// ===========
|
|
|
|
// = Getters =
|
|
|
|
// ===========
|
|
|
|
|
2012-06-13 13:47:50 -07:00
|
|
|
get_next_story: function(direction, options) {
|
|
|
|
options = options || {};
|
|
|
|
if (direction == -1) return this.get_previous_story(options);
|
2012-06-13 15:09:19 -07:00
|
|
|
|
2012-06-13 13:47:50 -07:00
|
|
|
var visible_stories = this.visible(options.score);
|
2016-12-01 19:23:38 -08:00
|
|
|
console.log(['get_next_story', this.active_story, this == NEWSBLUR.assets.stories ? "stories" : "dashboard"]);
|
2012-05-25 18:54:04 -07:00
|
|
|
if (!this.active_story) {
|
2012-05-25 20:52:30 -07:00
|
|
|
return visible_stories[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
var current_index = _.indexOf(visible_stories, this.active_story);
|
|
|
|
|
|
|
|
if (current_index+1 <= visible_stories.length) {
|
|
|
|
return visible_stories[current_index+1];
|
2012-05-25 18:54:04 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-06-13 13:47:50 -07:00
|
|
|
get_previous_story: function(options) {
|
|
|
|
options = options || {};
|
|
|
|
var visible_stories = this.visible(options.score);
|
2012-05-25 20:52:30 -07:00
|
|
|
|
2012-05-25 18:54:04 -07:00
|
|
|
if (!this.active_story) {
|
2012-05-25 20:52:30 -07:00
|
|
|
return visible_stories[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
var current_index = _.indexOf(visible_stories, this.active_story);
|
|
|
|
|
|
|
|
if (current_index-1 >= 0) {
|
|
|
|
return visible_stories[current_index-1];
|
2012-05-25 18:54:04 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-06-13 15:54:18 -07:00
|
|
|
get_next_unread_story: function(options) {
|
|
|
|
options = options || {};
|
2012-06-13 17:34:26 -07:00
|
|
|
var visible_stories = this.visible_and_unread(options.score, true);
|
2012-06-13 15:54:18 -07:00
|
|
|
if (!visible_stories.length) return;
|
|
|
|
|
|
|
|
if (!this.active_story) {
|
|
|
|
return visible_stories[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
var current_index = _.indexOf(visible_stories, this.active_story);
|
2012-06-13 17:07:15 -07:00
|
|
|
|
|
|
|
// The +1+1 is because the currently selected story is included, so it
|
|
|
|
// counts for more than what is available.
|
|
|
|
if (current_index+1+1 <= visible_stories.length) {
|
2012-07-31 15:53:16 -07:00
|
|
|
if (visible_stories[current_index+1])
|
2012-06-13 15:54:18 -07:00
|
|
|
return visible_stories[current_index+1];
|
2012-06-13 17:07:15 -07:00
|
|
|
} else if (current_index-1 >= 0) {
|
2012-06-13 15:54:18 -07:00
|
|
|
return visible_stories[current_index-1];
|
2012-07-31 15:53:16 -07:00
|
|
|
} else if (visible_stories.length == 1 && visible_stories[0] == this.active_story && !this.active_story.get('read_status')) {
|
|
|
|
// If the current story is unread yet selected, switch it back.
|
|
|
|
visible_stories[current_index].set('selected', false);
|
|
|
|
return visible_stories[current_index];
|
2012-06-13 15:54:18 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-06-13 17:34:26 -07:00
|
|
|
get_last_unread_story: function(unread_count, options) {
|
|
|
|
options = options || {};
|
|
|
|
var visible_stories = this.visible_and_unread(options.score);
|
|
|
|
if (!visible_stories.length || visible_stories.length < unread_count) return;
|
|
|
|
|
|
|
|
return _.last(visible_stories);
|
|
|
|
},
|
|
|
|
|
2016-11-16 11:35:02 -08:00
|
|
|
get_by_story_hash: function(story_hash) {
|
|
|
|
return this.detect(function(s) { return s.get('story_hash') == story_hash; });
|
|
|
|
},
|
|
|
|
|
2016-12-01 16:22:51 -08:00
|
|
|
deselect: function() {
|
|
|
|
this.each(function(story){
|
|
|
|
if (story.get('selected')) {
|
|
|
|
story.set('selected', false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-05-25 18:54:04 -07:00
|
|
|
// ==========
|
|
|
|
// = Events =
|
|
|
|
// ==========
|
|
|
|
|
2012-06-08 18:04:56 -07:00
|
|
|
detect_selected_story: function(selected_story, selected) {
|
|
|
|
if (selected) {
|
2017-01-11 12:33:52 -08:00
|
|
|
// console.log(['detect_selected_story', selected, selected_story, this.active_story, this == NEWSBLUR.assets.stories ? "stories" : "dashboard"]);
|
2012-09-21 11:36:27 -07:00
|
|
|
this.deselect_other_stories(selected_story);
|
2012-05-25 20:52:30 -07:00
|
|
|
this.active_story = selected_story;
|
2012-05-25 22:13:50 -07:00
|
|
|
NEWSBLUR.reader.active_story = selected_story;
|
2012-06-14 15:30:34 -07:00
|
|
|
this.previous_stories_stack.push(selected_story);
|
2012-05-25 20:52:30 -07:00
|
|
|
if (!selected_story.get('read_status')) {
|
|
|
|
this.mark_read(selected_story);
|
|
|
|
}
|
|
|
|
}
|
2012-05-25 16:42:41 -07:00
|
|
|
}
|
2016-12-01 19:23:38 -08:00
|
|
|
|
2015-09-06 21:34:18 +02:00
|
|
|
});
|