-
Notifications
You must be signed in to change notification settings - Fork 193
/
index.js
129 lines (106 loc) · 2.85 KB
/
index.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
/**
* Quickstart entry point. If you want to configure things, you're probably
* better off going custom.
*/
import {render} from 'react-dom'
var React = require('react')
var extend = require('./util/extend')
var keyHandlers = require('./key-handlers')
var defaultKeys = flattenKeySections(require('./views/list/keys'))
var KeyManager = require('./key-manager')
var ListView = require('./views/list')
var MainStore = require('./stores/main')
console.warn('DEPRECATED! Please switch to treed/classy')
function flattenKeySections(keys) {
var ret = {}
for (var name in keys) {
for (var sub in keys[name]) {
ret[sub] = keys[name][sub]
}
}
return ret
}
var Db = require('./db')
module.exports = {
quickstart,
initView,
initStore,
pluginType,
viewConfig,
}
/*
* get a store
* add a view to the store
*/
function quickstart(el, options, done) {
options = extend({
viewOptions: {},
storeOptions: {},
plugins: [],
}, options || {})
if ('string' === typeof el) {
var found = document.querySelector(el)
if (!found) throw new Error('element not found: ' + el)
el = found
}
initStore(options.plugins, options.storeOptions, (err, store) => {
if (err) return done(err)
var keyManager = new KeyManager()
keyManager.attach(store)
initView(el, store, keyManager, options.plugins, options.viewOptions, (storeView) => {
keyManager.listen(window)
done && done(err, store, keyManager, storeView)
})
})
}
function initStore(plugins, options, done) {
options = extend({
PL: require('./pl/mem'),
pl: null,
data: null,
}, options)
var pl = options.pl || new options.PL()
var db = new Db(pl, pluginType(plugins, 'db'))
db.init(options.data, function (err) {
if (err) return done(err)
var store = new MainStore({
plugins: pluginType(plugins, 'store'),
allPlugins: plugins,
db: db
})
done(null, store)
})
}
function viewConfig(store, plugins, options) {
options = extend({
root: null,
defaultKeys: defaultKeys,
}, options)
var storeView = store.registerView(options.root)
var props = {
plugins: pluginType(plugins, 'view'),
nodePlugins: pluginType(plugins, 'node'),
store: storeView,
}
return {
keys: keyHandlers(options.defaultKeys, storeView.actions, pluginType(plugins, 'keys'), plugins),
view: storeView,
props: props
}
}
function initView(el, store, keyManager, plugins, options, done) {
options = extend({
View: ListView,
}, options)
var config = viewConfig(store, plugins, options)
keyManager.addView(config.view.id, config.keys)
render(options.View(config.props), el, function () {
done(config.view)
})
}
function pluginType(plugins, type) {
if (!plugins) return []
return plugins.reduce((list, plugin) => {
return plugin[type] ? [plugin[type]].concat(list) : list
}, [])
}