← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9777: (PT) Settings validation improved.

 

Merge authors:
  Jan Henrik Øverland (janhenrik-overland)
------------------------------------------------------------
revno: 9777 [merge]
committer: Jan Henrik Overland <janhenrik.overland@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2013-02-08 16:00:53 +0100
message:
  (PT) Settings validation improved.
modified:
  dhis-2/dhis-web/dhis-web-mapping/src/main/webapp/dhis-web-mapping/app/scripts/app.js
  dhis-2/dhis-web/dhis-web-pivot/src/main/webapp/dhis-web-pivot/app/scripts/app.js
  dhis-2/dhis-web/dhis-web-pivot/src/main/webapp/dhis-web-pivot/app/scripts/core.js
  dhis-2/dhis-web/dhis-web-pivot/src/main/webapp/dhis-web-pivot/app/scripts/plugin.js
  dhis-2/dhis-web/dhis-web-pivot/src/main/webapp/dhis-web-pivot/app/styles/style.css


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-mapping/src/main/webapp/dhis-web-mapping/app/scripts/app.js'
--- dhis-2/dhis-web/dhis-web-mapping/src/main/webapp/dhis-web-mapping/app/scripts/app.js	2013-02-05 20:19:14 +0000
+++ dhis-2/dhis-web/dhis-web-mapping/src/main/webapp/dhis-web-mapping/app/scripts/app.js	2013-02-07 07:45:56 +0000
@@ -3369,11 +3369,12 @@
 			listConfig: {loadMask: false},
 			store: indicatorsByGroupStore,
 			listeners: {
-				select: function() {
+				select: function(cb) {
 					Ext.Ajax.request({
 						url: gis.baseUrl + gis.conf.url.path_api + 'indicators/' + this.getValue() + '.json?links=false',
 						success: function(r) {
 							r = Ext.decode(r.responseText);
+
 							if (Ext.isDefined(r.legendSet) && r.legendSet && r.legendSet.id) {
 								legendType.setValue(gis.conf.finals.widget.legendtype_predefined);
 								legendTypeToggler(gis.conf.finals.widget.legendtype_predefined);

=== modified file 'dhis-2/dhis-web/dhis-web-pivot/src/main/webapp/dhis-web-pivot/app/scripts/app.js'
--- dhis-2/dhis-web/dhis-web-pivot/src/main/webapp/dhis-web-pivot/app/scripts/app.js	2013-02-05 17:37:43 +0000
+++ dhis-2/dhis-web/dhis-web-pivot/src/main/webapp/dhis-web-pivot/app/scripts/app.js	2013-02-08 14:58:19 +0000
@@ -50,27 +50,29 @@
 		util.pivot.getSettingsConfig = function() {
 			var data = {},
 				setup = pt.viewport.settingsWindow.getSetup(),
-				config = {
-					col: {},
-					row: {},
-					filter: {}
-				},
 				getData,
-				extendSettings;
+				extendSettings,
+				config;
+
+			config = {
+				col: [],
+				row: [],
+				filter: []
+			};
 
 			getData = function() {
 				var panels = pt.cmp.dimension.panels,
 					dxItems = [];
 
-				for (var i = 0, dimData; i < panels.length; i++) {
-					dimData = panels[i].getData();
+				for (var i = 0, dim; i < panels.length; i++) {
+					dim = panels[i].getData();
 
-					if (dimData) {
-						if (dimData.param === pt.conf.finals.dimension.data.paramname) {
-							dxItems = dxItems.concat(dimData.items);
+					if (dim) {
+						if (dim.name === pt.conf.finals.dimension.data.paramname) {
+							dxItems = dxItems.concat(dim.items);
 						}
 						else {
-							data[dimData.param] = dimData.items;
+							data[dim.name] = dim.items;
 						}
 					}
 				}
@@ -81,19 +83,28 @@
 			}();
 
 			extendSettings = function() {
-				for (var i = 0, dim; i < setup.col.length; i++) {
-					dim = setup.col[i];
-					config.col[dim] = data[dim];
-				}
-
-				for (var i = 0, dim; i < setup.row.length; i++) {
-					dim = setup.row[i];
-					config.row[dim] = data[dim];
-				}
-
-				for (var i = 0, dim; i < setup.filter.length; i++) {
-					dim = setup.filter[i];
-					config.filter[dim] = data[dim];
+				for (var i = 0, name; i < setup.col.length; i++) {
+					name = setup.col[i];
+					config.col.push({
+						name: name,
+						items: data[name]
+					});
+				}
+				
+				for (var i = 0, name; i < setup.row.length; i++) {
+					name = setup.row[i];
+					config.row.push({
+						name: name,
+						items: data[name]
+					});
+				}
+				
+				for (var i = 0, name; i < setup.filter.length; i++) {
+					name = setup.filter[i];
+					config.filter.push({
+						name: name,
+						items: data[name]
+					});
 				}
 			}();
 			
@@ -652,7 +663,7 @@
 				hideCollapseTool: true,
 				getData: function() {
 					var data = {
-						param: pt.conf.finals.dimension.indicator.paramname,
+						name: pt.conf.finals.dimension.indicator.paramname,
 						items: []
 					};
 
@@ -836,7 +847,7 @@
 				hideCollapseTool: true,
 				getData: function() {
 					var data = {
-						param: pt.conf.finals.dimension.indicator.paramname,
+						name: pt.conf.finals.dimension.indicator.paramname,
 						items: []
 					};
 
@@ -1020,7 +1031,7 @@
 				hideCollapseTool: true,
 				getData: function() {
 					var data = {
-						param: pt.conf.finals.dimension.indicator.paramname,
+						name: pt.conf.finals.dimension.indicator.paramname,
 						items: []
 					};
 
@@ -1343,7 +1354,7 @@
 				hideCollapseTool: true,
 				getData: function() {
 					var data = {
-						param: pt.conf.finals.dimension.period.paramname,
+						name: pt.conf.finals.dimension.period.paramname,
 							items: []
 						},
 						chb = pt.cmp.dimension.relativePeriod.checkbox;
@@ -1458,7 +1469,7 @@
 				getData: function() {
 					var records = pt.cmp.dimension.organisationUnit.treepanel.getSelectionModel().getSelection(),
 						data = {
-							param: 'ou',
+							name: 'ou',
 							items: []
 						};
 
@@ -1908,7 +1919,7 @@
 						hideCollapseTool: true,
 						getData: function() {
 							var data = {
-								param: groupSet.id,
+								name: groupSet.id,
 								items: []
 							};
 
@@ -1972,9 +1983,10 @@
 			};
 
 			update = function() {
-				var settings = pt.api.Settings(pt.util.pivot.getSettingsConfig());
+				var config = pt.util.pivot.getSettingsConfig(),
+					settings = pt.api.Settings(config);
 
-				if (settings && Ext.isObject(settings)) {
+				if (settings) {
 					pt.util.pivot.getTable(settings, pt, centerRegion);
 				}
 			};				
@@ -2025,7 +2037,7 @@
 
 			centerRegion = Ext.create('Ext.panel.Panel', {
 				region: 'center',
-				bodyStyle: 'padding:3px',
+				bodyStyle: 'padding:1px',
 				autoScroll: true,
 				tbar: {
                     defaults: {

=== modified file 'dhis-2/dhis-web/dhis-web-pivot/src/main/webapp/dhis-web-pivot/app/scripts/core.js'
--- dhis-2/dhis-web/dhis-web-pivot/src/main/webapp/dhis-web-pivot/app/scripts/core.js	2013-02-05 23:26:21 +0000
+++ dhis-2/dhis-web/dhis-web-pivot/src/main/webapp/dhis-web-pivot/app/scripts/core.js	2013-02-08 14:58:19 +0000
@@ -393,10 +393,37 @@
 		}
 	};
 
+	util.array = {
+		sortDimensions: function(dimensions) {
+
+			// Sort object order
+			Ext.Array.sort(dimensions, function(a,b) {
+				if (a.name < b.name) {
+					return -1;
+				}
+				if (a.name > b.name) {
+					return 1;
+				}
+				return 0;
+			});
+
+			// Sort object items order
+			for (var i = 0, dim; i < dimensions.length; i++) {
+				dim = dimensions[i];
+
+				if (dim.items) {
+					dimensions[i].items.sort();
+				}
+			}
+
+			return dimensions;
+		}
+	};
+
 	util.pivot = {
 		getTable: function(settings, pt, container) {
-			var getDimensionItemsFromSettings,
-				getParamStringFromDimensionItems,
+			var getDimensionsFromSettings,
+				getParamStringFromDimensions,
 
 				validateResponse,
 				extendResponse,
@@ -409,47 +436,47 @@
 				createTableArray,
 				initialize;
 
-			getDimensionItemsFromSettings = function() {
-				var col = settings.col,
-					row = settings.row,
-					dimensionItems;
-
-				dimensionItems = Ext.clone(col);
-				Ext.apply(dimensionItems, row);
-
-				return dimensionItems;
+			getDimensionsFromSettings = function() {
+				var dimensions = [];
+
+				if (settings.col) {
+					dimensions = dimensions.concat(settings.col);
+				}
+				if (settings.row) {
+					dimensions = dimensions.concat(settings.row);
+				}
+				
+				return dimensions;
 			};
 
-			getParamStringFromDimensionItems = function(dimensionItems) {
+			getParamStringFromDimensions = function(dimensions) {
 				var paramString = '?',
+					filterDimensions = [],
 					dim;
 
-				for (var key in dimensionItems) {
-					if (dimensionItems.hasOwnProperty(key)) {
-						if (key === pt.conf.finals.dimension.category.paramname) {
-							paramString += 'dimension=' + key + '&';
-						}
-						else {
-							dim = dimensionItems[key];
-
-							if (!(dim && Ext.isArray(dim) && dim.length)) {
-								pt.util.mask.hideMask();
-								alert('Empty dimension');
-								return;
-							}
-
-							Ext.Array.sort(dim);
-							paramString += 'dimension=' + key + ':' + dim.join(';') + '&';
-						}
+				dimensions = pt.util.array.sortDimensions(dimensions);
+
+				for (var i = 0; i < dimensions.length; i++) {
+					dim = dimensions[i];
+
+					paramString += 'dimension=' + dim.name;
+
+					if (dim.name !== pt.conf.finals.dimension.category.paramname) {
+						paramString += ':' + dim.items.join(';');
+					}
+
+					if (i < (dimensions.length - 1)) {
+						paramString += '&';
 					}
 				}
 
-				paramString = paramString.substring(0, paramString.length-1);
+				if (settings.filter) {
+					filterDimensions = pt.util.array.sortDimensions(settings.filter.slice(0));
 
-				for (var key in settings.filter) {
-					if (settings.filter.hasOwnProperty(key)) {
-						dim = settings.filter[key];
-						paramString += '&filter=' + key + ':' + dim.join(';');
+					for (var i = 0, filterDim; i < filterDimensions.length; i++) {
+						filterDim = filterDimensions[i];
+						
+						paramString += '&filter=' + filterDim.name + ':' + filterDim.items.join(';');
 					}
 				}
 
@@ -457,11 +484,28 @@
 			};
 
 			validateResponse = function(response) {
-				if (response.width < 1 || response.height < 1 || response.rows.length < 1) {
+				if (!(response && Ext.isObject(response))) {
+					alert('Data invalid');
+					return false;
+				}
+
+				if (!(response.headers && Ext.isArray(response.headers) && response.headers.length)) {
+					alert('Data invalid');
+					return false;
+				}
+				
+				if (!(Ext.isNumber(response.width) && response.width > 0 &&
+					  Ext.isNumber(response.height) && response.height > 0 &&
+					  Ext.isArray(response.rows) && response.rows.length > 0)) {
 					alert('No values found');
 					return false;
 				}
 
+				if (response.headers.length !== response.rows[0].length) {
+					alert('Data invalid');
+					return false;
+				}
+
 				return true;
 			};
 		
@@ -678,28 +722,15 @@
 				var response = pt.response,
 					col = settings.col,
 					row = settings.row,
-					getUniqueColsArray,
-					getUniqueRowsArray;
-
-				getUniqueColsArray = function() {
-					var a = [];
-
-					for (var dim in col) {
-						if (col.hasOwnProperty(dim)) {
-							a.push(response.nameHeaderMap[dim].items);
-						}
-					}
-
-					return a;
-				};
-
-				getUniqueRowsArray = function() {
-					var a = [];
-
-					for (var dim in row) {
-						if (row.hasOwnProperty(dim)) {
-							a.push(response.nameHeaderMap[dim].items);
-						}
+					getUniqueDimensionsNames;
+
+				getUniqueDimensionsNames = function(axis) {
+					var a = [];
+
+					for (var i = 0, dim; i < axis.length; i++) {
+						dim = axis[i];
+						
+						a.push(response.nameHeaderMap[dim.name].items);
 					}
 
 					return a;
@@ -708,8 +739,8 @@
 				// aUniqueCols ->  [[p1, p2, p3], [ou1, ou2, ou3, ou4]]
 
 				return {
-					cols: extendDims(getUniqueColsArray()),
-					rows: extendDims(getUniqueRowsArray())
+					cols: extendDims(getUniqueDimensionsNames(col)),
+					rows: extendDims(getUniqueDimensionsNames(row))
 				};
 			};
 
@@ -911,11 +942,11 @@
 				var dimensionItems,
 					paramString;
 
-				pt.util.mask.showMask();
-
-				dimensionItems = getDimensionItemsFromSettings(settings);
-
-				paramString = getParamStringFromDimensionItems(dimensionItems);
+				pt.util.mask.showMask(container);
+
+				dimensions = getDimensionsFromSettings();
+
+				paramString = getParamStringFromDimensions(dimensions);
 
 				Ext.data.JsonP.request({
 					method: 'GET',
@@ -937,7 +968,6 @@
 						if (!validateResponse(r)) {
 							pt.util.mask.hideMask();
 							console.log(r);
-							alert('Data response invalid');
 							return;
 						}
 						
@@ -978,38 +1008,86 @@
 		var col,
 			row,
 			filter,
-			settings = {};
-
-		if (!(config && Ext.isObject(config))) {
-			alert('Settings config is not an object'); //i18n
-			return;
-		}
-
-		col = (config.col && Ext.isObject(config.col) && pt.util.object.getLength(config.col)) ? config.col : null;
-
-		row = (config.row && Ext.isObject(config.row) && pt.util.object.getLength(config.row)) ? config.row : null;
-
-		if (!(col || row)) {
-			alert('No col or row items selected'); //i18n
-			return;
-		}
-
-		filter = config.filter;
-
-		if (!(filter === undefined || Ext.isObject(filter))) {
-			alert('Illegal filter type'); //i18n
-			return;
-		}
-
-		if (col) {
-			settings.col = col;
-		}
-
-		if (row) {
-			settings.row = row;
-		}
-
-		settings.filter = filter;
+			settings = {},
+
+			removeEmptyDimensions,
+			isAxisValid,
+			initialize;
+
+		removeEmptyDimensions = function(axis) {			
+			if (!axis) {
+				return;
+			}
+			
+			for (var i = 0, dimension, remove; i < axis.length; i++) {
+				remove = false;
+				dimension = axis[i];
+				
+				if (dimension.name !== 'coc') {
+					if (!(Ext.isArray(dimension.items) && dimension.items.length)) {
+						remove = true;
+					}
+					else {
+						for (var j = 0; j < dimension.items.length; j++) {
+							if (!Ext.isString(dimension.items[j])) {
+								remove = true;
+							}
+						}
+					}
+				}
+
+				if (remove) {
+					axis = Ext.Array.erase(axis, i, 1);
+					i = i - 1;
+				}
+			}
+
+			return axis;
+		};
+
+		getValidatedAxis = function(axis) {
+			if (!(axis && Ext.isArray(axis) && axis.length)) {
+				return;
+			}
+
+			for (var i = 0, dimension; i < axis.length; i++) {
+				dimension = axis[i];
+
+				if (!(Ext.isObject(dimension) && Ext.isString(dimension.name))) {
+					return;
+				}
+			}
+			
+			axis = removeEmptyDimensions(axis);
+
+			return axis.length ? axis : null;
+		};
+
+		initialize = function() {
+			if (!(config && Ext.isObject(config))) {
+				alert('Settings config is not an object'); //i18n
+				return;
+			}
+			
+			col = getValidatedAxis(config.col);
+			row = getValidatedAxis(config.row);
+			filter = getValidatedAxis(config.filter);
+
+			if (!(col || row)) {
+				alert('Invalid column/row configuration'); //i18n
+				return;
+			}
+
+			if (col) {
+				settings.col = col;
+			}
+			if (row) {
+				settings.row = row;
+			}
+			if (filter) {
+				settings.filter = filter;
+			}
+		}();
 
 		return settings;
 	};

=== modified file 'dhis-2/dhis-web/dhis-web-pivot/src/main/webapp/dhis-web-pivot/app/scripts/plugin.js'
--- dhis-2/dhis-web/dhis-web-pivot/src/main/webapp/dhis-web-pivot/app/scripts/plugin.js	2013-01-21 13:26:25 +0000
+++ dhis-2/dhis-web/dhis-web-pivot/src/main/webapp/dhis-web-pivot/app/scripts/plugin.js	2013-02-07 12:16:27 +0000
@@ -1,10 +1,15 @@
-/*
+//format
 
-DHIS.getPivot({
-	de: [aaa, bbb, ccc],
-	pe: [201201, 201202],
-	ou: [111, 222, 333],
-	userOrganisationUnit: false,
-	userOrganisationUnitChildren: false,
-	categories: false
+DHIS.getPivotTable({
+	col: [
+		{name: 'dx', items: ['Uvn6LCg7dVU', 'OdiHJayrsKo', 'sB79w2hiLp8']},
+		{name: 'coc'}
+	],
+	row: [
+		{name: 'pe', items: ['201201', '201202', '201203']},
+		{name: 'Bpx0589u8y0', items: ['MAs88nJc9nL', 'PVLOW4bCshG']}
+	],
+	filter: [
+		{name: 'ou', items: ['ImspTQPwCqd']}
+	]
 });

=== modified file 'dhis-2/dhis-web/dhis-web-pivot/src/main/webapp/dhis-web-pivot/app/styles/style.css'
--- dhis-2/dhis-web/dhis-web-pivot/src/main/webapp/dhis-web-pivot/app/styles/style.css	2013-02-06 13:04:05 +0000
+++ dhis-2/dhis-web/dhis-web-pivot/src/main/webapp/dhis-web-pivot/app/styles/style.css	2013-02-07 12:16:27 +0000
@@ -101,7 +101,7 @@
 	background-color: #fff;
 }
 .pivot-valuetotal {
-	background-color: #eee;
+	background-color: #eaeaea;
 }
 .pivot-valuegrandtotal {
 	background-color: #ccc;