Source: widgets/SpwIdentifyResultTable.js

Retour à la documentation
/**
 * @class spw.widgets.SpwIdentifyResultTable
 */
define(["dojo/_base/declare", "spw/api/SpwBaseTemplatedWidget", "dojo/dom-construct", "dojo/_base/array", "dojo/_base/lang",
        "dojo/dom-style","dojo/query","dojo/has",
        "spw/api/CollapserItem", "dojo/i18n!./nls/SpwIdentifyResultTable", "dojo/text!./templates/SpwIdentifyResultTable.html",
        "spw/api/MessageManager", "spw/api/Utils", "dojo/sniff", "dijit/form/CheckBox"],
        function(declare, SpwBaseTemplatedWidget, domConstruct, array, lang,
        		Style,Query,Has,
        		CollapserItem,labels, template, MessageManager, Utils) {

            return declare("spw.widgets.SpwIdentifyResultTable", [SpwBaseTemplatedWidget], /** @lends spw.widgets.SpwIdentifyResultTable.prototype */{
            	
            	templateString: template,
        		labels: labels,
            	
        		exportCsvServerUrl: null,
        		allowUseSelectionIdentify: true,
        		
        		_results: null,
        		
        		_spwLayer: null,
        		_moreInfoPageLink: null,
        		
        		_spwIdentifyResultRows: null,
        		_cbSelectAllResultsClicked: false,
        		
        		_spwIdentifyResultWidget: null,
        		_spwAdvancedIdentify: null,
        		
        		_resultRowWidgetClass: null,
        		_resultRowWidgetConfig: null,
        		
        		_resultRowWidgetLoaded: false,
        		
        		postCreate: function(){
            		this.inherited(arguments);
            		
            		this._spwIdentifyResultRows = new Array();
            		
            		array.forEach(this.widgets, lang.hitch(this, function(widget){
        				require([widget.className], lang.hitch(this, function(widgetClass){
        					lang.mixin(widget.config, {spwViewer: this.spwViewer});
        					this._resultRowWidgetConfig = widget.config;
        					this._resultRowWidgetClass = widgetClass;
        					this._resultRowWidgetLoaded = true;
        					this._postCreate();
        				}));
        			}));
            	},
            	
            	_postCreate: function() {
            		if(this._spwAdvancedIdentify == null) {
            			Style.set(this._imgUseAsIdentify, "display", "none");
            		}
            		if(!this._spwLayer.moreInfoPageBaseUrl || !this._spwLayer.moreInfoPageFieldName) {
            			Style.set(this._imgMoreInfo, "display", "none");
            		}
            		if(this._spwIdentifyResultWidget != null && this._spwIdentifyResultWidget.exportCsvServerUrl) {
            			Style.set(this._imgExportCsv, "display", "inline");
            		} else {
            			Style.set(this._imgExportCsv, "display", "none");
            		}
            		if(this._spwIdentifyResultWidget != null && !this._spwIdentifyResultWidget.allowUseSelectionIdentify) {
            			if(!this._spwIdentifyResultWidget.allowUseSelectionIdentify){
            				Style.set(Query(this._imgUseAsIdentify).parent("td")[0], "display", "none");
            			}
            		}
            		
            		this._tbody = domConstruct.create("tbody", {}, this.resultsTable);
            		
            		this._columnHeaders = [];
            		
            		if(this._spwLayer.fields && this._spwLayer.fields.length > 0) {
            			for(var i=0; i<this._spwLayer.fields.length; i++) {
            				this._columnHeaders[i] = this._spwLayer.fields[i].alias;
            				domConstruct.create("th", {innerHTML: this._columnHeaders[i]}, this._headerTr);
            			}
            		}
            		else {
            			var firstResult = this._results[0];
            			for (key in firstResult.feature.attributes) {
            				if(	key.toUpperCase().indexOf("SHAPE") != 0
            					&& key.toUpperCase().indexOf("SE_ANNO_CAD_DATA" ) != 0
            					&& key.toUpperCase().indexOf("OBJECTID") != 0) {
            					this._columnHeaders.push(key);
            					domConstruct.create("th", {innerHTML: key}, this._headerTr);
            				}
            			}
            		}
            		
            		this._addResults(this._results);
            		
            		if(Has("ie")) {
            			Style.set(this.footerDiv, "paddingBottom", "20px");
            		}
            	},
            	
            	addResults:function(results) {
            		if(results && results.length) {
            			var resultToAdd = new Array();
            			
            			if(!this._results) {
            				this._results = new Array();
            			}
            			
            			for(var i=0;i<results.length;i++){
            				var alreadyExists = false;
            				for(var j=0;j<this._results.length;j++) {
            					if(Utils.objectsEquals(results[i],this._results[j])) {
            						alreadyExists = true;
            						break;
            					}
            				}
            				if(!alreadyExists) {
            					resultToAdd.push(results[i]);
            					this._results.push(results[i]);
            				}
            			}
            			this._addResults(resultToAdd);
            		}
            	},
            	
            	_addResults:function(results) {
            		if(results && results.length > 0) {
            			//rows
            			for(var i=0;i<results.length; i++) {
            				
            				lang.mixin(this._resultRowWidgetConfig, {
            					_result: results[i],
            					_spwLayer: this._spwLayer,
            					_spwIdentifyResultTable: this
    	        			});
    	        			var spwIdentifyResultRow = new this._resultRowWidgetClass(this._resultRowWidgetConfig);
    	        			
            				domConstruct.place(spwIdentifyResultRow.domNode, this._tbody, "last");
            				this._spwIdentifyResultRows.push(spwIdentifyResultRow);
            			}
            		}
            		this.updateFooterText();
            	},
            	
            	updateFooterText: function() {
            		if(this.footerDiv) {
            			var cpt = this.countSelected();
            			if(this._spwIdentifyResultRows.length > 0) {
            				this.footerDiv.innerText = cpt + " " + this.labels.SpwIdentifyResultTableSelectedObjectsOn + " " + this._spwIdentifyResultRows.length;
            			}
            			else {
            				this.footerDiv.innerText = this.labels.SpwIdentifyResultTableNoResults;
            			}
            		}
            	},
            	
            	allSelected: function() {
            		return this.countSelected() == this._spwIdentifyResultRows.length;
            	},
            	
            	countSelected: function() {
            		var cpt = 0;
            		for(var i=0; i < this._spwIdentifyResultRows.length; i++) {
            			if(this._spwIdentifyResultRows[i].isSelected()) {
            				cpt++;
            			}
            		}
            		return cpt;
            	},
            	
            	onRowSelected: function(checked) {
            		if(!checked) {
            			this._cbSelectAllResults.set('checked', false);
            		}
            		else if(this.allSelected()) {
            			this._cbSelectAllResults.set('checked', true);
            		}
            		this.updateFooterText();
            	},
            	
            	deleteRow: function(spwIdentifyResultRow) {
            		for(var i=0; i < this._spwIdentifyResultRows.length; i++) {
            			if(this._spwIdentifyResultRows[i] == spwIdentifyResultRow) {
            				this._spwIdentifyResultRows[i].destroy();
            				this._spwIdentifyResultRows.splice(i, 1);
            				this.updateFooterText();
            				break;
            			}
            		}
            		if(this._spwIdentifyResultRows.length == 0) {
            			this._spwIdentifyResultWidget.removeResult(this);
            		}
            		else if(this.allSelected()) {
            			this._cbSelectAllResults.set('checked', true);
            		}
            	},
            	
            	onClickCbSelectAllResults: function() {
            		this._cbSelectAllResultsClicked = true;
            	},
            	
            	onChangeCbSelectAllResults: function(checked) {
            		if(this._cbSelectAllResultsClicked) {
            			for(var i=0; i < this._spwIdentifyResultRows.length; i++) {
            				this._spwIdentifyResultRows[i].setSelected(checked);
            			}
            		}
            		this._cbSelectAllResultsClicked = false;
            		this.updateFooterText();
            	},
            	
            	onClickImgDeleteAllResults: function() {
            		for(var i=this._spwIdentifyResultRows.length - 1; i >= 0; i--) {
            			this._spwIdentifyResultRows[i].destroy();
            			this._spwIdentifyResultRows.splice(i, 1);
            		}
            		this.updateFooterText();
            		this._spwIdentifyResultWidget.removeResult(this);
            	},
            	
            	onClickImgUseAsIdentify: function() {
            		var geoms = new Array();
            		for(var i=0; i < this._spwIdentifyResultRows.length; i++) {
            			if(this._spwIdentifyResultRows[i].isSelected()) {
            				geoms.push(this._spwIdentifyResultRows[i].getGeometry());
            			}
            		}
            		if(geoms.length == 0) {
            			MessageManager.getInstance().notifyInfo(this.labels.SpwIdentifyResultTableNoResultSelected);
            		}
            		else {
            			this._spwAdvancedIdentify.setResultGeometry(geoms);
            		}
            	},
            	
            	onClickImgMoreInfo: function() {
            		var values = new Array();
            		for(var i=0; i < this._spwIdentifyResultRows.length; i++) {
            			if(this._spwIdentifyResultRows[i].isSelected()) {
            				values.push(this._spwIdentifyResultRows[i].getFeatureFieldValue(this._spwLayer.moreInfoPageFieldName));
            			}
            		}
            		if(values.length == 0) {
            			MessageManager.getInstance().notifyInfo(this.labels.SpwIdentifyResultTableNoResultSelected);
            		}
            		else {
            			var url = this._spwLayer.moreInfoPageBaseUrl + values.join((this._spwLayer.moreInfoPageSeparator || ","));
            			if(this._spwLayer.moreInfoPageInNewWindow) {
            				window.open(url);
            			}
            			else {
            				top.location.href = url;
            			}
            		}
            	},
            	
            	onClickExportCsv: function() {
            		var exportLines = [this._columnHeaders];
            		for(var i=0; i < this._spwIdentifyResultRows.length; i++) {
            			if(this._spwIdentifyResultRows[i].isSelected()) {
            				exportLines.push(this._spwIdentifyResultRows[i].getRowValues());
            			}
            		}
            		if(exportLines.length < 2) {
            			MessageManager.getInstance().notifyInfo(this.labels.SpwIdentifyResultTableNoResultSelected);
            		}
            		else {
            			//Transform to CSV text
            			this.csvTextValue.value = this.createCsvText(exportLines);
            			this.csvFileName.value = "EXPORT_" + this._spwLayer.get('name') + ".csv";
            			//Call REST WS
            			this.uploadCsvForm.action = this._spwIdentifyResultWidget.exportCsvServerUrl;
            			this.uploadCsvForm.method = "POST";
            			this.uploadCsvForm.target = "_blank";
            			this.uploadCsvForm.submit();
            		}
            	},
            	
            	createCsvText: function(data) {
            		var tmpData = new Array();
            		array.forEach(data, function(row){
            			var tmpRow = new Array();
            			array.forEach(row, function(cell){
            				tmpRow.push("\"" + cell.replace("\"", "\"\"") + "\"");
            			});
            			tmpData.push(tmpRow.join(";"));
            		});
            		return tmpData.join("\r\n");
            	}
            });
});