2011-07-27 09:33:47 -07:00
NEWSBLUR . ReaderAccount = function ( options ) {
2011-09-21 17:49:26 -07:00
var defaults = {
2013-01-08 14:11:59 -08:00
'width' : 700 ,
2011-09-21 17:49:26 -07:00
'animate_email' : false ,
2011-09-22 09:23:42 -07:00
'change_password' : false ,
2011-09-21 17:49:26 -07:00
'onOpen' : _ . bind ( function ( ) {
2011-09-22 09:23:42 -07:00
this . animate _fields ( ) ;
2011-09-21 17:49:26 -07:00
} , this )
} ;
2011-07-27 09:33:47 -07:00
this . options = $ . extend ( { } , defaults , options ) ;
2012-05-17 18:40:46 -07:00
this . model = NEWSBLUR . assets ;
2011-07-27 09:33:47 -07:00
this . runner ( ) ;
} ;
NEWSBLUR . ReaderAccount . prototype = new NEWSBLUR . Modal ;
NEWSBLUR . ReaderAccount . prototype . constructor = NEWSBLUR . ReaderAccount ;
_ . extend ( NEWSBLUR . ReaderAccount . prototype , {
runner : function ( ) {
2013-11-13 10:07:21 -08:00
this . options . onOpen = _ . bind ( function ( ) {
// $(window).resize();
} , this ) ;
2011-07-27 09:33:47 -07:00
this . make _modal ( ) ;
this . open _modal ( ) ;
this . $modal . bind ( 'click' , $ . rescope ( this . handle _click , this ) ) ;
2011-07-27 22:17:34 -07:00
this . handle _change ( ) ;
2011-09-21 17:49:26 -07:00
this . select _preferences ( ) ;
2012-12-03 15:03:47 -08:00
if ( NEWSBLUR . Globals . is _premium ) {
this . fetch _payment _history ( ) ;
}
2011-07-27 09:33:47 -07:00
} ,
make _modal : function ( ) {
var self = this ;
this . $modal = $ . make ( 'div' , { className : 'NB-modal-preferences NB-modal-account NB-modal' } , [
2013-04-11 16:50:18 -07:00
$ . make ( 'div' , { className : 'NB-modal-tabs' } , [
$ . make ( 'div' , { className : 'NB-modal-loading' } ) ,
$ . make ( 'div' , { className : 'NB-modal-tab NB-active NB-modal-tab-account' } , 'Account' ) ,
$ . make ( 'div' , { className : 'NB-modal-tab NB-modal-tab-premium' } , 'Payments' ) ,
$ . make ( 'div' , { className : 'NB-modal-tab NB-modal-tab-emails' } , 'Emails' )
] ) ,
2013-04-05 11:26:25 -07:00
$ . make ( 'h2' , { className : 'NB-modal-title' } , [
$ . make ( 'div' , { className : 'NB-icon' } ) ,
2013-05-22 17:05:30 -07:00
'Account' ,
$ . make ( 'div' , { className : 'NB-icon-dropdown' } )
2013-04-05 11:26:25 -07:00
] ) ,
2011-07-27 09:33:47 -07:00
$ . make ( 'form' , { className : 'NB-preferences-form' } , [
2013-04-11 16:50:18 -07:00
$ . make ( 'div' , { className : 'NB-tab NB-tab-account NB-active' } , [
$ . make ( 'div' , { className : 'NB-preference NB-preference-username' } , [
$ . make ( 'div' , { className : 'NB-preference-options' } , [
$ . make ( 'div' , { className : 'NB-preference-option' } , [
$ . make ( 'input' , { id : 'NB-preference-username' , type : 'text' , name : 'username' , value : NEWSBLUR . Globals . username } )
] )
] ) ,
$ . make ( 'div' , { className : 'NB-preference-label' } , [
$ . make ( 'label' , { 'for' : 'NB-preference-username' } , 'Username' ) ,
$ . make ( 'div' , { className : 'NB-preference-error' } )
2011-07-27 09:33:47 -07:00
] )
] ) ,
2013-04-11 16:50:18 -07:00
$ . make ( 'div' , { className : 'NB-preference NB-preference-email' } , [
$ . make ( 'div' , { className : 'NB-preference-options' } , [
$ . make ( 'div' , { className : 'NB-preference-option' } , [
$ . make ( 'input' , { id : 'NB-preference-email' , type : 'text' , name : 'email' , value : NEWSBLUR . Globals . email } )
] )
] ) ,
$ . make ( 'div' , { className : 'NB-preference-label' } , [
$ . make ( 'label' , { 'for' : 'NB-preference-email' } , 'Email address' ) ,
2011-07-27 09:33:47 -07:00
2013-04-11 16:50:18 -07:00
$ . make ( 'div' , { className : 'NB-preference-error' } )
2011-07-27 09:33:47 -07:00
] )
] ) ,
2013-04-11 16:50:18 -07:00
$ . make ( 'div' , { className : 'NB-preference NB-preference-password' } , [
$ . make ( 'div' , { className : 'NB-preference-options' } , [
$ . make ( 'div' , { className : 'NB-preference-option' , style : ( this . options . change _password ? 'opacity: .2' : '' ) } , [
$ . make ( 'label' , { 'for' : 'NB-preference-password-old' } , 'Old password' ) ,
$ . make ( 'input' , { id : 'NB-preference-password-old' , type : 'password' , name : 'old_password' , value : '' } )
] ) ,
$ . make ( 'div' , { className : 'NB-preference-option' } , [
$ . make ( 'label' , { 'for' : 'NB-preference-password-new' } , 'New password' ) ,
$ . make ( 'input' , { id : 'NB-preference-password-new' , type : 'password' , name : 'new_password' , value : '' } )
] )
2011-07-27 09:33:47 -07:00
] ) ,
2013-04-11 16:50:18 -07:00
$ . make ( 'div' , { className : 'NB-preference-label' } , [
'Change password' ,
$ . make ( 'div' , { className : 'NB-preference-error' } )
2011-07-27 09:33:47 -07:00
] )
] ) ,
2013-04-11 16:50:18 -07:00
$ . make ( 'div' , { className : 'NB-preference NB-preference-opml' } , [
$ . make ( 'div' , { className : 'NB-preference-options' } , [
$ . make ( 'a' , { className : 'NB-splash-link' , href : NEWSBLUR . URLs [ 'opml-export' ] } , 'Download OPML' )
] ) ,
$ . make ( 'div' , { className : 'NB-preference-label' } , [
'Backup your sites' ,
$ . make ( 'div' , { className : 'NB-preference-sublabel' } , 'Download this XML file as a backup' )
] )
2011-07-27 09:33:47 -07:00
] ) ,
2013-04-11 16:50:18 -07:00
$ . make ( 'div' , { className : 'NB-preference NB-preference-delete' } , [
$ . make ( 'div' , { className : 'NB-preference-options' } , [
2013-05-23 18:14:21 -07:00
$ . make ( 'div' , { className : 'NB-splash-link NB-account-delete-all-sites' } , 'Delete all of my sites' )
2013-04-11 16:50:18 -07:00
] ) ,
$ . make ( 'div' , { className : 'NB-preference-label' } , [
'Erase yourself' ,
2013-05-23 18:14:21 -07:00
$ . make ( 'div' , { className : 'NB-preference-sublabel' } , 'Notice: You will be emailed a backup of your sites' )
] )
] ) ,
$ . make ( 'div' , { className : 'NB-preference NB-preference-delete' } , [
$ . make ( 'div' , { className : 'NB-preference-options' } , [
$ . make ( 'a' , { className : 'NB-splash-link' , href : NEWSBLUR . URLs [ 'delete-account' ] } , 'Delete my account' )
] ) ,
$ . make ( 'div' , { className : 'NB-preference-label' } , [
'Erase yourself permanently' ,
2013-04-11 16:50:18 -07:00
$ . make ( 'div' , { className : 'NB-preference-sublabel' } , 'Warning: This is actually permanent' )
] )
2011-07-27 09:33:47 -07:00
] )
] ) ,
2013-04-11 16:50:18 -07:00
$ . make ( 'div' , { className : 'NB-tab NB-tab-premium' } , [
$ . make ( 'div' , { className : 'NB-preference NB-preference-premium' } , [
$ . make ( 'div' , { className : 'NB-preference-options' } , [
2014-04-03 12:44:04 -07:00
( ! NEWSBLUR . Globals . is _premium && $ . make ( 'div' , [
$ . make ( 'div' , 'You have a free account. Please consider upgrading to support NewsBlur.' )
$ . make ( 'a' , {
className : 'NB-modal-submit-button NB-modal-submit-green NB-account-premium-modal'
} , 'Go Premium!' )
] ) ,
2013-04-11 16:50:18 -07:00
( NEWSBLUR . Globals . is _premium && $ . make ( 'div' , [
'Thank you! You have a ' ,
$ . make ( 'b' , 'premium account' ) ,
'.' ,
$ . make ( 'div' , { className : 'NB-block' } , 'Your premium account will expire on:' ) ,
$ . make ( 'div' , { className : 'NB-block' } , [
$ . make ( 'span' , { className : 'NB-raquo' } , '»' ) ,
' ' ,
NEWSBLUR . utils . format _date ( NEWSBLUR . Globals . premium _expire )
] ) ,
$ . make ( 'a' , { href : '#' , className : 'NB-block NB-account-premium-renew NB-splash-link' } , 'Renew and change your payment amount' ) ,
$ . make ( 'a' , { href : '#' , className : 'NB-block NB-account-premium-cancel NB-splash-link' } , 'Cancel subscription renewal' )
] ) )
] ) ,
$ . make ( 'div' , { className : 'NB-preference-label' } , [
'Premium'
2012-12-03 15:03:47 -08:00
] )
] ) ,
2013-04-11 16:50:18 -07:00
$ . make ( 'div' , { className : 'NB-preference NB-preference-premium-history' } , [
$ . make ( 'div' , { className : 'NB-preference-options' } , [
$ . make ( 'ul' , { className : 'NB-account-payments' } , [
$ . make ( 'li' , { className : 'NB-payments-loading' } , 'Loading...' )
2011-09-21 17:49:26 -07:00
] )
] ) ,
2013-04-11 16:50:18 -07:00
$ . make ( 'div' , { className : 'NB-preference-label' } , [
'Payment history'
2011-09-21 17:49:26 -07:00
] )
] )
] ) ,
2013-04-11 16:50:18 -07:00
$ . make ( 'div' , { className : 'NB-tab NB-tab-emails' } , [
$ . make ( 'div' , { className : 'NB-preference NB-preference-emails' } , [
$ . make ( 'div' , { className : 'NB-preference-options' } , [
$ . make ( 'div' , [
$ . make ( 'input' , { id : 'NB-preference-emails-1' , type : 'radio' , name : 'send_emails' , value : 'true' } ) ,
$ . make ( 'label' , { 'for' : 'NB-preference-emails-1' } , [
'Email replies, re-shares, and new followers'
] )
] ) ,
$ . make ( 'div' , [
$ . make ( 'input' , { id : 'NB-preference-emails-2' , type : 'radio' , name : 'send_emails' , value : 'false' } ) ,
$ . make ( 'label' , { 'for' : 'NB-preference-emails-2' } , [
'Never ever send me an email'
] )
] )
] ) ,
$ . make ( 'div' , { className : 'NB-preference-label' } , [
'Emails'
] )
2013-01-03 10:33:22 -08:00
] )
] ) ,
2011-07-27 09:33:47 -07:00
$ . make ( 'div' , { className : 'NB-modal-submit' } , [
2013-04-11 16:50:18 -07:00
$ . make ( 'input' , { type : 'submit' , disabled : 'true' , className : 'NB-modal-submit-button NB-modal-submit-green NB-disabled' , value : 'Change what you like above...' } )
2011-07-27 09:33:47 -07:00
] )
2011-07-27 22:17:34 -07:00
] ) . bind ( 'submit' , function ( e ) {
e . preventDefault ( ) ;
self . save _account _settings ( ) ;
return false ;
} )
2011-07-27 09:33:47 -07:00
] ) ;
} ,
2011-09-22 09:23:42 -07:00
animate _fields : function ( ) {
2011-09-21 17:49:26 -07:00
if ( this . options . animate _email ) {
2013-04-11 16:50:18 -07:00
this . switch _tab ( 'emails' ) ;
2011-09-21 17:49:26 -07:00
_ . delay ( _ . bind ( function ( ) {
var $emails = $ ( '.NB-preference-emails' , this . $modal ) ;
var bgcolor = $emails . css ( 'backgroundColor' ) ;
$emails . css ( 'backgroundColor' , bgcolor ) . animate ( {
'backgroundColor' : 'orange'
} , {
'queue' : false ,
'duration' : 1200 ,
'easing' : 'easeInQuad' ,
'complete' : function ( ) {
$emails . animate ( {
'backgroundColor' : bgcolor
} , {
'queue' : false ,
'duration' : 650 ,
'easing' : 'easeOutQuad'
} ) ;
}
} ) ;
} , this ) , 200 ) ;
2011-09-22 09:23:42 -07:00
} else if ( this . options . change _password ) {
_ . delay ( _ . bind ( function ( ) {
var $emails = $ ( '.NB-preference-password' , this . $modal ) ;
var bgcolor = $emails . css ( 'backgroundColor' ) ;
$emails . css ( 'backgroundColor' , bgcolor ) . animate ( {
'backgroundColor' : 'orange'
} , {
'queue' : false ,
'duration' : 1200 ,
'easing' : 'easeInQuad' ,
'complete' : function ( ) {
$emails . animate ( {
'backgroundColor' : bgcolor
} , {
'queue' : false ,
'duration' : 650 ,
'easing' : 'easeOutQuad'
} ) ;
}
} ) ;
} , this ) , 200 ) ;
2011-09-21 17:49:26 -07:00
}
2011-09-22 09:23:42 -07:00
2011-09-21 17:49:26 -07:00
} ,
2011-07-27 09:33:47 -07:00
close _and _load _premium : function ( ) {
this . close ( function ( ) {
NEWSBLUR . reader . open _feedchooser _modal ( ) ;
} ) ;
} ,
2013-03-13 15:46:51 -07:00
cancel _premium : function ( ) {
this . model . cancel _premium _subscription ( _ . bind ( function ( data ) {
$ ( ".NB-preference-premium .NB-error" ) . remove ( ) ;
$ ( ".NB-preference-premium .NB-preference-options" ) . append ( $ . make ( "div" , { className : "NB-error" } , "Your subscription will no longer automatically renew." ) . fadeIn ( 500 ) ) ;
} , this ) , _ . bind ( function ( data ) {
$ ( ".NB-preference-premium .NB-error" ) . remove ( ) ;
$ ( ".NB-preference-premium .NB-preference-options" ) . append ( $ . make ( "div" , { className : "NB-error" } , data . message || "Could not cancel your membership. Contact support." ) . fadeIn ( 500 ) ) ;
} , this ) ) ;
} ,
2013-05-23 18:14:21 -07:00
delete _all _sites : function ( ) {
var $link = $ ( ".NB-account-delete-all-sites" , this . $modal ) ;
if ( window . confirm ( "Positive you want to delete everything?" ) ) {
NEWSBLUR . assets . delete _all _sites ( _ . bind ( function ( ) {
NEWSBLUR . assets . load _feeds ( ) ;
$link . replaceWith ( $ . make ( 'div' , 'Everything has been deleted.' ) ) ;
} , this ) , _ . bind ( function ( ) {
$link . replaceWith ( $ . make ( 'div' , { className : 'NB-error' } , 'There was a problem deleting your sites.' ) ) ;
} , this ) ) ;
}
} ,
2011-07-27 22:17:34 -07:00
handle _cancel : function ( ) {
var $cancel = $ ( '.NB-modal-cancel' , this . $modal ) ;
$cancel . click ( function ( e ) {
e . preventDefault ( ) ;
$ . modal . close ( ) ;
} ) ;
} ,
2011-09-21 17:49:26 -07:00
select _preferences : function ( ) {
var pref = this . model . preference ;
$ ( 'input[name=send_emails]' , this . $modal ) . each ( function ( ) {
if ( $ ( this ) . val ( ) == "" + pref ( 'send_emails' ) ) {
$ ( this ) . attr ( 'checked' , true ) ;
return false ;
}
} ) ;
} ,
2011-07-27 22:17:34 -07:00
serialize _preferences : function ( ) {
var preferences = { } ;
2011-09-21 17:49:26 -07:00
$ ( 'input[type=radio]:checked, select, input[type=text], input[type=password]' , this . $modal ) . each ( function ( ) {
2011-07-27 22:17:34 -07:00
var name = $ ( this ) . attr ( 'name' ) ;
var preference = preferences [ name ] = $ ( this ) . val ( ) ;
if ( preference == 'true' ) preferences [ name ] = true ;
else if ( preference == 'false' ) preferences [ name ] = false ;
} ) ;
$ ( 'input[type=checkbox]' , this . $modal ) . each ( function ( ) {
preferences [ $ ( this ) . attr ( 'name' ) ] = $ ( this ) . is ( ':checked' ) ;
} ) ;
return preferences ;
} ,
save _account _settings : function ( ) {
var self = this ;
var form = this . serialize _preferences ( ) ;
$ ( '.NB-preference-error' , this . $modal ) . text ( '' ) ;
$ ( 'input[type=submit]' , this . $modal ) . val ( 'Saving...' ) . attr ( 'disabled' , true ) . addClass ( 'NB-disabled' ) ;
2012-07-10 15:26:34 -07:00
NEWSBLUR . log ( [ "form['send_emails']" , form [ 'send_emails' ] ] ) ;
2011-09-21 17:49:26 -07:00
this . model . preference ( 'send_emails' , form [ 'send_emails' ] ) ;
2011-07-27 22:17:34 -07:00
this . model . save _account _settings ( form , function ( data ) {
if ( data . code == - 1 ) {
$ ( '.NB-preference-username .NB-preference-error' , this . $modal ) . text ( data . message ) ;
return self . disable _save ( ) ;
} else if ( data . code == - 2 ) {
$ ( '.NB-preference-email .NB-preference-error' , this . $modal ) . text ( data . message ) ;
return self . disable _save ( ) ;
} else if ( data . code == - 3 ) {
$ ( '.NB-preference-password .NB-preference-error' , this . $modal ) . text ( data . message ) ;
return self . disable _save ( ) ;
}
NEWSBLUR . Globals . username = data . payload . username ;
NEWSBLUR . Globals . email = data . payload . email ;
$ ( '.NB-module-account-username' ) . text ( NEWSBLUR . Globals . username ) ;
2013-05-06 15:21:17 -07:00
$ ( '.NB-feeds-header-user-name' ) . text ( NEWSBLUR . Globals . username ) ;
2011-07-27 22:17:34 -07:00
self . close ( ) ;
} ) ;
} ,
2012-12-03 15:03:47 -08:00
fetch _payment _history : function ( ) {
2013-05-10 12:05:24 -07:00
this . model . fetch _payment _history ( NEWSBLUR . Globals . user _id , _ . bind ( function ( data ) {
2012-12-03 15:03:47 -08:00
var $history = $ ( '.NB-account-payments' , this . $modal ) . empty ( ) ;
_ . each ( data . payments , function ( payment ) {
$history . append ( $ . make ( 'li' , { className : 'NB-account-payment' } , [
$ . make ( 'div' , { className : 'NB-account-payment-date' } , payment . payment _date ) ,
$ . make ( 'div' , { className : 'NB-account-payment-amount' } , "$" + payment . payment _amount ) ,
$ . make ( 'div' , { className : 'NB-account-payment-provider' } , payment . payment _provider )
] ) ) ;
} ) ;
2012-12-04 10:46:14 -08:00
$ ( window ) . resize ( ) ;
2012-12-03 15:03:47 -08:00
} , this ) ) ;
} ,
2011-07-27 09:33:47 -07:00
// ===========
// = Actions =
// ===========
handle _click : function ( elem , e ) {
var self = this ;
2013-04-11 16:50:18 -07:00
$ . targetIs ( e , { tagSelector : '.NB-modal-tab' } , function ( $t , $p ) {
e . preventDefault ( ) ;
var newtab ;
if ( $t . hasClass ( 'NB-modal-tab-account' ) ) {
newtab = 'account' ;
} else if ( $t . hasClass ( 'NB-modal-tab-premium' ) ) {
newtab = 'premium' ;
} else if ( $t . hasClass ( 'NB-modal-tab-emails' ) ) {
newtab = 'emails' ;
}
self . switch _tab ( newtab ) ;
} ) ;
2011-07-27 09:33:47 -07:00
$ . targetIs ( e , { tagSelector : '.NB-account-premium-modal' } , function ( $t , $p ) {
e . preventDefault ( ) ;
2013-03-04 11:12:10 -08:00
self . close _and _load _premium ( ) ;
} ) ;
$ . targetIs ( e , { tagSelector : '.NB-account-premium-renew' } , function ( $t , $p ) {
e . preventDefault ( ) ;
2011-07-27 09:33:47 -07:00
self . close _and _load _premium ( ) ;
} ) ;
2013-03-13 15:46:51 -07:00
$ . targetIs ( e , { tagSelector : '.NB-account-premium-cancel' } , function ( $t , $p ) {
e . preventDefault ( ) ;
self . cancel _premium ( ) ;
2013-05-23 18:14:21 -07:00
} ) ;
$ . targetIs ( e , { tagSelector : '.NB-account-delete-all-sites' } , function ( $t , $p ) {
e . preventDefault ( ) ;
self . delete _all _sites ( ) ;
2013-03-13 15:46:51 -07:00
} ) ;
2011-07-27 22:17:34 -07:00
$ . targetIs ( e , { tagSelector : '.NB-modal-cancel' } , function ( $t , $p ) {
e . preventDefault ( ) ;
self . 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...' ) ;
2011-07-27 09:33:47 -07:00
}
} ) ;