Created
November 13, 2017 08:46
-
-
Save Code-Stars/2066b4d20237d4f8c2aa24b1e3cee5e0 to your computer and use it in GitHub Desktop.
main.js getting too big
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Load JavaScript files. | |
* Elements are detected that need scripts. | |
* | |
* @author Floris Weijenburg <[email protected]> | |
* @version 08-11-2017 | |
*/ | |
// globals | |
var App = { | |
dialog: null, | |
validator: null, | |
rootPath: '/site/' | |
}; | |
(function () { | |
var min_ext = ""; | |
if (typeof environment !== 'undefined' && environment === 'production') { | |
min_ext = ".min"; | |
} | |
requirejs.config({ | |
shim: { | |
"common.layout": { | |
deps: ["jquery", "jqueryui"] | |
}, | |
"common.listSlider": { | |
deps: ["jquery", "jqueryui"] | |
}, | |
"forms.forms": { | |
deps: ["jquery", "jqueryui"] | |
}, | |
"service.mail": { | |
deps: ["jquery", "jqueryui"] | |
}, | |
"service.register": { | |
deps: ["jquery", "jqueryui"] | |
}, | |
"service.order": { | |
deps: ["jquery", "jqueryui"] | |
}, | |
"forms.registerSession": { | |
deps: ["forms.forms"] | |
}, | |
"utils.dialog": { | |
deps: ["utils.utils"] | |
} | |
}, | |
paths: { | |
jquery: "vendor/jquery-1.11.3.min", | |
jqueryui: "vendor/jquery-ui.min", | |
analytics: "vendor/analytics.min", | |
hotjar: "vendor/hotjar.min", | |
pickadate: "vendor/pickadate/picker.date.min", | |
pickatime: "vendor/pickadate/picker.time.min", | |
picker: "vendor/pickadate/picker.min", | |
pickadatenl: "vendor/pickadate/picker-nl_NL.min", | |
floValidator: "vendor/flo-validator.min", | |
// custom | |
"common.layout": "custom/common/layout" + min_ext, | |
"common.listSlider": "custom/common/listSlider" + min_ext, | |
"forms.forms": "custom/forms/forms" + min_ext, | |
"utils.dialog": "custom/utils/dialog" + min_ext, | |
"utils.utils": "custom/utils/utils" + min_ext, | |
"service.mail": "custom/services/mail" + min_ext, | |
"service.register": "custom/services/register" + min_ext, | |
"service.order": "custom/services/order" + min_ext, | |
"forms.registerSession": "custom/forms/registerSession" + min_ext | |
} | |
}); | |
requirejs(['utils.utils'], function () { | |
Utils.ready(function () { // DOM ready | |
// flo-validator.min.js (if needed) | |
if (typeof document.getElementsByTagName('form')[0] !== 'undefined') { | |
requirejs(['floValidator'], function () { | |
init_validator(); | |
}); | |
} | |
// layout.js | |
requirejs(['common.layout'], function () { | |
var layoutPlugin = $.common.layout(); | |
layoutPlugin.init("init"); | |
}); | |
// listSlider.js (when needed for a slide show) | |
if (typeof document.getElementsByClassName('list-slide')[0] !== 'undefined') { | |
requirejs(['common.listSlider'], function () { | |
var listSliderPlugin = $.common.listSlider(); | |
listSliderPlugin.init("init"); | |
}); | |
} | |
// dialog.js (when needed) | |
if (typeof document.getElementsByClassName('trigger-dialog--open')[0] !== 'undefined') { | |
requirejs(["utils.dialog"], function () { | |
App.dialog = new Dialog(); | |
}); | |
} | |
// register.js service (when needed) | |
if (typeof document.getElementsByClassName('form--training-register')[0] !== 'undefined') { | |
requirejs(["service.register"], function () { | |
termsAndConditionsPopupInit(); | |
var callback = function (form, allValid) { | |
if (allValid) { | |
var serviceRegister = $.services.serviceRegister(), | |
$form = $(form); | |
serviceRegister.registerForEvent($form); | |
$form.submit(); | |
} | |
}; | |
// run validation | |
App.validator.init('form--training-register', callback); | |
}); | |
} | |
// order.js service (when needed for order form) | |
if (typeof document.getElementsByClassName('form--product-order')[0] !== 'undefined') { | |
requirejs(["service.order"], function () { | |
termsAndConditionsPopupInit(); | |
var callback = function (form, allValid) { | |
if (allValid) { | |
var serviceOrder = $.services.serviceOrder(), | |
$form = $(form); | |
serviceOrder.orderProduct($form); | |
$form.submit(); | |
} | |
}; | |
// run validation | |
App.validator.init('form--product-order', callback); | |
}); | |
} | |
// order.js (when needed for acquire gifts) | |
if (typeof document.getElementsByClassName('form--gifts')[0] !== 'undefined') { | |
requirejs(["service.order"], function () { | |
termsAndConditionsPopupInit(); | |
var callback = function (form, allValid) { | |
if (allValid) { | |
var serviceOrder = $.services.serviceOrder(), | |
$form = $(form); | |
serviceOrder.acquireGifts($form); | |
$form.submit(); | |
} | |
}; | |
// run validation | |
App.validator.init('form--gifts', callback); | |
}); | |
} | |
// mail.js service (when needed for contact form) | |
if (typeof document.getElementsByClassName('form--contact')[0] !== 'undefined') { | |
requirejs(["service.mail", "floValidator"], function () { | |
var callback = function (form, allValid) { | |
if (allValid) { | |
var serviceMail = $.services.serviceMail(), | |
$form = $(form); | |
serviceMail.contact($form); | |
$form.submit(); | |
} | |
}; | |
// run validation | |
App.validator.init('form--contact', callback); | |
}); | |
} | |
// mail.js service (when needed for subscribe form) | |
if (typeof document.getElementsByClassName('form--subscribe')[0] !== 'undefined') { | |
requirejs(["service.mail", "floValidator"], function () { | |
var callback = function (form, allValid) { | |
if (allValid) { | |
var serviceMail = $.services.serviceMail(), | |
$form = $(form); | |
serviceMail.subscribe($form); | |
$form.submit(); | |
} | |
}; | |
// run validation | |
App.validator.init('form--subscribe', callback); | |
}); | |
} | |
// register.js (when needed for (trial) session register form) | |
if (typeof document.getElementsByClassName('form--session-register')[0] !== 'undefined') { | |
requirejs(["jquery"], function () { | |
termsAndConditionsPopupInit(); | |
var form = document.getElementsByClassName('form--session-register')[0], | |
serviceCategoryId = parseInt(document.getElementsByName('serviceCategoryId')[0].value); | |
registerForm(form, serviceCategoryId); | |
}); | |
} | |
// register.js (for partial at agenda) | |
if (typeof document.getElementsByClassName('btn-register')[0] !== 'undefined') { | |
requirejs(["jquery", "utils.dialog"], function () { | |
if (App.dialog === undefined || App.dialog === null) { | |
App.dialog = new Dialog(); | |
} | |
$('.btn-register').on('click', function () { | |
var dialog = document.getElementById('dialog-empty'); | |
var eventId = parseInt($(this).data('event-id')), | |
trainingId = parseInt($(this).data('training-id')), | |
serviceCategoryId = parseInt($(this).data('service-category-id')); | |
// get register form for event or consultation | |
var partial = serviceCategoryId === 2 ? 'pages/inc/blocks/block-event-register.php' : 'pages/inc/blocks/block-consultation-register.php'; | |
$.get(App.rootPath + partial + '?trainingId=' + trainingId + '&serviceCategoryId=' + serviceCategoryId + '&eventId=' + eventId, function (data) { | |
var $dialogBody = $('.dialog__body'); | |
$dialogBody.html(data); | |
// get dialog form | |
var dialogForm = $dialogBody.find('.form')[0]; // gets native DOM element | |
registerForm(dialogForm, serviceCategoryId); | |
App.dialog.openDialog(dialog); | |
App.dialog.setTitle(''); | |
$('.link-terms-and-conditions').on('click', function () { | |
var win = window.open('/meer-over/algemene-voorwaarden', '_blank'); | |
win.focus(); | |
}); | |
}); | |
}); | |
}); | |
} | |
// agenda partial - time blocks | |
if (typeof document.getElementsByClassName('btn-view-time-blocks')[0] !== 'undefined') { | |
requirejs(["jquery"], function () { | |
$('.btn-view-time-blocks').on('click', function () { | |
if (App.dialog === undefined || App.dialog === null) { | |
App.dialog = new Dialog(); | |
} | |
var eventId = $(this).data('event-id'); | |
var dialog = document.getElementById('dialog-empty'); | |
var partial = App.rootPath + 'pages/inc/blocks/block-time-blocks.php?eventId=' + eventId; | |
$.get(partial, function (data) { | |
$('.dialog__body').html(data); | |
App.dialog.openDialog(dialog); | |
}); | |
}); | |
}); | |
} | |
if (typeof document.getElementsByClassName('video-series__visual')[0] !== 'undefined') { | |
requirejs(["jquery", "utils.dialog"], function () { | |
App.dialog = new Dialog(); | |
registerForNewsLetterRequiredPopupInit(); | |
}); | |
} | |
if (typeof document.getElementsByClassName('hide-menu')[0] !== 'undefined') { | |
requirejs(["jquery"], function () { | |
// Let's hide the menu so people keep there focus | |
// on the landing page. | |
$('.btn-main-menu, .main-menu').hide(); | |
$('.l-header .l-container--aside').css('width', '100%'); | |
$('.l-header .l-container--main').css('width', '0%'); | |
$('.logo .logo__text, .logo .logo__slogan').css('text-align', 'center'); | |
}); | |
} | |
// analytics.min.js (when on production server) | |
if (typeof environment !== 'undefined' && environment === 'production') { | |
requirejs(["analytics"], function () { | |
}); | |
requirejs(["hotjar"], function () { | |
}); | |
} | |
// dialog.js - open dialog automatically when URL contains 'dialog' parameter with dialog id | |
requirejs(["utils.dialog"], function () { | |
var query = Utils.getQueryParams(document.location.search); | |
if (query.dialog !== undefined) { | |
if (App.dialog === undefined || App.dialog === null) { | |
App.dialog = new Dialog(); | |
} | |
var dialog = document.getElementById('dialog-empty'); | |
dialog.setAttribute('data-type', 'video'); | |
dialog.setAttribute('data-video-id', query.dialog.toString()); | |
App.dialog.openDialog(dialog); | |
} | |
}); | |
// automatically scroll to Y position when URL contains 'scroll_pos' parameter | |
requirejs(["utils.dialog"], function () { | |
var query = Utils.getQueryParams(document.location.search); | |
var scrollPos = parseInt(query.scroll_pos); | |
if (scrollPos > 0) { | |
window.scrollTo(0, scrollPos); | |
if (App.dialog.activeDialog !== undefined && App.dialog.activeDialog !== null) { | |
// let dialog scroll also | |
App.dialog.setYOffset(scrollPos); | |
} | |
} | |
}); | |
/** | |
* Register for a session or training. | |
* | |
* @param form | |
* @param serviceCategoryId | |
*/ | |
function registerForm(form, serviceCategoryId) { | |
requirejs(["service.mail", "service.register", "floValidator"], function () { | |
if (typeof form.getElementsByClassName('date-picker')[0] !== 'undefined') { | |
requirejs(["forms.registerSession"], function () { | |
var forms = $.forms.forms(); | |
forms.load_pickadate(function () { | |
// pickadate config for register session form | |
var registerSessionForm = $.forms.registerSession(); | |
registerSessionForm.init_pickadate(); | |
}); | |
}); | |
} | |
if (App.validator === undefined || App.validator === null) { | |
init_validator(); | |
} | |
// run validation | |
App.validator.init(form.getAttribute('class'), function (form, allValid) { | |
if (allValid) { | |
var $form = $(form), | |
serviceRegister = $.services.serviceRegister(); | |
$form.off('submit'); // remove previous event | |
if (serviceCategoryId === 1) { | |
// register for consultation | |
serviceRegister.registerForConsultation($form); | |
} | |
if (serviceCategoryId === 2) { | |
// register for event | |
serviceRegister.registerForEvent($form); | |
} | |
$form.submit(); | |
} | |
}); | |
}); | |
} | |
/** | |
* Initialize validator with default config. | |
*/ | |
function init_validator() { | |
var config = { | |
errorContainer: 'message--error', | |
language: 'nl_NL', | |
langPath: App.rootPath + 'scripts/languages/flo-validator/' | |
}; | |
App.validator = new FloValidator(config); | |
} | |
/** | |
* Initialize terms and conditions popup. | |
*/ | |
function termsAndConditionsPopupInit() { | |
$('.link-terms-and-conditions').on('click', function () { | |
$.get(App.rootPath + 'pages/meer-over/algemene-voorwaarden.php', function (data) { | |
if (App.dialog === undefined || App.dialog === null) { | |
App.dialog = new Dialog(); | |
} | |
var dialog = document.getElementById('dialog-empty'), | |
dialogBody = dialog.getElementsByClassName('dialog__body')[0]; | |
dialogBody.innerHTML = data; | |
App.dialog.setHeight(dialog, 500); | |
App.dialog.openDialog(dialog); | |
}); | |
}); | |
} | |
function registerForNewsLetterRequiredPopupInit() { | |
var $videoSeriesVisual = $('.video-series__visual .avatar__img'); | |
$videoSeriesVisual.css('cursor', 'pointer'); | |
$videoSeriesVisual.on('click', function () { | |
$('.form--subscribe .btn--submit').trigger('click'); | |
$.get(App.rootPath + 'pages/meer-over/aanmelding-nieuwsbrief-benodigd.php', function (data) { | |
if (App.dialog === undefined || App.dialog === null) { | |
App.dialog = new Dialog(); | |
} | |
var dialog = document.getElementById('dialog-empty'), | |
dialogBody = dialog.getElementsByClassName('dialog__body')[0]; | |
dialogBody.innerHTML = data; | |
App.dialog.openDialog(dialog); | |
}); | |
}); | |
} | |
}); | |
}); | |
})(document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment