-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.min.js
26 lines (26 loc) · 15.9 KB
/
index.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*!
*
* The MIT License (MIT)
*
* Copyright © 2024 Taufik Nurrohman <https://github.com/taufik-nurrohman>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).TagPicker=e()}(this,(function(){"use strict";var t=function(t,e){return-1!==e.indexOf(t)},e=function(t){return Array.isArray(t)},n=function(t){return void 0!==t},r=function(t){return"function"==typeof t},i=function(t,e,n){return!(!t||"object"!=typeof t)&&(n?s(e)&&s(t.constructor)&&e===t.constructor:s(e)&&t instanceof e)},u=function(t){return"number"==typeof t},a=function(t,e){return void 0===e&&(e=!0),!(!t||"object"!=typeof t)&&(!e||i(t,Object,1))},s=function(t){return n(t)&&!function(t){return null===t}(t)},o=function(t){return"string"==typeof t},f=function(t){return t.length},c=function(){for(var n=arguments.length,r=Array(n),i=0;i<n;i++)r[i]=arguments[i];for(var u=r.shift(),o=0,l=f(r);o<l;++o)for(var v in r[o])if(s(u[v]))if(e(u[v])&&e(r[o][v])){u[v]=[].concat(u[v]);for(var p=0,h=f(r[o][v]);p<h;++p)t(r[o][v][p],u[v])||u[v].push(r[o][v][p])}else a(u[v])&&a(r[o][v])?u[v]=c({},u[v],r[o][v]):u[v]=r[o][v];else u[v]=r[o][v];return u},l=function(t){if(e(t))return t.map((function(e){return l(t)}));if(a(t)){for(var n in t)t[n]=l(t[n]);return t}return!1===t?"false":null===t?"null":!0===t?"true":""+t},v=document,p=window,h=function(t,e){return _(t,e||0)},g=function(t){return t.firstElementChild||null},_=function(t,e){var n=[].slice.call(t.children);return u(e)?n[e]||null:n},d=function(t,e){return t["next"+(e?"":"Element")+"Sibling"]||null},m=function(t,e){return e?t.closest(e)||null:t.parentNode||null},k=function(t){var e="form";return w(t,e)&&e===function(t){return(t&&t.nodeName||"").toLowerCase()||null}(t[e])?t[e]:m(t,e)},b=function(t,e){return t.previousElementSibling||null},y=function(t,e){void 0===e&&(e=!0);var n="textContent";if(!w(t,n))return!1;var r=t[n];return""!==(r=e?r.trim():r)?r:null},x=function(t,e){return t.classList.contains(e)},w=function(t,e){return e in t},j=function(t,e){return t.removeAttribute(e),t},E=function(t,e){return t.classList.remove(e),t},S=function(t){var e=m(t);return t.remove(),e},D=function(t,e,n){return!0===n&&(n=e),t.setAttribute(e,l(n)),t},A=function(t,e){return t.append(e),e},T=function(t,e){return t.classList.add(e),t},C=function(t,e,n){return t=o(t)?v.createElement(t):t,a(e)&&(n=e,e=!1),o(e)&&R(t,e),a(n)&&function(t,e){var n;for(var r in e)(n=e[r])||""===n||0===n?D(t,r,n):j(t,r)}(t,n),t},R=function(t,e,n){if(void 0===n&&(n=!0),null===e)return t;var r="innerHTML";return w(t,r)&&(t[r]=n?e.trim():e),t},L=function(t,e){return m(t).insertBefore(e,t),e},q=function(t,e,n){if(void 0===n&&(n=!0),null===e)return t;var r="textContent";return w(t,r)&&(t[r]=n?e.trim():e),t},N=function(t,e,n){return t.classList.toggle(e,n),t},z=function(t,e){return function(){var n=arguments,r=this;setTimeout((function(){return t.apply(r,n)}),e)}};var K=function(t,e,n){e.removeEventListener(t,n)},M=function(t){return t&&t.preventDefault()},O=function(t,e,n,r){void 0===r&&(r=!1),e.addEventListener(t,n,r)},P=function(t,e){return function(t){return i(t,RegExp)}(t)?t:RegExp(t,s(e)?e:"g")},B="ArrowLeft",H="ArrowRight",I="Home",W="Backspace",F="End",G="Enter",J=new WeakMap;function Q(t,e,n){Object.defineProperty(t,e,n)}function U(t){t.focus()}function V(t,e){t.forEach(e)}function X(t,e){V(t,e)}function Y(t){var e,n=p.getSelection();if(n.rangeCount)return(e=n.getRangeAt(0).cloneRange()).collapse(!0),e.setStart(t,0),(e+"").slice(-1)}function Z(t){return tt(t,J)||null}function $(t){return(t.value||"").replace(/\r/g,"")}function tt(t,e){return e.get(t)}function et(t,e){return e.has(t)}function nt(t){return t.disabled}function rt(t){var e=v.getSelection();e.rangeCount&&e.removeRange(e.getRangeAt(0))}function it(t,e){var n=v.getSelection();rt();var r=v.createRange();r.selectNodeContents(t),n.addRange(r),1===e?n.collapseToEnd():-1===e&&n.collapseToStart()}function ut(t,e){return at(t,e,J)}function at(t,e,n){return n.set(t,e)}function st(t){var e=[];return X(t,(function(t,n){return e.push(n)})),e}function ot(t){return ct(t).shift()}function ft(t){return ct(t).pop()}function ct(t){var e=[];return X(t,(function(t){return e.push(t)})),e}function lt(t,e){var n=this;if(!t)return n;if(!i(n,lt))return new lt(t,e);ut(t,function(t,e){return(e=e||t).fire=function(t,e,n){var r=this,i=r.hooks;return s(i[t])?(i[t].forEach((function(t){return t.apply(n||r,e)})),r):r},e.off=function(t,e){var n=this,r=n.hooks;if(!s(t))return r={},n;if(s(r[t]))if(s(e)){var i=r[t].length;if(0===i)delete r[t];else for(var u=0;u<i;++u)if(e===r[t][u]){r[t].splice(u,1);break}}else delete r[t];return n},e.on=function(t,e){var n=this.hooks;return s(n[t])||(n[t]=[]),s(e)&&n[t].push(e),this},t.hooks={},t}(n,lt.prototype));var r=c({},lt.state,o(e)?{join:e}:e||{});return a(e)&&e.escape&&(r.escape=e.escape),n.attach(t,r)}lt.from=function(t,e){return new lt(t,e)},lt.of=Z,lt.state={escape:[","],join:", ",max:1/0,min:0,n:"tag-picker",pattern:null,with:[]},lt.version="4.1.0",Q(lt,"name",{value:"TagPicker"});var vt=lt.prototype;Q(vt,"text",{get:function(){return y(this._mask.input)},set:function(t){var e=this._mask,n=this.self,r=e.hint,i=e.input;q(r,""===(t+="")?n.placeholder:""),q(i,t),it(i)}}),Q(vt,"value",{get:function(){var t=$(this.self);return""===t?null:t},set:function(t){var e=this,n=e.state;e.value&&V(e.value.split(n.join),(function(t){return e.let(t,1)})),V(t.split(n.join),(function(t){return e.set(t,-1,1)})),e.fire("change")}}),vt._filter=function(t){var e=this.state;return(t||"").replace(/[^ -~]/g," ").split(e.join).join("").replace(/\s+/g," ").trim()};var pt=!1,ht=!1;function gt(){rt();var t=Z(this);t._mask;var e=t._tags,n=t.mask,r=t.state.n;pt||ht||X(e,(function(t){return E(t,r+"__tag--selected")})),E(n,r+="--focus"),E(n,r+="-tag")}function _t(t){rt();var e=Z(this),n=e._mask,r=e.mask,i=e.state,u=n.text,a=i.n;E(u,a+"__text--focus"),E(r,a+="--focus"),E(r,a+="-text"),e.fire("blur",[t])}function dt(t){var e=Z(this),n=t.target,r=e.state.n+"__tag";x(n,r)||m(n,"."+r)||e.focus()}function mt(t){var e=this,n=Z(e).state.n+"__tag--selected";T(e,n),U(e),it(g(e))}function kt(t){var e=Z(this),n=e._tags,r=e.state,i=r.n+"__tag--selected",u=[];X(n,(function(t,e){x(t,i)&&u.push(e)})),t.clipboardData.setData("text/plain",u.join(r.join)),e.fire("copy",[t,u]).focus(),M(t)}function bt(t){var e=Z(this),n=e._mask,r=e._tags,i=e.state;n.input;var u=i.n+"__tag--selected",a=[];X(r,(function(t,n){x(t,u)&&(a.push(n),e.let(n,1))})),t.clipboardData.setData("text/plain",a.join(i.join)),e.fire("cut",[t,a]).fire("change",[this.title]).focus(),M(t)}function yt(t){var e=Z(this),n=e._mask,r=e._tags,i=e.mask,u=e.self,a=e.state,s=n.hint,o=n.input,f=n.text,c=a.n;X(r,(function(t){return E(t,c+"__tag--selected")})),T(f,c+"__text--focus"),T(i,c+="--focus"),T(i,c+="-text"),z((function(){return q(s,y(o,!1)?"":u.placeholder)}),1)(),e.focus().fire("focus",[t])}function xt(){Z(this).focus()}function wt(t){Z(this).fire("min.tags").focus(),M(t)}function jt(t){var e=Z(this);e._mask;var n=e.mask,r=e.state.n;T(n,r+="--focus"),T(n,r+="-tag"),e.fire("focus.tag",[t])}function Et(t){var e,n=this,r=t.key,i=pt=t.ctrlKey,u=ht=t.shiftKey,a=Z(n),s=a._mask,o=a._tags;a.mask;var c,l,v,p=a.state,h=s.text,_=b(n),m=d(n),k=p.n+"__tag--selected";if(u)T(n,k),it(g(n)),B===r?(_&&(x(_,k)?E(n,k):T(_,k),U(_),it(g(_))),e=!0):H===r&&(m&&h!==m&&(x(m,k)?E(n,k):T(m,k),U(m),it(g(m))),e=!0);else if(i)"a"===r?(X(o,(function(t){U(t),it(g(t)),T(t,k)})),e=!0):B===r?(_&&U(_),e=!0):H===r?(m&&h!==m?U(m):a.focus(),e=!0):G!==r&&" "!==r||(N(n,k),x(n,k)?(U(n),it(g(n))):it(g(ft(o))),e=!0);else{var y=[];if(X(o,(function(t,e){x(t,k)&&y.push(e),n!==t&&E(t,k)})),I===r)(c=ot(o))&&U(c),e=!0;else if(F===r)(l=ft(o))&&U(l),e=!0;else if(G===r||" "===r)N(n,k),x(n,k)?(U(n),it(g(n))):it(g(ft(o))),e=!0;else if(B===r)_&&U(_),e=!0;else if(H===r)m&&h!==m?U(m):a.focus(),e=!0;else if(W===r){if(a.let(v=n.title,1),f(y)>1)for(var w,j;j=y.pop();)_=(w=tt(j,o))&&b(w),a.let(j,1);a.fire("change",[v]),_?(U(_),it(g(_))):a.focus(),e=!0}else if("Delete"===r){if(a.let(v=n.title,1),f(y)>1)for(var S;S=y.shift();)m=tt(S,o)&&d(S),a.let(S,1);a.fire("change",[v]),m&&h!==m?(U(m),it(g(m))):a.focus(),e=!0}else"Escape"===r||"Tab"===r?(a.focus(),e=!0):(a.focus(),e=!1)}e&&M(t)}function St(t){var e=t.key,n=Z(this),r=n._tags,i=n.state.n+"__tag--selected";if(ht)if(B===e||H===e);else{var u=0;X(r,(function(t){x(t,i)&&++u})),u<2&&E(this,i)}pt=ht=!1}function Dt(e){var n=this,r=Y(n),i=Z(n),u=i.state.escape;if("\n"===r&&(t("\n",u)||t(13,u))||"\t"===r&&(t("\t",u)||t(9,u))||t(r,u))return i.set(y(n).slice(0,-1)).focus().text="",M(e)}function At(e){var n,i=this,u=e.key,a=e.keyCode,s=pt=e.ctrlKey,o=ht=e.shiftKey,f=Z(i),c=f._active,l=f._mask,p=f._tags;f.mask;var h,_,d=f.self,m=f.state,b=l.hint,x=m.n+"__tag--selected";if(escape=m.escape,!c)return M(e);if(G===u&&(t("\n",escape)||t(13,escape))||"Tab"===u&&(t("\t",escape)||t(9,escape))||t(u,escape)||t(a,escape))return f.set(y(i)).focus().text="",M(e);z((function(){return q(b,y(i,!1)?"":d.placeholder)}),1)();var w,j=""===Y(i),E=null===y(i,!1);if(o)(E||j)&&B===u&&(_=ft(p))&&(_&&(U(_),it(g(_))),T(_,x),n=!0);else if(s)"a"===u&&null===y(i,!1)&&null!==f.value?(X(p,(function(t){U(t),it(g(t)),T(t,x)})),n=!0):I===u?((h=ot(p))&&U(h),n=!0):F===u||B===u?((_=ft(p))&&U(_),n=!0):W===u&&((_=ft(p))&&f.let(_.title),f.focus(),n=!0);else if(G===u){var S=k(d);if(S&&r(S.requestSubmit)){var D=(w="button:not([type]),button[type=submit],input[type=image],input[type=submit]",(S||v).querySelector(w));D?S.requestSubmit(D):S.requestSubmit()}n=!0}else E?I===u?((h=ot(p))&&U(h),n=!0):F===u||B===u?((_=ft(p))&&U(_),n=!0):W===u&&((_=ft(p))&&f.let(_.title),f.focus(),n=!0):j&&B===u&&((_=ft(p))&&U(_),n=!0);n&&M(e)}function Tt(){pt=ht=!1}function Ct(t){var e=Z(this),n=e._tags,r=e.state,i=r.n+"__tag--selected",u=!0,a=(t.clipboardData||p.clipboardData).getData("text")+"";try{X(n,(function(t){if(!x(t,i))throw u=!1,""}))}catch(t){}u&&e.value&&V(e.value.split(r.join),(function(t){return e.let(t,1)}));var s=a.split(r.join);V(s,(function(t){return e.set(t,-1,1)})),e.fire("paste",[t,s]).focus().fire("change"),M(t)}function Rt(t){var e=this,n=Z(e),r=n._mask,i=n.self,u=n.state,a=r.hint,s=(t.clipboardData||p.clipboardData).getData("text")+"";(function(t,e){var n,r=p.getSelection();r.rangeCount&&((n=r.getRangeAt(0)).deleteContents(),n.insertNode(v.createTextNode(e)))})(0,s),q(a,y(e)?"":i.placeholder),z((function(){if(s=y(e),n.text="",s){var r=s.split(u.join);V(r,(function(t){return n.set(t,-1,1)})),n.fire("paste",[t,r]).fire("change")}}),1)(),M(t)}function Lt(t){var e=this,n=Z(e),r=n._active,i=n._mask,u=n._tags,a=n.state,s=i.text,o=a.n+"__tag--selected";if(r){if(U(e),N(e,o),pt);else if(ht){rt();var c,l=m(e),p=(y="."+o,(l||v).querySelectorAll(y)),h=p[0],_=p[f(p)-1];if(h!==_)for(;(c=d(h))&&s!==c&&h!==_;)T(h=c,o)}else{rt();var k=2===t.button,b=0;X(u,(function(t){x(t,o)&&++b,e===t||k||E(t,o)})),b>0&&T(e,o)}var y;x(e,o)?(U(e),it(g(e))):it(ft(u)),n.fire("touch.tag",[t]),M(t),function(t){t&&t.stopPropagation()}(t)}}function qt(t){var e=this,n=m(e),r=Z(n);K("mousedown",e,qt),K("touchstart",e,qt),r.let(n.title).focus(),M(t)}function Nt(t){Z(this).let().fire("reset",[t])}function zt(t){var e=Z(this),n=e._tags,r=e.state;return n.size<r.min?(e.fire("min.tags").focus(),M(t)):e.fire("submit",[t])}return vt.attach=function(t,n){var i=this;if(t=t||i.self,n&&o(n)&&(n={join:n}),n=c({},i.state,n||{}),x(t,n.n+"__self"))return i;(function(t){return t.required})(t)&&!n.min&&(n.min=1),i._active=!nt(t)&&!function(t){return t.readOnly}(t),i._tags=new Map,i._value=$(t)||null,i.self=t,i.state=n;var u=n.n,l=k(t),v=C("div",{class:u,tabindex:!nt(t)&&-1});i.mask=v;var p,h,g=C("span",{class:u+"__tags"}),_=C("span",{class:u+"__text"}),b=C("span",{autocapitalize:"off",contenteditable:!nt(t)&&"",spellcheck:"false"}),y=C("span",t.placeholder+"");A(v,g),A(g,_),A(_,b),A(_,y),T(t,u+"__self"),h=v,m(p=t).insertBefore(h,d(p,!0)),l&&(ut(l,i),O("reset",l,Nt),O("submit",l,zt)),O("blur",b,_t),O("click",v,dt),O("focus",t,xt),O("focus",b,yt),O("input",b,Dt),O("invalid",t,wt),O("keydown",b,At),O("keyup",b,Tt),O("paste",b,Rt),t.tabIndex=-1,ut(v,i),ut(b,i);var w={};if(w.hint=y,w.input=b,w.of=t,w.self=v,w.tags=g,w.text=_,i._mask=w,i._value&&V(i._value.split(n.join),(function(t){return i.set(t,-1,1,1)})),s(n)&&e(n.with))for(var j=0,E=f(n.with);j<E;++j){var S=n.with[j];o(S)&&(S=lt[S]),r(S)?S.call(i,t,n):a(S)&&r(S.attach)&&S.attach.call(i,t,n)}return i},vt.blur=function(){rt();var t=this._mask,e=this._tags,n=t.input;return X(e,(function(t){return t.blur()})),n.blur()},vt.detach=function(){var t=this,n=t._mask,i=t.mask,u=t.self,s=t.state,c=n.input;if(!x(u,s.n+"__self"))return t;var l=k(u);if(t._active=!1,t._value=$(u)||null,l&&(K("reset",l,Nt),K("submit",l,zt)),K("blur",c,_t),K("click",i,dt),K("focus",c,yt),K("focus",u,xt),K("input",c,Dt),K("invalid",u,wt),K("keydown",c,At),K("keyup",c,Tt),K("paste",c,Rt),e(s.with))for(var v=0,p=f(s.with);v<p;++v){var h=s.with[v];o(h)&&(h=lt[h]),a(h)&&r(h.detach)&&h.detach.call(t,u,s)}return u.tabIndex=null,E(u,s.n+"__self"),S(i),t._mask={of:u},t.mask=null,t},vt.focus=function(t){var e=this._mask.input;return e&&(U(e),it(e,t)),this},vt.get=function(t){var e=this,n=e._active,r=e._tags;if(!n)return!1;if(e.fire("get.tag",[t]),!et(t,r))return null;var i=-1;try{X(r,(function(e,n){if(++i,t===n)throw""}))}catch(t){}return i},vt.let=function(t,e){var r=this,i=r._active,u=r._tags,a=r._value,s=r.self,o=r.state;if(!i)return r;if(!n(t))return r.value&&V(r.value.split(o.join),(function(t){return r.let(t,1)})),V(a.split(o.join),(function(t){return r.set(t,-1,1)})),r.fire("change");if(u.size<o.min)return r.fire("min.tags",[t]);if(!et(t,u))return r.fire("not.tag",[t]);r.fire("is.tag",[t]);var f=tt(t,u);h(f,0);var c,l=h(f,1);return K("blur",f,gt),K("contextmenu",f,mt),K("copy",f,kt),K("cut",f,bt),K("focus",f,jt),K("keydown",f,Et),K("keyup",f,St),K("mousedown",f,Lt),K("mousedown",l,qt),K("paste",f,Ct),K("touchstart",f,Lt),K("touchstart",l,qt),S(f),c=t,r._tags.delete(c),s.value=st(r._tags).join(o.join),r.fire("let.tag",[t]),e||r.fire("change",[t]),r},vt.set=function(t,e,n,i){var a=this,s=a._active,f=a._filter,c=a._mask,l=a._tags,v=a.self,p=a.state,h=c.text,g=p.pattern,_=p.n;if(!s&&!i)return a;if(l.size>=p.max)return a.fire("max.tags",[t]);if(r(f)&&(t=f.call(a,t)),""===t||o(g)&&!P(g).test(t))return a.fire("not.tag",[t]);if(et(t,l))return a.fire("has.tag",[t]);a.fire("is.tag",[t]);var d=C("span",{class:_+"__tag",tabindex:!!s&&-1,title:t}),m=C("span",t.replace(/&/g,"&").replace(/>/g,">").replace(/</g,"<")),k=C("span",{class:_+"__x",tabindex:-1});if(s&&(O("blur",d,gt),O("contextmenu",d,mt),O("copy",d,kt),O("cut",d,bt),O("focus",d,jt),O("keydown",d,Et),O("keyup",d,St),O("mousedown",d,Lt),O("mousedown",k,qt),O("paste",d,Ct),O("touchstart",d,Lt),O("touchstart",k,qt),ut(d,a)),A(d,m),A(d,k),function(t){return u(t)&&0==t%1}(e)&&e>=0){var b=st(l);b.splice(e,0,t),a._tags=new Map,at(t,d,l),V(b,(function(t){var e;at(t,e=tt(t,l),a._tags),L(h,e)}))}else L(h,d),at(t,d,a._tags);return v.value=st(a._tags).join(p.join),a.fire("set.tag",[t]),n||a.fire("change",[t]),a},lt}));