-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgrid-data.js
196 lines (186 loc) · 5.99 KB
/
grid-data.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
'use strict'
const assert = require('assert')
const shortid = require('shortid')
const pull = require('pull-stream')
const series = require('run-series')
const outbound = require('./lib/notify').outbound
const inbound = require('./lib/notify').inbound
const getActiveCellContents = require('./lib/get-active-cell-contents.js')
const move = require('./effects/move.js')
const validators = {}
const disallowedTypes = ['hidden', 'file', 'radio', 'reset', 'submit']
// default validators
const identity = (x) => x
const member = (group, x) => {
if (group.indexOf(x) > -1) {
return x
}
throw new Error(`${x} is not a member of ${group.join(', ')}`)
}
module.exports = function (chooRoot, header, data) {
assert.ok(typeof chooRoot === 'string', `${chooRoot} must be a string`)
data = data || []
header = header || []
if (!Array.isArray(data)) {
throw new Error(`data must be an array, was ${typeof data}`)
}
if (!Array.isArray(header)) {
throw new Error(`headers must be an array, was ${typeof data}`)
}
header = header.map(h => {
h.editorType = h.editorType || 'text'
if (disallowedTypes.indexOf(h.editorType) > -1) {
throw new Error(`${h.editorType} is not an allowed type of form element`)
}
let defaultValidator = identity
if (h.editorType === 'select') {
defaultValidator = member.bind(null, h.options.map(o => o.value))
}
const f = h.validator || defaultValidator
assert.ok(typeof f === 'function', 'validators must be functions')
validators[h.id] = f
h.validator = void 0
return h
})
return {
namespace: chooRoot,
state: {
header: header,
data: data,
activeCell: {row: 0, col: 0},
inEdit: false,
scratch: '',
error: null,
users: {}
},
effects: {
move: move(chooRoot),
edit: (action, state, send, done) => {
action = action || {}
const initialValue = action.value || getActiveCellContents(state)
series([
(cb) => send(`${chooRoot}:setEdit`, cb),
(cb) => send(`${chooRoot}:updateScratch`, {value: initialValue}, cb)
], done)
},
commit: (action, state, send, done) => {
outbound({type: 'commit', action})
let val = state.scratch
if (typeof action.value !== 'undefined') {
val = action.value
}
series([
(cb) => send(`${chooRoot}:unsetEdit`, cb),
(cb) => send(`${chooRoot}:updateData`, {value: val}, cb),
(cb) => send(`${chooRoot}:clearScratch`, cb)
], done)
},
revert: (action, state, send, done) => {
series([
(cb) => send(`${chooRoot}:unsetEdit`, cb),
(cb) => send(`${chooRoot}:clearScratch`, cb)
], done)
},
removeById: (action, state, send, done) => {
const row = state.data.findIndex(r => r.id === action.id)
send(`${chooRoot}:removeRow`, {row: row}, done)
},
insertById: (action, state, send, done) => {
let insertRow = action.id
if (action.id === 'top') {
insertRow = 0
} else if (action.id === 'bottom') {
insertRow = state.data.length
} else {
insertRow = state.data.findIndex(r => r.id === action.id)
}
send(`${chooRoot}:insertRow`, {row: insertRow}, done)
}
},
reducers: {
setActive: (action, state) => {
const cell = {row: action.row, col: action.col}
outbound({type: 'activeCell', data: cell})
return {activeCell: cell}
},
setEdit: (action, state) => {
return {inEdit: true}
},
unsetEdit: (action, state) => {
return {inEdit: false}
},
updateScratch: (action, state) => {
outbound({type: 'updateScratch', data: action.value})
return {scratch: action.value}
},
updateData: (action, state) => {
const row = state.activeCell.row
const colId = state.header[state.activeCell.col].id
const rowId = state.data[row].id
const validator = validators[colId]
let value
try {
value = validator(action.value)
} catch (err) {
return {error: err}
}
const data = state.data.slice(0)
outbound({type: 'update', data: {rowId: rowId, colId: colId, value: value}})
data[row][colId] = value
return {data: data}
},
clearScratch: (action, state) => {
return {scratch: ''}
},
insertRow: (action, state) => {
let rows = action.rows
if (!Array.isArray(rows)) {
rows = [{id: shortid.generate()}]
}
rows.forEach(r => {
state.header.forEach(h => {
r[h.id] = h.default || ''
})
})
const data = state.data.slice(0)
data.splice(action.row, 0, rows)
outbound({type: 'insertRow', data: {before: action.row, rows: rows}})
return {data: data}
},
removeRow: (action, state) => {
if (action.row >= state.data.length) {
action.row = 0
}
const data = state.data.slice(0)
data.splice(action.row, 1)
outbound({type: 'removeRow', data: {row: action.row}})
return {data: data}
},
userActive: (action, state) => {
const users = {}
users[action.user] = {row: action.row, col: action.col}
return users
}
},
subscriptions: [
(send) => {
pull(inbound.listen(), pull.drain((evt) => {
switch (evt.type) {
case 'removeRow':
send(`${chooRoot}:removeById`, {id: evt.id})
break
case 'insertRow':
send(`${chooRoot}:insertById`, {id: evt.id, rows: evt.rows})
break
case 'updateCell':
send(`${chooRoot}:updateCell`, {cell: evt.cell, data: evt.data})
break
case 'userActive':
send(`${chooRoot}:userActive`, {user: evt.user, row: evt.row, col: evt.col})
break
}
}))
}
]
}
}