vendor/symfony/twig-bundle/Resources/views/base_js.html.twig line 1

Open in your IDE?
  1. {# This file is based on WebProfilerBundle/Resources/views/Profiler/base_js.html.twig.
  2.    If you make any change in this file, verify the same change is needed in the other file. #}
  3. <script{% if csp_script_nonce is defined and csp_script_nonce %} nonce="{{ csp_script_nonce }}"{% endif %}>/*<![CDATA[*/
  4.     {# Caution: the contents of this file are processed by Twig before loading
  5.                 them as JavaScript source code. Always use '/*' comments instead
  6.                 of '//' comments to avoid impossible-to-debug side-effects #}
  7.     Sfjs = (function() {
  8.         "use strict";
  9.         if ('classList' in document.documentElement) {
  10.             var hasClass = function (el, cssClass) { return el.classList.contains(cssClass); };
  11.             var removeClass = function(el, cssClass) { el.classList.remove(cssClass); };
  12.             var addClass = function(el, cssClass) { el.classList.add(cssClass); };
  13.             var toggleClass = function(el, cssClass) { el.classList.toggle(cssClass); };
  14.         } else {
  15.             var hasClass = function (el, cssClass) { return el.className.match(new RegExp('\\b' + cssClass + '\\b')); };
  16.             var removeClass = function(el, cssClass) { el.className = el.className.replace(new RegExp('\\b' + cssClass + '\\b'), ' '); };
  17.             var addClass = function(el, cssClass) { if (!hasClass(el, cssClass)) { el.className += " " + cssClass; } };
  18.             var toggleClass = function(el, cssClass) { hasClass(el, cssClass) ? removeClass(el, cssClass) : addClass(el, cssClass); };
  19.         }
  20.         var addEventListener;
  21.         var el = document.createElement('div');
  22.         if (!('addEventListener' in el)) {
  23.             addEventListener = function (element, eventName, callback) {
  24.                 element.attachEvent('on' + eventName, callback);
  25.             };
  26.         } else {
  27.             addEventListener = function (element, eventName, callback) {
  28.                 element.addEventListener(eventName, callback, false);
  29.             };
  30.         }
  31.         return {
  32.             addEventListener: addEventListener,
  33.             createTabs: function() {
  34.                 var tabGroups = document.querySelectorAll('.sf-tabs:not([data-processed=true])');
  35.                 /* create the tab navigation for each group of tabs */
  36.                 for (var i = 0; i < tabGroups.length; i++) {
  37.                     var tabs = tabGroups[i].querySelectorAll('.tab');
  38.                     var tabNavigation = document.createElement('ul');
  39.                     tabNavigation.className = 'tab-navigation';
  40.                     for (var j = 0; j < tabs.length; j++) {
  41.                         var tabId = 'tab-' + i + '-' + j;
  42.                         var tabTitle = tabs[j].querySelector('.tab-title').innerHTML;
  43.                         var tabNavigationItem = document.createElement('li');
  44.                         tabNavigationItem.setAttribute('data-tab-id', tabId);
  45.                         if (j == 0) { addClass(tabNavigationItem, 'active'); }
  46.                         if (hasClass(tabs[j], 'disabled')) { addClass(tabNavigationItem, 'disabled'); }
  47.                         tabNavigationItem.innerHTML = tabTitle;
  48.                         tabNavigation.appendChild(tabNavigationItem);
  49.                         var tabContent = tabs[j].querySelector('.tab-content');
  50.                         tabContent.parentElement.setAttribute('id', tabId);
  51.                     }
  52.                     tabGroups[i].insertBefore(tabNavigation, tabGroups[i].firstChild);
  53.                 }
  54.                 /* display the active tab and add the 'click' event listeners */
  55.                 for (i = 0; i < tabGroups.length; i++) {
  56.                     tabNavigation = tabGroups[i].querySelectorAll('.tab-navigation li');
  57.                     for (j = 0; j < tabNavigation.length; j++) {
  58.                         tabId = tabNavigation[j].getAttribute('data-tab-id');
  59.                         document.getElementById(tabId).querySelector('.tab-title').className = 'hidden';
  60.                         if (hasClass(tabNavigation[j], 'active')) {
  61.                             document.getElementById(tabId).className = 'block';
  62.                         } else {
  63.                             document.getElementById(tabId).className = 'hidden';
  64.                         }
  65.                         tabNavigation[j].addEventListener('click', function(e) {
  66.                             var activeTab = e.target || e.srcElement;
  67.                             /* needed because when the tab contains HTML contents, user can click */
  68.                             /* on any of those elements instead of their parent '<li>' element */
  69.                             while (activeTab.tagName.toLowerCase() !== 'li') {
  70.                                 activeTab = activeTab.parentNode;
  71.                             }
  72.                             /* get the full list of tabs through the parent of the active tab element */
  73.                             var tabNavigation = activeTab.parentNode.children;
  74.                             for (var k = 0; k < tabNavigation.length; k++) {
  75.                                 var tabId = tabNavigation[k].getAttribute('data-tab-id');
  76.                                 document.getElementById(tabId).className = 'hidden';
  77.                                 removeClass(tabNavigation[k], 'active');
  78.                             }
  79.                             addClass(activeTab, 'active');
  80.                             var activeTabId = activeTab.getAttribute('data-tab-id');
  81.                             document.getElementById(activeTabId).className = 'block';
  82.                         });
  83.                     }
  84.                     tabGroups[i].setAttribute('data-processed', 'true');
  85.                 }
  86.             },
  87.             createToggles: function() {
  88.                 var toggles = document.querySelectorAll('.sf-toggle:not([data-processed=true])');
  89.                 for (var i = 0; i < toggles.length; i++) {
  90.                     var elementSelector = toggles[i].getAttribute('data-toggle-selector');
  91.                     var element = document.querySelector(elementSelector);
  92.                     addClass(element, 'sf-toggle-content');
  93.                     if (toggles[i].hasAttribute('data-toggle-initial') && toggles[i].getAttribute('data-toggle-initial') == 'display') {
  94.                         addClass(toggles[i], 'sf-toggle-on');
  95.                         addClass(element, 'sf-toggle-visible');
  96.                     } else {
  97.                         addClass(toggles[i], 'sf-toggle-off');
  98.                         addClass(element, 'sf-toggle-hidden');
  99.                     }
  100.                     addEventListener(toggles[i], 'click', function(e) {
  101.                         e.preventDefault();
  102.                         if ('' !== window.getSelection().toString()) {
  103.                             /* Don't do anything on text selection */
  104.                             return;
  105.                         }
  106.                         var toggle = e.target || e.srcElement;
  107.                         /* needed because when the toggle contains HTML contents, user can click */
  108.                         /* on any of those elements instead of their parent '.sf-toggle' element */
  109.                         while (!hasClass(toggle, 'sf-toggle')) {
  110.                             toggle = toggle.parentNode;
  111.                         }
  112.                         var element = document.querySelector(toggle.getAttribute('data-toggle-selector'));
  113.                         toggleClass(toggle, 'sf-toggle-on');
  114.                         toggleClass(toggle, 'sf-toggle-off');
  115.                         toggleClass(element, 'sf-toggle-hidden');
  116.                         toggleClass(element, 'sf-toggle-visible');
  117.                         /* the toggle doesn't change its contents when clicking on it */
  118.                         if (!toggle.hasAttribute('data-toggle-alt-content')) {
  119.                             return;
  120.                         }
  121.                         if (!toggle.hasAttribute('data-toggle-original-content')) {
  122.                             toggle.setAttribute('data-toggle-original-content', toggle.innerHTML);
  123.                         }
  124.                         var currentContent = toggle.innerHTML;
  125.                         var originalContent = toggle.getAttribute('data-toggle-original-content');
  126.                         var altContent = toggle.getAttribute('data-toggle-alt-content');
  127.                         toggle.innerHTML = currentContent !== altContent ? altContent : originalContent;
  128.                     });
  129.                     /* Prevents from disallowing clicks on links inside toggles */
  130.                     var toggleLinks = toggles[i].querySelectorAll('a');
  131.                     for (var j = 0; j < toggleLinks.length; j++) {
  132.                         addEventListener(toggleLinks[j], 'click', function(e) {
  133.                             e.stopPropagation();
  134.                         });
  135.                     }
  136.                     toggles[i].setAttribute('data-processed', 'true');
  137.                 }
  138.             }
  139.         };
  140.     })();
  141.     Sfjs.addEventListener(document, 'DOMContentLoaded', function() {
  142.         Sfjs.createTabs();
  143.         Sfjs.createToggles();
  144.     });
  145. /*]]>*/</script>