Skip to content

Commit

Permalink
WW-4532 Javascript generated by s:doubleselect has global variables t…
Browse files Browse the repository at this point in the history
…hat can interfer with the rest of the app

- Follow jslint recommendations in javascript resources
  • Loading branch information
jogep committed Aug 4, 2015
1 parent 49c43b3 commit 0469947
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 194 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*
* $Id$
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
Expand All @@ -19,57 +17,58 @@
* under the License.
*/

function printResult(result_string) {
var result_div = document.getElementById('wc-result');
var result_array = result_string.split('\n');

var new_command = document.getElementById('wc-command').value;
result_div.appendChild(document.createTextNode(new_command));
result_div.appendChild(document.createElement('br'));
function printResult(resultString) {
var resultDiv = document.getElementById('wc-result');
var resultArray = resultString.split('\n');

for (var line_index in result_array) {
var result_wrap = document.createElement('pre')
line = document.createTextNode(result_array[line_index]);
result_wrap.appendChild(line);
result_div.appendChild(result_wrap);
result_div.appendChild(document.createElement('br'));
var newCommand = document.getElementById('wc-command').value;
resultDiv.appendChild(document.createTextNode(newCommand));
resultDiv.appendChild(document.createElement('br'));

}
result_div.appendChild(document.createTextNode(':-> '));
for (var lineIndex in resultArray) {
if (resultArray.hasOwnProperty(lineIndex)) {
var resultWrap = document.createElement('pre'),
line = document.createTextNode(resultArray[lineIndex]);
resultWrap.appendChild(line);
resultDiv.appendChild(resultWrap);
resultDiv.appendChild(document.createElement('br'));
}
}
resultDiv.appendChild(document.createTextNode(':-> '));

result_div.scrollTop = result_div.scrollHeight;
document.getElementById('wc-command').value = '';
resultDiv.scrollTop = resultDiv.scrollHeight;
document.getElementById('wc-command').value = '';
}

function keyEvent(event, url) {
switch (event.keyCode) {
case 13:
var the_shell_command = document.getElementById('wc-command').value;
if (the_shell_command) {
commands_history[commands_history.length] = the_shell_command;
history_pointer = commands_history.length;
var the_url = url ? url : window.opener.location.pathname;
jQuery.post(the_url, jQuery("#wc-form").serialize(), function (data) {
printResult(data);
});
}
break;
case 38: // this is the arrow up
if (history_pointer > 0) {
history_pointer--;
document.getElementById('wc-command').value = commands_history[history_pointer];
}
break;
case 40: // this is the arrow down
if (history_pointer < commands_history.length - 1) {
history_pointer++;
document.getElementById('wc-command').value = commands_history[history_pointer];
}
break;
default:
break;
}
switch (event.keyCode) {
case 13:
var theShellCommand = document.getElementById('wc-command').value;
if (theShellCommand) {
commandsHistory[commandsHistory.length] = theShellCommand;
historyPointer = commandsHistory.length;
var theUrl = url ? url : window.opener.location.pathname;
jQuery.post(theUrl, jQuery("#wc-form").serialize(), function (data) {
printResult(data);
});
}
break;
case 38: // this is the arrow up
if (historyPointer > 0) {
historyPointer--;
document.getElementById('wc-command').value = commandsHistory[historyPointer];
}
break;
case 40: // this is the arrow down
if (historyPointer < commandsHistory.length - 1) {
historyPointer++;
document.getElementById('wc-command').value = commandsHistory[historyPointer];
}
break;
default:
break;
}
}

var commands_history = [];
var history_pointer;
var commandsHistory = [];
var historyPointer;
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*
* $Id$
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
Expand All @@ -21,15 +19,15 @@

function addOption(objTextBox, objTargetElement) {
var value = objTextBox.value;
if(value != null && value != '') {
if (value !== null && value !== '') {
objTargetElement.options[objTargetElement.options.length] = new Option(value, value);
objTextBox.value = '';
}
}

function removeOptions(objTargetElement) {
var i = 0;
while(objTargetElement.options.length > i) {
while (objTargetElement.options.length > i) {
if (objTargetElement.options[i].selected) {
objTargetElement.options.remove(i);
} else {
Expand All @@ -39,7 +37,7 @@ function removeOptions(objTargetElement) {
}

function removeAllOptions(objTargetElement) {
while(objTargetElement.options.length != 0) {
while (objTargetElement.options.length !== 0) {
objTargetElement.options[0] = null;
}
}
Expand All @@ -48,85 +46,90 @@ function selectAllOptionsExceptSome(objTargetElement, type, ptn) {
var test = compile(ptn);
for (var i = 0; i < objTargetElement.length; i++) {
var opt = objTargetElement.options[i];
if ((type == 'key' && !test(opt.value)) ||
(type == 'text' && !test(opt.text))) {
if ((type === 'key' && !test(opt.value)) ||
(type === 'text' && !test(opt.text))) {
opt.selected = true;
} else {
opt.selected = false;
}
}
}
return false;
}

function compile(ptn) {
if (ptn != undefined) {
if (ptn == '' || !window.RegExp) {
return function(val) { return val == ptn; }
if (ptn !== undefined) {
if (ptn === '' || !window.RegExp) {
return function (val) {
return val === ptn;
};
} else {
var reg = new RegExp("^" + ptn + "$");
return function (val) {
if (val == '') { // ignore empty option added by template
return true;
if (val === '') { // ignore empty option added by template
return true;
}
return reg.test(val); }
return reg.test(val);
};
}
}
return function(val) { return false; }
return function (val) {
return false;
};
}

function selectAllOptions(objTargetElement) {
for (var i = 0; i < objTargetElement.length; i++) {
if (objTargetElement.options[i].value != '') {
objTargetElement.options[i].selected = true;
}
if (objTargetElement.options[i].value !== '') {
objTargetElement.options[i].selected = true;
}
}
return false;
}

function moveOptionUp(objTargetElement, type, ptn) {
var test = compile(ptn);
for (i=0; i<objTargetElement.length; i++) {
if (objTargetElement[i].selected) {
var v;
if (i != 0 && !objTargetElement[i-1].selected) {
if (type == 'key') {
v = objTargetElement[i-1].value
}
else {
v = objTargetElement[i-1].text;
}
if (!test(v)) {
swapOptions(objTargetElement,i,i-1);
}
}
}
}
var test = compile(ptn);
for (i = 0; i < objTargetElement.length; i++) {
if (objTargetElement[i].selected) {
var v;
if (i !== 0 && !objTargetElement[i - 1].selected) {
if (type === 'key') {
v = objTargetElement[i - 1].value;
}
else {
v = objTargetElement[i - 1].text;
}
if (!test(v)) {
swapOptions(objTargetElement, i, i - 1);
}
}
}
}
}

function moveOptionDown(objTargetElement, type, ptn) {
var test = compile(ptn);
for (i=(objTargetElement.length-1); i>= 0; i--) {
if (objTargetElement[i].selected) {
var v;
if ((i != (objTargetElement.length-1)) && !objTargetElement[i+1].selected) {
if (type == 'key') {
v = objTargetElement[i].value
}
else {
v = objTargetElement[i].text;
}
if (!test(v)) {
swapOptions(objTargetElement,i,i+1);
}
}
}
}
var test = compile(ptn);
for (i = (objTargetElement.length - 1); i >= 0; i--) {
if (objTargetElement[i].selected) {
var v;
if ((i !== (objTargetElement.length - 1)) && !objTargetElement[i + 1].selected) {
if (type === 'key') {
v = objTargetElement[i].value;
}
else {
v = objTargetElement[i].text;
}
if (!test(v)) {
swapOptions(objTargetElement, i, i + 1);
}
}
}
}
}

function swapOptions(objTargetElement, first, second) {
var opt = objTargetElement.options;
var temp = new Option(opt[first].text, opt[first].value, opt[first].defaultSelected, opt[first].selected);
var temp2= new Option(opt[second].text, opt[second].value, opt[second].defaultSelected, opt[second].selected);
opt[first] = temp2;
opt[second] = temp;
var opt = objTargetElement.options;
var temp = new Option(opt[first].text, opt[first].value, opt[first].defaultSelected, opt[first].selected);
var temp2 = new Option(opt[second].text, opt[second].value, opt[second].defaultSelected, opt[second].selected);
opt[first] = temp2;
opt[second] = temp;
}
Loading

0 comments on commit 0469947

Please sign in to comment.