-
-
Notifications
You must be signed in to change notification settings - Fork 233
Added expand/collapse functionalities to relationship formwidget #1374
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
Open
damsfx
wants to merge
9
commits into
wintercms:develop
Choose a base branch
from
damsfx:collapsing-relation-formwidget
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+478
−38
Open
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cf7caa3
Add collapsing to relation form widget
damsfx ab6e3df
Update indentation
damsfx c711f06
Improve code quality
damsfx f7b8e12
Merge branch 'develop' into collapsing-relation-formwidget
damsfx c3fc78b
Global optimizations
damsfx 02603e6
Remove `transitionend` event when fired
damsfx 81df72c
Remove empty css selector
damsfx 90fbd40
Check that the controls exist before attaching events
damsfx 4088bb4
Remove unneeded submodules
damsfx 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
Large diffs are not rendered by default.
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
1 change: 1 addition & 0 deletions
1
modules/backend/formwidgets/relation/assets/js/dist/relation.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
248 changes: 248 additions & 0 deletions
248
modules/backend/formwidgets/relation/assets/js/src/Relation.js
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,248 @@ | ||
| import '../../less/relation.less'; | ||
|
|
||
| ((Snowboard) => { | ||
| /** | ||
| * Relation form widget. | ||
| * | ||
| * Renders a checkbox list field to select model related relations | ||
| * | ||
| * @author Damien MATHIEU <[email protected]> | ||
| * @copyright 2025 Winter CMS | ||
| */ | ||
| class Relation extends Snowboard.PluginBase { | ||
| /** | ||
| * Constructor. | ||
| * | ||
| * @param {HTMLElement} element | ||
| */ | ||
| construct(element) { | ||
| this.element = element; | ||
| this.config = this.snowboard.dataConfig(this, element); | ||
|
|
||
| // Control elements | ||
| this.expandAllControl = element.querySelector('[data-field-checkboxlist-expand-all]'); | ||
| this.collapseAllControl = element.querySelector('[data-field-checkboxlist-collapse-all]'); | ||
| this.expandCheckedControl = element.querySelector('[data-field-checkboxlist-expand-checked]'); | ||
|
|
||
| // Child elements | ||
| this.items = element.querySelectorAll('.checkboxlist-item'); | ||
| this.toggles = element.querySelectorAll('.checkboxlist-item-toggle'); | ||
|
|
||
| // Events | ||
| this.events = { | ||
| expandAll: () => this.onExpandAll(), | ||
| collapseAll: () => this.onCollapseAll(), | ||
| expandChecked: () => this.onExpandChecked(), | ||
| toggle: (el) => this.onToggle(el), | ||
| }; | ||
|
|
||
| this.attachEvents(); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the default options for this widget. | ||
| * | ||
| * @returns {Object} | ||
| */ | ||
| defaults() { | ||
| return {}; | ||
| } | ||
|
|
||
| /** | ||
| * Attaches event listeners for several interactions. | ||
| */ | ||
| attachEvents() { | ||
| this.expandAllControl.addEventListener('click', this.events.expandAll); | ||
| this.collapseAllControl.addEventListener('click', this.events.collapseAll); | ||
| this.expandCheckedControl.addEventListener('click', this.events.expandChecked); | ||
|
|
||
| this.toggles.forEach((toggle) => { | ||
| toggle.addEventListener('click', this.events.toggle) | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Destructor. | ||
| */ | ||
| destruct() { | ||
| this.expandAllControl.removeEventListener('click', this.events.expandAll); | ||
| this.collapseAllControl.removeEventListener('click', this.events.collapseAll); | ||
| this.expandCheckedControl.removeEventListener('click', this.events.expandChecked); | ||
|
|
||
| this.toggles.forEach((toggle) => { | ||
| toggle.removeEventListener('click', this.events.toggle) | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Open a single level of the tree | ||
| * | ||
| * @param {HTMLElement} el | ||
| */ | ||
| openLevel(el) { | ||
| el.classList.add('open'); | ||
|
|
||
| let child = el.querySelectorAll('.checkboxlist-children')[0]; | ||
| if (child) { | ||
| child.classList.add('open'); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Close an signle level of the tree | ||
| * | ||
| * @param {HTMLElement} el | ||
| */ | ||
| closeLevel(el) { | ||
| el.classList.remove('open'); | ||
|
|
||
| let child = el.querySelectorAll('.checkboxlist-children')[0]; | ||
| if (child) { | ||
| child.classList.remove('open'); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Expand all handler. | ||
| * | ||
| * Makes all nodes of the tree expanded. | ||
| */ | ||
| onExpandAll() { | ||
| const openPromise = new Promise((resolve, reject) => { | ||
| let animatedNodes = this.getExpandableNodes(); | ||
|
|
||
| animatedNodes.forEach((item) => { | ||
| this.openLevel(item); | ||
| }); | ||
|
|
||
| resolve([].slice.call(animatedNodes).pop()); | ||
| }); | ||
|
|
||
| openPromise.then((el) => { | ||
| this.updateScollBar(el); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Collapse all handler. | ||
| * | ||
| * Makes all nodes of the tree collapsed. | ||
| */ | ||
| onCollapseAll() { | ||
| const closePromise = new Promise((resolve, reject) => { | ||
| let animatedNodes = this.getOpenedNodes(); | ||
|
|
||
| animatedNodes.forEach((item) => { | ||
| this.closeLevel(item); | ||
| }); | ||
|
|
||
| resolve([].slice.call(animatedNodes).pop()); | ||
| }); | ||
|
|
||
| closePromise.then((el) => { | ||
| this.updateScollBar(el); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Expand checked handler. | ||
| * | ||
| * Makes all checked nodes of the tree expanded. | ||
| */ | ||
| onExpandChecked() { | ||
| this.onCollapseAll(); | ||
|
|
||
| const selectedPromise = new Promise((resolve, reject) => { | ||
| let animatedNodes = this.getCheckedNodes(); | ||
|
|
||
| animatedNodes.forEach((item) => { | ||
| this.openLevel(item); | ||
| }); | ||
|
|
||
| resolve([].slice.call(animatedNodes).pop()); | ||
| }); | ||
|
|
||
| selectedPromise.then((el) => { | ||
| this.updateScollBar(el); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Toggle handler. | ||
| * | ||
| * Toggles a tree level expanded/collapsed. | ||
| * | ||
| * @param {HTMLElement} el | ||
| */ | ||
| onToggle(el) { | ||
| const tooglePromise = new Promise((resolve, reject) => { | ||
| let parent = el.target.parentElement; | ||
|
|
||
| if (parent.classList.contains('open')) { | ||
| this.closeLevel(parent); | ||
| } else { | ||
| this.openLevel(parent); | ||
| } | ||
|
|
||
| resolve(parent); | ||
| }); | ||
|
|
||
| tooglePromise.then((parent) => { | ||
| this.updateScollBar(parent); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Update the sidebar height | ||
| * | ||
| * @param {HTMLElement} el The last animated node of the tree | ||
| */ | ||
| updateScollBar(el) { | ||
| if (el === undefined) { | ||
| return; | ||
| } | ||
|
|
||
| let openedLevel = el.classList.contains("checkboxlist-children") ? el : el.querySelector('.checkboxlist-children'); | ||
|
|
||
| openedLevel.addEventListener("transitionend", () => { | ||
| $('[data-control=scrollbar]').data('oc.scrollbar').update(); | ||
| }, {once: true}); | ||
| } | ||
|
|
||
| /** | ||
| * Filter treeview nodes to get only those who have childs | ||
| * | ||
| * @returns {Array} | ||
| */ | ||
| getExpandableNodes() { | ||
| return Array.prototype.filter.call(this.items, function (level) { | ||
| return level.matches(':has(.checkboxlist-children)'); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Filter treeview nodes to get only opened ones | ||
| * | ||
| * @returns {Array} | ||
| */ | ||
| getOpenedNodes() { | ||
| return Array.prototype.filter.call(this.items, function (level) { | ||
| return level.classList.contains("open") | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Filter treeview nodes to get only those containing checked checkboxes | ||
| * | ||
| * @returns {Array} | ||
| */ | ||
| getCheckedNodes() { | ||
| return Array.prototype.filter.call(this.getExpandableNodes(), function (level) { | ||
| return level.matches(':has(input:checked)'); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| Snowboard.addPlugin('backend.formwidget.relation', Relation); | ||
| Snowboard['backend.ui.widgethandler']().register('relation', 'backend.formwidget.relation'); | ||
| })(window.Snowboard); |
84 changes: 84 additions & 0 deletions
84
modules/backend/formwidgets/relation/assets/less/relation.less
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,84 @@ | ||
| @import "../../../../assets/less/core/boot.less"; | ||
|
|
||
|
|
||
| .checkboxlist-item { | ||
|
|
||
|
|
||
| } | ||
|
|
||
| div[data-control="relation"] { | ||
|
|
||
| // Top widget controls | ||
| .field-checkboxlist .checkboxlist-controls > div:nth-child(even) { | ||
| margin-left: auto; | ||
| margin-right: 0; | ||
| } | ||
|
|
||
| .checkboxlist { | ||
|
|
||
| &-item { | ||
| --background-padding : 10px; | ||
|
|
||
| display: flex; | ||
| flex-direction: row; | ||
| align-items: center; | ||
| justify-content: start; | ||
| flex-wrap: wrap; | ||
| margin-bottom: 10px; | ||
| margin-top: 15px; | ||
|
|
||
| .checkboxlist-item-toggle .icon-chevron-right { | ||
| display: block; | ||
| transition: transform 0.15s ease-in-out; | ||
| } | ||
|
|
||
| &:has(.checkboxlist-item).open { | ||
| background: linear-gradient(90deg, rgba(0, 0, 0, 0) calc(var(--background-padding) - 1px), rgba(0, 0, 0, 0.25) var(--background-padding), rgba(0, 0, 0, 0) calc(var(--background-padding) + 1px)); | ||
|
|
||
| > .checkboxlist-item-toggle .icon-chevron-right { | ||
| transform: rotate(90deg); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| & .custom-checkbox { | ||
| flex-grow: 1; | ||
| margin-top: 0; | ||
| margin-bottom: 0; | ||
| } | ||
|
|
||
| &-toggle { | ||
| margin: 0 calc(1rem + 15px) 0 1rem; | ||
|
|
||
| .icon-chevron-right { | ||
| pointer-events: none; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| &-children { | ||
| width: 100%; | ||
| display: grid; | ||
| grid-template-rows: 0fr; | ||
| transition: grid-template-rows 0.2s ease-in-out; | ||
|
|
||
| &.open { | ||
| grid-template-rows: 1fr; | ||
| } | ||
| & > div { | ||
| overflow: hidden; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| .checkboxlist-item .checkboxlist-item { | ||
| --background-padding: 20px; | ||
|
|
||
| margin-left: 10px; | ||
| padding-left: 10px; | ||
| } | ||
|
|
||
| .checkboxlist-item ~ .checkboxlist-item { | ||
| margin-top: 0; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.