Skip to content

Commit 4a48862

Browse files
author
Alain Dumesny
committed
WebComponent support
* insert style before our grid (instead of head) to support webcomponent * fix #540 * tested on Chrome, FF, Edge, IE11 and all our demos *Note: I didn't have a way to test webcomponent support, but this should do it.
1 parent ee53a17 commit 4a48862

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ $(function () {
143143

144144
// you can now call on any grid this...
145145
$('.grid-stack').data('gridstack').printCount();
146-
});
146+
});
147147
```
148148

149149
## Touch devices support

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Change log
3131

3232
## v0.6.3-dev (upcoming changes)
3333

34+
- fix [#540](https://github.com/gridstack/gridstack.js/issues/540) WebComponent support: CSS file now insert before grid instead of 'head'
3435
- fix [#1143](https://github.com/gridstack/gridstack.js/issues/1143) nested grids with different `acceptWidgets` class
3536
- fix [#1142](https://github.com/gridstack/gridstack.js/issues/1142) add/remove widget will also trigger change events when it should.
3637
- optimized `change` callback to save original x,y,w,h values and only call those that changed [1148](https://github.com/gridstack/gridstack.js/pull/1148)

src/gridstack.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,23 @@
7575
return Utils.sortBy(nodes, function(n) { return (n.x + n.y * column); });
7676
},
7777

78-
createStylesheet: function(id) {
79-
var style = document.createElement('style');
80-
style.setAttribute('type', 'text/css');
81-
style.setAttribute('data-gs-style-id', id);
82-
if (style.styleSheet) {
83-
style.styleSheet.cssText = '';
84-
} else {
85-
style.appendChild(document.createTextNode(''));
86-
}
87-
document.getElementsByTagName('head')[0].appendChild(style);
88-
return style.sheet;
89-
},
78+
createStylesheet: function(id, parent) {
79+
var style = document.createElement('style');
80+
style.setAttribute('type', 'text/css');
81+
style.setAttribute('data-gs-style-id', id);
82+
if (style.styleSheet) {
83+
style.styleSheet.cssText = '';
84+
} else {
85+
style.appendChild(document.createTextNode(''));
86+
}
87+
if (!parent) { parent = document.getElementsByTagName('head')[0]; } // default to head
88+
parent.insertBefore(style, parent.firstChild);
89+
return style.sheet;
90+
},
9091

91-
removeStylesheet: function(id) {
92-
$('STYLE[data-gs-style-id=' + id + ']').remove();
93-
},
92+
removeStylesheet: function(id) {
93+
$('STYLE[data-gs-style-id=' + id + ']').remove();
94+
},
9495

9596
insertCSSRule: function(sheet, selector, rules, index) {
9697
if (typeof sheet.insertRule === 'function') {
@@ -1068,7 +1069,8 @@
10681069
Utils.removeStylesheet(this._stylesId);
10691070
}
10701071
this._stylesId = 'gridstack-style-' + (Math.random() * 100000).toFixed();
1071-
this._styles = Utils.createStylesheet(this._stylesId);
1072+
// insert style to parent (instead of 'head') to support WebComponent
1073+
this._styles = Utils.createStylesheet(this._stylesId, this.container.get(0).parentNode);
10721074
if (this._styles !== null) {
10731075
this._styles._max = 0;
10741076
}

0 commit comments

Comments
 (0)