(function($) {
NEWSBLUR.MobileReader = function() {
// ===========
// = Globals =
// ===========
this.model = NEWSBLUR.assets;
this.story_view = 'page';
this.pages = {
'feeds' : $('#NB-page-feeds'),
'stories' : $('#NB-page-stories'),
'story' : $('#NB-page-story')
};
this.$s = {
$body: $('body'),
$feed_list: $('#NB-feed-list'),
$story_list: $('#NB-story-list'),
$story_detail: $('#NB-story-detail')
};
this.flags = {
'feeds_loaded' : false,
'active_view' : null
};
this.locks = {};
this.counts = {};
this.cache = {};
this.constants = {};
$(document).bind('mobileinit', function() {
$.mobile.ajaxEnabled = false;
});
this.runner();
};
NEWSBLUR.MobileReader.prototype = {
runner: function() {
this.load_feeds();
this.bind_clicks();
this.bind_scroll();
},
// =============
// = Feed List =
// =============
load_feeds: function() {
this.flags.active_view = 'feeds';
$.mobile.showPageLoadingMsg();
this.pages.feeds.unbind('pagebeforeshow').bind('pagebeforeshow', _.bind(function(e) {
$('ul', this.$s.$feed_list).listview('refresh');
}, this));
this.pages.feeds.unbind('pageshow').bind('pageshow', _.bind(function(e) {
$('ul', this.$s.$story_list).remove();
}, this));
this.model.load_feeds_flat($.rescope(this.build_feed_list, this));
},
build_feed_list: function() {
this.flags.active_view = 'feeds';
var self = this;
var folders = this.model.folders;
var feeds = this.model.feeds;
var $feed_list = this.$s.$feed_list;
var $feeds = '';
_.each(folders, function(items, folder_name) {
$feeds += '
NB-score-<%= score_color %>">\
\
<%= story.story_content %>
\
', {
story : story,
feed : feed,
score_color : score_color
});
return $story;
},
colorize_story_title: function() {
var feed = this.model.get_feed(this.active_feed);
$('.ui-header', this.pages.story)
.css('background-image', NEWSBLUR.utils.generate_gradient(feed, 'webkit'))
.css('background-image', NEWSBLUR.utils.generate_gradient(feed, 'moz'))
.css('borderBottom', NEWSBLUR.utils.generate_gradient(feed, 'border'))
.css('borderTop', NEWSBLUR.utils.generate_gradient(feed, 'border'))
.toggleClass('NB-inverse', NEWSBLUR.utils.is_feed_floater_gradient_light(feed));
var $feed = _.template('', {
feed : feed
});
$('.ui-title', this.pages.story).html($feed);
},
// =====================
// = General Utilities =
// =====================
story_color: function(story) {
var score = NEWSBLUR.utils.compute_story_score(story);
var score_color = 'neutral';
if (score > 0) score_color = 'positive';
if (score < 0) score_color = 'negative';
return score_color;
},
mark_story_as_read: function(story) {
var story_id = story.id;
var feed_id = story.story_feed_id;
this.model.mark_story_as_read(story_id, feed_id, _.bind(function(read) {
this.update_read_count(story_id, feed_id);
}, this));
},
update_read_count: function(story_id, feed_id) {
},
// ==========
// = Events =
// ==========
bind_clicks: function() {
var self = this;
this.$s.$feed_list.delegate('li a', 'tap', function(e) {
e.preventDefault();
var feed_id = $(e.currentTarget).jqmData('feed-id');
$.mobile.changePage(self.pages.stories);
self.reset_feed();
self.load_stories(feed_id);
});
this.$s.$story_list.delegate('li a', 'tap', function(e) {
e.preventDefault();
var story_id = $(e.currentTarget).jqmData('story-id');
$.mobile.showPageLoadingMsg();
$.mobile.changePage(self.pages.story);
self.load_story_detail(story_id);
});
this.$s.$story_detail.delegate('li a', 'tap', function(e) {
e.preventDefault();
var story_id = $(e.currentTarget).jqmData('story-id');
$.mobile.showPageLoadingMsg();
$.mobile.changePage(self.pages.story);
self.load_story_detail(story_id);
});
this.pages.story.delegate('.NB-next', 'tap', function(e) {
});
this.pages.story.delegate('.NB-previous', 'tap', function(e) {
});
},
bind_scroll: function() {
$(window).bind('scroll', _.throttle(_.bind(function(e) {
if (this.flags.active_view == 'stories') {
this.scroll_story_list();
}
}, this), 500));
}
};
})(jQuery);