-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.js
More file actions
86 lines (75 loc) · 1.64 KB
/
example.js
File metadata and controls
86 lines (75 loc) · 1.64 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
var html = require('yo-yo')
var css = require('sheetify')
var xtend = require('xtend')
var formatter = require('data-format')()
var formatted = formatter.format(require('./example-data.json'))
var createGrid = require('./index')
css('./example-styles.css', { global: true })
/*
* Create send function.
*/
var send = require('send-action')({
onaction: onaction,
onchange: onchange,
state: {
site: { title: 'data grid' },
data: formatted.data,
properties: formatted.properties
}
})
/*
* Set up the action handler to modify state based on the actions triggered
*/
function onaction (state, action, data) {
if (action === 'grid:click') {
console.log('clicked', state)
} else {
return state
}
}
/*
* Subscribe to changes to the store for rendering & logging
*/
function onchange (action, state, oldState) {
html.update(document.getElementById('app'), render(state))
}
/*
Create the grid
*/
var grid = createGrid({
height: window.innerHeight - 30,
rowHeight: 30,
onClickCell: onClickCell,
onClickHeader: onClickHeader
})
function onClickHeader (e, header) {
console.log('header', header)
console.log('event', e)
send('grid:click:header', {
header: header
})
}
function onClickCell (e, row, cell) {
console.log('cell', cell)
console.log('row', row)
console.log('event', e)
send('grid:click:cell', {
row:row,
cell:cell
})
}
/*
* Render the html of the app with yo-yo
*/
function render (state) {
return layout(state, send)
}
document.body.appendChild(render(send.state()))
/*
* Create a component to render
*/
function layout (state, send) {
return html`<div id="app">
${grid(state)}
</div>`
}