// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See http://@sbaseurl@/jsapi/jsapi/esri/copyright.txt and http://www.arcgis.com/apps/webappbuilder/copyright.txt for details.

//>>built
define("dojo/on dojo/_base/declare dojo/promise/all dojo/_base/lang dojo/_base/html dojo/_base/array jimu/dijit/CheckBox jimu/dijit/LayerChooserFromMap jimu/LayerStructure".split(" "),function(p,m,q,k,e,n,v,r,t){var g=m([r],{templateString:'\x3cdiv style\x3d"width:100%;"\x3e\x3cdiv data-dojo-attach-point\x3d"errorTipSection" class\x3d"error-tip-section"\x3e\x3cspan class\x3d"jimu-icon jimu-icon-error"\x3e\x3c/span\x3e\x3cspan class\x3d"jimu-state-error-text" data-dojo-attach-point\x3d"errTip"\x3e${nls.noLayersTip}\x3c/span\x3e\x3c/div\x3e\x3cdiv data-dojo-attach-point\x3d"treeSection" class\x3d"tree-section"\x3e\x3cul data-dojo-attach-point\x3d"treeUl" class\x3d"tree-ul tree-root-ul"\x3e\x3c/ul\x3e\x3c/div\x3e\x3c/div\x3e',
map:null,layerStateController:null,layerState:null,customFilter:null,onlySelectLeafLayer:!1,displayLayerTypeIcon:!0,showTables:!0,viewMode:!1,onlyShowWebMapLayers:!1,layerStructure:null,_layerDatas:null,_eventHandles:null,postMixInProperties:function(){this.nls=window.jimuNls.basicLayerChooserFromMap},postCreate:function(){e.addClass(this.domNode,"jimu-basic-layer-chooser-from-map");e.addClass(this.domNode,"jimu-basic-layer-chooser-from-map-lite");this._layerDatas={};this._eventHandles=[];this.layerStructure=
this.map?t.createInstance(this.map):t.getInstance();this.layerInfosObj=this.layerStructure._layerInfos;this.layerState=this._clearLayerState(this.layerState)||{};this.layerStateController||(this.layerStateController=new g.LayerStateController);var a=this.customFilter?k.hitch(this,this.customFilter):k.hitch(this,this.filter);this.filter=r.andCombineFilters([this.basicFilter,a]);this._createTree()},_createTree:function(){if(this.onlyShowWebMapLayers){var a=this.layerStructure.getWebmapLayerNodes();
var b=this.layerStructure.getWebmapTableNodes()}else a=this.layerStructure.getLayerNodes(),b=this.layerStructure.getTableNodes();0<this._createLayerNodes(a.concat(this.showTables?b:[]),this.treeUl)&&(e.setStyle(this.errorTipSection,"display","none"),this.layerStateController.restoreState(this.layerState,this.layerStructure))},_createLayerNodes:function(a,b){var c=n.map(a,function(h){return this.filter(h._layerInfo)},this),d=0;q(c).then(k.hitch(this,function(h){n.forEach(h,function(l,f){l&&(this._createLayerNode(a[f],
b),d++)},this)}));return d},_createLayerNode:function(a,b){var c=e.create("li",{"class":"tree-node-li",id:"layerchooserlite-tree-node-li-"+a.id},b),d=e.create("div",{"class":"tree-node-div"},c);b=e.create("span",{"class":"tree-node-column-span collapse-span"},d);var h=e.create("span",{"class":"tree-node-column-span check-box-span"},d);h=e.create("div",{"class":"tree-node-column-div check-box-div"},h);var l=e.create("span",{"class":"tree-node-column-span icon-span "+(this.displayLayerTypeIcon?"display":
"")},d);var f=(f=this.layerState[a.id])?f.selected:this.layerStateController.getState(a);f=new v({checked:f},h);var u=e.create("span",{"class":"tree-node-column-span title-span",innerHTML:a.title},d),w=e.create("ul",{"class":"tree-ul tree-subnode-ul",style:"display:none; "},c);c={layerNode:a,layerNodeLi:c,layerNodeDiv:d,collapseSpan:b,iconSpan:l,checkBox:f,subLayerNodeUl:w,hasBeenOpened:!1};this._layerDatas[a.id]=c;a.isLeaf()||(e.addClass(b,"is-leaf"),e.addClass(u,"is-leaf"),a=p(b,"click",k.hitch(this,
this._onCollapse,c)),this._eventHandles.push(a),a=p(u,"click",k.hitch(this,this._onCollapse,c)),this._eventHandles.push(a),this.onlySelectLeafLayer&&(f.setStatus(!1),e.setStyle(h,"display","none")));!0===this.viewMode&&f.setStatus(!1);a=p(f.domNode,"click",k.hitch(this,this._onCheckBoxChange,c));this._eventHandles.push(a);this._setIconImage(c,!1);return c},_setIconImage:function(a,b){if(this.displayLayerTypeIcon){var c=a.layerNode,d=c.getLayerType(),h=c.getLayerObject();q({layerType:d,layerObject:h}).then(k.hitch(this,
function(l){if(l.layerType&&l.layerObject){var f={type:l.layerType,layerInfo:c._layerInfo};l=window.location.protocol+"//"+window.location.host+require.toUrl("jimu");(f=this._getIconInfo(f,b).imageName)&&e.setStyle(a.iconSpan,"background-image","url("+l+"/css/images/"+f+")")}}))}},_getCheckBoxValue:function(a){return a.getStatus()?a.getValue():!1},_clearLayerState:function(a){var b={};a&&this.layerStructure.traversal(k.hitch(this,function(c){a[c.id]&&(b[c.id]={selected:a[c.id].selected})}));return b},
_selectOrDeselectLayer:function(a,b){if(a=this._layerDatas[a])a.checkBox.setValue(b),this._onCheckBoxChange(a)},selectLayer:function(a){this._selectOrDeselectLayer(a,!0)},deselectLayer:function(a){this._selectOrDeselectLayer(a,!1)},getState:function(){var a=k.clone(this.layerState),b;for(b in this._layerDatas)this._layerDatas.hasOwnProperty(b)&&"function"!==typeof this._layerDatas[b]&&(this._getCheckBoxValue(this._layerDatas[b].checkBox)?a[b]={selected:!0}:a[b]={selected:!1});return a},restoreState:function(a){this.layerState=
this._clearLayerState(a);for(var b in this._layerDatas)if(this._layerDatas.hasOwnProperty(b)&&"function"!==typeof this._layerDatas[b]){var c=(a=this._layerDatas[b])&&a.checkBox,d=this.layerState[b];d?c.setValue(d.selected):c.setValue(this.layerStateController.getState(a.layerNode))}this.layerStateController.restoreState(this.layerState,this.layerStructure)},setViewMode:function(a){for(var b in this._layerDatas)if(this._layerDatas.hasOwnProperty(b)&&"function"!==typeof this._layerDatas[b]){var c=this._layerDatas[b];
c=c&&c.checkBox;!0===a?(this.viewMode=!0,c.setStatus(!1)):(this.viewMode=!1,c.setStatus(!0))}},getSelectedLayerNodes:function(){var a=[],b=this.getState(),c;for(c in b)if(b.hasOwnProperty(c)&&"function"!==typeof b[c]&&b[c].selected){var d=this.layerStructure.getNodeById(c);d&&a.push(d)}return a},getLoadedLayerNodes:function(){var a=[],b;for(b in this._layerDatas)if(this._layerDatas.hasOwnProperty(b)&&"function"!==typeof this._layerDatas[b]){var c=this.layerStructure.getNodeById(b);c&&a.push(c)}return a},
getLayerAssociateDomNodesById:function(a){var b=null;(a=this._layerDatas[a])&&(b={collapseIcon:a.collapseSpan,checkBox:a.checkBox.domNode,layerTypeIcon:a.iconSpan});return b},getSelectedItems:function(){var a=[];return a=n.map(this.getSelectedLayerNodes(),function(b){return{name:b.title,url:b.getUrl(),layerInfo:b._layerInfo}},this)},getAllItems:function(){return[]},_clear:function(){this._layerDatas={};n.forEach(this._eventHandles,function(a){a.remove()},this);this._eventHandles=[];e.empty(this.treeUl)},
destroy:function(){this._clear();this.map&&this.layerStructure.destroy();this.shelter&&(this.shelter.destroy(),this.shelter=null);this.inherited(arguments)},_onCollapse:function(a){var b="none"===e.getStyle(a.subLayerNodeUl,"display")?!0:!1;b?(e.setStyle(a.subLayerNodeUl,"display","block"),e.addClass(a.collapseSpan,"opened")):(e.setStyle(a.subLayerNodeUl,"display","none"),e.removeClass(a.collapseSpan,"opened"));this._setIconImage(a,b);a.hasBeenOpened||(this._createLayerNodes(a.layerNode.getSubNodes(),
a.subLayerNodeUl),a.hasBeenOpened=!0)},_onCheckBoxChange:function(a,b){this.layerStateController.setState(a.layerNode,this._getCheckBoxValue(a.checkBox));this.emit("selection-change",a.layerNode,this._getCheckBoxValue(a.checkBox));this._onTreeClick(a,b)},_onLayerInfosChanged:function(){this._createTree();this.emit("update")},_onLayerInfosIsShowInMapChanged:function(){this._createTree();this.emit("update")},_onTreeClick:function(a,b){a={name:a.layerNode.title||"",parent:null,layerInfo:a.layerNode._layerInfo,
type:null,layerClass:null,id:null,isLeaf:a.layerNode.isLeaf(),hasChildren:a.layerNode.isLeaf()?!1:!0};this.emit("tree-click",a,null,b)}});g.LayerStateController=m(null,{getState:function(a){return!0},setState:function(a,b){return this},restoreState:function(a,b){return this}});g.LayerVisibilityStateController=m(g.LayerStateController,{getState:function(a){return a.isToggledOn()},setState:function(a,b){a.toggle();return this},restoreState:function(a,b){var c={layerOptions:{}},d;for(d in a)a.hasOwnProperty(d)&&
"function"!==typeof a[d]&&(c.layerOptions[d]={visible:a[d].selected});b.restoreState(c);return this}});g.layerVisibilityStateController=new g.LayerVisibilityStateController;g.LayerLegendStateController=m(g.LayerStateController,{getState:function(a){return a.isShowLegend()}});g.layerLegendStateController=new g.LayerLegendStateController;return g});