Skip to content

Instantly share code, notes, and snippets.

@zhengjunwei
Created October 14, 2014 13:24
Show Gist options
  • Save zhengjunwei/3d9f3bdc3b5088e1e19b to your computer and use it in GitHub Desktop.
Save zhengjunwei/3d9f3bdc3b5088e1e19b to your computer and use it in GitHub Desktop.
eee
(function(g,undefined){
//内部私有方法
function findControl(labelText, form){
var labels = document.getElementsByTagName("Label");
for(var i=0,l=labels.length; i<l; i++){
var label = labels[i];
var text = label.innerText;
if(text.trim() == labelText){
var labelFor = label.getAttribute("for");
if(labelFor){
var control = document.getElementById(labelFor);
if(control){
return control;
}else{
var controls = document.getElementsByName(labelFor);
return Array.prototype.slice.call(controls, 0);
}
}
}
}
}
function show(element){
element.style.display = "auto";
}
function hide(element){
element.style.display = "none";
}
function disable(element){
element.disabled = true;
}
function enable(element){
element.disabled = false;
}
var _alias = {};
function alias(element, name){
_alias.name = element;
}
function clearAlias(name){
delete _alias.name;
}
function _isArray(o){
return Array.prototype.isPrototypeOf(o);
}
function _toArray(o){
return Array.prototype.slice.call(o, 0);
}
function getValueByLabel(labelText){
var control = findControl(labelText);
if(_isArray(control)){
var controls = control;
var values = [];
for(var i=0,l=controls.length; i<l; i++){
values.push(controls[i].value);
}
return values;
}else{
return control.value;
}
}
function getValueByName(name){
var controls = document.getElementsByName(name);
if(controls.length>0){
var type = controls[0].type;
if(type === "radio"){
for(var i=0,l=controls.length; i<l; i++){
var aRadio = controls[i];
if(aRadio.checked){
return aRadio.value;
}
}
}else if(type === "checkbox"){
var values = [];
for(var i=0,l=controls.length; i<l; i++){
var aCheckbox = controls[i];
if(aCheckbox.checked){
values.push(aCheckbox.value);
}
}
return values;
}else{
return controls[0].value;
}
}
}
var Fn = function(){};
Fn.prototype.submit = function(FirstLegendText){
var legends = document.getElementsByTagName("Legend");
for(var i=0,l=legends.length; i<l; i++){
if(legends[i].innerText.trim() === FirstLegendText){
return legends[i].form.submit();
}
}
}
Fn.prototype.findControlLabel = function(controlId){
var labels = document.getElementsByTagName("Label");
for(var i=0,l=labels.length; i<l; i++){
var aLabel = labels[i];
if(aLabel.getAttribute("for") === controlId){
return aLabel.innerText;
}
}
}
Fn.prototype.getChoosedOf = function(labelText){
var radios = findControl(labelText);
for(var i=0,l=radios.length; i<l; i++){
var aRadio = radios[i];
if(aRadio.checked){
return findControlLabel(aRadio.id);
}
}
}
Fn.prototype.getSelectedOf = function(labelText){
var select = findControl(labelText);
if(select){
var options = select.options;
if(select.multiple){
var selected = [];
for(var i=0,l=options.length; i<l; i++){
var aOption = options[i];
if(aOption.selected){
selected.push(Option.text);
}
}
return selected;
}else{
for(var i=0,l=options.length; i<l; i++){
var aOption = options[i];
if(aOption.selected){
return aOption.text;
}
}
}
}
}
Fn.prototype.getCheckedOf = function(labelText){
var checkboxes = findControl(labelText);
var checked = [];
for(var i=0,l=checkboxes.length; i<l; i++){
var aCheckbox = checkboxes[i];
if(aCheckbox.checked){
checked.push(findControlLabel(aCheckbox.id));
}
}
return checked;
}
Fn.prototype.clearChecked = function(controlsLabel){
_toArray(findControl(controlsLabel)).forEach(function(checkbox){
checkbox.checked = false;
});
}
Fn.prototype.select = function(controlLabel, selectOptions){
if(!_isArray(selectOptions)){
selectOptions=[selectOptions];
}
var aField = findControl(controlLabel);
if(aField){
var ableOptions = aField.options;
selectOptions.forEach(function(selectOption){
for(var i=0,l=ableOptions.length; i<l; i++){
var option = ableOptions[i];
if(option.text == selectOption){
option.selected = true;
break;
}
}
});
}
}
Fn.prototype.fillIn = function(controlLabel, text){
var aField = findControl(controlLabel);
if(aField){
aField.value = text;
}
}
Fn.prototype.choose = function(controlLabel){
var aField = findControl(controlLabel);
if(aField){
aField.checked = true;
}
}
Fn.prototype.check = function(controlLabel){
if(arguments.length > 1){
var controlLabels = Array.prototype.slice.call(arguments, 0);
controlLabels.forEach(function(item){
check(item);
});
}else{
var aField = findControl(controlLabel);
if(aField){
aField.checked = true;
}
}
}
Fn.prototype.uncheck = function(controlLabel){
if(arguments.length > 1){
var controlLabels = Array.prototype.slice.call(arguments, 0);
controlLabels.forEach(function(item){
uncheck(item);
});
}else{
var aField = findControl(controlLabel);
if(aField){
aField.checked = false;
}
}
}
window.dodo = window.dodo || new Fn();
})(global || window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment