NewsBlur/media/js/newsblur/reader_feedchooser.js

263 lines
8.8 KiB
JavaScript
Raw Normal View History

NEWSBLUR.ReaderFeedchooser = function(options) {
var defaults = {};
this.options = $.extend({}, defaults, options);
this.model = NEWSBLUR.AssetModel.reader();
this.google_favicon_url = 'http://www.google.com/s2/favicons?domain_url=';
this.runner();
};
NEWSBLUR.ReaderFeedchooser.prototype = {
runner: function() {
this.start = new Date();
this.MAX_FEEDS = 40;
this.approve_list = [];
this.make_modal();
this.open_modal();
this.find_feeds_in_feed_list();
this.initial_load_feeds();
this.$modal.bind('mousedown', $.rescope(this.handle_click, this));
},
make_modal: function() {
var self = this;
this.$modal = $.make('div', { className: 'NB-modal-feedchooser NB-modal' }, [
$.make('h2', { className: 'NB-modal-title' }, 'Choose Your '+this.MAX_FEEDS),
$.make('h2', { className: 'NB-modal-subtitle' }, [
$.make('b', [
'You have a ',
$.make('span', { style: 'color: #303060;' }, 'Standard Account'),
', which can follow up to '+this.MAX_FEEDS+' sites at a time.'
]),
'Choose which '+this.MAX_FEEDS+' you would like to follow. You can always change these.'
]),
$.make('div', { className: 'NB-feedchooser-info'}, [
$.make('div', { className: 'NB-feedchooser-info-counts'})
]),
this.make_feeds(),
$.make('form', { className: 'NB-feedchooser-form' }, [
$.make('div', { className: 'NB-modal-submit' }, [
$.make('input', { type: 'submit', disabled: 'true', className: 'NB-disabled NB-modal-submit-save NB-modal-submit-green', value: 'Check what you like above...' })
])
])
]);
},
make_feeds: function() {
var feeds = this.model.feeds;
var $feeds = $('#feed_list').clone(true).attr({
'id': 'NB-feedchooser-feeds',
'class': 'NB-feedlist NB-feedchooser unread_view_positive',
'style': ''
});
// Expand collapsed folders
$('ul.folder', $feeds).css({
'display': 'block',
'opacity': 1
});
$('.unread_count_positive', $feeds).text('On');
$('.unread_count_negative', $feeds).text('Off');
return $feeds;
},
open_modal: function() {
var self = this;
this.$modal.modal({
'minWidth': 600,
'maxWidth': 600,
'overlayClose': true,
'onOpen': function (dialog) {
dialog.overlay.fadeIn(200, function () {
dialog.container.fadeIn(200);
dialog.data.fadeIn(200);
});
},
'onShow': function(dialog) {
$('#simplemodal-container').corner('6px');
},
'onClose': function(dialog) {
dialog.data.hide().empty().remove();
dialog.container.hide().empty().remove();
dialog.overlay.fadeOut(200, function() {
dialog.overlay.empty().remove();
$.modal.close();
});
$('.NB-modal-holder').empty().remove();
}
});
},
add_feed_to_decline: function(feed_id, update) {
this.approve_list = _.without(this.approve_list, feed_id);
var $feed = this.$feeds[feed_id];
$feed.removeClass('NB-feedchooser-approve');
$feed.addClass('NB-feedchooser-decline');
if (update) {
this.update_counts();
}
},
add_feed_to_approve: function(feed_id, update) {
if (!_.contains(this.approve_list, feed_id)) {
this.approve_list.push(feed_id);
var $feed = this.$feeds[feed_id];
$feed.removeClass('NB-feedchooser-decline');
$feed.addClass('NB-feedchooser-approve');
if (update) {
this.update_counts();
}
}
},
find_feeds_in_feed_list: function() {
var $feed_list = $('.NB-feedchooser', this.$modal);
var $feeds = {};
$('.feed', $feed_list).each(function() {
var feed_id = $(this).data('feed_id');
if (!_.contains(_.keys($feeds), feed_id)) {
$feeds[feed_id] = $([]);
}
$feeds[feed_id].push($(this).get(0));
});
this.$feeds = $feeds;
},
update_counts: function() {
var $count = $('.NB-feedchooser-info-counts');
var approved = this.approve_list.length;
var $submit = $('.NB-modal-submit-save', this.$modal);
var difference = approved - this.MAX_FEEDS;
$count.text(approved + '/' + this.MAX_FEEDS);
$count.toggleClass('NB-full', approved == this.MAX_FEEDS);
$count.toggleClass('NB-error', approved > this.MAX_FEEDS);
if (approved > this.MAX_FEEDS) {
$submit.addClass('NB-disabled').attr('disabled', true).val('Too many sites! Deselect ' + (
difference == 1 ?
'1 site...' :
difference + ' sites...'
));
} else {
$submit.removeClass('NB-disabled').attr('disabled', false).val('OK, These are my ' + approved + '. Save them, please!');
}
},
initial_load_feeds: function() {
var start = new Date();
var self = this;
var $feeds = $('.feed', this.$modal);
var feeds = this.model.get_feeds();
var active_feeds = _.any(_.pluck(feeds, 'active'));
if (!active_feeds) {
// Get feed subscribers
var min_subscribers = _.last(
_.first(
_.pluck(this.model.get_feeds(), 'subs').sort(function(a,b) {
return b-a;
}),
this.MAX_FEEDS
)
);
// Decline everything
var approve_feeds = [];
_.each(feeds, function(feed, feed_id) {
self.add_feed_to_decline(parseInt(feed_id, 10));
if (feed['subs'] >= min_subscribers) {
approve_feeds.push(parseInt(feed_id, 10));
}
});
// Approve feeds in subs
_.each(approve_feeds, function(feed_id) {
if (self.model.get_feed(feed_id)['subs'] > min_subscribers &&
self.approve_list.length < self.MAX_FEEDS) {
self.add_feed_to_approve(feed_id);
}
});
_.each(approve_feeds, function(feed_id) {
if (self.model.get_feed(feed_id)['subs'] == min_subscribers &&
self.approve_list.length < self.MAX_FEEDS) {
self.add_feed_to_approve(feed_id);
}
});
} else {
// Get active feeds
var active_feeds = _.pluck(_.select(feeds, function(feed) {
if (feed.active) return true;
}), 'id');
this.approve_list = active_feeds;
// Approve or decline
var feeds = [];
$feeds.each(function() {
var feed_id = $(this).data('feed_id');
if (_.contains(active_feeds, feed_id)) {
self.add_feed_to_approve(feed_id);
} else {
self.add_feed_to_decline(feed_id);
}
});
}
this.update_counts();
},
save: function() {
var $submit = $('.NB-modal-submit-save', this.$modal);
$submit.addClass('NB-disabled').val('Saving...');
this.model.save_feed_chooser(this.approve_list, function() {
NEWSBLUR.reader.load_feeds();
$.modal.close();
});
},
// ===========
// = Actions =
// ===========
handle_click: function(elem, e) {
var self = this;
$.targetIs(e, { tagSelector: '.feed' }, _.bind(function($t, $p) {
e.preventDefault();
var feed_id = $t.data('feed_id');
if (_.contains(this.approve_list, feed_id)) {
this.add_feed_to_decline(feed_id, true);
} else {
this.add_feed_to_approve(feed_id, true);
}
}, this));
$.targetIs(e, { tagSelector: '.NB-modal-submit-save' }, _.bind(function($t, $p) {
e.preventDefault();
this.save();
}, this));
},
handle_cancel: function() {
var $cancel = $('.NB-modal-cancel', this.$modal);
$cancel.click(function(e) {
e.preventDefault();
$.modal.close();
});
}
};