ui2022/schoolnow/modal--full.html.twig line 1

Open in your IDE?
  1. {% set attr = _args.attr|default({}) %}
  2. {% set dialog_styles = _args.dialog_styles|default(['centered', 'scrollable']) %}
  3. {% if dialog_styles is not iterable %}
  4.     {% set dialog_styles = dialog_styles|split(' ') %}
  5. {% endif %}
  6. {% set attr = attr|merge({
  7.     class: 'modal modal-template modal--fullscreen fade %s'|format(
  8.         attr.class|default('')
  9.     )|trim,
  10.     'data-keyboard': 'true',
  11.     tabindex: '-1',
  12.     role: 'dialog',
  13. }) %}
  14. <div {{ attributes(attr) }}>
  15.     <div class="modal-dialog modal-dialog-{{ dialog_styles|join(' modal-dialog-')|trim }}">
  16.         <div class="modal-content">
  17.             {% block content %}{% endblock %}
  18.         </div>
  19.     </div>
  20. </div>
  21. <script type="text/javascript">
  22.     (function (window, document, $, undefined) {
  23.         $(function () {
  24.             $('#{{ attr.id }}')
  25.                 // load up the start page when showing, only if a target page is set
  26.                 .on('show.bs.modal', function (e) {
  27.                     var $modal = $(e.target),
  28.                         $trigger = $(e.relatedTarget);
  29.                     if ($trigger.length && $trigger.is('a[href][href!=""][href!="#"]:not([href^="#"])')) {
  30.                         $modal
  31.                             .addClass('modal-ajax')
  32.                             .addClass('modal-ajax-loading')
  33.                             .trigger('navigate.cs.modal', {
  34.                                 url: $trigger.attr('href')
  35.                             });
  36.                     }
  37.                 })
  38.                 .on('shown.bs.modal', function (e) {
  39.                     var $modal = $(e.target);
  40.                     $modal.removeClass('modal-ajax-loading');
  41.                 })
  42.                 // once hidden, wipe the html content
  43.                 .on('hidden.bs.modal', function (e) {
  44.                     var $modal = $(e.target);
  45.                     if ($modal.is('.modal-ajax')) {
  46.                         $modal
  47.                             .removeClass('modal-ajax-loading')
  48.                             .removeClass('modal-ajax');
  49.                         $modal.find('.modal-content').html('');
  50.                     }
  51.                 })
  52.                 // special event handling for "navigating" within the modal
  53.                 .on('navigate.cs.modal', function (e, opts) {
  54.                     var $elem = $(e.target),// element the even is being triggered on, can be anything
  55.                         $modal = $('#{{ attr.id }}'),// get the modal wrapper/main element
  56.                         $ajax = $.ajax($.extend({}, {
  57.                             method: 'GET',
  58.                             xhrFields: {
  59.                                 withCredentials: true
  60.                             }
  61.                         }, opts));// ajax call to get the url
  62.                     // fade out the existing content
  63.                     $modal.find('.modal-content .modal-body').addClass('ajax-disabled');
  64.                     // handle success
  65.                     $ajax.done(function (resp) {
  66.                         // TODO: wait for animation then set the html on the element
  67.                         $modal.find('.modal-content').html(resp);
  68.                     });
  69.                     // TODO: handle errors
  70.                     $ajax.fail(function () {
  71.                         //alert('ERROR!');
  72.                     });
  73.                     // fade the content back in when done
  74.                     $ajax.always(function () {
  75.                         // TODO: need to wait for animations
  76.                         $modal
  77.                             .removeClass('ajax-disabled')
  78.                             .modal('handleUpdate');
  79.                         // scroll to the top of the modal
  80.                         $modal.scrollTop(0);
  81.                         // in case we need to run things after a modal reload
  82.                         $modal.trigger('updated.cs.modal');
  83.                     });
  84.                 })
  85.                 // handle special event of form ajax submission
  86.                 .on('submission.cs.modal', function (e, opts) {
  87.                     var $elem = $(e.target),// the element that triggered the form load
  88.                         $modal = $('#{{ attr.id }}'),// get the modal wrapper/main element
  89.                         $ajax = $.ajax($.extend({}, {
  90.                             xhrFields: {
  91.                                 withCredentials: true
  92.                             }
  93.                         }, opts));// ajax call for the form submission
  94.                     // fade out the content
  95.                     $modal.find('.modal-content .modal-body').addClass('ajax-disabled');
  96.                     // handle success
  97.                     $ajax.done(function (resp) {
  98.                         // check if we have an object or not
  99.                         // this chunk of code handles non-html returns
  100.                         // these special cases allow for basic redirection or modal closing
  101.                         if (typeof resp === 'object') {
  102.                             switch (true) {
  103.                                 // set a "close" key on the json object to TRUE in order to simply close the modal
  104.                                 // no page reload or anything will occur, the modal simply closes
  105.                                 case (resp.close === true):
  106.                                     $modal.modal('hide');
  107.                                     break;
  108.                                 // set a "redirect" key on the json object to TRUE in order to immediately reload the current URL in the browser
  109.                                 // this is useful for like modal form submissions
  110.                                 case (resp.redirect === true):
  111.                                     window.location.replace(window.location.origin + window.location.pathname + window.location.search);
  112.                                     break;
  113.                                 // the "redirect" key on the json object can also be set to a string
  114.                                 // in this case the string is expected to be a valid url pattern that the browser can understand
  115.                                 // this url will then be used to perform a redirection to the desired page
  116.                                 case (typeof resp.redirect === 'string'):
  117.                                     window.location.href = resp.redirect;
  118.                                     break;
  119.                             }
  120.                         } else {
  121.                             // TODO: wait for animation then set the html on the element
  122.                             $modal.find('.modal-content').html(resp);
  123.                         }
  124.                     });
  125.                     // TODO: handle errors
  126.                     $ajax.fail(function () {
  127.                         //alert('ERROR!');
  128.                     });
  129.                     // fade the content back in when done
  130.                     $ajax.always(function () {
  131.                         // TODO: wait for animation...
  132.                         $modal
  133.                             .removeClass('ajax-disabled')
  134.                             .modal('handleUpdate');
  135.                         // scroll to the top of the modal
  136.                         $modal.scrollTop(0);
  137.                         // in case we need to run things after a modal reload
  138.                         $modal.trigger('updated.cs.modal');
  139.                     });
  140.                 })
  141.             ;
  142.             // register click handlers in the modal to capture and process navigating to different "screens" in the modal
  143.             $('#{{ attr.id }}')
  144.                 // handle link clicks
  145.                 .on('click', 'a[target="_modal"][href][href!="#"]:not([href^="#"])', function (e) {
  146.                     var $link = $(e.currentTarget);// the link that was clicked
  147.                     // need to prevent the default behavior
  148.                     e.preventDefault();
  149.                     // due to using this in the page editor stuff, we also need to stop the click propagation
  150.                     e.stopPropagation();
  151.                     // trigger the navigation
  152.                     $link.trigger('navigate.cs.modal', {
  153.                         url: $link.attr('href')
  154.                     });
  155.                     // to be safe, also return false
  156.                     return false;
  157.                 })
  158.                 // handle form submissions, these need done through ajax otherwise the whole page will reload
  159.                 .on('submit', 'form[target="_modal"]', function (e) {
  160.                     var $form = $(e.currentTarget),// the form being submitted
  161.                         $button = (e.originalEvent) ? $(e.originalEvent.submitter) : $(),// input button, if any, that was used to submit the form
  162.                         data = $form.serializeArray();
  163.                     // add button if there is a name on it
  164.                     if ($button.attr('name')) {
  165.                         data.push({
  166.                             name: $button.attr('name'),
  167.                             value: ($button.attr('value')) ? $button.attr('value') : ''
  168.                         });
  169.                     }
  170.                     // we want to handle the submission, prevent the default
  171.                     e.preventDefault();
  172.                     // due to using this in the page editor stuff, we also need to stop the click propagation
  173.                     e.stopPropagation();
  174.                     // use the ajax submit
  175.                     $form.trigger('submission.cs.modal', {
  176.                         url: $button.attr('formaction') ? $button.attr('formaction') : $form.attr('action'),
  177.                         method: $button.attr('formmethod') ? $button.attr('formmethod') : $form.attr('method'),
  178.                         data: data
  179.                     });
  180.                     // to be safe, also return false
  181.                     return false;
  182.                 })
  183.             ;
  184.         });
  185.     })(window, document, jQuery);
  186. </script>
  187. <style type="text/css">
  188.     .modal-ajax.modal-ajax-loading .modal-content {
  189.         display: none !important;
  190.     }
  191. </style>