Repository URL to install this package:
|
Version:
0.1.0-alpha.1 ▾
|
(function ($, ally, drupalSettings, window) {
// Create a global state object
var modalState = {};
modalState.cache = {};
modalState.disabledHandler = null;
modalState.activeElement = null;
modalState.$triggerEl = null;
modalState.video = {
count: 0,
currentVideo: null
};
// Exit if there are no modal triggers on the page
if (!document.querySelector('[data-izimodal-open]')) { return; }
// Handle links that require functionality of a <button />
ally.when.key({
context: $('a[data-izimodal-open]'),
enter: simulateButtonClick,
space: simulateButtonClick
});
// Handle when users click on modal triggers
$(document).on('click', '[data-izimodal-open]', function handleModalTriggerClick(evt) {
modalState.$triggerEl = $(evt.currentTarget);
});
// Create unique ID's to video modals before instantiating them.
if($('[data-izimodal-video]').length) {
$('[data-izimodal-video]').each(function(){
var wrapper = $(this).closest('.video-modal-wrapper');
var id = 'video-modal-' + modalState.video.count++;
$(this).attr('data-izimodal-open', '#' + id);
$('.video-modal', wrapper).attr('id', id);
});
}
// Initiatialize each modal based on their trigger.
$('[data-izimodal-open]').each(function () {
var target = $(this).attr('data-izimodal-open');
// Exit if the target doesn't exist or has already been initialized
if (!$(target).length || modalState.cache[target]) { return; }
// Store target in modal cache
modalState.cache[target] = true;
var options = {
bodyOverflow: true,
onOpening: onOpening,
onOpened: onOpened,
onClosing: onClosing,
onClosed: onClosed
};
// Initialize modal
$(target).iziModal(options);
});
/***************************************************************************
* Function Declarations
***************************************************************************/
/**
* allyjs event handler for `ally.when.key` to
* simulate <button /> functionality for links
*
* @param {object} evt - event object
*/
function simulateButtonClick(evt) {
modalState.$triggerEl = $(evt.currentTarget);
evt.preventDefault();
evt.stopImmediatePropagation();
evt.target.click();
}
/**
* Handler for `iziModal.options.onOpening` callback
*
* @param {object} modal
*/
function onOpening(modal) {
// Save focus point
modalState.activeElement = document.activeElement;
// Disable all focusable elements exept
// those in the target element.
modalState.disabledHandler = ally.maintain.disabled({
filter: modal.$element
});
handlelVideoOptions(modal);
}
/**
* Handler for `iziModal.options.onOpened` callback
*
* @param {object} modal
*/
function onOpened(modal) {
// Add url to the continue button of the if the external link modal
if (modalState.$triggerEl.attr('data-externallink-continue')) {
var link = modalState.$triggerEl.attr('href');
var continueLink = modalState.$triggerEl.attr('data-externallink-continue');
$(continueLink, modal.$element).attr('href', link);
}
}
/**
* Handler for `iziModal.optionsonClosed` callback
*
* @param {object} modal
*/
function onClosed() {
// Focus on previously focused element
modalState.activeElement.focus();
}
/**
* Handler for `iziModal.options.onClosing` callback
*
* @param {object} modal
*/
function onClosing(modal) {
if(modalState.video.currentVideo) {
modalState.video.currentVideo.player.pause();
modalState.video.currentVideo.player.load();
}
// Re-enable focusable elements.
modalState.disabledHandler.disengage();
}
/**
* Parses the JSON passed to the `data-izimodal-video` attribute.
* The parsed data is then used to handle functionality of
* `MediaElement.js` video player.
*
*
*
* Autoplay is currently the only option available.
*
* @example
* <a href="#" data-izimodal-video='{"autoplay": true}' data-izimodal-open>Open Modal</a>
*
* @param {object} modal - iziModal instance
*/
function handlelVideoOptions(modal){
// Handle options passed to `data-izimodal-video`
if(modalState.$triggerEl.attr('data-izimodal-video')){
try {
var data = modalState.$triggerEl.attr('data-izimodal-video');
var options = JSON.parse(data);
var currentVideo = $('video', modal.$element)[0];
var player = currentVideo.player; // mediaelementjs instance
// Store `currentVideo` on modalState so we can use it in other methods
modalState.video.currentVideo = currentVideo;
// Need to reload the player so it triggers a resize event
// to add the correct height and width based on the modal.
player.play();
player.pause();
player.load();
if(options.autoplay) {
player.play();
}
} catch (err) {
console.log(err);
}
}
}
})(jQuery, ally, drupalSettings, window);