2010-09-24 01:08:03 -04:00
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 ( ) {
2010-09-27 11:43:08 -04:00
this . MAX _FEEDS = 40 ;
2010-09-24 01:08:03 -04:00
this . approve _list = [ ] ;
this . make _modal ( ) ;
this . open _modal ( ) ;
2010-09-27 11:43:08 -04:00
this . initial _load _feeds ( ) ;
2010-09-24 01:08:03 -04:00
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' } , [
2010-09-27 11:43:08 -04:00
$ . make ( 'input' , { type : 'submit' , disabled : 'true' , className : 'NB-disabled NB-modal-submit-save NB-modal-submit-green' , value : 'Check what you like above...' } ) ,
2010-09-24 01:08:03 -04:00
' or ' ,
$ . make ( 'a' , { href : '#' , className : 'NB-modal-cancel' } , 'cancel' )
] )
] ) . bind ( 'submit' , function ( e ) {
e . preventDefault ( ) ;
self . save _preferences ( ) ;
return false ;
} )
] ) ;
} ,
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' : ''
} ) ;
$ ( '.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 ) {
this . approve _list = _ . without ( this . approve _list , feed _id ) ;
var $feed = this . find _feed _in _feed _list ( feed _id ) ;
$feed . removeClass ( 'NB-feedchooser-approve' ) ;
$feed . addClass ( 'NB-feedchooser-decline' ) ;
this . update _counts ( ) ;
} ,
add _feed _to _approve : function ( feed _id ) {
if ( ! _ . contains ( this . approve _list , feed _id ) ) {
this . approve _list . push ( feed _id ) ;
var $feed = this . find _feed _in _feed _list ( feed _id ) ;
$feed . removeClass ( 'NB-feedchooser-decline' ) ;
$feed . addClass ( 'NB-feedchooser-approve' ) ;
}
this . update _counts ( ) ;
} ,
find _feed _in _feed _list : function ( feed _id ) {
var $feed _list = $ ( '.NB-feedchooser' , this . $modal ) ;
var $feeds = $ ( [ ] ) ;
$ ( '.feed' , $feed _list ) . each ( function ( ) {
if ( $ ( this ) . data ( 'feed_id' ) == feed _id ) {
$feeds . push ( $ ( this ) . get ( 0 ) ) ;
}
} ) ;
return $feeds ;
} ,
update _counts : function ( ) {
var $count = $ ( '.NB-feedchooser-info-counts' ) ;
var approved = this . approve _list . length ;
2010-09-27 11:43:08 -04:00
var $submit = $ ( '.NB-modal-submit-save' , this . $modal ) ;
var difference = approved - this . MAX _FEEDS ;
2010-09-24 01:08:03 -04:00
$count . text ( approved + '/' + this . MAX _FEEDS ) ;
$count . toggleClass ( 'NB-full' , approved == this . MAX _FEEDS ) ;
$count . toggleClass ( 'NB-error' , approved > this . MAX _FEEDS ) ;
2010-09-27 11:43:08 -04:00
if ( approved > this . MAX _FEEDS ) {
$submit . removeClass ( '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 + '.' ) ;
}
2010-09-24 01:08:03 -04:00
} ,
initial _load _feeds : function ( ) {
var self = this ;
var $feeds = $ ( '.feed' , this . $modal ) ;
2010-09-24 18:22:12 -04:00
// Get feed subscribers
2010-09-27 11:43:08 -04:00
var min _subscribers = _ . last (
_ . first (
_ . pluck ( this . model . get _feeds ( ) , 'subs' ) . sort ( function ( a , b ) {
return b - a ;
} ) ,
this . MAX _FEEDS
)
) ;
2010-09-24 18:22:12 -04:00
// Decline everything
var feeds = [ ] ;
2010-09-24 01:08:03 -04:00
$feeds . each ( function ( ) {
2010-09-24 18:22:12 -04:00
var feed _id = $ ( this ) . data ( 'feed_id' ) ;
self . add _feed _to _decline ( feed _id ) ;
if ( self . model . get _feed ( feed _id ) [ 'subs' ] >= min _subscribers ) {
feeds . push ( feed _id ) ;
}
} ) ;
// Approve feeds in subs
_ . each ( feeds , function ( feed _id ) {
2010-09-27 11:43:08 -04:00
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 ( feeds , function ( feed _id ) {
if ( self . model . get _feed ( feed _id ) [ 'subs' ] == min _subscribers &&
2010-09-24 18:22:12 -04:00
self . approve _list . length < self . MAX _FEEDS ) {
self . add _feed _to _approve ( feed _id ) ;
}
2010-09-24 01:08:03 -04:00
} ) ;
} ,
// ===========
// = 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 ) ;
} else {
this . add _feed _to _approve ( feed _id ) ;
}
} , this ) ) ;
2010-09-27 11:43:08 -04:00
$ . targetIs ( e , { tagSelector : '.NB-modal-submit-save' } , _ . bind ( function ( $t , $p ) {
e . preventDefault ( ) ;
} , this ) ) ;
2010-09-24 01:08:03 -04:00
} ,
handle _cancel : function ( ) {
var $cancel = $ ( '.NB-modal-cancel' , this . $modal ) ;
$cancel . click ( function ( e ) {
e . preventDefault ( ) ;
$ . modal . close ( ) ;
} ) ;
}
} ;