selecton
Version:
Selecton.js combines a searchbar and a dropdown menu with nested child lists.
1 lines • 27.7 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.Selecton=e()}(this,function(){"use strict";var r="http://www.w3.org/1999/xhtml",o={svg:"http://www.w3.org/2000/svg",xhtml:r,xlink:"http://www.w3.org/1999/xlink",xml:"http://www.w3.org/XML/1998/namespace",xmlns:"http://www.w3.org/2000/xmlns/"};function i(t){var e=t+="",n=e.indexOf(":");return 0<=n&&"xmlns"!==(e=t.slice(0,n))&&(t=t.slice(n+1)),o.hasOwnProperty(e)?{space:o[e],local:t}:t}function l(t){var e=i(t);return(e.local?function(t){return function(){return this.ownerDocument.createElementNS(t.space,t.local)}}:function(n){return function(){var t=this.ownerDocument,e=this.namespaceURI;return e===r&&t.documentElement.namespaceURI===r?t.createElement(n):t.createElementNS(e,n)}})(e)}function e(){}function h(t){return null==t?e:function(){return this.querySelector(t)}}function f(){return[]}var t=function(t){return function(){return this.matches(t)}};if("undefined"!=typeof document){var n=document.documentElement;if(!n.matches){var s=n.webkitMatchesSelector||n.msMatchesSelector||n.mozMatchesSelector||n.oMatchesSelector;t=function(t){return function(){return s.call(this,t)}}}}var a=t;function c(t){return new Array(t.length)}function p(t,e){this.ownerDocument=t.ownerDocument,this.namespaceURI=t.namespaceURI,this._next=null,this._parent=t,this.__data__=e}p.prototype={constructor:p,appendChild:function(t){return this._parent.insertBefore(t,this._next)},insertBefore:function(t,e){return this._parent.insertBefore(t,e)},querySelector:function(t){return this._parent.querySelector(t)},querySelectorAll:function(t){return this._parent.querySelectorAll(t)}};var m="$";function b(t,e,n,r,o,i){for(var l,s=0,c=e.length,u=i.length;s<u;++s)(l=e[s])?(l.__data__=i[s],r[s]=l):n[s]=new p(t,i[s]);for(;s<c;++s)(l=e[s])&&(o[s]=l)}function A(t,e,n,r,o,i,l){var s,c,u,a={},h=e.length,f=i.length,d=new Array(h);for(s=0;s<h;++s)(c=e[s])&&(d[s]=u=m+l.call(c,c.__data__,s,e),u in a?o[s]=c:a[u]=c);for(s=0;s<f;++s)(c=a[u=m+l.call(t,i[s],s,i)])?((r[s]=c).__data__=i[s],a[u]=null):n[s]=new p(t,i[s]);for(s=0;s<h;++s)(c=e[s])&&a[d[s]]===c&&(o[s]=c)}function d(t,e){return t<e?-1:e<t?1:e<=t?0:NaN}function u(t){return t.ownerDocument&&t.ownerDocument.defaultView||t.document&&t||t.defaultView}function v(t){return t.trim().split(/^|\s+/)}function y(t){return t.classList||new g(t)}function g(t){this._node=t,this._names=v(t.getAttribute("class")||"")}function _(t,e){for(var n=y(t),r=-1,o=e.length;++r<o;)n.add(e[r])}function w(t,e){for(var n=y(t),r=-1,o=e.length;++r<o;)n.remove(e[r])}function x(){this.textContent=""}function k(){this.innerHTML=""}function E(){this.nextSibling&&this.parentNode.appendChild(this)}function B(){this.previousSibling&&this.parentNode.insertBefore(this,this.parentNode.firstChild)}function S(){return null}function N(){var t=this.parentNode;t&&t.removeChild(this)}function C(){return this.parentNode.insertBefore(this.cloneNode(!1),this.nextSibling)}function T(){return this.parentNode.insertBefore(this.cloneNode(!0),this.nextSibling)}g.prototype={add:function(t){this._names.indexOf(t)<0&&(this._names.push(t),this._node.setAttribute("class",this._names.join(" ")))},remove:function(t){var e=this._names.indexOf(t);0<=e&&(this._names.splice(e,1),this._node.setAttribute("class",this._names.join(" ")))},contains:function(t){return 0<=this._names.indexOf(t)}};var H={},U=null;"undefined"!=typeof document&&("onmouseenter"in document.documentElement||(H={mouseenter:"mouseover",mouseleave:"mouseout"}));function I(n,t,e){return n=P(n,t,e),function(t){var e=t.relatedTarget;e&&(e===this||8&e.compareDocumentPosition(this))||n.call(this,t)}}function P(n,r,o){return function(t){var e=U;U=t;try{n.call(this,this.__data__,r,o)}finally{U=e}}}function R(i){return function(){var t=this.__on;if(t){for(var e,n=0,r=-1,o=t.length;n<o;++n)e=t[n],i.type&&e.type!==i.type||e.name!==i.name?t[++r]=e:this.removeEventListener(e.type,e.listener,e.capture);++r?t.length=r:delete this.__on}}}function M(c,u,a){var h=H.hasOwnProperty(c.type)?I:P;return function(t,e,n){var r,o=this.__on,i=h(u,e,n);if(o)for(var l=0,s=o.length;l<s;++l)if((r=o[l]).type===c.type&&r.name===c.name)return this.removeEventListener(r.type,r.listener,r.capture),this.addEventListener(r.type,r.listener=i,r.capture=a),void(r.value=u);this.addEventListener(c.type,i,a),r={type:c.type,name:c.name,value:u,listener:i,capture:a},o?o.push(r):this.__on=[r]}}function D(t,e,n,r){var o=U;t.sourceEvent=U,U=t;try{return e.apply(n,r)}finally{U=o}}function L(t,e,n){var r=u(t),o=r.CustomEvent;"function"==typeof o?o=new o(e,n):(o=r.document.createEvent("Event"),n?(o.initEvent(e,n.bubbles,n.cancelable),o.detail=n.detail):o.initEvent(e,!1,!1)),t.dispatchEvent(o)}var O=[null];function j(t,e){this._groups=t,this._parents=e}function X(t){return"string"==typeof t?new j([[document.querySelector(t)]],[document.documentElement]):new j([[t]],O)}function q(){for(var t,e=U;t=e.sourceEvent;)e=t;return e}function z(t,e){var n=t.ownerSVGElement||t;if(n.createSVGPoint){var r=n.createSVGPoint();return r.x=e.clientX,r.y=e.clientY,[(r=r.matrixTransform(t.getScreenCTM().inverse())).x,r.y]}var o=t.getBoundingClientRect();return[e.clientX-o.left-t.clientLeft,e.clientY-o.top-t.clientTop]}function V(t){var e=q();return e.changedTouches&&(e=e.changedTouches[0]),z(t,e)}function Y(t,e,n){arguments.length<3&&(n=e,e=q().changedTouches);for(var r,o=0,i=e?e.length:0;o<i;++o)if((r=e[o]).identifier===n)return z(t,r);return null}j.prototype=function(){return new j([[document.documentElement]],O)}.prototype={constructor:j,select:function(t){"function"!=typeof t&&(t=h(t));for(var e=this._groups,n=e.length,r=new Array(n),o=0;o<n;++o)for(var i,l,s=e[o],c=s.length,u=r[o]=new Array(c),a=0;a<c;++a)(i=s[a])&&(l=t.call(i,i.__data__,a,s))&&("__data__"in i&&(l.__data__=i.__data__),u[a]=l);return new j(r,this._parents)},selectAll:function(t){var e;"function"!=typeof t&&(t=null==(e=t)?f:function(){return this.querySelectorAll(e)});for(var n=this._groups,r=n.length,o=[],i=[],l=0;l<r;++l)for(var s,c=n[l],u=c.length,a=0;a<u;++a)(s=c[a])&&(o.push(t.call(s,s.__data__,a,c)),i.push(s));return new j(o,i)},filter:function(t){"function"!=typeof t&&(t=a(t));for(var e=this._groups,n=e.length,r=new Array(n),o=0;o<n;++o)for(var i,l=e[o],s=l.length,c=r[o]=[],u=0;u<s;++u)(i=l[u])&&t.call(i,i.__data__,u,l)&&c.push(i);return new j(r,this._parents)},data:function(t,e){if(!t)return p=new Array(this.size()),a=-1,this.each(function(t){p[++a]=t}),p;var n,r=e?A:b,o=this._parents,i=this._groups;"function"!=typeof t&&(n=t,t=function(){return n});for(var l=i.length,s=new Array(l),c=new Array(l),u=new Array(l),a=0;a<l;++a){var h=o[a],f=i[a],d=f.length,p=t.call(h,h&&h.__data__,a,o),m=p.length,v=c[a]=new Array(m),y=s[a]=new Array(m);r(h,f,v,y,u[a]=new Array(d),p,e);for(var g,_,w=0,x=0;w<m;++w)if(g=v[w]){for(x<=w&&(x=w+1);!(_=y[x])&&++x<m;);g._next=_||null}}return(s=new j(s,o))._enter=c,s._exit=u,s},enter:function(){return new j(this._enter||this._groups.map(c),this._parents)},exit:function(){return new j(this._exit||this._groups.map(c),this._parents)},merge:function(t){for(var e=this._groups,n=t._groups,r=e.length,o=n.length,i=Math.min(r,o),l=new Array(r),s=0;s<i;++s)for(var c,u=e[s],a=n[s],h=u.length,f=l[s]=new Array(h),d=0;d<h;++d)(c=u[d]||a[d])&&(f[d]=c);for(;s<r;++s)l[s]=e[s];return new j(l,this._parents)},order:function(){for(var t=this._groups,e=-1,n=t.length;++e<n;)for(var r,o=t[e],i=o.length-1,l=o[i];0<=--i;)(r=o[i])&&(l&&l!==r.nextSibling&&l.parentNode.insertBefore(r,l),l=r);return this},sort:function(n){function t(t,e){return t&&e?n(t.__data__,e.__data__):!t-!e}n||(n=d);for(var e=this._groups,r=e.length,o=new Array(r),i=0;i<r;++i){for(var l,s=e[i],c=s.length,u=o[i]=new Array(c),a=0;a<c;++a)(l=s[a])&&(u[a]=l);u.sort(t)}return new j(o,this._parents).order()},call:function(){var t=arguments[0];return arguments[0]=this,t.apply(null,arguments),this},nodes:function(){var t=new Array(this.size()),e=-1;return this.each(function(){t[++e]=this}),t},node:function(){for(var t=this._groups,e=0,n=t.length;e<n;++e)for(var r=t[e],o=0,i=r.length;o<i;++o){var l=r[o];if(l)return l}return null},size:function(){var t=0;return this.each(function(){++t}),t},empty:function(){return!this.node()},each:function(t){for(var e=this._groups,n=0,r=e.length;n<r;++n)for(var o,i=e[n],l=0,s=i.length;l<s;++l)(o=i[l])&&t.call(o,o.__data__,l,i);return this},attr:function(t,e){var n=i(t);if(arguments.length<2){var r=this.node();return n.local?r.getAttributeNS(n.space,n.local):r.getAttribute(n)}return this.each((null==e?n.local?function(t){return function(){this.removeAttributeNS(t.space,t.local)}}:function(t){return function(){this.removeAttribute(t)}}:"function"==typeof e?n.local?function(e,n){return function(){var t=n.apply(this,arguments);null==t?this.removeAttributeNS(e.space,e.local):this.setAttributeNS(e.space,e.local,t)}}:function(e,n){return function(){var t=n.apply(this,arguments);null==t?this.removeAttribute(e):this.setAttribute(e,t)}}:n.local?function(t,e){return function(){this.setAttributeNS(t.space,t.local,e)}}:function(t,e){return function(){this.setAttribute(t,e)}})(n,e))},style:function(t,e,n){return 1<arguments.length?this.each((null==e?function(t){return function(){this.style.removeProperty(t)}}:"function"==typeof e?function(e,n,r){return function(){var t=n.apply(this,arguments);null==t?this.style.removeProperty(e):this.style.setProperty(e,t,r)}}:function(t,e,n){return function(){this.style.setProperty(t,e,n)}})(t,e,null==n?"":n)):(r=this.node(),o=t,r.style.getPropertyValue(o)||u(r).getComputedStyle(r,null).getPropertyValue(o));var r,o},property:function(t,e){return 1<arguments.length?this.each((null==e?function(t){return function(){delete this[t]}}:"function"==typeof e?function(e,n){return function(){var t=n.apply(this,arguments);null==t?delete this[e]:this[e]=t}}:function(t,e){return function(){this[t]=e}})(t,e)):this.node()[t]},classed:function(t,e){var n=v(t+"");if(arguments.length<2){for(var r=y(this.node()),o=-1,i=n.length;++o<i;)if(!r.contains(n[o]))return!1;return!0}return this.each(("function"==typeof e?function(t,e){return function(){(e.apply(this,arguments)?_:w)(this,t)}}:e?function(t){return function(){_(this,t)}}:function(t){return function(){w(this,t)}})(n,e))},text:function(t){return arguments.length?this.each(null==t?x:("function"==typeof t?function(e){return function(){var t=e.apply(this,arguments);this.textContent=null==t?"":t}}:function(t){return function(){this.textContent=t}})(t)):this.node().textContent},html:function(t){return arguments.length?this.each(null==t?k:("function"==typeof t?function(e){return function(){var t=e.apply(this,arguments);this.innerHTML=null==t?"":t}}:function(t){return function(){this.innerHTML=t}})(t)):this.node().innerHTML},raise:function(){return this.each(E)},lower:function(){return this.each(B)},append:function(t){var e="function"==typeof t?t:l(t);return this.select(function(){return this.appendChild(e.apply(this,arguments))})},insert:function(t,e){var n="function"==typeof t?t:l(t),r=null==e?S:"function"==typeof e?e:h(e);return this.select(function(){return this.insertBefore(n.apply(this,arguments),r.apply(this,arguments)||null)})},remove:function(){return this.each(N)},clone:function(t){return this.select(t?T:C)},datum:function(t){return arguments.length?this.property("__data__",t):this.node().__data__},on:function(t,e,n){var r,o,i=(t+"").trim().split(/^|\s+/).map(function(t){var e="",n=t.indexOf(".");return 0<=n&&(e=t.slice(n+1),t=t.slice(0,n)),{type:t,name:e}}),l=i.length;if(!(arguments.length<2)){for(s=e?M:R,null==n&&(n=!1),r=0;r<l;++r)this.each(s(i[r],e,n));return this}var s=this.node().__on;if(s)for(var c,u=0,a=s.length;u<a;++u)for(r=0,c=s[u];r<l;++r)if((o=i[r]).type===c.type&&o.name===c.name)return c.value},dispatch:function(t,e){return this.each(("function"==typeof e?function(t,e){return function(){return L(this,t,e.apply(this,arguments))}}:function(t,e){return function(){return L(this,t,e)}})(t,e))}};var G={value:function(){}};function W(){for(var t,e=0,n=arguments.length,r={};e<n;++e){if(!(t=arguments[e]+"")||t in r)throw new Error("illegal type: "+t);r[t]=[]}return new $(r)}function $(t){this._=t}function F(t,e){for(var n,r=0,o=t.length;r<o;++r)if((n=t[r]).name===e)return n.value}function J(t,e,n){for(var r=0,o=t.length;r<o;++r)if(t[r].name===e){t[r]=G,t=t.slice(0,r).concat(t.slice(r+1));break}return null!=n&&t.push({name:e,value:n}),t}function K(){U.stopImmediatePropagation()}function Q(){U.preventDefault(),U.stopImmediatePropagation()}function Z(t){return function(){return t}}function tt(t,e,n,r,o,i,l,s,c,u){this.target=t,this.type=e,this.subject=n,this.identifier=r,this.active=o,this.x=i,this.y=l,this.dx=s,this.dy=c,this._=u}function et(){return!U.button}function nt(){return this.parentNode}function rt(t){return null==t?{x:U.x,y:U.y}:t}function ot(){return"ontouchstart"in this}function it(){var o,i,l,s,c=et,u=nt,e=rt,n=ot,p={},r=W("start","drag","end"),m=0,a=0;function v(t){t.on("mousedown.drag",h).filter(n).on("touchstart.drag",y).on("touchmove.drag",g).on("touchend.drag touchcancel.drag",_).style("touch-action","none").style("-webkit-tap-highlight-color","rgba(0,0,0,0)")}function h(){if(!s&&c.apply(this,arguments)){var t,e,n,r=w("mouse",u.apply(this,arguments),V,this,arguments);if(r)X(U.view).on("mousemove.drag",f,!0).on("mouseup.drag",d,!0),t=U.view,e=t.document.documentElement,n=X(t).on("dragstart.drag",Q,!0),"onselectstart"in e?n.on("selectstart.drag",Q,!0):(e.__noselect=e.style.MozUserSelect,e.style.MozUserSelect="none"),K(),l=!1,o=U.clientX,i=U.clientY,r("start")}}function f(){if(Q(),!l){var t=U.clientX-o,e=U.clientY-i;l=a<t*t+e*e}p.mouse("drag")}function d(){var t,e,n,r;X(U.view).on("mousemove.drag mouseup.drag",null),t=U.view,e=l,n=t.document.documentElement,r=X(t).on("dragstart.drag",null),e&&(r.on("click.drag",Q,!0),setTimeout(function(){r.on("click.drag",null)},0)),"onselectstart"in n?r.on("selectstart.drag",null):(n.style.MozUserSelect=n.__noselect,delete n.__noselect),Q(),p.mouse("end")}function y(){if(c.apply(this,arguments)){var t,e,n=U.changedTouches,r=u.apply(this,arguments),o=n.length;for(t=0;t<o;++t)(e=w(n[t].identifier,r,Y,this,arguments))&&(K(),e("start"))}}function g(){var t,e,n=U.changedTouches,r=n.length;for(t=0;t<r;++t)(e=p[n[t].identifier])&&(Q(),e("drag"))}function _(){var t,e,n=U.changedTouches,r=n.length;for(s&&clearTimeout(s),s=setTimeout(function(){s=null},500),t=0;t<r;++t)(e=p[n[t].identifier])&&(K(),e("end"))}function w(o,i,l,s,c){var u,a,h,f=l(i,o),d=r.copy();if(D(new tt(v,"beforestart",u,o,m,f[0],f[1],0,0,d),function(){return null!=(U.subject=u=e.apply(s,c))&&(a=u.x-f[0]||0,h=u.y-f[1]||0,!0)}))return function t(e){var n,r=f;switch(e){case"start":p[o]=t,n=m++;break;case"end":delete p[o],--m;case"drag":f=l(i,o),n=m}D(new tt(v,e,u,o,n,f[0]+a,f[1]+h,f[0]-r[0],f[1]-r[1],d),d.apply,d,[e,s,c])}}return v.filter=function(t){return arguments.length?(c="function"==typeof t?t:Z(!!t),v):c},v.container=function(t){return arguments.length?(u="function"==typeof t?t:Z(t),v):u},v.subject=function(t){return arguments.length?(e="function"==typeof t?t:Z(t),v):e},v.touchable=function(t){return arguments.length?(n="function"==typeof t?t:Z(!!t),v):n},v.on=function(){var t=r.on.apply(r,arguments);return t===r?v:t},v.clickDistance=function(t){return arguments.length?(a=(t=+t)*t,v):Math.sqrt(a)},v}$.prototype=W.prototype={constructor:$,on:function(t,e){var n,r,o=this._,i=(r=o,(t+"").trim().split(/^|\s+/).map(function(t){var e="",n=t.indexOf(".");if(0<=n&&(e=t.slice(n+1),t=t.slice(0,n)),t&&!r.hasOwnProperty(t))throw new Error("unknown type: "+t);return{type:t,name:e}})),l=-1,s=i.length;if(!(arguments.length<2)){if(null!=e&&"function"!=typeof e)throw new Error("invalid callback: "+e);for(;++l<s;)if(n=(t=i[l]).type)o[n]=J(o[n],t.name,e);else if(null==e)for(n in o)o[n]=J(o[n],t.name,null);return this}for(;++l<s;)if((n=(t=i[l]).type)&&(n=F(o[n],t.name)))return n},copy:function(){var t={},e=this._;for(var n in e)t[n]=e[n].slice();return new $(t)},call:function(t,e){if(0<(n=arguments.length-2))for(var n,r,o=new Array(n),i=0;i<n;++i)o[i]=arguments[i+2];if(!this._.hasOwnProperty(t))throw new Error("unknown type: "+t);for(i=0,n=(r=this._[t]).length;i<n;++i)r[i].value.apply(e,o)},apply:function(t,e,n){if(!this._.hasOwnProperty(t))throw new Error("unknown type: "+t);for(var r=this._[t],o=0,i=r.length;o<i;++o)r[o].value.apply(e,n)}},tt.prototype.on=function(){var t=this._.on.apply(this._,arguments);return t===this._?this:t};var lt=null;function st(t,e){return lt=[],function t(e,n,r){if("object"==typeof e)for(var o=0;o<e.length;o++)e[o]._parent_=n.key,e[o][r[0]]===r[1]&<.push(e[o]),e[o].children&&t(e[o].children,e[o],r)}(t,{key:"root"},e),lt}function ct(t){return(e=t,e.select(".selecton-input-group")).select(".selecton-search");var e}function ut(t){return ct(t).select(".selecton-search-input")}function at(t){return ct(t).selectAll(".input-item")}function ht(t){return t.select(".selecton-dropdown")}function ft(t){return ht(t).selectAll("ul.list-wrapper")}function dt(t){return ft(t).selectAll("li.dropdown-list-item")}function pt(t,e,n){!function t(e,n,r){if("root"!==n){var o=dt(e).filter(function(t){return r(t)===n}).data()[0];o.closed=!1,o.children.forEach(function(t){t.closed=!1}),t(e,o._parent_,r)}}(t,e,n)}function mt(t,e,n){var r=t.select("div.selecton-search").insert("div","input.selecton-search-input").style("background-color","color"in e?e.color:null).attr("class","input-item");r.append("span").attr("class","input-item-content").html(n.render(e)),n.showHierarchyButton&&r.append("span").attr("class","input-item-expand-hierachy-button").html(" "),n.showRemoveButton&&r.append("span").attr("class","input-item-remove-button").html(" "),r.datum(e)}function vt(t,e,n){at(t).nodes().forEach(function(t){if(n.render(e)===X(t).select("span.input-item-content").html())return e.selected=!1,void X(t).remove()})}function yt(t){return'<span class="dropdown-list-item-highlighted">'+t+"</span>"}function gt(t,e,n){return function t(e,n,r){if("object"==typeof e)for(var o=0;o<e.length;o++)e[o][n]=r,e[o].children&&t(e[o].children)}(t,e,n)}return function(t){var s=0,c=null,e=X(t.el),n={},u=null,a=0,h=null,r=null,f=0,o=null,i=" ",d=null,p=null,m="",v=null,y=null,l="<div><span></span><span></span><span></span><span></span></div>",g=null,_=null,w=null,x=40,b=13,A=27,k=9,E=38,B={key:function(t){return t.key},open:!1,mode:"select",renderInputItem:function(t){return t.key},renderDropdownItem:function(t){return t.key},showHierarchyButton:!0,showRemoveButton:!0};function S(t){if("select"===r){var e=c.selectAll("ul.list-wrapper").data(t,h),n=e.enter().append("ul").attr("class","list-wrapper").classed("root-list",!0).merge(e);e.exit().remove(),function t(e){var n=e.selectAll("li.dropdown-list-item").data(function(t){return[t]},h),r=n.enter().append("li").attr("class","dropdown-list-item").merge(n).each(function(t){t.closed=!("closed"in t)||t.closed,t.selectable=!("selectable"in t)||t.selectable,t.selected="selected"in t&&t.selected,t._temporarilyHighlighted_="_temporarilyHighlighted_"in t&&t._temporarilyHighlighted_,t._depth_=s}).classed("temporarily-highlighted",function(t){return t._temporarilyHighlighted_}).classed("dropdown-list-item-selectable",function(t){return t.selectable}).classed("dropdown-list-item-selected",function(t){return t.selected}).classed("dropdown-list-item-hidden",function(t){return function(t,e,n){if(e&&t.selectable){var r=n(t),o=new RegExp(e,"gi");return r.match(o)?t._search_=r.replace(o,yt):delete t._search_,-1===r.toLowerCase().indexOf(e)||t.selected}return!e&&(delete t._search_,t.closed)}(t,m,p)}).html(function(t){return function(t,e,n){var r="",o=X(document.createElement("span"));if(o.attr("class","dropdown-list-item-content").html(t._search_?t._search_:n(t)),r+=o.node().outerHTML,t.children&&0<t.children.length&&!e){var i=t.children.every(function(t){return!0===t.closed||void 0===t.closed}),l=X(document.createElement("span"));l.attr("class","dropdown-list-item-expand-toggle").classed("dropdown-list-item-expand-toggle-closed",i).classed("dropdown-list-item-expand-toggle-open",!i).html(" "),r+=l.node().outerHTML}return r}(t,m,p)});n.exit().remove();var o=n.merge(r).selectAll("li.dropdown-list-item").data(function(t){return t.children?t.children:[]},h);if(!o.enter().empty()){var i=e.selectAll("ul.list-wrapper").data(function(t){return t.children?t.children:[]},h),l=i.enter().append("ul").attr("class","list-wrapper").merge(i);s++,t(l)}o.exit().remove()}(n)}c.selectAll(".dropdown-list-item.dropdown-list-item-selectable span.dropdown-list-item-content").on("click",function(){U.stopPropagation();var t=this.parentNode.__data__;q(t),t.selected=!t.selected,function(t){if(H(""),t)t.__data__.selected?mt(g,t.__data__,{showRemoveButton:y,showHierarchyButton:v,render:d}):vt(g,t.__data__,{render:d})}(this.parentNode),D(),z("change",Y())}),c.selectAll("span.dropdown-list-item-expand-toggle").on("click",function(){U.stopPropagation(),this.parentNode.__data__.children.forEach(function(t){!t.closed&>(t.children,"closed",!0),t.closed=!t.closed}),D()}),u.select("div.selecton-search").selectAll("span.input-item-expand-hierachy-button").on("click",function(){L();var t,e=function(t,e){var n=X(this.parentNode).data()[0];return U.stopPropagation(),pt(t,n._parent_,e),n._temporarilyHighlighted_=!0,n}.call(this,g,h),n=ht(g),r=null;H(""),I(),D(),t=parseInt(n.style("height")),r=dt(g).filter(function(t){return h(t)===h(e)}),n.node().scrollTop=r.node().offsetTop-t/2,setTimeout(function(){e._temporarilyHighlighted_=!1,D()},750)}),u.select("div.selecton-search").selectAll("span.input-item-remove-button").on("click",function(){U.stopPropagation(),vt(g,X(this.parentNode).data()[0],{render:d}),H(""),D(),z("change",Y())}),g.select("div.selecton-search").selectAll("div.input-item span.input-item-content").call(it().on("start",N).on("drag",C).on("end",T))}function N(){H("");var t=function(t){var e=ut(t),n=X(this.parentNode).classed("drag-active",!0).node().cloneNode(!0),r=U.sourceEvent.pageX,o=U.sourceEvent.pageY,i=this.getBoundingClientRect(),l=r-i.left,s=o-i.top-(window.scrollY||window.pageYOffset);return X(n).attr("class","input-item-clone").style("transform-origin",l+"px "+s+"px").style("left",r-l+"px").style("top",o-s+"px"),document.body.appendChild(n),e.remove(),{x:l,y:s}}.call(this,g);_=t.x,w=t.y}function C(){(function(t,e,n){var r=ct(t).node(),o=this.parentNode,i=U.sourceEvent.pageY,l=U.sourceEvent.pageX,s=X("div.input-item-clone").node(),c=s.getBoundingClientRect().top,u=s.clientWidth,a=at(t),h=a.filter(function(){var t=this.clientHeight,e=this.getBoundingClientRect().top;return e-t/2<c&&c<e+t/2&&l<this.getBoundingClientRect().right-u/2}).nodes()[0],f=a.filter(function(){var t=this.clientHeight,e=this.getBoundingClientRect().top;return e-t/2<c&&c<e+t/2&&l>this.getBoundingClientRect().left+u/2}).nodes()[0];h?r.insertBefore(o,h):f&&void 0===h&&r.append(o),X("div.input-item-clone").style("left",l-e+"px").style("top",i-n+"px")}).call(this,g,_,w)}function T(){var t=function(t,e,n){return X("div.input-item-clone").remove(),X(this.parentNode).classed("drag-active",!1),ct(t).append("input").attr("class","selecton-search-input").on("keyup",e).on("keydown",n),{x:0,y:0}}.call(this,g,R,M);_=t.x,w=t.y,z("change",Y())}function H(t){var e=ut(g);e.style("width",null),e.node().value=m=t}function I(){ut(g).node().focus(),g.classed("selecton-is-open",!0),c.classed("open",!0),c.classed("closed",!1)}function P(){f=0;var t=c.classed("open");g.classed("selecton-is-open",!t),c.classed("open",!t).classed("closed",t),L(),H(""),D()}function R(){"add"!==r?"select"===r&&function(){var t=ut(g),e=t.node(),n=u.select("div.selecton-ghost"),r=U.keyCode||U.which,o=null;if(g.classed("selecton-is-open",!0),c.classed("open",!0).classed("closed",!1),m=e.value.toLowerCase(),t.style("width",function(){return n.html(m).node().clientWidth+20+"px"}),r===b||r===k){var i=j();if(f=0,i.empty())return;return q(i.node().__data__),i.node().__data__.selected=!0,mt(g,i.node().__data__,{showRemoveButton:y,showHierarchyButton:v,render:d}),P(),z("change",Y())}D(),r!==E&&r!==x&&r!==A&&r!==k&&r!==b&&z("keyup",m),r===E&&f--,r===x&&f++,f<0&&(f=a+f),a-1<f&&(f=0),(o=c.selectAll("ul.list-wrapper li.dropdown-list-item-selectable:not(.dropdown-list-item-hidden)")).classed("dropdown-list-item-preselected",function(t,e){return e===f}),a=o.size();var l=j().node(),s=parseInt(c.style("height"));if(l&&l.offsetTop+s/2>s)return c.node().scrollTop=l.offsetTop-s/2;c.node().scrollTop=0}():function(){var t=ut(g),e=t.node(),n=u.select("div.selecton-ghost"),r=U.keyCode||U.which;if(m=e.value){if(t.style("width",function(){return n.html(m).node().clientWidth+20+"px"}),r===b||r===k){var o=m.split(",");return o.forEach(function(t){t&&mt(g,{selected:!0,key:t.trim()},{showRemoveButton:y,showHierarchyButton:v,render:d})}),z("change",Y()),H(""),D()}r!==E&&r!==x&&r!==A&&r!==k&&r!==b&&z("keyup",m)}}()}function M(){var t=U.keyCode||U.which;(t===k||t===x||t===E)&&U.preventDefault()}function D(){s=0,S(V())}function L(){ft(g).selectAll("li.dropdown-list-item-selectable").each(function(t){t.closed=!0})}function O(){return this===U.target}function j(){return c.selectAll("ul.list-wrapper li.dropdown-list-item-selectable:not(.dropdown-list-item-hidden)").filter(function(){return X(this).classed("dropdown-list-item-preselected")})}function q(e){var t=dt(g).filter(function(t){return h(t)===e._parent_}).data()[0];!1===t.multiselect&&t.children.forEach(function(t){!0===t.selected&&vt(g,t,{render:d}),t.selected=!1})}function z(t,e){n[t]&&n[t].forEach(function(t){t(e)})}function V(){return c.selectAll("div.selecton-dropdown > ul.root-list").data()}function Y(){return at(g).data()}return h="key"in t?t.key:B.key,r="mode"in t?t.mode:B.mode,o="open"in t?t.open:B.open,p="renderDropdownItem"in t?t.renderDropdownItem:B.renderDropdownItem,d="renderInputItem"in t?t.renderInputItem:B.renderInputItem,v="showHierarchyButton"in t?t.showHierarchyButton:B.showHierarchyButton,y="showRemoveButton"in t?t.showRemoveButton:B.showHierarchyButton,e.html(null),g=e.append("div").attr("class","selecton selecton-"+r).classed("selecton-is-open",o),(u=g.append("div").attr("class","selecton-input-group")).append("div").attr("class","selecton-input-group-pre").html(i).on("click",I),u.append("div").attr("class","selecton-search").on("click",I).append("input").attr("class","selecton-search-input").on("keyup",R).on("keydown",M),u.append("div").attr("class","selecton-ghost").attr("contenteditable","true").style("position","absolute").style("top","0").style("left","0").style("visibility","hidden").style("width","auto"),"select"===r&&u.append("div").html(l).attr("class","selecton-input-group-suf").on("click",P),c=g.append("div").attr("class","selecton-dropdown").classed("open",o).classed("closed",!o),X(document).on("click touchstart",function(){g.selectAll("div.selecton, div.selecton *").filter(O).empty()&&g.classed("selecton-is-open")&&(g.classed("selecton-is-open",!1),c.classed("open",!1),c.classed("closed",!0),L(),H(""),D())}),X(document).on("keydown",function(){var t=ut(g);if((U.keyCode||U.which)===A)return f=0,t.node().blur(),g.classed("selecton-is-open",!1),c.classed("open",!1),c.classed("closed",!0),H(""),L(),void D()}),{getSelectedItems:Y,getItemsByValue:st,update:function(t,e){at(g).remove();var r,o,n=st(t,["selected",!0]);e&&(r=n,o=[],e.forEach(function(e){var n=!1;r=r.filter(function(t){return!(!n&&t.key===e&&(delete t._temporarilyHighlighted_,o.push(t),n=!0))})}),n=o),n.forEach(function(t){mt(g,t,{showRemoveButton:y,showHierarchyButton:v,render:d})}),S(t)},focus:I,on:function(t,e){n[t]=n[t]||[],n[t].push(e)},getData:V}}});