Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Q
question2answer
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
outils
question2answer
Commits
3011b268
Commit
3011b268
authored
Oct 28, 2015
by
Scott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reformat code in qa_set_display_rules
parent
abd09370
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
33 deletions
+39
-33
format.php
qa-include/app/format.php
+39
-33
No files found.
qa-include/app/format.php
View file @
3011b268
...
...
@@ -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
)
/*
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
();
$keysourceobjs
=
array
();
$jsVar
=
'/[A-Za-z_][A-Za-z0-9_]*/'
;
$jsVarRegex
=
'/[A-Za-z_][A-Za-z0-9_]*/'
;
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
)
{
if
(
!
in_array
(
$element
,
$keysourceids
))
$keysourceids
[]
=
$element
;
...
...
@@ -1492,39 +1494,43 @@
}
}
$funcscript
=
array
();
$loadscript
=
array
();
$funcscript
[]
=
"var qa_checkboxids = "
.
json_encode
(
$keysourceids
)
.
";"
;
$funcscript
[]
=
"var qa_options = {};"
;
// set default state of options
$loadscript
[]
=
"for (var i = 0; i < qa_checkboxids.length; i++) {"
;
$loadscript
[]
=
"
\t
jQuery('#'+qa_checkboxids[i]).click(function() { "
.
$function
.
"(false); });"
;
$loadscript
[]
=
"}"
;
$funcscript
[]
=
"function
{
$function
}
_show(target, show, first) {"
;
$funcscript
[]
=
"
\t
var e = document.getElementById(target);"
;
$funcscript
[]
=
"
\t
if (e) {"
;
$funcscript
[]
=
"
\t\t
if (first || e.nodeName == 'SPAN') { e.style.display = (show ? '' : 'none'); }"
;
$funcscript
[]
=
"
\t\t
else if (show) { $(e).fadeIn(); }"
;
$funcscript
[]
=
"
\t\t
else { $(e).fadeOut(); }"
;
$funcscript
[]
=
"
\t
}"
;
$funcscript
[]
=
"}"
;
$function
=
'qa_display_rule_'
.
count
(
@
$qa_content
[
'script_lines'
]);
$funcscript
=
array
(
// set up variables
"var qa_checkboxids = "
.
json_encode
(
$keysourceids
)
.
";"
,
// show/hide an element
"function
{
$function
}
_show(target, show, first) {"
,
"
\t
var e = document.getElementById(target);"
,
"
\t
if (e) {"
,
"
\t\t
if (first || e.nodeName == 'SPAN') { e.style.display = (show ? '' : 'none'); }"
,
"
\t\t
else if (show) { $(e).fadeIn(); }"
,
"
\t\t
else { $(e).fadeOut(); }"
,
"
\t
}"
,
"}"
,
);
// check and set all display rules
$funcscript
[]
=
"function
{
$function
}
(first) {"
;
$funcscript
[]
=
"
\t
var qa_options = {};"
;
$funcscript
[]
=
"
\t
for (var i = 0; i < qa_checkboxids.length; i++) {"
;
$funcscript
[]
=
"
\t\t
var e = document.getElementById(qa_checkboxids[i]);"
;
$funcscript
[]
=
"
\t\t
qa_options[qa_checkboxids[i]] = e && (e.checked || (e.options && e.options[e.selectedIndex].value));"
;
$funcscript
[]
=
"
\t
}"
;
foreach
(
$effects
as
$target
=>
$sources
)
{
$sourcesobj
=
preg_replace
(
$jsVar
,
'qa_options.$0'
,
$sources
);
$sourcesobj
=
preg_replace
(
$jsVar
Regex
,
'qa_options.$0'
,
$sources
);
$funcscript
[]
=
"
\t
"
.
$function
.
"_show("
.
qa_js
(
$target
)
.
", ("
.
$sourcesobj
.
"), first);"
;
}
$funcscript
[]
=
"}"
;
$loadscript
[]
=
"
{
$function
}
(true);"
;
// set default state of options
$loadscript
=
array
(
"for (var i = 0; i < qa_checkboxids.length; i++) {"
,
"
\t
jQuery('#'+qa_checkboxids[i]).click(function() { "
.
$function
.
"(false); });"
,
"}"
,
"
{
$function
}
(true);"
,
);
$qa_content
[
'script_lines'
][]
=
$funcscript
;
$qa_content
[
'script_onloads'
][]
=
$loadscript
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment