Commit abd09370 by Scott

Clean up JavaScript for display rules

Remove code duplication.
Close #273.
parent faa1fb1c
...@@ -1479,35 +1479,55 @@ ...@@ -1479,35 +1479,55 @@
{ {
$function='qa_display_rule_'.count(@$qa_content['script_lines']); $function='qa_display_rule_'.count(@$qa_content['script_lines']);
$keysourceids=array(); $keysourceids = array();
$keysourceobjs = array();
$jsVar = '/[A-Za-z_][A-Za-z0-9_]*/';
foreach ($effects as $target => $sources) foreach ($effects as $target => $sources) {
if (preg_match_all('/[A-Za-z_][A-Za-z0-9_]*/', $sources, $matches)) // element names must be legal JS variable names if (preg_match_all($jsVar, $sources, $matches)) { // element names must be legal JS variable names
foreach ($matches[0] as $element) foreach ($matches[0] as $element) {
$keysourceids[$element]=true; if (!in_array($element, $keysourceids))
$keysourceids[] = $element;
}
}
}
$funcscript=array("function ".$function."(first) {"); // build the Javascripts $funcscript = array();
$loadscript=array(); $loadscript = array();
foreach ($keysourceids as $key => $dummy) { $funcscript[] = "var qa_checkboxids = " . json_encode($keysourceids) . ";";
$funcscript[]="\tvar e=document.getElementById(".qa_js($key).");"; $funcscript[] = "var qa_options = {};";
$funcscript[]="\tvar ".$key."=e && (e.checked || (e.options && e.options[e.selectedIndex].value));";
$loadscript[]="jQuery(".qa_js('#'.$key).").click(function() {"; // set default state of options
$loadscript[]="\t".$function."(false);"; $loadscript[] = "for (var i = 0; i < qa_checkboxids.length; i++) {";
$loadscript[]="});"; $loadscript[] = "\tjQuery('#'+qa_checkboxids[i]).click(function() { ".$function."(false); });";
} $loadscript[] = "}";
$funcscript[] = "function {$function}_show(target, show, first) {";
$funcscript[] = "\tvar e = document.getElementById(target);";
$funcscript[] = "\tif (e) {";
$funcscript[] = "\t\tif (first || e.nodeName == 'SPAN') { e.style.display = (show ? '' : 'none'); }";
$funcscript[] = "\t\telse if (show) { $(e).fadeIn(); }";
$funcscript[] = "\t\telse { $(e).fadeOut(); }";
$funcscript[] = "\t}";
$funcscript[] = "}";
$funcscript[] = "function {$function}(first) {";
$funcscript[] = "\tfor (var i = 0; i < qa_checkboxids.length; i++) {";
$funcscript[] = "\t\tvar e = document.getElementById(qa_checkboxids[i]);";
$funcscript[] = "\t\tqa_options[qa_checkboxids[i]] = e && (e.checked || (e.options && e.options[e.selectedIndex].value));";
$funcscript[] = "\t}";
foreach ($effects as $target => $sources) { foreach ($effects as $target => $sources) {
$funcscript[]="\tvar e=document.getElementById(".qa_js($target).");"; $sourcesobj = preg_replace($jsVar, 'qa_options.$0', $sources);
$funcscript[]="\tif (e) { var d=(".$sources."); if (first || (e.nodeName=='SPAN')) { e.style.display=d ? '' : 'none'; } else { if (d) { $(e).fadeIn(); } else { $(e).fadeOut(); } } }"; $funcscript[] = "\t".$function."_show(".qa_js($target).", (".$sourcesobj."), first);";
} }
$funcscript[]="}"; $funcscript[] = "}";
$loadscript[]=$function."(true);"; $loadscript[] = "{$function}(true);";
$qa_content['script_lines'][]=$funcscript; $qa_content['script_lines'][] = $funcscript;
$qa_content['script_onloads'][]=$loadscript; $qa_content['script_onloads'][] = $loadscript;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment