8000 optimize DDGridStack to not create on destroy/disable by adumesny · Pull Request #2941 · gridstack/gridstack.js · GitHub
[go: up one dir, main page]

Skip to content

optimize DDGridStack to not create on destroy/disable #2941

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 1 commit into from
Feb 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/dd-gridstack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { GridItemHTMLElement, GridStackElement, DDDragOpt } from './types';
import { Utils } from './utils';
import { DDManager } from './dd-manager';
import { DDElement, DDElementHost } from './dd-element';
import { GridHTMLElement } from './gridstack';

/** Drag&Drop drop options */
export type DDDropOpt = {
Expand All @@ -32,7 +33,7 @@ export type DDCallback = (event: Event, arg2: GridItemHTMLElement, helper?: Grid
export class DDGridStack {

public resizable(el: GridItemHTMLElement, opts: DDOpts, key?: DDKey, value?: DDValue): DDGridStack {
this._getDDElements(el).forEach(dEl => {
this._getDDElements(el, opts).forEach(dEl => {
if (opts === 'disable' || opts === 'enable') {
dEl.ddResizable && dEl.ddResizable[opts](); // can't create DD as it requires options for setupResizable()
} else if (opts === 'destroy') {
Expand Down Expand Up @@ -67,7 +68,7 @@ export class DDGridStack {
}

public draggable(el: GridItemHTMLElement, opts: DDOpts, key?: DDKey, value?: DDValue): DDGridStack {
this._getDDElements(el).forEach(dEl => {
this._getDDElements(el, opts).forEach(dEl => {
if (opts === 'disable' || opts === 'enable') {
dEl.ddDraggable && dEl.ddDraggable[opts](); // can't create DD as it requires options for setupDraggable()
} else if (opts === 'destroy') {
Expand Down Expand Up @@ -100,13 +101,11 @@ export class DDGridStack {
opts._accept = opts.accept;
opts.accept = (el) => opts._accept(el);
}
this._getDDElements(el).forEach(dEl => {
this._getDDElements(el, opts).forEach(dEl => {
if (opts === 'disable' || opts === 'enable') {
dEl.ddDroppable && dEl.ddDroppable[opts]();
} else if (opts === 'destroy') {
if (dEl.ddDroppable) { // error to call destroy if not there
dEl.cleanDroppable();
}
dEl.ddDroppable && dEl.cleanDroppable();
} else if (opts === 'option') {
dEl.setupDroppable({ [key]: value });
} else {
Expand Down Expand Up @@ -148,12 +147,13 @@ export class DDGridStack {
return this;
}

/** @internal returns a list of DD elements, creating them on the fly by default */
protected _getDDElements(els: GridStackElement, create = true): DDElement[] {
/** @internal returns a list of DD elements, creating them on the fly by default unless option is to destroy or disable */
protected _getDDElements(els: GridStackElement, opts?: DDOpts): DDElement[] {
// don't force create if we're going to destroy it, unless it's a grid which is used as drop target for it's children
const create = (els as GridHTMLElement).gridstack || opts !== 'destroy' && opts !== 'disable';
const hosts = Utils.getElements(els) as DDElementHost[];
if (!hosts.length) return [];
const list = hosts.map(e => e.ddElement || (create ? DDElement.init(e) : null));
if (!create) { list.filter(d => d); } // remove nulls
const list = hosts.map(e => e.ddElement || (create ? DDElement.init(e) : null)).filter(d => d); // remove nulls
return list;
}
}
0