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-06 20:37:20 -07:00
|
|
|
this.bind('change:comments', this.populate_comments);
|
|
|
|
this.populate_comments();
|
|
|
|
},
|
|
|
|
|
|
|
|
populate_comments: function() {
|
2012-06-05 16:18:30 -07:00
|
|
|
if (this.get('comments')) {
|
|
|
|
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-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-05-25 18:54:04 -07:00
|
|
|
active_story: null,
|
|
|
|
|
|
|
|
initialize: function() {
|
2012-05-25 20:52:30 -07:00
|
|
|
this.bind('change:selected', this.detect_selected_story);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.last_read_story_id = story.id;
|
|
|
|
clearTimeout(this.read_story_delay);
|
|
|
|
|
|
|
|
this.read_story_delay = _.delay(_.bind(function() {
|
|
|
|
if (delay || this.last_read_story_id == story.id || delay == 0) {
|
|
|
|
var mark_read_fn = NEWSBLUR.assets.mark_story_as_read;
|
|
|
|
var feed = NEWSBLUR.assets.get_feed(story.get('story_feed_id'));
|
|
|
|
if (feed.is_social()) {
|
|
|
|
mark_read_fn = NEWSBLUR.assets.mark_social_story_as_read;
|
|
|
|
}
|
|
|
|
mark_read_fn.call(NEWSBLUR.assets, story.id, story.get('story_feed_id'), function(read) {
|
|
|
|
NEWSBLUR.reader.update_read_count(story.id, story.get('story_feed_id'), {previously_read: read});
|
|
|
|
});
|
|
|
|
story.set('read_status', 1);
|
|
|
|
}
|
|
|
|
}, this), delay * 1000);
|
|
|
|
},
|
|
|
|
|
|
|
|
mark_unread: function(story, options) {
|
|
|
|
options = options || {};
|
|
|
|
NEWSBLUR.assets.mark_story_as_unread(story.id, story.get('story_feed_id'), function(read) {
|
2012-05-25 22:13:50 -07:00
|
|
|
NEWSBLUR.reader.update_read_count(story.id, story.get('story_feed_id'), {unread: true});
|
2012-05-25 20:52:30 -07:00
|
|
|
});
|
|
|
|
story.set('read_status', 0);
|
|
|
|
},
|
|
|
|
|
2012-05-25 18:54:04 -07:00
|
|
|
// ==================
|
|
|
|
// = Model Managers =
|
|
|
|
// ==================
|
|
|
|
|
2012-05-25 16:42:41 -07:00
|
|
|
visible: function() {
|
|
|
|
var unread_score = NEWSBLUR.assets.preference('unread_view');
|
|
|
|
|
|
|
|
return this.select(function(story) {
|
2012-05-25 20:52:30 -07:00
|
|
|
return story.score() >= unread_score;
|
2012-05-25 16:42:41 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
hidden: function() {
|
|
|
|
var unread_score = NEWSBLUR.assets.preference('unread_view');
|
|
|
|
|
|
|
|
return this.select(function(story) {
|
|
|
|
return story.score() < unread_view;
|
|
|
|
});
|
2012-05-25 18:54:04 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// ===========
|
|
|
|
// = Getters =
|
|
|
|
// ===========
|
|
|
|
|
2012-05-25 20:52:30 -07:00
|
|
|
get_next_story: function(direction) {
|
|
|
|
if (direction == -1) return this.get_previous_story();
|
|
|
|
|
|
|
|
var visible_stories = this.visible();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
get_previous_story: function() {
|
2012-05-25 20:52:30 -07:00
|
|
|
var visible_stories = this.visible();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// ==========
|
|
|
|
// = Events =
|
|
|
|
// ==========
|
|
|
|
|
2012-05-25 20:52:30 -07:00
|
|
|
detect_selected_story: function(selected_story) {
|
|
|
|
if (selected_story.get('selected')) {
|
|
|
|
this.deselect(selected_story);
|
|
|
|
this.active_story = selected_story;
|
2012-05-25 22:13:50 -07:00
|
|
|
NEWSBLUR.reader.active_story = 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
|
|
|
|
|
|
|
});
|