Skip to content

Commit a902f3c

Browse files
authored
Merge pull request #1038 from adumesny/develop
addWidget(el, options) with object param
2 parents 0fc9b72 + 52dfb46 commit a902f3c

File tree

10 files changed

+102
-44
lines changed

10 files changed

+102
-44
lines changed

demo/float.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ <h1>Float grid demo</h1>
7575
width: 1 + 3 * Math.random(),
7676
height: 1 + 3 * Math.random()
7777
};
78-
this.grid.addWidget($('<div><div class="grid-stack-item-content"></div></div>'),
79-
node.x, node.y, node.width, node.height);
78+
this.grid.addWidget($('<div><div class="grid-stack-item-content"></div></div>'), node);
8079
return false;
8180
}.bind(this);
8281

demo/responsive.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ <h1>Responsive grid demo</h1>
107107
this.grid.removeAll();
108108
var items = GridStackUI.Utils.sort(this.serializedData);
109109
items.forEach(function (node, i) {
110-
this.grid.addWidget($('<div><div class="grid-stack-item-content">' + i + '</div></div>'),
111-
node.x, node.y, node.width, node.height);
110+
this.grid.addWidget($('<div><div class="grid-stack-item-content">' + i + '</div></div>'), node);
112111
}.bind(this));
113112
return false;
114113
}.bind(this);

demo/serialization.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ <h1>Serialization demo</h1>
7777
this.grid.removeAll();
7878
var items = GridStackUI.Utils.sort(this.serializedData);
7979
items.forEach(function (node) {
80-
this.grid.addWidget($('<div><div class="grid-stack-item-content"></div></div>'),
81-
node.x, node.y, node.width, node.height);
80+
this.grid.addWidget($('<div><div class="grid-stack-item-content"></div></div>'), node);
8281
}, this);
8382
return false;
8483
}.bind(this);

demo/two.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ <h1>Two grids demo</h1>
129129
var grid = $(this).data('gridstack');
130130

131131
items.forEach(function (node) {
132-
grid.addWidget($('<div><div class="grid-stack-item-content"></div></div>'),
133-
node.x, node.y, node.width, node.height);
132+
grid.addWidget($('<div><div class="grid-stack-item-content"></div></div>'), node);
134133
});
135134
});
136135

doc/CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ Change log
2626
- undefined x,y position messes up grid ([#1017](https://github.com/gridstack/gridstack.js/issues/1017)).
2727
- changed code to 2 spaces.
2828
- fix minHeight during `onStartMoving()` ([#999](https://github.com/gridstack/gridstack.js/issues/999)).
29-
- TypeScript definition file now included - no need to include @types/gridstack, easier to update.
29+
- TypeScript definition file now included - no need to include @types/gridstack, easier to update ([#1036](https://github.com/gridstack/gridstack.js/pull/1036)).
30+
- new addWidget(el, options) to pass object so you don't have to spell 10 params. ([#907](https://github.com/gridstack/gridstack.js/issues/907)).
3031

3132
## v0.5.1 (2019-11-07)
3233

doc/README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ gridstack.js API
2020
- [resizestart(event, ui)](#resizestartevent-ui)
2121
- [gsresizestop(event, ui)](#gsresizestopevent-ui)
2222
- [API](#api)
23-
- [addWidget(el[, x, y, width, height, autoPosition, minWidth, maxWidth, minHeight, maxHeight, id])](#addwidgetel-x-y-width-height-autoposition-minwidth-maxwidth-minheight-maxheight-id)
23+
- [addWidget(el, [options])](#addwidgetel-options)
24+
- [addWidget(el, [x, y, width, height, autoPosition, minWidth, maxWidth, minHeight, maxHeight, id])](#addwidgetel-x-y-width-height-autoposition-minwidth-maxwidth-minheight-maxheight-id)
2425
- [batchUpdate()](#batchupdate)
2526
- [cellHeight()](#cellheight)
2627
- [cellHeight(val, noUpdate)](#cellheightval-noupdate)
@@ -217,7 +218,11 @@ $('.grid-stack').on('gsresizestop', function(event, elem) {
217218

218219
## API
219220

220-
### addWidget(el[, x, y, width, height, autoPosition, minWidth, maxWidth, minHeight, maxHeight, id])
221+
### addWidget(el, [options])
222+
223+
Creates new widget and returns it. Options is an object containing the fields x,y,width,height,etc... described below.
224+
225+
### addWidget(el, [x, y, width, height, autoPosition, minWidth, maxWidth, minHeight, maxHeight, id])
221226

222227
Creates new widget and returns it.
223228

@@ -238,7 +243,6 @@ before calling `addWidget` for additional check.
238243

239244
```javascript
240245
$('.grid-stack').gridstack();
241-
242246
var grid = $('.grid-stack').data('gridstack');
243247
grid.addWidget(el, 0, 0, 3, 2, true);
244248
```

spec/e2e/html/gridstack-with-height.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ <h1>gridstack.js tests</h1>
6363
items.forEach(function(node) {
6464
var w = $('<div><div class="grid-stack-item-content"></div></div>');
6565
w.attr('id', 'item-' + (++id));
66-
this.grid.addWidget(w,
67-
node.x, node.y, node.width, node.height);
66+
this.grid.addWidget(w, node);
6867
}, this);
6968
};
7069
});

spec/gridstack-spec.js

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -864,18 +864,10 @@ describe('gridstack', function() {
864864
afterEach(function() {
865865
document.body.removeChild(document.getElementById('gs-cont'));
866866
});
867-
it('should allow same x, y coordinates for widgets.', function() {
868-
var options = {
869-
cellHeight: 80,
870-
verticalMargin: 10,
871-
float: true
872-
};
873-
$('.grid-stack').gridstack(options);
867+
it('should keep all widget options the same (autoPosition off', function() {
868+
$('.grid-stack').gridstack({float: true});
874869
var grid = $('.grid-stack').data('gridstack');
875-
var widgetHTML =
876-
' <div class="grid-stack-item">' +
877-
' <div class="grid-stack-item-content"></div>' +
878-
' </div>';
870+
var widgetHTML = '<div class="grid-stack-item"><div class="grid-stack-item-content"></div></div>';
879871
var widget = grid.addWidget(widgetHTML, 6, 7, 2, 3, false, 1, 4, 2, 5, 'coolWidget');
880872
var $widget = $(widget);
881873
expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(6);
@@ -900,23 +892,43 @@ describe('gridstack', function() {
900892
document.body.removeChild(document.getElementById('gs-cont'));
901893
});
902894
it('should change x, y coordinates for widgets.', function() {
903-
var options = {
904-
cellHeight: 80,
905-
verticalMargin: 10
906-
};
907-
$('.grid-stack').gridstack(options);
895+
$('.grid-stack').gridstack({float: true});
908896
var grid = $('.grid-stack').data('gridstack');
909-
var widgetHTML =
910-
' <div class="grid-stack-item">' +
911-
' <div class="grid-stack-item-content"></div>' +
912-
' </div>';
897+
var widgetHTML = '<div class="grid-stack-item"><div class="grid-stack-item-content"></div></div>';
913898
var widget = grid.addWidget(widgetHTML, 9, 7, 2, 3, true);
914899
var $widget = $(widget);
915-
expect(parseInt($widget.attr('data-gs-x'), 10)).not.toBe(6);
900+
expect(parseInt($widget.attr('data-gs-x'), 10)).not.toBe(9);
916901
expect(parseInt($widget.attr('data-gs-y'), 10)).not.toBe(7);
917902
});
918903
});
919904

905+
describe('grid method addWidget with widget options', function() {
906+
beforeEach(function() {
907+
document.body.insertAdjacentHTML(
908+
'afterbegin', gridstackHTML);
909+
});
910+
afterEach(function() {
911+
document.body.removeChild(document.getElementById('gs-cont'));
912+
});
913+
it('should keep all widget options the same (autoPosition off', function() {
914+
$('.grid-stack').gridstack();
915+
var grid = $('.grid-stack').data('gridstack');
916+
var widgetHTML = '<div class="grid-stack-item"><div class="grid-stack-item-content"></div></div>';
917+
var widget = grid.addWidget(widgetHTML, {x: 8, height: 2, id: 'optionWidget'});
918+
var $widget = $(widget);
919+
expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(8);
920+
expect(parseInt($widget.attr('data-gs-width'), 10)).toBe(1);
921+
expect(parseInt($widget.attr('data-gs-height'), 10)).toBe(2);
922+
expect($widget.attr('data-gs-auto-position')).toBe(undefined);
923+
expect($widget.attr('data-gs-min-width')).toBe(undefined);
924+
expect($widget.attr('data-gs-max-width')).toBe(undefined);
925+
expect($widget.attr('data-gs-min-height')).toBe(undefined);
926+
expect($widget.attr('data-gs-max-height')).toBe(undefined);
927+
expect($widget.attr('data-gs-id')).toBe('optionWidget');
928+
});
929+
});
930+
931+
920932
describe('grid.destroy', function() {
921933
beforeEach(function() {
922934
document.body.insertAdjacentHTML(

src/gridstack.d.ts

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// TypeScript Version: 2.3
1010

1111
interface JQuery {
12-
gridstack(options: IGridstackOptions): JQuery;
12+
gridstack(options: GridstackOptions): JQuery;
1313
data(key: 'gridstack'): GridStack;
1414
}
1515

@@ -27,7 +27,21 @@ interface GridStack {
2727
*
2828
* Widget will be always placed even if result height is more than actual grid height.
2929
* You need to use willItFit method before calling addWidget for additional check.
30-
* See also makeWidget.
30+
* See also `makeWidget()`.
31+
*
32+
* @example
33+
* $('.grid-stack').gridstack();
34+
* var grid = $('.grid-stack').data('gridstack');
35+
* grid.addWidget(el, {width: 3, autoPosition: true});
36+
*
37+
* @param {GridStackElement} el widget to add
38+
* @param {GridstackWidget} options widget position/size options (optional)
39+
*/
40+
addWidget(el: GridStackElement, options?: GridstackWidget): JQuery;
41+
42+
/**
43+
* Creates new widget and returns it.
44+
* Legacy: Spelled out version of the widgets options, recommend use new version instead.
3145
*
3246
* @example
3347
* $('.grid-stack').gridstack();
@@ -60,7 +74,7 @@ interface GridStack {
6074
cellHeight(): number;
6175

6276
/**
63-
* Update current cell height - see `IGridstackOptions.cellHeight` for format.
77+
* Update current cell height - see `GridstackOptions.cellHeight` for format.
6478
* This method rebuilds an internal CSS style sheet.
6579
* Note: You can expect performance issues if call this method too often.
6680
*
@@ -277,7 +291,7 @@ interface GridStack {
277291
verticalMargin(): number;
278292

279293
/**
280-
* Updates the vertical margin - see `IGridstackOptions.verticalMargin` for format options.
294+
* Updates the vertical margin - see `GridstackOptions.verticalMargin` for format options.
281295
*
282296
* @param {number | string} value new vertical margin value
283297
* @param {boolean} noUpdate (optional) if true, styles will not be updated
@@ -313,13 +327,39 @@ interface MousePosition {
313327
}
314328

315329
/**
316-
* Defines the position of a cell inside the grid
330+
* Defines the position of a cell inside the grid
317331
*/
318332
interface CellPosition {
319333
x: number;
320334
y: number;
321335
}
322336

337+
/**
338+
* Gridstack Widget creation options
339+
*/
340+
interface GridstackWidget {
341+
/** x position (default?: 0) */
342+
x?: number;
343+
/** y position (default?: 0) */
344+
y?: number;
345+
/** width (default?: 1) */
346+
width?: number;
347+
/** height (default?: 1) */
348+
height?: number;
349+
/** autoPosition if true then x, y parameters will be ignored and widget will be places on the first available position (default?: false) */
350+
autoPosition?: boolean;
351+
/** minimum width allowed during resize/creation (default?: undefined = un-constrained) */
352+
minWidth?: number;
353+
/** maximum width allowed during resize/creation (default?: undefined = un-constrained) */
354+
maxWidth?: number;
355+
/** minimum height allowed during resize/creation (default?: undefined = un-constrained) */
356+
minHeight?: number;
357+
/** maximum height allowed during resize/creation (default?: undefined = un-constrained) */
358+
maxHeight?: number;
359+
/** id value for `data-gs-id` stored on the widget (default?: undefined)*/
360+
id?: number | string;
361+
}
362+
323363
declare namespace GridStackUI {
324364
interface Utils {
325365
/**
@@ -336,7 +376,7 @@ declare namespace GridStackUI {
336376
* Gridstack Options
337377
* Defines the options for a Gridstack
338378
*/
339-
interface IGridstackOptions {
379+
interface GridstackOptions {
340380
/**
341381
* if true of jquery selector the grid will accept widgets dragged from other grids or from
342382
* outside (default: false) See [example](http://gridstack.github.io/gridstack.js/demo/two.html)
@@ -479,3 +519,4 @@ interface IGridstackOptions {
479519
*/
480520
width?: number;
481521
}
522+

src/gridstack.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@
394394

395395
GridStackEngine.prototype._prepareNode = function(node, resizing) {
396396
node = node || {};
397-
// if we're missing position, have grid position us automatically (before we set them to 0,0)
397+
// if we're missing position, have the grid position us automatically (before we set them to 0,0)
398398
if (node.x === undefined || node.y === undefined) {
399399
node.autoPosition = true;
400400
}
@@ -1435,8 +1435,13 @@
14351435
}
14361436
};
14371437

1438-
GridStack.prototype.addWidget = function(el, x, y, width, height, autoPosition, minWidth, maxWidth,
1439-
minHeight, maxHeight, id) {
1438+
GridStack.prototype.addWidget = function(el, x, y, width, height, autoPosition, minWidth, maxWidth, minHeight, maxHeight, id) {
1439+
1440+
// instead of passing all the params, the user might pass an object with all fields instead, if so extract them and call us back
1441+
if (typeof x === 'object') {
1442+
return this.addWidget(el, x.x, x.y, x.width, x.height, x.autoPosition, x.minWidth, x.maxWidth, x.minHeight, x.maxHeight, x.id);
1443+
}
1444+
14401445
el = $(el);
14411446
if (typeof x != 'undefined') { el.attr('data-gs-x', x); }
14421447
if (typeof y != 'undefined') { el.attr('data-gs-y', y); }

0 commit comments

Comments
 (0)