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-04-04 18:39:43 +00:00
|
|
|
// If panelDisplayLocation setting is missing, set default value
|
2013-03-30 14:56:10 +00:00
|
|
|
if (typeof clientVars.panelDisplayLocation != "object") {
|
2013-04-04 18:39:43 +00:00
|
|
|
clientVars.panelDisplayLocation = {
|
|
|
|
mysettings: true, // In the "mysettings" menu
|
|
|
|
popup: true
|
|
|
|
}
|
2013-03-30 14:56:10 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-04-04 18:56:19 +00:00
|
|
|
// Add cookie to say an email is registered for this pad
|
|
|
|
cookie.setPref(pad.getPadId() + "email", "true");
|
|
|
|
|
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
|
|
|
|
2013-04-04 18:56:19 +00:00
|
|
|
// Set cookie to say no email is registered for this pad
|
|
|
|
cookie.setPref(pad.getPadId() + "email", "false");
|
|
|
|
|
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-04-04 18:39:43 +00:00
|
|
|
exports.handleClientMessage_emailNotificationMissingParams = function (hook, context) { // Settings are missing in settings.json file
|
|
|
|
if (context.payload == true) {
|
|
|
|
$.gritter.add({
|
|
|
|
// (string | mandatory) the heading of the notification
|
2013-04-04 21:07:13 +00:00
|
|
|
title: window._('ep_email_notifications.titleGritterError'),
|
2013-04-04 18:39:43 +00:00
|
|
|
// (string | mandatory) the text inside the notification
|
2013-04-04 21:07:13 +00:00
|
|
|
text: window._('ep_email_notifications.msgParamsMissing'),
|
2013-04-04 18:39:43 +00:00
|
|
|
// (bool | optional) if you want it to fade out on its own or just sit there
|
|
|
|
sticky: true
|
|
|
|
});
|
|
|
|
|
|
|
|
// Hide the notification menu in mysettings
|
|
|
|
if (clientVars.panelDisplayLocation.mysettings == true && $('.ep_email_settings').is(":visible")) {
|
|
|
|
$('.ep_email_settings').slideToggle();
|
|
|
|
$('#options-emailNotifications').prop('checked', false);
|
|
|
|
$('#options-emailNotifications').parent().hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hide the popup if it is visible
|
|
|
|
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
|
|
|
/**
|
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
|
2013-04-02 23:57:32 +00:00
|
|
|
title: window._('ep_email_notifications.titleGritterSubscr'),
|
2013-03-30 00:34:19 +00:00
|
|
|
// (string | mandatory) the text inside the notification
|
2013-04-04 19:03:54 +00:00
|
|
|
text: "<p class='ep_email_form_popup_header'>" + window._('ep_email_notifications.headerGritterSubscr') + "</p>" + formContent,
|
2013-03-30 00:34:19 +00:00
|
|
|
// (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
|
2013-04-02 23:57:32 +00:00
|
|
|
title: window._('ep_email_notifications.titleGritterError'),
|
2013-03-26 19:44:34 +00:00
|
|
|
// (string | mandatory) the text inside the notification
|
2013-04-02 23:57:32 +00:00
|
|
|
text: window._('ep_email_notifications.msgOptionsNotChecked')
|
2013-03-26 19:44:34 +00:00
|
|
|
});
|
|
|
|
} 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);
|
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
|
2013-04-02 23:57:32 +00:00
|
|
|
title: window._('ep_email_notifications.titleGritterSubscr'),
|
2013-01-31 00:09:31 +00:00
|
|
|
// (string | mandatory) the text inside the notification
|
2013-04-02 23:57:32 +00:00
|
|
|
text: window._('ep_email_notifications.msgSubscrSuccess'),
|
2013-04-02 03:14:01 +00:00
|
|
|
// (int | optional) the time you want it to be alive for before fading out
|
|
|
|
time: 10000
|
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") {
|
2013-04-02 23:57:32 +00:00
|
|
|
var msg = window._('ep_email_notifications.msgEmailMalformed');
|
2013-03-30 00:34:19 +00:00
|
|
|
} else if (type == "alreadyRegistered") {
|
2013-04-02 23:57:32 +00:00
|
|
|
var msg = window._('ep_email_notifications.msgAlreadySubscr');
|
2013-03-30 00:34:19 +00:00
|
|
|
} else {
|
2013-04-02 23:57:32 +00:00
|
|
|
var msg = window._('ep_email_notifications.msgUnknownErr');
|
2013-03-30 00:34:19 +00:00
|
|
|
}
|
2013-01-30 18:09:04 +00:00
|
|
|
$.gritter.add({
|
|
|
|
// (string | mandatory) the heading of the notification
|
2013-04-02 23:57:32 +00:00
|
|
|
title: window._('ep_email_notifications.titleGritterSubscr'),
|
2013-01-30 18:09:04 +00:00
|
|
|
// (string | mandatory) the text inside the notification
|
2013-03-30 00:34:19 +00:00
|
|
|
text: msg,
|
2013-04-02 23:57:32 +00:00
|
|
|
// (int | optional) the time you want it to be alive for before fading out
|
|
|
|
time: 7000
|
2013-01-30 18:09:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-26 19:44:34 +00:00
|
|
|
/**
|
|
|
|
* Show a successful unregistration message
|
|
|
|
*/
|
|
|
|
function showUnregistrationSuccess(){
|
|
|
|
$.gritter.add({
|
|
|
|
// (string | mandatory) the heading of the notification
|
2013-04-02 23:57:32 +00:00
|
|
|
title: window._('ep_email_notifications.titleGritterUnsubscr'),
|
2013-03-26 19:44:34 +00:00
|
|
|
// (string | mandatory) the text inside the notification
|
2013-04-02 23:57:32 +00:00
|
|
|
text: window._('ep_email_notifications.msgUnsubscrSuccess'),
|
2013-04-02 03:14:01 +00:00
|
|
|
// (int | optional) the time you want it to be alive for before fading out
|
|
|
|
time: 10000
|
2013-03-26 19:44:34 +00:00
|
|
|
});
|
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-04-02 23:57:32 +00:00
|
|
|
title: window._('ep_email_notifications.titleGritterUnsubscr'),
|
2013-01-30 02:12:57 +00:00
|
|
|
// (string | mandatory) the text inside the notification
|
2013-04-02 23:57:32 +00:00
|
|
|
text: window._('ep_email_notifications.msgUnsubscrNotExisting'),
|
|
|
|
// (int | optional) the time you want it to be alive for before fading out
|
|
|
|
time: 7000
|
2013-01-30 02:12:57 +00:00
|
|
|
});
|
|
|
|
|
2013-01-30 00:21:21 +00:00
|
|
|
}
|