-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[5.1] Rewrite treeselect in vanilla JS #42776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
db1f404
Rewrite treeselect in vanilla JS
C-Lodder a9f0377
Indentation
C-Lodder 29ae5d3
eslint fixes
C-Lodder 4f775be
Update build/media_source/system/js/treeselectmenu.es6.js
C-Lodder 1514eb3
Merge branch '5.1-dev' into treeselectmenu-js
Razzo1987 5dde95f
Merge branch '5.1-dev' into treeselectmenu-js
LadySolveig ae42617
Fix checkbox state
C-Lodder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| /** | ||
| * @copyright (C) 2012 Open Source Matters, Inc. <https://www.joomla.org> | ||
| * @license GNU General Public License version 2 or later; see LICENSE.txt | ||
| */ | ||
|
|
||
| const treeselectmenu = document.getElementById('treeselectmenu').innerHTML; | ||
| const direction = (document.dir !== undefined) ? document.dir : document.getElementsByTagName('html')[0].getAttribute('dir'); | ||
|
|
||
| document.querySelectorAll('.treeselect li').forEach((li) => { | ||
| // Add icons | ||
| const icon = document.createElement('span'); | ||
| icon.classList.add('icon-'); | ||
| li.prepend(icon); | ||
|
|
||
| if (li.querySelector('ul.treeselect-sub')) { | ||
| // Add classes to Expand/Collapse icons | ||
| li.querySelector('span.icon-').classList.add('treeselect-toggle', 'icon-chevron-down'); | ||
|
|
||
| // Append drop down menu in nodes | ||
| li.querySelector('div.treeselect-item label').insertAdjacentHTML('afterend', treeselectmenu); | ||
|
|
||
| const sub = li.querySelector('ul.treeselect-sub'); | ||
| if (!sub.querySelector('ul.treeselect-sub')) { | ||
| li.querySelector('div.treeselect-menu-expand').remove(); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| // Takes care of the Expand/Collapse of a node | ||
| document.querySelectorAll('span.treeselect-toggle').forEach((toggle) => { | ||
| toggle.addEventListener('click', ({ target }) => { | ||
| const chevron = direction === 'rtl' ? 'icon-chevron-left' : 'icon-chevron-right'; | ||
|
|
||
| // Take care of parent UL | ||
| const { parentNode } = target; | ||
| if (parentNode.querySelector('ul.treeselect-sub').classList.contains('hidden')) { | ||
| target.classList.remove(chevron); | ||
| target.classList.add('icon-chevron-down'); | ||
| parentNode.querySelectorAll('ul.treeselect-sub').forEach((item) => item.classList.remove('hidden')); | ||
| parentNode.querySelectorAll('ul.treeselect-sub i.treeselect-toggle').forEach((item) => { | ||
| item.classList.add('icon-chevron-down'); | ||
| item.classList.remove(chevron); | ||
| }); | ||
| } else { | ||
| target.classList.add(chevron); | ||
| target.classList.remove('icon-chevron-down'); | ||
|
|
||
| parentNode.querySelectorAll('ul.treeselect-sub').forEach((item) => item.classList.add('hidden')); | ||
| parentNode.querySelectorAll('ul.treeselect-sub i.treeselect-toggle').forEach((item) => { | ||
| item.classList.remove('icon-chevron-down'); | ||
| item.classList.add(chevron); | ||
| }); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| // Takes care of the filtering | ||
| document.getElementById('treeselectfilter').addEventListener('keyup', ({ target }) => { | ||
| const noResults = document.getElementById('noresultsfound'); | ||
| const text = target.value.toLowerCase(); | ||
| let hidden = 0; | ||
|
|
||
| noResults.classList.add('hidden'); | ||
|
|
||
| const listItems = document.querySelectorAll('.treeselect li'); | ||
| listItems.forEach((item) => { | ||
| if (item.innerText.toLowerCase().includes(text)) { | ||
| item.classList.remove('d-none'); | ||
| } else { | ||
| item.classList.add('d-none'); | ||
| hidden += 1; | ||
| } | ||
| }); | ||
|
|
||
| if (hidden === listItems.length) { | ||
| noResults.classList.remove('hidden'); | ||
| } | ||
| }); | ||
|
|
||
| // Checks all checkboxes the tree | ||
| document.getElementById('treeCheckAll').addEventListener('click', () => { | ||
| document.querySelectorAll('.treeselect input').forEach((input) => input.setAttribute('checked', 'checked')); | ||
| }); | ||
|
|
||
| // Unchecks all checkboxes the tree | ||
| document.getElementById('treeUncheckAll').addEventListener('click', () => { | ||
| document.querySelectorAll('.treeselect input').forEach((input) => input.removeAttribute('checked')); | ||
| }); | ||
|
|
||
| // Expands all subtrees | ||
| document.getElementById('treeExpandAll').addEventListener('click', () => { | ||
| document.querySelectorAll('ul.treeselect ul.treeselect-sub').forEach((input) => input.classList.remove('hidden')); | ||
| document.querySelectorAll('ul.treeselect span.treeselect-toggle').forEach((item) => { | ||
| item.classList.remove('icon-chevron-right'); | ||
| item.classList.add('icon-chevron-down'); | ||
| }); | ||
| }); | ||
|
|
||
| // Collapses all subtrees | ||
| document.getElementById('treeCollapseAll').addEventListener('click', () => { | ||
| document.querySelectorAll('ul.treeselect ul.treeselect-sub').forEach((input) => input.classList.add('hidden')); | ||
| document.querySelectorAll('ul.treeselect span.treeselect-toggle').forEach((item) => { | ||
| item.classList.remove('icon-chevron-down'); | ||
| item.classList.add('icon-chevron-right'); | ||
| }); | ||
| }); | ||
|
|
||
| // Take care of children check/uncheck all | ||
| document.querySelectorAll('a.checkall').forEach((item) => { | ||
| item.addEventListener('click', ({ target }) => { | ||
| target.closest('li').querySelectorAll('ul.treeselect-sub input').forEach((input) => input.setAttribute('checked', 'checked')); | ||
| }); | ||
| }); | ||
| document.querySelectorAll('a.uncheckall').forEach((item) => { | ||
| item.addEventListener('click', ({ target }) => { | ||
| target.closest('li').querySelectorAll('ul.treeselect-sub input').forEach((input) => input.removeAttribute('checked')); | ||
| }); | ||
| }); | ||
|
|
||
| // Take care of children toggle all | ||
| document.querySelectorAll('a.expandall').forEach((item) => { | ||
| item.addEventListener('click', ({ target }) => { | ||
| const parent = target.closest('ul'); | ||
| parent.querySelectorAll('ul.treeselect-sub').forEach((input) => input.classList.remove('hidden')); | ||
| parent.querySelectorAll('ul.treeselect-sub .treeselect-toggle').forEach((toggle) => { | ||
| toggle.classList.remove('icon-chevron-right'); | ||
| toggle.classList.add('icon-chevron-down'); | ||
| }); | ||
| }); | ||
| }); | ||
| document.querySelectorAll('a.collapseall').forEach((item) => { | ||
| item.addEventListener('click', ({ target }) => { | ||
| const parent = target.closest('ul'); | ||
| parent.querySelectorAll('ul.treeselect-sub').forEach((input) => input.classList.add('hidden')); | ||
| parent.querySelectorAll('ul.treeselect-sub .treeselect-toggle').forEach((toggle) => { | ||
| toggle.classList.remove('icon-chevron-down'); | ||
| toggle.classList.add('icon-chevron-right'); | ||
| }); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.