|
12 | 12 | <body>
|
13 | 13 | <div class="container-fluid">
|
14 | 14 | <h1>Serialization demo</h1>
|
15 |
| - <a onClick="saveGrid()" class="btn btn-primary" href="#">Save</a> |
16 |
| - <a onClick="loadGrid()" class="btn btn-primary" href="#">Load</a> |
17 |
| - <a onClick="saveFullGrid()" class="btn btn-primary" href="#">Save Full</a> |
18 |
| - <a onClick="loadFullGrid()" class="btn btn-primary" href="#">Load Full</a> |
19 |
| - <a onClick="clearGrid()" class="btn btn-primary" href="#">Clear</a> |
| 15 | + <a onclick="saveGrid()" class="btn btn-primary" href="#">Save</a> |
| 16 | + <a onclick="loadGrid()" class="btn btn-primary" href="#">Load</a> |
| 17 | + <a onclick="saveFullGrid()" class="btn btn-primary" href="#">Save Full</a> |
| 18 | + <a onclick="loadFullGrid()" class="btn btn-primary" href="#">Load Full</a> |
| 19 | + <a onclick="clearGrid()" class="btn btn-primary" href="#">Clear</a> |
20 | 20 | <br/><br/>
|
21 |
| - <div contentEditable="true">Editable Div</div> |
22 | 21 | <div id="gridCont"><div class="grid-stack"></div></div>
|
23 | 22 | <hr/>
|
24 |
| - <textarea id="saved-data" cols="100" rows="20" readonly="readonly"></textarea> |
| 23 | + <textarea id="saved-data" style="width: 100%" cols="100" rows="20" readonly="readonly"></textarea> |
25 | 24 | </div>
|
26 |
| - |
| 25 | + <script src="events.js"></script> |
27 | 26 | <script type="text/javascript">
|
28 |
| - let grid = GridStack.init({ |
29 |
| - minRow: 1, // don't let it collapse when empty |
30 |
| - cellHeight: '7rem', |
31 |
| - draggable: { cancel: '.no-drag'} // example of additional custom elements to skip drag on |
32 |
| - }); |
33 |
| - |
34 |
| - grid.on('added removed change', function(e, items) { |
35 |
| - if (!items) return; |
36 |
| - let str = ''; |
37 |
| - items.forEach(function(item) { str += ' (x,y)=' + item.x + ',' + item.y; }); |
38 |
| - console.log(e.type + ' ' + items.length + ' items:' + str ); |
39 |
| - }); |
40 |
| - |
41 | 27 | let serializedData = [
|
42 |
| - {x: 0, y: 0, w: 2, h: 2, id: '0'}, |
43 |
| - {x: 3, y: 1, h: 3, id: '1', |
44 |
| - content: "<button onclick=\"alert('clicked!')\">Press me</button><div>text area</div><div><textarea></textarea></div><div>Input Field</div><input type='text'><div contentEditable=\"true\">Editable Div</div><div class=\"no-drag\">no drag</div>"}, |
45 |
| - {x: 4, y: 1, id: '2'}, |
46 |
| - {x: 2, y: 3, w: 3, id: '3'}, |
47 |
| - {x: 1, y: 3, id: '4'} |
| 28 | + {x: 0, y: 0, w: 2, h: 2}, |
| 29 | + {x: 2, y: 1, w: 2, h: 3, |
| 30 | + content: `<button onclick=\"alert('clicked!')\">Press me</button><div>text area</div><div><textarea></textarea></div><div>Input Field</div><input type="text"><div contenteditable=\"true\">Editable Div</div><div class=\"no-drag\">no drag</div>`}, |
| 31 | + {x: 4, y: 1}, |
| 32 | + {x: 1, y: 3}, |
| 33 | + {x: 2, y: 3, w:3}, |
48 | 34 | ];
|
49 |
| - serializedData.forEach((n, i) => |
50 |
| - n.content = `<button onClick="removeWidget(this.parentElement.parentElement)">X</button><br> ${i}<br> ${n.content ? n.content : ''}`); |
| 35 | + serializedData.forEach((n, i) => { |
| 36 | + n.id = String(i); |
| 37 | + n.content = `<button onclick="removeWidget(this.parentElement.parentElement)">X</button><br> ${i}<br> ${n.content ? n.content : ''}`; |
| 38 | + }); |
51 | 39 | let serializedFull;
|
52 | 40 |
|
| 41 | + let grid = GridStack.init({ |
| 42 | + minRow: 1, // don't let it collapse when empty |
| 43 | + cellHeight: 80, |
| 44 | + float: true, |
| 45 | + draggable: { cancel: '.no-drag'} // example of additional custom elements to skip drag on |
| 46 | + }).load(serializedData); |
| 47 | + addEvents(grid); |
| 48 | + |
53 | 49 | // 2.x method - just saving list of widgets with content (default)
|
54 | 50 | function loadGrid() {
|
55 |
| - grid.load(serializedData, true); // update things |
| 51 | + grid.load(serializedData); |
56 | 52 | }
|
57 | 53 |
|
58 | 54 | // 2.x method
|
@@ -86,7 +82,7 @@ <h1>Serialization demo</h1>
|
86 | 82 | grid.removeWidget(el, false);
|
87 | 83 | }
|
88 | 84 |
|
89 |
| - loadGrid(); |
| 85 | + // setTimeout(() => loadGrid(), 1000); // TEST force a second load which should be no-op |
90 | 86 | </script>
|
91 | 87 | </body>
|
92 | 88 | </html>
|
0 commit comments