define(['ko','mageUtils'],function(ko,utils){'use strict';var captionPlaceholder={},optgroupTmpl='',nbspRe=/ /g,optionsText,optionsValue,optionTitle;ko.bindingHandlers.optgroup={init:function(element){if(ko.utils.tagNameLower(element)!=='select'){throw new Error('options binding applies only to SELECT elements');} while(element.length>0){element.remove(0);}},update:function(element,valueAccessor,allBindings){var selectWasPreviouslyEmpty=element.length===0,previousScrollTop=!selectWasPreviouslyEmpty&&element.multiple?element.scrollTop:null,includeDestroyed=allBindings.get('optionsIncludeDestroyed'),arrayToDomNodeChildrenOptions={},captionValue,unwrappedArray=ko.utils.unwrapObservable(valueAccessor()),filteredArray,previousSelectedValues,itemUpdate=false,callback=setSelectionCallback,nestedOptionsLevel=-1;optionsText=ko.utils.unwrapObservable(allBindings.get('optionsText'))||'text';optionsValue=ko.utils.unwrapObservable(allBindings.get('optionsValue'))||'value';optionTitle=optionsText+'title';if(element.multiple){previousSelectedValues=ko.utils.arrayMap(selectedOptions(),ko.selectExtensions.readValue);}else{previousSelectedValues=element.selectedIndex>=0?[ko.selectExtensions.readValue(element.options[element.selectedIndex])]:[];} if(unwrappedArray){if(typeof unwrappedArray.length==='undefined'){unwrappedArray=[unwrappedArray];} filteredArray=ko.utils.arrayFilter(unwrappedArray,function(item){if(item&&!item.label){return false;} return includeDestroyed||item===undefined||item===null||!ko.utils.unwrapObservable(item._destroy);});filteredArray.map(recursivePathBuilder,null);} arrayToDomNodeChildrenOptions.beforeRemove=function(option){element.removeChild(option);};if(allBindings.has('optionsAfterRender')){callback=function(arrayEntry,newOptions){setSelectionCallback(arrayEntry,newOptions);ko.dependencyDetection.ignore(allBindings.get('optionsAfterRender'),null,[newOptions[0],arrayEntry!==captionPlaceholder?arrayEntry:undefined]);};} filteredArray=formatOptions(filteredArray);ko.utils.setDomNodeChildrenFromArrayMapping(element,filteredArray,optionNodeFromArray,arrayToDomNodeChildrenOptions,callback);ko.dependencyDetection.ignore(function(){var selectionChanged;if(allBindings.get('valueAllowUnset')&&allBindings.has('value')){ko.selectExtensions.writeValue(element,ko.utils.unwrapObservable(allBindings.get('value')),true);}else{if(element.multiple){selectionChanged=previousSelectedValues.length&&selectedOptions().length=0?ko.selectExtensions.readValue(element.options[element.selectedIndex])!==previousSelectedValues[0]:previousSelectedValues.length||element.selectedIndex>=0;} if(selectionChanged){ko.utils.triggerEvent(element,'change');}}});if(previousScrollTop&&Math.abs(previousScrollTop-element.scrollTop)>20){element.scrollTop=previousScrollTop;} function selectedOptions(){return ko.utils.arrayFilter(element.options,function(node){return node.selected;});} function applyToObject(object,predicate,defaultValue){var predicateType=typeof predicate;if(predicateType==='function'){return predicate(object);}else if(predicateType==='string'){return object[predicate];} return defaultValue;} function recursivePathBuilder(obj){obj[optionTitle]=(this&&this[optionTitle]?this[optionTitle]+'/':'')+obj[optionsText].trim();if(Array.isArray(obj[optionsValue])){obj[optionsValue].map(recursivePathBuilder,obj);}} function optionNodeFromArray(arrayEntry,oldOptions){var option;if(oldOptions.length){previousSelectedValues=oldOptions[0].selected?[ko.selectExtensions.readValue(oldOptions[0])]:[];itemUpdate=true;} if(arrayEntry===captionPlaceholder){option=element.ownerDocument.createElement('option');ko.utils.setTextContent(option,allBindings.get('optionsCaption'));ko.selectExtensions.writeValue(option,undefined);}else if(typeof arrayEntry[optionsValue]==='undefined'){if(arrayEntry.__disableTmpl){option='';}else{option=utils.template(optgroupTmpl,{label:arrayEntry[optionsText],title:arrayEntry[optionsText+'title']});} option=ko.utils.parseHtmlFragment(option)[0];}else{option=element.ownerDocument.createElement('option');option.setAttribute('data-title',arrayEntry[optionsText+'title']);ko.selectExtensions.writeValue(option,arrayEntry[optionsValue]);ko.utils.setTextContent(option,arrayEntry[optionsText]);} return[option];} function setSelectionCallback(newOptions){var isSelected;if(previousSelectedValues.length&&newOptions.value){isSelected=ko.utils.arrayIndexOf(previousSelectedValues,ko.selectExtensions.readValue(newOptions.value))>=0;ko.utils.setOptionNodeSelectionState(newOptions.value,isSelected);if(itemUpdate&&!isSelected){ko.dependencyDetection.ignore(ko.utils.triggerEvent,null,[element,'change']);}}} function strPad(string,times){return new Array(times+1).join(string);} function formatOptions(options){var res=[];nestedOptionsLevel++;if(!nestedOptionsLevel){if(allBindings.has('optionsCaption')){captionValue=ko.utils.unwrapObservable(allBindings.get('optionsCaption'));if(captionValue!==null&&captionValue!==undefined&&captionValue!==false){res.push(captionPlaceholder);}}} ko.utils.arrayForEach(options,function(option){var value=applyToObject(option,optionsValue,option),label=applyToObject(option,optionsText,value)||'',disabled=applyToObject(option,'disabled',false)||false,obj={},space='\u2007\u2007\u2007';obj[optionTitle]=applyToObject(option,optionsText+'title',value);if(disabled){obj.disabled=disabled;} if(option.hasOwnProperty('__disableTmpl')){obj.__disableTmpl=option.__disableTmpl;} label=label.replace(nbspRe,'').trim();if(Array.isArray(value)){obj[optionsText]=strPad(' ',nestedOptionsLevel*4)+label;res.push(obj);res=res.concat(formatOptions(value));}else{obj[optionsText]=strPad(space,nestedOptionsLevel*2)+label;obj[optionsValue]=value;res.push(obj);}});nestedOptionsLevel--;return res;}}};});