Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIXED JENKINS-32007][FIXED JENKINS-34242] Use a custom control to di…
…splay spinner and errors
- Loading branch information
Showing
10 changed files
with
265 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/org/jenkinsci/plugins/github_branch_source/FillErrorResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.jenkinsci.plugins.github_branch_source; | ||
|
||
import hudson.Util; | ||
import java.io.IOException; | ||
import java.util.Locale; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletResponse; | ||
import jenkins.model.Jenkins; | ||
import org.kohsuke.stapler.HttpResponse; | ||
import org.kohsuke.stapler.StaplerRequest; | ||
import org.kohsuke.stapler.StaplerResponse; | ||
|
||
// TODO replace with corresponding core functionality once Jenkins core has JENKINS-42443 | ||
class FillErrorResponse extends IOException implements HttpResponse { | ||
|
||
private final String message; | ||
private final boolean clearList; | ||
|
||
public FillErrorResponse(String message, boolean clearList) { | ||
this.message = message; | ||
this.clearList = clearList; | ||
} | ||
|
||
@Override | ||
public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node) | ||
throws IOException, ServletException { | ||
rsp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); | ||
rsp.setContentType("text/html;charset=UTF-8"); | ||
rsp.setHeader("X-Jenkins-Select-Error", clearList ? "clear" : "retain"); | ||
rsp.getWriter().print( | ||
"<div class=" + "error".toLowerCase(Locale.ENGLISH) + "><img src=\'" + req.getContextPath() | ||
+ Jenkins.RESOURCE_PATH + "/images/none.gif\' height=16 width=1>" + Util.escape(message) + | ||
"</div>"); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/resources/org/jenkinsci/plugins/github_branch_source/form/select.jelly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<!-- | ||
The MIT License | ||
Copyright (c) 2004-2017, Sun Microsystems, Inc., Kohsuke Kawaguchi, Bruce Chapman, Alan Harder, CloudBees, 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. | ||
--> | ||
<!-- TODO remove once Jenkins core has JENKINS-42443 --> | ||
<!-- Tomcat doesn't like us using the attribute called 'class' --> | ||
<?jelly escape-by-default='true'?> | ||
<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"> | ||
<st:documentation> | ||
Glorified <select> control that supports the data binding and AJAX updates. | ||
Your descriptor should have the 'doFillXyzItems' method, which returns a ListBoxModel | ||
representation of the items in your drop-down list box, and your instance field should | ||
hold the current value. | ||
|
||
<st:attribute name="clazz"> | ||
Additional CSS classes that the control gets. | ||
</st:attribute> | ||
<st:attribute name="field"> | ||
Used for databinding. | ||
</st:attribute> | ||
<st:attribute name="default"> | ||
The default value of the text box, in case both @value is and 'instance[field]' is null. | ||
</st:attribute> | ||
</st:documentation> | ||
|
||
<f:prepareDatabinding/> | ||
<st:adjunct includes="org.jenkinsci.plugins.github_branch_source.form.select.select"/> | ||
|
||
${descriptor.calcFillSettings(field,attrs)} <!-- this figures out the 'fillUrl' and 'fillDependsOn' attribute --> | ||
<j:set var="value" value="${attrs.value ?: instance[attrs.field] ?: attrs.default}" /> | ||
<m:select xmlns:m="jelly:hudson.util.jelly.MorphTagLibrary" | ||
class="setting-input ${attrs.checkUrl!=null?'validated':''} select2 ${attrs.clazz}" | ||
name="${attrs.name ?: '_.'+attrs.field}" | ||
value="${value}" | ||
ATTRIBUTES="${attrs}" EXCEPT="field clazz"> | ||
<j:if test="${value!=null}"> | ||
<option value="${value}">${value}</option> | ||
</j:if> | ||
</m:select> | ||
<!-- TODO consider customizedFields --> | ||
</j:jelly> |
11 changes: 11 additions & 0 deletions
11
src/main/resources/org/jenkinsci/plugins/github_branch_source/form/select/select.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* TODO remove once Jenkins core has JENKINS-42443 */ | ||
select.select2-loading { | ||
padding-left: 1.5em; | ||
padding-top: 0.5em; | ||
padding-bottom: 0.5em; | ||
color: transparent; | ||
background-image: url("spinner.gif"); | ||
background-repeat: no-repeat; | ||
background-position: 2px; | ||
} | ||
|
100 changes: 100 additions & 0 deletions
100
src/main/resources/org/jenkinsci/plugins/github_branch_source/form/select/select.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// TODO remove once Jenkins core has JENKINS-42443 | ||
// send async request to the given URL (which will send back serialized ListBoxModel object), | ||
// then use the result to fill the list box. | ||
function updateListBox2(listBox,url,config) { | ||
config = config || {}; | ||
config = object(config); | ||
var originalOnSuccess = config.onSuccess; | ||
var l = $(listBox); | ||
var status = findFollowingTR(listBox, "validation-error-area").firstChild.nextSibling; | ||
if (status.firstChild && status.firstChild.getAttribute('data-select2')) { | ||
status.innerHTML = ""; | ||
} | ||
config.onSuccess = function(rsp) { | ||
l.removeClassName("select2-loading"); | ||
var currentSelection = l.value; | ||
|
||
// clear the contents | ||
while(l.length>0) l.options[0] = null; | ||
|
||
var selectionSet = false; // is the selection forced by the server? | ||
var possibleIndex = null; // if there's a new option that matches the current value, remember its index | ||
var opts = eval('('+rsp.responseText+')').values; | ||
for( var i=0; i<opts.length; i++ ) { | ||
l.options[i] = new Option(opts[i].name,opts[i].value); | ||
if(opts[i].selected) { | ||
l.selectedIndex = i; | ||
selectionSet = true; | ||
} | ||
if (opts[i].value==currentSelection) | ||
possibleIndex = i; | ||
} | ||
|
||
// if no value is explicitly selected by the server, try to select the same value | ||
if (!selectionSet && possibleIndex!=null) | ||
l.selectedIndex = possibleIndex; | ||
|
||
if (originalOnSuccess!=undefined) | ||
originalOnSuccess(rsp); | ||
}; | ||
config.onFailure = function(rsp) { | ||
l.removeClassName("select2-loading"); | ||
status.innerHTML = rsp.responseText; | ||
if (status.firstChild) { | ||
status.firstChild.setAttribute('data-select2', 'true') | ||
} | ||
Behaviour.applySubtree(status); | ||
// deleting values can result in the data loss, so let's not do that unless instructed | ||
var header = rsp.getResponseHeader('X-Jenkins-Select-Error'); | ||
if (header && "clear".toUpperCase() === header.toUpperCase()) { | ||
for (var i = l.options.length - 1; i >= 0; i--) { | ||
l.remove(i); | ||
} | ||
} | ||
|
||
}; | ||
|
||
l.addClassName("select2-loading"); | ||
new Ajax.Request(url, config); | ||
} | ||
|
||
Behaviour.specify("SELECT.select2", 'select2', 1000, function(e) { | ||
|
||
function hasChanged(selectEl, originalValue) { | ||
var firstValue = selectEl.options[0].value; | ||
var selectedValue = selectEl.value; | ||
if (originalValue == "" && selectedValue == firstValue) { | ||
// There was no value pre-selected but after the call to updateListBox the first value is selected by | ||
// default. This must not be considered a change. | ||
return false; | ||
} else { | ||
return originalValue != selectedValue; | ||
} | ||
}; | ||
|
||
// controls that this SELECT box depends on | ||
refillOnChange(e,function(params) { | ||
var value = e.value; | ||
updateListBox2(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 | ||
} | ||
} | ||
|
||
fireEvent(e,"filled"); // let other interested parties know that the items have changed | ||
|
||
// if the update changed the current selection, others listening to this control needs to be notified. | ||
if (hasChanged(e, value)) { | ||
fireEvent(e,"change"); | ||
} | ||
} | ||
}); | ||
}); | ||
}); |
Binary file added
BIN
+1.62 KB
...in/resources/org/jenkinsci/plugins/github_branch_source/form/select/spinner.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.