Commit 3011b268 by Scott

Reformat code in qa_set_display_rules

parent abd09370
...@@ -1470,21 +1470,23 @@ ...@@ -1470,21 +1470,23 @@
} }
/**
* Adds JavaScript to the page to handle toggling of form fields based on other fields.
*
* @param array $qa_content Page content array.
* @param array $effects List of rules for element toggling, with the structure:
* array('target1' => 'source1', 'target2' => 'source2', ...)
* When the source expression is true, the DOM element ID represented by target is shown. The
* source can be a combination of ID as a JS expression.
*/
function qa_set_display_rules(&$qa_content, $effects) function qa_set_display_rules(&$qa_content, $effects)
/*
For each [target] => [source] in $effects, set up $qa_content so that the visibility of the DOM element ID
target is equal to the checked state or boolean-casted value of the DOM element ID source. Each source can
also combine multiple DOM IDs using JavaScript(=PHP) operators. This is twisted but rather convenient.
*/
{ {
$function='qa_display_rule_'.count(@$qa_content['script_lines']);
$keysourceids = array(); $keysourceids = array();
$keysourceobjs = array(); $jsVarRegex = '/[A-Za-z_][A-Za-z0-9_]*/';
$jsVar = '/[A-Za-z_][A-Za-z0-9_]*/';
foreach ($effects as $target => $sources) { foreach ($effects as $target => $sources) {
if (preg_match_all($jsVar, $sources, $matches)) { // element names must be legal JS variable names // element names must be legal JS variable names
if (preg_match_all($jsVarRegex, $sources, $matches)) {
foreach ($matches[0] as $element) { foreach ($matches[0] as $element) {
if (!in_array($element, $keysourceids)) if (!in_array($element, $keysourceids))
$keysourceids[] = $element; $keysourceids[] = $element;
...@@ -1492,39 +1494,43 @@ ...@@ -1492,39 +1494,43 @@
} }
} }
$funcscript = array(); $function = 'qa_display_rule_'.count(@$qa_content['script_lines']);
$loadscript = array();
$funcscript = array(
$funcscript[] = "var qa_checkboxids = " . json_encode($keysourceids) . ";"; // set up variables
$funcscript[] = "var qa_options = {};"; "var qa_checkboxids = " . json_encode($keysourceids) . ";",
// set default state of options // show/hide an element
$loadscript[] = "for (var i = 0; i < qa_checkboxids.length; i++) {"; "function {$function}_show(target, show, first) {",
$loadscript[] = "\tjQuery('#'+qa_checkboxids[i]).click(function() { ".$function."(false); });"; "\tvar e = document.getElementById(target);",
$loadscript[] = "}"; "\tif (e) {",
"\t\tif (first || e.nodeName == 'SPAN') { e.style.display = (show ? '' : 'none'); }",
$funcscript[] = "function {$function}_show(target, show, first) {"; "\t\telse if (show) { $(e).fadeIn(); }",
$funcscript[] = "\tvar e = document.getElementById(target);"; "\t\telse { $(e).fadeOut(); }",
$funcscript[] = "\tif (e) {"; "\t}",
$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[] = "}";
// check and set all display rules
$funcscript[] = "function {$function}(first) {"; $funcscript[] = "function {$function}(first) {";
$funcscript[] = "\tvar qa_options = {};";
$funcscript[] = "\tfor (var i = 0; i < qa_checkboxids.length; i++) {"; $funcscript[] = "\tfor (var i = 0; i < qa_checkboxids.length; i++) {";
$funcscript[] = "\t\tvar e = document.getElementById(qa_checkboxids[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\tqa_options[qa_checkboxids[i]] = e && (e.checked || (e.options && e.options[e.selectedIndex].value));";
$funcscript[] = "\t}"; $funcscript[] = "\t}";
foreach ($effects as $target => $sources) { foreach ($effects as $target => $sources) {
$sourcesobj = preg_replace($jsVar, 'qa_options.$0', $sources); $sourcesobj = preg_replace($jsVarRegex, 'qa_options.$0', $sources);
$funcscript[] = "\t".$function."_show(".qa_js($target).", (".$sourcesobj."), first);"; $funcscript[] = "\t".$function."_show(".qa_js($target).", (".$sourcesobj."), first);";
} }
$funcscript[] = "}"; $funcscript[] = "}";
$loadscript[] = "{$function}(true);";
// set default state of options
$loadscript = array(
"for (var i = 0; i < qa_checkboxids.length; i++) {",
"\tjQuery('#'+qa_checkboxids[i]).click(function() { ".$function."(false); });",
"}",
"{$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