/*
# -----------------------------------------------------------------------------
# ~/theme_uno/modules/iconPicker/js/universal-icon-picker.min.js
# UniversalIconPicker v.1.1.0 implementation for J1 Theme
#
# Product/Info:
# https://jekyll.one
# https://github.com/migliori/universal-icon-picker
#
# Copyright (C) 2023 Juergen Adams
# Copyright (C) 2023 Gilles Migliori
#
# J1 Template is licensed under the MIT License.
# See: https://github.com/jekyll-one-org/j1-template/blob/main/LICENSE.md
# -----------------------------------------------------------------------------
*/
const iconPickerUrl=document.currentScript.src.replace(/js\/([a-z\.-]+)$/gm,""),loadedDependencies=[];!function(i,e){"function"==typeof define&&define.amd?define([],e("UniversalIconPicker")):"object"==typeof exports?module.exports=e("UniversalIconPicker"):i.UniversalIconPicker=e("UniversalIconPicker")}(this,function(){"use strict";let i=function(i){let e=document.createElement("div");return e.innerHTML=i,e.firstChild},e=function(i,e,t){let s;return function(){let r=this,n=arguments,a=function(){s=null,t||i.apply(r,n)},o=t&&!s;clearTimeout(s),s=setTimeout(a,e),o&&i.apply(r,n)}},t=function(i){let e={"&":"&","<":"<",">":">",'"':""","'":"'"};return i.replace(/[&<>"']/g,function(i){return e[i]})},s=function(i,e){let t,s={};for(t in i)Object.prototype.hasOwnProperty.call(i,t)&&(s[t]=i[t]);for(t in e)Object.prototype.hasOwnProperty.call(e,t)&&(s[t]=e[t]);return s};function r(e,t){this.selector=e,this.options=s({allowEmpty:!0,iconLibraries:null,iconLibrariesCss:null,mode:"autoload",onBeforeOpen:null,onReset:null,onSelect:null,resetSelector:null,loadCustomCss:!1},t),this.activeLibraryId="",this.filterIcon="",this.iconEventsLoaded=!1,this.iconLibraries={},this.iconLibrariesLoaded=!1,this.iconMarkup="",this.iconWrap="",this.idSuffix="-"+this.selector.replace(/[#\s[\]="]/g,""),this.sideBarBtn="",this.sideBarList=[],this.universalWrap='
',this.universalDomEle=i(this.universalWrap),this.sidebarTabs=this.universalDomEle.querySelector(".uip-modal--sidebar-tabs"),this.previewWrap=this.universalDomEle.querySelector("#uip-modal--icon-preview"+this.idSuffix),this.searchInput=this.universalDomEle.querySelector(".uip-modal--icon-search input"),"autoload"===this.options.mode?this.init():document.querySelector(this.selector).addEventListener("click",this.init.bind(this),{once:!0})}return r.prototype={init:function(){this._loadCssFiles(),"autoload"!==this.options.mode&&this._onBeforeOpen().then(()=>{this.open()}),document.querySelector(this.selector).addEventListener("click",()=>{this._onBeforeOpen().then(()=>{this.open()})}),this.options.resetSelector&&document.querySelector(this.options.resetSelector).addEventListener("click",this.options.onReset)},open:function(){this._loadIconLibraries().then(()=>{this.iconLibrariesLoaded=!0,document.getElementById("uip-modal"+this.idSuffix)?(this.universalDomEle.classList.remove("uip-close"),this.universalDomEle.classList.add("uip-open"),document.body.classList.add("stop-scrolling")):(document.body.appendChild(this.universalDomEle),document.body.classList.add("stop-scrolling"),this.universalDomEle.querySelector(".uip-modal--header-close-btn").addEventListener("click",()=>{this.universalDomEle.classList.add("uip-close"),this.universalDomEle.classList.remove("uip-open"),document.body.classList.remove("stop-scrolling")}),this.universalDomEle.querySelector(".uip-insert-icon-button").addEventListener("click",()=>{let i=this.universalDomEle.querySelector(".universal-selected");if(i){let e=i.querySelector("i").outerHTML,s={libraryId:i.dataset.libraryId,libraryName:i.dataset.libraryName,iconHtml:null,iconMarkup:null,iconClass:null,iconText:null};i.querySelector("i").classList.value.match("uip-icon-none")||(s.iconHtml=e,s.iconMarkup=t(e),s.iconClass=i.querySelector("i").classList.value,s.iconText=i.querySelector("i").innerText),this.options.onSelect(s)}})),this.iconEventsLoaded||(this.universalDomEle.querySelectorAll(".uip-icon-item").forEach(i=>{i.addEventListener("click",i=>{this.iconWrap.forEach(i=>{i.classList.remove("universal-selected")}),i.currentTarget.classList.toggle("universal-selected")}),i.addEventListener("dblclick",i=>{this.universalDomEle.querySelector(".uip-insert-icon-button").click()})}),this.iconEventsLoaded=!0),this.universalDomEle.querySelector(".uip-modal--icon-search input").focus()})},setOptions:function(i){this.options=s(this.options,i),i.iconLibrariesCss&&this._loadCssFiles(),i.iconLibraries&&(this.iconEventsLoaded=!1,this.iconLibrariesLoaded=!1,this._resetIconAndSidebarList())},_clickHandlerFunc:function(i){i.currentTarget.classList.contains("universal-active")||(this.sideBarBtn.forEach(function(i){i.classList.remove("universal-active")}),i.currentTarget.classList.add("universal-active")),this._sidebarFilterFunc(i.currentTarget.dataset.libraryId)},_iconItemMarkup:function(i,e){let t="",s=e["icon-style"],r=e.prefix;return this.options.allowEmpty&&(t+=''),r.match(/^material-icons/)?e.icons.forEach(function(e){t+=''+e+''+e.replace("-"," ")+"
"}):e.icons.forEach(function(e){t+='"}),t},_iconItemPush:function(i){this.previewWrap.innerHTML="",i.forEach(i=>{this.previewWrap.appendChild(i[1])})},_loadCssFiles:function(){let i=document.createElement("link");!loadedDependencies.includes("universal-icon-picker.min.css")&!this.options.loadCustomCss&&(i.rel="stylesheet",i.type="text/css",i.href=iconPickerUrl+"css/universal-icon-picker.min.css",i.media="screen",document.head.appendChild(i),loadedDependencies.push("universal-icon-picker.min.css")),this.options.iconLibrariesCss&&this.options.iconLibrariesCss.forEach(e=>{if(!loadedDependencies.includes(e)){let t=iconPickerUrl+"css/"+e;e.match(/^http|^\/\//)&&(t=e),(i=document.createElement("link")).rel="stylesheet",i.type="text/css",i.href=t,i.media="screen",document.head.appendChild(i),loadedDependencies.push(e)}})},_loadIconLibraries:async function(i=0){if(!this.options.iconLibraries)return console.error("Universal icon picker - no icon library loaded"),!1;if(this.iconLibrariesLoaded)return!0;0===i&&this.options.iconLibraries.length>1&&this.sideBarList.push({title:"all icons","list-icon":"","library-id":"all",prefix:""});let t=this.options.iconLibraries[i];await fetch(iconPickerUrl+"icons-libraries/"+t).then(i=>i.json()).then(s=>{let r=t.replace(/-([a-z])/g,function(i){return i[1].toUpperCase()}).replace(/\.[a-z.]+$/,""),n={};return(n[r]=s,Object.assign(this.iconLibraries,n),i+1===this.options.iconLibraries.length)?(this._setIconAndSidebarList(),this.activeLibraryId=this.sideBarList[0]["library-id"],this.sidebarTabs.innerHTML=this._sideBarListMarkup(this.sideBarList),this.previewWrap.innerHTML=this.iconMarkup,this.iconWrap=this.previewWrap.querySelectorAll(".uip-icon-item"),this.searchInput.addEventListener("keyup",e(this._searchFunc,100).bind(this),!1),this.sideBarBtn=this.sidebarTabs.querySelectorAll(".uip-modal--sidebar-tab-item"),this.sideBarBtn.forEach(i=>{i.addEventListener("click",this._clickHandlerFunc.bind(this),!1)}),!0):this._loadIconLibraries(i+1)}).catch(i=>(console.log(i),i))},_onBeforeOpen:async function(){if("function"==typeof this.options.onBeforeOpen)return this.options.onBeforeOpen()},_resetIconAndSidebarList:function(){this.sideBarList=[],this.iconMarkup="",this.iconLibraries={},this.iconWrap="",this.filterIcon="",this.sideBarBtn="",this.activeLibraryId=""},_searchFunc:function(i){let e=i.target.value.toLowerCase();this._searchFilterFunc(e,"filter")},_searchFilterFunc:function(i,e){this.filterIcon=Object.entries(this.iconWrap).filter(t=>-1!=t[1].dataset[e].indexOf(i)&&("all"===this.activeLibraryId||t[1].dataset.libraryId===this.activeLibraryId)),this._iconItemPush(this.filterIcon)},_setIconAndSidebarList:function(){for(let[i,e]of Object.entries(this.iconLibraries)){var t;this._setSideBarList((t=i).replace(/([A-Z])/g," $1"),e),this._setIconMarkup(i,e)}},_setIconMarkup:function(i,e){void 0!==e.icons?this.iconMarkup+=this._iconItemMarkup(i,e):Object.entries(e).forEach(e=>{this.iconMarkup+=this._iconItemMarkup(i,e[1])})},_sidebarFilterFunc:function(i){this.activeLibraryId=i,this.filterIcon=Object.entries(this.iconWrap).filter(function(e){return"all"===i||i===e[1].dataset.libraryId}),this._iconItemPush(this.filterIcon)},_setSideBarList:function(i,e){let t;void 0!==e.icons?(t={title:i,prefix:void 0!==e.prefix?e.prefix:"","list-icon":void 0!==e["list-icon"]?e["list-icon"]:"","library-id":void 0!==e["icon-style"]?e["icon-style"]:"all"},this.sideBarList.push(t)):Object.entries(e).forEach(e=>{t={title:i+" - "+e[0],prefix:void 0!==e[1].prefix?e[1].prefix:"","list-icon":void 0!==e[1]["list-icon"]?e[1]["list-icon"]:"","library-id":void 0!==e[1]["icon-style"]?e[1]["icon-style"]:"all"},this.sideBarList.push(t)})},_sideBarListMarkup:function(i){let e="";return i.forEach(i=>{let t="";if(i["library-id"]===this.activeLibraryId&&(t=" universal-active"),"all"!==i["library-id"]){let s='';i.prefix.match(/^material-icons/)&&(s=''+i["list-icon"]+""),e+='"}else e+='"}),e}},r});