From mboxrd@z Thu Jan 1 00:00:00 1970 From: rmccabe@sourceware.org Date: 30 Jun 2006 17:45:59 -0000 Subject: [Cluster-devel] conga/luci/homebase homebase_common.js Message-ID: <20060630174559.10123.qmail@sourceware.org> List-Id: To: cluster-devel.redhat.com MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit CVSROOT: /cvs/cluster Module name: conga Changes by: rmccabe at sourceware.org 2006-06-30 17:45:59 Added files: luci/homebase : homebase_common.js Log message: move common javascript into its own file Patches: http://sourceware.org/cgi-bin/cvsweb.cgi/conga/luci/homebase/homebase_common.js.diff?cvsroot=cluster&r1=NONE&r2=1.1 /cvs/cluster/conga/luci/homebase/homebase_common.js,v --> standard output revision 1.1 --- conga/luci/homebase/homebase_common.js +++ - 2006-06-30 17:45:59.417970000 +0000 @@ -0,0 +1,196 @@ +function error_dialog(errors) { + if (!errors || errors.length < 1) + return (null); + alert('The following errors were found:\n\n' + errors.join('\n')); + return (-1); +} + +function str_is_blank(str) { + return (!str || !str.replace(/\s/g, '')); +} + +function str_is_valid(str, valid_regex_str) { + if (!str || !valid_regex_str) + return (null); + var re = eval(valid_regex_str); + var invalid = str.replace(re, ''); + if (!invalid) + return (null); + return (invalid); +} + +function checkAllBoxes(str, val) { + var i = 0; + var element; + while ((element = document.getElementById(str + i++))) + element.checked = val; +} + +function checkChildren(parent_cont, parent_input) { + if (!parent_cont || !parent_input) + return + parent = document.getElementById(parent_cont); + children = parent.getElementsByTagName('input') + for (var i = 0 ; i < children.length ; i++) { + if (children[i] == parent_input) + continue; + if (children[i].type == 'checkbox') + children[i].checked = parent_input.checked; + } +} + +function hide_element(id) { + var elem = document.getElementById(id); + if (elem) + elem.style['visibility'] = 'hidden'; +} + +function isValidHost(str) { + var i = str.split('.'); + + if (i.length == 1) + return ('Hostnames must be fully qualified.'); + + if (i.length == 4 && !isNaN(parseInt(i[3]))) { + var o1 = parseInt(i[0]); + var o2 = parseInt(i[1]); + var o3 = parseInt(i[2]); + var o4 = parseInt(i[3]); + + if (isNaN(o1) || isNaN(o2) || isNaN(o3) || + ((o1 & 0xff) != o1) || + ((o2 & 0xff) != o2) || + ((o3 & 0xff) != o3) || + ((o4 & 0xff) != o4)) + { + return ('Invalid IP Address.'); + } + + return (null); + } + + if (!isNaN(parseInt(i[i.length - 1]))) + return ('Invalid IP Address.'); + + if (!str.match(/^[0-9A-Za-z][0-9A-Za-z.-]*$/)) + return ('Hostnames can contain only alphanumeric characters and hyphens.'); + + return (null); +} + +function allPasswdsSame(form) { + var cb = document.getElementById('allSameCheckBox'); + if (!cb) + return (-1); + var num_systems = form.numStorage.value; + + var state = cb.checked; + var passwd = document.getElementById('__SYSTEM0:Passwd').value; + if (!passwd || !state) + passwd = ''; + + for (var i = 1 ; i < num_systems ; i++) { + var element = document.getElementById('__SYSTEM' + i + ':Passwd') + if (element) { + element.value = passwd; + element.disabled = state; + } + } +} + +function pwd0Change(form) { + var element = document.getElementById('allSameCheckBox'); + if (element && element.checked) + allPasswdsSame(form); +} + +function addSystem(form) { + var sltab = document.getElementById('systemsTable'); + if (!sltab) + return; + var num_systems = form.numStorage.value; + + var newsys = document.createElement('input'); + newsys.setAttribute('style', 'padding:.20em !important;width:200px;'); + newsys.setAttribute('name', '__SYSTEM' + num_systems + ':Addr'); + newsys.setAttribute('id', '__SYSTEM' + num_systems + ':Addr'); + newsys.setAttribute('type', 'text'); + newsys.setAttribute('value', ''); + + var newsysp = document.createElement('input'); + newsysp.setAttribute('style', 'padding:.20em !important;width:160px;'); + newsysp.setAttribute('name', '__SYSTEM' + num_systems + ':Passwd'); + newsysp.setAttribute('id', '__SYSTEM' + num_systems + ':Passwd'); + newsysp.setAttribute('type', 'password'); + newsysp.setAttribute('value', ''); + + var allSameCB = document.getElementById('allSameCheckBox'); + if (allSameCB && allSameCB.checked) { + newsysp.setAttribute('value', document.getElementById('__SYSTEM0:Passwd').value); + newsysp.setAttribute('disabled', true); + } + + var newrow = document.createElement('tr'); + var hcol = document.createElement('td') + hcol.setAttribute('style', 'margin: 0em;padding: 0em 1em .33em 0em;background: #dee7ec;padding-left: .5em;text-align: left;'); + var pcol = document.createElement('td') + pcol.setAttribute('style', 'margin: 0em;padding: 0em 1em .33em 0em;background: #dee7ec;padding-left: .5em;text-align: left;'); + + hcol.appendChild(newsys) + pcol.appendChild(newsysp) + newrow.appendChild(hcol) + newrow.appendChild(pcol) + sltab.appendChild(newrow) + + form.numStorage.value = ++num_systems; + if (num_systems == 2) { + var temp = document.getElementById('allSameDiv'); + temp.style.visibility = 'visible'; + temp = document.getElementById('allSameCheckBox'); + temp.style.visibility = 'visible'; + } +} + +function validate_systems(form, errors) { + var allSameCB = document.getElementById('allSameCheckBox'); + var added_storage = new Array(); + var num_systems = form.numStorage.value; + + for (var i = 0 ; i < num_systems ; i++) { + var element = document.getElementById('__SYSTEM' + i + ':Addr'); + + if (!element) + continue; + + var pwdElem = document.getElementById('__SYSTEM' + i + ':Passwd'); + if (!element.value) { + if (pwdElem.value) { + if (!allSameCB.checked) { + errors.push('You entered a password, but no hostname for system ' + (i + 1)); + continue; + } else + pwdElem.value = ''; + } + + continue; + } else if (!pwdElem || !pwdElem.value) + errors.push('No password was given for \"' + element.value + '\"'); + else if (str_is_blank(pwdElem.value)) + errors.push('The password entered for \"' + element.value + '\" is blank.'); + + if (str_is_blank(element.value)) { + errors.push('You entered a blank hostname for system ' + (i + 1)); + element.value = ''; + } else { + var errmsg; + if ((errmsg = isValidHost(element.value))) + errors.push('\"' + element.value + '\" is not a valid hostname: ' + errmsg); + else { + pwdElem.disabled = false; + added_storage.push(element.value); + } + } + } + + return (added_storage); +}