Skip to content

Commit 0c06ccb

Browse files
committed
more addGrid() fixes
* continue #1498 * addGrid() is now static as it can replace inti() method actually * fixed nested.html demo to use it instead of load() and fixed button actions. * noticed dragOut:false is no longer working (even in JQ case). Will post bug.
1 parent 2612ee2 commit 0c06ccb

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

demo/nested.html

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
<meta charset="utf-8">
55
<meta http-equiv="X-UA-Compatible" content="IE=edge">
66
<meta name="viewport" content="width=device-width, initial-scale=1">
7-
<title>Nested grids demo</title>
8-
7+
<title>Nested grids demo (ES6)</title>
98
<link rel="stylesheet" href="demo.css"/>
109
<script src="../dist/gridstack-h5.js"></script>
1110
<style type="text/css">
1211
.grid-stack .grid-stack {
13-
/*margin: 0 -10px;*/
1412
background: rgba(255, 255, 255, 0.3);
1513
}
1614
.grid-stack .grid-stack .grid-stack-item-content {
@@ -21,42 +19,44 @@
2119
<body>
2220
<div class="container-fluid">
2321
<h1>Nested grids demo</h1>
24-
<a class="btn btn-primary" onClick="addNewWidget(grid1)" href="#">Add Widget Grid1</a>
25-
<a class="btn btn-primary" onClick="addNewWidget(grid2)" href="#">Add Widget Grid2</a>
22+
<p>This example uses new v3.1 API to load the entire nested grid from JSON, and shows dragging between nested grid items (pink) vs dragging higher grid items (green)</p>
23+
<!-- <p>Note: initial v3.0.0 HTML5 release doesn't support 'dragOut:false' constrain (always true) so this uses JQ version for now.</p> -->
24+
<a class="btn btn-primary" onClick="addNewWidget('.nested1')" href="#">Add Widget Grid1</a>
25+
<a class="btn btn-primary" onClick="addNewWidget('.nested2')" href="#">Add Widget Grid2</a>
2626
<br><br>
2727

2828
<div class="grid-stack"></div>
2929
</div>
3030

3131
<script type="text/javascript">
32-
let sub1 = [ {w: 3}, {w: 3}, {w: 3}, {w: 3}, {w: 3}, {w: 3}];
33-
let sub2 = [ {w: 3}, {x:0, y: 1, w: 3}];
32+
let sub1 = [ {w:3}, {w:3}, {w:3}, {w:3}, {w:3}, {w:3}];
33+
let sub2 = [ {w:3}, {x:0, y:1, w:3}];
3434
let count = 0;
3535
[...sub1, ...sub2].forEach(d => d.content = String(count++));
3636
let subOptions = {
37+
itemClass: 'sub', // style sub items differently and use to prevent dragging in/out
3738
acceptWidgets: '.grid-stack-item.sub', // only pink sub items can be inserted, otherwise grid-items causes all sort of issues
3839
disableOneColumnMode: true, // nested are small, but still want N columns
39-
itemClass: 'sub',
4040
margin: 1
4141
};
42-
let items = [
42+
let layout = {cellHeight: 70, children: [
4343
{w:1, content: 'regular item'},
4444
{x:1, w:4, h:4, content: 'nested 1 - can drag items out', subGrid: {children: sub1, dragOut: true, class: 'nested1', ...subOptions}},
45-
{x:5, w:4, h:4, content: 'nested 2 - constrained to parent (default)', subGrid: {children: sub2, class: 'nested2', ...subOptions}},
46-
];
45+
{x:5, w:4, h:4, content: 'nested 2 - constrained to parent (default)', subGrid: {children: sub2, dragOut: false, class: 'nested2', ...subOptions}},
46+
]};
4747

48-
let grid = GridStack.init({cellHeight: 70});
49-
grid.load(items);
48+
GridStack.addGrid(document.querySelector('.container-fluid'), layout);
5049

51-
addNewWidget = function(grid) {
50+
addNewWidget = function(selector) {
51+
grid = document.querySelector(selector).gridstack;
5252
let node = {
5353
x: Math.round(12 * Math.random()),
5454
y: Math.round(5 * Math.random()),
5555
w: Math.round(1 + 3 * Math.random()),
56-
h: Math.round(1 + 3 * Math.random())
56+
h: Math.round(1 + 3 * Math.random()),
57+
content: count++
5758
};
58-
// Note: we have additional style .sub here so add the HTML passed directly...
59-
grid.addWidget('<div class="grid-stack-item sub"><div class="grid-stack-item-content">' + count++ + '</div></div>', node);
59+
grid.addWidget(node);
6060
return false;
6161
};
6262
</script>

src/gridstack.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class GridStack {
5555
/**
5656
* initializing the HTML element, or selector string, into a grid will return the grid. Calling it again will
5757
* simply return the existing instance (ignore any passed options). There is also an initAll() version that support
58-
* multiple grids initialization at once.
58+
* multiple grids initialization at once. Or you can use addGrid() to create the entire grid from JSON.
5959
* @param options grid options (optional)
6060
* @param elOrString element or CSS selector (first one used) to convert to a grid (default to '.grid-stack' class selector)
6161
*
@@ -106,6 +106,30 @@ export class GridStack {
106106
return grids;
107107
}
108108

109+
/**
110+
* call to create a grid with the given options, including loading any children from JSON structure. This will call GridStack.init(), then
111+
* grid.load() on any passed children (recursively). Great alternative to calling init() if you want entire grid to come from
112+
* JSON serialized data, including options.
113+
* @param parent HTML element parent to the grid
114+
* @param opt grids options used to initialize the grid, and list of children
115+
*/
116+
public static addGrid(parent: HTMLElement, opt: GridStackOptions = {}): GridStack {
117+
if (!parent) { return null; }
118+
119+
// create the grid element
120+
let doc = document.implementation.createHTMLDocument();
121+
doc.body.innerHTML = `<div class="grid-stack ${opt.class || ''}"></div>`;
122+
let el = doc.body.children[0] as HTMLElement;
123+
parent.append(el);
124+
125+
// create grid class and load any children
126+
let grid = GridStack.init(opt, el);
127+
if (opt.children) {
128+
grid.load(opt.children);
129+
}
130+
return grid;
131+
}
132+
109133
/** scoping so users can call GridStack.Utils.sort() for example */
110134
public static Utils = Utils;
111135

@@ -372,28 +396,6 @@ export class GridStack {
372396
return list;
373397
}
374398

375-
/**
376-
* call to create a grid with the given options, including loading any children
377-
* @param parent HTML element parent to the grid
378-
* @param opt grids options used to initialize the grid, and list of children
379-
*/
380-
public addGrid(parent: HTMLElement, opt: GridStackOptions = {}): GridStack {
381-
if (!parent) { return null; }
382-
383-
// create the grid element
384-
let doc = document.implementation.createHTMLDocument();
385-
doc.body.innerHTML = `<div class="grid-stack ${opt.class || ''}"></div>`;
386-
let el = doc.body.children[0] as HTMLElement;
387-
parent.append(el);
388-
389-
// create grid class and load any children
390-
let grid = GridStack.init(opt, el);
391-
if (opt.children) {
392-
grid.load(opt.children);
393-
}
394-
return grid;
395-
}
396-
397399
/**
398400
* load the widgets from a list. This will call update() on each (matching by id) or add/remove widgets that are not there.
399401
*
@@ -452,7 +454,7 @@ export class GridStack {
452454
}
453455
if (w.subGrid) { // see if there is a sub-grid to create too
454456
let content = w.el.querySelector('.grid-stack-item-content') as HTMLElement;
455-
this.addGrid(content, w.subGrid);
457+
GridStack.addGrid(content, w.subGrid);
456458
}
457459
}
458460
});

0 commit comments

Comments
 (0)