adding lockoptions functionality to formslib library and added lockoptionsall() function which allows multiple dependencies for one dependent element and handles that properly.

This commit is contained in:
jamiesensei 2006-11-21 09:17:46 +00:00
parent c70095e440
commit 50ef8eb9bd
2 changed files with 71 additions and 5 deletions

View file

@ -448,6 +448,7 @@ class moodleform_mod extends moodleform {
*/ */
class MoodleQuickForm extends HTML_QuickForm_DHTMLRulesTableless { class MoodleQuickForm extends HTML_QuickForm_DHTMLRulesTableless {
var $_types = array(); var $_types = array();
var $_dependencies = array();
/** /**
@ -794,13 +795,49 @@ function validate_' . $this->_attributes['id'] . '(frm) {
} }
function getLockOptionStartScript(){ function getLockOptionStartScript(){
return ''; $js = '<script type="text/javascript" language="javascript">'."\n";
$js .= "var ".$this->getAttribute('id')."items= {";
foreach ($this->_dependencies as $dependentOn => $elements){
$js .= $dependentOn.' : {dependents :[';
foreach ($elements as $element){
$js.="'".$element['dependent']."', ";
}
$js=rtrim($js, ', ');
$js .= "],\n";
$js .= "condition : '{$element['condition']}'},\n";
};
$js=rtrim($js, ",\n");
$js .= '};'."\n";
$js .='</script>'."\n";
return $js;
} }
function getLockOptionEndScript(){ function getLockOptionEndScript(){
$js = '<script type="text/javascript" language="javascript">'."\n";
return ''; $js .="lockoptionsall('".$this->getAttribute('id')."');\n";
$js .='</script>'."\n";
return $js;
}
function addDependency($elementName, $dependentOn, $condition='checked'){
$el=$this->getElement($elementName);
if (is_a($el, 'HTML_QuickForm_group')){
$group=$el;
$els=$group->getElements();
foreach (array_keys($els) as $elkey){
$dependentNames[]=array('dependent'=>$group->getElementName($elkey),
'condition'=>$condition);
}
}else{
$dependentNames=array(array('dependent'=>$el->getName(),
'condition'=>$condition));
}
foreach ($dependentNames as $dependentName){
$dependentOnEl=$this->getElement($dependentOn);
$name=$dependentOnEl->getName();
$dependentOnEl->updateAttributes(array('onClick'=>"return lockoptionsall('".$this->getAttribute('id')."');\n"));
$this->_dependencies[$name][]=$dependentName;
}
} }
} }
/** /**

View file

@ -58,6 +58,35 @@ function lockoptions(form, master, subitems) {
return(true); return(true);
} }
function lockoptionsall(formid) {
var lock=new Object();
var items=eval(formid+'items');
var form=document.forms[formid];
for (var master in items){
var subitems=items[master].dependents;
var thislock;
if (items[master].condition=='checked'){
thislock=!form.elements[master].checked;
}else{
thislock=!(form.elements[master].value==items[master].condition);
}
for (var i=0; i<subitems.length; i++) {
if (lock[subitems[i]]!=null){
lock[subitems[i]] = thislock || lock[subitems[i]];
}else{
lock[subitems[i]] = thislock;
}
}
for (var item in lock) {
form.elements[item].disabled=lock[item];
}
}
return(true);
}
function lockoption(form,item) { function lockoption(form,item) {
eval("document."+form+"."+item+".disabled=true");/* IE thing */ eval("document."+form+"."+item+".disabled=true");/* IE thing */
if(document.forms[form].elements['h'+item]) { if(document.forms[form].elements['h'+item]) {
@ -115,7 +144,7 @@ function confirm_if(expr, message) {
/* /*
findParentNode (start, elementName, elementClass, elementID) findParentNode (start, elementName, elementClass, elementID)
Travels up the DOM hierarchy to find a parent element with the Travels up the DOM hierarchy to find a parent element with the
specified tag name, class, and id. All conditions must be met, specified tag name, class, and id. All conditions must be met,
but any can be ommitted. Returns the BODY element if no match but any can be ommitted. Returns the BODY element if no match