-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
181 lines (151 loc) · 3.84 KB
/
index.js
File metadata and controls
181 lines (151 loc) · 3.84 KB
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
var html = require('bel')
var css = require('sheetify')
var infiniteElements = require('infinite-elements')
module.exports = function createGrid (options) {
var prefix = css`
:host {
position: relative;
overflow-x: scroll;
overflow-y: hidden;
width: 100%;
height: 100%;
}
.data-grid-cell {
display: inline-block;
font-size: 12px;
border: 0px;
padding: 0px 8px;
margin: 0px;
width: 150px;
background: none;
resize: none;
border-right: 1px solid #ccc;
border-bottom: 1px solid #ccc;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
-o-text-overflow: ellipsis;
}
`
var headers = createHeaders(options)
var rows = createRows(options)
return function render (state) {
var el = html`<div class="data-grid ${prefix}">
${headers(state)}
${rows(state)}
</div>`
el.style.height = (options.height + options.rowHeight * 2) + 'px'
return el
}
}
function createHeaders (options) {
var rowHeight = options.rowHeight
var prefix = css`
:host {
white-space: nowrap;
margin: 0px;
padding: 0px;
}
`
var header = createHeader(options)
function render (state) {
var props = state.properties
var keys = Object.keys(props)
function prop (key) {
return header(props[key])
}
var el = html`<ul class="data-grid-headers ${prefix}">
${keys.map(prop)}
</ul>`
el.style.height = rowHeight + 'px'
return el
}
return render
}
function createHeader (options) {
var rowHeight = options.rowHeight
var onClickHeader = options.onClickHeader || function () {}
var prefix = css`
:host {
width: 150px;
line-height: 30px;
padding: 0px 8px;
display: inline-block;
font-size: 15px;
list-style-type: none;
overflow-x: hidden;
font-weight: 700;
border-right: 1px solid #aaa;
border-bottom: 1px solid #888;
cursor: pointer;
}
`
function render (state) {
function onclick (e) {
return onClickHeader(e, state)
}
var el = html`<div class="data-grid-header ${prefix}" onclick=${onclick}>
<span class="data-grid-header-key">${state.name}</span>
</div>`
el.style.height = rowHeight + 'px'
return el
}
return render
}
function createRows (options) {
console.time('data-grid:createRows')
var height = options.height
var rowHeight = options.rowHeight
var onClickCell = options.onClickCell || function () {}
var prefix = css`
:host {
margin: 0px;
padding: 0px;
overflow-y: scroll;
position: absolute;
white-space: nowrap;
height: 100%;
}
`
function row (data, index) {
var value = data.value
var cells = []
var keys = Object.keys(value)
var i = 0
var l = keys.length
for (i; i < l; i++) {
var key = keys[i]
var item = value[key]
cells[i] = cell({ key: key, value: value[key] }, data)
}
var el = html`<div id="${data.key}" class="data-grid-row">
${cells}
</div>`
el.style['list-style-type'] = 'none'
el.style.height = rowHeight + 'px'
return el
}
function cell (state, row) {
function onclick (e) {
return onClickCell(e, row, state)
}
var el = html`<div id="${state.key}" class="data-grid-cell" onclick=${onclick}>
${state.value}
</div>`
el.style.height = rowHeight + 'px'
el.style['line-height'] = rowHeight + 'px'
el.style['max-height'] = rowHeight + 'px'
el.style['min-height'] = rowHeight + 'px'
return el
}
options.class = prefix
options.eachRow = row
var renderElements = infiniteElements(options)
function render (state) {
var rows = state.data
var tree = renderElements(rows)
console.timeEnd('data-grid:createRows')
return tree
}
return render
}