// 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 dijit/_WidgetBase dijit/_TemplatedMixin dijit/_WidgetsInTemplateMixin dojo/Evented dojo/_base/lang dojo/_base/html dojo/_base/array dojo/Deferred dojo/promise/all dojo/store/Memory dojo/store/Observable dijit/tree/ObjectStoreModel jimu/utils jimu/dijit/_Tree jimu/dijit/LoadingIndicator".split(" "),function(t,u,v,w,x,e,q,k,m,p,y,z,A,B,C){return t([u,v,w,x],{templateString:'\x3cdiv style\x3d"width:100%;"\x3e\x3cdiv data-dojo-attach-point\x3d"shelter"  data-dojo-type\x3d"jimu/dijit/LoadingIndicator" data-dojo-props\x3d"hidden:true"\x3e\x3c/div\x3e\x3c/div\x3e',
_store:null,_id:0,_currentUrl:"",_treeClass:"service-browser-tree",_def:null,url:"",multiple:!1,rule:null,postMixInProperties:function(){this.nls=window.jimuNls.basicServiceBrowser},postCreate:function(){this.inherited(arguments);q.addClass(this.domNode,"jimu-basic-service-browser");this.multiple=!!this.multiple;this._createTree();this.url&&"string"===typeof this.url&&this.setUrl(this.url)},reset:function(){this.url="";this._clear()},getSelectedItems:function(){return this.tree.getSelectedItems()},
setUrl:function(a){this._def&&(this._def.isFulfilled()||this._def.cancel(),this._def=null);this._def=new m;a&&"string"===typeof a&&a.replace(/\/*$/g,"")||this._def.reject();a=a.replace(/\/*$/g,"");a=e.trim(a);var b=a.match(/^http(s?):\/\//gi);b&&0<b.length||(a="http://"+a);if(!(0>=a.search(/\/rest\/services/i))&&(this._clear(),this._currentUrl=a))return a=this._getRootItem(),a=B.isStringEndWith(this._currentUrl,"rest/services")?this._searchBaseServiceUrl(this._currentUrl,a):this._isUrlContainsServiceType(this._currentUrl)?
this._searchServiceUrl(this._currentUrl,a):this._searchFolderServiceUrl(this._currentUrl,a),this.shelter.show(),a.then(e.hitch(this,function(c){this.domNode&&this.shelter.hide();var d=this.tree.getAllLeafTreeNodeWidgets();1===d.length&&d[0].select();this._def.resolve(c)}),e.hitch(this,function(c){this.domNode&&this.shelter.hide();this._showRequestError();this._def.reject(c)})),this._def;this._def.reject()},_getItem:function(a){return this.rule.getItem(a)},_getSubItemUrls:function(a){return this.rule.getSubItemUrls(a)},
_getSubItems:function(a){var b=new m;this._getSubItemUrls(a).then(e.hitch(this,function(c){c=k.map(c,e.hitch(this,function(d){return this._getItem(d)}));p(c).then(e.hitch(this,function(d){d=k.filter(d,e.hitch(this,function(g){return g&&"object"===typeof g}));b.resolve(d)}),e.hitch(this,function(d){b.reject(d)}))}),e.hitch(this,function(c){b.reject(c)}));return b},_selectFirstLeafTreeNodeWidget:function(){var a=this.tree.getAllLeafTreeNodeWidgets();1===a.length&&a[0].select()},isLeafItem:function(a){return 0<=
this.rule.leafTypes.indexOf(a.type)},isServiceTypeSupported:function(a){return this.rule.isServiceTypeSupported(a)},_getStringEndWith:function(a,b){var c="",d=a.indexOf(b);0<=d&&(c=a.slice(0,d+b.length));return c},_isUrlContainsServiceType:function(a){return this.rule.isUrlContainsServiceType(a)},_getBaseServiceUrl:function(){return this._getStringEndWith(this._currentUrl,"rest/services")},_getServiceName:function(a){var b="";a=a.split("/");return b=a[a.length-1]},_searchBaseServiceUrl:function(a,
b){var c=new m;this._getRestInfo(a).then(e.hitch(this,function(d){if(this.domNode){var g=[];k.map(d.folders,e.hitch(this,function(f){var h={name:f,type:"folder",url:a+"/"+f,parent:b.id};this._addItem(h);var l=new m;this._doSearchFolderServiceUrl(h.url,h.id).then(e.hitch(this,function(n){0<n.length?k.forEach(n,e.hitch(this,function(r){r.parent=h.id;this._addItem(r)})):this._removeItem(h.id);l.resolve()}),e.hitch(this,function(n){l.reject(n)}));g.push(l);return l}));k.forEach(d.services,e.hitch(this,
function(f){if(this.isServiceTypeSupported(f.type)){f=a+"/"+f.name+"/"+f.type;var h=new m;this.rule.getItem(f).then(e.hitch(this,function(l){l&&(l.parent=b.id,this._addItem(l));h.resolve()}),e.hitch(this,function(l){console.error(l);h.reject(l)}));g.push(h)}}));p(g).then(e.hitch(this,function(){this.domNode&&c.resolve()}),e.hitch(this,function(f){console.error(f);this.domNode&&c.reject(f)}))}}),e.hitch(this,function(d){console.error(d);this.domNode&&c.reject({errorCode:"NETWORK_ERROR",error:d})}));
return c},_searchFolderServiceUrl:function(a,b){var c=new m;this._doSearchFolderServiceUrl(a,b).then(e.hitch(this,function(d){this.domNode&&(k.forEach(d,e.hitch(this,function(g){g.parent=b.id;this._addItem(g)})),c.resolve())}),e.hitch(this,function(d){console.error(d);this.domNode&&c.reject({errorCode:"NETWORK_ERROR",error:d})}));return c},_doSearchFolderServiceUrl:function(a){var b=new m,c=this._getBaseServiceUrl();this._getRestInfo(a).then(e.hitch(this,function(d){d=d.services;var g=[];d&&0<d.length&&
k.forEach(d,e.hitch(this,function(f){this.isServiceTypeSupported(f.type)&&(f=this.rule.getItem(c+"/"+f.name+"/"+f.type),g.push(f))}));p(g).then(e.hitch(this,function(f){f=k.filter(f,e.hitch(this,function(h){return h}));b.resolve(f)}),e.hitch(this,function(f){console.error(f);b.reject(f)}))}),e.hitch(this,function(d){console.error(d);b.reject(d)}));return b},_searchServiceUrl:function(a,b){var c=new m;this._getSubItems(a).then(e.hitch(this,function(d){d&&0<d.length?(k.forEach(d,e.hitch(this,function(g){g.parent=
b.id;this._addItem(g)})),c.resolve()):this._getItem(a).then(e.hitch(this,function(g){g&&(g.parent=b.id,this._addItem(g));c.resolve()}),e.hitch(this,function(g){c.reject(g)}))}),e.hitch(this,function(d){c.reject(d)}));return c},_getRestInfo:function(a){var b=new m;this.rule.getRestInfo(a).then(e.hitch(this,function(c){this.domNode&&b.resolve(c)}),e.hitch(this,function(c){this.domNode&&b.reject(c)}));return b},_clear:function(){var a=this._store.query({parent:"root"});k.forEach(a,e.hitch(this,function(b){b&&
"root"!==b.id&&this._store.remove(b.id)}))},_showRequestError:function(){this.emit("error",this.nls.invalidUrlTip)},_addItem:function(a){this._id++;a.id=this._id.toString();this._store.add(a);return a},_removeItem:function(a){this._store.remove(a)},_getRootItem:function(){return{id:"root",name:"Services Root",type:"root"}},_createTree:function(){var a=this._getRootItem();a=new y({data:[a],getChildren:function(b){return this.query({parent:b.id})}});this._store=new z(a);a=new A({store:this._store,query:{id:"root"},
mayHaveChildren:e.hitch(this,this._mayHaveChildren)});this.tree=new C({multiple:this.multiple,model:a,showRoot:!1,style:{width:"100%"},isLeafItem:e.hitch(this,this.isLeafItem),onOpen:e.hitch(this,function(b,c){this._onTreeOpen(b,c);this.emit("tree-open",b,c)}),onClick:e.hitch(this,function(b,c,d){this._onTreeClick(b,c,d);this.emit("tree-click",b,c,d)}),getIconStyle:e.hitch(this,function(b,c){var d=null;if(!b)return null;var g={width:"20px",height:"20px",backgroundRepeat:"no-repeat",backgroundPosition:"center center",
backgroundImage:""},f=window.location.protocol+"//"+window.location.host+require.toUrl("jimu"),h=this._getIconImageName(b,c);h||("folder"===b.type?h=c?"folder_open_default.png":"folder_close_default.png":this.isServiceTypeSupported(b.type)&&(h=c?"folder_open_default.png":"folder_close_default.png"));h&&(g.backgroundImage="url("+f+"/css/images/"+h+")",d=g);return d})});q.addClass(this.tree.domNode,this._treeClass);this.tree.placeAt(this.domNode)},_getIconImageName:function(a,b){var c="";"function"===
typeof this.rule.getIconImageName&&(c=this.rule.getIconImageName(a,b));return c},_mayHaveChildren:function(a){return"root"===a.type?!0:!this.isLeafItem(a)},_onTreeOpen:function(a,b){"root"===a.id||0<this._store.query({parent:a.id}).length||a.checking||a.checked||(a.checking=!0,this._getSubItems(a.url).then(e.hitch(this,function(c){k.forEach(c,e.hitch(this,function(d){d.parent=a.id;this._addItem(d)}));a.checking=!1;a.checked=!0}),e.hitch(this,function(c){console.error(c);a.checking=!1;a.checked=!0})))},
_onTreeClick:function(a,b,c){},destroy:function(){this.shelter&&(this.shelter.destroy(),this.shelter=null);this.inherited(arguments)}})});