Skip to content

Commit

Permalink
JENKINS-31625 Add configuration parameter, which defines, when filter…
Browse files Browse the repository at this point in the history
… must started work
  • Loading branch information
klimas7 authored and kinow committed Jul 2, 2017
1 parent 8a97011 commit 24113d5
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 28 deletions.
32 changes: 25 additions & 7 deletions src/main/java/org/biouno/unochoice/CascadeChoiceParameter.java
Expand Up @@ -31,7 +31,7 @@
import org.kohsuke.stapler.DataBoundConstructor;

/**
* <p>A choice parameter, that gets updated when another parameter changes. The simplest
* <p>A choice parameter, that gets updated when another parameter changes. The simplest
* use case for this, would be having a list of states, and when the user selected a
* state it would trigger an update of the city fields.</p>
*
Expand Down Expand Up @@ -60,6 +60,11 @@ public class CascadeChoiceParameter extends AbstractCascadableParameter {
*/
private final Boolean filterable;

/**
* Filter Length
*/
private final Integer filterLength;

/**
* Constructor called from Jelly with parameters.
*
Expand All @@ -69,13 +74,15 @@ public class CascadeChoiceParameter extends AbstractCascadableParameter {
* @param choiceType choice type
* @param referencedParameters referenced parameters
* @param filterable filter flag
* @param filterLength length when filter start filtering
* @deprecated see JENKINS-32149
*/
public CascadeChoiceParameter(String name, String description, Script script,
String choiceType, String referencedParameters, Boolean filterable) {
public CascadeChoiceParameter(String name, String description, Script script,
String choiceType, String referencedParameters, Boolean filterable, Integer filterLength) {
super(name, description, script, referencedParameters);
this.choiceType = StringUtils.defaultIfBlank(choiceType, PARAMETER_TYPE_SINGLE_SELECT);
this.filterable = filterable;
this.filterLength = filterLength;
}

/**
Expand All @@ -88,13 +95,15 @@ public CascadeChoiceParameter(String name, String description, Script script,
* @param choiceType choice type
* @param referencedParameters referenced parameters
* @param filterable filter flag
* @param filterLength length when filter start filtering
*/
@DataBoundConstructor
public CascadeChoiceParameter(String name, String description, String randomName, Script script,
String choiceType, String referencedParameters, Boolean filterable) {
public CascadeChoiceParameter(String name, String description, String randomName, Script script,
String choiceType, String referencedParameters, Boolean filterable, Integer filterLength) {
super(name, description, randomName, script, referencedParameters);
this.choiceType = StringUtils.defaultIfBlank(choiceType, PARAMETER_TYPE_SINGLE_SELECT);
this.filterable = filterable;
this.filterLength = filterLength;
}

/*
Expand All @@ -107,14 +116,23 @@ public String getChoiceType() {
}

/**
* Gets the filter flag.
*
* Get the filter flag.
* @return filter flag
*/
public Boolean getFilterable() {
return filterable;
}

/**
* Get the filter length.
* @return filter length
*/
public Integer getFilterLength() {
if (filterLength == null) {
return 1;
}
return filterLength;
}
// --- descriptor

@Extension
Expand Down
47 changes: 42 additions & 5 deletions src/main/java/org/biouno/unochoice/ChoiceParameter.java
Expand Up @@ -43,6 +43,11 @@ public class ChoiceParameter extends AbstractScriptableParameter {
*/
private static final long serialVersionUID = -4449319038169585222L;

/**
* Default length to active filter feature.
*/
private static final int DEFAULT_FILTER_LENGTH = 1;

/**
* Choice type.
*/
Expand All @@ -53,6 +58,11 @@ public class ChoiceParameter extends AbstractScriptableParameter {
*/
private final Boolean filterable;

/**
* Filter Length
*/
private final Integer filterLength;

/**
* Constructor called from Jelly with parameters.
*
Expand All @@ -67,24 +77,44 @@ public ChoiceParameter(String name, String description, Script script, String ch
super(name, description, script);
this.choiceType = StringUtils.defaultIfBlank(choiceType, PARAMETER_TYPE_SINGLE_SELECT);
this.filterable = filterable;
this.filterLength = null;
}

/**
* Constructor called from Jelly with parameters.
* @param name name
* @param description description
* @param randomName parameter random generated name
* @param script script
* @param choiceType choice type
* @param filterable filter flag
* @deprecated see JENKINS-31625
*/
public ChoiceParameter(String name, String description, String randomName, Script script, String choiceType,
Boolean filterable) {
super(name, description, randomName, script);
this.choiceType = StringUtils.defaultIfBlank(choiceType, PARAMETER_TYPE_SINGLE_SELECT);
this.filterable = filterable;
this.filterLength = null;
}

/**
* Constructor called from Jelly with parameters.
*
* @param name name
* @param description description
* @param randomName parameter random generated name (uuid)
* @param randomName parameter random generated name
* @param script script
* @param choiceType choice type
* @param filterable filter flag
* @param filterLength length when filter start filtering
*/
@DataBoundConstructor
public ChoiceParameter(String name, String description, String randomName, Script script, String choiceType,
Boolean filterable) {
Boolean filterable, Integer filterLength) {
super(name, description, randomName, script);
this.choiceType = StringUtils.defaultIfBlank(choiceType, PARAMETER_TYPE_SINGLE_SELECT);
this.filterable = filterable;
this.filterLength = filterLength;
}

/*
Expand All @@ -97,14 +127,21 @@ public String getChoiceType() {
}

/**
* Gets the filter flag.
*
* Get the filter flag.
* @return filter flag
*/
public Boolean getFilterable() {
return filterable;
}

/**
* Get the filter length.
* @return filter length
*/
public Integer getFilterLength() {
return filterLength == null ? DEFAULT_FILTER_LENGTH : filterLength;
}

// --- descriptor

@Extension
Expand Down
Expand Up @@ -55,4 +55,7 @@
<f:entry title="${%Enable filters}" field="filterable" help="${rootURL}/../plugin/uno-choice/help-filterableParameters.html">
<f:checkbox name="parameter.filterable" checked="${instance.filterable}">${%Filterable}</f:checkbox>
</f:entry>
<f:entry title="${%Start filtered length}" field="filterLength" help="${rootURL}/../plugin/uno-choice/help-filterLengthParameters.html">
<f:textbox name="parameter.filterLength" default="1">${%FilterLength}</f:textbox>
</f:entry>
</j:jelly>
Expand Up @@ -26,7 +26,7 @@
<j:if test="${it.filterable}">
var filterHtmlElement = parentDiv.find('.uno_choice_filter');
if (filterHtmlElement &amp;&amp; filterHtmlElement.get(0)) {
var filterElement = new UnoChoice.FilterElement(parameterHtmlElement.get(0), filterHtmlElement.get(0));
var filterElement = new UnoChoice.FilterElement(parameterHtmlElement.get(0), filterHtmlElement.get(0), ${it.getFilterLength()});
cascadeParameter.setFilterElement(filterElement);
} else {
console.log('Filter error: Missing filter element!');
Expand Down
Expand Up @@ -52,4 +52,7 @@
<f:entry title="${%Enable filters}" field="filterable" help="${rootURL}/../plugin/uno-choice/help-filterableParameters.html">
<f:checkbox name="parameter.filterable" checked="${instance.filterable}">${%Filterable}</f:checkbox>
</f:entry>
<f:entry title="${%Start filtered length}" field="filterLength" help="${rootURL}/../plugin/uno-choice/help-filterLengthParameters.html">
<f:textbox name="parameter.filterLength" default="1">${%FilterLength}</f:textbox>
</f:entry>
</j:jelly>
Expand Up @@ -14,7 +14,7 @@
if (parameterHtmlElement &amp;&amp; parameterHtmlElement.get(0)) {
var filterHtmlElement = parentDiv.find('.uno_choice_filter');
if (filterHtmlElement &amp;&amp; filterHtmlElement.get(0)) {
filterElement = new UnoChoice.FilterElement(parameterHtmlElement.get(0), filterHtmlElement.get(0));
filterElement = new UnoChoice.FilterElement(parameterHtmlElement.get(0), filterHtmlElement.get(0), ${it.getFilterLength()});
parameterHtmlElement.filterElement = filterElement; // TBD: not very elegant
} else {
console.log('Filter error: Missing filter element!');
Expand Down
Expand Up @@ -547,10 +547,12 @@ var UnoChoice = UnoChoice || (function($) {
*
* @param paramElement parameter HTML element being filtered
* @param filterElement HTML element where the user enter the filter
* @param filterLength length when filter start filtering
*/
/* public */ function FilterElement(paramElement, filterElement) {
/* public */ function FilterElement(paramElement, filterElement, filterLength) {
this.paramElement = paramElement;
this.filterElement = filterElement;
this.filterLength = filterLength;
this.originalArray = new Array();
// push existing values into originalArray array
if (this.paramElement.tagName == 'SELECT') { // handle SELECTS
Expand Down Expand Up @@ -607,6 +609,12 @@ var UnoChoice = UnoChoice || (function($) {
FilterElement.prototype.getOriginalArray = function() {
return this.originalArray;
}
/**
* @return length when filter start filtering
*/
FilterElement.prototype.getFilterLength = function() {
return this.filterLength;
}
/**
* Sets the array with the original options of the filtered element. Once the array has been
* set, it empties the value of the filter input box, thus allowing the user to type in again.
Expand Down Expand Up @@ -636,6 +644,10 @@ var UnoChoice = UnoChoice || (function($) {
var filterElement = _self.getFilterElement();
var filteredElement = _self.getParameterElement();
var text = filterElement.value.toLowerCase();
if (text.length != 0 && text.length < _self.getFilterLength()) {
console.log("Filter pattern to short " + text.length + " < " + _self.getFilterLength());
return;
}
var options = _self.originalArray;
var newOptions = Array();
for (var i = 0; i < options.length; i++) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/webapp/help-filterLengthParameters.html
@@ -0,0 +1,5 @@
<div>
<p>
The length of the pattern of the filter when the filter starts to work.
</p>
</div>
Expand Up @@ -70,7 +70,7 @@ public void testCreateValue() {
GroovyScript script = new GroovyScript(new SecureGroovyScript(SCRIPT, Boolean.FALSE, null),
new SecureGroovyScript(FALLBACK_SCRIPT, Boolean.FALSE, null));
ChoiceParameter param = new ChoiceParameter("name", "description", "some-random-name", script, "choiceType",
true);
true, 0);
ParameterValue value = param.createValue("value");

assertEquals("value", value.getValue().toString());
Expand Down
Expand Up @@ -59,7 +59,7 @@ public void testConstructor() {
GroovyScript script = new GroovyScript(new SecureGroovyScript(SCRIPT, Boolean.FALSE, null),
new SecureGroovyScript(FALLBACK_SCRIPT, Boolean.FALSE, null));
CascadeChoiceParameter param = new CascadeChoiceParameter("param000", "description", "some-random-name", script,
CascadeChoiceParameter.ELEMENT_TYPE_FORMATTED_HIDDEN_HTML, "param001, param002", true);
CascadeChoiceParameter.ELEMENT_TYPE_FORMATTED_HIDDEN_HTML, "param001, param002", true, 5);

assertEquals("param000", param.getName());
assertEquals("description", param.getDescription());
Expand All @@ -68,14 +68,15 @@ public void testConstructor() {
assertEquals("ET_FORMATTED_HIDDEN_HTML", param.getChoiceType());
assertEquals("param001, param002", param.getReferencedParameters());
assertTrue(param.getFilterable());
assertEquals(Integer.valueOf(5), param.getFilterLength());
}

@Test
public void testParameters() {
GroovyScript script = new GroovyScript(new SecureGroovyScript(SCRIPT, Boolean.FALSE, null),
new SecureGroovyScript(FALLBACK_SCRIPT, Boolean.FALSE, null));
CascadeChoiceParameter param = new CascadeChoiceParameter("param000", "description", "some-random-name", script,
CascadeChoiceParameter.ELEMENT_TYPE_FORMATTED_HIDDEN_HTML, "param001, param002", true);
CascadeChoiceParameter.ELEMENT_TYPE_FORMATTED_HIDDEN_HTML, "param001, param002", true, 0);
assertTrue(param.getParameters().isEmpty());

param.doUpdate("param001=A__LESEP__param002=B__LESEP__param003=");
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/biouno/unochoice/TestChoiceParameter.java
Expand Up @@ -55,13 +55,14 @@ public void testConstructor() {
GroovyScript script = new GroovyScript(new SecureGroovyScript(SCRIPT, Boolean.FALSE, null),
new SecureGroovyScript(FALLBACK_SCRIPT, Boolean.FALSE, null));
ChoiceParameter param = new ChoiceParameter("param000", "description", "some-random-name", script,
CascadeChoiceParameter.ELEMENT_TYPE_FORMATTED_HIDDEN_HTML, true);
CascadeChoiceParameter.ELEMENT_TYPE_FORMATTED_HIDDEN_HTML, true, 5);

assertEquals("param000", param.getName());
assertEquals("description", param.getDescription());
assertEquals("some-random-name", param.getRandomName());
assertEquals(script, param.getScript());
assertEquals("ET_FORMATTED_HIDDEN_HTML", param.getChoiceType());
assertTrue(param.getFilterable());
assertEquals(Integer.valueOf(5), param.getFilterLength());
}
}
13 changes: 11 additions & 2 deletions src/test/java/org/biouno/unochoice/TestParametersOrder.java
Expand Up @@ -64,10 +64,19 @@ public void testParametersOrder() {
ChoiceParameter parameter = new ChoiceParameter("script001", "description", "random name",
new GroovyScript(new SecureGroovyScript(SCRIPT, Boolean.FALSE, null),
new SecureGroovyScript(FALLBACK_SCRIPT, Boolean.FALSE, null)),
ChoiceParameter.PARAMETER_TYPE_MULTI_SELECT, true);
ChoiceParameter.PARAMETER_TYPE_MULTI_SELECT, true, 0);
System.out.println("D1");
/*
ChoiceParameter parameter = new ChoiceParameter(
"script001", "description", "random name", new GroovyScript(SCRIPT, FALLBACK_SCRIPT),
ChoiceParameter.PARAMETER_TYPE_MULTI_SELECT, true, 0);
System.out.println("D2");
Map<Object, Object> result = parameter.getChoices(Collections.<Object, Object>emptyMap());
assertArrayEquals(parameters.keySet().toArray(), result.keySet().toArray());
System.out.println("D3");
*/
//assertArrayEquals(parameters.keySet().toArray(), result.keySet().toArray());
}

}
10 changes: 5 additions & 5 deletions src/test/java/org/biouno/unochoice/TestPersistingParameters.java
Expand Up @@ -72,7 +72,7 @@
* <p>
* These tests have been created after regressions in 1.5.0 and 1.5.1.
* </p>
*
*
* @since 1.5.3
*/
public class TestPersistingParameters {
Expand Down Expand Up @@ -117,11 +117,11 @@ public void testSaveParameters() throws Exception {
GroovyScript scriptParam001 = new GroovyScript(new SecureGroovyScript(SCRIPT_PARAM001, false, null),
new SecureGroovyScript(SCRIPT_FALLBACK_PARAM001, false, null));
ChoiceParameter param001 = new ChoiceParameter("param001", "param001 description", "random-name",
scriptParam001, AbstractUnoChoiceParameter.PARAMETER_TYPE_SINGLE_SELECT, true);
scriptParam001, AbstractUnoChoiceParameter.PARAMETER_TYPE_SINGLE_SELECT, true, 1);
GroovyScript scriptParam002 = new GroovyScript(new SecureGroovyScript(SCRIPT_PARAM002, false, null),
new SecureGroovyScript(SCRIPT_FALLBACK_PARAM002, false, null));
CascadeChoiceParameter param002 = new CascadeChoiceParameter("param002", "param002 description", "random-name",
scriptParam002, AbstractUnoChoiceParameter.PARAMETER_TYPE_SINGLE_SELECT, "param001", true);
scriptParam002, AbstractUnoChoiceParameter.PARAMETER_TYPE_SINGLE_SELECT, "param001", true, 1);
ParametersDefinitionProperty param001Def = new ParametersDefinitionProperty(
Arrays.<ParameterDefinition>asList(param001, param002));
project.addProperty(param001Def);
Expand Down Expand Up @@ -176,11 +176,11 @@ public void testSaveScriptlerParameters() throws Exception {
ScriptlerScriptParameter scriptlerScriptParameters = new ScriptlerScriptParameter("arg1", "bla");
ScriptlerScript scriptParam001 = new ScriptlerScript("dummy.groovy", Arrays.asList(scriptlerScriptParameters));
ChoiceParameter param001 = new ChoiceParameter("param001", "param001 description", "random-name",
scriptParam001, AbstractUnoChoiceParameter.PARAMETER_TYPE_SINGLE_SELECT, true);
scriptParam001, AbstractUnoChoiceParameter.PARAMETER_TYPE_SINGLE_SELECT, true, 1);
GroovyScript scriptParam002 = new GroovyScript(new SecureGroovyScript(SCRIPT_PARAM002, false, null),
new SecureGroovyScript(SCRIPT_FALLBACK_PARAM002, false, null));
CascadeChoiceParameter param002 = new CascadeChoiceParameter("param002", "param002 description", "random-name",
scriptParam002, AbstractUnoChoiceParameter.PARAMETER_TYPE_SINGLE_SELECT, "param001", true);
scriptParam002, AbstractUnoChoiceParameter.PARAMETER_TYPE_SINGLE_SELECT, "param001", true, 1);
ParametersDefinitionProperty param001Def = new ParametersDefinitionProperty(
Arrays.<ParameterDefinition>asList(param001, param002));
project.addProperty(param001Def);
Expand Down
Expand Up @@ -74,7 +74,7 @@ public void testScriptAccessingGlobalProperties() {
GroovyScript script = new GroovyScript(new SecureGroovyScript(SCRIPT, Boolean.FALSE, null),
new SecureGroovyScript(FALLBACK_SCRIPT, Boolean.FALSE, null));
ChoiceParameter param = new ChoiceParameter("param000", "description", "randomName", script,
CascadeChoiceParameter.ELEMENT_TYPE_FORMATTED_HIDDEN_HTML, true);
CascadeChoiceParameter.ELEMENT_TYPE_FORMATTED_HIDDEN_HTML, true, 0);

assertEquals(Arrays.asList("a", "b", "20:13:13").toString(), param.getChoices().values().toString());
}
Expand Down

0 comments on commit 24113d5

Please sign in to comment.