2013-01-31 21:55:51 +00:00
var cookie = require ( 'ep_etherpad-lite/static/js/pad_cookie' ) . padcookie ;
2013-03-30 00:34:19 +00:00
var optionsAlreadyRecovered = false ;
2013-01-31 21:55:51 +00:00
2013-01-31 15:58:19 +00:00
if ( typeof exports == 'undefined' ) {
2013-01-31 15:58:32 +00:00
var exports = this [ 'mymodule' ] = { } ;
2013-01-31 15:58:19 +00:00
}
2013-01-31 15:49:45 +00:00
exports . postAceInit = function ( hook , context ) {
2013-03-30 14:56:10 +00:00
// Test if settings are good before continuing
if ( typeof clientVars . panelDisplayLocation != "object" ) {
$ . gritter . add ( {
// (string | mandatory) the heading of the notification
title : "Email subscription error" ,
// (string | mandatory) the text inside the notification
text : "Some settings for the 'email_Notifications' plugin are missing.<br />Please contact your administrator." ,
// (int | optional) the time you want it to be alive for before fading out
time : 10000 ,
} ) ;
// Hide the notification menu in mysettings
$ ( '#options-emailNotifications' ) . parent ( ) . hide ( ) ;
return false ;
}
2013-03-30 00:34:19 +00:00
// If plugin settings set panel form in mysettings menu
if ( clientVars . panelDisplayLocation . mysettings == true ) {
// Uncheck the checkbox incase of reminiscence
$ ( '#options-emailNotifications' ) . prop ( 'checked' , false ) ;
2013-03-26 19:44:34 +00:00
2013-03-30 00:34:19 +00:00
$ ( '#options-emailNotifications' ) . on ( 'click' , function ( ) {
if ( ! optionsAlreadyRecovered ) {
getDataForUserId ( 'ep_email_form_mysettings' ) ;
optionsAlreadyRecovered = true ;
} else {
$ ( '.ep_email_settings' ) . slideToggle ( ) ;
}
} ) ;
2013-03-26 19:44:34 +00:00
2013-03-30 00:34:19 +00:00
// Prepare subscription before submit form
$ ( '[name=ep_email_subscribe]' ) . on ( 'click' , function ( e ) {
$ ( '[name=ep_email_option]' ) . val ( 'subscribe' ) ;
checkAndSend ( e ) ;
} ) ;
2013-01-31 01:16:34 +00:00
2013-03-30 00:34:19 +00:00
// Prepare unsubscription before submit form
$ ( '[name=ep_email_unsubscribe]' ) . on ( 'click' , function ( e ) {
$ ( '[name=ep_email_option]' ) . val ( 'unsubscribe' ) ;
checkAndSend ( e ) ;
} ) ;
// subscribe by email can be active..
$ ( '#ep_email_form_mysettings' ) . submit ( function ( ) {
sendEmailToServer ( 'ep_email_form_mysettings' ) ;
return false ;
} ) ;
} else {
// Hide the notification menu in mysettings
$ ( '#options-emailNotifications' ) . parent ( ) . hide ( ) ;
}
// If settings set popup panel form to true, show it
if ( clientVars . panelDisplayLocation . popup == true ) {
// after 10 seconds if we dont already have an email for this author then prompt them
setTimeout ( function ( ) { initPopupForm ( ) } , 10000 ) ;
}
2013-01-31 01:16:34 +00:00
}
2013-01-31 00:09:31 +00:00
exports . handleClientMessage _emailSubscriptionSuccess = function ( hook , context ) { // was subscribing to the email a big win or fail?
2013-03-30 00:34:19 +00:00
if ( context . payload . success == false ) {
showAlreadyRegistered ( context . payload . type ) ;
$ ( '#' + context . payload . formName + ' [name=ep_email]' ) . select ( ) ;
} else {
2013-01-31 00:09:31 +00:00
showRegistrationSuccess ( ) ;
2013-03-30 00:34:19 +00:00
if ( clientVars . panelDisplayLocation . mysettings == true && $ ( '.ep_email_settings' ) . is ( ":visible" ) ) {
$ ( '.ep_email_settings' ) . slideToggle ( ) ;
$ ( '#options-emailNotifications' ) . prop ( 'checked' , false ) ;
}
if ( clientVars . panelDisplayLocation . popup == true && $ ( '#ep_email_form_popup' ) . is ( ":visible" ) ) {
$ ( '#ep_email_form_popup' ) . parent ( ) . parent ( ) . parent ( ) . hide ( ) ;
}
2013-01-31 00:09:31 +00:00
}
}
2013-01-30 02:12:57 +00:00
2013-03-26 19:44:34 +00:00
exports . handleClientMessage _emailUnsubscriptionSuccess = function ( hook , context ) { // was subscribing to the email a big win or fail?
2013-03-30 00:34:19 +00:00
if ( context . payload . success == false ) {
2013-03-26 19:44:34 +00:00
showWasNotRegistered ( ) ;
2013-03-30 00:34:19 +00:00
$ ( '#' + context . payload . formName + ' [name=ep_email]' ) . select ( ) ;
} else {
2013-03-26 19:44:34 +00:00
showUnregistrationSuccess ( ) ;
2013-03-30 00:34:19 +00:00
if ( clientVars . panelDisplayLocation . mysettings == true && $ ( '.ep_email_settings' ) . is ( ":visible" ) ) {
$ ( '.ep_email_settings' ) . slideToggle ( ) ;
$ ( '#options-emailNotifications' ) . prop ( 'checked' , false ) ;
}
if ( clientVars . panelDisplayLocation . popup == true && $ ( '#ep_email_form_popup' ) . is ( ":visible" ) ) {
$ ( '#ep_email_form_popup' ) . parent ( ) . parent ( ) . parent ( ) . hide ( ) ;
}
2013-03-26 19:44:34 +00:00
}
}
exports . handleClientMessage _emailNotificationGetUserInfo = function ( hook , context ) { // return the existing options for this userId
2013-03-30 00:34:19 +00:00
var result = context . payload ;
if ( result . success == true ) { // If data found, set the options with them
$ ( '[name=ep_email]' ) . val ( result . email ) ;
$ ( '[name=ep_email_onStart]' ) . prop ( 'checked' , result . onStart ) ;
$ ( '[name=ep_email_onEnd]' ) . prop ( 'checked' , result . onEnd ) ;
} else { // No data found, set the options to default values
$ ( '[name=ep_email_onStart]' ) . prop ( 'checked' , true ) ;
$ ( '[name=ep_email_onEnd]' ) . prop ( 'checked' , false ) ;
2013-03-26 19:44:34 +00:00
}
2013-03-30 00:34:19 +00:00
if ( result . formName == 'ep_email_form_mysettings' ) {
$ ( '.ep_email_settings' ) . slideToggle ( ) ;
}
2013-03-26 19:44:34 +00:00
}
/ * *
2013-03-30 00:34:19 +00:00
* Initialize the popup panel form for subscription
2013-03-26 19:44:34 +00:00
* /
2013-03-30 00:34:19 +00:00
function initPopupForm ( ) {
var popUpIsAlreadyVisible = $ ( '#ep_email_form_popup' ) . is ( ":visible" ) ;
if ( ! popUpIsAlreadyVisible ) { // if the popup isn't already visible
var cookieVal = pad . getPadId ( ) + "email" ;
if ( cookie . getPref ( cookieVal ) !== "true" ) { // if this user hasn't already subscribed
askClientToEnterEmail ( ) ; // ask the client to register TODO uncomment me for a pop up
}
}
}
function clientHasAlreadyRegistered ( ) { // Has the client already registered for emails on this?
// Given a specific AuthorID do we have an email address in the database?
// Given that email address is it registered to this pad?
// need to pass the server a message to check
var userId = pad . getUserId ( ) ;
var message = { } ;
message . type = 'USERINFO_AUTHOR_EMAIL_IS_REGISTERED_TO_PAD' ;
message . userInfo = { } ;
message . userInfo . userId = userId ;
pad . collabClient . sendMessage ( message ) ;
}
function askClientToEnterEmail ( ) {
var formContent = $ ( '.ep_email_settings' )
. html ( )
. replace ( 'ep_email_form_mysettings' , 'ep_email_form_popup' ) ;
$ . gritter . add ( {
// (string | mandatory) the heading of the notification
title : "Email subscription" ,
// (string | mandatory) the text inside the notification
text : "<p>(Receive an email when someone modifies this pad)</p>" + formContent ,
// (bool | optional) if you want it to fade out on its own or just sit there
sticky : true ,
// (int | optional) the time you want it to be alive for before fading out
time : 2000 ,
// the function to bind to the form
after _open : function ( e ) {
$ ( '#ep_email_form_popup' ) . submit ( function ( ) {
sendEmailToServer ( 'ep_email_form_popup' ) ;
return false ;
} ) ;
// Prepare subscription before submit form
$ ( '#ep_email_form_popup [name=ep_email_subscribe]' ) . on ( 'click' , function ( e ) {
$ ( '#ep_email_form_popup [name=ep_email_option]' ) . val ( 'subscribe' ) ;
checkAndSend ( e ) ;
} ) ;
// Prepare unsubscription before submit form
$ ( '#ep_email_form_popup [name=ep_email_unsubscribe]' ) . on ( 'click' , function ( e ) {
$ ( '#ep_email_form_popup [name=ep_email_option]' ) . val ( 'unsubscribe' ) ;
checkAndSend ( e ) ;
} ) ;
getDataForUserId ( 'ep_email_form_popup' ) ;
optionsAlreadyRecovered = true ;
}
} ) ;
2013-03-26 19:44:34 +00:00
}
/ * *
* Control options before submitting the form
* /
2013-03-30 00:34:19 +00:00
function checkAndSend ( e ) {
var formName = $ ( e . currentTarget . parentNode ) . attr ( 'id' ) ;
var email = $ ( '#' + formName + ' [name=ep_email]' ) . val ( ) ;
if ( email && $ ( '#' + formName + ' [name=ep_email_option]' ) . val ( ) == 'subscribe'
&& ! $ ( '#' + formName + ' [name=ep_email_onStart]' ) . is ( ':checked' )
&& ! $ ( '#' + formName + ' [name=ep_email_onEnd]' ) . is ( ':checked' ) ) {
2013-03-26 19:44:34 +00:00
$ . gritter . add ( {
// (string | mandatory) the heading of the notification
title : "Email subscription error" ,
// (string | mandatory) the text inside the notification
text : "You need to check at least one of the two options from 'Send a mail when someone..'"
} ) ;
} else if ( email ) {
2013-03-30 00:34:19 +00:00
$ ( '#' + formName ) . submit ( ) ;
2013-03-26 19:44:34 +00:00
}
return false ;
}
/ * *
* Ask the server to register the email
* /
2013-03-30 00:34:19 +00:00
function sendEmailToServer ( formName ) {
var email = $ ( '#' + formName + ' [name=ep_email]' ) . val ( ) ;
2013-03-26 19:44:34 +00:00
var userId = pad . getUserId ( ) ;
var message = { } ;
message . type = 'USERINFO_UPDATE' ;
message . userInfo = { } ;
message . padId = pad . getPadId ( ) ;
message . userInfo . email = email ;
2013-03-30 00:34:19 +00:00
message . userInfo . email _option = $ ( '#' + formName + ' [name=ep_email_option]' ) . val ( ) ;
message . userInfo . email _onStart = $ ( '#' + formName + ' [name=ep_email_onStart]' ) . is ( ':checked' ) ;
message . userInfo . email _onEnd = $ ( '#' + formName + ' [name=ep_email_onEnd]' ) . is ( ':checked' ) ;
message . userInfo . formName = formName ;
2013-03-26 19:44:34 +00:00
message . userInfo . userId = userId ;
if ( email ) {
pad . collabClient . sendMessage ( message ) ;
cookie . setPref ( message . padId + "email" , "true" ) ;
2013-01-30 18:09:04 +00:00
}
}
2013-03-26 19:44:34 +00:00
/ * *
* Thanks to the userId , we can get back from the Db the options set for this user
* and fill the fields with them
* /
2013-03-30 00:34:19 +00:00
function getDataForUserId ( formName ) {
2013-03-26 19:44:34 +00:00
var userId = pad . getUserId ( ) ;
var message = { } ;
message . type = 'USERINFO_GET' ;
message . padId = pad . getPadId ( ) ;
message . userInfo = { } ;
message . userInfo . userId = userId ;
2013-03-30 00:34:19 +00:00
message . userInfo . formName = formName ;
2013-03-26 19:44:34 +00:00
pad . collabClient . sendMessage ( message ) ;
}
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Manage return msgs from server
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/ * *
* Show a successful registration message
* /
function showRegistrationSuccess ( ) {
2013-01-31 00:09:31 +00:00
$ . gritter . add ( {
// (string | mandatory) the heading of the notification
title : "Email subscribed" ,
// (string | mandatory) the text inside the notification
2013-02-09 20:36:51 +00:00
text : "You will receive email when someone changes this pad. If this is the first time you have requested emails you may need to confirm your email address"
2013-01-31 00:09:31 +00:00
} ) ;
}
2013-03-26 19:44:34 +00:00
/ * *
* The client already registered for emails on this pad so notify the UI
* /
2013-03-30 00:34:19 +00:00
function showAlreadyRegistered ( type ) {
if ( type == "malformedEmail" ) {
var msg = "The email address is malformed" ;
} else if ( type == "alreadyRegistered" ) {
var msg = "You are already registered for emails for this pad" ;
} else {
var msg = "Unknown error" ;
}
2013-01-30 18:09:04 +00:00
$ . gritter . add ( {
// (string | mandatory) the heading of the notification
title : "Email subscription" ,
// (string | mandatory) the text inside the notification
2013-03-30 00:34:19 +00:00
text : msg ,
2013-01-30 18:09:04 +00:00
// (bool | optional) if you want it to fade out on its own or just sit there
sticky : false
} ) ;
}
2013-03-26 19:44:34 +00:00
/ * *
* Show a successful unregistration message
* /
function showUnregistrationSuccess ( ) {
$ . gritter . add ( {
// (string | mandatory) the heading of the notification
title : "Email unsubscribed" ,
// (string | mandatory) the text inside the notification
text : "You won't receive anymore email when someone changes this pad."
} ) ;
2013-01-30 02:12:57 +00:00
}
2013-03-26 19:44:34 +00:00
/ * *
* The client wasn ' t registered for emails
* /
function showWasNotRegistered ( ) {
2013-01-30 02:12:57 +00:00
$ . gritter . add ( {
// (string | mandatory) the heading of the notification
2013-03-26 19:44:34 +00:00
title : "Email unsubscription" ,
2013-01-30 02:12:57 +00:00
// (string | mandatory) the text inside the notification
2013-03-26 19:44:34 +00:00
text : "This email address is not registered for this pad" ,
2013-01-30 02:12:57 +00:00
// (bool | optional) if you want it to fade out on its own or just sit there
2013-03-26 19:44:34 +00:00
sticky : false
2013-01-30 02:12:57 +00:00
} ) ;
2013-01-30 00:21:21 +00:00
}