Skip to content

Commit 8c46701

Browse files
authored
Merge pull request #1280 from adumesny/typescript
TS: fix 'addWidget' ignores data attributes #1276, 1.1.2 release
2 parents 910d00f + d12dbee commit 8c46701

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ npm install --save gridstack
8080
* Using CDN (minimized):
8181

8282
```html
83-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected].1/dist/gridstack.min.css" />
84-
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/dist/gridstack.all.js"></script>
83+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected].2/dist/gridstack.min.css" />
84+
<script src="https://cdn.jsdelivr.net/npm/[email protected].2/dist/gridstack.all.js"></script>
8585
```
8686

8787
if you need to debug, look at the git demo/ examples for non min includes.
@@ -164,7 +164,7 @@ GridStack.init( {column: N} );
164164

165165
2) include `gridstack-extra.css` if **N < 12** (else custom CSS - see next). Without these, things will not render/work correctly.
166166
```html
167-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected].1/dist/gridstack-extra.css"/>
167+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected].2/dist/gridstack-extra.css"/>
168168

169169
<div class="grid-stack grid-stack-N">...</div>
170170
```

doc/CHANGES.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Change log
66
**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*
77

88
- [2.0.0-dev (upcoming)](#200-dev-upcoming)
9-
- [1.1.1-dev (upcoming)](#111-dev-upcoming)
9+
- [1.1.2 (2020-05-17)](#112-2020-05-17)
1010
- [1.1.1 (2020-03-17)](#111-2020-03-17)
1111
- [1.1.0 (2020-02-29)](#110-2020-02-29)
1212
- [v1.0.0 (2020-02-23)](#v100-2020-02-23)
@@ -36,14 +36,16 @@ Change log
3636

3737
## 2.0.0-dev (upcoming)
3838

39+
- re-write to native Typescript, removing all JQuery from main code and API (drag&drop plugin still using for now)
3940
- add `getGridItems()` to return list of HTML grid items
4041

41-
## 1.1.1-dev (upcoming)
42+
## 1.1.2 (2020-05-17)
4243

4344
- fix [1229](https://github.com/gridstack/gridstack.js/issues/1229) `staticGrid` no longer disable oneColumnMode
4445
- fix [1195](https://github.com/gridstack/gridstack.js/issues/1195) options broken with ember hash helper - thanks [@btecu](https://github.com/btecu)
4546
- fix [1250](https://github.com/gridstack/gridstack.js/issues/1250) don't remove item from another grid
4647
- fix [1261](https://github.com/gridstack/gridstack.js/issues/1261) `init()` clones passed options so second doesn't affect first one
48+
- fix [1276](https://github.com/gridstack/gridstack.js/issues/1276) `addWidget()` ignores data attributes
4749

4850
## 1.1.1 (2020-03-17)
4951

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)