Skip to content

Commit fa8634c

Browse files
authored
Merge pull request #1491 from adumesny/develop
more update(el, opts) changes
2 parents f5b4292 + ddf627f commit fa8634c

File tree

4 files changed

+26
-187
lines changed

4 files changed

+26
-187
lines changed

doc/CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Change log
4848
- fix [1484](https://github.com/gridstack/gridstack.js/issues/1484) dragging between 2 grids and back (regression in 2.0.1)
4949
- fix [1471](https://github.com/gridstack/gridstack.js/issues/1471) `load()` into 1 column mode doesn't resize back to 12 correctly
5050
- fix [1235](https://github.com/gridstack/gridstack.js/issues/1235) `update(el, opts)` re-write to take all `GridStackWidget` options (not just x,y,width,height) and do everything efficiently.
51-
Fixed `locked()`, `move()`, `resize()`, `minWidth()`,etc... to call update() instead which does all the constrain, not just update attributes!
51+
Hiding `locked()`, `move()`, `resize()`, `minWidth()`, etc... as they just simply call update() which does all the constrain now as well!
5252
- del `ddPlugin` grid option as we only have one drag&drop plugin at runtime, which is defined by the include you use (HTML5 vs jquery vs none)
5353

5454
## 2.2.0 (2020-11-7)

doc/README.md

Lines changed: 5 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,16 @@ gridstack.js API
4040
- [getMargin()](#getmargin)
4141
- [isAreaEmpty(x, y, width, height)](#isareaemptyx-y-width-height)
4242
- [load(layout: GridStackWidget[], boolean | ((w: GridStackWidget, add: boolean) => void) = true)](#loadlayout-gridstackwidget-boolean--w-gridstackwidget-add-boolean--void---true)
43-
- [locked(el, val)](#lockedel-val)
4443
- [makeWidget(el)](#makewidgetel)
4544
- [margin(value: numberOrString)](#marginvalue-numberorstring)
46-
- [maxHeight(el, val)](#maxheightel-val)
47-
- [minHeight(el, val)](#minheightel-val)
48-
- [maxWidth(el, val)](#maxwidthel-val)
49-
- [minWidth(el, val)](#minwidthel-val)
5045
- [movable(el, val)](#movableel-val)
51-
- [move(el, x, y)](#moveel-x-y)
5246
- [removeWidget(el, removeDOM = true, triggerEvent = true)](#removewidgetel-removedom--true-triggerevent--true)
5347
- [removeAll(removeDOM = true)](#removeallremovedom--true)
54-
- [resize(el, width, height)](#resizeel-width-height)
5548
- [resizable(el, val)](#resizableel-val)
5649
- [save(saveContent = true): GridStackWidget[]](#savesavecontent--true-gridstackwidget)
5750
- [setAnimation(doAnimate)](#setanimationdoanimate)
5851
- [setStatic(staticValue)](#setstaticstaticvalue)
59-
- [update(el, x, y, width, height)](#updateel-x-y-width-height)
52+
- [update(el: GridStackElement, opts: GridStackWidget)](#updateel-gridstackelement-opts-gridstackwidget)
6053
- [willItFit(x, y, width, height, autoPosition)](#willitfitx-y-width-height-autoposition)
6154
- [Utils](#utils)
6255
- [GridStack.Utils.sort(nodes[, dir[, width]])](#gridstackutilssortnodes-dir-width)
@@ -398,13 +391,6 @@ Checks if specified area is empty.
398391
- `addAndRemove` boolean (default true) or callback method can be passed to control if and how missing widgets can be added/removed, giving the user control of insertion.
399392
- see [example](http://gridstackjs.com/demo/serialization.html)
400393

401-
### locked(el, val)
402-
403-
Locks/unlocks widget.
404-
405-
- `el` - widget or selector to modify.
406-
- `val` - if `true` widget will be locked.
407-
408394
### makeWidget(el)
409395

410396
If you add elements to your gridstack container by hand, you have to tell gridstack afterwards to make them widgets. If you want gridstack to add the elements for you, use `addWidget` instead.
@@ -428,50 +414,13 @@ gap between grid item and content (default?: 10). This will set all 4 sides and
428414
- string with space separated values (ex: `'5px 10px 0 20px'` for all 4 sides, or `'5em 10em'` for top/bottom and left/right pairs like CSS).
429415
- Note: all sides must have same units (last one wins, default px)
430416

431-
### maxHeight(el, val)
432-
433-
Set the `maxHeight` for a widget.
434-
435-
- `el` - widget to modify.
436-
- `val` - A numeric value of the number of rows
437-
438-
### minHeight(el, val)
439-
440-
Set the `minHeight` for a widget.
441-
442-
- `el` - widget to modify.
443-
- `val` - A numeric value of the number of rows
444-
445-
### maxWidth(el, val)
446-
447-
Set the `maxWidth` for a widget.
448-
449-
- `el` - widget to modify.
450-
- `val` - A numeric value of the number of columns
451-
452-
### minWidth(el, val)
453-
454-
Set the `minWidth` for a widget.
455-
456-
- `el` - widget to modify.
457-
- `val` - A numeric value of the number of columns
458-
459417
### movable(el, val)
460418

461419
Enables/Disables moving.
462420

463421
- `el` - widget to modify
464422
- `val` - if `true` widget will be draggable.
465423

466-
### move(el, x, y)
467-
468-
Changes widget position
469-
470-
Parameters:
471-
472-
- `el` - widget to move
473-
- `x`, `y` - new position. If value is `null` or `undefined` it will be ignored.
474-
475424
### removeWidget(el, removeDOM = true, triggerEvent = true)
476425

477426
Removes widget from the grid.
@@ -490,15 +439,6 @@ Parameters:
490439

491440
- `removeDOM` - if `false` nodes won't be removed from the DOM (Optional. Default `true`).
492441

493-
### resize(el, width, height)
494-
495-
Changes widget size
496-
497-
Parameters:
498-
499-
- `el` - widget to resize
500-
- `width`, `height` - new dimensions. If value is `null` or `undefined` it will be ignored.
501-
502442
### resizable(el, val)
503443

504444
Enables/Disables resizing.
@@ -523,15 +463,14 @@ Toggle the grid static state. Also toggle the `grid-stack-static` class.
523463

524464
- `staticValue` - if `true` the grid becomes static.
525465

526-
### update(el, x, y, width, height)
466+
### update(el: GridStackElement, opts: GridStackWidget)
527467

528468
Parameters:
529469

530-
- `el` - widget to move
531-
- `x`, `y` - new position. If value is `null` or `undefined` it will be ignored.
532-
- `width`, `height` - new dimensions. If value is `null` or `undefined` it will be ignored.
470+
- `el` - widget to move (element or class string)
471+
- `opts` - updates all the possible item attributes passed in the structure (x,y,width,height, etc..). Only those set will be updated.
533472

534-
Updates widget position/size.
473+
Updates widget position/size and other info. Note: if you need to call this on all nodes, use load() instead which will update what changed and more.
535474

536475
### willItFit(x, y, width, height, autoPosition)
537476

spec/gridstack-spec.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,27 +1768,13 @@ describe('gridstack', function() {
17681768
afterEach(function() {
17691769
document.body.removeChild(document.getElementById('gs-cont'));
17701770
});
1771+
/* saving as example
17711772
it('warning if OLD setGridWidth is called', function() {
17721773
let grid: any = GridStack.init();
17731774
grid.setGridWidth(11); // old 0.5.2 API
17741775
expect(grid.getColumn()).toBe(11);
17751776
expect(console.warn).toHaveBeenCalledWith('gridstack.js: Function `setGridWidth` is deprecated in v0.5.3 and has been replaced with `column`. It will be **completely** removed in v1.0');
17761777
});
1777-
it('warning if OLD setColumn is called', function() {
1778-
let grid: any = GridStack.init();
1779-
grid.setColumn(10); // old 0.6.4 API
1780-
expect(grid.getColumn()).toBe(10);
1781-
expect(console.warn).toHaveBeenCalledWith('gridstack.js: Function `setColumn` is deprecated in v0.6.4 and has been replaced with `column`. It will be **completely** removed in v1.0');
1782-
});
1783-
it('warning if OLD grid height is set', function() {
1784-
let grid = (GridStack as any).init({height: 10}); // old 0.5.2 Opt now maxRow
1785-
expect(grid.opts.maxRow).toBe(10);
1786-
expect(grid.engine.maxRow).toBe(10);
1787-
expect(console.warn).toHaveBeenCalledWith('gridstack.js: Option `height` is deprecated in v0.5.3 and has been replaced with `maxRow`. It will be **completely** removed in v1.0');
1788-
});
1789-
it('warning if OLD oneColumnModeClass is set (no changes)', function() {
1790-
(GridStack as any).init({oneColumnModeClass: 'foo'}); // deleted 0.6.3 Opt
1791-
expect(console.warn).toHaveBeenCalledWith('gridstack.js: Option `oneColumnModeClass` is deprecated in v0.6.3. Use class `.grid-stack-1` instead');
1792-
});
1778+
*/
17931779
});
17941780
});

src/gridstack.ts

Lines changed: 18 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { GridStackEngine } from './gridstack-engine';
10-
import { obsoleteOpts, obsoleteOptsDel, obsoleteAttr, obsolete, Utils, HeightData } from './utils';
10+
import { obsoleteOpts, obsoleteOptsDel, obsoleteAttr, Utils, HeightData } from './utils';
1111
import { GridStackElement, GridItemHTMLElement, GridStackWidget, GridStackNode, GridStackOptions, numberOrString, ColumnOptions } from './types';
1212
import { GridStackDDI } from './gridstack-ddi';
1313

@@ -147,14 +147,8 @@ export class GridStack {
147147
this.el = el; // exposed HTML element to the user
148148
opts = opts || {}; // handles null/undefined/0
149149

150-
obsoleteOpts(opts, 'width', 'column', 'v0.5.3');
151-
obsoleteOpts(opts, 'height', 'maxRow', 'v0.5.3');
152150
obsoleteOpts(opts, 'verticalMargin', 'margin', 'v2.0');
153-
obsoleteOptsDel(opts, 'oneColumnModeClass', 'v0.6.3', '. Use class `.grid-stack-1` instead');
154151

155-
// container attributes
156-
obsoleteAttr(this.el, 'data-gs-width', 'data-gs-column', 'v0.5.3');
157-
obsoleteAttr(this.el, 'data-gs-height', 'data-gs-max-row', 'v0.5.3');
158152
obsoleteAttr(this.el, 'data-gs-current-height', 'data-gs-current-row', 'v1.0.0');
159153

160154
// if row property exists, replace minRow and maxRow instead
@@ -713,15 +707,6 @@ export class GridStack {
713707
return this.engine.isAreaEmpty(x, y, width, height);
714708
}
715709

716-
/**
717-
* Locks/unlocks widget.
718-
* @param el element or selector to modify.
719-
* @param locked if true widget will be locked.
720-
*/
721-
public locked(els: GridStackElement, locked: boolean): GridStack {
722-
return this.update(els, {locked});
723-
}
724-
725710
/**
726711
* If you add elements to your grid by hand, you have to tell gridstack afterwards to make them widgets.
727712
* If you want gridstack to add the elements for you, use `addWidget()` instead.
@@ -742,52 +727,6 @@ export class GridStack {
742727
return el;
743728
}
744729

745-
/**
746-
* Set the maxWidth for a widget.
747-
* @param els widget or selector to modify.
748-
* @param maxWidth A numeric value of the number of columns
749-
*/
750-
public maxWidth(els: GridStackElement, maxWidth: number): GridStack {
751-
return this.update(els, {maxWidth});
752-
}
753-
754-
/**
755-
* Set the minWidth for a widget.
756-
* @param els widget or selector to modify.
757-
* @param minWidth A numeric value of the number of columns
758-
*/
759-
public minWidth(els: GridStackElement, minWidth: number): GridStack {
760-
return this.update(els, {minWidth});
761-
}
762-
763-
/**
764-
* Set the maxHeight for a widget.
765-
* @param els widget or selector to modify.
766-
* @param maxHeight A numeric value of the number of rows
767-
*/
768-
public maxHeight(els: GridStackElement, maxHeight: number): GridStack {
769-
return this.update(els, {maxHeight});
770-
}
771-
772-
/**
773-
* Set the minHeight for a widget.
774-
* @param els widget or selector to modify.
775-
* @param minHeight A numeric value of the number of rows
776-
*/
777-
public minHeight(els: GridStackElement, minHeight: number): GridStack {
778-
return this.update(els, {minHeight});
779-
}
780-
781-
/**
782-
* Changes widget position
783-
* @param els widget or singular selector to modify
784-
* @param x new position x. If value is null or undefined it will be ignored.
785-
* @param y new position y. If value is null or undefined it will be ignored.
786-
*/
787-
public move(els: GridStackElement, x?: number, y?: number): GridStack {
788-
return this.update(els, {x, y});
789-
}
790-
791730
/**
792731
* Event handler that extracts our CustomEvent data out automatically for receiving custom
793732
* notifications (see doc for supported events)
@@ -903,16 +842,6 @@ export class GridStack {
903842
return this;
904843
}
905844

906-
/**
907-
* Changes widget size
908-
* @param els widget or singular selector to modify
909-
* @param width new dimensions width. If value is null or undefined it will be ignored.
910-
* @param height new dimensions height. If value is null or undefined it will be ignored.
911-
*/
912-
public resize(els: GridStackElement, width?: number, height?: number): GridStack {
913-
return this.update(els, {width, height});
914-
}
915-
916845
/**
917846
* Toggle the grid animation state. Toggles the `grid-stack-animate` class.
918847
* @param doAnimate if true the grid will animate.
@@ -940,7 +869,7 @@ export class GridStack {
940869
}
941870

942871
/**
943-
* Updates widget position/size.
872+
* Updates widget position/size and other info. Note: if you need to call this on all nodes, use load() instead which will update what changed.
944873
* @param els widget or selector of objects to modify (note: setting the same x,y for multiple items will be indeterministic and likely unwanted)
945874
* @param opt new widget options (x,y,width,height, etc..). Only those set will be updated.
946875
*/
@@ -1357,21 +1286,13 @@ export class GridStack {
13571286
}
13581287

13591288
/** @internal convert a potential selector into actual element */
1360-
public static getElement(els: GridStackElement = '.grid-stack-item'): GridItemHTMLElement {
1361-
return Utils.getElement(els);
1362-
}
1289+
public static getElement(els: GridStackElement = '.grid-stack-item'): GridItemHTMLElement { return Utils.getElement(els) }
13631290
/** @internal */
1364-
public static getElements(els: GridStackElement = '.grid-stack-item'): GridItemHTMLElement[] {
1365-
return Utils.getElements(els);
1366-
}
1291+
public static getElements(els: GridStackElement = '.grid-stack-item'): GridItemHTMLElement[] { return Utils.getElements(els) }
13671292
/** @internal */
1368-
public static getGridElement(els: GridStackElement): GridHTMLElement {
1369-
return GridStack.getElement(els);
1370-
}
1293+
public static getGridElement(els: GridStackElement): GridHTMLElement { return GridStack.getElement(els) }
13711294
/** @internal */
1372-
public static getGridElements(els: string): GridHTMLElement[] {
1373-
return Utils.getElements(els);
1374-
}
1295+
public static getGridElements(els: string): GridHTMLElement[] { return Utils.getElements(els) }
13751296

13761297
/** @internal initialize margin top/bottom/left/right and units */
13771298
private initMargin(): GridStack {
@@ -1437,21 +1358,6 @@ export class GridStack {
14371358
return this;
14381359
}
14391360

1440-
/** @internal called to update an element(s) attributes and node values */
1441-
private _updateAttr(els: GridStackElement, val: number, attr: string, field: string): GridStack {
1442-
GridStack.getElements(els).forEach(el => {
1443-
if (val) {
1444-
el.setAttribute(attr, String(val));
1445-
} else {
1446-
el.removeAttribute(attr);
1447-
}
1448-
if (el.gridstackNode) {
1449-
el.gridstackNode[field] = (val || undefined);
1450-
}
1451-
});
1452-
return this;
1453-
}
1454-
14551361
/*
14561362
* drag&drop empty stubs that will be implemented in gridstack-dd.ts for non static grid
14571363
* so we don't incur the load unless needed.
@@ -1486,11 +1392,19 @@ export class GridStack {
14861392
/** @internal prepares the element for drag&drop **/
14871393
public _prepareDragDropByNode(node: GridStackNode): GridStack { return this; }
14881394

1489-
// legacy method renames
1395+
// 2.x API that just calls the new and better update() - keep those around for backward compat only...
1396+
/** @internal */
1397+
public locked(els: GridStackElement, locked: boolean): GridStack { return this.update(els, {locked}) }
1398+
/** @internal */
1399+
public maxWidth(els: GridStackElement, maxWidth: number): GridStack { return this.update(els, {maxWidth}) }
1400+
/** @internal */
1401+
public minWidth(els: GridStackElement, minWidth: number): GridStack { return this.update(els, {minWidth}) }
1402+
/** @internal */
1403+
public maxHeight(els: GridStackElement, maxHeight: number): GridStack { return this.update(els, {maxHeight}) }
14901404
/** @internal */
1491-
private setGridWidth = obsolete(this, GridStack.prototype.column, 'setGridWidth', 'column', 'v0.5.3');
1405+
public minHeight(els: GridStackElement, minHeight: number): GridStack { return this.update(els, {minHeight}) }
14921406
/** @internal */
1493-
private setColumn = obsolete(this, GridStack.prototype.column, 'setColumn', 'column', 'v0.6.4');
1407+
public move(els: GridStackElement, x?: number, y?: number): GridStack { return this.update(els, {x, y}) }
14941408
/** @internal */
1495-
private getGridHeight = obsolete(this, GridStackEngine.prototype.getRow, 'getGridHeight', 'getRow', 'v1.0.0');
1409+
public resize(els: GridStackElement, width?: number, height?: number): GridStack { return this.update(els, {width, height}) }
14961410
}

0 commit comments

Comments
 (0)