define([ "dojo/_base/declare", "dojo/_base/lang", "dojo/dom", "dojo/dom-construct", "dojo/promise/all", "dojo/date/locale", "dojo/number", "dojo/html", "dojo/topic", "dojo/date/locale", "dijit/_WidgetBase", "dijit/_TemplatedMixin", "esri/tasks/QueryTask", "esri/tasks/query", "dojo/text!./templates/Footer.html" ], function (declare, lang, dom, domConstruct, promisesAll, dateLocale, number, html, topic, dateLocale, _WidgetBase, _TemplatedMixin, QueryTask, Query, template) { return declare("app.layout.Footer", [_WidgetBase, _TemplatedMixin], { templateString: template, config: null, totalOutages: 0, totalCustomers:0, bannerDateTimeStamp:0, maxRecordsPerRequest: 1000, fieldCustomersAffected: null, fieldBannerDateTimeStampField: null, minutesToWait: 1, extent: null, timerId: null, bannerQueryTask: null, postCreate: function () { this.inherited(arguments); this._attachEventHandlers(); this.initUI(this.config); // this._updateSummaryInfo(); this.minutesToWait = this.config.outageLayers[0].outageLayerRefreshMinutes; clearInterval(this.timerId); this.timerId = setInterval(lang.hitch(this, this._updateSummaryInfo), this.minutesToWait * 60 * 1000); }, _attachEventHandlers: function () { var self = this; this.own( topic.subscribe("mapExtentChange", lang.hitch(this, function(ext) { console.log("mapExtentChanged subscriber" + ext); // [PA]: check if any extent at all if (ext.extent) { this.extent = ext.extent; this._updateSummaryInfo(); } })) ); }, initUI: function(config) { if(config.footerLinks&&config.footerLinks.length > 0) { this.initLinks(config.footerLinks); } if(config.smsLinks&&config.smsLinks.length > 0) { this.smsLinksNode.style.display = ""; this.initLinks(config.smsLinks, "sms"); } }, initLinks: function(links, linkType) { if(!links) return; for(var i = 0; i < links.length; i++) { this._createLink(links[i], linkType); } }, _createLink: function(link, linkType) { if(!link) return; var a = domConstruct.create("a", { href: link.url || "#", title: link.title || "" }); var aText, targetNode; if(linkType === "sms") { a.className = "social-button"; aText = document.createElement("i"); aText.className = "fa"; if(link.type === "facebook") { aText.className += " fa-facebook-square"; } else if(link.type === "twitter") { aText.className += " fa-twitter-square"; } targetNode = this.smsLinksNode; } else { aText = document.createTextNode(a.title); targetNode = this.LinksNode; } a.appendChild(aText); targetNode.appendChild(a); }, _showSummaryInfo:function(outages, customers, date) { var strSummary = "As of XX/XX/XXXX hh:mm am/pm, the current view of the map shows X outages affecting X customers."; strSummary = strSummary.replace("{1}", outages); strSummary = strSummary.replace("{2}", customers); }, _updateSummaryInfo:function() { clearInterval(this.timerId); var promises = []; this.fieldCustomersAffected = this.config.outageLayers[0].customerCountField; this.totalCustomers = 0; var url = this.config.outageLayers[0].url; //dojo.html.set(this.statisticsDiv, ""); var maxRecordsPerRequest = 1000; var query = new Query(); var bannerDateTimeStampField = ""; query.outFields = ['*']; query.returnGeometry = false; query.where = this.config.outageLayers[0].queryWhere || "1=1"; if(this.extent != null) { query.geometry = this.extent; } var queryResult = new QueryTask(url).executeForIds(query); //this.promises.push(queryResult); queryResult.then(lang.hitch(this, function(promises, results) { if(results == null) { this.totalOutages = 0; this._updateSummaryInfo0(promises); return; } this.totalOutages = results.length; var startIndex = 0; var count = this.maxRecordsPerRequest; var promises = []; var numRequests = Math.ceil(this.totalOutages/maxRecordsPerRequest); for(var i = 0; i < numRequests; i++) { var query = new Query(); query.outFields = ['*']; query.returnGeometry = false; query.where = this.config.outageLayers[0].queryWhere || "1=1"; query.num = maxRecordsPerRequest; query.start = maxRecordsPerRequest * i; if(this.extent != null) { query.geometry = this.extent; } var queryResult = new QueryTask(url).execute(query); promises.push(queryResult); } this._updateSummaryInfo0(promises); }, promises)); }, _updateSummaryInfo0:function(promises) { var query = new Query(); query.outFields = ['*']; query.returnGeometry = false; query.where = "1=1"; if (this.bannerQueryTask == null) { this.bannerQueryTask = new QueryTask(this.config.bannerServiceUrl); } var queryResult = this.bannerQueryTask.execute(query); queryResult.then(lang.hitch(this,function(results) { }), function(err) { console.error(err.message); }); promisesAll(promises).cancel(); promises.push(queryResult); this.bannerDateTimeStamp = null; console.log(promises); promisesAll(promises).then(lang.hitch(this, function( results) { this.totalCustomers = 0; console.log("Results for all "+ results.length); for(var k = 0; k < results.length; k++) { var result = results[k]; console.log("Features length" + result.features.length); console.log("Total customers so far = " + this.totalCustomers); for(var l = 0; l < result.features.length; l++) { var feature = result.features[l]; if(this.fieldCustomersAffected in feature.attributes) { this.totalCustomers += feature.attributes[this.fieldCustomersAffected]; console.log(this.totalCustomers); } else if (this.config.bannerDateTimeStampField in feature.attributes) { this.bannerDateTimeStamp = feature.attributes[this.config.bannerDateTimeStampField]; console.log("Updating time " + this.bannerDateTimeStamp); } } } this.updateStaticText(this.totalOutages, this.totalCustomers, this.bannerDateTimeStamp, this.minutesToWait); })); }, updateStaticText:function(totalOutages, totalCustomers, bannerUpdateTime, minutesToWait) { clearInterval(this.timerId); this.timerId = setInterval(lang.hitch(this, this._updateSummaryInfo), this.minutesToWait * 60 * 1000); var totalOutagesCommas = number.format(totalOutages); var totalCustomersCommas = number.format(totalCustomers); if (typeof(bannerUpdateTime) == 'undefined' || bannerUpdateTime === null) { //currentdate = new Date(); } else { //"2016-06-08 00:36:56" currentdate = dateLocale.parse(bannerUpdateTime, { datePattern: 'yyyy-MM-dd HH:mm:ss', selector: 'date' }); //currentdate= new Date(bannerUpdateTime); var t = currentdate; currentdate = new Date(Date.UTC(t.getFullYear(), t.getMonth(), t.getDate(), t.getHours(), t.getMinutes(), t.getSeconds())); } if (typeof(currentdate) != 'object' || typeof(currentdate.getTime()) != 'number' || currentdate.getTime() <= 0) { var statsText = "