This repository has been archived on 2025-02-19. You can view files and clone it, but cannot push or open issues or pull requests.
Amen/Amen/Scripts/site-prayer.js
Denis Volkov 3675a28791
Update site-prayer.js
modify email validation regex
2023-03-20 10:10:33 -07:00

765 lines
24 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//on page load
function InitPrayers() {
$('.prayer-item-edit-summary').setMaxLength(50);
$('.prayer-item-edit-content').setMaxLength(1000);
$('.prayer-item-sendnote-content').setMaxLength(1000);
$('.prayer-item-flag-reason').setMaxLength(1000);
$('.prayer-item-edit-content').setAutoExpand(3);
$('.prayer-item-sendnote-content').setAutoExpand(3);
$('.prayer-item-flag-reason').setAutoExpand(3);
//style the sort link
var isSortNoPrayers = ParseQueryString("sort") == 'noprayers';
if (isSortNoPrayers)
$('.prayer-item-sort-noprayers').addClass('selected');
else
$('.prayer-item-sort-newest').addClass('selected');
}
$(function () {
$('.prayer-item-sort-newest').click(function () {
var isPraise = ParseQueryString("isPraise") == 'True';
var url = GetSiteUrlPath('/Prayer/Index');
if (isPraise)
url += "?isPraise=True&sort=newest";
else
url += "?sort=newest";
window.location.href = url;
});
$('.prayer-item-sort-noprayers').click(function () {
var isPraise = ParseQueryString("isPraise") == 'True';
var url = GetSiteUrlPath('/Prayer/Index');
if (isPraise)
url += "?isPraise=True&sort=noprayers";
else
url += "?sort=noprayers";
window.location.href = url;
});
$(window).scroll(function () {
var reloadOffset = $(window).height() / 2;
var container = $('.prayer-item-container');
if ($(window).scrollTop() + $(window).height() >=
container.offset().top + container.height() - reloadOffset) {
FetchMorePrayers();
}
});
$('.prayer-item-sendnote-cancel').click(function () {
var noteContainer = $('.prayer-item-sendnote');
noteContainer.addClass('transparent');
$('.full-page-shim').addClass('transparent');
var listItemTop = $('.prayer-item[prayerid=' + noteContainer.attr('prayerid') + ']').offset().top;
setTimeout(function () {
$('html, body').scrollTop(listItemTop);
}, 100);
});
$('.prayer-item-sendnote-submit').click(function () {
var noteContainer = $('.prayer-item-sendnote');
var success = SendNote(noteContainer.attr('prayerid'),
noteContainer.find('.prayer-item-sendnote-content').val(),
noteContainer.find('.prayer-item-sendnote-terms').is(':checked'));
if (success)
{
noteContainer.addClass('transparent');
$('.full-page-shim').addClass('transparent');
var listItemTop = $('.prayer-item[prayerid=' + noteContainer.attr('prayerid') + ']').offset().top;
setTimeout(function () {
$('html, body').scrollTop(listItemTop);
}, 100);
noteContainer.attr('prayerid', 0);
noteContainer.find('input, textarea').val('');
noteContainer.find('input, textarea').trigger('change');//ensure we update the remaining character counts
}
});
$('.prayer-item-flag-cancel').click(function () {
var flagContainer = $('.prayer-item-flag');
flagContainer.addClass('transparent');
$('.full-page-shim').addClass('transparent');
var listItemTop = $('.prayer-item[prayerid=' + flagContainer.attr('prayerid') + ']').offset().top;
setTimeout(function () {
$('html, body').scrollTop(listItemTop);
}, 100);
});
$('.prayer-item-flag-submit').click(function () {
var flagContainer = $('.prayer-item-flag');
flagContainer.addClass('transparent');
$('.full-page-shim').addClass('transparent');
Flag(flagContainer.attr('prayerid'), flagContainer.find('.prayer-item-flag-reason').val(), flagContainer.find('.prayer-item-flag-name').val(), flagContainer.find('.prayer-item-flag-email').val());
var listItemTop = $('.prayer-item[prayerid=' + flagContainer.attr('prayerid') + ']').offset().top;
setTimeout(function () {
$('html, body').scrollTop(listItemTop);
}, 100);
flagContainer.attr('prayerid', 0);
flagContainer.find('input, textarea').val('');
flagContainer.find('input, textarea').trigger('change');//ensure we update the remaining character counts
});
$('.prayer-item-new-add').click(function () {
var editContainer = $('.prayer-item-edit');
editContainer.removeClass('transparent');
$('.full-page-shim').removeClass('transparent');
editContainer.attr('prayerid', 0);
editContainer.find('input[type!=hidden], textarea').val('');
editContainer.find('input, textarea').trigger('change');//ensure we update the remaining character counts
});
$('.prayer-item-edit-cancel').click(function () {
var editContainer = $('.prayer-item-edit');
editContainer.addClass('transparent');
$('.full-page-shim').addClass('transparent');
var prayerItem = $('.prayer-item[prayerid=' + editContainer.attr('prayerid') + ']');
if (prayerItem.length > 0) {
var listItemTop = prayerItem.offset().top;
setTimeout(function () {
$('html, body').scrollTop(listItemTop);
}, 100);
}
});
$('.prayer-item-edit-submit').click(function () {
var editContainer = $('.prayer-item-edit');
var success = Edit(editContainer.attr('prayerid'),
editContainer.attr('ispraise') == 'True',
/*editContainer.find('.prayer-item-edit-summary').val(),*/
"summary",
editContainer.find('.prayer-item-edit-content').val(),
editContainer.find('.prayer-item-edit-name').val(),
editContainer.find('.prayer-item-edit-email').val(),
editContainer.find('.prayer-item-edit-phone').val(),
editContainer.find('.prayer-item-edit-sharethis').val(),
editContainer.find('.prayer-item-edit-contactpreference').val(),
editContainer.find('.prayer-item-edit-terms').is(':checked'),
editContainer.find('.prayer-item-edit-note').is(':checked'),
editContainer.find('.prayer-item-edit-notification').is(':checked'),
editContainer.find('.prayer-item-edit-notification-sms').is(':checked'));
if (success) {
editContainer.addClass('transparent');
$('.full-page-shim').addClass('transparent');
var prayerItem = $('.prayer-item[prayerid=' + editContainer.attr('prayerid') + ']');
if (prayerItem.length > 0) {
var listItemTop = prayerItem.offset().top;
setTimeout(function () {
$('html, body').scrollTop(listItemTop);
}, 100);
}
}
});
InitPrayerItemEventHandlers();
function InitPrayerItemEventHandlers() {
$('.prayer-item-prayed a').off().click(function (e) {
e.preventDefault();
$(this).off();//don't fire it again if clicked multiple times
var prayerItem = $(this).closest('.prayer-item');
var count = parseInt(prayerItem.find('.prayer-item-prayed-count').text());
prayerItem.find('.prayer-item-prayed-count').text(count + 1);
prayerItem.find('.prayer-item-prayed-action').text('Prayed');
Prayed(prayerItem.attr('prayerid'));
$(this).blur();
});
$('.prayer-item-actions-flag').off().click(function (e) {
e.preventDefault();
var flagContainer = $('.prayer-item-flag');
var prayerItem = $(this).closest('.prayer-item');
flagContainer.attr('prayerid', prayerItem.attr('prayerid'));
flagContainer.removeClass('transparent');
$('.full-page-shim').removeClass('transparent');
$('html, body').scrollTop(0);
});
$('.prayer-item-actions-sendnote').off().click(function (e) {
e.preventDefault();
var noteContainer = $('.prayer-item-sendnote');
var prayerItem = $(this).closest('.prayer-item');
noteContainer.attr('prayerid', prayerItem.attr('prayerid'));
noteContainer.removeClass('transparent');
$('.full-page-shim').removeClass('transparent');
$('html, body').scrollTop(0);
});
$('.prayer-item-actions-edit').off().click(function (e) {
e.preventDefault();
$('.prayer-item-container').toggleClass('fixedContainer');
var editContainer = $('.prayer-item-edit');
var prayerItem = $(this).closest('.prayer-item');
editContainer.removeClass('transparent');
$('.full-page-shim').removeClass('transparent');
var temp = $('<textarea/>');
temp.html(prayerItem.find('.prayer-item-content').html().replace(/<br>/g, '\n'));
editContainer.attr('prayerid', prayerItem.attr('prayerid'));
editContainer.find('.prayer-item-edit-summary').val(prayerItem.find('.prayer-item-summary').text().trim());
editContainer.find('.prayer-item-edit-content').val(temp.text().trim());
editContainer.find('.prayer-item-edit-summary').trigger('change');
editContainer.find('.prayer-item-edit-content').trigger('change');
if (editContainer.find('.prayer-item-edit-email').attr('type') != 'hidden') {
editContainer.find('.prayer-item-edit-email').val('');
}
editContainer.find('.prayer-item-edit-phone').val('');
editContainer.find('.prayer-item-edit-name').val('');
//TODO: set Anonymous flag to unchecked
$('html, body').scrollTop(0);
});
$('.prayer-item-actions-delete').off().click(function (e) {
e.preventDefault();
var prayerItem = $(this).closest('.prayer-item');
var prayerId = prayerItem.attr('prayerid');
if (window.confirm('Are you sure? This cannot be undone.')) {
Delete(prayerId);
prayerItem.addClass('hidden');
}
});
$('.prayer-item-actions-share').off().click(function (event) {
var shareContainer = $(this).closest('.prayer-item').find('.prayer-item-share-container');
if (shareContainer.hasClass('hidden'))
shareContainer.removeClass('hidden');
else
shareContainer.addClass('hidden');
var top = $(this).position().top + 8;
var left = 170;
shareContainer.css({ position: "absolute", top: top, left: left });
});
var collapseShareTimeout = null;
$('.prayer-item-actions-share').mouseover(function () {
var shareContainer = $(this).closest('.prayer-item').find('.prayer-item-share-container');
if (collapseShareTimeout)
clearTimeout(collapseShareTimeout);
});
$('.prayer-item-actions-share').mouseout(function () {
var shareContainer = $(this).closest('.prayer-item').find('.prayer-item-share-container');
collapseShareTimeout = setTimeout(function () {
shareContainer.addClass('hidden');
}, 4000);
});
$('.prayer-item-actions-subscribe').off().click(function (e) {
e.preventDefault();
var prayerItem = $(this).closest('.prayer-item');
var prayerId = prayerItem.attr('prayerid');
var link = $(this).find('span');
var doSubscribe = link.text() == 'Subscribe';
if (doSubscribe) {
link.text('Unsubscribe');
Subscribe(prayerId);
}
else {
link.text('Subscribe');
Unsubscribe(prayerId);
}
});
$('.prayer-item-actions-bookmark').off().click(function (e) {
e.preventDefault();
var prayerItem = $(this).closest('.prayer-item');
var prayerId = prayerItem.attr('prayerid');
var link = $(this).find('span');
var doBookmark = link.text() == 'Bookmark';
if (doBookmark) {
link.text('Remove Bookmark');
Bookmark(prayerId);
}
else {
link.text('Bookmark');
RemoveBookmark(prayerId);
}
});
var collapseMouseOutTimeout = null;
$('.prayer-item .actions-collapse').off().mouseover(function () {
if (collapseMouseOutTimeout)
clearTimeout(collapseMouseOutTimeout);
});
$('.prayer-item .actions-collapse').mouseout(function () {
var item = $(this);
collapseMouseOutTimeout = setTimeout(function () {
if (item.hasClass('in')) {
item.closest('.prayer-item').find('.actions-toggle-btn').trigger('click');
}
}, 3000);
});
}
function Prayed(prayerid) {
var url = GetSiteUrlPath('/Prayer/Prayed/');
var json = {
prayerId: prayerid
};
$.ajax({
type: "POST",
url: url,
cache: false,
contentType: "application/json",
data: JSON.stringify(json),
dataType: 'json',
success: function (result) {
//alert(JSON.stringify(result));
if (result) {
}
},
error: function (result, status) {
//alert(JSON.stringify(result));
}
});
}
function Subscribe(prayerid) {
var url = GetSiteUrlPath('/Prayer/Subscribe/');
var json = {
prayerId: prayerid
};
$.ajax({
type: "POST",
url: url,
cache: false,
contentType: "application/json",
data: JSON.stringify(json),
dataType: 'json',
success: function (result) {
//alert(JSON.stringify(result));
if (result) {
}
},
error: function (result, status) {
//alert(JSON.stringify(result));
}
});
}
function Unsubscribe(prayerid) {
var url = GetSiteUrlPath('/Prayer/Unsubscribe/');
var json = {
prayerId: prayerid
};
$.ajax({
type: "POST",
url: url,
cache: false,
contentType: "application/json",
data: JSON.stringify(json),
dataType: 'json',
success: function (result) {
//alert(JSON.stringify(result));
if (result) {
}
},
error: function (result, status) {
//alert(JSON.stringify(result));
}
});
}
function Bookmark(prayerid) {
var url = GetSiteUrlPath('/Prayer/Bookmark/');
var json = {
prayerId: prayerid
};
$.ajax({
type: "POST",
url: url,
cache: false,
contentType: "application/json",
data: JSON.stringify(json),
dataType: 'json',
success: function (result) {
//alert(JSON.stringify(result));
if (result) {
}
},
error: function (result, status) {
//alert(JSON.stringify(result));
}
});
}
function RemoveBookmark(prayerid) {
var url = GetSiteUrlPath('/Prayer/RemoveBookmark/');
var json = {
prayerId: prayerid
};
$.ajax({
type: "POST",
url: url,
cache: false,
contentType: "application/json",
data: JSON.stringify(json),
dataType: 'json',
success: function (result) {
//alert(JSON.stringify(result));
if (result) {
}
},
error: function (result, status) {
//alert(JSON.stringify(result));
}
});
}
function SendNote(prayerid, content, terms) {
if (!terms) {
alert('You must first agree to the terms of use.');
return false;
}
if (!content || content.length == 0) {
alert('Content cannot be empty.');
return false;
}
if (UpperCasePercent(content) > 0.5) {
alert('Please do not use ALL CAPS')
return false;
}
var containsLinkRegex = new RegExp(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi);
if (content.match(containsLinkRegex)) {
alert('Please do not include links')
return false;
}
var url = GetSiteUrlPath('/Prayer/SendNote/');
var json = {
prayerId: prayerid,
content: content
};
$.ajax({
type: "POST",
url: url,
cache: false,
contentType: "application/json",
data: JSON.stringify(json),
dataType: 'json',
success: function (result) {
if (!result.success) {
alert(result.message);
}
//alert(JSON.stringify(result));
},
error: function (result, status) {
//alert(JSON.stringify(result));
}
});
return true;
}
function Flag(prayerid, reason, name, email) {
var url = GetSiteUrlPath('/Prayer/Flag/');
var json = {
prayerId: prayerid,
reason: reason,
name: name,
email: email
};
$.ajax({
type: "POST",
url: url,
cache: false,
contentType: "application/json",
data: JSON.stringify(json),
dataType: 'json',
success: function (result) {
//alert(JSON.stringify(result));
if (result && result.hide) {
$('.prayer-item[prayerid=' + result.id + ']').addClass('hidden');
}
},
error: function (result, status) {
//alert(JSON.stringify(result));
}
});
}
function Delete(prayerid) {
var url = GetSiteUrlPath('/Prayer/Delete/');
var json = {
id: prayerid,
};
$.ajax({
type: "POST",
url: url,
cache: false,
contentType: "application/json",
data: JSON.stringify(json),
dataType: 'json',
success: function (result) {
//alert(JSON.stringify(result));
},
error: function (result, status) {
//alert(JSON.stringify(result));
}
});
}
function Edit(id, ispraise, summary, content, name, email, phone, sharethis, contactpreference, terms, note, notificationemail, notificationsms) {
if (!terms) {
alert('You must first agree to the terms of use.');
return false;
}
if (name.length == 0) {
alert('First Name is required.');
return false;
}
if (content.length == 0) {
alert('Prayer Request is required.');
return false;
}
if (email.length == 0) {
alert('Email is required.');
return false;
}
if (contactpreference == 'phone' || contactpreference == 'any') {
if (phone.length == 0) {
alert('Phone number is required for text notifications.');
return false;
}
}
if (email && email.length > 0) {
if (!(/^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(email))) {
// old regex /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
// updated to default html5 regex
alert("You have entered an invalid email address. Please, check and try again.");
return (false);
}
}
if (phone && phone.length > 0) {
if (!(/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/.test(phone))) {
alert("You have entered an invalid phone number. Please, check and try again.");
return (false);
}
}
if (UpperCasePercent(content) > 0.5) {
alert('Please do not use ALL CAPS.')
return false;
}
var containsLinkRegex = new RegExp(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi);
if (content.match(containsLinkRegex)) {
alert('Please do not include links.')
return false;
}
if (ispraise) { summary = 'Praise report' } else { summary = 'Prayer request' };
var isnew = id == 0;
var url = GetSiteUrlPath('/Prayer/Edit/');
var json = {
Id: id,
Summary: summary,
Content: content,
AnonymousName: name,
AnonymousEmail: email,
AnonymousPhone: phone,
ShareThis: sharethis,
ContactPreference: contactpreference,
IsPraise: ispraise,
IsNew: isnew,
AgreeTermsOfUse: terms,
EnableEmailNote: note,
EnableEmailNotification: notificationemail,
EnableTextNotification: notificationsms
};
$.ajax({
type: "POST",
url: url,
cache: false,
contentType: "application/json",
data: JSON.stringify(json),
dataType: 'json',
success: function (result) {
//alert(JSON.stringify(result));
if (!result.success) {
setTimeout(function () {
var editContainer = $('.prayer-item-edit');
editContainer.removeClass('transparent');
$('.full-page-shim').removeClass('transparent');
}, 100);
alert('Something went wrong:\r' + result.message);
//alert('Something went wrong. Try again, or review content and ensure you don\'t have something missing, or accidentally offensive.')
return;
}
if ($('.prayer-item-container').find('.prayer-item').length == 1) {
var urlParms = json.IsPraise ? "?isPraise=True" : "";
window.location.href = GetSiteUrlPath("/Prayer" + urlParms);
}
else {
$('.prayer-item-container').find('.prayer-item').remove();
FetchMorePrayers();
}
},
error: function (result, status) {
//alert(JSON.stringify(result));
}
});
return true;
}
var fetchMoreInProgress = false;
function FetchMorePrayers() {
var isDetailPage = $('#IsDetail').val() == 'True';
if (isDetailPage)
return;
if (fetchMoreInProgress)
return;
var container = $('.prayer-item-container');
fetchMoreInProgress = true;
var url = GetSiteUrlPath('/Prayer/PrayerItems/');
var isPraise = ParseQueryString("isPraise");
var sort = ParseQueryString("sort");
var json = {
lastPrayerId: container.find('.prayer-item:last').attr('prayerid'),
isPraise: isPraise == 'True',
sort: sort
};
//alert(JSON.stringify(json));
//return;
$.ajax({
type: "POST",
url: url,
cache: false,
contentType: "application/json",
data: JSON.stringify(json),
dataType: 'html',
success: function (result) {
//alert(JSON.stringify(result));
container.append(result);
fetchMoreInProgress = false;
InitPrayerItemEventHandlers();
},
error: function (result, status) {
alert(JSON.stringify(result));
fetchMoreInProgress = false;
}
});
}
});