ep_email_notifications/static/js/ep_email.js

103 lines
3.6 KiB
JavaScript
Raw Normal View History

2013-01-31 15:49:45 +00:00
exports.postAceInit = function(hook, context){
2013-01-31 01:16:34 +00:00
// after 10 seconds if we dont already have an email for this author then prompt them
setTimeout(function(){init()},10000);
// subscribe by email can be active..
$('.ep_email_form').submit(function(){
sendEmailToServer();
return false;
});
}
exports.handleClientMessage_emailSubscriptionSuccess = function(hook, context){ // was subscribing to the email a big win or fail?
if(context.payload == false){
showAlreadyRegistered();
}else{
showRegistrationSuccess();
}
}
2013-01-30 02:12:57 +00:00
function init(){
var popUpIsAlreadyVisible = $('.ep_email_form').is(":visible");
2013-01-31 01:16:34 +00:00
if(!popUpIsAlreadyVisible){ // if the popup isn't already visible
if(clientHasAlreadyRegistered()){ // if the client has already registered for emails on this pad.
// showAlreadyRegistered(); // client has already registered, let em know..
}else{
// askClientToEnterEmail(); // ask the client to register TODO uncomment me for a pop up
2013-01-31 01:16:34 +00:00
}
}
}
function showRegistrationSuccess(){ // show a successful registration message
$.gritter.add({
// (string | mandatory) the heading of the notification
title: "Email subscribed",
// (string | mandatory) the text inside the notification
text: "You will recieve email when someone changes this pad. If this is the first time you have requested emails you may need to confirm your email address"
});
}
function showAlreadyRegistered(){ // the client already registered for emails on this pad so notify the UI
$.gritter.add({
// (string | mandatory) the heading of the notification
title: "Email subscription",
// (string | mandatory) the text inside the notification
text: "You are already registered for emails for this pad",
// (bool | optional) if you want it to fade out on its own or just sit there
sticky: false
});
}
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);
2013-01-30 02:12:57 +00:00
}
function askClientToEnterEmail(){
$.gritter.add({
// (string | mandatory) the heading of the notification
2013-01-31 01:16:34 +00:00
title: "Enter your email to recieve an email when someone modifies this pad",
2013-01-30 02:12:57 +00:00
// (string | mandatory) the text inside the notification
2013-01-31 01:24:47 +00:00
text: "<form class='ep_email_form'><label for='ep_email'><input id='ep_email_notification' placeholder='your@email.com' style='padding:5px;width:200px;' type=email><input type=submit style='padding:5px;'></form>",
2013-01-30 02:12:57 +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){
2013-01-31 01:16:34 +00:00
$('.ep_email_form').submit(function(){
2013-01-30 02:12:57 +00:00
$(e).hide();
sendEmailToServer();
return false;
});
}
});
}
2013-01-30 00:21:21 +00:00
function sendEmailToServer(){
2013-01-30 02:12:57 +00:00
var email = $('#ep_email').val();
2013-01-31 01:24:47 +00:00
if(!email){ // if we're not using the top value use the notification value
email = $('#ep_email_notification').val();
}
var userId = pad.getUserId();
2013-01-30 00:21:21 +00:00
var message = {};
message.type = 'USERINFO_UPDATE';
message.userInfo = {};
message.padId = pad.getPadId();
message.userInfo.email = email;
message.userInfo.userId = userId;
2013-01-30 00:21:21 +00:00
if(email){
pad.collabClient.sendMessage(message);
}
}
2013-01-31 01:24:47 +00:00