text: // COPYRIGHT © 201 Esri
  //
  // All rights reserved under the copyright laws of the United States
  // and applicable international laws, treaties, and conventions.
  //
  // This material is licensed for use under the Esri Master License
  // Agreement (MLA), and is bound by the terms of that agreement.
  // You may redistribute and use this code without modification,
  // provided you adhere to the terms of the MLA and include this
  // copyright notice.
  //
  // See use restrictions at http://www.esri.com/legal/pdfs/mla_e204_e300/english
  //
  // For additional information, contact:
  // Environmental Systems Research Institute, Inc.
  // Attn: Contracts and Legal Services Department
  // 380 New York Street
  // Redlands, California, USA 92373
  // USA
  //
  // email: contracts@esri.com
  //
  // See http://js.arcgis.com/3.34/esri/copyright.txt for details.
  
  define(["require","dojo/aspect","dojo/_base/declare","dojo/_base/lang","dojo/_base/array","dojo/_base/connect","dojo/_base/json","dojo/has","dojo/json","dojo/string","dojo/dom-style","dojo/dom-attr","dojo/dom-construct","dojo/query","dojo/dom-class","dojo/on","dijit/_WidgetBase","dijit/_TemplatedMixin","dijit/_WidgetsInTemplateMixin","dijit/_OnDijitClickMixin","dijit/_FocusMixin","dijit/registry","dijit/form/Button","dijit/form/CheckBox","dijit/form/Form","dijit/form/Select","dijit/form/TextBox","dijit/form/ValidationTextBox","dijit/layout/ContentPane","dijit/form/FilteringSelect","dijit/Dialog","dojox/form/CheckedMultiSelect","../../kernel","../../lang","./AnalysisBase","./utils","./CreditEstimator","./_AnalysisOptions","../CalculateField","./components/TimeBoundarySelect/TimeBoundarySelect","dojo/i18n!../../nls/jsapi","dojo/text!./templates/CalculateField.html"],(function(e,t,i,s,a,l,n,r,o,h,d,c,u,p,y,_,m,f,S,g,L,F,v,b,k,x,C,B,A,T,w,I,j,E,D,N,O,R,U,M,P,H){var J=i([m,f,S,g,L,R,D],{declaredClass:"esri.dijit.analysis.CalculateField",templateString:H,widgetsInTemplate:!0,inputLayer:null,outputLayerName:null,i18n:null,toolName:"CalculateField",helpFileName:"CalculateField",resultParameter:"output",constructor:function(e){this._pbConnects=[],e.containerNode&&(this.container=e.containerNode),e.trackFields&&"string"==typeof e.trackFields&&(e.trackFields=e.trackFields.split(",")),this._setClasses(e)},_setClasses:function(e){this._addBtnClass=e.primaryActionButttonClass||"esriAnalysisSubmitButton"},destroy:function(){this.inherited(arguments),a.forEach(this._pbConnects,l.disconnect),delete this._pbConnects},postMixInProperties:function(){this.inherited(arguments),s.mixin(this.i18n,P.calculateFields),s.mixin(this.i18n,P.calculateFieldTool)},postCreate:function(){this.inherited(arguments),y.add(this._form.domNode,"esriSimpleForm"),d.set(this._trackFieldSelect.selectNode,"width","80%"),this._outputLayerInput.set("validator",s.hitch(this,this.validateServiceName)),this.filterObjects=[{layer:"inputLayer",select:this._analysisSelect,expressionObj:"attributeExprObj"}],this._buildUI()},startup:function(){},_onClose:function(e){this._aspectHandle&&(this._aspectHandle.remove(),this._aspectHandle=null),e&&(this._save(),this.emit("save",{save:!0})),this.emit("close",{save:e})},_handleSaveBtnClick:function(){if(this._form.validate()||this._timeBoundarySelect.validate()){this._saveBtn.set("disabled",!0);var e={};e.jobParams=this._buildJobParams(),e.itemParams={description:h.substitute(this.i18n.itemDescription,{inputLayername:this.inputLayer.name}),tags:h.substitute(this.i18n.itemTags,{inputLayername:this.inputLayer.name}),snippet:this.i18n.itemSnippet},this.showSelectFolder&&(e.itemParams.folder=this.get("folderId")),this.showGeoAnalyticsParams&&(e.isSpatioTemporalDataStore=!0),console.log(e),this.execute(e)}},_handleShowCreditsClick:function(e){e.preventDefault();var t=this._buildJobParams();(this._form.validate()||this._timeBoundarySelect.validate())&&this.getCreditsEstimate(this.toolName,t).then(s.hitch(this,(function(e){this._usageForm.set("content",e),this._usageDialog.show()})))},_buildJobParams:function(){var e={},t=this.constructAnalysisInputLyrObj(this.inputLayer,!0);return e.inputLayer=t,e.fieldName=this._fieldsSelect.get("value"),e.dataType=this._fieldTypeSelect.get("value"),e.trackAware=this._expTrackAwareCheck.get("checked"),this._expTrackAwareCheck.get("checked")&&(e.trackFields=this._trackFieldSelect.get("value").toString(),this._timeIntervalCheck.get("checked")&&this._timeBoundarySelect.get("timeBoundarySplit")&&(e.timeBoundarySplit=this._timeBoundarySelect.get("timeBoundarySplit"),e.timeBoundarySplitUnit=this._timeBoundarySelect.get("timeBoundarySplitUnit"),this._timeBoundarySelect.get("timeBoundaryReference")&&(e.timeBoundaryReference=this._timeBoundarySelect.get("timeBoundaryReference"),this.timeBoundaryReferenceType=this._timeBoundarySelect.get("timeBoundaryReferenceType")))),this._calcExprInput.get("value")&&(e.expression=this._calcExprInput.get("value")),this.returnFeatureCollection||(e.OutputName=n.toJson({serviceProperties:{name:this._outputLayerInput.get("value")}})),this.showChooseExtent&&this._useExtentCheck.get("checked")&&(e.context=n.toJson({extent:this.map.extent._normalize(!0)})),this._updateJobFilterAndSelection(e)},_handleBrowseItemsSelect:function(e,t){e&&e.selection&&N.addAnalysisReadyLayer({item:e.selection,layers:this.inputLayers,layersSelect:this._analysisSelect,browseDialog:e.dialog||this._browsedlg,widget:this},t).always(s.hitch(this,(function(){this._handleAnalysisLayerChange(this._analysisSelect.get("value"))})))},_save:function(){},_buildUI:function(){var e=!0;N.initHelpLinks(this.domNode,this.showHelp),this.get("showSelectAnalysisLayer")&&(this.inputLayers&&this.inputLayer&&!N.isLayerInLayers(this.inputLayer,this.inputLayers)&&this.inputLayers.push(this.inputLayer),this.get("inputLayer")||!this.get("inputLayers")||this.rerun||this.set("inputLayer",this.inputLayers[0]),N.populateAnalysisLayers(this,"inputLayer","inputLayers")),N.addReadyToUseLayerOption(this,[this._analysisSelect]),this.outputLayerName&&(this._outputLayerInput.set("value",this.outputLayerName),e=!1),d.set(this._chooseFolderRow,"display",!0===this.showSelectFolder?"block":"none"),this.showSelectFolder&&this.getFolderStore().then(s.hitch(this,(function(e){this.folderStore=e,N.setupFoldersUI({folderStore:this.folderStore,folderId:this.folderId,folderName:this.folderName,folderSelect:this._webMapFolderSelect,username:this.portalUser?this.portalUser.username:""})}))),d.set(this._chooseExtentDiv,"display",!0===this.showChooseExtent?"inline-block":"none"),d.set(this._showCreditsLink,"display",!0===this.showCredits?"block":"none"),this.outputLayerName&&(this._outputLayerInput.set("value",this.outputLayerName),e=!1),this.rerun&&(e=!1),this._updateAnalysisLayerUI(e),this.timeBoundarySplit&&(this._timeBoundarySelect.set("timeBoundarySplit",this.timeBoundarySplit),this._timeBoundarySelect.set("timeBoundarySplitUnit",this.timeBoundarySplitUnit),this._timeIntervalCheck.set("checked",!0)),this.timeBoundaryReference&&(this._timeBoundarySelect.set("timeBoundaryReferenceType",this.timeBoundaryReferenceType),this._timeBoundarySelect.set("timeBoundaryReference",this.timeBoundaryReference)),this._handleTimeIntervalCheckChange(this._timeIntervalCheck.get("value")),this._createFilterAndSelections(),this._loadConnections()},_loadConnections:function(){this.on("start",s.hitch(this,"_onClose",!0)),this._connect(this._closeBtn,"onclick",s.hitch(this,"_onClose",!1))},_handleAnalysisLayerChange:function(e){this._isAnalysisSelect=!0,"browse"===e||"browselayers"===e?this._createBrowseItems({browseValue:e,disabledSubResources:[this.inputLayer]},{layerTypes:[]},this._analysisSelect):this.inputLayer!==this.inputLayers[e]&&(this.inputLayer=this.inputLayers[e],this._updateAnalysisLayerUI(!0))},_updateAnalysisLayerUI:function(e){var i;if(this._expBtn.set("disabled",!this.inputLayer),this._calcExprInput.set("disabled",!this.inputLayer),this.inputLayer)if(i=N.isTimeInstantLayer(this.inputLayer),this._expTrackAwareCheck.set("disabled",!i),y.toggle(this._expTrackAwareLabel,"esriSelectLabel",i),y.toggle(this._expTrackAwareLabel,"esriAnalysisTextDisabled",!i),y.toggle(this._trackAwareFieldsLabel,"esriAnalysisTextDisabled",!i),N.addAttributeOptions({selectWidget:this._trackFieldSelect,layer:this.inputLayer,allowStringType:!0,allowSelectLabel:!1}),this._expTrackAwareCheck.reset(),!e&&this.trackFields&&this.trackFields.length>0&&(this._trackFieldSelect.set("value",this.trackFields),this._expTrackAwareCheck.set("checked",!0)),this._handleExpTrackAwareChange(this._expTrackAwareCheck.get("checked")),N.addAttributeOptions({selectWidget:this._fieldsSelect,layer:this.inputLayer,allowDateType:!0,allowStringType:!0,allowSelectLabel:!1}),this._fieldsSelect.addOption([{type:"separator",value:""},{value:"ADD_FIELD",label:this.i18n.addFieldTitle}]),!e&&this.fieldName&&(a.some(this.inputLayer.fields,(function(e){return e.name===this.fieldName}))||this._addNewFieldToFieldSelect(this.fieldName),this._fieldsSelect.set("value",this.fieldName)),e||(this.dataType&&this._fieldTypeSelect.set("value",this.dataType),this.expression&&this._calcExprInput.set("value",this.expression)),e&&(this.outputLayerName=h.substitute(this.i18n.outputLayerName,{inputLayername:this.inputLayer.name}),this._outputLayerInput.set("value",this.outputLayerName)),this._calcField)this._calcField&&this._calcField.layer!==this.inputLayer&&(this._calcExprInput.set("value",""),this._calcField.reset(),this._calcField.set("layer",this.inputLayer));else{var l=N.getExprFunctions();this._calcField=new U({expressionMode:U.MODE_ARCADE,arcadeEditor:this.arcadeEditor,arcadeProfileType:"geoanalyticstrack",map:this.map,layer:this.inputLayer,field:this.i18n.bufField,baseClass:"esriBufFieldExp",helperMethods:l,showHelp:!0,helpUrl:N.getHelpUrl({widget:this,topic:"BufferExpression"}),css:{base:"esriBufFieldExp",addButton:"btn calcite primary",closeButton:"btn calcite cancel"},helperType:"numeric",showHeader:!1,calculateLabel:this.i18n.add,expression:this.expression||null},this._expressionCtr),this._calcField.startup(),this._calcField.expressionMode===U.MODE_SQL?(d.set(this._calcField._validateBtn.domNode,"display","none"),this._calcField._handleHelperTypeChange("value",null,{functionType:"NumType"}),this._aspectHandle=t.around(this._calcField,"_handleAddFieldButtonClick",s.hitch(this,(function(e){return s.hitch(this,(function(e,t){var i=this._calcField.get("expression")[0];this._calcExprInput.set("value",i.sqlExpression),this._exprDialog.hide()}))})))):this._calcField.expressionMode===U.MODE_ARCADE&&this._calcField.on("expression-add",s.hitch(this,(function(e){this._calcExprInput.set("value",e.expression)}))),this._calcField.on("close",s.hitch(this,(function(){this._exprDialog.hide()})))}},_handleFieldChange:function(e){"ADD_FIELD"===e&&this._addDialog.show()},_addNewFieldToFieldSelect:function(e){var t=this._fieldsSelect.getOptions(),i=t.splice(t.length-2,2);this._fieldsSelect.removeOption(i),i.unshift({value:e,label:e,selected:!0}),this._fieldsSelect.addOption(i),this._fieldsSelect.set("value",e)},_handleAddFieldButtonClick:function(){this._addNewFieldToFieldSelect(this._nameInput.get("value")),this._addDialog.hide()},_handleCloseAddFieldButtonClick:function(){this._nameInput.reset(),this._addDialog.hide(),this._fieldsSelect.reset()},_handleExpBtnClick:function(){this._calcField.set("expression",this._calcExprInput.get("value")),this._calcField.reset(),this._exprDialog.show()},_handleExpTrackAwareChange:function(e){var t=N.isTimeInstantLayer(this.inputLayer),i=!(t&&e),s=!(t&&e&&this._trackFieldSelect.get("value").length>0);if(this._trackFieldSelect.set("disabled",i),y.toggle(this._trackAwareFieldsLabel,"esriAnalysisTextDisabled",i),this._timeIntervalCheck.set("disabled",s),y.toggle(this._timeIntervalSplitLabel,"esriAnalysisTextDisabled",s),!this._trackFieldSelect.get("disabled")){var l=this._trackFieldSelect.getOptions();a.forEach(l,(function(e){e.disabled=!1}),this),this._trackFieldSelect.updateOption(l)}},_handleTimeIntervalCheckChange:function(e){this._timeBoundarySelect.set("disabled",!e)},_handleTrackFieldSelectChange:function(){var e=!(N.isTimeInstantLayer(this.inputLayer)&&this._expTrackAwareCheck.get("checked")&&this._trackFieldSelect.get("value").length>0);this._timeIntervalCheck.set("disabled",e),y.toggle(this._timeIntervalSplitLabel,"esriAnalysisTextDisabled",e)},_setAnalysisGpServerAttr:function(e){e&&(this.analysisGpServer=e,this.set("toolServiceUrl",this.analysisGpServer+"/"+this.toolName))},_setDisableRunAnalysisAttr:function(e){this._saveBtn.set("disabled",e)},validateServiceName:function(e){return N.validateServiceName(e,{textInput:this._outputLayerInput,isItem:!this.returnFeatureCollection})},_setInputLayersAttr:function(e){E.isDefined(e)&&(this.inputLayers=e)},_setInputLayerAttr:function(e){this.inputLayer=e},_getInputLayerAttr:function(){return this.inputLayer},_setPrimaryActionButttonClassAttr:function(e){this.primaryActionButttonClass=e},_getPrimaryActionButttonClassAttr:function(){return this.primaryActionButttonClass},_connect:function(e,t,i){this._pbConnects.push(l.connect(e,t,i))}});return r("extend-esri")&&s.setObject("dijit.analysis.CalculateField",J,j),J}));