Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gruberroland committed Jan 8, 2024
1 parent a2dc642 commit 074a663
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
40 changes: 20 additions & 20 deletions lam/lib/html.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1489,31 +1489,31 @@ class htmlSelect extends htmlElement {
// build Java script to show/hide depending fields
foreach ($values as $val) {
// build onChange listener
$onChange .= 'if (jQuery(\'#' . $this->name . '\').val() == \'' . $val . '\') {';
$onChange .= 'if (document.getElementById(\'' . $this->name . '\').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 .= '};';
}
// 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->selectedElements[0]])) {
for ($i = 0; $i < sizeof($this->tableRowsToShow[$this->selectedElements[0]]); $i++) {
$classType = 'removeClass';
$script .= 'jQuery(\'#' . $this->tableRowsToShow[$this->selectedElements[0]][$i] . '\').closest(\'' . $selector . '\').' . $classType . '(\'hidden\');' . "\n";
$classType = 'remove';
$script .= 'document.getElementById(\'' . $this->tableRowsToShow[$this->selectedElements[0]][$i] . '\').closest(\'' . $selector . '\').classList.' . $classType . '(\'hidden\');' . "\n";
}
}
if (isset($this->tableRowsToHide[$this->selectedElements[0]])) {
for ($i = 0; $i < sizeof($this->tableRowsToHide[$this->selectedElements[0]]); $i++) {
$classType = 'addClass';
$script .= 'jQuery(\'#' . $this->tableRowsToHide[$this->selectedElements[0]][$i] . '\').closest(\'' . $selector . '\').' . $classType . '(\'hidden\');' . "\n";
$classType = 'add';
$script .= 'document.getElementById(\'' . $this->tableRowsToHide[$this->selectedElements[0]][$i] . '\').closest(\'' . $selector . '\').classList.' . $classType . '(\'hidden\');' . "\n";
}
}
$script .= '});</script>';
Expand Down Expand Up @@ -1997,37 +1997,37 @@ class htmlInputCheckbox extends htmlElement {
$selector = $this->getShowHideSelector();
if ((sizeof($this->tableRowsToShow) > 0) || (sizeof($this->tableRowsToHide) > 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->tableRowsToShow); $i++) {
$onChange .= 'jQuery(\'#' . $this->tableRowsToShow[$i] . '\').closest(\'' . $selector . '\').removeClass(\'hidden\');';
$onChange .= 'document.getElementById(\'' . $this->tableRowsToShow[$i] . '\').closest(\'' . $selector . '\').classList.remove(\'hidden\');';
}
for ($i = 0; $i < sizeof($this->tableRowsToHide); $i++) {
$onChange .= 'jQuery(\'#' . $this->tableRowsToHide[$i] . '\').closest(\'' . $selector . '\').addClass(\'hidden\');';
$onChange .= 'document.getElementById(\'' . $this->tableRowsToHide[$i] . '\').closest(\'' . $selector . '\').classList.add(\'hidden\');';
}
$onChange .= '}';
$onChange .= 'else {';
for ($i = 0; $i < sizeof($this->tableRowsToShow); $i++) {
$onChange .= 'jQuery(\'#' . $this->tableRowsToShow[$i] . '\').closest(\'' . $selector . '\').addClass(\'hidden\');';
$onChange .= 'document.getElementById(\'' . $this->tableRowsToShow[$i] . '\').closest(\'' . $selector . '\').classList.add(\'hidden\');';
}
for ($i = 0; $i < sizeof($this->tableRowsToHide); $i++) {
$onChange .= 'jQuery(\'#' . $this->tableRowsToHide[$i] . '\').closest(\'' . $selector . '\').removeClass(\'hidden\');';
$onChange .= 'document.getElementById(\'' . $this->tableRowsToHide[$i] . '\').closest(\'' . $selector . '\').classList.remove(\'hidden\');';
}
$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->tableRowsToShow); $i++) {
$classType = 'addClass';
$classType = 'add';
if ($this->checked) {
$classType = 'removeClass';
$classType = 'remove';
}
$script .= 'jQuery(\'#' . $this->tableRowsToShow[$i] . '\').closest(\'' . $selector . '\').' . $classType . '(\'hidden\');';
$script .= 'document.getElementById(\'' . $this->tableRowsToShow[$i] . '\').closest(\'' . $selector . '\').classList.' . $classType . '(\'hidden\');';
}
for ($i = 0; $i < sizeof($this->tableRowsToHide); $i++) {
$classType = 'removeClass';
$classType = 'remove';
if ($this->checked) {
$classType = 'addClass';
$classType = 'add';
}
$script .= 'jQuery(\'#' . $this->tableRowsToHide[$i] . '\').closest(\'' . $selector . '\').' . $classType . '(\'hidden\');';
$script .= 'document.getElementById(\'' . $this->tableRowsToHide[$i] . '\').closest(\'' . $selector . '\').classList.' . $classType . '(\'hidden\');';
}
$script .= '});</script>';
}
Expand Down
33 changes: 32 additions & 1 deletion lam/tests/design/designExamples.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2021 - 2023 Roland Gruber
Copyright (C) 2021 - 2024 Roland Gruber
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -353,6 +353,37 @@
$textAreaWysiwyg->setIsRichEdit(true);
$row->addField($textAreaWysiwyg);

$row->add(new htmlSubTitle('Show/hide'));

$showHideOptions = [
'Input1' => 'input1',
'Input2' => 'input2',
'Both' => 'both',
];
$showHideSelect = new htmlResponsiveSelect('showHideSelect', $showHideOptions, ['input1'], 'Show/hide select');
$showHideSelect->setHasDescriptiveElements(true);
$showHideSelect->setTableRowsToShow([
'input1' => ['showHideSelect_i1'],
'input2' => ['showHideSelect_i2'],
'both' => ['showHideSelect_i1', 'showHideSelect_i2'],
]);
$showHideSelect->setTableRowsToHide([
'input1' => ['showHideSelect_i2'],
'input2' => ['showHideSelect_i1'],
'both' => [],
]);
$row->add($showHideSelect);
$row->add(new htmlResponsiveInputField('Input 1', 'showHideSelect_i1', ''));
$row->add(new htmlResponsiveInputField('Input 2', 'showHideSelect_i2', ''));

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

$showHideCheckbox = new htmlResponsiveInputCheckbox('showHideCheckbox', false, 'Show/hide checkbox');
$showHideCheckbox->setTableRowsToShow(['showHideCheckbox_i1']);
$showHideCheckbox->setTableRowsToHide(['showHideCheckbox_i2']);
$row->add($showHideCheckbox);
$row->add(new htmlResponsiveInputField('Input 1', 'showHideCheckbox_i1', ''));
$row->add(new htmlResponsiveInputField('Input 2', 'showHideCheckbox_i2', ''));

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

Expand Down

0 comments on commit 074a663

Please sign in to comment.