From b0f664dfcfc2505ffe50afedc52ca490fb331c95 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Tue, 27 Apr 2010 21:09:40 +0000 Subject: [PATCH] improved construction of query string. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@30447 71c3de6d-444a-0410-be80-ed276b4c234a --- .../main/java/hudson/model/Descriptor.java | 8 ++--- war/resources/scripts/hudson-behavior.js | 33 +++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/hudson/model/Descriptor.java b/core/src/main/java/hudson/model/Descriptor.java index 8d50e49c5f..c897bd1a79 100644 --- a/core/src/main/java/hudson/model/Descriptor.java +++ b/core/src/main/java/hudson/model/Descriptor.java @@ -285,7 +285,7 @@ public abstract class Descriptor> implements Saveable { if(method==null) return NONE; - return singleQuote(getDescriptorUrl() +"/check"+capitalizedFieldName+"?") + buildParameterList(method, new StringBuilder()); + return singleQuote(getDescriptorUrl() +"/check"+capitalizedFieldName) + buildParameterList(method, new StringBuilder()).append(".toString()"); } /** @@ -300,13 +300,13 @@ public abstract class Descriptor> implements Saveable { if (name==null || name.length()==0) continue; // unknown parameter name. we'll report the error when the form is submitted. - if (query.length()>0) query.append('+').append(singleQuote("&")); + if (query.length()==0) query.append("+qs(this)"); if (name.equals("value")) { // The special 'value' parameter binds to the the current field - query.append('+').append(singleQuote("value=")).append("+toValue(this)"); + query.append(".addThis()"); } else { - query.append('+').append(singleQuote(name+'=')).append("+toValue(findNearBy(this,'"+name+"'))"); + query.append(".nearBy('"+name+"')"); } continue; } diff --git a/war/resources/scripts/hudson-behavior.js b/war/resources/scripts/hudson-behavior.js index 80575b327a..51466a3ffb 100644 --- a/war/resources/scripts/hudson-behavior.js +++ b/war/resources/scripts/hudson-behavior.js @@ -160,6 +160,7 @@ function findNearBy(e,name) { } function controlValue(e) { + if (e==null) return null; // compute the form validation value to be sent to the server var type = e.getAttribute("type"); if(type!=null && type.toLowerCase()=="checkbox") @@ -171,6 +172,38 @@ function toValue(e) { return encodeURIComponent(controlValue(e)); } +/** + * Builds a query string in a fluent API pattern. + * @param {HTMLElement} owner + * The 'this' control. + */ +function qs(owner) { + return { + params : "", + + append : function(s) { + if (this.params.length==0) this.params+='?'; + else this.params+='&'; + this.params += s; + return this; + }, + + nearBy : function(name) { + var e = findNearBy(owner,name); + if (e==null) return this; // skip + return this.append(name+'='+toValue(e)); + }, + + addThis : function() { + return this.append("value="+toValue(owner)); + }, + + toString : function() { + return this.params; + } + }; +} + // find the nearest ancestor node that has the given tag name function findAncestor(e, tagName) { do { -- GitLab