Skip to content

Commit d643914

Browse files
committed
TS: detachNode -> removeDOM rename
and use typescript default value
1 parent 1532d01 commit d643914

File tree

4 files changed

+33
-34
lines changed

4 files changed

+33
-34
lines changed

doc/CHANGES.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ thanks [@ermcgrat](https://github.com/ermcgrat) and others for pointing out code
195195

196196
- update requirements to the latest versions of jQuery (v3.1.0+) and jquery-ui (v1.12.0+).
197197
- fix jQuery `size()` ([#486](https://github.com/gridstack/gridstack.js/issues/486)).
198-
- update `destroy([detachGrid])` call ([#422](https://github.com/gridstack/gridstack.js/issues/422)).
198+
- update `destroy([removeDOM])` call ([#422](https://github.com/gridstack/gridstack.js/issues/422)).
199199
- don't mutate options when calling `draggable` and `resizable`. ([#505](https://github.com/gridstack/gridstack.js/issues/505)).
200200
- update _notify to allow detach ([#411](https://github.com/gridstack/gridstack.js/issues/411)).
201201
- fix code that checks for jquery-ui ([#481](https://github.com/gridstack/gridstack.js/issues/481)).
@@ -217,7 +217,7 @@ thanks [@ermcgrat](https://github.com/ermcgrat) and others for pointing out code
217217
- add `setAnimation` method to API
218218
- add `column` method ([#227](https://github.com/gridstack/gridstack.js/issues/227))
219219
- add `removable`/`removeTimeout` *(experimental)*
220-
- add `detachGrid` parameter to `destroy` method ([#216](https://github.com/gridstack/gridstack.js/issues/216)) (thanks @jhpedemonte)
220+
- add `removeDOM` parameter to `destroy` method ([#216](https://github.com/gridstack/gridstack.js/issues/216)) (thanks @jhpedemonte)
221221
- add `useOffset` parameter to `getCellFromPixel` method ([#237](https://github.com/gridstack/gridstack.js/issues/237))
222222
- add `minWidth`, `maxWidth`, `minHeight`, `maxHeight`, `id` parameters to `addWidget` ([#188](https://github.com/gridstack/gridstack.js/issues/188))
223223
- add `added` and `removed` events for when a widget is added or removed, respectively. ([#54](https://github.com/gridstack/gridstack.js/issues/54))

doc/README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ gridstack.js API
2828
- [cellWidth()](#cellwidth)
2929
- [commit()](#commit)
3030
- [column(column, doNotPropagate)](#columncolumn-donotpropagate)
31-
- [destroy([detachGrid])](#destroydetachgrid)
31+
- [destroy([removeDOM])](#destroyremovedom)
3232
- [disable()](#disable)
3333
- [enable()](#enable)
3434
- [enableMove(doEnable, includeNewWidgets)](#enablemovedoenable-includenewwidgets)
@@ -45,8 +45,8 @@ gridstack.js API
4545
- [minWidth(el, val)](#minwidthel-val)
4646
- [movable(el, val)](#movableel-val)
4747
- [move(el, x, y)](#moveel-x-y)
48-
- [removeWidget(el[, detachNode])](#removewidgetel-detachnode)
49-
- [removeAll([detachNode])](#removealldetachnode)
48+
- [removeWidget(el[, removeDOM])](#removewidgetel-removedom)
49+
- [removeAll([removeDOM])](#removeallremovedom)
5050
- [resize(el, width, height)](#resizeel-width-height)
5151
- [resizable(el, val)](#resizableel-val)
5252
- [setAnimation(doAnimate)](#setanimationdoanimate)
@@ -295,13 +295,13 @@ else you will need to generate correct CSS (see https://github.com/gridstack/gri
295295
- `column` - Integer > 0 (default 12), if missing it will return the current count instead.
296296
- `doNotPropagate` - if true existing widgets will not be updated during a set.
297297

298-
### destroy([detachGrid])
298+
### destroy([removeDOM])
299299

300300
Destroys a grid instance.
301301

302302
Parameters:
303303

304-
- `detachGrid` - if `false` nodes and grid will not be removed from the DOM (Optional. Default `true`).
304+
- `removeDOM` - if `false` nodes and grid will not be removed from the DOM (Optional. Default `true`).
305305

306306
### disable()
307307

@@ -431,22 +431,22 @@ Parameters:
431431
- `el` - widget to move
432432
- `x`, `y` - new position. If value is `null` or `undefined` it will be ignored.
433433

434-
### removeWidget(el[, detachNode])
434+
### removeWidget(el[, removeDOM])
435435

436436
Removes widget from the grid.
437437

438438
Parameters:
439439

440440
- `el` - widget to remove.
441-
- `detachNode` - if `false` node won't be removed from the DOM (Optional. Default `true`).
441+
- `removeDOM` - if `false` node won't be removed from the DOM (Optional. Default `true`).
442442

443-
### removeAll([detachNode])
443+
### removeAll([removeDOM])
444444

445445
Removes all widgets from the grid.
446446

447447
Parameters:
448448

449-
- `detachNode` - if `false` nodes won't be removed from the DOM (Optional. Default `true`).
449+
- `removeDOM` - if `false` nodes won't be removed from the DOM (Optional. Default `true`).
450450

451451
### resize(el, width, height)
452452

src/gridstack-engine.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import { Utils, obsolete } from './utils';
1010
import { GridStackNode } from './types';
1111

12-
export type onChangeCB = (nodes: GridStackNode[], detachNode?: boolean) => void;
12+
export type onChangeCB = (nodes: GridStackNode[], removeDOM?: boolean) => void;
1313

1414
/**
1515
* Defines the GridStack engine that does most no DOM grid manipulation.
@@ -268,13 +268,12 @@ export class GridStackEngine {
268268
}
269269

270270
/** @internal */
271-
private _notify(nodes?: GridStackNode | GridStackNode[], detachNode?: boolean): GridStackEngine {
271+
private _notify(nodes?: GridStackNode | GridStackNode[], removeDOM = true): GridStackEngine {
272272
if (this.batchMode) { return this }
273-
detachNode = (detachNode === undefined ? true : detachNode);
274273
nodes = (nodes === undefined ? [] : (Array.isArray(nodes) ? nodes : [nodes]) );
275274
let dirtyNodes = nodes.concat(this.getDirtyNodes());
276275
if (this.onchange) {
277-
this.onchange(dirtyNodes, detachNode);
276+
this.onchange(dirtyNodes, removeDOM);
278277
}
279278
return this;
280279
}
@@ -320,25 +319,26 @@ export class GridStackEngine {
320319
return node;
321320
}
322321

323-
public removeNode(node: GridStackNode, detachNode = true, triggerRemoveEvent = false): GridStackEngine {
322+
public removeNode(node: GridStackNode, removeDOM = true, triggerRemoveEvent = false): GridStackEngine {
324323
if (triggerRemoveEvent) {
325324
this.removedNodes.push(node);
326325
}
327326
node._id = null; // hint that node is being removed
328327
this.nodes = this.nodes.filter(n => n !== node);
329328
this._packNodes();
330-
this._notify(node, detachNode);
329+
this._notify(node, removeDOM);
331330
return this;
332331
}
333332

334-
public removeAll(detachNode?: boolean): GridStackEngine {
333+
public removeAll(removeDOM = true): GridStackEngine {
335334
delete this._layouts;
336335
if (this.nodes.length === 0) { return this }
337-
detachNode = (detachNode === undefined ? true : detachNode);
338-
this.nodes.forEach(n => { n._id = null; }); // hint that node is being removed
336+
if (removeDOM) {
337+
this.nodes.forEach(n => { n._id = null; }); // hint that node is being removed
338+
}
339339
this.removedNodes = this.nodes;
340340
this.nodes = [];
341-
this._notify(this.removedNodes, detachNode);
341+
this._notify(this.removedNodes, removeDOM);
342342
return this;
343343
}
344344

src/gridstack.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,12 @@ export class GridStack {
242242

243243
this._initStyles();
244244

245-
this.engine = new GridStackEngine(this.opts.column, (cbNodes, detachNode) => {
246-
detachNode = (detachNode === undefined ? true : detachNode);
245+
this.engine = new GridStackEngine(this.opts.column, (cbNodes, removeDOM = true) => {
247246
let maxHeight = 0;
248247
this.engine.nodes.forEach(n => { maxHeight = Math.max(maxHeight, n.y + n.height) });
249248
cbNodes.forEach(n => {
250249
let el = n.el;
251-
if (detachNode && n._id === null) {
250+
if (removeDOM && n._id === null) {
252251
if (el && el.parentNode) { el.parentNode.removeChild(el) }
253252
} else {
254253
this._writeAttrs(el, n.x, n.y, n.width, n.height);
@@ -466,13 +465,13 @@ export class GridStack {
466465

467466
/**
468467
* Destroys a grid instance.
469-
* @param detachGrid if false nodes and grid will not be removed from the DOM (Optional. Default true).
468+
* @param removeDOM if `false` grid and items elements will not be removed from the DOM (Optional. Default `true`).
470469
*/
471-
public destroy(detachGrid = true): GridStack {
470+
public destroy(removeDOM = true): GridStack {
472471
window.removeEventListener('resize', this._onResizeHandler);
473472
this.disable();
474-
if (!detachGrid) {
475-
this.removeAll(false);
473+
if (!removeDOM) {
474+
this.removeAll(removeDOM);
476475
this.el.classList.remove(this.opts._class);
477476
delete this.el.gridstack;
478477
} else {
@@ -825,9 +824,9 @@ export class GridStack {
825824
/**
826825
* Removes widget from the grid.
827826
* @param el widget or selector to modify
828-
* @param detachNode if false DOM node won't be removed from the tree (Default? true).
827+
* @param removeDOM if `false` DOM element won't be removed from the tree (Default? true).
829828
*/
830-
public removeWidget(els: GridStackElement, detachNode = true): GridStack {
829+
public removeWidget(els: GridStackElement, removeDOM = true): GridStack {
831830
this.getElements(els).forEach(el => {
832831
if (el.parentElement !== this.el) return; // not our child!
833832
let node = el.gridstackNode;
@@ -841,7 +840,7 @@ export class GridStack {
841840
delete el.gridstackNode;
842841
this.dd.draggable(el, 'destroy').resizable(el, 'destroy');
843842

844-
this.engine.removeNode(node, detachNode, true); // true for trigger event
843+
this.engine.removeNode(node, removeDOM, true); // true for trigger event
845844
});
846845
this._triggerRemoveEvent();
847846
this._triggerChangeEvent();
@@ -850,15 +849,15 @@ export class GridStack {
850849

851850
/**
852851
* Removes all widgets from the grid.
853-
* @param detachNode if false DOM nodes won't be removed from the tree (Default? true).
852+
* @param removeDOM if `false` DOM elements won't be removed from the tree (Default? `true`).
854853
*/
855-
public removeAll(detachNode?: boolean): GridStack {
854+
public removeAll(removeDOM = true): GridStack {
856855
// always remove our DOM data (circular link) before list gets emptied and drag&drop permanently
857856
this.engine.nodes.forEach(n => {
858857
delete n.el.gridstackNode;
859858
this.dd.draggable(n.el, 'destroy').resizable(n.el, 'destroy');
860859
});
861-
this.engine.removeAll(detachNode);
860+
this.engine.removeAll(removeDOM);
862861
this._triggerRemoveEvent();
863862
return this;
864863
}

0 commit comments

Comments
 (0)