mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-16 21:42:27 +00:00
Implement autocompletion for label expression text box on project configuration
page. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@36581 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
1 parent
e5820d084f
commit
3f1f53f238
5 files changed
+158
-4
No files matched your search
@@ -3,7 +3,8 @@
|
||||
*
|
||||
* Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi,
|
||||
* Brian Westrich, Erik Ramfelt, Ertan Deniz, Jean-Baptiste Quenot,
|
||||
* Luca Domenico Milanesio, R. Tyler Ballance, Stephen Connolly, Tom Huybrechts, id:cactusman
|
||||
* Luca Domenico Milanesio, R. Tyler Ballance, Stephen Connolly, Tom Huybrechts,
|
||||
* id:cactusman, Yahoo! Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -25,6 +26,8 @@
|
||||
*/
|
||||
package hudson.model;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import antlr.ANTLRException;
|
||||
import hudson.AbortException;
|
||||
import hudson.CopyOnWrite;
|
||||
@@ -1763,6 +1766,63 @@ public abstract class AbstractProject<P extends AbstractProject<P,R>,R extends A
|
||||
return FormValidation.warning("There's no slave/cloud that matches this assignment");
|
||||
return FormValidation.ok();
|
||||
}
|
||||
|
||||
public AutoCompletionCandidates doAutoCompleteAssignedLabelString(@QueryParameter String value) {
|
||||
AutoCompletionCandidates c = new AutoCompletionCandidates();
|
||||
Set<Label> labels = Hudson.getInstance().getLabels();
|
||||
List<String> queries = new AutoCompleteSeeder(value).getSeeds();
|
||||
|
||||
for (String term : queries) {
|
||||
for (Label l : labels) {
|
||||
if (l.getName().startsWith(term)) {
|
||||
c.add(l.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility class for taking the current input value and computing a list
|
||||
* of potential terms to match against the list of defined labels.
|
||||
*/
|
||||
static class AutoCompleteSeeder {
|
||||
private String source;
|
||||
private Pattern quoteMatcher = Pattern.compile("(\\\"?)(.+?)(\\\"?+)(\\s*)");
|
||||
|
||||
AutoCompleteSeeder(String source) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
List<String> getSeeds() {
|
||||
ArrayList<String> terms = new ArrayList();
|
||||
boolean trailingQuote = source.endsWith("\"");
|
||||
boolean leadingQuote = source.startsWith("\"");
|
||||
boolean trailingSpace = source.endsWith(" ");
|
||||
|
||||
if (trailingQuote || (trailingSpace && !leadingQuote)) {
|
||||
terms.add("");
|
||||
} else {
|
||||
if (leadingQuote) {
|
||||
int quote = source.lastIndexOf('"');
|
||||
if (quote == 0) {
|
||||
terms.add(source.substring(1));
|
||||
} else {
|
||||
terms.add("");
|
||||
}
|
||||
} else {
|
||||
int space = source.lastIndexOf(' ');
|
||||
if (space > -1) {
|
||||
terms.add(source.substring(space+1));
|
||||
} else {
|
||||
terms.add(source);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return terms;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
<!--
|
||||
The MIT License
|
||||
|
||||
Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Eric Lefevre-Ardant, id:cactusman
|
||||
Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi,
|
||||
Eric Lefevre-Ardant, id:cactusman, Yahoo! Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -50,7 +51,7 @@ THE SOFTWARE.
|
||||
<f:optionalBlock name="hasSlaveAffinity" title="${%Restrict where this project can be run}" checked="${it.assignedLabel!=null}"
|
||||
help="/help/project-config/slave.html">
|
||||
<f:entry title="${%Label Expression}" field="assignedLabelString">
|
||||
<f:textbox />
|
||||
<f:textbox autoCompleteDelimiChar=" "/>
|
||||
</f:entry>
|
||||
</f:optionalBlock>
|
||||
</j:if>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<!--
|
||||
The MIT License
|
||||
|
||||
Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
|
||||
Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Yahoo! Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -59,6 +59,12 @@ THE SOFTWARE.
|
||||
If @field is specified, this will be inferred automatically,
|
||||
which is the recommended approach.
|
||||
</st:attribute>
|
||||
<st:attribute name="autoCompleteDelimChar">
|
||||
A single character that can be used as a delimiter for autocompletion. Normal
|
||||
autocomplete will replace the entire content of the text box with the autocomplete
|
||||
selection. With this attribute set, the selection will be appended with the
|
||||
delimiter to the existing value of the text box.
|
||||
</st:attribute>
|
||||
</st:documentation>
|
||||
<f:prepareDatabinding />
|
||||
${descriptor.calcAutoCompleteSettings(field,attrs)} <!-- this figures out the 'autoCompleteUrl' attribute -->
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* The MIT License
|
||||
*
|
||||
* Copyright 2010 Yahoo! Inc.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package hudson.model;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runner.RunWith;
|
||||
import hudson.model.AbstractProject.AbstractProjectDescriptor.AutoCompleteSeeder;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author dty
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class AutoCompleteSeederTest {
|
||||
|
||||
public static class TestData {
|
||||
private String seed;
|
||||
private List<String> expected;
|
||||
|
||||
public TestData(String seed, String... expected) {
|
||||
this.seed = seed;
|
||||
this.expected = Arrays.asList(expected);
|
||||
}
|
||||
}
|
||||
|
||||
@Parameterized.Parameters
|
||||
public static Collection<Object[]> data() {
|
||||
return Arrays.asList( new Object[][] {
|
||||
{ new TestData("", "") },
|
||||
{ new TestData("\"", "") },
|
||||
{ new TestData("\"\"", "") },
|
||||
{ new TestData("freebsd", "freebsd") },
|
||||
{ new TestData(" freebsd", "freebsd") },
|
||||
{ new TestData("freebsd ", "") },
|
||||
{ new TestData("freebsd 6", "6") },
|
||||
{ new TestData("\"freebsd", "freebsd") },
|
||||
{ new TestData("\"freebsd ", "freebsd ") },
|
||||
{ new TestData("\"freebsd\"", "") },
|
||||
{ new TestData("\"freebsd\" ", "") },
|
||||
{ new TestData("\"freebsd 6", "freebsd 6") },
|
||||
{ new TestData("\"freebsd 6\"", "") },
|
||||
});
|
||||
}
|
||||
|
||||
private String seed;
|
||||
private List<String> expected;
|
||||
|
||||
public AutoCompleteSeederTest(TestData dataSet) {
|
||||
this.seed = dataSet.seed;
|
||||
this.expected = dataSet.expected;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAutoCompleteSeeds() throws Exception {
|
||||
AutoCompleteSeeder seeder = new AbstractProject.AbstractProjectDescriptor.AutoCompleteSeeder(seed);
|
||||
assertEquals(expected, seeder.getSeeds());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -605,6 +605,7 @@ var hudsonRules = {
|
||||
ac.animSpeed = 0;
|
||||
ac.useShadow = true;
|
||||
ac.autoSnapContainer = true;
|
||||
ac.delimChar = e.getAttribute("autoCompleteDelimiChar");
|
||||
ac.doBeforeExpandContainer = function(textbox,container) {// adjust the width every time we show it
|
||||
container.style.width=textbox.clientWidth+"px";
|
||||
var Dom = YAHOO.util.Dom;
|
||||
|
||||
Reference in new issue
Block a user