forked from dat-ecosystem-archive/dat-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable-row.js
416 lines (377 loc) · 10.3 KB
/
table-row.js
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
var Nanocomponent = require('nanocomponent')
var encoding = require('dat-encoding')
var html = require('choo/html')
var css = require('sheetify')
var TitleField = require('./table-title-field')
var button = require('./button')
var status = require('./status')
var icon = require('./icon')
var { datSize, datPeers, datState } = require('../lib/datInfo')
var cellStyles = css`
:host {
transition: background-color .025s ease-out;
&:hover, &:focus {
background-color: var(--color-neutral-04);
cursor: pointer;
}
.cell-1 {
width: 4rem;
}
.cell-2 {
width: 14rem;
max-width: 12rem;
@media (min-width: 768px) {
max-width: 20rem;
}
@media (min-width: 1280px) {
max-width: 24rem;
}
}
.cell-3 {
width: 15rem;
}
.cell-4 {
width: 4.5rem;
white-space: nowrap;
}
.cell-5 {
width: 4.5rem;
white-space: nowrap;
}
.cell-6 {
width: 10.25rem;
}
.cell-truncate {
width: 100%;
}
}
`
var iconStyles = css`
:host {
.row-action {
height: 1.5rem;
display: inline-block;
color: var(--color-neutral-20);
svg {
vertical-align: middle;
width: 1.1em;
max-height: 1.6em;
@media (min-width: 960px) {
width: 1.4em;
}
}
&:hover,
&:focus {
outline: none;
color: var(--color-neutral-50);
}
&:first-child {
padding-left: 0;
}
&:last-child {
padding-right: 0;
}
}
.icon-network {
display: inline-block;
color: var(--color-neutral-20);
vertical-align: sub;
width: 1em;
svg polygon {
fill: inherit;
}
}
}
`
var networkStyles = css`
:host {
vertical-align: top;
svg {
height: 1.5rem;
display: inline-block;
color: var(--color-neutral-20);
vertical-align: top;
width: 1.1em;
max-height: 1.6em;
}
.network-peers-many {
--polygon-1-color: var(--color-green);
--polygon-2-color: var(--color-green);
--polygon-3-color: var(--color-green);
}
.network-peers-1 {
--polygon-1-color: var(--color-yellow);
--polygon-2-color: var(--color-yellow);
}
.network-peers-0 {
--polygon-1-color: var(--color-red);
}
}
`
module.exports = Row
function Row () {
if (!(this instanceof Row)) return new Row()
Nanocomponent.call(this)
this.partials = {
hexContent: HexContent(),
finderButton: FinderButton(),
linkButton: LinkButton(),
deleteButton: DeleteButton(),
titleField: TitleField(),
networkIcon: NetworkIcon()
}
}
Row.prototype = Object.create(Nanocomponent.prototype)
Row.prototype.createElement = function (props) {
var { dat, emit, highlight } = props
var partials = this.partials
if (dat instanceof Error) return errorRow(dat, emit, partials.deleteButton)
var key = encoding.encode(dat.key)
var styles = cellStyles
if (highlight) styles += ' fade-highlight'
function onclick () {
if (!partials.titleField.state.isEditing) {
emit('dats:inspect', dat)
}
}
return html`
<tr id=${key} class=${styles} onclick=${onclick}>
<td class="cell-1">
<div class="w2 center">
${partials.hexContent.render(props)}
</div>
</td>
<td class="cell-2">
<div class="cell-truncate">
${partials.titleField.render(props)}
<p class="f7 f6-l color-neutral-60 truncate">
<span class="author">${dat.metadata.author || 'Anonymous'} • </span>
<span class="title">
${dat.writable ? 'Read & Write' : 'Read-only'}
</span>
</p>
</div>
</td>
<td class="cell-3">
${status(dat)}
</td>
<td class="f6 f5-l cell-4 size">
${datSize(dat)}
</td>
<td class="cell-5 ${networkStyles}">
${partials.networkIcon.render(props)}
<span class="network v-top f6 f5-l ml1">${datPeers(dat)}</span>
</td>
<td class="cell-6">
<div class="flex justify-end ${iconStyles}">
${partials.finderButton.render(props)}
${partials.linkButton.render(props)}
${partials.deleteButton.render(props)}
</div>
</td>
</tr>
`
}
Row.prototype.update = function (props) {
return Object.values(this.partials).find(component => component.update(props, true)) !== undefined
}
function FinderButton () {
if (!(this instanceof FinderButton)) return new FinderButton()
Nanocomponent.call(this)
}
FinderButton.prototype = Object.create(Nanocomponent.prototype)
FinderButton.prototype.createElement = function (props) {
var { dat, emit } = props
return button.icon('Open in Finder', {
icon: icon('open-in-finder'),
class: 'row-action',
onclick: function (e) {
e.preventDefault()
e.stopPropagation()
emit('dats:open', dat)
}
})
}
FinderButton.prototype.update = function () {
return false
}
function LinkButton () {
if (!(this instanceof LinkButton)) return new LinkButton()
Nanocomponent.call(this)
}
LinkButton.prototype = Object.create(Nanocomponent.prototype)
LinkButton.prototype.createElement = function (props) {
var { dat, emit } = props
return button.icon('Share Dat', {
icon: icon('link'),
class: 'row-action',
onclick: function (e) {
e.preventDefault()
e.stopPropagation()
emit('dats:share', dat)
}
})
}
LinkButton.prototype.update = function () {
return false
}
function DeleteButton () {
if (!(this instanceof DeleteButton)) return new DeleteButton()
Nanocomponent.call(this)
}
DeleteButton.prototype = Object.create(Nanocomponent.prototype)
DeleteButton.prototype.createElement = function (props) {
var { dat, emit } = props
return button.icon('Remove Dat', {
icon: icon('delete'),
class: 'row-action delete',
onclick: function (e) {
e.preventDefault()
e.stopPropagation()
emit('dats:remove', { key: dat.key || dat.data.key })
}
})
}
DeleteButton.prototype.update = function () {
return false
}
function NetworkIcon () {
if (!(this instanceof NetworkIcon)) return new NetworkIcon()
Nanocomponent.call(this)
this.state = {
peerCount: 0
}
}
NetworkIcon.prototype = Object.create(Nanocomponent.prototype)
NetworkIcon.prototype.createElement = function (props) {
var { dat } = props
var peerCount = this.state.peerCount = dat.network
? dat.network.connected
: 0
var iconClass = peerCount === 0
? 'network-peers-0'
: peerCount === 1
? 'network-peers-1'
: 'network-peers-many'
return html`
<div>${icon('network', { class: iconClass })}</div>
`
}
NetworkIcon.prototype.update = function ({ dat, emit }) {
var newPeerCount = dat.network ? dat.network.connected : 0
return this.state.peerCount !== newPeerCount
}
// create a new hexcontent icon
function HexContent () {
if (!(this instanceof HexContent)) return new HexContent()
Nanocomponent.call(this)
this.state = {
peerCount: 0
}
}
HexContent.prototype = Object.create(Nanocomponent.prototype)
HexContent.prototype.createElement = function (props) {
var state = this.state.state = datState(props.dat)
var { emit, dat } = props
var self = this
function onmousemove (ev) {
if (!self.element.contains(ev.target)) {
self.state.hover = false
self.state.update = true
self.state.listening = false
document.body.removeEventListener('mousemove', onmousemove)
emit('render')
}
}
if (this.state.hover && !this.state.listening) {
document.body.addEventListener('mousemove', onmousemove)
this.state.listening = true
}
if (this.state.hover) {
return button.icon('pause', {
icon: icon('hexagon-pause', {class: 'w2'}),
class: 'color-neutral-40 ph0',
onclick: togglePause
})
} else if (state === 'loading') {
return button.icon('loading', {
icon: icon('hexagon-down', {class: 'w2'}),
class: 'color-blue hover-color-blue-hover ph0',
onclick: togglePause
})
} else if (state === 'paused') {
return button.icon('paused', {
icon: icon('hexagon-resume', {class: 'w2'}),
class: 'color-neutral-30 hover-color-neutral-40 ph0',
onclick: togglePause
})
} else if (state === 'complete') {
return button.icon('complete', {
icon: icon('hexagon-up', {class: 'w2'}),
class: 'color-green hover-color-green-hover ph0',
onclick: togglePause,
onmouseover: ev => {
this.state.hover = true
this.state.update = true
emit('render')
}
})
} else {
return button.icon('stale', {
icon: icon('hexagon-x', {class: 'w2'}),
class: 'color-neutral-30 hover-color-neutral-40 ph0',
onclick: togglePause
})
}
function togglePause (e) {
e.preventDefault()
e.stopPropagation()
emit('dats:toggle-pause', dat)
}
}
HexContent.prototype.update = function ({ dat, emit }, external) {
if (this.state.update) {
if (!external) {
// `.update` is usually called (and part of) the `.render` method
// meaning that setting "state.update" once will result in exactly
// one rendering. However, since the table-row is a complex component
// and the table row uses `.update` to see if any of the child
// components would need an update. If the call comes from
// the parent (or externally) we do not reset the update property
// to make sure that `.state.update` stays true until actually
// rendering the component
this.state.update = false
}
return true
}
return datState(dat) !== this.state.state
}
function errorRow (err, emit, deleteButton) {
var errorHexIcon = icon('hexagon-x', {
class: 'w2 color-red'
})
return html`
<tr class="bg-yellow-disabled">
<td class="cell-1">
<div class="w2 center">
${errorHexIcon}
</div>
</td>
<td class="cell-2" colspan="4">
<div class="cell-truncate color-red">
<h2 class="f6 f5-l normal">
Error
</h2>
<p class="f7 f6-l">
Could not share ${err.data.dir}
</p>
</div>
</td>
<td class="cell-6">
<div class="flex justify-end ${iconStyles}">
${deleteButton.render({ dat: err, emit })}
</div>
</td>
</tr>
`
}