define([ "dojo/_base/declare", "dojo/_base/lang", "dojo/_base/array", "dojo/dom", "dojo/on", "dojo/topic", "dojo/query", "dojo/dom-class", "dojo/dom-style", "dojo/dom-construct", "config/config.js", "app/Map", "app/layout/Header", "app/layout/Footer", "app/layout/TabContentPane", "app/layout/Sidebar", "app/widgets/Legend", "app/widgets/LayerList", "app/widgets/Locations", "app/widgets/Reports", "app/widgets/Help", "app/widgets/SplashPage", "app/widgets/MapSplash", "esri/tasks/QueryTask", "esri/tasks/query", "esri/request", "esri/config" ], function ( declare, lang, arrayUtil, dom, on, topic, query, domClass, domStyle, domConstruct, appConfig, Map, Header, Footer, TabContentPane, Sidebar, Legend, LayerList, Locations, Reports, Help, SplashPage, MapSplash, QueryTask, Query, esriRequest, esriConfig ) { return declare(null, { appConfig: appConfig, Map: null, TabContentPane: null, Sidebar: null, sidebarWidgets: {}, selectedWidget: null, splashPage: null, initApp: function() { // cache buster function myCallbackFunction(ioArgs) { console.log(ioArgs.url, ioArgs.content); ioArgs.content = ioArgs.content || {}; ioArgs.content.cacheBuster = Math.random(); return ioArgs; } esriRequest.setRequestPreCallback(myCallbackFunction); esriConfig.defaults.io.proxyUrl = this.appConfig.proxyUrl; esriConfig.defaults.io.alwaysUseProxy = this.appConfig.alwaysUseProxy; this._initRemoteConfigCheck(); if (appConfig.corsEnabledServers) { arrayUtil.forEach(appConfig.corsEnabledServers, function (corsServer) { esriConfig.defaults.io.corsEnabledServers.push(corsServer); }); } }, initApp0: function () { document.title = this.appConfig.app.company + " - " + this.appConfig.app.name; // initialize layout new Header({config: this.appConfig}, "header"); new Footer({config: this.appConfig}, "footer"); this.TabContentPane = new TabContentPane({}, "TabContent"); //initialize map this.Map = new Map({ config: this.appConfig }); //initialize sidebar this.Sidebar = new Sidebar({ config: this.appConfig }, "Sidebar"); // //init messages/warnings // processMessages(config); // attach event handlers this._initListners(); this._initIsNativeAppCheck(); this._checkSplashPage(this.appConfig); if(document.getElementsByTagName("BODY")[0].clientWidth > 768) { this.selectPanel("legendTab", "legend"); } }, _initRemoteConfigCheck:function(){ query = new Query(); query.where="1=1"; query.outFields = ["*"]; var queryResult = new QueryTask(appConfig.appModeUrl).execute(query); queryResult.then(lang.hitch(this,function(results) { console.log(results.features); arrayUtil.forEach(results.features,lang.hitch(this,function(item,index){ var key = item.attributes["Key"]; var value = item.attributes["Value"]; try{ value=JSON.parse(value); } catch(e) { } if(value != null) { this.appConfig[key] = value; } })); this.initApp0(); }), function(err) { alert('Error getting remote config!'); }); }, _checkSplashPage: function(config) { if (config.Splash_Status != null && config.Splash_Status != "" && config.Splash_Status.toLowerCase() != "hidden") { if(this.splashPage == null) { var splashConfig = { mode: config.Splash_Status, title: config.Splash_Title, content: config.Splash_Body }; this.splashPage = new SplashPage({config:splashConfig},"splashPage"); } } else { domStyle.set(dom.byId("splashPage"), { visibility: "hidden", display: "none"}); topic.publish("splashDone",{}); } }, _initIsNativeAppCheck:function(){ var uri = location.href; var query = uri.substring(uri.indexOf("?") + 1, uri.length); if (query == "from=nativeapp"){ domClass.add(document.getElementsByTagName("BODY")[0], "nativeAppMode"); } }, _initListners: function () { var self = this; var bodyTag = document.getElementsByTagName("BODY")[0]; topic.subscribe("Sidebar/Selected", function(args) { self.selectPanel(args.id, args.title); }); on(dom.byId("searchbarPlaceholder"), "click", function (e) { var targetNode = document.getElementById("searchbar"); domClass.remove(targetNode, "hidden-in-mobile"); document.getElementById("search_input").focus(); domClass.add(bodyTag, "has-search-open"); }); on(dom.byId("SearchCloseButton"), "click", function (e) { var targetNode = document.getElementById("searchbar"); domClass.add(targetNode, "hidden-in-mobile"); document.getElementById("search_input").blur(); domClass.remove(bodyTag, "has-search-open"); }); //dojo.connect(document.getElementById("AlertBannerClose"), "onclick", function (e) { // var targetNode = document.getElementById("AlertBanner"); // targetNode.style.display = "none"; // document.getElementById("page-wrap").className = ""; // map.resize(); //}); }, selectPanel: function(panelId, panelTitle) { var widget; switch(panelId) { case "legendTab": widget = this.sidebarWidgets.legend; if(!widget) { widget = this.sidebarWidgets.legend = this._createLegend(this.appConfig); } break; case "layerTab": widget = this.sidebarWidgets.layerlist; if(!widget) { widget = this.sidebarWidgets.layerlist = this._createLayerList(this.Map, this.appConfig); } break; case "locationsTab": widget = this.sidebarWidgets.locations; if(!widget) { widget = this.sidebarWidgets.locations = this._createLocations(this.Map, this.appConfig); } break; case "ReportsTab": widget = this.sidebarWidgets.reports; if(!widget) { widget = this.sidebarWidgets.reports = this._createReports(this.Map, this.appConfig); } break; case "HelpTab": widget = this.sidebarWidgets.help; if(!widget) { widget = this.sidebarWidgets.help = this._createHelp(this.appConfig); } break; default: } if(this.selectedWidget) this.selectedWidget.wrapper.style.display = "none"; widget.wrapper.style.display = "block"; this.selectedWidget = widget; this.TabContentPane.updateTitle(panelTitle); if(this.Sidebar) this.Sidebar.updateSelectedTab(panelId); }, __createWidgetWrapper: function(widget) { if(widget && widget.domNode) { var widgetWrapper = widget.wrapper = domConstruct.create("DIV"); widgetWrapper.appendChild(widget.domNode); this.TabContentPane.addChild(widgetWrapper); } }, _createLegend: function(config) { var legendWidget; legendWidget = new Legend({config: config}); this.__createWidgetWrapper(legendWidget); return legendWidget; }, _createLayerList: function(map, config) { var layerlistWidget; layerlistWidget = new LayerList({map: map, config: config}); this.__createWidgetWrapper(layerlistWidget); return layerlistWidget; }, _createLocations: function(map, config) { var locationsWidget; locationsWidget = new Locations({map: map, config: config}); this.__createWidgetWrapper(locationsWidget); locationsWidget.updateAddNewText("Save Location"); return locationsWidget; }, _createReports: function(map, config) { var reportsWidget; reportsWidget = new Reports({map: map, config: config}); this.__createWidgetWrapper(reportsWidget); return reportsWidget; }, _createHelp: function(config) { var helpWidget; helpWidget = new Help({config: config}); this.__createWidgetWrapper(helpWidget); return helpWidget; } } ); } );