fixed width of custom select dropdown elements (issue #29)

This commit is contained in:
larsz 2012-01-15 22:34:45 +02:00
parent a0b61f6d0e
commit 9ab2787e5e

View file

@ -1,161 +1,164 @@
/* /*
CUSTOM FORM ELEMENTS CUSTOM FORM ELEMENTS
Created by Ryan Fait Created by Ryan Fait
www.ryanfait.com www.ryanfait.com
The only things you may need to change in this file are the following The only things you may need to change in this file are the following
variables: checkboxHeight, radioHeight and selectWidth (lines 24, 25, 26) variables: checkboxHeight, radioHeight and selectWidth (lines 24, 25, 26)
The numbers you set for checkboxHeight and radioHeight should be one quarter The numbers you set for checkboxHeight and radioHeight should be one quarter
of the total height of the image want to use for checkboxes and radio of the total height of the image want to use for checkboxes and radio
buttons. Both images should contain the four stages of both inputs stacked buttons. Both images should contain the four stages of both inputs stacked
on top of each other in this order: unchecked, unchecked-clicked, checked, on top of each other in this order: unchecked, unchecked-clicked, checked,
checked-clicked. checked-clicked.
You may need to adjust your images a bit if there is a slight vertical You may need to adjust your images a bit if there is a slight vertical
movement during the different stages of the button activation. movement during the different stages of the button activation.
The value of selectWidth should be the width of your select list image. The value of selectWidth should be the width of your select list image.
Visit http://ryanfait.com/ for more information. Visit http://ryanfait.com/ for more information.
*/ */
var checkboxHeight = "25"; var checkboxHeight = "25";
var radioHeight = "25"; var radioHeight = "25";
var selectWidth = "210"; var selectWidth = "230";
/* No need to change anything after this */ /* No need to change anything after this */
document.write('<style type="text/css">input.styled { display: none; } select.styled { position: relative; width: ' + selectWidth + 'px; opacity: 0; filter: alpha(opacity=0); z-index: 5; } .disabled { opacity: 0.5; filter: alpha(opacity=50); }</style>'); document.write('<style type="text/css">input.styled { display: none; } select.styled { position: relative; width: ' + selectWidth + 'px; opacity: 0; filter: alpha(opacity=0); z-index: 5; } .disabled { opacity: 0.5; filter: alpha(opacity=50); }</style>');
var Custom = { var Custom = {
init: function() { init: function() {
var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active; var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
for(a = 0; a < inputs.length; a++) { for(a = 0; a < inputs.length; a++) {
if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && $(inputs[a]).hasClass("styled") && !$(inputs[a]).hasClass("style-applied")) { if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && $(inputs[a]).hasClass("styled") && !$(inputs[a]).hasClass("style-applied")) {
$(inputs[a]).addClass('style-applied'); $(inputs[a]).addClass('style-applied');
span[a] = document.createElement("span"); span[a] = document.createElement("span");
span[a].className = inputs[a].type; span[a].className = inputs[a].type;
if ($(inputs[a]).attr('class').indexOf('do_action_toggle_suspend') != -1) { if ($(inputs[a]).attr('class').indexOf('do_action_toggle_suspend') != -1) {
span[a].className += ' do_action_toggle_suspend'; // save toggle functionality span[a].className += ' do_action_toggle_suspend'; // save toggle functionality
} }
if ($(inputs[a]).attr('class').indexOf('do_action_toggle_batch_selector') != -1) { if ($(inputs[a]).attr('class').indexOf('do_action_toggle_batch_selector') != -1) {
span[a].className += ' do_action_toggle_batch_selector'; // save toggle functionality span[a].className += ' do_action_toggle_batch_selector'; // save toggle functionality
} }
if ($(inputs[a]).attr('class').indexOf('do_action_toggle_ssl_support') != -1) {
span[a].className += ' do_action_toggle_ssl_support'; // save toggle functionality
if(inputs[a].checked == true) { }
if(inputs[a].type == "checkbox") {
position = "0 -" + (checkboxHeight*2) + "px";
span[a].style.backgroundPosition = position; if(inputs[a].checked == true) {
} else { if(inputs[a].type == "checkbox") {
position = "0 -" + (radioHeight*2) + "px"; position = "0 -" + (checkboxHeight*2) + "px";
span[a].style.backgroundPosition = position; span[a].style.backgroundPosition = position;
} } else {
} position = "0 -" + (radioHeight*2) + "px";
inputs[a].parentNode.insertBefore(span[a], inputs[a]); span[a].style.backgroundPosition = position;
inputs[a].onchange = Custom.clear; }
if(!inputs[a].getAttribute("disabled")) { }
span[a].onmousedown = Custom.pushed; inputs[a].parentNode.insertBefore(span[a], inputs[a]);
span[a].onmouseup = Custom.check; inputs[a].onchange = Custom.clear;
} else { if(!inputs[a].getAttribute("disabled")) {
span[a].className = span[a].className += " disabled"; span[a].onmousedown = Custom.pushed;
} span[a].onmouseup = Custom.check;
} } else {
} span[a].className = span[a].className += " disabled";
inputs = document.getElementsByTagName("select"); }
try { }
for(a = 0; a < inputs.length; a++) { }
if($(inputs[a]).hasClass("styled")) { inputs = document.getElementsByTagName("select");
option = inputs[a].getElementsByTagName("option"); try {
active = option[0].childNodes[0].nodeValue; for(a = 0; a < inputs.length; a++) {
textnode = document.createTextNode(active); if($(inputs[a]).hasClass("styled")) {
for(b = 0; b < option.length; b++) { option = inputs[a].getElementsByTagName("option");
if(option[b].selected == true) { active = option[0].childNodes[0].nodeValue;
textnode = document.createTextNode(option[b].childNodes[0].nodeValue); textnode = document.createTextNode(active);
} for(b = 0; b < option.length; b++) {
} if(option[b].selected == true) {
span[a] = document.createElement("span"); textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
span[a].className = "select"; }
span[a].id = "select-" + inputs[a].name + a; }
span[a].appendChild(textnode); span[a] = document.createElement("span");
inputs[a].parentNode.insertBefore(span[a], inputs[a]); span[a].className = "select";
inputs[a].id = inputs[a].name + a; span[a].id = "select-" + inputs[a].name + a;
if(!inputs[a].getAttribute("disabled")) { span[a].appendChild(textnode);
inputs[a].onchange = Custom.choose; inputs[a].parentNode.insertBefore(span[a], inputs[a]);
} else { inputs[a].id = inputs[a].name + a;
inputs[a].previousSibling.className = inputs[a].previousSibling.className += " disabled"; if(!inputs[a].getAttribute("disabled")) {
} inputs[a].onchange = Custom.choose;
} } else {
} inputs[a].previousSibling.className = inputs[a].previousSibling.className += " disabled";
document.onmouseup = Custom.clear; }
} }
catch(e){ /* */ } }
}, document.onmouseup = Custom.clear;
pushed: function() { }
element = this.nextSibling; catch(e){ /* */ }
if(element.checked == true && element.type == "checkbox") { },
this.style.backgroundPosition = "0 -" + checkboxHeight*3 + "px"; pushed: function() {
} else if(element.checked == true && element.type == "radio") { element = this.nextSibling;
this.style.backgroundPosition = "0 -" + radioHeight*3 + "px"; if(element.checked == true && element.type == "checkbox") {
} else if(element.checked != true && element.type == "checkbox") { this.style.backgroundPosition = "0 -" + checkboxHeight*3 + "px";
this.style.backgroundPosition = "0 -" + checkboxHeight + "px"; } else if(element.checked == true && element.type == "radio") {
} else { this.style.backgroundPosition = "0 -" + radioHeight*3 + "px";
this.style.backgroundPosition = "0 -" + radioHeight + "px"; } else if(element.checked != true && element.type == "checkbox") {
} this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
}, } else {
check: function() { this.style.backgroundPosition = "0 -" + radioHeight + "px";
element = this.nextSibling; }
if(element.checked == true && element.type == "checkbox") { },
this.style.backgroundPosition = "0 0"; check: function() {
element.checked = false; element = this.nextSibling;
} else { if(element.checked == true && element.type == "checkbox") {
if(element.type == "checkbox") { this.style.backgroundPosition = "0 0";
this.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px"; element.checked = false;
} else { } else {
this.style.backgroundPosition = "0 -" + radioHeight*2 + "px"; if(element.type == "checkbox") {
group = this.nextSibling.name; this.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
inputs = document.getElementsByTagName("input"); } else {
for(a = 0; a < inputs.length; a++) { this.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
if(inputs[a].name == group && inputs[a] != this.nextSibling) { group = this.nextSibling.name;
inputs[a].previousSibling.style.backgroundPosition = "0 0"; inputs = document.getElementsByTagName("input");
} for(a = 0; a < inputs.length; a++) {
} if(inputs[a].name == group && inputs[a] != this.nextSibling) {
} inputs[a].previousSibling.style.backgroundPosition = "0 0";
element.checked = true; }
} }
}, }
clear: function() { element.checked = true;
inputs = document.getElementsByTagName("input"); }
for(var b = 0; b < inputs.length; b++) { },
if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") { clear: function() {
inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px"; inputs = document.getElementsByTagName("input");
} else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") { for(var b = 0; b < inputs.length; b++) {
inputs[b].previousSibling.style.backgroundPosition = "0 0"; if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
} else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") { inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px"; } else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") {
} else if(inputs[b].type == "radio" && inputs[b].className == "styled") { inputs[b].previousSibling.style.backgroundPosition = "0 0";
inputs[b].previousSibling.style.backgroundPosition = "0 0"; } else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
} inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
} } else if(inputs[b].type == "radio" && inputs[b].className == "styled") {
}, inputs[b].previousSibling.style.backgroundPosition = "0 0";
choose: function() { }
option = this.getElementsByTagName("option"); }
for(d = 0; d < option.length; d++) { },
if(option[d].selected == true) { choose: function() {
// option = this.getElementsByTagName("option");
var expr = '#select-' + this.id; for(d = 0; d < option.length; d++) {
fb.log(expr); if(option[d].selected == true) {
$(expr).text(option[d].childNodes[0].nodeValue); //
// bad! var expr = '#select-' + this.id;
//document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue; fb.log(expr);
} $(expr).text(option[d].childNodes[0].nodeValue);
} // bad!
} //document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
} }
window.onload = Custom.init; }
}
}
window.onload = Custom.init;