Skip to content

Commit 7a4c611

Browse files
committed
TS: fix 'addWidget' ignores data attributes #1276
1 parent 910d00f commit 7a4c611

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

spec/gridstack-spec.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -885,14 +885,14 @@ describe('gridstack', function() {
885885

886886
});
887887

888-
describe('addWidget() with bad string value widget options', function() {
888+
describe('addWidget()', function() {
889889
beforeEach(function() {
890890
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
891891
});
892892
afterEach(function() {
893893
document.body.removeChild(document.getElementById('gs-cont'));
894894
});
895-
it('should use default', function() {
895+
it('bad string options should use default', function() {
896896
let grid = GridStack.init();
897897
let widget = grid.addWidget(widgetHTML, {x: 'foo', y: null, width: 'bar', height: ''} as any);
898898

@@ -901,24 +901,26 @@ describe('gridstack', function() {
901901
expect(parseInt(widget.getAttribute('data-gs-width'), 10)).toBe(1);
902902
expect(parseInt(widget.getAttribute('data-gs-height'), 10)).toBe(1);
903903
});
904-
});
905-
906-
describe('addWidget with null options, ', function() {
907-
beforeEach(function() {
908-
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
909-
});
910-
afterEach(function() {
911-
document.body.removeChild(document.getElementById('gs-cont'));
912-
});
913-
it('should clear x position', function() {
904+
it('null options should clear x position', function() {
914905
let grid = GridStack.init({float: true});
915906
let widgetHTML = '<div class="grid-stack-item" data-gs-x="9"><div class="grid-stack-item-content"></div></div>';
916907
let widget = grid.addWidget(widgetHTML, {x:null, y:null, width:undefined});
917908

918909
expect(parseInt(widget.getAttribute('data-gs-x'), 10)).toBe(8);
919910
expect(parseInt(widget.getAttribute('data-gs-y'), 10)).toBe(0);
920911
});
921-
});
912+
it('width attr should be retained', function() { // #1276
913+
let grid = GridStack.init({float: true});
914+
let widgetHTML = '<div class="grid-stack-item" data-gs-width="3" data-gs-max-width="4" data-gs-id="foo"><div class="grid-stack-item-content"></div></div>';
915+
let widget = grid.addWidget(widgetHTML, {x: 1, y: 5});
916+
expect(parseInt(widget.getAttribute('data-gs-x'), 10)).toBe(1);
917+
expect(parseInt(widget.getAttribute('data-gs-y'), 10)).toBe(5);
918+
expect(parseInt(widget.getAttribute('data-gs-width'), 10)).toBe(3);
919+
expect(parseInt(widget.getAttribute('data-gs-max-width'), 10)).toBe(4);
920+
expect(parseInt(widget.getAttribute('data-gs-height'), 10)).toBe(1);
921+
expect(widget.getAttribute('data-gs-id')).toBe('foo');
922+
});
923+
});
922924

923925
describe('method getFloat()', function() {
924926
beforeEach(function() {

src/gridstack.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ export class GridStack {
323323
// Tempting to initialize the passed in opt with default and valid values, but this break knockout demos
324324
// as the actual value are filled in when _prepareElement() calls el.getAttribute('data-gs-xyz) before adding the node.
325325
if (options) {
326+
// make sure we load any DOM attributes that are not specified in passed in options (which override)
327+
let domAttr = this._readAttr(el);
328+
Utils.defaults(options, domAttr);
329+
this.engine.prepareNode(options);
326330
this._writeAttr(el, options);
327331
}
328332

@@ -1405,7 +1409,7 @@ export class GridStack {
14051409
return this;
14061410
}
14071411

1408-
/** @internal call to write any default attributes back to element */
1412+
/** @internal call to read any default attributes from element */
14091413
private _readAttr(el: HTMLElement, node: GridStackNode = {}): GridstackWidget {
14101414
node.x = Utils.toNumber(el.getAttribute('data-gs-x'));
14111415
node.y = Utils.toNumber(el.getAttribute('data-gs-y'));

0 commit comments

Comments
 (0)