[Mapbender-commits] r1915 - in trunk/mapbender/http: javascripts php

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Wed Dec 12 11:21:27 EST 2007


Author: christoph
Date: 2007-12-12 11:21:27 -0500 (Wed, 12 Dec 2007)
New Revision: 1915

Modified:
   trunk/mapbender/http/javascripts/event.js
   trunk/mapbender/http/javascripts/map.php
   trunk/mapbender/http/php/mod_addWmsFromFeatureInfo.php
   trunk/mapbender/http/php/mod_wfs.php
Log:
added new (but backwards compatible) event management

Modified: trunk/mapbender/http/javascripts/event.js
===================================================================
--- trunk/mapbender/http/javascripts/event.js	2007-12-12 14:41:46 UTC (rev 1914)
+++ trunk/mapbender/http/javascripts/event.js	2007-12-12 16:21:27 UTC (rev 1915)
@@ -10,28 +10,116 @@
 	 * registered via this function.
 	 */
 	this.register = function(aFunction, aPriority) {
-		var e = new Mb_notice("MapbenderEvent: registering " + aFunction);
+//		var e = new Mb_notice("MapbenderEvent: registering " + aFunction);
 
-		if (typeof(aFunction) == "function") {
-			var mpbnFunction = new MapbenderFunction(aFunction, aPriority);
-			functionArray.push(mpbnFunction);
+		var mpbnFunction = new MapbenderFunction(aFunction, aPriority, functionArray.length);
+		functionArray.push(mpbnFunction);
 
-			// unfortunately, there is no way of using static variables in JS...
-			sortFunction = mpbnFunction.getSortFunction();
-		}	
+		// unfortunately, there is no way of using static variables in JS...
+		sortFunction = mpbnFunction.getSortFunction();
 	};
 
 	/**
+	 * Exclude a previously registered function from the event
+	 */
+	this.unregister = function(aFunction) {
+		var e = new Mb_notice("functions (before del): " + functionArray.join(","));
+		for (var i = 0, len = functionArray.length; i < len; i++) {
+			if (functionArray[i].getFunction() === aFunction) {
+				for (var j = i + 1; j < len; j++) {
+					functionArray[j-1] = functionArray[j];
+					functionArray[j-1].setArrayPosition(j-1);
+				}
+				delete functionArray[len - 1];
+				len = len - 1;
+			}
+		}
+		functionArray.length = len;
+		var e = new Mb_notice("functions (after del): " + functionArray.join(","));
+	};
+
+	/**
+	 * Checks if a function is already registered with this event
+	 */
+	this.isRegistered = function (aFunction) {
+		for (var i = 0, len = functionArray.length; i < len; i++) {
+			if (functionArray[i].getFunction() === aFunction) {
+				return true;
+			}
+		}
+		return false;
+	}
+	
+	/**
 	 * This function triggers the event
 	 */
-	this.trigger = function(properties) {
-		var e = new Mb_notice("MapbenderEvent: triggering " + this);
-		propertiesObj = properties;
+	this.trigger = function(properties, booleanOperator) {
+		if (!(functionArray.length > 0)) {
+			return true;
+		}
+		//
+		// check arguments
+		//
+		// properties
+		if (typeof(properties) != "object") {
+			// maybe properties is missing, and so 
+			// properties represents booleanOperator
+			if (typeof(booleanOperator) == "undefined") {
+				booleanOperator = properties;
+			}
+			else {
+				var e = new Mb_exception("MapbenderEvent.trigger: invalid properties: %s", properties);
+			}
+		}		
+
+		// booleanOperator
+		if (typeof(booleanOperator) == "string") {
+			if (booleanOperator != "AND" && booleanOperator != "OR") {
+				var e = new Mb_exception("MapbenderEvent.trigger: invalid booleanOperator: %s", booleanOperator);
+			}
+		}		
+		else if (typeof(booleanOperator) != "undefined") {
+			var e = new Mb_exception("MapbenderEvent.trigger: invalid booleanOperator, must be a string, but is %s", typeof(booleanOperator));
+		}
+		
+		var result;
+
+		// the optional boolean operator allows to combine the return values of the functions
+		// into a single result value.
+		switch (booleanOperator) {
+			case "AND":
+				result = true;
+				break;
+			case "OR":
+				result = false;
+				break;
+			default:
+				result = true;
+				break;
+		}
+
+		// sort the function array by priority
 		functionArray.sort(sortFunction);
+		if (log) {
+			var e = new Mb_notice("functions (after sort): " + functionArray.join(","));
+		}
+
 		for (var i = 0; i < functionArray.length; i++) {
 			// executes the function at position i
-			functionArray[i].execute(propertiesObj);
+			// and determines the return value based on the (optional) boolean operator
+			switch (booleanOperator) {
+				case "AND":
+					result = result && functionArray[i].execute(properties);
+					break;
+				case "OR":
+					result = result || functionArray[i].execute(properties);
+					break;
+				default:
+					result = functionArray[i].execute(properties);
+					break;
+			}
 		}
+		return result;
 	};	
 	
 	this.getProperties = function () {
@@ -50,14 +138,14 @@
 	var sortFunction;
 	
 	var propertiesObj;
-	
+	var log = false;
 	// constructor
 };
 
 /**
  * A MapbenderFunction is a function with a priority.
  */
-var MapbenderFunction = function (aFunction, aPriority) {
+var MapbenderFunction = function (aFunction, aPriority, posInArray) {
 	
 	// public
 	/**
@@ -75,18 +163,63 @@
 	};
 	
 	/**
+	 *  Returns the array position
+	 */
+	this.getArrayPosition = function () {
+		return posInArray;
+	};
+	
+	/**
+	 *  Returns the array position
+	 */
+	this.setArrayPosition = function (i) {
+		posInArray = i;
+	};
+	
+	/**
 	 * Executes the function
 	 */
 	this.execute = function (argumentObj) {
-		var e = new Mb_notice("MapbenderFunction: executing " + this);
-		return func(argumentObj);
+//		var e = new Mb_notice("MapbenderFunction: executing " + this);
+		if (typeof(aFunction) == "function") {
+			return func(argumentObj);
+		}
+		else {
+			var argumentNames = [];
+			var argumentValues = [];
+			for (var i in argumentObj) {
+				if (typeof(argumentObj[i]) == "number" || typeof(argumentObj[i]) == "boolean") {
+					argumentNames.push(i);
+					argumentValues.push(argumentObj[i]);
+				}
+				else if (typeof(argumentObj[i]) == "string") {
+					argumentNames.push(i);
+					argumentValues.push("'" + argumentObj[i] + "'");
+				}
+			}
+			var str = "";
+			str += "(function (" + argumentNames.join(", ") + ") {";
+			str += "return " + aFunction;
+			str += "}";
+			str += "(" + argumentValues.join(", ") + "));";
+			var returnValue = eval(str);
+			console.log("%s: %s", aFunction, returnValue);
+			return returnValue;
+		}	
 	}
 	/**
 	 * Returns a sort function for Mapbender functions. Sorted by priority.
 	 */
 	this.getSortFunction = function () {
 		var mapbenderFunctionSort = function (functionA, functionB) {
-			return functionA.getPriority() - functionB.getPriority();
+			var diff = functionA.getPriority() - functionB.getPriority();
+			if (diff === 0) {
+				// I assumed that if the return value of the sort function was 0, then no switch
+				// would occur. But this is not the case. A random looking output is what I get.
+				// So I had to use the array position as well. 
+				return functionA.getArrayPosition() - functionB.getArrayPosition();
+			}
+			return diff;
 		};
 		return mapbenderFunctionSort;
 	};
@@ -105,4 +238,11 @@
 	else {
 		var priority = aPriority;
 	}
-};
\ No newline at end of file
+	
+	// the array position is needed for the sort function, if the priorities are equal.
+	var posInArray = posInArray;
+};
+
+MapbenderFunction.prototype.toString = function () {
+	return this.getFunction() + " (pos: "+this.getArrayPosition()+")";
+};

Modified: trunk/mapbender/http/javascripts/map.php
===================================================================
--- trunk/mapbender/http/javascripts/map.php	2007-12-12 14:41:46 UTC (rev 1914)
+++ trunk/mapbender/http/javascripts/map.php	2007-12-12 16:21:27 UTC (rev 1915)
@@ -63,8 +63,8 @@
 echo "var global_mb_log_level = '".LOG_LEVEL."';";
 echo "var global_log_levels = '".LOG_LEVEL_LIST."';";
 
+include("event.js");
 include("map.js");
-include("event.js");
 
 // see http://trac.osgeo.org/mapbender/ticket/79
 ini_set('session.bug_compat_42',0); 

Modified: trunk/mapbender/http/php/mod_addWmsFromFeatureInfo.php
===================================================================
--- trunk/mapbender/http/php/mod_addWmsFromFeatureInfo.php	2007-12-12 14:41:46 UTC (rev 1914)
+++ trunk/mapbender/http/php/mod_addWmsFromFeatureInfo.php	2007-12-12 16:21:27 UTC (rev 1915)
@@ -71,5 +71,6 @@
 	if (mod_addWmsFromFeatureInfo_position > 0 && mod_addWmsFromFeatureInfo_position < mb_mapObj[getMapObjIndexByName(mod_target)].wms.length-1) {
 		mb_wmsMoveByIndex(getMapObjIndexByName(mod_target), mb_mapObj[getMapObjIndexByName(mod_target)].wms.length-1, mod_addWmsFromFeatureInfo_position-1);
 	}
-	mb_removeFunctionFromArray("mb_loadWmsSubFunctions", "addWmsFromInfo_pos()");	
+	eventAfterLoadWMS.unregister("addWmsFromInfo_pos()");
+//	mb_removeFunctionFromArray("mb_loadWmsSubFunctions", "addWmsFromInfo_pos()");	
 }
\ No newline at end of file

Modified: trunk/mapbender/http/php/mod_wfs.php
===================================================================
--- trunk/mapbender/http/php/mod_wfs.php	2007-12-12 14:41:46 UTC (rev 1914)
+++ trunk/mapbender/http/php/mod_wfs.php	2007-12-12 16:21:27 UTC (rev 1915)
@@ -27,6 +27,7 @@
 <title>mod_wfs</title>
 <script language='JavaScript' type='text/javascript'>
 var wfs_conf = new Array();
+/*
 function register(){
 	var isReg = false;
 	for(var i=0; i<parent.mb_InitFunctions.length; i++){
@@ -38,6 +39,14 @@
 		parent.mb_registerInitFunctions(window.name+".fetchInf()");
 	}
 }
+*/
+function register() {
+	var functionStatement = window.name+".fetchInf()";
+	
+	if(!parent.eventInit.isRegistered(functionStatement)){
+		parent.mb_registerInitFunctions(functionStatement);
+	}
+}
 function fetchInf(){
 	var wfs = new Array();
 	var l;



More information about the Mapbender_commits mailing list