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

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