Attendize/public/assets/javascript/app.js

568 lines
15 KiB
JavaScript
Raw Normal View History

2016-02-29 15:59:36 +00:00
$(function () {
/*
* --------------------------
* Set up all our required plugins
* --------------------------
*/
/* Datepicker */
2016-02-29 15:59:36 +00:00
$(document).ajaxComplete(function () {
$('#DatePicker').remove();
var $div = $("<div>", {id: "DatePicker"});
$("body").append($div);
$div.DateTimePicker({
2018-10-04 03:42:46 +00:00
dateTimeFormat: Attendize.DateTimeFormat,
dateSeparator: Attendize.DateSeparator
2016-02-29 15:59:36 +00:00
});
});
/* Responsive sidebar */
$(document.body).on('click', '.toggleSidebar', function (e) {
$('html').toggleClass('sidebar-open-ltr');
e.preventDefault();
});
/* Scroll to top */
$(window).scroll(function () {
if ($(this).scrollTop() > 100) {
$('.totop').fadeIn();
} else {
$('.totop').fadeOut();
}
});
$(".totop").click(function () {
$("html, body").animate({
scrollTop: 0
}, 200);
});
/*
* --------------------
* Ajaxify those forms
* --------------------
2016-03-27 20:12:44 +00:00
*
2016-02-29 15:59:36 +00:00
* All forms with the 'ajax' class will automatically handle showing errors etc.
2016-03-27 20:12:44 +00:00
*
2016-02-29 15:59:36 +00:00
*/
$('form.ajax').ajaxForm({
delegation: true,
beforeSubmit: function (formData, jqForm, options) {
$(jqForm[0])
.find('.error.help-block')
.remove();
$(jqForm[0]).find('.has-error')
.removeClass('has-error');
var $submitButton = $(jqForm[0]).find('input[type=submit]');
toggleSubmitDisabled($submitButton);
},
uploadProgress: function (event, position, total, percentComplete) {
$('.uploadProgress').show().html('Uploading Images - ' + percentComplete + '% Complete... ');
},
error: function (data, statusText, xhr, $form) {
2016-03-27 20:12:44 +00:00
// Form validation error.
if (422 == data.status) {
processFormErrors($form, $.parseJSON(data.responseText));
return;
}
2016-07-26 19:33:22 +00:00
showMessage('Whoops!, it looks like the server returned an error.');
2016-02-29 15:59:36 +00:00
var $submitButton = $form.find('input[type=submit]');
toggleSubmitDisabled($submitButton);
$('.uploadProgress').hide();
},
success: function (data, statusText, xhr, $form) {
switch (data.status) {
case 'success':
if ($form.hasClass('reset')) {
$form.resetForm();
}
if ($form.hasClass('closeModalAfter')) {
$('.modal, .modal-backdrop').fadeOut().remove();
}
var $submitButton = $form.find('input[type=submit]');
toggleSubmitDisabled($submitButton);
if (typeof data.message !== 'undefined') {
showMessage(data.message);
}
if (typeof data.runThis !== 'undefined') {
eval(data.runThis);
}
if (typeof data.redirectUrl !== 'undefined') {
window.location.href = data.redirectUrl;
2016-02-29 15:59:36 +00:00
}
break;
case 'error':
2016-03-27 20:12:44 +00:00
processFormErrors($form, data.messages);
2016-02-29 15:59:36 +00:00
break;
default:
break;
}
$('.uploadProgress').hide();
},
dataType: 'json'
});
/*
* --------------------
* Create a simple way to show remote dynamic modals from the frontend
* --------------------
2016-03-27 20:12:44 +00:00
*
* E.g :
2016-02-29 15:59:36 +00:00
* <a href='/route/to/modal' class='loadModal'>
* Click For Modal
* </a>
2016-03-27 20:12:44 +00:00
*
2016-02-29 15:59:36 +00:00
*/
$(document.body).on('click', '.loadModal, [data-invoke~=modal]', function (e) {
var loadUrl = $(this).data('href'),
cacheResult = $(this).data('cache') === 'on',
$button = $(this);
2016-02-29 15:59:36 +00:00
$('.modal').remove();
(localization) Several big changes: 1) Added localization components to the package. They allow usage of localized routes, like http://attendize.site/en/login 2) Added English and Polish localization files. They are ugly, repetitive, but mostly true to the original and relevant. It required rewriting several phrases, and certainly required editing most of the views and controllers. 3) Edited routes to accomodate point 1 4) Rewritten several rules regarding dates. In most cases using English notation (with English names for months) is bad in all other languages. I used environment wide date format that is used. 5) Updated installer. Haven't tested it yet, but should work. Rewrites .env.example file instead of creating it from scratch (by concatenating strings). There are some minor changes that were simple fixes or other funky requirements from my employer that kinda make sense: 1) QR code reader wasn't working in firefox, fixed it. Works in chrome/firefox on mobile on https sites. 2) Added subscript text in some instances: below ticket registration, below ticket. It is kinda dumb, but in most cases is necessary to receive less complaints from clients. 3) Fixed geocoding api by adding api key in env file. At some point in 2016-2017 it was required by google to use API key from developer console and this requirement wasn't challenged in the code. 4) Ticket has been displaying either flyer or site logo on the side. Now displays both (which may affect 1d barcode - it might need some fixin). Regarding the same issue - description of an event contained the flyer image on the side, it was removed, cause it didn't fit in here. 5) Ticket style was updated, because of the above and because it didn't fit longer character strings. Now it's slightly uglier, but works in all cases. and other. There are also some inconveniences, like: 1) Unfinished translations. It was impossible for me to create translations based on strings located inside of a database, which I ignored (I think it's only at one place - surveys). 2) Ugly translation files. At some point I thought it is going to be easier to locate when I try translating vased by file name. Later I divided it by topics, and then I segmented it even more. It might require some serious clean-up. 3) Redundancy. In some cases there are several definitions for the same phrase in my localization files. I used it mostly to protect myself from different contexts for the phrase usage in different languages. 4) File division. There are several files that are placed in dedicated language directory (in /view/, like /view/pl/ or /view/en/). These files don't use language phrases, but they are translated as a whole. Mostly because using language phrases would make those view files unreadable. 5) Localzation helper marks some phrases as obsolete (in file "basic"), because they are used in app/Helpers folder (where this plugin doesn't reach)
2018-05-03 21:41:22 +00:00
$('.modal-backdrop').remove();
2016-02-29 15:59:36 +00:00
$('html').addClass('working');
$.ajax({
url: loadUrl,
data: {},
2016-02-29 15:59:36 +00:00
localCache: cacheResult,
dataType: 'html',
success: function (data) {
hideMessage();
$('body').append(data);
var $modal = $('.modal');
2016-02-29 15:59:36 +00:00
$modal.modal({
'backdrop': 'static'
});
$modal.modal('show');
$modal.on('hidden.bs.modal', function (e) {
// window
location.hash = '';
});
$('html').removeClass('working');
2016-02-29 15:59:36 +00:00
}
}).done().fail(function (data) {
$('html').removeClass('working');
(localization) Several big changes: 1) Added localization components to the package. They allow usage of localized routes, like http://attendize.site/en/login 2) Added English and Polish localization files. They are ugly, repetitive, but mostly true to the original and relevant. It required rewriting several phrases, and certainly required editing most of the views and controllers. 3) Edited routes to accomodate point 1 4) Rewritten several rules regarding dates. In most cases using English notation (with English names for months) is bad in all other languages. I used environment wide date format that is used. 5) Updated installer. Haven't tested it yet, but should work. Rewrites .env.example file instead of creating it from scratch (by concatenating strings). There are some minor changes that were simple fixes or other funky requirements from my employer that kinda make sense: 1) QR code reader wasn't working in firefox, fixed it. Works in chrome/firefox on mobile on https sites. 2) Added subscript text in some instances: below ticket registration, below ticket. It is kinda dumb, but in most cases is necessary to receive less complaints from clients. 3) Fixed geocoding api by adding api key in env file. At some point in 2016-2017 it was required by google to use API key from developer console and this requirement wasn't challenged in the code. 4) Ticket has been displaying either flyer or site logo on the side. Now displays both (which may affect 1d barcode - it might need some fixin). Regarding the same issue - description of an event contained the flyer image on the side, it was removed, cause it didn't fit in here. 5) Ticket style was updated, because of the above and because it didn't fit longer character strings. Now it's slightly uglier, but works in all cases. and other. There are also some inconveniences, like: 1) Unfinished translations. It was impossible for me to create translations based on strings located inside of a database, which I ignored (I think it's only at one place - surveys). 2) Ugly translation files. At some point I thought it is going to be easier to locate when I try translating vased by file name. Later I divided it by topics, and then I segmented it even more. It might require some serious clean-up. 3) Redundancy. In some cases there are several definitions for the same phrase in my localization files. I used it mostly to protect myself from different contexts for the phrase usage in different languages. 4) File division. There are several files that are placed in dedicated language directory (in /view/, like /view/pl/ or /view/en/). These files don't use language phrases, but they are translated as a whole. Mostly because using language phrases would make those view files unreadable. 5) Localzation helper marks some phrases as obsolete (in file "basic"), because they are used in app/Helpers folder (where this plugin doesn't reach)
2018-05-03 21:41:22 +00:00
showMessage(lang("whoops_and_error", {"code": data.status, "error": data.statusText}));
2016-02-29 15:59:36 +00:00
});
e.preventDefault();
});
/*
* ------------------------------------------------------------
* A slightly hackish way to close modals on back button press.
* ------------------------------------------------------------
*/
$(window).on('hashchange', function (e) {
$('.modal').modal('hide');
});
/*
* -------------------------------------------------------------
2016-03-27 20:12:44 +00:00
* Simple way for any type of object to be deleted.
2016-02-29 15:59:36 +00:00
* -------------------------------------------------------------
2016-03-27 20:12:44 +00:00
*
2016-02-29 15:59:36 +00:00
* E.g markup:
* <a data-route='/route/to/delete' data-id='123' data-type='objectType'>
* Delete This Object
* </a>
2016-03-27 20:12:44 +00:00
*
2016-02-29 15:59:36 +00:00
*/
$('.deleteThis').on('click', function (e) {
/*
* Confirm if the user wants to delete this object
*/
if ($(this).data('confirm-delete') !== 'yes') {
$(this).data('original-text', $(this).html()).html('Click To Confirm?').data('confirm-delete', 'yes');
var that = $(this);
setTimeout(function () {
that.data('confirm-delete', 'no').html(that.data('original-text'));
}, 2000);
return;
}
var deleteId = $(this).data('id'),
deleteType = $(this).data('type'),
route = $(this).data('route');
$.post(route, deleteType + '_id=' + deleteId)
.done(function (data) {
if (typeof data.message !== 'undefined') {
showMessage(data.message);
}
switch (data.status) {
case 'success':
$('#' + deleteType + '_' + deleteId).fadeOut();
break;
case 'error':
/* Error */
break;
default:
break;
}
}).fail(function (data) {
showMessage(Attendize.GenericErrorMessages);
});
e.preventDefault();
});
$(document.body).on('click', '.pauseTicketSales', function (e) {
var ticketId = $(this).data('id'),
route = $(this).data('route');
$.post(route, 'ticket_id=' + ticketId)
.done(function (data) {
if (typeof data.message !== 'undefined') {
showMessage(data.message);
}
switch (data.status) {
case 'success':
setTimeout(function () {
document.location.reload();
}, 300);
break;
case 'error':
/* Error */
break;
default:
break;
}
}).fail(function (data) {
showMessage(Attendize.GenericErrorMessages);
});
e.preventDefault();
});
/**
* Toggle checkboxes
*/
$(document.body).on('click', '.check-all', function (e) {
var toggleClass = $(this).data('check-class');
$('.' + toggleClass).each(function () {
this.checked = $(this).checked;
});
});
/*
* ------------------------------------------------------------
* Toggle hidden content when a.show-more-content is clicked
* ------------------------------------------------------------
*/
$(document.body).on('click', '.show-more-options', function (e) {
var toggleClass = !$(this).data('toggle-class')
? '.more-options'
: $(this).data('toggle-class');
if ($(this).hasClass('toggled')) {
$(this).html($(this)
.data('original-text'));
} else {
if (!$(this).data('original-text')) {
$(this).data('original-text', $(this).html());
}
$(this).html(!$(this).data('show-less-text') ? 'Show Less' : $(this).data('show-less-text'));
}
$(this).toggleClass('toggled');
/*
* ?
*/
if ($(this).data('clear-field')) {
$($(this).data('clear-field')).val('');
}
$(toggleClass).slideToggle();
e.preventDefault();
});
/*
* Sort by trigger
*/
$('select[name=sort_by_select]').on('change', function () {
$('input[name=sort_by]').val($(this).val()).closest('form').submit();
});
/**
* Custom file inputs
*/
$(document).on('change', '.btn-file :file', function () {
var input = $(this),
numFiles = input.get(0).files ? input.get(0).files.length : 1,
label = input.val().replace(/\\/g, '/').replace(/.*\//, '');
input.trigger('fileselect', [
numFiles,
label
]);
});
$(document.body).on('fileselect', '.btn-file :file', function (event, numFiles, label) {
var input = $(this).parents('.input-group').find(':text'),
log = numFiles > 1 ? numFiles + ' files selected' : label;
if (input.length) {
input.val(log);
} else {
if (log) {
console.log(log);
}
}
});
/**
* Scale the preview iFrames when changing the design of organiser/event pages.
*/
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
var target = $(e.target).attr("href");
if ($(target).hasClass('scale_iframe')) {
var $iframe = $('iframe', target);
var iframeWidth = $('.iframe_wrap').innerWidth();
var iframeHeight = $('.iframe_wrap').height();
$iframe.css({
width: 1200,
height: 1400
});
var iframeScale = (iframeWidth / 1200);
$iframe.css({
'-webkit-transform': 'scale(' + iframeScale + ')',
'-ms-transform': 'scale(' + iframeScale + ')',
'transform': 'scale(' + iframeScale + ')',
'-webkit-transform-origin': '0 0',
'-ms-transform-origin': '0 0',
'transform-origin': '0 0',
});
}
});
$(document.body).on('click', '.markPaymentReceived', function (e) {
var orderId = $(this).data('id'),
route = $(this).data('route');
$.post(route, 'order_id=' + orderId)
.done(function (data) {
if (typeof data.message !== 'undefined') {
showMessage(data.message);
}
switch (data.status) {
case 'success':
setTimeout(function () {
document.location.reload();
}, 300);
break;
case 'error':
/* Error */
break;
default:
break;
}
}).fail(function (data) {
showMessage(Attendize.GenericErrorMessages);
});
e.preventDefault();
});
2016-02-29 15:59:36 +00:00
});
2016-03-27 20:12:44 +00:00
function changeQuestionType(select)
{
var select = $(select);
var selected = select.find(':selected');
if (selected.data('has-options') == '1') {
$('#question-options').removeClass('hide');
} else {
$('#question-options').addClass('hide');
}
}
2016-03-27 20:12:44 +00:00
function addQuestionOption()
{
var tbody = $('#question-options tbody');
var questionOption = $('#question-option-template').html();
tbody.append(questionOption);
}
function removeQuestionOption(removeBtn)
{
var removeBtn = $(removeBtn);
var tbody = removeBtn.parents('tbody');
if (tbody.find('tr').length > 1) {
removeBtn.parents('tr').remove();
} else {
(localization) Several big changes: 1) Added localization components to the package. They allow usage of localized routes, like http://attendize.site/en/login 2) Added English and Polish localization files. They are ugly, repetitive, but mostly true to the original and relevant. It required rewriting several phrases, and certainly required editing most of the views and controllers. 3) Edited routes to accomodate point 1 4) Rewritten several rules regarding dates. In most cases using English notation (with English names for months) is bad in all other languages. I used environment wide date format that is used. 5) Updated installer. Haven't tested it yet, but should work. Rewrites .env.example file instead of creating it from scratch (by concatenating strings). There are some minor changes that were simple fixes or other funky requirements from my employer that kinda make sense: 1) QR code reader wasn't working in firefox, fixed it. Works in chrome/firefox on mobile on https sites. 2) Added subscript text in some instances: below ticket registration, below ticket. It is kinda dumb, but in most cases is necessary to receive less complaints from clients. 3) Fixed geocoding api by adding api key in env file. At some point in 2016-2017 it was required by google to use API key from developer console and this requirement wasn't challenged in the code. 4) Ticket has been displaying either flyer or site logo on the side. Now displays both (which may affect 1d barcode - it might need some fixin). Regarding the same issue - description of an event contained the flyer image on the side, it was removed, cause it didn't fit in here. 5) Ticket style was updated, because of the above and because it didn't fit longer character strings. Now it's slightly uglier, but works in all cases. and other. There are also some inconveniences, like: 1) Unfinished translations. It was impossible for me to create translations based on strings located inside of a database, which I ignored (I think it's only at one place - surveys). 2) Ugly translation files. At some point I thought it is going to be easier to locate when I try translating vased by file name. Later I divided it by topics, and then I segmented it even more. It might require some serious clean-up. 3) Redundancy. In some cases there are several definitions for the same phrase in my localization files. I used it mostly to protect myself from different contexts for the phrase usage in different languages. 4) File division. There are several files that are placed in dedicated language directory (in /view/, like /view/pl/ or /view/en/). These files don't use language phrases, but they are translated as a whole. Mostly because using language phrases would make those view files unreadable. 5) Localzation helper marks some phrases as obsolete (in file "basic"), because they are used in app/Helpers folder (where this plugin doesn't reach)
2018-05-03 21:41:22 +00:00
alert(lang("at_least_one_option"));
2016-03-27 20:12:44 +00:00
}
}
function processFormErrors($form, errors)
{
$.each(errors, function (index, error)
{
var $input = $(':input[name=' + index + ']', $form);
if ($input.prop('type') === 'file') {
$('#input-' + $input.prop('name')).append('<div class="help-block error">' + error + '</div>')
.parent()
.addClass('has-error');
} else {
$input.after('<div class="help-block error">' + error + '</div>')
.parent()
.addClass('has-error');
}
});
var $submitButton = $form.find('input[type=submit]');
toggleSubmitDisabled($submitButton);
}
2016-02-29 15:59:36 +00:00
/**
*
* @param elm $submitButton
* @returns void
*/
function toggleSubmitDisabled($submitButton) {
if ($submitButton.hasClass('disabled')) {
$submitButton.attr('disabled', false)
.removeClass('disabled')
.val($submitButton.data('original-text'));
return;
}
$submitButton.data('original-text', $submitButton.val())
.attr('disabled', true)
.addClass('disabled')
.val('Working...');
}
2016-06-07 13:00:34 +00:00
/**
2018-10-04 03:42:46 +00:00
*
2016-06-07 13:00:34 +00:00
* @returns {{}}
*/
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
/**
* Replaces a parameter in a URL with a new parameter
*
* @param url
* @param paramName
* @param paramValue
* @returns {*}
*/
function replaceUrlParam(url, paramName, paramValue) {
var pattern = new RegExp('\\b(' + paramName + '=).*?(&|$)')
if (url.search(pattern) >= 0) {
return url.replace(pattern, '$1' + paramValue + '$2');
}
return url + (url.indexOf('?') > 0 ? '&' : '?') + paramName + '=' + paramValue
}
2016-02-29 15:59:36 +00:00
/**
* Shows users a message.
* Currently uses humane.js
*
* @param string message
* @returns void
*/
function showMessage(message) {
humane.log(message, {
2016-06-17 00:28:12 +00:00
timeoutAfterMove: 3000,
waitForMove: true
2016-02-29 15:59:36 +00:00
});
}
function showHelp(message) {
humane.log(message, {
timeout: 12000
});
}
function hideMessage() {
humane.remove();
}