[Mapbender-commits] r5382 - trunk/mapbender/http/plugins

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Wed Jan 20 11:01:10 EST 2010


Author: christoph
Date: 2010-01-20 11:01:09 -0500 (Wed, 20 Jan 2010)
New Revision: 5382

Added:
   trunk/mapbender/http/plugins/mb_print.js
Log:


Added: trunk/mapbender/http/plugins/mb_print.js
===================================================================
--- trunk/mapbender/http/plugins/mb_print.js	                        (rev 0)
+++ trunk/mapbender/http/plugins/mb_print.js	2010-01-20 16:01:09 UTC (rev 5382)
@@ -0,0 +1,443 @@
+/**
+ * Package: printPDF
+ *
+ * Description:
+ * Mapbender print PDF with PDF templates module.
+ * 
+ * Files:
+ *  - http/plugins/mb_print.js
+ *  - http/print/classes
+ *  - http/print/printFactory.php
+ *  - http/print/printPDF_download.php
+ *  - lib/printbox.js
+ *
+ * SQL:
+ * > INSERT INTO gui_element(fkey_gui_id, e_id, e_pos, e_public, e_comment, 
+ * > e_title, e_element, e_src, e_attributes, e_left, e_top, e_width, 
+ * > e_height, e_z_index, e_more_styles, e_content, e_closetag, e_js_file, 
+ * > e_mb_mod, e_target, e_requires, e_url) VALUES('<appId>','printPDF',
+ * > 2,1,'pdf print','Print','div','','',1,1,2,2,5,'',
+ * > '<div id="printPDF_working_bg"></div><div id="printPDF_working"><img src="../img/indicator_wheel.gif" style="padding:10px 0 0 10px">Generating PDF</div><div id="printPDF_input"><form id="printPDF_form" action="../print/printFactory.php"><div id="printPDF_selector"></div><div class="print_option"><input type="hidden" id="map_url" name="map_url" value=""/><input type="hidden" id="overview_url" name="overview_url" value=""/><input type="hidden" id="map_scale" name="map_scale" value=""/><input type="hidden" name="measured_x_values" /><input type="hidden" name="measured_y_values" /><br /></div><div class="print_option" id="printPDF_formsubmit"><input id="submit" type="submit" value="Print"><br /></div></form><div id="printPDF_result"></div></div>',
+ * > 'div','../plugins/mb_print.js',
+ * > '../../lib/printbox.js,../extensions/jquery-ui-1.7.2.custom/development-bundle/external/bgiframe/jquery.bgiframe.min.js,../extensions/jquery.form.min.js',
+ * > 'mapframe1','','http://www.mapbender.org/index.php/Print');
+ * >
+ * > INSERT INTO gui_element_vars(fkey_gui_id, fkey_e_id, var_name, 
+ * > var_value, context, var_type) VALUES('<appId>', 'printPDF', 
+ * > 'mbPrintConfig', '{"Standard": "mapbender_template.json"}', '' ,'var');
+ * >
+ * > INSERT INTO gui_element_vars(fkey_gui_id, fkey_e_id, var_name, 
+ * > var_value, context, var_type) VALUES('<appId>', 'body', 
+ * > 'print_css', '../css/print_div.css', '' ,'file/css');
+ * 
+ * Help:
+ * http://www.mapbender.org/PrintPDF_with_template
+ *
+ * Maintainer:
+ * http://www.mapbender.org/User:Michael_Schulz
+ * http://www.mapbender.org/User:Christoph_Baudson
+ * 
+ * Parameters:
+ * mbPrintConfig      - *[optional]* object with name and filename of template
+ *
+ * License:
+ * Copyright (c) 2009, Open Source Geospatial Foundation
+ * This program is dual licensed under the GNU General Public License 
+ * and Simplified BSD license.  
+ * http://svn.osgeo.org/mapbender/trunk/mapbender/license/license.txt
+ */
+
+var myTarget = options.target ? options.target[0] : "mapframe1";
+var myId = options ? options.id : "printPDF";
+
+
+/* the array of json print config files */
+
+if (typeof mbPrintConfigFilenames === "undefined") {
+	mbPrintConfigFilenames = ["mapbender_template.json"];
+}
+
+if (typeof mbPrintConfigTitles === "undefined") {
+	mbPrintConfigTitles = ["Default"];
+}
+
+
+var mbPrintConfigPath = "../print/";
+
+
+/* ------------- printbox addition ------------- */
+
+var PrintPDF = function (options) {
+
+	var that = this;
+	
+	/**
+ 	 * Property: actualConfig
+	 * 
+	 * object, holds the actual configuration after loading the json file
+	 */
+	var actualConfig;
+
+	/**
+ 	 * constructor
+	 */
+	eventInit.register(function () {
+		mod_printPDF_init();
+	});
+	
+	/**
+ 	 * Property: printBox
+	 * 
+	 * the movable printframe
+	 */	
+	var printBox = null;
+	
+	eventAfterMapRequest.register(function () {
+		if (printBox !== null) {
+			printBox.repaint();
+		}
+	});
+	/**
+ 	 * Method: createPrintBox
+	 * 
+	 * creates a printBox in the current view, calculates the scale 
+	 * (tbd. if not set from the config) so that the printbox fits in the mapframe.
+	 * Width and height are taken from the configuration.
+	 */
+	this.createPrintBox = function () {
+		size = "A4";
+		//document.form1.size.value = size;
+		format = "portrait";
+		var w, h;
+		//validate();
+		var map = getMapObjByName(myTarget);
+		var map_el = map.getDomElement();
+		var jqForm = $("#"+myId+"_form");
+		var $scaleInput = $("#scale");
+
+		if (printBox !== null) {
+			printBox.destroy();
+			printBox = null;
+			jqForm[0].scale.value = "";
+			jqForm[0].coordinates.value = "";
+			jqForm[0].angle.value = "";
+		}
+		else {
+			printBox = new Mapbender.PrintBox({
+				target : myTarget,
+				printWidth : getPDFMapSize("width")/10,
+				printHeight : getPDFMapSize("height")/10,
+				scale : $scaleInput.size() > 0 && !isNaN(parseInt($scaleInput.val(), 10)) ? 
+					parseInt($scaleInput.val(), 10) : 
+					Math.pow(10, Math.floor(Math.log(map.getScale())/Math.LN10)),
+				afterChangeAngle : function (obj) {
+					if (typeof(obj) == "object") {
+						if (typeof(obj.angle) == "number") {
+							jqForm[0].angle.value = obj.angle;
+						}
+						if (obj.coordinates) {
+							jqForm[0].coordinates.value = String(obj.coordinates);
+						}
+					}
+				},
+				afterChangeSize : function (obj) {
+					if (typeof(obj) == "object") {
+						if (obj.scale) {
+							jqForm[0].scale.value = parseInt(obj.scale / 100, 10) * 100;
+						}
+						if (obj.coordinates) {
+							jqForm[0].coordinates.value = String(obj.coordinates);
+						}
+					}
+				}
+			});
+			printBox.paintPoints();
+			printBox.paintBox();
+
+		}
+	};
+
+	/**
+ 	 * Method: getPDFMapSize
+	 * 
+	 * checks the actual config for the size w/h values.
+	 *
+	 * Parameters:
+	 * key      - string, the key which value to retrieve (currently width or height)
+	 */	
+	var getPDFMapSize = function (key) {
+		for (var page in actualConfig.pages) {
+			for (var pageElement in actualConfig.pages[page].elements) {
+				if (actualConfig.pages[page].elements[pageElement].type == "map") {
+					return actualConfig.pages[page].elements[pageElement][key];
+				}
+			}
+		} 
+	};
+
+	/**
+ 	 * Method: destroyPrintBox
+	 * 
+	 * removes an existing printBox.
+	 */	
+	var destroyPrintBox = function () {
+		if (printBox) {
+			printBox.destroy();
+		}
+	};
+	
+	/**
+ 	 * Method: mod_printPDF_init
+	 * 
+	 * initializes the print modules, generates template chooser and loads first configuration.
+	 */	
+	var mod_printPDF_init = function () {
+		/* first we'd need to build the configuration selection */
+		buildConfigSelector();	
+		/* second we'd need to read the json configuration */
+		that.loadConfig(mbPrintConfigFilenames[0]);
+	};
+
+	/**
+ 	 * Method: loadConfig
+	 * 
+	 * GETs the config, build corresponding form, remove an existing printBox
+	 */		
+	this.loadConfig = function (configFilename, callback) {
+		$.get(mbPrintConfigPath + configFilename, function(json, status){
+		    actualConfig = $.parseJSON(json);
+		    buildForm(); 
+		    hookForm();
+			if (typeof callback === "function") {
+				printBox = null;
+				callback();
+			}
+		});
+		destroyPrintBox();
+	};
+
+	/**
+ 	 * Method: hookForm
+	 * 
+	 * utility method to connect the form plugin to the print form.
+	 */	
+	var hookForm = function () {
+		var options = { 
+	   		url:			'../print/printFactory.php', 
+	   		type:			'post',
+	   		dataType:		'json',
+	   		beforeSubmit: 	validate,
+	   		success:    	showResult
+		};
+		$("#"+myId+"_form").ajaxForm(options);	
+	};
+	
+	/**
+	 * Change status of the working elements. These should begin with "$myId_working" 
+	 *
+	 * @param {String} newStatus either "hide or "show"
+	 */
+	var showHideWorking = function (newStatus) {
+		if (newStatus=="hide") {
+	    	$("[id^='"+myId+"_working']").hide();
+		}
+	    else {
+	    	$("[id^='"+myId+"_working']").show();	
+		}
+	};
+	
+	/**
+	 * update form values helper function
+	 *
+	 */
+	var updateFormField = function (formData, key, value) {
+		for(var j=0; j < formData.length; j++){
+			if (formData[j].name == key) {
+				formData[j].value = value;
+				break;
+			}
+		}	
+	};
+	
+	
+	/**
+	 * Validates and updates form data values.
+	 * Adds the elements before the submit button.
+	 *
+	 * @see jquery.forms#beforeSubmitHandler
+	 */
+	var validate = function (formData, jqForm, options) {
+		showHideWorking("show");
+		
+		// map urls
+		var ind = getMapObjIndexByName(myTarget);
+		var f = jqForm[0];
+		f.map_url.value = '';
+		f.overview_url.value = '';
+		for(var i=0; i < mb_mapObj[ind].wms.length; i++){
+			if(mb_mapObj[ind].wms[i].gui_wms_visible > 0){
+				if(mb_mapObj[ind].wms[i].mapURL != false && mb_mapObj[ind].wms[i].mapURL != 'false' && mb_mapObj[ind].wms[i].mapURL != ''){   
+					if(f.map_url.value != ""){
+						f.map_url.value += '___';
+					}      
+					f.map_url.value += mb_mapObj[ind].wms[i].mapURL;
+				}
+			}
+		}
+		updateFormField(formData, "map_url", f.map_url.value);
+		
+		//overview_url
+		var ind_overview = getMapObjIndexByName('overview');
+		if(mb_mapObj[ind_overview].mapURL != false ){
+		    f.overview_url.value = mb_mapObj[ind_overview].mapURL;
+			updateFormField(formData, "overview_url", f.overview_url.value);
+		}
+		
+		updateFormField(formData, "map_scale", mb_getScale(myTarget));
+		// write the measured coordinates
+		if (typeof(mod_measure_RX) !== "undefined") {
+			var tmp_x = '';
+			var tmp_y = '';
+			for(i = 0; i < mod_measure_RX.length; i++) {
+				if(tmp_x != '') {
+					tmp_x += ',';
+				}
+				tmp_x += mod_measure_RX[i];
+			}
+			for(i = 0; i < mod_measure_RY.length; i++) {
+				if(tmp_y != '') {
+					tmp_y += ',';
+				}
+				tmp_y += mod_measure_RY[i];
+			}
+			updateFormField(formData, "measured_x_values", tmp_x);
+			updateFormField(formData, "measured_y_values", tmp_y);
+		}	
+		
+		
+		if (f.map_url.value!="") {
+			//return true;
+		} else {
+		    showHideWorking("hide");
+			return false;
+		}
+			
+	};
+
+	/**
+ 	 * Method: showResult
+	 * 
+	 * load the generated PDF from the returned URL as an attachment, 
+	 * that triggers a download popup or is displayed in PDF plugin.
+	 */		
+	var showResult = function (res, text) {
+		if (text == 'success') {
+			var $downloadFrame = $("#" + myId + "_frame");
+			if ($downloadFrame.size() === 0) {
+				$downloadFrame = $(
+					"<iframe id='" + myId + "_frame' name='" + 
+					myId + "_frame' width='0' height='0' style='display:none'></iframe>"
+				).appendTo("body");
+			}
+			if ($.browser.msie && $.browser.version === "6.0") {
+				var newWin = window.open(stripslashes(res.outputFileName), "Druckausgabe", "width=200,height=200,left=700,top=200,resizable=yes");
+			}
+			else {
+				window.frames[myId + "_frame"].location.href = 
+					stripslashes(res.outputFileName);
+			}
+			showHideWorking("hide");	
+		}
+		else {
+			/* something went wrong */
+			$("#"+myId+"_result").html(text);
+		}
+	};
+	
+	/**
+	 * Generates form elements as specified in the config controls object.
+	 * Adds the elements before the submit button.
+	 * 
+	 * @param {Object} json the config object in json
+	 */
+	var buildForm = function () {
+		$(".print_option_dyn").remove();
+		var str = "";
+		for (var item in actualConfig.controls) {
+			var element = actualConfig.controls[item];
+			var element_id = myId + "_" + element.id;
+			if (element.type != "hidden") {
+				str += '<div class="print_option_dyn">\n';	
+				str += '<label class="print_label" for="'+element.id+'">'+element.label+'</label>\n';
+			} else {
+				str += '<div class="print_option_dyn" style="display:none;">\n';	
+			}
+			switch (element.type) {
+				case "text":
+					str += '<input type="'+element.type+'" name="'+element.id+'" id="'+element.id+'" size="'+element.size+'"><br>\n';
+					break;
+				case "hidden":
+					str += '<input type="'+element.type+'" name="'+element.id+'" id="'+element.id+'">\n';
+					break;				
+				case "textarea":
+					str += '<textarea id="'+element.id+'" name="'+element.id+'" size="'+element.size+'"></textarea><br>\n';
+					break;				
+				case "select":
+					str += '<select id="'+element.id+'" name="'+element.id+'" size="1">\n';
+					for (var option_index in element.options) {
+						option = element.options[option_index];
+						str += '<option value="'+option.value+'">'+option.label+'</option>\n';
+					}
+					str += '</select><br>\n';
+					break;
+			}
+			str += '</div>\n';
+		}
+		if (str) {
+			$("#" + myId + "_formsubmit").before(str);
+			$("#scale").keydown(function (e) {
+				if (e.keyCode !== 13) {
+					return;
+				}
+				var scale = parseInt(this.value, 10);
+				if (isNaN(scale) || typeof printBox === "undefined") {
+					return false;
+				}
+				printBox.setScale(scale);
+				return false;
+			});
+		}
+	};
+	
+	/**
+	 * Generates the configuration select element from the gui element vars
+	 * mbPrintConfigFilenames and mbPrintConfigTitles
+	 */
+	var buildConfigSelector = function () {
+		var str = "";
+		str += '<label class="print_label" for="printPDF_template">Vorlage</label>\n';
+		str += '<select id="printPDF_template" name="printPDF_template" size="1" onchange="printObj.loadConfig(mbPrintConfigFilenames[this.selectedIndex], function () {printObj.createPrintBox()});">\n';
+		for (var i = 0; i < mbPrintConfigFilenames.length; i++) {
+			str += '<option value="'+mbPrintConfigFilenames[i]+'">'+mbPrintConfigTitles[i]+'</option>\n';
+		}
+		str += '</select><img id="printPDF_handle" src="../print/img/shape_handles.png" title="Use printbox">\n';
+		if (str) {
+			$("#printPDF_selector").append(str);
+			$("#printPDF_handle").click(function () {
+				printObj.createPrintBox();
+			});
+			$("#printPDF_working").bgiframe({ 
+				src: "BLOCKED SCRIPT'&lt;html&gt;&lt;/html&gt;';",
+				width: 200,
+				height: 200
+			});
+		}
+	};
+	
+	var stripslashes = function ( str ) {
+	    return (str+'').replace(/\0/g, '0').replace(/\\([\\'"])/g, '$1');
+	};
+
+};
+
+var printObj = new PrintPDF();



More information about the Mapbender_commits mailing list