1(function() { 2 "use strict"; 3 4 window.CQ = window.CQ || {}; 5 window.CQ.CoreComponents = window.CQ.CoreComponents || {}; 6 window.CQ.CoreComponents.container = window.CQ.CoreComponents.container || {}; 7 window.CQ.CoreComponents.container.utils = {}; 8 9 /** 10 * Utilities for Container Components (accordion, tabs) 11 * 12 * @namespace 13 * @alias CQ.CoreComponents.container.utils 14 * @type {{}} 15 */ 16 window.CQ.CoreComponents.container.utils = { 17 18 /** 19 * Removes the hash from the URL. 20 */ 21 removeUrlHash: function() { 22 history.replaceState(undefined, undefined, " "); 23 }, 24 25 /** 26 * Updates the URL hash with the panel ID without scrolling to it. 27 * 28 * @param {Object} component The container component (e.g. Accordion, Carousel, Tabs). 29 * @param {String} itemType The type of the item as defined in the component. 30 * @param {Number} index The index of the container item 31 */ 32 updateUrlHash: function(component, itemType, index) { 33 if (component && component._elements && component._elements[itemType] && 34 component._elements[itemType][index] && component._elements[itemType][index].id) { 35 var ID = component._elements[itemType][index].id; 36 history.replaceState(undefined, undefined, "#" + ID); 37 } 38 }, 39 40 /** 41 * Returns the index of the component item (accordion, carousel, tabs) that: 42 * - either corresponds to the deep link in the URL fragment 43 * - or contains the element that corresponds to the deep link in the URL fragment 44 * 45 * @param {Object} component The container component (Accordion, Carousel or Tabs). 46 * @param {String} itemType The type of the item as defined in the component. 47 * @param {String} itemContentType The type of the item content as defined in the component. 48 * @returns {Number} the index within the items array if the item exists, -1 otherwise. 49 */ 50 getDeepLinkItemIdx: function(component, itemType, itemContentType) { 51 if (window.location.hash) { 52 var deepLinkId = decodeURI(window.location.hash.substring(1)); 53 54 if (deepLinkId && document.getElementById(deepLinkId) && 55 component && component._config && component._config.element && 56 component._elements[itemType] && 57 component._config.element.querySelector("[id='" + deepLinkId + "']")) { 58 for (var i = 0; i < component._elements[itemType].length; i++) { 59 var item = component._elements[itemType][i]; 60 var itemContentContainsId = false; 61 if (component._elements[itemContentType]) { 62 var itemContent = component._elements[itemContentType][i]; 63 itemContentContainsId = itemContent && itemContent.querySelector("[id='" + deepLinkId + "']"); 64 } 65 if (item.id === deepLinkId || itemContentContainsId) { 66 return i; 67 } 68 } 69 } 70 return -1; 71 } 72 return -1; 73 }, 74 75 /** 76 * Returns the item of the container component (accordion, carousel, tabs) that: 77 * - either corresponds to the deep link in the URL fragment 78 * - or contains the element that corresponds to the deep link in the URL fragment 79 * 80 * @param {Object} component The Accordion or Tabs component. 81 * @param {String} itemType The type of the item as defined in the component. 82 * @param {String} itemContentType The type of the item content as defined in the component. 83 * @returns {Object} the item if it exists, undefined otherwise. 84 */ 85 getDeepLinkItem: function(component, itemType, itemContentType) { 86 var idx = window.CQ.CoreComponents.container.utils.getDeepLinkItemIdx(component, itemType, itemContentType); 87 if (component && component._elements && component._elements[itemType]) { 88 return component._elements[itemType][idx]; 89 } 90 }, 91 92 /** 93 * Scrolls the browser on page reload (if URI contains URI fragment) to the item of the container component (accordion, tabs) 94 that corresponds to the deep link in the URI fragment. 95 * This method fixes the issue existent with Chrome and related browsers, which are not scrolling on page reload (if URI contains URI fragment) 96 to the element that corresponds to the deep link in the URI fragment. 97 * Small setTimeout is needed, otherwise the scrolling will not work on Chrome. 98 */ 99 scrollToAnchor: function() { 100 setTimeout(function() { 101 if (window.location.hash) { 102 var id = decodeURIComponent(window.location.hash.substring(1)); 103 var anchorElement = document.getElementById(id); 104 if (anchorElement && anchorElement.offsetTop) { 105 anchorElement.scrollIntoView(); 106 } 107 } 108 }, 100); 109 } 110 }; 111}());
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.