2011-05-03 20:59:07 -04:00
NEWSBLUR . ReaderSendEmail = function ( story _id , options ) {
2011-05-03 11:40:38 -04:00
var defaults = { } ;
2011-05-06 10:06:13 -04:00
2011-10-28 10:29:11 -07:00
_ . bindAll ( this , 'close' , 'save_callback' , 'error' ) ;
2011-05-03 11:40:38 -04:00
this . options = $ . extend ( { } , defaults , options ) ;
this . model = NEWSBLUR . AssetModel . reader ( ) ;
2011-05-03 20:59:07 -04:00
this . story _id = story _id ;
2011-05-03 11:40:38 -04:00
this . story = this . model . get _story ( story _id ) ;
2011-05-06 10:06:13 -04:00
this . feed _id = this . story . story _feed _id ;
this . feed = this . model . get _feed ( this . feed _id ) ;
2011-05-03 11:40:38 -04:00
this . runner ( ) ;
} ;
2011-05-03 20:59:07 -04:00
NEWSBLUR . ReaderSendEmail . prototype = _ . extend ( { } , NEWSBLUR . Modal . prototype , {
2011-05-03 11:40:38 -04:00
runner : function ( ) {
this . make _modal ( ) ;
this . open _modal ( ) ;
2011-05-09 10:30:07 -04:00
if ( ! NEWSBLUR . Globals . is _authenticated ) {
this . save _callback ( { 'code' : - 1 , 'message' : 'You must be logged in to send a story over email.' } ) ;
}
2011-05-03 11:40:38 -04:00
this . $modal . bind ( 'click' , $ . rescope ( this . handle _click , this ) ) ;
} ,
make _modal : function ( ) {
var self = this ;
2011-05-03 20:59:07 -04:00
this . $modal = $ . make ( 'div' , { className : 'NB-modal-email NB-modal' } , [
2011-05-07 17:58:53 -04:00
$ . make ( 'span' , { className : 'NB-modal-loading NB-spinner' } ) ,
2011-10-28 10:29:11 -07:00
$ . make ( 'div' , { className : 'NB-modal-error' } ) ,
2011-05-03 20:59:07 -04:00
$ . make ( 'h2' , { className : 'NB-modal-title' } , 'Send Story by Email' ) ,
$ . make ( 'h2' , { className : 'NB-modal-subtitle' } , [
2011-05-06 10:06:13 -04:00
$ . make ( 'div' , { className : 'NB-modal-email-feed' } , [
$ . make ( 'img' , { className : 'NB-modal-feed-image feed_favicon' , src : $ . favicon ( this . feed . favicon ) } ) ,
$ . make ( 'div' , { className : 'NB-modal-feed-title' } , this . feed . feed _title )
] ) ,
2011-05-07 21:12:13 -04:00
$ . make ( 'div' , { className : 'NB-modal-email-story-title' } , this . story . story _title ) ,
2011-05-03 20:59:07 -04:00
$ . make ( 'div' , { className : 'NB-modal-email-story-permalink' } , this . story . story _permalink )
2011-05-03 11:40:38 -04:00
] ) ,
2011-05-06 10:06:13 -04:00
$ . make ( 'div' , { className : 'NB-modal-email-to-container' } , [
2011-05-07 21:12:13 -04:00
$ . make ( 'label' , { 'for' : 'NB-send-email-to' } , [
$ . make ( 'span' , { className : 'NB-raquo' } , '»' ) ,
' Recipient\'s email: '
] ) ,
2012-01-16 09:47:54 -08:00
$ . make ( 'input' , { className : 'NB-input NB-modal-to' , name : 'to' , id : 'NB-send-email-to' , value :
( $ . cookie ( 'NB:email:to' ) || "" ) } )
2011-05-06 10:06:13 -04:00
] ) ,
2011-05-03 20:59:07 -04:00
$ . make ( 'div' , { className : 'NB-modal-email-explanation' } , [
2011-05-06 10:06:13 -04:00
"Add an optional comment to send with the story. The story will be sent below your comment."
2011-05-03 11:40:38 -04:00
] ) ,
2011-05-03 20:59:07 -04:00
$ . make ( 'div' , { className : 'NB-modal-email-comments-container' } , [
$ . make ( 'textarea' , { className : 'NB-modal-email-comments' } )
2011-05-03 11:40:38 -04:00
] ) ,
2011-05-06 10:06:13 -04:00
$ . make ( 'div' , { className : 'NB-modal-email-from-container' } , [
2011-05-07 21:12:13 -04:00
$ . make ( 'div' , [
$ . make ( 'label' , { 'for' : 'NB-send-email-from-name' } , [
$ . make ( 'span' , { className : 'NB-raquo' } , '»' ) ,
' Your name: '
] ) ,
$ . make ( 'input' , { className : 'NB-input NB-modal-email-from' , name : 'from_name' , id : 'NB-send-email-from-name' , value : this . model . preference ( 'full_name' ) || NEWSBLUR . Globals . username || '' } )
] ) ,
$ . make ( 'div' , { style : 'margin-top: 8px' } , [
$ . make ( 'label' , { 'for' : 'NB-send-email-from-email' } , [
$ . make ( 'span' , { className : 'NB-raquo' } , '»' ) ,
' Your email: '
] ) ,
$ . make ( 'input' , { className : 'NB-input NB-modal-email-from' , name : 'from_email' , id : 'NB-send-email-from-email' , value : NEWSBLUR . Globals . email || this . model . preference ( 'email' ) || '' } )
] )
2011-05-03 11:40:38 -04:00
] ) ,
2011-05-03 20:59:07 -04:00
$ . make ( 'form' , { className : 'NB-recommend-form' } , [
2011-05-07 21:12:13 -04:00
$ . make ( 'div' , { className : 'NB-error' } ) ,
2011-05-03 20:59:07 -04:00
$ . make ( 'div' , { className : 'NB-modal-submit' } , [
$ . make ( 'input' , { type : 'submit' , className : 'NB-modal-submit-save NB-modal-submit-green' , value : 'Send this story' } ) ,
' or ' ,
$ . make ( 'a' , { href : '#' , className : 'NB-modal-cancel' } , 'cancel' )
] )
2011-05-03 11:40:38 -04:00
] )
] ) ;
} ,
2011-05-06 10:06:13 -04:00
save : function ( e ) {
2011-05-07 21:12:13 -04:00
var from _name = $ ( 'input[name=from_name]' , this . $modal ) . val ( ) ;
var from _email = $ ( 'input[name=from_email]' , this . $modal ) . val ( ) ;
var to = $ ( 'input[name=to]' , this . $modal ) . val ( ) ;
var comments = $ ( 'textarea' , this . $modal ) . val ( ) ;
var $save = $ ( 'input[type=submit]' , this . $modal ) ;
2011-10-28 10:29:11 -07:00
var $error = $ ( '.NB-modal-error' , this . $modal ) ;
2011-05-06 10:06:13 -04:00
2011-10-28 10:29:11 -07:00
$error . hide ( ) ;
2011-05-06 10:06:13 -04:00
$save . addClass ( 'NB-disabled' ) . val ( 'Sending...' ) ;
2011-05-07 17:58:53 -04:00
$ ( '.NB-modal-loading' , this . $modal ) . addClass ( 'NB-active' ) ;
2011-05-07 21:12:13 -04:00
this . model . preference ( 'full_name' , from _name ) ;
this . model . preference ( 'email' , from _email ) ;
$ ( '.NB-error' , this . $modal ) . fadeOut ( 500 ) ;
2011-05-06 10:06:13 -04:00
this . model . send _story _email ( {
2011-05-07 21:12:13 -04:00
story _id : this . story _id ,
feed _id : this . feed _id ,
from _name : from _name ,
from _email : from _email ,
to : to ,
comments : comments
2011-10-28 10:29:11 -07:00
} , this . save _callback , this . error ) ;
2011-05-09 10:30:07 -04:00
} ,
save _callback : function ( data ) {
var $save = $ ( 'input[type=submit]' , this . $modal ) ;
if ( ! data || data . code < 0 ) {
$ ( '.NB-error' , this . $modal ) . html ( data . message ) . fadeIn ( 500 ) ;
$ ( '.NB-modal-loading' , this . $modal ) . removeClass ( 'NB-active' ) ;
$save . removeClass ( 'NB-disabled' ) . val ( 'Send this story' ) ;
} else {
$save . val ( 'Sent!' ) ;
2012-01-16 09:47:54 -08:00
$ . cookie ( 'NB:email:to' , $ ( 'input[name=to]' , this . $modal ) . val ( ) ) ;
2011-05-09 10:30:07 -04:00
this . close ( ) ;
}
2011-05-06 10:06:13 -04:00
} ,
2011-10-28 10:29:11 -07:00
error : function ( data ) {
var $error = $ ( '.NB-modal-error' , this . $modal ) ;
2012-01-23 10:16:50 -08:00
var $save = $ ( 'input[type=submit]' , this . $modal ) ;
2011-10-28 10:29:11 -07:00
$error . show ( ) ;
2012-01-23 10:16:50 -08:00
if ( ! data ) {
$error . text ( "There was a issue on the backend with sending your email. Sorry about this! It has been noted and will be fixed soon. You should probably send this manually now." ) ;
} else {
$ ( '.NB-error' , this . $modal ) . html ( data . message ) . fadeIn ( 500 ) ;
}
$save . removeClass ( 'NB-disabled' ) . val ( 'Send this story' ) ;
2011-10-28 10:29:11 -07:00
$ ( '.NB-modal-loading' , this . $modal ) . removeClass ( 'NB-active' ) ;
} ,
2011-05-03 11:40:38 -04:00
// ===========
// = Actions =
// ===========
handle _click : function ( elem , e ) {
var self = this ;
2011-05-06 10:06:13 -04:00
$ . targetIs ( e , { tagSelector : '.NB-modal-submit-save' } , function ( $t , $p ) {
2011-05-03 11:40:38 -04:00
e . preventDefault ( ) ;
2011-05-06 10:06:13 -04:00
self . save ( ) ;
return false ;
2011-05-03 11:40:38 -04:00
} ) ;
}
} ) ;