NewsBlur/media/js/newsblur/reader_friends.js

237 lines
9.1 KiB
JavaScript
Raw Normal View History

NEWSBLUR.ReaderFriends = function(options) {
var defaults = {
width: 780
};
this.options = $.extend({}, defaults, options);
this.model = NEWSBLUR.AssetModel.reader();
this.runner();
};
NEWSBLUR.ReaderFriends.prototype = new NEWSBLUR.Modal;
_.extend(NEWSBLUR.ReaderFriends.prototype, {
runner: function() {
this.make_modal();
this.open_modal();
this.fetch_friends();
this.$modal.bind('click', $.rescope(this.handle_click, this));
this.handle_change();
},
make_modal: function() {
var self = this;
this.$modal = $.make('div', { className: 'NB-modal NB-modal-friends' }, [
$.make('div', { className: 'NB-modal-tabs' }, [
$.make('div', { className: 'NB-modal-loading' }),
$.make('div', { className: 'NB-modal-tab NB-active NB-modal-tab-findfriends' }, 'Find Friends'),
$.make('div', { className: 'NB-modal-tab NB-modal-tab-following' }, 'I\'m Following'),
$.make('div', { className: 'NB-modal-tab NB-modal-tab-followers' }, 'Following Me')
]),
$.make('h2', { className: 'NB-modal-title' }, 'Friends and Followers'),
$.make('div', { className: 'NB-tab NB-tab-findfriends NB-active' }, [
$.make('div', { className: 'NB-modal-section NB-friends-services'})
]),
$.make('div', { className: 'NB-tab NB-tab-following' }),
$.make('div', { className: 'NB-tab NB-tab-followers' })
]);
},
fetch_friends: function() {
$('.NB-modal-loading', this.$modal).addClass('NB-active');
this.model.fetch_friends(_.bind(function(data) {
this.make_friends(data);
this.make_followers(data);
this.make_following(data);
}, this));
},
make_friends: function(data) {
console.log(["data", data]);
$('.NB-modal-loading', this.$modal).removeClass('NB-active');
var $services = $('.NB-friends-services', this.$modal).empty();
_.each(['twitter', 'facebook'], function(service) {
var $service;
if (data.services[service][service+'_uid']) {
$service = $.make('div', { className: 'NB-friends-service NB-connected NB-friends-service-'+service}, [
$.make('div', { className: 'NB-friends-service-title' }, _.capitalize(service)),
$.make('div', { className: 'NB-friends-service-connect NB-modal-submit-button NB-modal-submit-close' }, 'Disconnect')
]);
} else {
$service = $.make('div', { className: 'NB-friends-service NB-friends-service-'+service}, [
$.make('div', { className: 'NB-friends-service-title' }, _.capitalize(service)),
$.make('div', { className: 'NB-friends-service-connect NB-modal-submit-button NB-modal-submit-green' }, [
$.make('img', { src: NEWSBLUR.Globals.MEDIA_URL + '/img/reader/' + service + '_icon.png' }),
'Connect to ' + _.capitalize(service)
])
]);
}
$services.append($service);
});
$autofollow = $.make('div', { className: 'NB-friends-service NB-friends-autofollow'}, [
$.make('input', { type: 'checkbox', className: 'NB-friends-autofollow-checkbox', id: 'NB-friends-autofollow-checkbox', checked: data.autofollow ? 'checked' : null }),
$.make('label', { className: 'NB-friends-autofollow-label', 'for': 'NB-friends-autofollow-checkbox' }, [
'Auto-follow',
$.make('br'),
'my friends'
])
]);
$services.append($autofollow);
this.resize();
},
make_followers: function(data) {
if (!data.followers || !data.followers.length) {
var $ghost = $.make('div', { className: 'NB-ghost NB-modal-section' }, 'Nobody has yet subscribed to your shared stories.');
$('.NB-tab-followers', this.$modal).empty().append($ghost);
}
},
make_following: function(data) {
if (!data.following || !data.following.length) {
var $ghost = $.make('div', { className: 'NB-ghost NB-modal-section' }, 'You are not yet subscribing to anybody\'s shared stories.');
$('.NB-tab-following', this.$modal).empty().append($ghost);
}
},
open_modal: function(callback) {
var self = this;
this.$modal.modal({
'minWidth': 740,
'maxWidth': 740,
'overlayClose': true,
'onOpen': function (dialog) {
dialog.overlay.fadeIn(200, function () {
dialog.container.fadeIn(200);
dialog.data.fadeIn(200, function() {
if (self.options.onOpen) {
self.options.onOpen();
}
});
setTimeout(function() {
$(window).resize();
});
});
},
'onShow': function(dialog) {
$('#simplemodal-container').corner('6px');
if (self.options.onShow) {
self.options.onShow();
}
},
'onClose': function(dialog, callback) {
dialog.data.hide().empty().remove();
dialog.container.hide().empty().remove();
dialog.overlay.fadeOut(200, function() {
dialog.overlay.empty().remove();
$.modal.close(callback);
});
$('.NB-modal-holder').empty().remove();
}
});
},
switch_tab: function(newtab) {
var $modal_tabs = $('.NB-modal-tab', this.$modal);
var $tabs = $('.NB-tab', this.$modal);
$modal_tabs.removeClass('NB-active');
$tabs.removeClass('NB-active');
$modal_tabs.filter('.NB-modal-tab-'+newtab).addClass('NB-active');
$tabs.filter('.NB-tab-'+newtab).addClass('NB-active');
},
connect: function(service) {
var options = "location=0,status=0,width=800,height=500";
var url = "/social/" + service + "_connect";
this.connect_window = window.open(url, '_blank', options);
},
disconnect: function(service) {
var $service = $('.NB-friends-service-'+service, this.$modal);
$('.NB-friends-service-connect', $service).text('Disconnecting...');
this.model.disconnect_social_service(service, _.bind(this.make_friends, this));
},
post_connect: function(data) {
if (data.error) {
var $error = $.make('div', { className: 'DV-error' }, data.error);
$('.NB-friends-services', this.$modal).append($error);
} else {
this.fetch_friends();
}
},
// ===========
// = Actions =
// ===========
handle_click: function(elem, e) {
var self = this;
$.targetIs(e, { tagSelector: '.NB-modal-tab' }, function($t, $p) {
e.preventDefault();
var newtab;
if ($t.hasClass('NB-modal-tab-findfriends')) {
newtab = 'findfriends';
} else if ($t.hasClass('NB-modal-tab-followers')) {
newtab = 'followers';
} else if ($t.hasClass('NB-modal-tab-following')) {
newtab = 'following';
}
self.switch_tab(newtab);
});
$.targetIs(e, { tagSelector: '.NB-friends-service-connect' }, function($t, $p) {
e.preventDefault();
var service;
var $service = $t.closest('.NB-friends-service');
if ($service.hasClass('NB-friends-service-twitter')) {
service = 'twitter';
} else if ($service.hasClass('NB-friends-service-facebook')) {
service = 'facebook';
}
if ($service.hasClass('NB-connected')) {
self.disconnect(service);
} else {
self.connect(service);
}
});
$.targetIs(e, { tagSelector: '.NB-modal-cancel' }, function($t, $p) {
e.preventDefault();
self.close();
});
},
handle_cancel: function() {
var $cancel = $('.NB-modal-cancel', this.$modal);
$cancel.click(function(e) {
e.preventDefault();
$.modal.close();
});
},
handle_change: function() {
$('input[type=radio],input[type=checkbox],select,input', this.$modal).bind('change', _.bind(this.enable_save, this));
$('input', this.$modal).bind('keydown', _.bind(this.enable_save, this));
},
enable_save: function() {
$('input[type=submit]', this.$modal).removeAttr('disabled').removeClass('NB-disabled').val('Save My Account');
},
disable_save: function() {
this.resize();
$('input[type=submit]', this.$modal).attr('disabled', true).addClass('NB-disabled').val('Change what you like above...');
}
});