define([
"dojo/_base/declare",
"dojo/on",
"dojo/query",
"dojo/request",
"dojo/dom-class",
"dojo/dom-construct",
"dijit/_WidgetBase",
"dijit/Dialog",
"dojo/store/Memory",
"dojo/data/ObjectStore",
"dojo/store/Observable",
"dgrid/OnDemandGrid",
"dgrid/Selection",
"dgrid/Keyboard",
"dojo/DeferredList",
"dojo/_base/Deferred"
], function (declare, on, query, request, domClass, domConstruct,
_WidgetBase,
Dialog,
Memory,ObjectStore, Observable,
OnDemandGrid, Selection, Keyboard,
DeferredList, Deferred) {
return declare("app.widgets._reportDialog", [_WidgetBase], {
config: null,
_dialog: null,
_dGrid: null,
_datastore: null,
rowId: 1,
Content: null,
postCreate: function () {
this.inherited(arguments);
this._createDgrid();
this._createDialog();
},
_createDialog:function() {
if(!this.config) return;
var self = this;
var title = this.config.type;
var content = this.Content = domConstruct.create("DIV", {
"class": "report-table-outer"
});
if(this._dGrid) {
content.appendChild(this._dGrid.domNode);
}
this._dialog = new Dialog({
title: title,
content: content
});
var bodyTag = document.getElementsByTagName("body")[0];
this._dialog.on("show", function() {
domClass.add(bodyTag, "has-dialog-open");
self._updateData();
});
this._dialog.on("hide", function() {
domClass.remove(bodyTag, "has-dialog-open");
});
},
show: function() {
if(this._dialog) {
this._dialog.show();
if(this._dGrid) {
this._dGrid.resize();
}
}
},
hide: function() {
if(this._dialog) {
this._dialog.hide();
}
},
showLoading: function() {
if(!this.LoadingDom) {
this.LoadingDom = domConstruct.create("DIV", {
"class": "loading",
"innerHTML": "Loading..."
});
if(this.Content)
this.Content.appendChild(this.LoadingDom);
}
this.LoadingDom.style.display = "block";
},
hideLoading: function() {
if(this.LoadingDom)
this.LoadingDom.style.display = "none";
},
_getGridColumnDefination: function () {
if(!this.config.gridColumns) return;
var columns = this.config.gridColumns;
return columns;
},
_updateData: function() {
this.rowId = 1;
this.showLoading();
var self = this;
var type = this.config.type;
var rootServiceUrl = this.config.reportServiceUrlPath;
if(type === "county") {
this._requestData([rootServiceUrl]);
}
else if(type === "municipality/township") {
this._requestServiceUrls(rootServiceUrl).then(function(urls) {
self._requestData(urls);
});
} else if(type === "town") {
this._requestServiceUrls(rootServiceUrl).then(function(countyUrls) {
for(var i = 0; i < countyUrls.length; i++) {
var countyServiceUrl = countyUrls[i];
self._requestServiceUrls(countyServiceUrl).then(function(townUrls) {
self._requestData(townUrls);
});
}
});
} else if(type === "street") {
this._requestServiceUrls(rootServiceUrl).then(function(countyUrls) {
for(var i = 0; i < countyUrls.length; i++) {
var countyServiceUrl = countyUrls[i];
self._requestServiceUrls(countyServiceUrl).then(function(townUrls) {
for(var ii = 0; ii < townUrls.length; ii++) {
var townServiceUrl = townUrls[ii];
self._requestServiceUrls(townServiceUrl).then(function(streetUrls) {
self._requestData(streetUrls);
});
}
});
}
});
}
this.hideLoading();
// var query = new esri.tasks.Query();
// query.outFields = ['*'];
// query.returnGeometry = false;
// if (curState == null || curState.length == 0)
// query.where = "1=1";
// else
// query.where = "State='" + curState + "'";
// var queryResult = new esri.tasks.QueryTask(serviceUrl).execute(query);
// queryResult.then(
// function (featureSet) {
// dojo.forEach(
// featureSet.features,
// function (feature) {
// if (feature.attributes[config.reportCustomerOutField] > 0) {
// var x = feature.attributes[config.reportTotalCustomerField];
// var totalCust = x === 0 ? '-' : x;
// console.log('value', x);
// console.log(totalCust);
// console.log(x === 0);
// reportdata.push([feature.attributes[fieldName], (totalCust), (feature.attributes[config.reportCustomerOutField])]);
// }
// })
// //var dataitems = {
// // items: reportdata
// //};
// //datastore = new dojo.data.ItemFileReadStore({
// // data: dataitems
// //});
// gridDijit.renderArray(reportdata);
// dlg.set('title', dlgTitle);
// dlg.show();
// gridDijit.startup();
// gridDijit.resize();
// });
},
_requestServiceUrls: function(serviceUrl) {
var deferred = new Deferred();
var r = request(_Controller.appConfig.proxyUrl + "?" + this.config.reportServiceUrlOrigin + serviceUrl);
r.then(function (response) {
if (response) {
var htmlDom = document.createElement( 'html' );
htmlDom.innerHTML = response;
var newServiceUrls = query("tr:not(.firstRow) td a", htmlDom).map(function(link) {
return link.getAttribute("href");
});
deferred.resolve(newServiceUrls);
} else {
console.error(serviceUrl + " service returns nothing.");
}
}, function (err) {
console.error(err.message);
});
return deferred;
},
_requestData: function(urls) {
var self = this;
for(var i = 0; i' + "No Data" + ''
}, this.dgridContainer);
// Attach events
this._dGrid.on("dgrid-select", function (e) {
// Get the rows that were just selected
var selectedRow = e.rows[0];
});
this._dGrid.startup();
},
_parseReportHTML: function(htmlString, columns) {
var reportData = {
data: []
};
var dataStartIndex = 3; // NOTE: this is a hard coded number based on IUSA's report page
var htmlDom = document.createElement( 'html' );
htmlDom.innerHTML = htmlString;
var tableRows = htmlDom.getElementsByTagName("TR");
if(tableRows.length) {
for(var i = dataStartIndex; i < tableRows.length; i++) {
var row = tableRows[i];
if(row && row.children.length >= 3) {
var rowData = {};
rowData[columns[0].field] = this.rowId; // "id"
rowData[columns[1].field] = null; // "County" || "Municipality/Township" || "Town" || "Street"
rowData[columns[2].field] = null; // "customersTotal"
rowData[columns[3].field] = null; // "customersAffected"
rowData[columns[4].field] = null; // "estimatedRestoration"
for(var j = 0; j < row.children.length; j++) {
if(row.children[j].tagName === "TD") {
if(j === 0)
rowData[columns[1].field] = row.children[j].textContent;
else if(j === 1)
rowData[columns[2].field] = row.children[j].textContent;
else
rowData[columns[3].field] = row.children[j].textContent;
}
}
if(row.children[0].tagName === "TD")
reportData.data.push(rowData);
this.rowId++;
}
}
}
console.log(reportData);
return reportData;
},
__numberFormatter: function(value) {
if (value === '-') {
return value;
}
if (!isNaN(parseFloat(value)) && isFinite(value)) {
return dojo.number.format(value, {
locale: "en-us"
});
}
}
});
});