// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See http://js.arcgis.com/3.23/esri/copyright.txt for details.
//>>built
define("esri/dijit/AppProxySettings","require dojo/_base/array dojo/_base/declare dojo/_base/lang ../kernel dojo/uacss dijit/_WidgetBase ../arcgis/AppProxyManager ../arcgis/utils dojo/i18n!../nls/jsapi dojo/Deferred dojo/on dojo/promise/all dojo/query dojo/string dojo/dom-attr dojo/dom-class dojo/dom-construct dojo/store/Memory dijit/form/CheckBox dijit/form/Select dijit/form/NumberSpinner dgrid/OnDemandGrid dgrid/editor dojo/domReady!".split(" "),function(r,g,k,c,t,u,h,v,w,x,l,m,p,G,q,H,I,y,z,A,
B,C,D,n){function E(a){return"second"===a?1:"minute"===a?60:"hour"===a?3600:"day"===a?86400:0}var f=x.widgets.appProxySettings,F=k([B],{destroy:function(){this._destroyed||this._beingDestroyed?this.closeDropDown():this.inherited(arguments)}});h=k([h],{declaredClass:"esri.dijit.AppProxySettings",_creditDomains:[/demographics\w*\.arcgis\.com/i,/geoenrich\w*\.arcgis\.com/i,/route\w*\.arcgis\.com/i,/logistics\w*\.arcgis\.com/i,/analysis\w*\.arcgis\.com/i,/elevation\w*\.arcgis\.com/i],_premiumDomains:[/traffic\w*\.arcgis\.com/i,
/elevation\w*\.arcgis\.com/i,/geoenrich\w*\.arcgis\.com/i,/hydro\w*\.arcgis\.com/i,/naip\w*\.arcgis\.com/i,/livefeeds\w*\.arcgis\.com/i,/demographics\w*\.arcgis\.com/i,/landscape\w*\.arcgis\.com/i,/historical\w*\.arcgis\.com/i,/earthobs\w*\.arcgis\.com/i],defaults:{webmaps:[],proxyManagerOptions:{appid:""},proxies:[]},constructor:function(a){this._editing=!1;this.css={container:"esriAppProxySettings"};a=c.mixin({},this.defaults,a);this.set(a);this.appProxyManager=new v(this.proxyManagerOptions);if(this.appProxyManager.loaded)this._init();
else m.once(this.appProxyManager,"load",c.hitch(this,function(){this._init()}))},startup:function(){this.inherited(arguments);if(this.loaded)this._createTable();else m.once(this,"load",c.hitch(this,function(){this._createTable()}))},resize:function(){this._grid&&this._grid.resize()},_queryForSecureServices:function(a){return w.getItem(a).then(c.hitch(this,this._parseMap)).then(c.hitch(this,function(a){var b=this._mapsProxies;g.forEach(a,c.hitch(this,function(a){b.push(a)}));return a}))},_loaded:function(){this.set("loaded",
!0);this.emit("load")},_itemInApp:function(a,b){return g.some(this._mapsProxies,function(c){if(c[a]===b.url)return c.title=b.title,!0})},_parseUrl:function(a){var b=document.createElement("a");b.href=a;return b},_consumesCredits:function(a){return g.some(this._creditDomains,function(b){return-1<a.search(b)})},_isPremium:function(a){return g.some(this._premiumDomains,function(b){return-1<a.search(b)})},_checkItem:function(a){var b=new l,c=this._parseUrl(a.url);this._isPremium(c.host)?b.resolve({sourceUrl:a.url,
id:a.id,title:a.title,proxyId:null,proxied:!1}):b.resolve();return b.promise},_parseMap:function(a){if(a&&a.itemData){var b=[];g.forEach(a.itemData.operationalLayers,c.hitch(this,function(a){this._itemInApp("sourceUrl",a)||b.push(this._checkItem(a))}));return p(b).then(function(a){var b=[];g.forEach(a,function(a){a&&b.push(a)});return b})}a=new l;a.resolve();return a.promise},_getWebmapsProxies:function(){this._mapsProxies=this.proxies;for(var a=[],b=0,c=this.webmaps.length;b<c;b++)a.push(this._queryForSecureServices(this.webmaps[b]));
return p(a)},_webmapsChanged:function(){var a=new l;this.webmaps&&this.webmaps.length?this._getWebmapsProxies().then(c.hitch(this,function(){this.set("proxies",this._mapsProxies);a.resolve()})):a.resolve();return a.promise},_init:function(){var a=this.appProxyManager.proxies;g.forEach(a,c.hitch(this,function(a,c){if(!a.hasOwnProperty("title")){var b=this._parseUrl(a.sourceUrl);b&&b.host&&(a.title=q.substitute(f.untitled,{url:b.host}))}a.hasOwnProperty("proxied")||(a.proxied=!0,a.id="AppProxy"+c)}));
this.proxies=a;this.webmaps&&this.webmaps.length?this._webmapsChanged().then(c.hitch(this,function(){this._loaded()})):this._loaded()},_createTable:function(){this._memoryStore=new z({data:this.proxies});var a=y.create("div",{className:this.css.container},this.domNode);this._grid=new (k([D]))({store:this._memoryStore,columns:{proxied:n({label:"",field:"proxied",canEdit:c.hitch(this,this._canToggleProxied),editor:A,get:c.hitch(this,this._getProxied),formatter:c.hitch(this,this._getFieldValue),editorArgs:{value:!0}}),
title:{label:f.premiumContent,get:c.hitch(this,this._getTitle),formatter:function(a){return q.substitute(a,{url:'\x3cimg width\x3d"16" height\x3d"16" src\x3d"'+r.toUrl("../css/images/item_type_icons/premiumcredits16.png")+'" /\x3e'})}},requestLimit:n({label:f.requestLimit,field:"requestLimit",get:this._getHitsPerInterval,formatter:c.hitch(this,this._getFieldValue),canEdit:c.hitch(this,this._canUpdateField),editor:C,editorArgs:{constraints:{min:0},style:"width:75%;"}}),interval:n({label:f.interval,
field:"interval",get:this._getInterval,formatter:c.hitch(this,this._getFieldValue),canEdit:c.hitch(this,this._canUpdateField),editor:F,editorArgs:{style:"width:75%;",options:[{value:"none",label:f.intervalNone},{value:"second",label:f.intervalSecond},{value:"minute",label:f.intervalMinute},{value:"hour",label:f.intervalHour},{value:"day",label:f.intervalDay}]}})}},a);this._grid.startup();this.own(m(this._grid,"dgrid-datachange",c.hitch(this,function(a){function b(){this._editing=!1;this._grid.refresh()}
var d=a.cell,f=d&&d.column&&d.column.field;a=a.value;var e=d&&d.row&&d.row.data;f&&(e[f]=a);d={proxyId:e.proxyId,proxyUrl:e.proxyUrl,sourceUrl:e.sourceUrl,proxied:e.proxied,hitsPerInterval:this._getHitsPerInterval(e),intervalSeconds:E(e.interval)};d=d.proxied?d.proxyId?this.appProxyManager.updateProxy(d).then(c.hitch(this,function(a){a&&(this._updateProxy(e,a),this.emit("update-proxy",e))})):this.appProxyManager.createProxies([d]).then(c.hitch(this,function(a){a&&(this._updateProxy(e,a),this.emit("create-proxy",
e))})):this.appProxyManager.deleteProxies([d]).then(c.hitch(this,function(a){a&&(this._updateProxy(e,a),this.emit("delete-proxy",e))}));this._editing=!0;this._grid.refresh();d.then(c.hitch(this,b)).otherwise(c.hitch(this,b))})))},_getFieldValue:function(a){return this._editing?'\x3cspan class\x3d"esriAppProxyLoading"\x3e\x3c/span\x3e':a},_canUpdateField:function(a){return!this._editing&&this._getProxied(a)},_canToggleProxied:function(){return!this._editing},_updateProxy:function(a,b){this._memoryStore&&
g.some(b,c.hitch(this,function(b){if(b.sourceUrl===a.sourceUrl)return a=b=c.mixin(a,{proxied:a.proxied,proxyId:a.proxied?b.proxyId:void 0}),this._memoryStore.put(b,{overwrite:!0}),!0}))},_getTitle:function(a){var b=a.title;this._consumesCredits(a.sourceUrl)&&(b+=" ${url}");return b},_getProxied:function(a){return a&&!!a.proxied},_getInterval:function(a){var b;(b=a.interval)||(b=a.intervalSeconds,b=1===b?"second":60===b?"minute":3600===b?"hour":86400===b?"day":"none");return a&&b&&a.proxied?b:"none"},
_getHitsPerInterval:function(a){var b="number"===typeof a.requestLimit?a.requestLimit:a.hitsPerInterval;return a&&b&&a.proxied?b:0},_setWebmapsAttr:function(a){a=a.slice();this._set("webmaps",a);this._created&&this._memoryStore&&this._webmapsChanged()},_setProxiesAttr:function(a){a=a.slice();this._set("proxies",a);this._created&&this._memoryStore&&(this._memoryStore.setData(this.proxies),this._grid.set("store",this._memoryStore))}});u("extend-esri")&&c.setObject("dijit.AppProxySettings",h,t);return h});