-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
sizeToContent.html
136 lines (130 loc) · 4.97 KB
/
sizeToContent.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>sizeToContent demo</title>
<link rel="stylesheet" href="demo.css"/>
<link rel="stylesheet" href="../dist/gridstack-extra.css"/>
<script src="../dist/gridstack-all.js"></script>
<style type="text/css">
.grid-stack-item-content {
text-align: unset;
}
.sidebar.inline {
width: fit-content;
height: fit-content;
display: inline-block;
padding: 0;
}
</style>
</head>
<body>
<div class="container">
<h1>sizeToContent options demo</h1>
<p>New 9.x feature that size the items to fit their content height as to not have scroll bars
<br>case C: `sizeToContent:false` to turn off.
<br>case E: has soft maxsize `sizeToContent:3`, shrinking to smaller content as needed
<br>Defaulting to different initial size (see code) to show grow/shrink behavior</p>
<div>
<a onClick="clearGrid()" class="btn btn-primary" href="#">clear</a>
<a onClick="load()" class="btn btn-primary" href="#">load</a>
column:
<a onClick="column(8)" class="btn btn-primary" href="#">8</a>
<a onClick="column(12)" class="btn btn-primary" href="#">12</a>
cellHeight:
<a onClick="cellHeight(25)" class="btn btn-primary" href="#">25</a>
<a onClick="cellHeight('3rem')" class="btn btn-primary" href="#">3rem</a>
<a onClick="cellHeight(50)" class="btn btn-primary" href="#">50</a>
<a onClick="cellHeight(75)" class="btn btn-primary" href="#">75</a>
Widget:
<a onClick="addWidget()" class="btn btn-primary" href="#">Add</a>
<a onClick="makeWidget()" class="btn btn-primary" href="#">Make w:2</a>
<div class="sidebar inline">
<div class="grid-stack-item" gs-w="2" gs-h="3">
<div class="grid-stack-item-content"><div>Insert me 2x3</div></div>
</div>
</div>
</div>
<br>
<div id="grid1"></div>
<p>from DOM test:</p>
<div id="grid2">
<div class="grid-stack-item" gs-x="11" gs-y="0" gs-h="4">
<div class="grid-stack-item-content">
<div>DOM: h:4 sized down</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
// NOTE: REAL apps would sanitize-html or DOMPurify before blinding setting innerHTML. see #2736
GridStack.renderCB = function(el, w) {
el.innerHTML = w.content;
};
let text ='some very large content that will normally not fit in the window.'
text = text + text;
let count = 0;
let items = [
// {x:0, y:0, w:2, h:3, sizeToContent: false, content: `<div>A no h: ${text}</div>`},
{x:0, y:0, w:2, content: `<div>A no h: ${text}</div>`},
{x:2, y:0, w:1, h:2, content: '<div>B: shrink h=2</div>'}, // make taller than needed upfront
{x:3, y:0, w:2, sizeToContent: false, content: `<div>C: WILL SCROLL. ${text}</div>`}, // prevent this from fitting testing
{x:0, y:1, w:3, content: `<div>D no h: ${text} ${text}</div>`},
{x:3, y:1, w:2, sizeToContent:3, content: `<div>E sizeToContent=3 <button onClick="more()">more</button><button onClick="less()">less</button><div id="dynContent">${text} ${text} ${text}</div></div>`},
];
items.forEach(n => n.id = String(count++));
let opts = {
margin: 5,
cellHeight: '3rem', // = 48px
sizeToContent: true, // default to make them all fit
resizable: { handles: 'all'}, // do all sides for testing
acceptWidgets: true,
// cellHeightThrottle: 100, // ms before sizeToContent happens
// children: items, // test loading first
}
let grid = GridStack.init(opts, '#grid1');
grid.load(items); // test loading after
GridStack.init({...opts, children:undefined}, '#grid2');
GridStack.setupDragIn('.sidebar>.grid-stack-item');
function clearGrid() {
grid.removeAll();
}
function load() {
grid.load(items);
}
function column(n) {
grid.column(n, 'none');
}
function cellHeight(n) {
grid.cellHeight(n);
}
function addWidget() {
grid.addWidget({content: `<div>New: ${text}</div>`});
}
function makeWidget() {
let el = GridStack.Utils.createWidgetDivs(undefined, {content: `<div>New Make: ${text}</div>`}, grid.el)
grid.makeWidget(el, {w:2});
}
function more() {
let cont = document.getElementById('dynContent');
if (!cont) return;
cont.innerHTML += cont.innerHTML;
let el = cont.parentElement.parentElement.parentElement;
grid.resizeToContent(el)
}
function less() {
let cont = document.getElementById('dynContent');
if (!cont) return;
let content = cont.innerHTML;
cont.innerHTML = content.substring(0, content.length/2);
let el = cont.parentElement.parentElement.parentElement;
grid.resizeToContent(el);
}
// TEST
// grid.update(grid.engine.nodes[0].el, {x:7});
// load();
</script>
</body>
</html>