2012-08-01 18:52:54 -07:00
NEWSBLUR . ReaderSendEmail = function ( story , 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 ) ;
2012-05-17 18:40:46 -07:00
this . model = NEWSBLUR . assets ;
2012-08-01 18:52:54 -07:00
this . story = story ;
2012-06-19 11:56:04 -07:00
this . feed _id = this . story . get ( 'story_feed_id' ) ;
2011-05-06 10:06:13 -04:00
this . feed = this . model . get _feed ( this . feed _id ) ;
2011-05-03 11:40:38 -04:00
this . runner ( ) ;
} ;
2012-04-11 18:09:35 -07:00
NEWSBLUR . ReaderSendEmail . prototype = new NEWSBLUR . Modal ;
_ . extend ( NEWSBLUR . ReaderSendEmail . prototype , {
2011-05-03 11:40:38 -04:00
runner : function ( ) {
2012-07-01 14:20:21 -07:00
_ . bindAll ( this , 'save' ) ;
this . options . onOpen = _ . bind ( function ( ) {
$ ( 'input[name=to]' , this . $modal ) . focus ( ) ;
} , this ) ;
2011-05-03 11:40:38 -04:00
this . make _modal ( ) ;
this . open _modal ( ) ;
2013-01-02 13:38:04 -08:00
this . existing _emails = $ . evalJSON ( $ . cookie ( 'NB:email:addresses' ) ) || [ ] ;
this . autocomplete _emails ( ) ;
2011-05-03 11:40:38 -04:00
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 ) ) ;
2012-07-01 14:20:21 -07:00
$ ( 'input, textarea' , this . $modal ) . bind ( 'keydown' , 'ctrl+return' , this . save ) ;
$ ( 'input, textarea' , this . $modal ) . bind ( 'keydown' , 'meta+return' , this . save ) ;
2011-05-03 11:40:38 -04:00
} ,
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' } , [
2013-02-14 15:59:03 -08:00
( this . feed && $ . make ( 'div' , { className : 'NB-modal-email-feed' } , [
2012-01-26 18:59:40 -08:00
$ . make ( 'img' , { className : 'NB-modal-feed-image feed_favicon' , src : $ . favicon ( this . feed ) } ) ,
2012-05-21 20:08:27 -07:00
$ . make ( 'div' , { className : 'NB-modal-feed-title' } , this . feed . get ( 'feed_title' ) )
2013-02-14 15:59:03 -08:00
] ) ) ,
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' } , [
2013-05-29 11:28:54 -07:00
' Recipient emails: '
2011-05-07 21:12:13 -04:00
] ) ,
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' } , [
' 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' } , [
' 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' ) || '' } )
2013-01-23 11:06:28 -08:00
] ) ,
$ . make ( 'div' , { style : 'margin-top: 8px' } , [
$ . make ( 'label' , { 'for' : 'NB-send-email-cc' } , [
' CC me: '
] ) ,
$ . make ( 'div' , { className : 'NB-modal-email-cc-wrapper' } , [
$ . make ( 'input' , { className : 'NB-modal-email-cc' , name : 'email_cc' , id : 'NB-send-email-cc' , type : "checkbox" , checked : this . model . preference ( 'email_cc' ) } ) ,
$ . make ( 'label' , { 'for' : 'NB-send-email-cc' } , [
"Yes, send me a copy of this email"
] )
] )
2011-05-07 21:12:13 -04:00
] )
2011-05-03 11:40:38 -04:00
] ) ,
2011-05-03 20:59:07 -04:00
$ . make ( 'form' , { className : 'NB-recommend-form' } , [
$ . make ( 'div' , { className : 'NB-modal-submit' } , [
2013-04-03 11:57:40 -07:00
$ . make ( 'input' , { type : 'submit' , className : 'NB-modal-submit-button NB-modal-submit-green' , value : 'Send this story' } ) ,
2011-05-03 20:59:07 -04:00
' or ' ,
2012-04-25 17:15:11 -07:00
$ . make ( 'a' , { href : '#' , className : 'NB-modal-emailclient' } , 'open in an email client' )
2018-07-13 11:20:31 -04:00
] ) ,
$ . make ( 'div' , { className : 'NB-error' } )
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 ( ) ;
2013-01-23 11:06:28 -08:00
var email _cc = $ ( 'input[name=email_cc]' , this . $modal ) . is ( ":checked" ) ;
2011-05-07 21:12:13 -04:00
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 ) ;
2013-01-23 11:06:28 -08:00
this . model . preference ( 'email_cc' , email _cc ) ;
2018-07-13 11:20:31 -04:00
$ ( '.NB-error' , this . $modal ) . hide ( ) ;
2011-05-06 10:06:13 -04:00
this . model . send _story _email ( {
2012-08-01 18:52:54 -07:00
story _id : this . story . id ,
2011-05-07 21:12:13 -04:00
feed _id : this . feed _id ,
from _name : from _name ,
from _email : from _email ,
2013-01-23 11:06:28 -08:00
email _cc : email _cc ,
2011-05-07 21:12:13 -04:00
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 ( ) ) ;
2013-01-02 13:38:04 -08:00
var emails = $ ( 'input[name=to]' , this . $modal ) . val ( ) ;
emails = emails . replace ( /[, ]+/g , ' ' ) . split ( ' ' ) ;
emails = _ . uniq ( this . existing _emails . concat ( emails ) ) ;
emails = _ . map ( emails , function ( e ) { return _ . string . trim ( e ) ; } ) ;
emails = _ . compact ( emails ) ;
2013-05-29 11:28:54 -07:00
$ . cookie ( 'NB:email:addresses' , $ . toJSON ( emails ) , { expires : 365 * 10 } ) ;
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 ) {
2018-07-13 11:20:31 -04:00
var $error = $ ( '.NB-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 ( ) ;
2018-07-13 11:20:31 -04:00
console . log ( [ 'Error sending email' , data ] ) ;
if ( ! data || ! data . message ) {
2012-01-23 10:16:50 -08:00
$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 {
2018-07-13 11:20:31 -04:00
$error . html ( data . message ) . fadeIn ( 500 ) ;
2012-01-23 10:16:50 -08:00
}
$save . removeClass ( 'NB-disabled' ) . val ( 'Send this story' ) ;
2011-10-28 10:29:11 -07:00
$ ( '.NB-modal-loading' , this . $modal ) . removeClass ( 'NB-active' ) ;
2018-07-13 11:20:31 -04:00
this . resize ( ) ;
2011-10-28 10:29:11 -07:00
} ,
2012-04-25 17:15:11 -07:00
open _email _client : function ( ) {
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 url = [
'mailto:' ,
to ,
'?subject=' ,
from _name ,
' is sharing a story: ' ,
this . story . story _title ,
'&body=' ,
comments ,
'%0D%0A%0D%0A--%0D%0A%0D%0A' ,
this . story . story _permalink ,
'%0D%0A%0D%0A' ,
2020-06-16 14:56:37 -04:00
encodeURIComponent ( $ ( this . story . story _content ( ) ) . text ( ) ) ,
2012-04-25 17:15:11 -07:00
'%0D%0A%0D%0A' ,
'--' ,
'%0D%0A%0D%0A' ,
'Shared with NewsBlur.com'
] . join ( '' ) ;
window . open ( url ) ;
} ,
2013-01-02 13:38:04 -08:00
autocomplete _emails : function ( ) {
var $to = $ ( 'input[name=to]' , this . $modal ) ;
var existing _emails = this . existing _emails ;
var split = function ( val ) {
return val . split ( /,\s*/ ) ;
} ;
var extractLast = function ( term ) {
return split ( term ) . pop ( ) ;
} ;
$to
// don't navigate away from the field on tab when selecting an item
. bind ( "keydown" , function ( event ) {
if ( event . keyCode === $ . ui . keyCode . TAB &&
2013-07-02 18:26:57 -07:00
$ ( this ) . data ( "ui-autocomplete" ) . menu . active ) {
2013-01-02 13:38:04 -08:00
event . preventDefault ( ) ;
}
} )
. autocomplete ( {
delay : 0 ,
minLength : 0 ,
2013-07-03 15:37:40 -07:00
appendTo : '.NB-modal-email' ,
2013-01-02 13:38:04 -08:00
source : function ( request , response ) {
// delegate back to autocomplete, but extract the last term
2013-07-02 18:26:57 -07:00
console . log ( [ "autocomplete" , request , request . term , existing _emails ] ) ;
2013-01-02 13:38:04 -08:00
response ( $ . ui . autocomplete . filter (
existing _emails , extractLast ( request . term ) ) ) ;
} ,
focus : function ( ) {
// prevent value inserted on focus
return false ;
} ,
select : function ( event , ui ) {
var terms = split ( this . value ) ;
// remove the current input
terms . pop ( ) ;
// add the selected item
terms . push ( ui . item . value ) ;
// add placeholder to get the comma-and-space at the end
terms . push ( "" ) ;
this . value = terms . join ( ", " ) ;
return false ;
}
} ) ;
} ,
2011-05-03 11:40:38 -04:00
// ===========
// = Actions =
// ===========
handle _click : function ( elem , e ) {
var self = this ;
2013-04-03 11:57:40 -07:00
$ . targetIs ( e , { tagSelector : '.NB-modal-submit-button' } , 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
} ) ;
2012-04-25 17:15:11 -07:00
$ . targetIs ( e , { tagSelector : '.NB-modal-emailclient' } , function ( $t , $p ) {
2012-04-20 08:39:40 -07:00
e . preventDefault ( ) ;
2012-04-25 17:15:11 -07:00
self . open _email _client ( ) ;
2012-04-20 08:39:40 -07:00
return false ;
} ) ;
2011-05-03 11:40:38 -04:00
}
} ) ;