-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.min.js
26 lines (26 loc) · 16.6 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(t){if(e(t))return t.map((function(t){return c(t)}));if(function(t){return/^-?(?:\d*.)?\d+$/.test(t+"")}(t))return function(t,e){return void 0===e&&(e=10),e?parseInt(t,e):parseFloat(t)}(t);if(a(t)){for(var n in t)t[n]=c(t[n]);return t}return"false"!==t&&("null"===t?null:"true"===t||t)},l=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,c=f(r);o<c;++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]=l({},u[v],r[o][v]):u[v]=r[o][v];else u[v]=r[o][v];return u},v=function(t){if(e(t))return t.map((function(e){return v(t)}));if(a(t)){for(var n in t)t[n]=v(t[n]);return t}return!1===t?"false":null===t?"null":!0===t?"true":""+t},p=document,h=window,g=function(t,e){return _(t,e||0)},d=function(t){return t.firstElementChild||null},_=function(t,e){var n=[].slice.call(t.children);return u(e)?n[e]||null:n},m=function(t,e,n){void 0===n&&(n=!0);var r=function(t,e,n){if(void 0===n&&(n=!0),!j(t,e))return null;var r=t.getAttribute(e);return n?c(r):r}(t,"data-"+e,n),i=(r+"").trim();return n&&i&&("["===i[0]&&"]"===i.slice(-1)||"{"===i[0]&&"}"===i.slice(-1))&&null!==(i=function(t){var e=null;try{e=JSON.parse(t)}catch(t){}return e}(r))?i:r},k=function(t,e){return t["next"+(e?"":"Element")+"Sibling"]||null},b=function(t,e){return e?t.closest(e)||null:t.parentNode||null},y=function(t){var e="form";return S(t,e)&&e===function(t){return(t&&t.nodeName||"").toLowerCase()||null}(t[e])?t[e]:b(t,e)},x=function(t,e){return t.previousElementSibling||null},w=function(t,e){void 0===e&&(e=!0);var n="textContent";if(!S(t,n))return!1;var r=t[n];return""!==(r=e?r.trim():r)?r:null},j=function(t,e){return t.hasAttribute(e)},E=function(t,e){return t.classList.contains(e)},S=function(t,e){return e in t},A=function(t,e){return t.removeAttribute(e),t},D=function(t,e){return t.classList.remove(e),t},T=function(t){var e=b(t);return t.remove(),e},C=function(t,e,n){return!0===n&&(n=e),t.setAttribute(e,v(n)),t},R=function(t,e){return t.append(e),e},L=function(t,e){return t.classList.add(e),t},q=function(t,e,n){return t=o(t)?p.createElement(t):t,a(e)&&(n=e,e=!1),o(e)&&N(t,e),a(n)&&function(t,e){var n;for(var r in e)(n=e[r])||""===n||0===n?C(t,r,n):A(t,r)}(t,n),t},N=function(t,e,n){if(void 0===n&&(n=!0),null===e)return t;var r="innerHTML";return S(t,r)&&(t[r]=n?e.trim():e),t},O=function(t,e){return b(t).insertBefore(e,t),e},z=function(t,e,n){if(void 0===n&&(n=!0),null===e)return t;var r="textContent";return S(t,r)&&(t[r]=n?e.trim():e),t},K=function(t,e,n){return t.classList.toggle(e,n),t},M=function(t,e){return function(){var n=arguments,r=this;setTimeout((function(){return t.apply(r,n)}),e)}};var P=function(t,e,n){e.removeEventListener(t,n)},B=function(t){return t&&t.preventDefault()},I=function(t,e,n,r){void 0===r&&(r=!1),e.addEventListener(t,n,r)},H=function(t,e){return function(t){return i(t,RegExp)}(t)?t:RegExp(t,s(e)?e:"g")},F="ArrowLeft",J="ArrowRight",W="Home",$="Backspace",G="End",Q="Enter",U=new WeakMap;function V(t,e,n){Object.defineProperty(t,e,n)}function X(t){t.focus()}function Y(t,e){t.forEach(e)}function Z(t,e){Y(t,e)}function tt(t){var e,n=h.getSelection();if(n.rangeCount)return(e=n.getRangeAt(0).cloneRange()).collapse(!0),e.setStart(t,0),(e+"").slice(-1)}function et(t){return it(t,U)||null}function nt(t){return m(t,"name")}function rt(t){return(t.value||"").replace(/\r/g,"")}function it(t,e){return e.get(t)}function ut(t,e){return e.has(t)}function at(t){return t.disabled}function st(t){var e=p.getSelection();e.rangeCount&&e.removeRange(e.getRangeAt(0))}function ot(t,e){var n=p.getSelection();st();var r=p.createRange();r.selectNodeContents(t),n.addRange(r),1===e?n.collapseToEnd():-1===e&&n.collapseToStart()}function ft(t,e){return ct(t,e,U)}function ct(t,e,n){return n.set(t,e)}function lt(t){var e=[];return Z(t,(function(t,n){return e.push(n)})),e}function vt(t){return ht(t).shift()}function pt(t){return ht(t).pop()}function ht(t){var e=[];return Z(t,(function(t){return e.push(t)})),e}function gt(t,e){var n=this;if(!t)return n;if(!i(n,gt))return new gt(t,e);ft(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,gt.prototype));var r=l({},gt.state,o(e)?{join:e}:e||{});return a(e)&&e.escape&&(r.escape=e.escape),n.attach(t,r)}gt.from=function(t,e){return new gt(t,e)},gt.of=et,gt.state={escape:[","],join:", ",max:1/0,min:0,n:"tag-picker",pattern:null,with:[]},gt.version="4.1.0",V(gt,"name",{value:"TagPicker"});var dt=gt.prototype;V(dt,"text",{get:function(){return w(this._mask.input)},set:function(t){var e=this._mask,n=this.self,r=e.hint,i=e.input;z(r,""===(t+="")?n.placeholder:""),z(i,t),ot(i)}}),V(dt,"value",{get:function(){var t=rt(this.self);return""===t?null:t},set:function(t){var e=this,n=e.state;e.value&&Y(e.value.split(n.join),(function(t){return e.let(t,1)})),Y(t.split(n.join),(function(t){return e.set(t,-1,1)})),e.fire("change")}}),dt._filter=function(t){var e=this.state;return(t||"").replace(/[^ -~]/g," ").split(e.join).join("").replace(/\s+/g," ").trim()};var _t=!1,mt=!1;function kt(){st();var t=et(this);t._mask;var e=t._tags,n=t.mask,r=t.state.n;_t||mt||Z(e,(function(t){return D(t,r+"__tag--selected")})),D(n,r+="--focus"),D(n,r+="-tag")}function bt(t){st();var e=et(this),n=e._mask,r=e.mask,i=e.state,u=n.text,a=i.n;D(u,a+"__text--focus"),D(r,a+="--focus"),D(r,a+="-text"),e.fire("blur",[t])}function yt(t){var e=et(this),n=t.target,r=e.state.n+"__tag";E(n,r)||b(n,"."+r)||e.focus()}function xt(t){var e=this,n=et(e).state.n+"__tag--selected";L(e,n),X(e),ot(d(e))}function wt(t){var e=et(this),n=e._tags,r=e.state,i=r.n+"__tag--selected",u=[];Z(n,(function(t,e){E(t,i)&&u.push(e)})),t.clipboardData.setData("text/plain",u.join(r.join)),e.fire("copy",[t,u]).focus(),B(t)}function jt(t){var e=et(this),n=e._mask,r=e._tags,i=e.state;n.input;var u=i.n+"__tag--selected",a=[];Z(r,(function(t,n){E(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",[nt(this)]).focus(),B(t)}function Et(t){var e=et(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;Z(r,(function(t){return D(t,c+"__tag--selected")})),L(f,c+"__text--focus"),L(i,c+="--focus"),L(i,c+="-text"),M((function(){return z(s,w(o,!1)?"":u.placeholder)}),1)(),e.focus().fire("focus",[t])}function St(){et(this).focus()}function At(t){et(this).fire("min.tags").focus(),B(t)}function Dt(t){var e=et(this);e._mask;var n=e.mask,r=e.state.n;L(n,r+="--focus"),L(n,r+="-tag"),e.fire("focus.tag",[t])}function Tt(t){var e,n=this,r=t.key,i=_t=t.ctrlKey,u=mt=t.shiftKey,a=et(n),s=a._mask,o=a._tags;a.mask;var c,l,v,p=a.state,h=s.text,g=x(n),_=k(n),m=p.n+"__tag--selected";if(u)L(n,m),ot(d(n)),F===r?(g&&(E(g,m)?D(n,m):L(g,m),X(g),ot(d(g))),e=!0):J===r&&(_&&h!==_&&(E(_,m)?D(n,m):L(_,m),X(_),ot(d(_))),e=!0);else if(i)"a"===r?(Z(o,(function(t){X(t),ot(d(t)),L(t,m)})),e=!0):F===r?(g&&X(g),e=!0):J===r?(_&&h!==_?X(_):a.focus(),e=!0):Q!==r&&" "!==r||(K(n,m),E(n,m)?(X(n),ot(d(n))):ot(d(pt(o))),e=!0);else{var b=[];if(Z(o,(function(t,e){E(t,m)&&b.push(e),n!==t&&D(t,m)})),W===r)(c=vt(o))&&X(c),e=!0;else if(G===r)(l=pt(o))&&X(l),e=!0;else if(Q===r||" "===r)K(n,m),E(n,m)?(X(n),ot(d(n))):ot(d(pt(o))),e=!0;else if(F===r)g&&X(g),e=!0;else if(J===r)_&&h!==_?X(_):a.focus(),e=!0;else if($===r){if(a.let(v=nt(n),1),f(b)>1)for(var y,w;w=b.pop();)g=(y=it(w,o))&&x(y),a.let(w,1);a.fire("change",[v]),g?(X(g),ot(d(g))):a.focus(),e=!0}else if("Delete"===r){if(a.let(v=nt(n),1),f(b)>1)for(var j;j=b.shift();)_=it(j,o)&&k(j),a.let(j,1);a.fire("change",[v]),_&&h!==_?(X(_),ot(d(_))):a.focus(),e=!0}else"Escape"===r||"Tab"===r?(a.focus(),e=!0):(a.focus(),e=!1)}e&&B(t)}function Ct(t){var e=t.key,n=et(this),r=n._tags,i=n.state.n+"__tag--selected";if(mt)if(F===e||J===e);else{var u=0;Z(r,(function(t){E(t,i)&&++u})),u<2&&D(this,i)}_t=mt=!1}function Rt(e){var n=this,r=tt(n),i=et(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(w(n).slice(0,-1)).focus().text="",B(e)}function Lt(e){var n,i=this,u=e.key,a=e.keyCode,s=_t=e.ctrlKey,o=mt=e.shiftKey,f=et(i),c=f._active,l=f._mask,v=f._tags;f.mask;var h,g,_=f.self,m=f.state,k=l.hint,b=m.n+"__tag--selected";if(escape=m.escape,!c)return B(e);if(Q===u&&(t("\n",escape)||t(13,escape))||"Tab"===u&&(t("\t",escape)||t(9,escape))||t(u,escape)||t(a,escape))return f.set(w(i)).focus().text="",B(e);M((function(){return z(k,w(i,!1)?"":_.placeholder)}),1)();var x,j=""===tt(i),E=null===w(i,!1);if(o)(E||j)&&F===u&&(g=pt(v))&&(g&&(X(g),ot(d(g))),L(g,b),n=!0);else if(s)"a"===u&&null===w(i,!1)&&null!==f.value?(Z(v,(function(t){X(t),ot(d(t)),L(t,b)})),n=!0):W===u?((h=vt(v))&&X(h),n=!0):G===u||F===u?((g=pt(v))&&X(g),n=!0):$===u&&((g=pt(v))&&f.let(nt(g)),f.focus(),n=!0);else if(Q===u){var S=y(_);if(S&&r(S.requestSubmit)){var A=(x="button:not([type]),button[type=submit],input[type=image],input[type=submit]",(S||p).querySelector(x));A?S.requestSubmit(A):S.requestSubmit()}n=!0}else E?W===u?((h=vt(v))&&X(h),n=!0):G===u||F===u?((g=pt(v))&&X(g),n=!0):$===u&&((g=pt(v))&&f.let(nt(g)),f.focus(),n=!0):j&&F===u&&((g=pt(v))&&X(g),n=!0);n&&B(e)}function qt(){_t=mt=!1}function Nt(t){var e=et(this),n=e._tags,r=e.state,i=r.n+"__tag--selected",u=!0,a=(t.clipboardData||h.clipboardData).getData("text")+"";try{Z(n,(function(t){if(!E(t,i))throw u=!1,""}))}catch(t){}u&&e.value&&Y(e.value.split(r.join),(function(t){return e.let(t,1)}));var s=a.split(r.join);Y(s,(function(t){return e.set(t,-1,1)})),e.fire("paste",[t,s]).focus().fire("change"),B(t)}function Ot(t){var e=this,n=et(e),r=n._mask,i=n.self,u=n.state,a=r.hint,s=(t.clipboardData||h.clipboardData).getData("text")+"";(function(t,e){var n,r=h.getSelection();r.rangeCount&&((n=r.getRangeAt(0)).deleteContents(),n.insertNode(p.createTextNode(e)))})(0,s),z(a,w(e)?"":i.placeholder),M((function(){if(s=w(e),n.text="",s){var r=s.split(u.join);Y(r,(function(t){return n.set(t,-1,1)})),n.fire("paste",[t,r]).fire("change")}}),1)(),B(t)}function zt(t){var e=this,n=et(e),r=n._active,i=n._mask,u=n._tags,a=n.state,s=i.text,o=a.n+"__tag--selected";if(r){if(X(e),K(e,o),_t);else if(mt){st();var c,l=b(e),v=(y="."+o,(l||p).querySelectorAll(y)),h=v[0],g=v[f(v)-1];if(h!==g)for(;(c=k(h))&&s!==c&&h!==g;)L(h=c,o)}else{st();var _=2===t.button,m=0;Z(u,(function(t){E(t,o)&&++m,e===t||_||D(t,o)})),m>0&&L(e,o)}var y;E(e,o)?(X(e),ot(d(e))):ot(pt(u)),n.fire("touch.tag",[t]),B(t),function(t){t&&t.stopPropagation()}(t)}}function Kt(t){var e=this,n=b(e),r=et(n);P("mousedown",e,Kt),P("touchstart",e,Kt),r.let(nt(n)).focus(),B(t)}function Mt(t){et(this).let().fire("reset",[t])}function Pt(t){var e=et(this),n=e._tags,r=e.state;return n.size<r.min?(e.fire("min.tags").focus(),B(t)):e.fire("submit",[t])}return dt.attach=function(t,n){var i=this;if(t=t||i.self,n&&o(n)&&(n={join:n}),n=l({},i.state,n||{}),E(t,n.n+"__self"))return i;(function(t){return t.required})(t)&&!n.min&&(n.min=1),i._active=!at(t)&&!function(t){return t.readOnly}(t),i._tags=new Map,i._value=rt(t)||null,i.self=t,i.state=n;var u=n.n,c=y(t),v=q("div",{class:u,tabindex:!at(t)&&-1});i.mask=v;var p,h,g=q("span",{class:u+"__tags"}),d=q("span",{class:u+"__text"}),_=q("span",{autocapitalize:"off",contenteditable:!at(t)&&"",spellcheck:"false"}),m=q("span",t.placeholder+"");R(v,g),R(g,d),R(d,_),R(d,m),L(t,u+"__self"),h=v,b(p=t).insertBefore(h,k(p,!0)),c&&(ft(c,i),I("reset",c,Mt),I("submit",c,Pt)),I("blur",_,bt),I("click",v,yt),I("focus",t,St),I("focus",_,Et),I("input",_,Rt),I("invalid",t,At),I("keydown",_,Lt),I("keyup",_,qt),I("paste",_,Ot),t.tabIndex=-1,ft(v,i),ft(_,i);var x={};if(x.hint=m,x.input=_,x.of=t,x.self=v,x.tags=g,x.text=d,i._mask=x,i._value&&Y(i._value.split(n.join),(function(t){return i.set(t,-1,1,1)})),s(n)&&e(n.with))for(var w=0,j=f(n.with);w<j;++w){var S=n.with[w];o(S)&&(S=gt[S]),r(S)?S.call(i,t,n):a(S)&&r(S.attach)&&S.attach.call(i,t,n)}return i},dt.blur=function(){st();var t=this._mask,e=this._tags,n=t.input;return Z(e,(function(t){return t.blur()})),n.blur()},dt.detach=function(){var t=this,n=t._mask,i=t.mask,u=t.self,s=t.state,c=n.input;if(!E(u,s.n+"__self"))return t;var l=y(u);if(t._active=!1,t._value=rt(u)||null,l&&(P("reset",l,Mt),P("submit",l,Pt)),P("blur",c,bt),P("click",i,yt),P("focus",c,Et),P("focus",u,St),P("input",c,Rt),P("invalid",u,At),P("keydown",c,Lt),P("keyup",c,qt),P("paste",c,Ot),e(s.with))for(var v=0,p=f(s.with);v<p;++v){var h=s.with[v];o(h)&&(h=gt[h]),a(h)&&r(h.detach)&&h.detach.call(t,u,s)}return u.tabIndex=null,D(u,s.n+"__self"),T(i),t._mask={of:u},t.mask=null,t},dt.focus=function(t){var e=this._mask.input;return e&&(X(e),ot(e,t)),this},dt.get=function(t){var e=this,n=e._active,r=e._tags;if(!n)return!1;if(e.fire("get.tag",[t]),!ut(t,r))return null;var i=-1;try{Z(r,(function(e,n){if(++i,t===n)throw""}))}catch(t){}return i},dt.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&&Y(r.value.split(o.join),(function(t){return r.let(t,1)})),Y(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(!ut(t,u))return r.fire("not.tag",[t]);r.fire("is.tag",[t]);var f=it(t,u);g(f,0);var c,l=g(f,1);return P("blur",f,kt),P("contextmenu",f,xt),P("copy",f,wt),P("cut",f,jt),P("focus",f,Dt),P("keydown",f,Tt),P("keyup",f,Ct),P("mousedown",f,zt),P("mousedown",l,Kt),P("paste",f,Nt),P("touchstart",f,zt),P("touchstart",l,Kt),T(f),c=t,r._tags.delete(c),s.value=lt(r._tags).join(o.join),r.fire("let.tag",[t]),e||r.fire("change",[t]),r},dt.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,d=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)&&!H(g).test(t))return a.fire("not.tag",[t]);if(ut(t,l))return a.fire("has.tag",[t]);a.fire("is.tag",[t]);var _=q("span",{class:d+"__tag","data-name":t,tabindex:!!s&&-1}),m=q("span",t.replace(/&/g,"&").replace(/>/g,">").replace(/</g,"<")),k=q("span",{class:d+"__x",tabindex:-1});if(s&&(I("blur",_,kt),I("contextmenu",_,xt),I("copy",_,wt),I("cut",_,jt),I("focus",_,Dt),I("keydown",_,Tt),I("keyup",_,Ct),I("mousedown",_,zt),I("mousedown",k,Kt),I("paste",_,Nt),I("touchstart",_,zt),I("touchstart",k,Kt),ft(_,a)),R(_,m),R(_,k),function(t){return u(t)&&0==t%1}(e)&&e>=0){var b=lt(l);b.splice(e,0,t),a._tags=new Map,ct(t,_,l),Y(b,(function(t){var e;ct(t,e=it(t,l),a._tags),O(h,e)}))}else O(h,_),ct(t,_,a._tags);return v.value=lt(a._tags).join(p.join),a.fire("set.tag",[t]),n||a.fire("change",[t]),a},gt}));