Skip to content

Commit 910d00f

Browse files
authored
Merge pull request #1263 from adumesny/typescript
TS: type any cleanup + clone grid option fix
2 parents ef9e06c + 5b7f023 commit 910d00f

File tree

8 files changed

+399
-386
lines changed

8 files changed

+399
-386
lines changed

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Change log
4343
- fix [1229](https://github.com/gridstack/gridstack.js/issues/1229) `staticGrid` no longer disable oneColumnMode
4444
- fix [1195](https://github.com/gridstack/gridstack.js/issues/1195) options broken with ember hash helper - thanks [@btecu](https://github.com/btecu)
4545
- fix [1250](https://github.com/gridstack/gridstack.js/issues/1250) don't remove item from another grid
46+
- fix [1261](https://github.com/gridstack/gridstack.js/issues/1261) `init()` clones passed options so second doesn't affect first one
4647

4748
## 1.1.1 (2020-03-17)
4849

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"karma-chrome-launcher": "^3.1.0",
7070
"karma-cli": "^2.0.0",
7171
"karma-jasmine": "^3.1.1",
72-
"karma-typescript": "^5.0.1",
72+
"karma-typescript": "4.1.1",
7373
"node-sass": "^4.13.1",
7474
"puppeteer": "^2.1.1",
7575
"serve-static": "^1.14.1",

src/gridstack-dragdrop-plugin.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export type DDDropOpt = {
2020
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2121
export type DDOpts = 'enable' | 'disable' | 'destroy' | 'option' | string | {} | any;
2222
export type DDKey = 'minWidth' | 'minHeight' | string;
23+
export type DDValue = number | string;
2324

2425
/** drag&drop events callbacks */
2526
export type DDCallback = (event: Event, arg2: GridItemHTMLElement) => void;
@@ -39,15 +40,15 @@ export class GridStackDragDropPlugin {
3940
this.grid = grid;
4041
}
4142

42-
public resizable(el: GridItemHTMLElement, opts: DDOpts, key?: DDKey, value?): GridStackDragDropPlugin {
43+
public resizable(el: GridItemHTMLElement, opts: DDOpts, key?: DDKey, value?: DDValue): GridStackDragDropPlugin {
4344
return this;
4445
}
4546

46-
public draggable(el: GridItemHTMLElement, opts: DDOpts, key?: DDKey, value?): GridStackDragDropPlugin {
47+
public draggable(el: GridItemHTMLElement, opts: DDOpts, key?: DDKey, value?: DDValue): GridStackDragDropPlugin {
4748
return this;
4849
}
4950

50-
public droppable(el: GridItemHTMLElement, opts: DDOpts | DDDropOpt, key?: DDKey, value?): GridStackDragDropPlugin {
51+
public droppable(el: GridItemHTMLElement, opts: DDOpts | DDDropOpt, key?: DDKey, value?: DDValue): GridStackDragDropPlugin {
5152
return this;
5253
}
5354

src/gridstack-engine.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ export class GridStackEngine {
251251
public getDirtyNodes(verify?: boolean): GridStackNode[] {
252252
// compare original X,Y,W,H (or entire node?) instead as _dirty can be a temporary state
253253
if (verify) {
254-
let dirtNodes = [];
254+
let dirtNodes: GridStackNode[] = [];
255255
this.nodes.forEach(n => {
256256
if (n._dirty) {
257257
if (n.y === n._origY && n.x === n._origX && n.width === n._origW && n.height === n._origH) {
@@ -352,7 +352,7 @@ export class GridStackEngine {
352352
return true;
353353
}
354354

355-
let clonedNode;
355+
let clonedNode: GridStackNode;
356356
let clone = new GridStackEngine(
357357
this.column,
358358
null,

src/gridstack.ts

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class GridStack {
7272
return null;
7373
}
7474
if (!el.gridstack) {
75-
el.gridstack = new GridStack(el, options);
75+
el.gridstack = new GridStack(el, Utils.clone(options));
7676
}
7777
return el.gridstack
7878
}
@@ -152,14 +152,11 @@ export class GridStack {
152152
obsoleteAttr(this.el, 'data-gs-height', 'data-gs-max-row', 'v0.5.3');
153153
obsoleteAttr(this.el, 'data-gs-current-height', 'data-gs-current-row', 'v1.0.0');
154154

155-
opts.itemClass = opts.itemClass || 'grid-stack-item';
156-
157155
// if row property exists, replace minRow and maxRow instead
158156
if (opts.row) {
159157
opts.minRow = opts.maxRow = opts.row;
160158
delete opts.row;
161159
}
162-
163160
let rowAttr = Utils.toNumber(el.getAttribute('data-gs-row'));
164161

165162
// elements attributes override any passed options (like CSS style) - merge the two together
@@ -180,29 +177,28 @@ export class GridStack {
180177
staticGrid: false,
181178
_class: 'grid-stack-instance-' + (Math.random() * 10000).toFixed(0),
182179
animate: Utils.toBool(el.getAttribute('data-gs-animate')) || false,
183-
alwaysShowResizeHandle: opts.alwaysShowResizeHandle || false,
184-
resizable: Utils.defaults(opts.resizable || {}, {
180+
alwaysShowResizeHandle: false,
181+
resizable: {
185182
autoHide: !(opts.alwaysShowResizeHandle || false),
186183
handles: 'se'
187-
}),
188-
draggable: Utils.defaults(opts.draggable || {}, {
189-
handle: (opts.handleClass ? '.' + opts.handleClass : (opts.handle ? opts.handle : '')) ||
190-
'.grid-stack-item-content',
184+
},
185+
draggable: {
186+
handle: (opts.handleClass ? '.' + opts.handleClass : (opts.handle ? opts.handle : '')) || '.grid-stack-item-content',
191187
scroll: false,
192188
appendTo: 'body'
193-
}),
194-
disableDrag: opts.disableDrag || false,
195-
disableResize: opts.disableResize || false,
189+
},
190+
disableDrag: false,
191+
disableResize: false,
196192
rtl: 'auto',
197193
removable: false,
198-
removableOptions: Utils.defaults(opts.removableOptions || {}, {
194+
removableOptions: {
199195
accept: '.' + opts.itemClass
200-
}),
196+
},
201197
removeTimeout: 2000,
202198
verticalMarginUnit: 'px',
203199
cellHeightUnit: 'px',
204-
disableOneColumnMode: opts.disableOneColumnMode || false,
205-
oneColumnModeDomSort: opts.oneColumnModeDomSort
200+
disableOneColumnMode: false,
201+
oneColumnModeDomSort: false
206202
};
207203

208204
this.opts = Utils.defaults(opts, defaults);
@@ -460,7 +456,7 @@ export class GridStack {
460456
/** returns an array of grid HTML elements (no placeholder) - used internally to iterate through our children */
461457
public getGridItems(): GridItemHTMLElement[] {
462458
return Array.from(this.el.children)
463-
.filter(el => el.matches('.' + this.opts.itemClass) && !el.matches('.' + this.opts.placeholderClass)) as GridItemHTMLElement[];
459+
.filter((el: HTMLElement) => el.matches('.' + this.opts.itemClass) && !el.matches('.' + this.opts.placeholderClass)) as GridItemHTMLElement[];
464460
}
465461

466462
/**
@@ -781,9 +777,9 @@ export class GridStack {
781777
let noData = (name === 'enable' || name === 'disable');
782778
this._gsEventHandler = this._gsEventHandler || {};
783779
if (noData) {
784-
this._gsEventHandler[name] = (event) => callback(event);
780+
this._gsEventHandler[name] = (event: Event) => callback(event);
785781
} else {
786-
this._gsEventHandler[name] = (event) => callback(event, event.detail);
782+
this._gsEventHandler[name] = (event: CustomEvent) => callback(event, event.detail);
787783
}
788784
this.el.addEventListener(name, this._gsEventHandler[name]);
789785
} else if (name === 'dragstart' || name === 'dragstop' || name === 'resizestart' || name === 'resizestop' || name === 'dropped') {
@@ -1071,11 +1067,11 @@ export class GridStack {
10711067
let margin = this.opts.verticalMargin as number;
10721068

10731069
if (!this.opts.verticalMargin || this.opts.cellHeightUnit === this.opts.verticalMarginUnit) {
1074-
getHeight = (nbRows, nbMargins) => {
1070+
getHeight = (nbRows: number, nbMargins: number) => {
10751071
return (height * nbRows + margin * nbMargins) + this.opts.cellHeightUnit;
10761072
}
10771073
} else {
1078-
getHeight = (nbRows, nbMargins) => {
1074+
getHeight = (nbRows: number, nbMargins: number) => {
10791075
if (!nbRows || !nbMargins) {
10801076
return (height * nbRows + margin * nbMargins) + this.opts.cellHeightUnit;
10811077
}
@@ -1173,8 +1169,8 @@ export class GridStack {
11731169
/** @internal prepares the element for drag&drop **/
11741170
private _prepareElementsByNode(el: GridItemHTMLElement, node: GridStackNode): GridStack {
11751171
// variables used/cashed between the 3 start/move/end methods, in addition to node passed above
1176-
let cellWidth;
1177-
let cellFullHeight; // internal cellHeight + v-margin
1172+
let cellWidth: number;
1173+
let cellFullHeight: number; // internal cellHeight + v-margin
11781174

11791175
/** called when item starts moving/resizing */
11801176
let onStartMoving = (event, ui) => {
@@ -1220,7 +1216,7 @@ export class GridStack {
12201216
}
12211217

12221218
/** called when item is being dragged/resized */
1223-
let dragOrResize = (event, ui) => {
1219+
let dragOrResize = (event: Event, ui) => {
12241220
let x = Math.round(ui.position.left / cellWidth);
12251221
let y = Math.floor((ui.position.top + cellFullHeight / 2) / cellFullHeight);
12261222
let width;
@@ -1281,8 +1277,8 @@ export class GridStack {
12811277
}
12821278

12831279
/** called when the item stops moving/resizing */
1284-
let onEndMoving = (event) => {
1285-
let { target } = event;
1280+
let onEndMoving = (event: Event) => {
1281+
let target: GridItemHTMLElement = event.target as GridItemHTMLElement;
12861282
if (!target.gridstackNode) return;
12871283

12881284
// let forceNotify = false; what is the point of calling 'change' event with no data, when the 'removed' event is already called ?
@@ -1323,7 +1319,7 @@ export class GridStack {
13231319

13241320
// if we re-sized a nested grid item, let the children resize as well
13251321
if (event.type === 'resizestop') {
1326-
target.querySelectorAll('.grid-stack').forEach(el => el.gridstack._onResizeHandler());
1322+
target.querySelectorAll('.grid-stack').forEach((el: GridHTMLElement) => el.gridstack._onResizeHandler());
13271323
}
13281324
}
13291325

src/jq/jqueryui-gridstack-dragdrop-plugin.ts

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

99
import { GridStack } from '../gridstack';
10-
import { GridStackDragDropPlugin, DDOpts, DDKey, DDDropOpt, DDCallback } from '../gridstack-dragdrop-plugin';
10+
import { GridStackDragDropPlugin, DDOpts, DDKey, DDDropOpt, DDCallback, DDValue } from '../gridstack-dragdrop-plugin';
1111
import { GridItemHTMLElement } from '../types';
1212

1313
// TODO: TEMPORARY until can remove jquery-ui drag&drop and this class and use HTML5 instead !
@@ -23,7 +23,7 @@ export class JQueryUIGridStackDragDropPlugin extends GridStackDragDropPlugin {
2323
super(grid);
2424
}
2525

26-
public resizable(el: GridItemHTMLElement, opts: DDOpts, key?: DDKey, value?): GridStackDragDropPlugin {
26+
public resizable(el: GridItemHTMLElement, opts: DDOpts, key?: DDKey, value?: DDValue): GridStackDragDropPlugin {
2727
let $el: JQuery = $(el);
2828
if (opts === 'disable' || opts === 'enable') {
2929
$el.resizable(opts);
@@ -44,7 +44,7 @@ export class JQueryUIGridStackDragDropPlugin extends GridStackDragDropPlugin {
4444
return this;
4545
}
4646

47-
public draggable(el: GridItemHTMLElement, opts: DDOpts, key?: DDKey, value?): GridStackDragDropPlugin {
47+
public draggable(el: GridItemHTMLElement, opts: DDOpts, key?: DDKey, value?: DDValue): GridStackDragDropPlugin {
4848
let $el: JQuery = $(el);
4949
if (opts === 'disable' || opts === 'enable') {
5050
$el.draggable(opts);
@@ -66,7 +66,7 @@ export class JQueryUIGridStackDragDropPlugin extends GridStackDragDropPlugin {
6666
return this;
6767
}
6868

69-
public droppable(el: GridItemHTMLElement, opts: DDOpts | DDDropOpt, key?: DDKey, value?): GridStackDragDropPlugin {
69+
public droppable(el: GridItemHTMLElement, opts: DDOpts | DDDropOpt, key?: DDKey, value?: DDValue): GridStackDragDropPlugin {
7070
let $el: JQuery = $(el);
7171
if (typeof opts.accept === 'function' && !opts._accept) {
7272
// convert jquery event to generic element

src/utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ export class Utils {
173173
return Utils.closestByClass(el, name);
174174
}
175175

176-
static throttle(callback, delay) {
176+
/** @internal */
177+
static throttle(callback: () => void, delay: number) {
177178
let isWaiting = false;
178179

179180
return function (...args) {
@@ -204,7 +205,8 @@ export class Utils {
204205
}
205206
}
206207

207-
static getScrollParent(el: HTMLElement) {
208+
/** @internal */
209+
static getScrollParent(el: HTMLElement): HTMLElement {
208210
let returnEl;
209211
if (el === null) {
210212
returnEl = null;

0 commit comments

Comments
 (0)