1import { Tab } from "bootstrap"; 2import { a as useUniDirective, z as module$1, a7 as mergeDeep, v as selectOne, al as sleep, w as selectAll } from "./unicorn.js"; 3const defaultOptions = { 4 tabItemSelector: "[data-toggle=tab],[data-bs-toggle=tab],[data-toggle=pill],[data-bs-toggle=pill]", 5 delay: 0 6}; 7class KeepTab { 8 $element; 9 tabButtons; 10 storageKey = ""; 11 options; 12 constructor(selector, options = {}) { 13 options = mergeDeep({}, defaultOptions, options); 14 let uid; 15 if (typeof selector === "object") { 16 uid = options.uid || selector.id; 17 } else { 18 uid = selector; 19 } 20 const $element = this.$element = selectOne(selector); 21 if (!$element) { 22 console.warn(`[KeepTab] Element ${selector} not found.`); 23 return; 24 } 25 this.options = options; 26 this.$element = $element; 27 this.tabButtons = $element.querySelectorAll(this.options.tabItemSelector); 28 this.init(uid); 29 } 30 async init(uid) { 31 this.storageKey = "tab-href-" + await this.hashCode(location.href + ":" + uid); 32 this.bindEvents(); 33 await sleep(this.options.delay || 0); 34 this.switchTab(); 35 } 36 bindEvents() { 37 [].forEach.call(this.tabButtons, (button) => { 38 button.addEventListener("click", () => { 39 window.localStorage.setItem(this.storageKey, this.getButtonHref(button)); 40 }); 41 }); 42 } 43 getButtonHref(button) { 44 return button.dataset.bsTarget || button.dataset.target || button.href; 45 } 46 findTabButtonByHref(href) { 47 return selectAll(this.options.tabItemSelector).filter((button) => { 48 if (button.href === href) { 49 return true; 50 } 51 if (button.dataset.bsTarget === href) { 52 return true; 53 } 54 return button.dataset.target === href; 55 }).shift(); 56 } 57 activateTab(href) { 58 const tabTrigger = this.findTabButtonByHref(href); 59 if (tabTrigger) { 60 Tab.getOrCreateInstance(tabTrigger).show(); 61 } 62 } 63 hasTab(href) { 64 return this.findTabButtonByHref(href) != null; 65 } 66 /** 67 * Switch tab. 68 * 69 * @returns {boolean} 70 */ 71 switchTab() { 72 if (localStorage.getItem(this.storageKey)) { 73 if (!this.hasTab(localStorage.getItem(this.storageKey) || "")) { 74 localStorage.removeItem(this.storageKey); 75 return true; 76 } 77 const tabhref = localStorage.getItem(this.storageKey) || ""; 78 this.activateTab(tabhref); 79 } 80 } 81 /** 82 * Hash code. 83 */ 84 async hashCode(text) { 85 const msgBuffer = new TextEncoder().encode(text); 86 const hashBuffer = await globalThis.crypto.subtle.digest("SHA-256", msgBuffer); 87 const hashArray = Array.from(new Uint8Array(hashBuffer)); 88 return hashArray.map((b) => b.toString(16).padStart(2, "0")).join(""); 89 } 90} 91const ready = /* @__PURE__ */ useUniDirective("keeptab", { 92 mounted(el, { value }) { 93 const options = JSON.parse(value || "{}"); 94 module$1(el, "uni.keeptab", () => new KeepTab(el, options)); 95 } 96}); 97export { 98 KeepTab, 99 ready 100}; 101//# sourceMappingURL=keep-tab.js.map
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.