diff --git a/core/src/main/java/hudson/model/Descriptor.java b/core/src/main/java/hudson/model/Descriptor.java index 44e7bc1c38..fcc19f9fb0 100644 --- a/core/src/main/java/hudson/model/Descriptor.java +++ b/core/src/main/java/hudson/model/Descriptor.java @@ -29,6 +29,8 @@ import hudson.Util; import static hudson.Util.singleQuote; import hudson.diagnosis.OldDataMonitor; import hudson.model.listeners.SaveableListener; +import hudson.util.ReflectionUtils; +import hudson.util.ReflectionUtils.Parameter; import hudson.views.ListViewColumn; import net.sf.json.JSONArray; import net.sf.json.JSONObject; @@ -248,6 +250,20 @@ public abstract class Descriptor> implements Saveable { */ public abstract String getDisplayName(); + /** + * Gets the URL that this Descriptor is bound to, relative to the nearest {@link DescriptorByNameOwner}. + * Since {@link Hudson} is a {@link DescriptorByNameOwner}, there's always one such ancestor to any request. + */ + public String getUrl() { + return "descriptorByName/"+clazz.getName(); + } + + private String getCurrentDescriptorByNameUrl() { + StaplerRequest req = Stapler.getCurrentRequest(); + Ancestor a = req.findAncestor(DescriptorByNameOwner.class); + return a.getUrl(); + } + /** * If the field "xyz" of a {@link Describable} has the corresponding "doCheckXyz" method, * return the form-field validation string. Otherwise null. @@ -266,56 +282,71 @@ public abstract class Descriptor> implements Saveable { // put this under the right contextual umbrella. // a is always non-null because we already have Hudson as the sentinel - StaplerRequest req = Stapler.getCurrentRequest(); - Ancestor a = req.findAncestor(DescriptorByNameOwner.class); - - return singleQuote(a.getUrl())+'+'+method; + return singleQuote(getCurrentDescriptorByNameUrl()+'/')+'+'+method; } private String calcCheckUrl(String fieldName) { String capitalizedFieldName = StringUtils.capitalize(fieldName); - Method method = null; - String methodName = "doCheck"+ capitalizedFieldName; - for (Class c=getClass(); c!=null; c=c.getSuperclass()) { - for( Method m : c.getMethods() ) { - if(m.getName().equals(methodName)) { - method = m; - break; - } - } - } + Method method = ReflectionUtils.getPublicMethodNamed(getClass(),"doCheck"+ capitalizedFieldName); if(method==null) return NONE; // build query parameter line by figuring out what should be submitted StringBuilder query = new StringBuilder(); - String[] names = ClassDescriptor.loadParameterNames(method); boolean first = true; - Annotation[][] all = method.getParameterAnnotations(); - for (int i = 0, allLength = all.length; i < allLength; i++) { - for (Annotation a : all[i]) { - if (a instanceof QueryParameter) { - QueryParameter qp = (QueryParameter) a; - String name = qp.value(); - if (name.length()==0 && i attributes) { + String capitalizedFieldName = StringUtils.capitalize(field); + String methodName = "doFill" + capitalizedFieldName + "Items"; + Method method = ReflectionUtils.getPublicMethodNamed(getClass(), methodName); + if(method==null) + throw new IllegalStateException(String.format("%s doesn't have the %s method for filling a drop-down list", getClass(), methodName)); + + // build query parameter line by figuring out what should be submitted + List depends = new ArrayList(); + + for (Parameter p : ReflectionUtils.getParameters(method)) { + QueryParameter qp = p.annotation(QueryParameter.class); + if (qp==null) continue; + + String name = qp.value(); + if (name.length()==0) name = p.name(); + if (name==null || name.length()==0) + continue; // unknown parameter name. we'll report the error when the form is submitted. + + depends.add(name); + } + + attributes.put("fillDependsOn",Util.join(depends," ")); + attributes.put("fillUrl", String.format("%s/%s/fill%sItems", getCurrentDescriptorByNameUrl(), getUrl(), capitalizedFieldName)); } /** diff --git a/core/src/main/resources/lib/form/select.jelly b/core/src/main/resources/lib/form/select.jelly new file mode 100644 index 0000000000..ea439f4dbc --- /dev/null +++ b/core/src/main/resources/lib/form/select.jelly @@ -0,0 +1,50 @@ + + + + + + Glorified <select> control that supports the data binding and AJAX updates. + + + Additional CSS classes that the control gets. + + + List of possible values. Either this or nested <f:editableComboBoxValue/>s are required. + + + Used for databinding. + + + + + + ${descriptor.calcFillSettings(field,attrs)} + + + diff --git a/war/resources/images/48x48/document.gif b/war/resources/images/48x48/document.gif new file mode 100644 index 0000000000..866137ce91 Binary files /dev/null and b/war/resources/images/48x48/document.gif differ diff --git a/war/resources/scripts/hudson-behavior.js b/war/resources/scripts/hudson-behavior.js index 89500bf284..1666480d63 100644 --- a/war/resources/scripts/hudson-behavior.js +++ b/war/resources/scripts/hudson-behavior.js @@ -150,12 +150,16 @@ function findNearBy(e,name) { return null; // not found } -function toValue(e) { +function controlValue(e) { // compute the form validation value to be sent to the server var type = e.getAttribute("type"); if(type!=null && type.toLowerCase()=="checkbox") return e.checked; - return encodeURIComponent(e.value); + return e.value; +} + +function toValue(e) { + return encodeURIComponent(controlValue(e)); } // find the nearest ancestor node that has the given tag name @@ -246,7 +250,26 @@ function parseHtml(html) { return c.firstChild; } - +/** + * Emulate the firing of an event. + * + * @param {HTMLElement} element + * The element that will fire the event + * @param {String} event + * like 'change', 'blur', etc. + */ +function fireEvent(element,event){ + if (document.createEvent) { + // dispatch for firefox + others + var evt = document.createEvent("HTMLEvents"); + evt.initEvent(event, true, true ); // event type,bubbling,cancelable + return !element.dispatchEvent(evt); + } else { + // dispatch for IE + var evt = document.createEventObject(); + return element.fireEvent('on'+event,evt) + } +} // shared tooltip object var tooltip; @@ -780,7 +803,48 @@ var hudsonRules = { return values; }); } + }, + + // select.jelly + "SELECT.select" : function(e) { + var dep; // controls that this SELECT box depends on + + function refill() { + var params = {}; + dep.each(function (d) { + params[shortenName(d.getAttribute("name"))] = controlValue(d); + }); + + var value = e.value; + updateListBox(e,e.getAttribute("fillUrl"),{ + parameters: params, + onSuccess: function() { + if (value=="") { + // reflect the initial value. if the control depends on several other SELECT.select, + // it may take several updates before we get the right items, which is why all these precautions. + var v = e.getAttribute("value"); + if (v) { + e.value = v; + if (e.value==v) e.removeAttribute("value"); // we were able to apply our initial value + } + } + + // if the update changed the current selection, others listening to this control needs to be notified. + if (e.value!=value) fireEvent(e,"change"); + } + }); + } + + // install change handlers + dep = e.getAttribute("fillDependsOn").split(" ").collect(function (name) { + var c = findNearBy(e,name); + c.addEventListener("change",refill,false); + return c; + }); + + refill(); // initial fill } + }; function applyTooltip(e,text) { @@ -1259,24 +1323,29 @@ function updateBuildHistory(ajaxUrl,nBuild) { // send async request to the given URL (which will send back serialized ListBoxModel object), // then use the result to fill the list box. -function updateListBox(listBox,url) { - new Ajax.Request(url, { - onSuccess: function(rsp) { - var l = $(listBox); - while(l.length>0) l.options[0] = null; +function updateListBox(listBox,url,config) { + config = config || {}; + config = object(config); + var originalOnSuccess = config.onSuccess; + config.onSuccess = function(rsp) { + var l = $(listBox); + while(l.length>0) l.options[0] = null; - var opts = eval('('+rsp.responseText+')').values; - for( var i=0; i abc.def.ghi + if(name.startsWith('[')) + return name.substring(1,name.length-1); + + // abc.def.ghi -> ghi + var idx = name.lastIndexOf('.'); + if(idx>=0) name = name.substring(idx+1); + return name; +} + + // // structured form submission handling @@ -1382,17 +1464,6 @@ function buildFormTree(form) { var doms = []; // DOMs that we added 'formDom' for. doms.push(form); - function shortenName(name) { - // [abc.def.ghi] -> abc.def.ghi - if(name.startsWith('[')) - return name.substring(1,name.length-1); - - // abc.def.ghi -> ghi - var idx = name.lastIndexOf('.'); - if(idx>=0) name = name.substring(idx+1); - return name; - } - function addProperty(parent,name,value) { name = shortenName(name); if(parent[name]!=null) {