Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 621a239

Browse files
authored
adding styleInHead option (#1313)
* Adding styleInHead option
1 parent 46b6cd1 commit 621a239

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Change log
3737
## 1.1.2-dev (upcoming)
3838

3939
- fix [1311](https://github.com/gridstack/gridstack.js/issues/1311) domAttr is not defined
40+
- adds `styleInHead` option to allow for selecting older behavior (adding STYLE element to HEAD element instead of parentNode)
4041

4142
## 1.1.2 (2020-05-17)
4243

doc/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ gridstack.js API
102102
- `row` - fix grid number of rows. This is a shortcut of writing `minRow:N, maxRow:N`. (default `0` no constrain)
103103
- `rtl` - if `true` turns grid to RTL. Possible values are `true`, `false`, `'auto'` (default: `'auto'`) See [example](http://gridstackjs.com/demo/rtl.html)
104104
- `staticGrid` - removes drag&drop&resize (default `false`). If `true` widgets are not movable/resizable by the user, but code can still move and oneColumnMode will still work. You don't even need jQueryUI draggable/resizable. A CSS class `grid-stack-static` is also added to the container.
105+
- `styleInHead` - if `true` will add style element to `<head>` otherwise will add it to element's parent node (default `false`).
105106
- `verticalMargin` - vertical gap size (default: `20`). Can be:
106107
* an integer (px)
107108
* a string (ex: '2em', '20px', '2rem')

spec/gridstack-spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,34 @@ describe('gridstack', function() {
11551155
expect($('.grid-stack').hasClass('grid-stack-rtl')).toBe(false);
11561156
});
11571157
});
1158+
1159+
describe('grid.opts.styleInHead', function() {
1160+
beforeEach(function() {
1161+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
1162+
});
1163+
afterEach(function() {
1164+
document.body.removeChild(document.getElementById('gs-cont'));
1165+
});
1166+
it('should add STYLE to parent node as a default', function() {
1167+
var options = {
1168+
cellHeight: 80,
1169+
verticalMargin: 10,
1170+
float: false,
1171+
};
1172+
var grid = GridStack.init(options);
1173+
expect(grid._styles.ownerNode.parentNode.tagName).toBe('DIV');
1174+
});
1175+
it('should add STYLE to HEAD if styleInHead === true', function() {
1176+
var options = {
1177+
cellHeight: 80,
1178+
verticalMargin: 10,
1179+
float: false,
1180+
styleInHead: true
1181+
};
1182+
var grid = GridStack.init(options);
1183+
expect(grid._styles.ownerNode.parentNode.tagName).toBe('HEAD');
1184+
});
1185+
});
11581186

11591187
describe('grid.enableMove', function() {
11601188
beforeEach(function() {

src/gridstack.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@
733733
placeholderText: '',
734734
handle: '.grid-stack-item-content',
735735
handleClass: null,
736+
styleInHead: false,
736737
cellHeight: 60,
737738
verticalMargin: 20,
738739
auto: true,
@@ -1092,8 +1093,9 @@
10921093
Utils.removeStylesheet(this._stylesId);
10931094
}
10941095
this._stylesId = 'gridstack-style-' + (Math.random() * 100000).toFixed();
1095-
// insert style to parent (instead of 'head') to support WebComponent
1096-
this._styles = Utils.createStylesheet(this._stylesId, this.el.parentNode);
1096+
var styleLocation = this.opts.styleInHead ? undefined : this.el.parentNode
1097+
// if styleInHead === false insert style to parent to support WebComponent
1098+
this._styles = Utils.createStylesheet(this._stylesId, styleLocation);
10971099
if (this._styles !== null) {
10981100
this._styles._max = 0;
10991101
}

0 commit comments

Comments
 (0)