-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
264 lines (235 loc) · 7.93 KB
/
index.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Lit Grid Layout</title>
<!-- <script
src="https://unpkg.com/[email protected]/dist/lit-grid-layout.js?module"
crossorigin="anonymous"
type="module"
></script> -->
</head>
<body style="margin: 0;">
<my-app></my-app>
<script type="module">
import './dist/lit-grid-layout.js';
// import {html, LitElement} from 'https://cdn.skypack.dev/pin/[email protected]/lit-element.js'
import { html } from 'lit-html/lit-html.js';
import { LitElement } from 'lit-element/lit-element.js';
class MyItem extends LitElement {
static get properties () {
return {
activated: {
type: Boolean
},
title: {
type: String
}
};
}
constructor () {
super();
this.activated = false;
if (!this.title) this.title = '';
}
onClick () {
this.activated = !this.activated;
}
render () {
const itemBackgroundColor = this.activated
? 'lightgreen'
: '#ccc';
const itemStyle = `background-color: ${itemBackgroundColor};`;
return html`
<style>
.lit-grid-item {
background-color: #ccc;
border: 1px solid black;
height: 100%;
width: 100%;
font-size: 24px;
display: flex;
position: relative;
justify-content: center;
align-items: center;
}
.drag-handle {
position: absolute;
top: 0;
left: 0;
cursor: move;
line-height: 0;
}
</style>
<div
class="lit-grid-item"
style="${itemStyle}">
<span style="cursor: pointer;" @click="${this.onClick}">${this.title}</span>
<div class="drag-handle">
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-grid-dots" width="24" height="24" viewBox="0 0 24 24" stroke-width="1.5" stroke="#607D8B" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z"/>
<circle cx="5" cy="5" r="1" />
<circle cx="12" cy="5" r="1" />
<circle cx="19" cy="5" r="1" />
<circle cx="5" cy="12" r="1" />
<circle cx="12" cy="12" r="1" />
<circle cx="19" cy="12" r="1" />
<circle cx="5" cy="19" r="1" />
<circle cx="12" cy="19" r="1" />
<circle cx="19" cy="19" r="1" />
</svg>
</div>
</div>`;
}
}
customElements.define('my-item', MyItem);
function generateRandomLayout (n) {
if (n < 0) throw new Error('Number of layout items must be positive');
const layout = [];
for (let i = 0; i < n; i++) {
var y = Math.ceil(Math.random() * 4) + 1;
var x = Math.floor(Math.random() * 3) + 1;
layout.push({
posX: 3 * (i % 4),
posY: Math.floor(i / 6) * y,
width: 3,
height: y + 1,
key: i.toString(),
});
}
return layout;
}
const mediaQueryColumns = [12,9,6,2, 6, 9, 12];
class MyApp extends LitElement {
static get properties () {
return {
isSunnyMode: {
type: Boolean
},
index: {
type: Number
}
};
}
constructor () {
super();
this.isSunnyMode = false;
this.index = 0;
console.log("generate");
this.layout = generateRandomLayout(50);
}
onSunnyButtonClick () {
this.isSunnyMode = !this.isSunnyMode;
}
onNewLayoutClick () {
this.layout = generateRandomLayout(50);
}
onColummnClick() {
this.index +=1;
}
itemRenderer (itemKey) {
return html`
<my-item
.title=${itemKey}>
</my-item>
`;
}
onLayoutChanged (event) {
console.log(event);
// this.layout = event.detail.layout;
}
updated(props) {
console.log("props");
console.log(props);
console.log(this.layout);
}
render () {
console.log("render", [...this.layout]);
const buttonText = this.isSunnyMode ? 'Switch to regular mode' : 'Switch to sunny mode';
const layoutBackgroundColor = this.isSunnyMode ? '#ffe493' : '#eee';
const sortStyle = 'masonry';
const dragHandle = '.drag-handle';
return html`
<style>
.container {
margin: 5em auto auto auto;
max-width: 1000px;
}
.highlighted-green {
background-color: lightgreen;
}
.buttons-row {
display: flex;
flex-direction: row;
justify-content: center;
margin-top: 2.5em;
}
.layout-button {
border: solid darkgrey;
color: black;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
background-color: white;
margin-left: 1em;
margin-right: 1em;
}
</style>
<div class="container">
<div>
<p>
Click on the item number to activate or deactivate that item, it will turn <span class="highlighted-green">green</span> when activated.
</p>
<p>
You can press "Switch to sunny mode" to change then app state and force trigger a re-render. Notice that the internal state of items is preserved.
</p>
<p>
Also, the layout of the parent is reactive. Click "New layout" to change genereate a new layout. Notice again that active items remain active.
</p>
</div>
<div class="buttons-row">
<button
class="layout-button"
@click=${this.onSunnyButtonClick}>
${buttonText}
</button>
<button
class="layout-button"
@click=${this.onNewLayoutClick}>
New layout
</button>
<button
class="layout-button"
@click=${this.onColummnClick}>
+ Columns
</button>
</div>
</div>
<div
class="layout"
style="
background-color: ${layoutBackgroundColor};
box-sizing: border-box;
position: relative;
">
<lit-grid-layout
.layout=${this.layout}
.dragHandle=${dragHandle}
.sortStyle=${sortStyle}
.itemRenderer=${this.itemRenderer}
.columns=${mediaQueryColumns[this.index]}
@layout-changed=${this.onLayoutChanged}
@item-changed=${this.onLayoutChanged}>
</lit-grid-layout>
</div>`;
}
}
customElements.define('my-app', MyApp);
</script>
</body>
</html>