{% set htmlId = _args.htmlId %}
{% set ajax = _args.ajax %}
{% set version = _args.version|default(null) %}
<div class="v3_2">
<div id="{{ htmlId }}" class="modal modal--full" data-cs-modal-start="{{ ajax }}" tabindex="-1">
{% if version is not empty %}
<div class="modal__version"><span class="badge badge--beta">{{ version }}</span></div>
{% endif %}
<button class="modal__close" data-dismiss="modal">
<span></span>
</button>
<div class="modal__content"></div>
</div>
</div>
<script>
$(function () {
$('#{{ htmlId }}')
// load up the start page when showing
.on('show.bs.modal', function (e) {
var $modal = $(e.target),
url = $modal.attr('data-cs-modal-start');
$modal.trigger('navigate.cs.modal', {
url: url
});
})
// once hidden, wipe the html content
.on('hidden.bs.modal', function (e) {
var $modal = $(e.target);
$modal.find('.modal__content').html('');
})
// special event handling for "navigating" within the modal
.on('navigate.cs.modal', function (e, opts) {
var $elem = $(e.target),// element the even is being triggered on, can be anything
$modal = $elem.closest('.modal'),// get the modal wrapper/main element
$ajax = $.ajax(opts.url, {
method: 'GET',
xhrFields: {
withCredentials: true
}
});// ajax call to get the url
// fade out the existing content
$modal.find('.modal__content').fadeOut(100);
// handle success
$ajax.done(function (resp, status, xhr) {
// wait for animation then set the html on the element
$modal.find('.modal__content').promise().done(function () {
$(this).html(resp);
});
});
// TODO: handle errors
$ajax.fail(function () {
//alert('ERROR!');
});
// fade the content back in when done
$ajax.always(function () {
$modal.find('.modal__content').promise().done(function () {
$(this).fadeIn(100);
});
});
})
// handle special event of form ajax submission
.on('submission.cs.modal', function (e, opts) {
var $elem = $(e.target),// the element that triggered the form load
$modal = $elem.closest('.modal'),// the modal wrapper
$ajax = $.ajax(opts.url, {
method: opts.method,
data: opts.data,
xhrFields: {
withCredentials: true
}
});// ajax call for the form submission
// fade out the content
$modal.find('.modal__content').fadeOut(100);
// handle success
$ajax.done(function (resp, status, xhr) {
// wait for animation then set the html on the element
$modal.find('.modal__content').promise().done(function () {
$(this).html(resp);
});
});
// TODO: handle errors
$ajax.fail(function () {
//alert('ERROR!');
});
// fade the content back in when done
$ajax.always(function () {
$modal.find('.modal__content').promise().done(function () {
$(this).fadeIn(100);
});
});
})
;
// register click handlers in the modal to capture and process navigating to different "screens" in the modal
$('#{{ htmlId }} .modal__content')
// hide the content, it will fade in when loaded
.hide()
// handle links
.on('click', 'a', function (e) {
var $link = $(e.currentTarget),// the link that was clicked
href = $link.attr('href'),// the href attribute of the link, if any
target = $link.attr('target');// the target value
// if we have an href attribute and the target is self; we are treating self as if the modal is an iframe
if (href && ( ! target || target === '_self')) {
// need to prevent the default behavior
e.preventDefault();
// if the href is legit follow it via modal navigation (ajax load and inject response into modal content)
if (href && href !== '#') {
// due to using this in the page editor stuff, we also need to stop the click propagation
e.stopPropagation();
// trigger the navigation
$link.trigger('navigate.cs.modal', {
url: href
});
// to be safe, also return false
return false;
}
}
})
// handle for submissions
.on('submit', 'form', function (e) {
var $form = $(e.currentTarget),// the form being submitted
$button = (e.originalEvent) ? $(e.originalEvent.submitter) : $(''),// input button, if any, that was used to submit the form
target = $form.attr('target'),// target value of the form
data = $form.serializeArray();
// add button if there is a name
if ($button.attr('name')) {
data.push({
name: $button.attr('name'),
value: ($button.attr('value')) ? $button.attr('value') : ''
});
}
// make sure that the target is one we are concerned with
if ( ! target || target === '_self') {
// we want to handle the submission, prevent the default
e.preventDefault();
// due to using this in the page editor stuff, we also need to stop the click propagation
e.stopPropagation();
// use the ajax submit
$form.trigger('submission.cs.modal', {
url: $form.attr('action'),
method: $form.attr('method'),
data: data
});
// to be safe, also return false
return false;
}
})
;
});
</script>