Skip to content
Permalink
Browse files
[JENKINS-19835] added page-leave confirmation on the configuration page
  • Loading branch information
mcupak authored and kohsuke committed Oct 27, 2013
1 parent 99d62c7 commit 4c21bcee27cb36e1a8b7d687663b445b2f336d81
Showing 3 changed files with 66 additions and 2 deletions.
@@ -63,11 +63,12 @@ THE SOFTWARE.
<j:if test="${h.hasPermission(it,it.CONFIGURE)}">
<f:bottomButtonBar>
<!--<input type="button" name="StructureTest" value="Test" onclick="buildFormTree(this.form)" />-->
<f:submit value="${%Save}" />
<f:submit value="${%Save}"/>
<f:apply />
</f:bottomButtonBar>
</j:if>
</f:form>
<st:adjunct includes="lib.form.confirm" />
</l:main-panel>
</l:layout>
</j:jelly>
@@ -35,5 +35,5 @@ THE SOFTWARE.
</s:documentation>
<st:adjunct includes="lib.form.apply.apply"/>
<input type="hidden" name="core:apply" value="" />
<input type="button" value="${%Apply}" class="apply-button applyButton" /><!-- applyButton is legacy -->
<input type="button" value="${%Apply}" class="apply-button applyButton" name="Apply"/><!-- applyButton is legacy -->
</j:jelly>
@@ -0,0 +1,63 @@
var errorMessage = "Leave without saving?";
var needToConfirm = false;

window.onbeforeunload = confirmExit;
Event.observe(window, 'load', initConfirm);

function initConfirm() {
var configForm = document.getElementsByName("config")[0];

var buttons = configForm.getElementsByTagName("button");
var name;
for ( var i = 0; i < buttons.length; i++) {
name = buttons[i].parentNode.parentNode.getAttribute('name');
if (name == "Submit") {
addEventToElement(buttons[i], 'click', function() {
needToConfirm = false;
});
} else if (name == "Apply") {
} else {
addEventToElement(buttons[i], 'click', confirm);
}
}

var inputs = configForm.getElementsByTagName("input");
for ( var i = 0; i < inputs.length; i++) {
addEventToElement(inputs[i], 'change', confirm);
if (inputs[i].type == 'checkbox' || inputs[i].type == 'radio') {
addEventToElement(inputs[i], 'click', confirm);
} else {
addEventToElement(inputs[i], 'keydown', confirm);
}
}

inputs = configForm.getElementsByTagName("select");
for ( var i = 0; i < inputs.length; i++) {
addEventToElement(inputs[i], 'keydown', confirm);
addEventToElement(inputs[i], 'click', confirm);
}

inputs = configForm.getElementsByTagName("textarea");
for ( var i = 0; i < inputs.length; i++) {
addEventToElement(inputs[i], 'keydown', confirm);
}
}

function confirm() {
needToConfirm = true;
}

function addEventToElement(obj, evType, fn) {
if (obj.addEventListener) {
obj.addEventListener(evType, fn, false);
return true;
} else if (obj.attachEvent) {
return obj.attachEvent("on" + evType, fn);
}
}

function confirmExit() {
if (needToConfirm) {
return errorMessage;
}
}

0 comments on commit 4c21bce

Please sign in to comment.