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-06-26 13:31:37 -07:00
|
|
|
this.bind('change:selected', this.change_selected);
|
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);
|
2012-06-06 20:37:20 -07:00
|
|
|
this.populate_comments();
|
|
|
|
},
|
|
|
|
|
2012-06-07 10:25:15 -07:00
|
|
|
populate_comments: function(story, collection, changes) {
|
|
|
|
var comments = this.get('comments');
|
|
|
|
|
|
|
|
if (!this.get('comment_count')) {
|
|
|
|
delete this.comments;
|
|
|
|
} else if (comments && comments.length) {
|
2012-06-05 16:18:30 -07:00
|
|
|
this.comments = new NEWSBLUR.Collections.Comments(this.get('comments'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-05-24 17:32:01 -07:00
|
|
|
score: function() {
|
|
|
|
if (NEWSBLUR.reader.active_feed == 'starred') {
|
|
|
|
return 1;
|
|
|
|
} 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
|
|
|
},
|
|
|
|
|
|
|
|
has_modifications: function() {
|
|
|
|
if (this.get('story_content').indexOf('<ins') != -1) {
|
|
|
|
return true;
|
|
|
|
} else if (NEWSBLUR.assets.preference('hide_story_changes') &&
|
|
|
|
this.get('story_content').indexOf('<del') != -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
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
|
|
|
},
|
|
|
|
|
|
|
|
change_selected: function(model, selected, changes) {
|
|
|
|
model.collection.detect_selected_story(model, selected);
|
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,
|
|
|
|
|
|
|
|
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() {
|
2012-06-26 13:31:37 -07:00
|
|
|
// this.bind('change:selected', this.detect_selected_story, this);
|
2012-06-14 15:30:34 -07:00
|
|
|
this.bind('reset', this.clear_previous_stories_stack, this);
|
2012-05-25 18:54:04 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// ===========
|
|
|
|
// = Actions =
|
|
|
|
// ===========
|
|
|
|
|
2012-05-25 20:52:30 -07:00
|
|
|
deselect: function(selected_story) {
|
2012-05-29 11:48:40 -07:00
|
|
|
this.any(function(story) {
|
|
|
|
if (story.get('selected') && story != selected_story) {
|
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;
|
|
|
|
} else if (delay == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
clearTimeout(this.read_story_delay);
|
|
|
|
|
|
|
|
this.read_story_delay = _.delay(_.bind(function() {
|
2012-06-26 11:26:45 -07:00
|
|
|
if (!delay || (delay && this.active_story.id == story.id)) {
|
2012-05-25 20:52:30 -07:00
|
|
|
var mark_read_fn = NEWSBLUR.assets.mark_story_as_read;
|
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'));
|
|
|
|
}
|
|
|
|
if (feed && feed.is_social()) {
|
2012-05-25 20:52:30 -07:00
|
|
|
mark_read_fn = NEWSBLUR.assets.mark_social_story_as_read;
|
|
|
|
}
|
2012-06-14 17:38:09 -07:00
|
|
|
mark_read_fn.call(NEWSBLUR.assets, story, feed, _.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
|
|
|
story.set('read_status', 1);
|
|
|
|
}
|
|
|
|
}, this), delay * 1000);
|
|
|
|
},
|
|
|
|
|
|
|
|
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});
|
|
|
|
}, 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-06-13 11:28:32 -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;
|
|
|
|
}
|
2012-06-13 11:28:32 -07:00
|
|
|
if (story.score() > 0) {
|
2012-06-14 17:38:09 -07:00
|
|
|
var active_count = Math.max(active_feed.get('ps') + (options.unread?1:-1), 0);
|
|
|
|
var story_count = Math.max(story_feed.get('ps') + (options.unread?1:-1), 0);
|
|
|
|
active_feed.set('ps', active_count, {instant: true});
|
|
|
|
story_feed.set('ps', story_count, {instant: true});
|
2012-06-13 11:28:32 -07:00
|
|
|
} else if (story.score() == 0) {
|
2012-06-14 17:38:09 -07:00
|
|
|
var active_count = Math.max(active_feed.get('nt') + (options.unread?1:-1), 0);
|
|
|
|
var story_count = Math.max(story_feed.get('nt') + (options.unread?1:-1), 0);
|
|
|
|
active_feed.set('nt', active_count, {instant: true});
|
|
|
|
story_feed.set('nt', story_count, {instant: true});
|
2012-06-13 11:28:32 -07:00
|
|
|
} else if (story.score() < 0) {
|
2012-06-14 17:38:09 -07:00
|
|
|
var active_count = Math.max(active_feed.get('ng') + (options.unread?1:-1), 0);
|
|
|
|
var story_count = Math.max(story_feed.get('ng') + (options.unread?1:-1), 0);
|
|
|
|
active_feed.set('ng', active_count, {instant: true});
|
|
|
|
story_feed.set('ng', story_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 = [];
|
|
|
|
},
|
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
// ===========
|
|
|
|
// = 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);
|
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 <= 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-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-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);
|
|
|
|
},
|
|
|
|
|
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) {
|
2012-05-25 20:52:30 -07:00
|
|
|
this.deselect(selected_story);
|
|
|
|
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
|
|
|
}
|
2012-05-17 18:40:46 -07:00
|
|
|
|
|
|
|
});
|