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

Skip to content

Commit 60d7806

Browse files
committed
web 3.1.5
1 parent 18ccae5 commit 60d7806

14 files changed

+283
-101
lines changed

demo/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ <h1>Demos</h1>
1515
<li><a href="nested.html">Nested grids</a></li>
1616
<li><a href="responsive.html">Responsive</a></li>
1717
<li><a href="react.html">ReactJS</a></li>
18+
<li><a href="react-hooks.html">ReactJS (Hooks)</a></li>
1819
<li><a href="right-to-left(rtl).html">Right-To-Left (RTL)</a></li>
1920
<li><a href="serialization.html">Serialization</a></li>
2021
<li><a href="static.html">Static</a></li>
2122
<li><a href="two.html">Two grids</a></li>
22-
<li><a href="vue2js.html">Vue2.js</a></li>
2323
<li><a href="vue3js.html">Vue3.js</a></li>
24+
<li><a href="vue2js.html">Vue2.js</a></li>
25+
<li><a href="web-comp.html">Web Component</a></li>
2426
</ul>
2527
</body>
2628
</html>

demo/nested.html

Lines changed: 25 additions & 43 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="../node_modules/gridstack/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,60 +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 so this uses JQ version for now, otherwise recommend you use h5 version.</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>
27-
28-
<div class="grid-stack top">
29-
<div class="grid-stack-item" gs-x="0" gs-y="0" gs-w="1">
30-
<div class="grid-stack-item-content">regular item</div>
31-
</div>
32-
<div class="grid-stack-item" gs-x="1" gs-y="0" gs-w="4" gs-h="4">
33-
<div class="grid-stack-item-content">
34-
nested 1 - can drag items out
35-
<div class="grid-stack nested1">
36-
<div class="grid-stack-item sub" gs-x="0" gs-y="0" gs-w="3"><div class="grid-stack-item-content">1</div></div>
37-
<div class="grid-stack-item sub" gs-x="3" gs-y="0" gs-w="3"><div class="grid-stack-item-content">2</div></div>
38-
<div class="grid-stack-item sub" gs-x="6" gs-y="0" gs-w="3"><div class="grid-stack-item-content">3</div></div>
39-
<div class="grid-stack-item sub" gs-x="9" gs-y="0" gs-w="3"><div class="grid-stack-item-content">4</div></div>
40-
<div class="grid-stack-item sub" gs-x="0" gs-y="1" gs-w="3"><div class="grid-stack-item-content">5</div></div>
41-
<div class="grid-stack-item sub" gs-x="3" gs-y="1" gs-w="3"><div class="grid-stack-item-content">6</div></div>
42-
</div>
43-
</div>
44-
</div>
45-
<div class="grid-stack-item" gs-x="5" gs-y="0" gs-w="3" gs-h="4">
46-
<div class="grid-stack-item-content">
47-
nested 2 - constrained to parent (default)
48-
<div class="grid-stack nested2">
49-
<div class="grid-stack-item sub" gs-x="0" gs-y="0" gs-w="3"><div class="grid-stack-item-content">7</div></div>
50-
<div class="grid-stack-item sub" gs-x="0" gs-y="3" gs-w="3"><div class="grid-stack-item-content">8</div></div>
51-
</div>
52-
</div>
53-
</div>
27+
<!-- grid will be added here -->
5428
</div>
5529

5630
<script type="text/javascript">
57-
let nestOptions = {
31+
let sub1 = [ {w:3}, {w:3}, {w:3}, {w:3}, {w:3}, {w:3}];
32+
let sub2 = [ {w:3}, {x:0, y:1, w:3}];
33+
let count = 0;
34+
[...sub1, ...sub2].forEach(d => d.content = String(count++));
35+
let subOptions = {
36+
itemClass: 'sub', // style sub items differently and use to prevent dragging in/out
5837
acceptWidgets: '.grid-stack-item.sub', // only pink sub items can be inserted, otherwise grid-items causes all sort of issues
59-
dragOut: true, // let us drag them out!
6038
disableOneColumnMode: true, // nested are small, but still want N columns
6139
margin: 1
6240
};
63-
let grid0 = GridStack.init({cellHeight: 70}, '.grid-stack.top');
64-
let grid1 = GridStack.init(nestOptions, '.grid-stack.nested1');
65-
nestOptions.dragOut = false;
66-
let grid2 = GridStack.init(nestOptions, '.grid-stack.nested2');
41+
let layout = {cellHeight: 70, children: [
42+
{w:1, content: 'regular item'},
43+
{x:1, w:4, h:4, content: 'nested 1 - can drag items out', subGrid: {children: sub1, dragOut: true, class: 'nested1', ...subOptions}},
44+
{x:5, w:4, h:4, content: 'nested 2 - constrained to parent (default)', subGrid: {children: sub2, class: 'nested2', ...subOptions}},
45+
]};
46+
47+
// create and load it all from JSON above
48+
GridStack.addGrid(document.querySelector('.container-fluid'), layout);
6749

68-
let count = 9;
69-
addNewWidget = function(grid) {
50+
addNewWidget = function(selector) {
51+
grid = document.querySelector(selector).gridstack;
7052
let node = {
7153
x: Math.round(12 * Math.random()),
7254
y: Math.round(5 * Math.random()),
7355
w: Math.round(1 + 3 * Math.random()),
74-
h: Math.round(1 + 3 * Math.random())
56+
h: Math.round(1 + 3 * Math.random()),
57+
content: count++
7558
};
76-
// Note: we have additional style .sub here so add the HTML passed directly...
77-
grid.addWidget('<div class="grid-stack-item sub"><div class="grid-stack-item-content">' + count++ + '</div></div>', node);
59+
grid.addWidget(node);
7860
return false;
7961
};
8062
</script>

demo/react-hooks.html

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="UTF-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Gridstack.js React integration example</title>
8+
<link rel="stylesheet" href="demo.css" />
9+
<script src="../node_modules/gridstack/dist/gridstack-h5.js"></script>
10+
11+
<!-- Scripts to use react inside html -->
12+
<script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
13+
<script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
14+
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
15+
</head>
16+
17+
<body>
18+
<div>
19+
<h1>Using GridStack.js with React hooks</h1>
20+
<p>
21+
As with any virtualDOM-based framework, you need to check if Reacthas rendered the DOM (or any updates to it)
22+
<strong>before</strong> you initialize GridStack or call its methods. This example shows how to make rendered
23+
components widgets:
24+
</p>
25+
<ol>
26+
<li>Render items, each with a reference</li>
27+
<li>Convert each rendered item to a widget using the reference and the <a
28+
href="https://github.com/gridstack/gridstack.js/tree/develop/doc#makewidgetel">
29+
makeWidget</a> function</li>
30+
</ol>
31+
</div>
32+
<div>
33+
<h2>Controlled stack</h2>
34+
<div id="controlled-stack"></div>
35+
</div>
36+
<div>
37+
<h2>Uncontrolled stack</h2>
38+
<div id="uncontrolled-stack"></div>
39+
</div>
40+
</body>
41+
42+
<script type="text/babel">
43+
const { useState, useEffect, createRef, useRef } = React
44+
45+
const Item = ({ id }) => <div>I am item: {id}</div>
46+
47+
//
48+
// Controlled example
49+
//
50+
51+
const ControlledStack = ({ items, addItem }) => {
52+
const refs = useRef({})
53+
const gridRef = useRef()
54+
55+
if (Object.keys(refs.current).length !== items.length) {
56+
items.forEach(({ id }) => {
57+
refs.current[id] = refs.current[id] || createRef()
58+
})
59+
}
60+
61+
useEffect(() => {
62+
gridRef.current =
63+
gridRef.current ||
64+
GridStack.init(
65+
{
66+
float: true,
67+
},
68+
'.controlled'
69+
)
70+
const grid = gridRef.current
71+
grid.batchUpdate()
72+
grid.removeAll(false)
73+
items.forEach(({ id }) => grid.makeWidget(refs.current[id].current))
74+
grid.commit()
75+
}, [items])
76+
77+
return (
78+
<div>
79+
<button onClick={addItem}>Add new widget</button>
80+
<div className={`grid-stack controlled`}>
81+
{items.map((item, i) => {
82+
return (
83+
<div ref={refs.current[item.id]} key={item.id} className={'grid-stack-item'}>
84+
<div className="grid-stack-item-content">
85+
<Item {...item} />
86+
</div>
87+
</div>
88+
)
89+
})}
90+
</div>
91+
</div>
92+
)
93+
}
94+
95+
const ControlledExample = () => {
96+
const [items, setItems] = useState([{ id: 'item-1' }, { id: 'item-2' }])
97+
98+
return (
99+
<ControlledStack
100+
items={items}
101+
addItem={() => setItems([...items, { id: `item-${items.length + 1}` }])}
102+
/>
103+
)
104+
}
105+
106+
//
107+
// Uncontrolled example
108+
//
109+
110+
const UncontrolledExample = () => {
111+
const gridRef = useRef()
112+
113+
const [state, setState] = useState({
114+
count: 0,
115+
info: '',
116+
items: [
117+
{ x: 2, y: 1, h: 2 },
118+
{ x: 2, y: 4, w: 3 },
119+
{ x: 4, y: 2 },
120+
{ x: 3, y: 1, h: 2 },
121+
{ x: 0, y: 6, w: 2, h: 2 },
122+
],
123+
})
124+
125+
useEffect(() => {
126+
gridRef.current =
127+
gridRef.current ||
128+
GridStack.init(
129+
{
130+
float: true,
131+
cellHeight: '70px',
132+
minRow: 1,
133+
},
134+
'.uncontrolled'
135+
)
136+
137+
const grid = gridRef.current
138+
139+
grid.on('dragstop', (event, element) => {
140+
const node = element.gridstackNode
141+
setState(prevState => ({
142+
...prevState,
143+
info: `you just dragged node #${node.id} to ${node.x},${node.y} – good job!`,
144+
}))
145+
146+
let timerId
147+
window.clearTimeout(timerId)
148+
timerId = window.setTimeout(() => {
149+
setState(prevState => ({
150+
...prevState,
151+
info: '',
152+
}))
153+
}, 2000)
154+
})
155+
}, [])
156+
157+
return (
158+
<div>
159+
<button
160+
onClick={() => {
161+
const grid = gridRef.current
162+
const node = state.items[state.count] || {
163+
x: Math.round(12 * Math.random()),
164+
y: Math.round(5 * Math.random()),
165+
w: Math.round(1 + 3 * Math.random()),
166+
h: Math.round(1 + 3 * Math.random()),
167+
}
168+
node.id = node.content = String(state.count)
169+
setState(prevState => ({
170+
...prevState,
171+
count: prevState.count + 1,
172+
}))
173+
grid.addWidget(node)
174+
}}
175+
>
176+
Add Widget
177+
</button>
178+
<div>{JSON.stringify(state)}</div>
179+
<section class="grid-stack uncontrolled"></section>
180+
</div>
181+
)
182+
}
183+
184+
ReactDOM.render(<ControlledExample />, document.getElementById('controlled-stack'))
185+
ReactDOM.render(<UncontrolledExample />, document.getElementById('uncontrolled-stack'))
186+
187+
</script>
188+
189+
</html>

demo/serialization.html

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
<h1>Serialization demo</h1>
1515
<a onClick="saveGrid()" class="btn btn-primary" href="#">Save</a>
1616
<a onClick="loadGrid()" class="btn btn-primary" href="#">Load</a>
17-
<a onClick="saveGridManual()" class="btn btn-primary" href="#">Save Manual</a>
18-
<a onClick="loadGridManual()" class="btn btn-primary" href="#">Load Manual</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>
1919
<a onClick="clearGrid()" class="btn btn-primary" href="#">Clear</a>
2020
<br/><br/>
21-
<div class="grid-stack"></div>
21+
<div id="gridCont"><div class="grid-stack"></div></div>
2222
<hr/>
2323
<textarea id="saved-data" cols="100" rows="20" readonly="readonly"></textarea>
2424
</div>
@@ -41,54 +41,33 @@ <h1>Serialization demo</h1>
4141
];
4242
serializedData.forEach((n, i) =>
4343
n.content = `<button onClick="grid.removeWidget(this.parentNode.parentNode)">X</button><br> ${i}<br> ${n.content ? n.content : ''}`);
44+
let serializedFull;
4445

45-
// NEW 2.x method
46+
// 2.x method - just saving list of widgets with content (default)
4647
loadGrid = function() {
47-
grid.load(serializedData, true);
48+
grid.load(serializedData, true); // update things
4849
}
4950

50-
// NEW 2.x method
51+
// 2.x method
5152
saveGrid = function() {
53+
delete serializedFull;
5254
serializedData = grid.save();
5355
document.querySelector('#saved-data').value = JSON.stringify(serializedData, null, ' ');
5456
}
5557

56-
// old (pre 2.x) way to manually load a grid
57-
loadGridManual = function() {
58-
let items = GridStack.Utils.sort(serializedData);
59-
grid.batchUpdate();
60-
61-
if (grid.engine.nodes.length === 0) {
62-
// load from empty
63-
items.forEach(function (item) {
64-
grid.addWidget('<div class="grid-stack-item"><div class="grid-stack-item-content">' + item.id + '</div></div>', item);
65-
});
66-
} else {
67-
// else update existing nodes (instead of calling grid.removeAll())
68-
grid.engine.nodes.forEach(function (node) {
69-
let item = items.find(function(e) { return e.id === node.id});
70-
grid.move(node.el, item.x, item.y, item.w, item.h);
71-
});
72-
}
73-
74-
grid.commit();
75-
};
58+
// 3.1 full method saving the grid options + children (which is recursive for nested grids)
59+
saveFullGrid = function() {
60+
serializedFull = grid.save(true, true);
61+
serializedData = serializedFull.children;
62+
document.querySelector('#saved-data').value = JSON.stringify(serializedFull, null, ' ');
63+
}
7664

77-
// old (pre 2.x) way to manually save a grid
78-
saveGridManual = function() {
79-
serializedData = [];
80-
grid.engine.nodes.forEach(function(node) {
81-
serializedData.push({
82-
x: node.x,
83-
y: node.y,
84-
w: node.w,
85-
h: node.h,
86-
id: node.id,
87-
custom: 'save anything here'
88-
});
89-
});
90-
document.querySelector('#saved-data').value = JSON.stringify(serializedData, null, ' ');
91-
};
65+
// 3.1 full method to reload from scratch - delete the grid and add it back from JSON
66+
loadFullGrid = function() {
67+
if (!serializedFull) return;
68+
grid.destroy(true); // nuke everything
69+
grid = GridStack.addGrid(document.querySelector('#gridCont'), serializedFull)
70+
}
9271

9372
clearGrid = function() {
9473
grid.removeAll();

0 commit comments

Comments
 (0)