ui2022/modal.html.twig line 1

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