/* * Copyright (c) 2005-2008 * Authors: KSS Project Contributors (see doc/CREDITS.txt) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as published * by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. */ /* Form handling utilities */ kukit.fo = new function() { /// MODULE START var fo = this; /* form query assembler */ // Prefix constants for dict marshalling, // pattern: %s(_dictPrefix)%(name)s%(_dictSeparator)s%(key)s%(_dictPostfix)s // XXX this should be settable var _dictPrefix = ''; var _dictSeparator = '.'; var _dictPostfix = ':record'; /* * class _FormQueryElem */ var _FormQueryElem = function() { this.initialize = function(name, value) { this.name = name; this.value = value; }; this.encode = function() { return this.name+ "=" + encodeURIComponent(this.value); }; this.initialize.apply(this, arguments); }; /* * class FormQuery */ fo.FormQuery = function() { this.initialize = function() { this.l = []; }; this.appendElem = function(name, value) { if (value == null) { // do not marshall nulls ;;; var msg = "Parameter '" + name + "' is null,"; ;;; msg += " it is not marshalled."; ;;; kukit.logDebug(msg); } else if (typeof(value) == 'string') { var elem = new _FormQueryElem(name, value); this.l.push(elem); } // value.length is for detection of an Array. // In addition we also check that value.pop is a function else if (typeof(value) == 'object' && typeof(value.length) == 'number' && typeof(value.pop) == 'function') { // Special marshalling of arrays for (var i=0; i < value.length; i++) { var elem = new _FormQueryElem(name, value[i]); this.l.push(elem); } } else if (typeof(value) == 'object') { // Special marshalling of dicts for (var key in value) { var qkey = _dictPrefix + name + _dictSeparator; qkey += key + _dictPostfix; var elem = new _FormQueryElem(qkey, value[key]); this.l.push(elem); } } }; this.encode = function() { var poster = []; for (var i=0;i < this.l.length;i++) { poster[poster.length] = this.l[i].encode(); } return poster.join("&"); }; this.toDict = function() { var d = {}; for (var i=0;i < this.l.length;i++) { var elem = this.l[i]; d[elem.name] = elem.value; } return d; }; this.initialize.apply(this, arguments); }; /* Form data extraction, helpers */ var findContainer = function(node, func) { // Starting with the given node, find the nearest containing element // for which the given function returns true. while (node != null) { if (func(node)) { return node; } node = node.parentNode; } return null; }; /* * class CurrentFormLocator: gets the current form of a target * */ fo.CurrentFormLocator = function() { this.initialize = function(target) { this.target = target; }; this.queryForm = function() { // Find the form that contains the target node. return findContainer(this.target, function(node) { if (!node.nodeName) { return false; } if (node.nodeName.toLowerCase() == "form") { return true; } else { return false; } }); }; this.getForm = function() { var form = this.queryForm(); if (!form) { ;;; kukit.logWarning("No form found"); return null; } return form; }; this.initialize.apply(this, arguments); }; /* * class NamedFormLocator: gets the form with a given name * */ fo.NamedFormLocator = function() { this.initialize = function(formname) { this.formname = formname; }; this.queryForm = function() { // Find the form with the given name. return document.forms[this.formname]; }; this.initialize.apply(this, arguments); }; fo.NamedFormLocator.prototype = new fo.CurrentFormLocator(); /* methods to take the desired value(s) from the form */ fo.getValueOfFormElement = function(element) { // Returns the value of the form element / or null // First: update the field in case an editor is lurking // in the background this.fieldUpdateRegistry.doUpdate(element); if (element.disabled) { return null; } // Collect the data if (element.selectedIndex != undefined) { // handle single selects first if(!element.multiple) { if (element.selectedIndex < 0) { value=""; } else { var option = element.options[element.selectedIndex]; // on FF and safari, option.value has the value // on IE, option.text needs to be used value = option.value || option.text; } // Now process selects with the multiple option set } else { var value = []; for(i=0; i