提交 0cd23f0e 编写于 作者: M mindless

Make f:dropdownList work in repeatable content, such as a build step.

Removed uses of element ids and moved initialization into hudson-behavior rules.
Added unit test.


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@29987 71c3de6d-444a-0410-be80-ed276b4c234a
上级 a559b163
......@@ -38,7 +38,6 @@ THE SOFTWARE.
</st:attribute>
</st:documentation>
<j:set var="id" value="${h.generateId()}"/>
<tr>
<td class="setting-leftspace"><st:nbsp/></td>
<td class="setting-name">
......@@ -46,13 +45,10 @@ THE SOFTWARE.
</td>
<td class="setting-main">
<!-- create drop-down list -->
<select class="setting-input" id="${id}" onchange="updateDropDownList(this)">
<select class="setting-input dropdownList" onchange="updateDropDownList(this)">
<j:set var="dropdownListMode" value="createSelectField"/>
<d:invokeBody/>
</select>
<script>
$$('${id}').subForms = [];
</script>
</td>
<j:if test="${attrs.help!=null}">
<td class="setting-help">
......@@ -63,17 +59,16 @@ THE SOFTWARE.
</tr>
<!-- generate the actual form entries -->
<f:nested>
<tr class="dropdownList-container">
<td colspan="2"/>
<td colspan="2">
<table width="100%" name="${name}">
<j:set var="dropdownListMode" value="generateEntries"/>
<d:invokeBody/>
</table>
</f:nested>
</td>
</tr>
<!-- set the initial visibility -->
<script>
updateDropDownList($$('${id}'));
</script>
<j:if test="${!empty(attrs.description)}">
<f:description>
${description}
......@@ -82,4 +77,4 @@ THE SOFTWARE.
<j:if test="${attrs.help!=null}">
<f:helpArea/>
</j:if>
</j:jelly>
\ No newline at end of file
</j:jelly>
......@@ -34,6 +34,10 @@ THE SOFTWARE.
<st:attribute name="selected" type="boolean">
is this value initially selected?
</st:attribute>
<st:attribute name="staplerClass">
provide hint for stapler data binding.
typically set to ${descriptor.clazz.name} if dropdownList is for a list of descriptors.
</st:attribute>
</st:documentation>
<j:choose>
<j:when test="${dropdownListMode=='createSelectField'}">
......@@ -41,17 +45,13 @@ THE SOFTWARE.
</j:when>
<j:when test="${dropdownListMode=='generateEntries'}">
<!-- sandwich them by markers so that we know what to show/hide -->
<j:set var="sid" value="${h.generateId()}"/>
<j:set var="eid" value="${h.generateId()}"/>
<tr id="${sid}" style="display:none" />
<tr class="dropdownList-start" style="display:none">
<j:if test="${!empty(attrs.staplerClass)}">
<td><input type="hidden" name="stapler-class" value="${attrs.staplerClass}"/></td>
</j:if>
</tr>
<d:invokeBody />
<tr id="${eid}" style="display:none" />
<script>
$$('${id}').subForms.push({
start: $$('${sid}'),
end: $$('${eid}')
});
</script>
<tr class="dropdownList-end" style="display:none" />
</j:when>
</j:choose>
</j:jelly>
......@@ -32,12 +32,10 @@ THE SOFTWARE.
<f:dropdownListBlock value="auto" title="(${%Auto})" />
<j:set var="currentBrowser" value="${scm.browser}"/>
<j:forEach var="descriptor" items="${scmd.browserDescriptors}" varStatus="loop">
<f:dropdownListBlock value="${loop.index}" title="${descriptor.displayName}" selected="${currentBrowser.descriptor==descriptor}">
<f:dropdownListBlock value="${loop.index}" title="${descriptor.displayName}"
selected="${currentBrowser.descriptor==descriptor}" staplerClass="${descriptor.clazz.name}">
<j:set var="browser" value="${currentBrowser.descriptor==descriptor ? currentBrowser : null}"/><!-- for compatibility -->
<j:set var="instance" value="${browser}" />
<tr><td>
<input type="hidden" name="stapler-class" value="${descriptor.clazz.name}" />
</td></tr>
<st:include from="${descriptor}" page="${descriptor.configPage}"/>
</f:dropdownListBlock>
</j:forEach>
......
......@@ -24,9 +24,9 @@
package lib.form;
import com.gargoylesoftware.htmlunit.ElementNotFoundException;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
import java.io.IOException;
import java.util.ArrayList;
......@@ -34,6 +34,12 @@ import java.util.List;
import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob;
import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManagerImpl;
import hudson.DescriptorExtensionList;
import hudson.Extension;
import hudson.ExtensionPoint;
import hudson.model.Describable;
import hudson.model.Descriptor;
import hudson.model.Hudson;
import net.sf.json.JSONObject;
import org.jvnet.hudson.test.HudsonTestCase;
import org.kohsuke.stapler.DataBoundConstructor;
......@@ -55,6 +61,8 @@ public class RepeatableTest extends HudsonTestCase {
bindResult = req.bindJSONToList(bindClass, formData.get("items"));
}
// ========================================================================
private void doTestSimple() throws Exception {
HtmlPage p = createWebClient().goTo("self/testSimple");
HtmlForm f = p.getFormByName("config");
......@@ -75,6 +83,8 @@ public class RepeatableTest extends HudsonTestCase {
formData.get("foos").toString());
}
// ========================================================================
public static class Foo {
public String txt;
public boolean bool;
......@@ -126,6 +136,8 @@ public class RepeatableTest extends HudsonTestCase {
formData.get("foos").toString());
}
// ========================================================================
// hudson-behavior uniquifies radiobutton names so the browser properly handles each group,
// then converts back to original names when submitting form.
public void testRadio() throws Exception {
......@@ -183,6 +195,75 @@ public class RepeatableTest extends HudsonTestCase {
formData.get("foos").toString());
}
// ========================================================================
public static class Fruit implements ExtensionPoint, Describable<Fruit> {
protected String name;
private Fruit(String name) { this.name = name; }
public Descriptor<Fruit> getDescriptor() {
return Hudson.getInstance().getDescriptor(getClass());
}
}
public static class FruitDescriptor extends Descriptor<Fruit> {
public FruitDescriptor(Class<? extends Fruit> clazz) {
super(clazz);
}
public String getDisplayName() {
return clazz.getSimpleName();
}
}
public static class Apple extends Fruit {
private int seeds;
@DataBoundConstructor public Apple(int seeds) { super("Apple"); this.seeds = seeds; }
@Extension public static final FruitDescriptor D = new FruitDescriptor(Apple.class);
@Override public String toString() { return name + " with " + seeds + " seeds"; }
}
public static class Banana extends Fruit {
private boolean yellow;
@DataBoundConstructor public Banana(boolean yellow) { super("Banana"); this.yellow = yellow; }
@Extension public static final FruitDescriptor D = new FruitDescriptor(Banana.class);
@Override public String toString() { return (yellow ? "Yellow" : "Green") + " " + name; }
}
public static class Fruity {
public Fruit fruit;
public String word;
@DataBoundConstructor public Fruity(Fruit fruit, String word) {
this.fruit = fruit;
this.word = word;
}
@Override public String toString() { return fruit + " " + word; }
}
public DescriptorExtensionList<Fruit,Descriptor<Fruit>> getFruitDescriptors() {
return hudson.getDescriptorList(Fruit.class);
}
public void testDropdownList() throws Exception {
HtmlPage p = createWebClient().goTo("self/testDropdownList");
HtmlForm f = p.getFormByName("config");
f.getButtonByCaption("Add").click();
waitForJavaScript(p);
f.getInputByValue("").setValueAttribute("17"); // seeds
f.getInputByValue("").setValueAttribute("pie"); // word
f.getButtonByCaption("Add").click();
waitForJavaScript(p);
// select banana in 2nd select element:
((HtmlSelect)f.getElementsByTagName("select").get(1)).getOption(1).click();
f.getInputsByName("yellow").get(1).click(); // checkbox
f.getInputsByValue("").get(1).setValueAttribute("split"); // word
String xml = f.asXml();
bindClass = Fruity.class;
submit(f);
assertEquals(formData + "\n" + xml,
"[Apple with 17 seeds pie, Yellow Banana split]", bindResult.toString());
}
// ========================================================================
public static class FooList {
public String title;
public Foo[] list = new Foo[0];
......
<!--
The MIT License
Copyright (c) 2004-2010, Sun Microsystems, Inc., Alan Harder
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry>
<f:textbox clazz="number" name="seeds" value="${fruit.seeds}"/>
</f:entry>
</j:jelly>
<!--
The MIT License
Copyright (c) 2004-2010, Sun Microsystems, Inc., Alan Harder
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry>
<f:checkbox name="yellow" checked="${fruit.yellow}"/>
</f:entry>
</j:jelly>
<!--
The MIT License
Copyright (c) 2004-2010, Sun Microsystems, Inc., Alan Harder
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<l:layout title="Testing dropdownList in repeatable">
<l:main-panel>
<form method="post" name="config" action="submitTest">
<f:repeatable var="fruity" items="${it.list}" name="items">
<table>
<f:dropdownList name="fruit" title="Fruits">
<j:forEach var="descriptor" items="${it.fruitDescriptors}" varStatus="loop">
<j:set var="fruit" value="${descriptor==fruity.fruit.descriptor?fruity.fruit:null}"/>
<f:dropdownListBlock title="${descriptor.displayName}" value="${loop.index}"
selected="${fruit!=null}" staplerClass="${descriptor.clazz.name}">
<st:include page="${descriptor.configPage}" from="${descriptor}"/>
</f:dropdownListBlock>
</j:forEach>
</f:dropdownList>
<f:entry title="Word">
<f:textbox name="word" value="${fruity.word}"/>
</f:entry>
</table>
</f:repeatable>
<f:submit value="submit"/>
</form>
</l:main-panel>
</l:layout>
</j:jelly>
......@@ -191,7 +191,7 @@ function findFollowingTR(input, className) {
// then next TR that matches the CSS
do {
tr = tr.nextSibling;
} while (tr.tagName != "TR" || tr.className != className);
} while (tr != null && (tr.tagName != "TR" || tr.className != className));
return tr;
}
......@@ -199,8 +199,7 @@ function findFollowingTR(input, className) {
function find(src,filter,traversalF) {
while(src!=null) {
src = traversalF(src);
if(src==null) break;
if(filter(src))
if(src!=null && filter(src))
return src;
}
return null;
......@@ -816,6 +815,24 @@ var hudsonRules = {
}
},
// dropdownList.jelly
"SELECT.dropdownList" : function(e) {
if(isInsideRemovable(e)) return;
e.subForms = [];
var start = findFollowingTR(e, 'dropdownList-container').firstChild.nextSibling, end;
do { start = start.firstChild; } while (start.tagName != 'TR');
if (start.className != 'dropdownList-start')
start = findFollowingTR(start, 'dropdownList-start');
while (start != null) {
end = findFollowingTR(start, 'dropdownList-end');
e.subForms.push({ 'start': start, 'end': end });
start = findFollowingTR(end, 'dropdownList-start');
}
updateDropDownList(e);
},
// select.jelly
"SELECT.select" : function(e) {
var dep; // controls that this SELECT box depends on
......@@ -1165,15 +1182,15 @@ function updateDropDownList(sel) {
for (var i = 0; i < sel.subForms.length; i++) {
var show = sel.selectedIndex == i;
var f = sel.subForms[i];
var td = f.start;
var tr = f.start;
while (true) {
td.style.display = (show ? "" : "none");
tr.style.display = (show ? "" : "none");
if(show)
td.removeAttribute("field-disabled");
tr.removeAttribute("field-disabled");
else // buildFormData uses this attribute and ignores the contents
td.setAttribute("field-disabled","true");
if (td == f.end) break;
td = td.nextSibling;
tr.setAttribute("field-disabled","true");
if (tr == f.end) break;
tr = tr.nextSibling;
}
}
}
......@@ -1477,7 +1494,7 @@ function shortenName(name) {
//
// structured form submission handling
// see http://hudson.gotdns.com/wiki/display/HUDSON/Structured+Form+Submission
// see http://wiki.hudson-ci.org/display/HUDSON/Structured+Form+Submission
function buildFormTree(form) {
try {
// I initially tried to use an associative array with DOM elemnets as keys
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册