// 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/_base/declare dojo/_base/array dojo/_base/lang dojo/Deferred dojo/promise/all ./LayerInfo dojox/gfx dojo/dom-construct dojo/dom-attr dojo/dom-class dojo/aspect jimu/portalUrlUtils jimu/portalUtils jimu/utils esri/symbols/jsonUtils esri/layers/LabelLayer esri/layers/LabelClass esri/dijit/PopupTemplate esri/dijit/Legend esri/graphic esri/geometry/Point esri/tasks/query esri/tasks/RelationshipQuery esri/tasks/QueryTask".split(" "),function(x,l,f,n,y,z,t,m,A,B,C,u,D,p,E,v,F,G,w,H,I,J,K,L){return x(z,
{_legendsNode:null,controlPopupInfo:null,constructor:function(){this._initControlPopup();this._updateLayerObjectName();try{this._initToShowLabels()}catch(a){console.warn(a.message)}},_updateLayerObjectName:function(){if(this.layerObject&&!this.layerObject.empty&&this.layerObject.name&&!f.getObject("_wabProperties.originalLayerName",!1,this.layerObject)){f.setObject("_wabProperties.originalLayerName",this.layerObject.name,this.layerObject);this.layerObject.name=this.title;if(void 0===this.layerObject.arcgisProps||
null===this.layerObject.arcgisProps)this.layerObject.arcgisProps={};f.setObject("arcgisProps.title",this.title,this.layerObject)}},_initToShowLabels:function(){var a=f.getObject("_wabProperties.itemLayerInfo",!1,this.layerObject);a&&!this.layerObject.empty&&D.getPortal(a.portalUrl).getItemData(a.itemId).then(f.hitch(this,function(b){var c;b&&b.layers&&(l.some(b.layers,function(d){return d.id===this.layerObject.layerId?(c=d,!0):!1},this),c&&c.showLabels&&((b=f.getObject("layerDefinition.drawingInfo.labelingInfo",
!1,c))&&!this.layerObject.labelingInfo&&this.layerObject.setLabelingInfo&&(b=l.map(b,function(d){return new F(d)},this),this.layerObject.setLabelingInfo(b)),this.showLabels()))}),f.hitch(this,function(b){b&&b.message&&console.log(b.message)}))},_initOldFilter:function(){this._oldFilter=this.layerObject&&!this.layerObject.empty&&this.layerObject.getDefinitionExpression?this.layerObject.getDefinitionExpression():null},_getLayerOptionsForCreateLayerObject:function(){var a={};a.id=this.id;var b=[],c=
this.getInfoTemplate();c&&c.info&&c.info.fieldInfos?l.forEach(c.info.fieldInfos,function(d){d.visible&&b.push(d.fieldName)},this):b=["*"];a.outFields=b;return a},_getExtent:function(){var a=new n;var b=this.layerObject.graphics&&0<this.layerObject.graphics.length?p.graphicsExtent(this.layerObject.graphics):this.layerObject.fullExtent||this.layerObject.initialExtent;this.layerObject.url?this.getServiceDefinition().then(f.hitch(this,function(c){var d=new L(this.layerObject.url),e=new J,h=this.getFilter();
e.where=h?h:"1\x3d1";e.outSpatialReference=this.map.spatialReference;e.returnGeometry=!0;c&&c.advancedQueryCapabilities&&c.advancedQueryCapabilities.supportsReturningQueryExtent?d.executeForExtent(e,f.hitch(this,function(k){var g=k.extent;1===k.count&&"esriGeometryPoint"===this.layerObject.geometryType&&(k=new I(k.extent.xmin,k.extent.ymin,k.extent.spatialReference),g=p.graphicsExtent([new H(k)]));a.resolve(g)}),f.hitch(this,function(){a.resolve(b);console.log("executeForExtent failed.")})):this.getSupportTableInfo().then(f.hitch(this,
function(k){k.isSupportQuery?d.execute(e).then(f.hitch(this,function(g){g=p.graphicsExtent(g.features);a.resolve(g)}),f.hitch(this,function(){a.resolve(b);console.log("query execute failed.")})):a.resolve(b)}))})):a.resolve(b);return a},_resetLayerObjectVisiblity:function(a){a=a?a[this.id]:null;!this.originOperLayer.collection&&a&&(this.layerObject.setVisibility(a.visible),this._visible=this.layerObject.visible)},_initVisible:function(){var a=!1;if(this.originOperLayer.collection&&this._notFirstInitVisilbeFlag){a=
this.originOperLayer.collection.layerInfo;var b=this.layerObject.visible;if(a._oldIsShowInMap!==a.isShowInMap())return;this._visible=b?!0:!1;a._onVisibilityChanged()}else this._visible=a=this.originOperLayer.layerObject.visible;this._notFirstInitVisilbeFlag=!0},_setTopLayerVisible:function(a){this.originOperLayer.collection?this.originOperLayer.collection.layerInfo._visible?a?(this.layerObject.show(),this._visible=!0):(this.layerObject.hide(),this._visible=!1):a?(this.layerObject.hide(),this._visible=
!0):(this.layerObject.hide(),this._visible=!1):(a?this.layerObject.show():this.layerObject.hide(),this._visible=a)},setLayerVisiblefromTopLayer:function(){this.originOperLayer.collection.layerInfo._visible?this._visible&&this.layerObject.show():this.layerObject.hide()},_prepareCheckedInfoForShowOrHide:function(a){for(var b={},c=this;c.parentLayerInfo;)b[c.id]=a,c=c.parentLayerInfo;return b},show:function(){if(this.isRootLayer())this._setTopLayerVisible(!0);else{var a=this.getRootLayerInfo();if(a._setSubLayerVisibleByCheckedInfo){var b=
this._prepareCheckedInfoForShowOrHide(!0);a._setSubLayerVisibleByCheckedInfo(b);a.show()}}},_initControlPopup:function(){this.controlPopupInfo={enablePopup:this.layerObject.infoTemplate?!0:!1,infoTemplate:this.layerObject.infoTemplate};this.layerObject._infoTemplate=this.layerObject.infoTemplate;var a=C.after(this.layerObject,"setInfoTemplate",f.hitch(this,function(){this.layerObject._infoTemplate=this.layerObject.infoTemplate;this.controlPopupInfo.infoTemplate=this.layerObject.infoTemplate;this.controlPopupInfo.enablePopup||
(this.layerObject.infoTemplate=null)}));this._eventHandles.push(a)},_getServiceDefinition:function(){var a;(a=this.getUrl())&&this.isRootLayer()&&"esri.layers.FeatureLayer"===this.layerObject.declaredClass?a=this._serviceDefinitionBuffer.getRequest(this.subId).request(a):(a=new n,a.resolve(null));return a},createLegendsNode:function(){var a=m.create("div",{"class":"legends-div jimu-legends-div-flag jimu-leading-margin1",legendsDivId:this.id},document.body);m.create("img",{"class":"legends-loading-img",
src:require.toUrl("jimu")+"/images/loading.gif"},a);return a},drawLegends:function(a){this._initLegendsNodeByLegendDijit(a)},_initLegendsNodeByLegendDijit:function(a){if(this.layerObject&&!this.layerObject.empty&&(!this.originOperLayer.subLayer||0===this.originOperLayer.subLayers.length)&&this.layerObject.loaded){m.empty(a);B.remove(a,"jimu-legends-div-flag");var b=new w({map:this.map,layerInfos:[{layer:this.layerObject}],arrangement:w.ALIGN_LEFT,respectCurrentMapScale:!1,respectVisibility:!1},m.create("div",
{},a));b.startup();a._legendDijit=b}},_initLegendsNode:function(a){var b=[],c=this.layerObject;!this.layerObject||this.layerObject.empty||this.originOperLayer.subLayer&&0!==this.originOperLayer.subLayers.length||(m.empty(a),c.renderer&&(c.renderer.infos?b=f.clone(c.renderer.infos):b.push({label:c.renderer.label,symbol:c.renderer.symbol}),c.renderer&&c.renderer.defaultSymbol&&0<b.length&&b.push({label:c.renderer.defaultLabel||"others",symbol:c.renderer.defaultSymbol}),l.forEach(b,function(d){d.legendDiv=
m.create("div",{"class":"legend-div"},a);d.symbolDiv=m.create("div",{"class":"legend-symbol jimu-float-leading"},d.legendDiv);d.labelDiv=m.create("div",{"class":"legend-label jimu-float-leading",innerHTML:p.sanitizeHTML(d.label)||" "},d.legendDiv);if("textsymbol"===d.symbol.type)A.set(d.symbolDiv,"innerHTML",p.sanitizeHTML(d.symbol.text));else{var e=t.createSurface(d.symbolDiv,50,50);d=E.getShapeDescriptors(d.symbol);e.createShape(d.defaultShape).setFill(d.fill).setStroke(d.stroke).setTransform(t.matrix.translate(25,
25))}},this)))},obtainNewSubLayers:function(){var a=[];l.forEach(this.originOperLayer.subLayers,function(b){b.parentLayerInfo=this;b=this._layerInfoFactory.create(b);a.push(b);b.init()},this);return a},_isAllSubLayerVisibleOnPath:function(){for(var a=!0,b=this;!b.isRootLayer();)a=a&&b.isVisible(),b=b.parentLayerInfo;return a},_getCustomPopupInfo:function(a,b){var c=null;a&&a.fields&&(c={title:a.name,fieldInfos:[],description:null,showAttachments:!0,mediaInfos:[]},l.forEach(a.fields,function(d){var e=
!1;b?l.some(b,f.hitch(this,function(h){return h&&d.name.toLowerCase()===h.toLowerCase()}))&&(e=!0):e=!0;e&&(e=p.getDefaultPortalFieldInfo(d),e.visible=!0,e.isEditable=d.editable,c.fieldInfos.push(e))},this));return c},_getDefaultPopupInfo:function(a){if(a=this._getCustomPopupInfo(a))a.fieldInfos=l.filter(a.fieldInfos,f.hitch(this,function(b){return(b=b.fieldName&&b.fieldName.toLowerCase())&&0>b.indexOf("object")&&0>b.indexOf("globalid")&&0>b.indexOf("shape")&&0>b.indexOf("perimeter")?!0:!1}));return a},
_getDefaultPopupTemplate:function(a){var b=null;(a=this.getPopupInfo()||this._getDefaultPopupInfo(a))&&(b=new G(a));return b},enablePopup:function(){return this.loadInfoTemplate().then(f.hitch(this,function(){return this.controlPopupInfo.infoTemplate?(this.controlPopupInfo.enablePopup=!0,this.layerObject.infoTemplate=this.controlPopupInfo.infoTemplate,!0):!1}))},disablePopup:function(){this.controlPopupInfo.enablePopup=!1;this.layerObject.infoTemplate=null},isPopupEnabled:function(){return this.controlPopupInfo&&
this.controlPopupInfo.enablePopup?!0:!1},isSupportPopup:function(){var a=new n;this.loadInfoTemplate().then(f.hitch(this,function(b){b?a.resolve(!0):a.resolve(!1)}));return a},loadInfoTemplate:function(){var a=new n;this.controlPopupInfo.infoTemplate?a.resolve(this.controlPopupInfo.infoTemplate):this.getLayerObject().then(f.hitch(this,function(){this.controlPopupInfo.infoTemplate=this._getDefaultPopupTemplate(this.layerObject);a.resolve(this.controlPopupInfo.infoTemplate)}),f.hitch(this,function(){a.resolve(null)}));
return a},getInfoTemplate:function(){return this.controlPopupInfo.infoTemplate},_getRelatedUrls:function(a,b){var c=[];if(!a||!a.url||!a.relationships)return c;var d=a.url.lastIndexOf("/"),e=a.url.slice(0,d);l.forEach(a.relationships,function(h){b&&h.role&&"esriRelCardinalityManyToMany"!==h.cardinality&&b!==h.role||c.push(e+"/"+h.relatedTableId.toString())},this);return c},getRelatedTableInfoArray:function(a){var b=[],c=new n;this.getLayerObject().then(f.hitch(this,function(d){var e=this._getRelatedUrls(d,
a);0!==e.length&&this._getLayerInfosObj().traversalAll(f.hitch(this,function(h){if(0===e.length)return!0;l.forEach(e,function(k,g){f.getObject("layerObject.url",!1,h)&&k&&u.removeProtocol(k.toString().toLowerCase()).replace(/\/+/g,"/")===u.removeProtocol(h.layerObject.url.toString().toLowerCase()).replace(/\/+/g,"/")&&(b.push(h),e[g]="")},this);return!1}));c.resolve(b)}),f.hitch(this,function(){c.resolve(b)}));return c},_getOrderByFields:function(a){var b=null;if(void 0===a||null===a)return b;var c=
this.getPopupInfo();c&&c.relatedRecordsInfo&&c.relatedRecordsInfo.orderByFields&&l.some(c.relatedRecordsInfo.orderByFields,function(d){var e=d.field.split("/");if(e[1]===a.toString())return b=[e[2]+" "+d.order],!0},this);return b},getOriRelationshipByDestLayer:function(a,b,c){var d=null;a=l.filter(a.relationships,function(e){if(e.relatedTableId===b.layerId)return!0},this);return d=a[c]?a[c]:a[0]},getRelatedRecords:function(a,b,c){var d=new n,e=new K,h=this.getLayerObject(),k=b.getLayerObject();y({originalLayerObject:h,
relatedLayerObject:k}).then(f.hitch(this,function(g){g.originalLayerObject&&g.relatedLayerObject||d.resolve([]);g=this.getOriRelationshipByDestLayer(this.layerObject,g.relatedLayerObject,c);e.outFields=["*"];e.relationshipId=g&&g.id;var r=a.attributes[this.layerObject.objectIdField];e.objectIds=[r];e.definitionExpression=b.getFilter();e.orderByFields=this._getOrderByFields(g&&g.id);this.layerObject.queryRelatedFeatures(e,f.hitch(this,function(q){(q=q[r]&&q[r].features)?d.resolve(q):d.resolve([])}),
f.hitch(this,function(){d.resolve([])}))}));return d},getFilter:function(){return this.layerObject&&!this.layerObject.empty&&this.layerObject.getDefinitionExpression?this.layerObject.getDefinitionExpression():null},setFilter:function(a,b){this.layerObject&&!this.layerObject.empty&&this.layerObject.setDefinitionExpression&&(b=f.mixin({},b),f.setObject("_wabProperties.objectPassWithFilterChangeEvent",b,this.layerObject),this.layerObject.setDefinitionExpression(a))},_isAlreadyInLabelLayerOfMap:function(a){var b=
!1;a&&(b=l.some(a.getFeatureLayers(),function(c){return this.id===c.id},this));return b},_addToLabelLayerOfMap:function(){var a=this.getLabelLayerOfMap();a&&!this._isAlreadyInLabelLayerOfMap(a)&&(a.addFeatureLayer(this.layerObject),this.map.removeLayer(a),this.map.addLayer(a))},_removeFromLabelLayerOfMap:function(){this.getLabelLayerOfMap().removeFeatureLayer(this.id)},_isSupportLabelControl:function(){return this.isRootLayer()&&!this.layerObject.empty&&"esri.layers.FeatureLayer"===this.layerObject.declaredClass&&
this.layerObject.labelingInfo&&0<this.layerObject.labelingInfo.length?!0:!1},getLabelLayerOfMap:function(){if(this.map._labels)var a=this.map._labels;else a=this.map.getLayer("labels"),a||(a=new v({id:"labels"}),this.map.addLayer(a));return a},obtainLabelControl:function(){var a=this.id+"_labelLayer",b=this.map.getLayer(a);if(!b&&this._isSupportLabelControl()){var c=this.getLabelLayerOfMap();c&&this._isAlreadyInLabelLayerOfMap(c)&&(b=new v({id:a}),b.addFeatureLayer(this.layerObject),this.map.addLayer(b),
this._removeFromLabelLayerOfMap())}return b?b:null},restoreLabelControl:function(){this._isSupportLabelControl()&&(this.destroyRealtedLabelLayer(),this._addToLabelLayerOfMap())},destroyRealtedLabelLayer:function(){var a=this.map.getLayer(this.id+"_labelLayer");a&&this.map.removeLayer(a)},destroyLabelLayer:function(){this.map.getLayer(this.id)||(this._removeFromLabelLayerOfMap(),this.destroyRealtedLabelLayer())},canShowLabel:function(){return this._isSupportLabelControl()},isShowLabels:function(){var a=
this.map.getLayer(this.id+"_labelLayer"),b=this.getLabelLayerOfMap();return this._isSupportLabelControl()&&(a||b&&this._isAlreadyInLabelLayerOfMap(b))?this.layerObject.showLabels:!1},showLabels:function(){this._isSupportLabelControl()&&this.layerObject.setShowLabels&&(this.map.getLayer(this.id+"_labelLayer")||this._addToLabelLayerOfMap(),this.layerObject.setShowLabels(!0))},hideLabels:function(){this._isSupportLabelControl()&&this.layerObject.setShowLabels&&this.layerObject.setShowLabels(!1)}})});