This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
knockout-select.min.js
1 lines (1 loc) · 12.6 KB
/
knockout-select.min.js
1
define(["require","exports","knockout"],function(a,b,c){var d;!function(a){var b="knockout-select",d=b,e=b+"-button",f=b+"-container",g=b+"-arrow",h=b+"-row",i=b+"-row-selected",j=b+"-row-active",k=9999,l="Choose...",m=" items selected",n=function(){function a(a,b,d){this.captionElement=void 0,this.containerDiv=document.createElement("div"),this.captionText=void 0,this.element=d,this.options=a()||b().options,this.selectedOptions=b().selectedOptions,this.value=b().cap3Value||b().value,this.enable=b().enable,this.disable=b().disable,this.optionsCaption=b().optionsCaption,this.optionsText=b().optionsText,this.optionsValue=b().optionsValue,this.multiple=d.hasAttribute("multiple"),this.size=parseInt(d.getAttribute("size")),this.hoverIndex=c.observable(-1),this.maxIndex=-1,this.inside=!1,this.showBox=!1,this.subscriptions=[]}return a.prototype.getOptionsText=function(a){return a&&this.optionsText&&"string"==typeof this.optionsText?c.unwrap(c.unwrap(a)[this.optionsText]):this.optionsText&&"function"==typeof this.optionsText?this.optionsText(c.unwrap(c.unwrap(a))):c.unwrap(a)},a.prototype.getOptionsValue=function(a){return this.optionsValue&&"string"==typeof this.optionsValue?c.unwrap(c.unwrap(a)[this.optionsValue]):this.optionsValue&&"function"==typeof this.optionsValue?this.optionsValue(c.unwrap(c.unwrap(a))):c.unwrap(a)},a.prototype.createCaptionsElement=function(){this.captionElement=document.createElement("button"),this.captionText=document.createElement("span"),this.captionElement.appendChild(this.captionText);var a=document.createElement("span");a.classList.add(g),this.captionElement.appendChild(a),this.captionText.textContent=c.unwrap(this.optionsCaption),this.captionElement.setAttribute("data-open",(!1).toString()),this.element.getAttribute("tabindex")&&(this.captionElement.setAttribute("tabindex",this.element.getAttribute("tabindex")),this.element.removeAttribute("tabindex"))},a.prototype.createOptionDiv=function(a){var b=this,c=document.createElement("div");c.textContent=b.getOptionsText(a),c.setAttribute("data-selected",(!1).toString()),c.classList.add(h),c.onmouseover=b.onRowHover(),c.onclick=b.onItemSelect();var d=-1,e=new o(-1,-1);return c.ontouchstart=function(a){return 1===a.touches.length?(e.x=a.touches[0].pageX,e.y=a.touches[0].pageY,d=a.timeStamp||(new Date).getTime(),a.stopPropagation(),b.onRowHover()(a)):(b.hoverIndex(-1),!0)},c.ontouchend=function(a){if(0===a.touches.length&&1===a.changedTouches.length){var c=new o(a.changedTouches[0].pageX,a.changedTouches[0].pageY),f=o.distance(e,c),g=a.timeStamp||(new Date).getTime();if(1e3>g-d&&10>f)return a.stopPropagation(),b.onItemSelect()()}return b.hoverIndex(-1),!0},c},a.prototype.findOption=function(a,b){for(var d=0;d<a.length;d++)if(this.getOptionsValue(a[d])===c.unwrap(b))return a[d];return void 0},a.prototype.styleOptionsDivMultiple=function(a,b){c.unwrap(this.selectedOptions).indexOf(this.getOptionsValue(a))>=0?(b.classList.add(i),b.setAttribute("data-selected",(!0).toString())):(b.classList.remove(i),b.setAttribute("data-selected",(!1).toString()))},a.prototype.styleOptionsDivSingle=function(a,b){var d=this.getOptionsValue(a);d&&d===c.unwrap(this.value)?(b.classList.add(i),b.setAttribute("data-selected",(!0).toString()),this.captionText.textContent=this.getOptionsText(a)):(b.classList.remove(i),b.setAttribute("data-selected",(!1).toString()))},a.prototype.setCaptionMultiple=function(){switch(c.unwrap(this.selectedOptions).length){case 0:this.captionText.textContent=c.unwrap(this.optionsCaption);break;case 1:this.captionText.textContent=this.getOptionsText(this.findOption(c.unwrap(this.options),c.unwrap(this.selectedOptions)[0]));break;default:this.captionText.textContent=c.unwrap(this.selectedOptions).length+m}},a.prototype.selectItemMultiple=function(a,b){var d,e;e=this.getOptionsValue(a),d=c.unwrap(this.selectedOptions).indexOf(e),d>=0?(this.selectedOptions.splice(d,1),b.classList.remove(i),b.setAttribute("data-selected",(!1).toString())):(this.selectedOptions.push(e),b.classList.add(i),b.setAttribute("data-selected",(!0).toString()))},a.prototype.selectItemSingle=function(a,b){if(c.isObservable(this.value))this.value(this.getOptionsValue(a));else{this.value=this.getOptionsValue(a),this.captionText.textContent=this.getOptionsText(a);for(var d=this.hoverIndex(),e=0;e<b.length;e++)e===d?(b[e].classList.add(i),b[e].setAttribute("data-selected",(!0).toString())):(b[e].classList.remove(i),b[e].setAttribute("data-selected",(!1).toString()))}},a.prototype.onRowHover=function(){var a=this;return function(b){var c=b.target||b.srcElement;return a.hoverIndex(Array.prototype.indexOf.call(a.containerDiv.childNodes,c)),b.stopPropagation(),!0}},a.prototype.onUpArrowPressed=function(){var a=this;return function(){a.maxIndex<0||(a.hoverIndex()<=0?a.hoverIndex(a.maxIndex):a.hoverIndex(a.hoverIndex()-1),a.hoverIndex()>=0&&a.containerDiv.childNodes[a.hoverIndex()].scrollIntoView(!1))}},a.prototype.onDownArrowPressed=function(){var a=this;return function(){a.maxIndex<0||(a.showBox||(a.onSelectPressed()(void 0),a.hoverIndex(-1)),a.hoverIndex()>=a.maxIndex?a.hoverIndex(0):a.hoverIndex(a.hoverIndex()+1),a.hoverIndex()>=0&&a.containerDiv.childNodes[a.hoverIndex()].scrollIntoView(!1))}},a.prototype.onSelectPressed=function(){var a=this;return function(b){6!==b.mozInputSource&&(a.showBox?(a.showBox=!1,a.inside=!1,a.captionElement.setAttribute("data-open",(!1).toString()),a.containerDiv.style.display="none"):(a.captionElement.focus(),a.captionElement.setAttribute("data-open",(!0).toString()),a.showBox=!0,a.applyContainerStyle(a.containerDiv,a.captionElement),a.containerDiv.style.display="block",a.size=parseInt(a.element.getAttribute("size")),a.size?a.containerDiv.style.height=a.containerDiv.firstChild.getBoundingClientRect().height*a.size+"px":a.containerDiv.style.height="auto"))}},a.prototype.onMouseOut=function(){var a=this;return function(b){var c=b.toElement||b.relatedTarget;c.parentNode!==a.containerDiv&&c!==a.containerDiv&&(a.inside=!1)}},a.prototype.onMouseEnter=function(){var a=this;return function(){a.inside=!0}},a.prototype.onHoverChanged=function(){var a=this;return function(b){a.captionElement.focus();for(var c=a.containerDiv.childNodes,d=0;d<c.length;d++)d===b?c[d].classList.add(j):c[d].classList.remove(j)}},a.prototype.onHideOptions=function(){var a=this;return function(){a.inside?a.captionElement.focus():(a.showBox=!1,a.containerDiv.style.display="none",a.captionElement.setAttribute("data-open",(!1).toString()))}},a.prototype.onItemSelect=function(){var a=this;return function(){var b=a.hoverIndex();if(0>b||!a.showBox)return!1;var d=c.unwrap(c.unwrap(a.options)[b]),e=a.containerDiv.childNodes,f=e[b];return a.multiple?(a.selectItemMultiple(d,f),a.setCaptionMultiple(),a.captionElement.focus()):(a.selectItemSingle(d,e),a.inside=!1,a.onHideOptions()()),!1}},a.prototype.onKeyDown=function(){var a=this;return function(b){switch(b=b||window.event,b.keyCode){case 38:return a.onUpArrowPressed()(),b.preventDefault(),!1;case 40:return a.onDownArrowPressed()(),b.preventDefault(),!1;case 27:return a.inside=!1,a.showBox=!1,a.onHideOptions()(),!0;case 32:return a.onItemSelect()(),b.stopPropagation(),b.preventDefault(),!1;default:return!0}}},a.prototype.onOptionsChange=function(){var a=this;return function(){a.initializeDropDown()}},a.prototype.onEnable=function(){var a=this;return function(b){a.captionElement.disabled=!b}},a.prototype.onDisable=function(){var a=this;return function(b){a.captionElement.disabled=b}},a.prototype.onValueChanged=function(){var a=this;return function(b){var d=a.findOption(c.unwrap(a.options),c.unwrap(b));void 0===b?a.captionText.textContent=c.unwrap(a.optionsCaption):a.captionText.textContent=a.getOptionsText(d);for(var e=a.containerDiv.childNodes,f=c.unwrap(a.options).indexOf(d),g=0;g<e.length;g++)b&&g===f?(e[g].classList.add(i),e[g].setAttribute("data-selected",(!0).toString())):(e[g].classList.remove(i),e[g].setAttribute("data-selected",(!1).toString()))}},a.prototype.onSelectedOptionsChanged=function(){var a=this;return function(){c.unwrap(a.options).forEach(function(b,c){a.styleOptionsDivMultiple(b,a.containerDiv.childNodes[c])}),a.setCaptionMultiple()}},a.prototype.onOptionsCaptionChanged=function(){var a=this;return function(b){a.multiple&&0===c.unwrap(a.selectedOptions).length?a.captionText.textContent=b:a.multiple||void 0!==a.value||(a.captionText.textContent=b)}},a.prototype.clearCallbacks=function(){this.captionElement.onkeydown=null,this.captionElement.onclick=null,this.containerDiv.onmouseenter=null,this.captionElement.onblur=null,this.containerDiv.onmouseout=null},a.prototype.applyContainerStyle=function(a,b){b.classList.add(d),b.classList.add(e),a.classList.add(d),a.classList.add(f),a.style.minWidth=b.offsetWidth+"px",a.style.width="auto",a.style.position="absolute",a.style.overflow="auto",a.style.zIndex=k.toString()},a.prototype.initializeDropDown=function(){for(var a=this;a.containerDiv.firstChild;)a.containerDiv.removeChild(a.containerDiv.firstChild);if(a.maxIndex=c.unwrap(a.options).length-1,c.unwrap(a.options).length>0){var b;c.unwrap(a.options).forEach(function(c){b=a.createOptionDiv(c),a.containerDiv.appendChild(b),a.multiple?a.styleOptionsDivMultiple(c,b):a.styleOptionsDivSingle(c,b)})}a.multiple&&a.setCaptionMultiple()},a.prototype.isBindingAllowed=function(){if(this.multiple&&(void 0===this.options||void 0===this.selectedOptions))throw"with multiple set, options and selectedOptions binding must be valid for this dropDownSelect-binding to work";if(!this.multiple&&(void 0===this.options||void 0===this.value))throw"without multiple set, options and value binding must be valid for this dropDownSelect-binding to work";if(void 0!==this.optionsText&&"string"!=typeof this.optionsText&&"function"!=typeof this.optionsText)throw"if optionsText is set it must be a valid string describing a property name";if(void 0!==this.optionsValue&&"string"!=typeof this.optionsValue&&"function"!=typeof this.optionsValue)throw"if optionsValue is set it must be a valid string describing a property name";if(void 0!==this.optionsCaption&&"string"!=typeof this.optionsCaption&&!c.isObservable(this.optionsCaption))throw"if optionsCaption is set it must be a valid string";return void 0===this.optionsCaption&&(this.optionsCaption=l),!0},a.prototype.initCallbacks=function(){this.captionElement.onkeydown=this.onKeyDown(),this.captionElement.onclick=this.onSelectPressed(),this.containerDiv.onmouseenter=this.onMouseEnter(),this.captionElement.onblur=this.onHideOptions(),this.containerDiv.onmouseout=this.onMouseOut()},a.prototype.initDomElements=function(){var a=this.element.parentNode,b=document.createElement("div");b.classList.add(d),this.createCaptionsElement(),a.replaceChild(b,this.element),this.containerDiv.style.display="none",b.style.position="relative",this.element.style.display="none",b.appendChild(this.element),b.appendChild(this.captionElement),b.appendChild(this.containerDiv)},a.prototype.initSubscriptions=function(){this.subscriptions.push(this.hoverIndex.subscribe(this.onHoverChanged())),c.isObservable(this.options)&&this.subscriptions.push(this.options.subscribe(this.onOptionsChange())),void 0!==this.enable&&(this.onEnable()(c.unwrap(this.enable)),c.isObservable(this.enable)&&this.subscriptions.push(this.enable.subscribe(this.onEnable()))),void 0!==this.disable&&(this.onDisable()(c.unwrap(this.disable)),c.isObservable(this.disable)&&this.subscriptions.push(this.disable.subscribe(this.onDisable()))),void 0===this.value||this.multiple||this.subscriptions.push(this.value.subscribe(this.onValueChanged())),void 0!==this.selectedOptions&&this.multiple&&this.subscriptions.push(this.selectedOptions.subscribe(this.onSelectedOptionsChanged())),c.isObservable(this.optionsCaption)&&this.subscriptions.push(this.optionsCaption.subscribe(this.onOptionsCaptionChanged()))},a.prototype.dispose=function(){if(this.clearCallbacks(),this.captionElement=null,this.containerDiv=null,this.captionText=null,this.element=null,this.options=null,this.selectedOptions=null,this.value=null,this.enable=null,this.disable=null,this.optionsCaption=null,this.optionsText=null,this.optionsValue=null,this.multiple=null,this.hoverIndex=null,this.subscriptions){for(var a=0;a<this.subscriptions.length;a++)this.subscriptions[a].dispose();this.subscriptions=null}},a}(),o=function(){function a(a,b){this.x=a,this.y=b}return a.prototype.distance=function(a){var b=a.x-this.x,c=a.y-this.y;return Math.sqrt(b*b+c*c)},a.distance=function(a,b){var c=b.x-a.x,d=b.y-a.y;return Math.sqrt(c*c+d*d)},a}();a.Binding={init:function(a,b,d){var e=new n(b,d,a);e.isBindingAllowed(),e.initDomElements(),e.initCallbacks(),e.initSubscriptions(),e.initializeDropDown(),e.applyContainerStyle(e.containerDiv,e.captionElement),c.utils.domNodeDisposal.addDisposeCallback(a,function(){e.dispose(),e=void 0})}}}(d||(d={})),c.bindingHandlers.cap3Options=d.Binding});