Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gruberroland committed Jan 10, 2024
1 parent 074a663 commit 9b8be96
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
45 changes: 18 additions & 27 deletions lam/lib/html.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1642,20 +1642,13 @@ class htmlRadio extends htmlElement {
elseif (!empty($showHideOnchange)) {
$onchange = ' onchange="' . $showHideOnchange . '"';
}
$onClick = 'onClick="
jQuery(\'input[name=' . $this->name . ']\').prop(\'checked\', false);
jQuery(\'#' . $this->name . $counter . '\').prop(\'checked\', true);
jQuery(\'#' . $this->name . $counter . '\').trigger(\'change\');
"';
if ($this->isEnabled === false) {
$onClick = '';
}
echo '<div class="nowrap" ' . $onClick . '>';
echo '<div class="nowrap">';
$selected = '';
if ($value == $this->selectedElement) {
$selected = ' checked';
}
echo '<input type="radio" id="' . $this->name . $counter . '"' . $name . $disabled . $selected . $onchange . ' value="' . $value . '"> ' . $label;
echo '<input type="radio" id="' . $this->name . $counter . '"' . $name . $disabled . $selected . $onchange . ' value="' . $value . '"> ';
echo '<label for="' . $this->name . $counter . '">' . $label . '</label>';
echo '</div>';
$counter++;
}
Expand Down Expand Up @@ -1712,15 +1705,15 @@ class htmlRadio extends htmlElement {
// build Java script to show/hide depending fields
foreach ($values as $val) {
// build onChange listener
$onChange .= 'if (jQuery(\'#' . $this->name . $counter . '\').val() == \'' . $val . '\') {';
$onChange .= 'if (document.getElementById(\'' . $this->name . $counter . '\').value == \'' . $val . '\') {';
if (isset($this->tableRowsToShow[$val])) {
for ($i = 0; $i < sizeof($this->tableRowsToShow[$val]); $i++) {
$onChange .= 'jQuery(\'#' . $this->tableRowsToShow[$val][$i] . '\').closest(\'' . $selector . '\').removeClass(\'hidden\');';
$onChange .= 'document.getElementById(\'' . $this->tableRowsToShow[$val][$i] . '\').closest(\'' . $selector . '\').classList.remove(\'hidden\');';
}
}
if (isset($this->tableRowsToHide[$val])) {
for ($i = 0; $i < sizeof($this->tableRowsToHide[$val]); $i++) {
$onChange .= 'jQuery(\'#' . $this->tableRowsToHide[$val][$i] . '\').closest(\'' . $selector . '\').addClass(\'hidden\');';
$onChange .= 'document.getElementById(\'' . $this->tableRowsToHide[$val][$i] . '\').closest(\'' . $selector . '\').classList.add(\'hidden\');';
}
}
$onChange .= '};';
Expand All @@ -1731,17 +1724,15 @@ class htmlRadio extends htmlElement {
private function printInitialState() {
$selector = $this->getShowHideSelector();
// build script to set initial state
$script = '<script type="text/javascript">jQuery(document).ready(function() {' . "\n";
$script = '<script type="text/javascript">window.lam.utility.documentReady(function() {' . "\n";
if (isset($this->tableRowsToShow[$this->selectedElement])) {
for ($i = 0; $i < sizeof($this->tableRowsToShow[$this->selectedElement]); $i++) {
$classType = 'removeClass';
$script .= 'jQuery(\'#' . $this->tableRowsToShow[$this->selectedElement][$i] . '\').closest(\'' . $selector . '\').' . $classType . '(\'hidden\');' . "\n";
$script .= 'document.getElementById(\'' . $this->tableRowsToShow[$this->selectedElement][$i] . '\').closest(\'' . $selector . '\').classList.remove(\'hidden\');' . "\n";
}
}
if (isset($this->tableRowsToHide[$this->selectedElement])) {
for ($i = 0; $i < sizeof($this->tableRowsToHide[$this->selectedElement]); $i++) {
$classType = 'addClass';
$script .= 'jQuery(\'#' . $this->tableRowsToHide[$this->selectedElement][$i] . '\').closest(\'' . $selector . '\').' . $classType . '(\'hidden\');' . "\n";
$script .= 'document.getElementById(\'' . $this->tableRowsToHide[$this->selectedElement][$i] . '\').closest(\'' . $selector . '\').classList.add(\'hidden\');' . "\n";
}
}
$script .= '});</script>';
Expand Down Expand Up @@ -2014,7 +2005,7 @@ class htmlInputCheckbox extends htmlElement {
}
$onChange .= '};';
// build script to set initial state
$script = '<script type="text/javascript">window.lam.utility.documentReady(function() {';
$script .= '<script type="text/javascript">window.lam.utility.documentReady(function() {';
for ($i = 0; $i < sizeof($this->tableRowsToShow); $i++) {
$classType = 'add';
if ($this->checked) {
Expand All @@ -2034,37 +2025,37 @@ class htmlInputCheckbox extends htmlElement {
// build Java script to enable/disable elements
if ((sizeof($this->elementsToEnable) > 0) || (sizeof($this->elementsToDisable) > 0)) {
// build onChange listener
$onChange .= 'if (jQuery(\'#' . $this->name . ':checked\').val() !== undefined) {';
$onChange .= 'if (document.querySelector(\'#' . $this->name . ':checked\') !== null) {';
for ($i = 0; $i < sizeof($this->elementsToEnable); $i++) {
$onChange .= 'jQuery(\'#' . $this->elementsToEnable[$i] . '\').prop(\'disabled\', false);';
$onChange .= 'document.getElementById(\'' . $this->elementsToEnable[$i] . '\').disabled = false;';
}
for ($i = 0; $i < sizeof($this->elementsToDisable); $i++) {
$onChange .= 'jQuery(\'#' . $this->elementsToDisable[$i] . '\').prop(\'disabled\', true);';
$onChange .= 'document.getElementById(\'' . $this->elementsToDisable[$i] . '\').disabled = true;';
}
$onChange .= '}';
$onChange .= 'else {';
for ($i = 0; $i < sizeof($this->elementsToEnable); $i++) {
$onChange .= 'jQuery(\'#' . $this->elementsToEnable[$i] . '\').prop(\'disabled\', true);';
$onChange .= 'document.getElementById(\'' . $this->elementsToEnable[$i] . '\').disabled = true;';
}
for ($i = 0; $i < sizeof($this->elementsToDisable); $i++) {
$onChange .= 'jQuery(\'#' . $this->elementsToDisable[$i] . '\').prop(\'disabled\', false);';
$onChange .= 'document.getElementById(\'' . $this->elementsToDisable[$i] . '\').disabled = false;';
}
$onChange .= '};';
// build script to set initial state
$script = '<script type="text/javascript">jQuery(document).ready(function() {';
$script .= '<script type="text/javascript">window.lam.utility.documentReady(function() {';
for ($i = 0; $i < sizeof($this->elementsToEnable); $i++) {
$classType = 'true';
if ($this->checked) {
$classType = 'false';
}
$script .= 'jQuery(\'#' . $this->elementsToEnable[$i] . '\').prop(\'disabled\', ' . $classType . ');';
$script .= 'document.getElementById(\'' . $this->elementsToEnable[$i] . '\').disabled = ' . $classType . ';';
}
for ($i = 0; $i < sizeof($this->elementsToDisable); $i++) {
$classType = 'false';
if ($this->checked) {
$classType = 'true';
}
$script .= 'jQuery(\'#' . $this->elementsToDisable[$i] . '\').prop(\'disabled\', ' . $classType . ');';
$script .= 'document.getElementById(\'' . $this->elementsToDisable[$i] . '\').disabled = ' . $classType . ';';
}
$script .= '});</script>';
}
Expand Down
24 changes: 24 additions & 0 deletions lam/tests/design/designExamples.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,36 @@

$row->add(new htmlSpacer(null, '3rem'));

$showHideRadio = new htmlResponsiveRadio('Show/hide radio', 'showHideRadio', $showHideOptions, 'input2');
$showHideRadio->setTableRowsToShow([
'input1' => ['showHideRadio_i1'],
'input2' => ['showHideRadio_i2'],
'both' => ['showHideRadio_i1', 'showHideRadio_i2'],
]);
$showHideRadio->setTableRowsToHide([
'input1' => ['showHideRadio_i2'],
'input2' => ['showHideRadio_i1'],
'both' => [],
]);
$row->add($showHideRadio);
$row->add(new htmlResponsiveInputField('Input 1', 'showHideRadio_i1', ''));
$row->add(new htmlResponsiveInputField('Input 2', 'showHideRadio_i2', ''));

$row->add(new htmlSpacer(null, '3rem'));

$showHideCheckbox = new htmlResponsiveInputCheckbox('showHideCheckbox', false, 'Show/hide checkbox');
$showHideCheckbox->setTableRowsToShow(['showHideCheckbox_i1']);
$showHideCheckbox->setTableRowsToHide(['showHideCheckbox_i2']);
$showHideCheckbox->setElementsToEnable(['showHideCheckbox_ie']);
$showHideCheckbox->setElementsToDisable(['showHideCheckbox_id']);
$row->add($showHideCheckbox);
$row->add(new htmlResponsiveInputField('Input 1', 'showHideCheckbox_i1', ''));
$row->add(new htmlResponsiveInputField('Input 2', 'showHideCheckbox_i2', ''));
$row->addVerticalSpacer('0.5rem');
$row->add(new htmlResponsiveInputField('Enabled on checked', 'showHideCheckbox_ie', ''));
$row->add(new htmlResponsiveInputField('Disabled on checked', 'showHideCheckbox_id', ''));

$row->add(new htmlSpacer(null, '3rem'));

$row->add(new htmlSpacer(null, '20rem'));

Expand Down

0 comments on commit 9b8be96

Please sign in to comment.