Skip to content

Commit fc0dec2

Browse files
committed
strict mode fixes, + allow async plugin loading
1 parent 26e459b commit fc0dec2

File tree

15 files changed

+105
-73
lines changed

15 files changed

+105
-73
lines changed

classy.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,24 @@ class Treed {
3737

3838
var store = this.store = new MainStore({
3939
actions: options.actions,
40-
plugins: pluginType(this.options.plugins, 'store'),
41-
allPlugins: this.options.plugins,
40+
// plugins: pluginType(this.options.plugins, 'store'),
41+
// allPlugins: this.options.plugins,
4242
db: db
4343
})
44-
this.keyManager.attach(store)
45-
resolve(store)
44+
45+
store.init(this.options.plugins, (err) => {
46+
if (err) return reject(err)
47+
this.keyManager.attach(store)
48+
resolve(store)
49+
})
4650
})
4751
})
4852
}
4953

5054
on (what, handler) {
5155
this.store.on(what, handler)
5256
}
57+
5358
off (what, handler) {
5459
this.store.on(what, handler)
5560
}

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ var KeyManager = require('./key-manager')
1313
var ListView = require('./views/list')
1414
var MainStore = require('./stores/main')
1515

16+
console.warn('DEPRECATED! Please switch to treed/classy')
17+
1618
function flattenKeySections(keys) {
1719
var ret = {}
1820
for (var name in keys) {

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"dependencies": {
2626
"async": "~0.9.0",
2727
"bluebird": "~1.2.4",
28+
"classnames": "^1.2.0",
2829
"envify": "~3.0.0",
2930
"escodegen": "~1.4.1",
3031
"esprima": "~1.2.2",

plugins/collapse/store.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ module.exports = {
5757

5858
toggleCollapseDeep: function (id) {
5959
if (!arguments.length) id = this.view.active
60+
let ids
6061
if (this.view.mode === 'visual') {
6162
ids = this.view.selection
6263
} else {
@@ -69,7 +70,7 @@ module.exports = {
6970
if (!node.children.length) return []
7071
return [].concat.apply([id], node.children.map(allParents))
7172
}
72-
pedigrees = ids.map(allParents)
73+
let pedigrees = ids.map(allParents)
7374
var commands = pedigrees.map((ids) => {
7475
return ['setMany', {ids: ids, attr: 'collapsed', values: !this.db.nodes[ids[0]].collapsed}]
7576
})

plugins/rebase/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
import React from 'react'
3+
24
var Breadcrumb = require('./breadcrumb')
35

46
module.exports = {

plugins/rebase/index.less

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
position: relative;
1313
cursor: pointer;
1414
border-radius: 10px;
15-
font-family: bold;
15+
font-weight: bold;
16+
color: #999;
1617

1718
&:hover {
1819
background-color: #eee;

quickstart.js

-14
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,6 @@ Treed.prototype.quickstart = function (el, options) {
1717
actions: options.actions || require('./views/list/actions'),
1818
}).then(() => {
1919
return this.startView(el, options)
20-
/*
21-
return new Promise((resolve, reject) => {
22-
var viewOptions = extend({
23-
keys: options.keys || ListView.keys,
24-
}, options.viewOptions)
25-
var View = options.View || ListView
26-
, props = this.addView(viewOptions)
27-
28-
React.render(<View {...props}/>, el, function (err) {
29-
if (err) return reject(err)
30-
resolve(props.store)
31-
})
32-
})
33-
*/
3420
}).catch(error => {
3521
console.warn('Treed initialization failed!', error)
3622
throw error

skins/regular/index.less

+13
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,19 @@
154154
box-sizing: border-box;
155155
background-color: transparent;
156156

157+
blockquote {
158+
border-left: 5px solid #ccc;
159+
padding-left: 10px;
160+
margin: 10px 0;
161+
162+
> p:first-child {
163+
margin-top: 0;
164+
}
165+
> p:last-child {
166+
margin-bottom: 0;
167+
}
168+
}
169+
157170
> *:first-child {
158171
margin-top: 0;
159172
}

stores/base.js

+29-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
import async from 'async'
3+
24
var Promise = require('bluebird')
35

46
module.exports = BaseStore
@@ -13,23 +15,33 @@ function BaseStore(options) {
1315
}
1416
}
1517

16-
if (options.plugins) {
17-
options.plugins.forEach((plugin) => this.addPlugin(plugin, options.allPlugins))
18-
}
19-
this.allPlugins = options.allPlugins
2018
}
2119

2220
BaseStore.prototype = {
2321
actions: {},
2422

23+
init(plugins, done) {
24+
this.allPlugins = plugins || []
25+
if (!plugins) return done()
26+
const tasks = plugins
27+
.map(plugin => plugin.store)
28+
.filter(plugin => !!plugin)
29+
.map(plugin => this.addPlugin.bind(this, plugin))
30+
31+
async.parallel(tasks, done)
32+
/*
33+
if (options.plugins) {
34+
options.plugins.forEach((plugin) => this.addPlugin(plugin, options.allPlugins))
35+
}
36+
*/
37+
38+
},
39+
2540
teardown: function () {
2641
this._plugin_teardowns.forEach(fn => fn(this))
2742
},
2843

29-
addPlugin: function (plugin, allPlugins) {
30-
if (plugin.init) {
31-
plugin.init(this) // TODO async?
32-
}
44+
addPlugin: function (plugin, done) {
3345
if (plugin.teardown) {
3446
this._plugin_teardowns.push(plugin.teardown)
3547
}
@@ -38,7 +50,7 @@ BaseStore.prototype = {
3850
, actions
3951
if (plugin.actions) {
4052
if ('function' === typeof plugin.actions) {
41-
actions = plugin.actions(allPlugins)
53+
actions = plugin.actions(this.allPlugins)
4254
} else {
4355
actions = plugin.actions
4456
}
@@ -52,6 +64,14 @@ BaseStore.prototype = {
5264
this[name] = plugin.extend[name]
5365
}
5466
}
67+
68+
if (plugin.init) {
69+
plugin.init(this) // TODO async?
70+
} else if (plugin.asyncInit) {
71+
return plugin.asyncInit(this, done)
72+
}
73+
74+
done()
5575
},
5676

5777
on: function (changes, listener) {

stores/main.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,13 @@ MainStore.prototype = extend(Object.create(BaseStore.prototype), {
190190

191191
events: {
192192
nodeChanged: (id) => 'node:' + id,
193-
nodeViewChanged: function (id) { return this.nodeChanged(id) + ':view:' + this.vid },
193+
nodeViewChanged(id) { return this.nodeChanged(id) + ':view:' + this.vid },
194194

195195
activeViewChanged: () => 'active-view',
196-
activeNodeChanged: function () { return 'active-node:' + this.vid },
197-
rootChanged: function () { return 'root:' + this.vid },
198-
modeChanged: function () { return 'mode:' + this.vid },
199-
activeModeChanged: function () { return 'mode:active' },
196+
activeNodeChanged() { return 'active-node:' + this.vid },
197+
rootChanged() { return 'root:' + this.vid },
198+
modeChanged() { return 'mode:' + this.vid },
199+
activeModeChanged() { return 'mode:active' },
200200
changed: () => 'changed'
201201
},
202202

views/mindmap/actions.js

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = {
2323
}
2424
}
2525
this.executeCommand('create', pos, (err, cmd) => {
26+
if (err) return console.warn('failed to create', err) // TODO UI?
2627
this.edit(cmd.id)
2728
})
2829
},
@@ -54,11 +55,13 @@ module.exports = {
5455
},
5556
'create', pos,
5657
(err, cmd) => {
58+
if (err) return console.warn('failed to create', err) // TODO UI?
5759
this.editStart(cmd.id)
5860
}
5961
)
6062
} else {
6163
this.executeCommand('create', pos, (err, cmd) => {
64+
if (err) return console.warn('failed to create', err) // TODO UI?
6265
this.edit(cmd.id)
6366
})
6467
}

views/mindmap/calcpos.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ module.exports = function calcPos(root, nodes, xsep, ysep, cellHeight, heights)
44

55
var {boxes, height, width} = calcBoxes(tree, cellHeight, xsep, 1, ysep)
66
var links = []
7-
var rbox = boxes[root]
8-
, rx = 0//rbox.x
9-
, ry = 0//rbox.y
7+
var rx = 0
8+
, ry = 0
109
relativize(tree, rx, ry)
1110

1211
return {boxes, links, height, width}
@@ -55,13 +54,13 @@ function calcBoxes(data, cellHeight, xsep, pxscale, ysep) {
5554
return a.width + b.width + (xsep || 0)
5655
});
5756
var nodes = t.nodes(data)
58-
var links = t.links(nodes)
57+
// var links = t.links(nodes)
5958

6059
var {xscale, ydepth} = findScale(data, xsep)
6160
var xs = pxscale
6261
, ys = cellHeight
6362
, height = ydepth * (cellHeight + ysep || 0)
64-
, width = pxscale / xscale
63+
, width = pxscale / xscale
6564
//console.log(xscale, ydepth)
6665

6766
var boxes = {}
@@ -78,7 +77,7 @@ function calcBoxes(data, cellHeight, xsep, pxscale, ysep) {
7877
return {boxes, width, height}
7978
}
8079

81-
function findScale(node, sep) {
80+
function findScale(rootNode, sep) {
8281
var min = null
8382
, maxdepth = 1
8483
function getWidth(node, depth) {
@@ -98,7 +97,7 @@ function findScale(node, sep) {
9897
getWidth(node.children[node.children.length-1], depth + 1)
9998
}
10099
}
101-
getWidth(node, 1)
100+
getWidth(rootNode, 1)
102101
return {xscale: min, ydepth: maxdepth}
103102
}
104103

views/mindmap/links.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var React = require('react/addons')
2-
, cx = React.addons.classSet
3-
, PT = React.PropTypes
2+
// , PT = React.PropTypes
43

54
function line(obj) {
65
var {x1, y1, x2, y2} = obj

views/mindmap/movable.js

+24-24
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,30 @@ var Movable = React.createClass({
5353
],
5454

5555
componentWillReceiveProps: function (nextProps) {
56-
var aid = this.state.activeNode
57-
, pos = nextProps.positions.boxes[aid]
58-
if (!pos) return
59-
var nx = this.state.left + pos.x
60-
, ny = this.state.top + pos.y
61-
, margin = 30
62-
, dx = 0
63-
, dy = 0
64-
if (nx - margin < 0) {
65-
dx -= nx - margin
66-
}
67-
if (ny - margin < 0) {
68-
dy -= ny - margin
69-
}
70-
if (nx + pos.width + margin > this.props.width) {
71-
dx -= nx + pos.width + margin - this.props.width
72-
}
73-
if (ny + pos.height + margin > this.props.height) {
74-
dy -= ny + pos.height + margin - this.props.height
75-
}
76-
this.setState( {
77-
left: this.state.left + dx,
78-
top: this.state.top + dy,
79-
})
56+
var aid = this.state.activeNode
57+
, pos = nextProps.positions.boxes[aid]
58+
if (!pos) return
59+
var nx = this.state.left + pos.x
60+
, ny = this.state.top + pos.y
61+
, margin = 30
62+
, dx = 0
63+
, dy = 0
64+
if (nx - margin < 0) {
65+
dx -= nx - margin
66+
}
67+
if (ny - margin < 0) {
68+
dy -= ny - margin
69+
}
70+
if (nx + pos.width + margin > this.props.width) {
71+
dx -= nx + pos.width + margin - this.props.width
72+
}
73+
if (ny + pos.height + margin > this.props.height) {
74+
dy -= ny + pos.height + margin - this.props.height
75+
}
76+
this.setState( {
77+
left: this.state.left + dx,
78+
top: this.state.top + dy,
79+
})
8080
/*
8181
var aid = this.props.store.view.active
8282
, ppos = this.props.positions.boxes[aid]

views/mindmap/movement.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ var movement = module.exports = {
44
if (nodes[id].children && nodes[id].children.length && (!nodes[id].collapsed || id === root)) {
55
return nodes[id].children[0]
66
}
7-
var up = down = id
7+
let up = id
8+
let down = id
89
while (up || down) {
910
if (down) {
1011
if (nodes[down].children && nodes[down].children.length && !nodes[down].collapsed) {
@@ -26,7 +27,7 @@ var movement = module.exports = {
2627
// TODO: refactor below and nextSiblingOrCousin to remove duplicate logic.
2728
below: function (id, root, nodes) {
2829
if (id === root) return
29-
var pid = nodes[id].parent
30+
let pid = nodes[id].parent
3031
, p = nodes[pid]
3132
, ch = p.children
3233
, ix = ch.indexOf(id)
@@ -149,12 +150,11 @@ var movement = module.exports = {
149150

150151
nextCousin: function (id, root, nodes) {
151152
if (id === root) return
152-
var aunt
153+
let aunt
153154
, degree = 1
154155
, parent = nodes[id].parent
155156
while (!aunt) {
156-
var pid = nodes[parent].parent
157-
, ch = nodes[parent].children
157+
let ch = nodes[parent].children
158158
, ix = ch.indexOf(parent) + 1
159159
while (ix < ch.length && !(nodes[ch[ix]].children &&
160160
!nodes[ch[ix]].collapsed &&
@@ -169,7 +169,7 @@ var movement = module.exports = {
169169
degree += 1
170170
parent = nodes[parent].parent
171171
}
172-
cousin = aunt
172+
let cousin = aunt
173173
for (; degree > 0 && nodes[cousin].children && nodes[cousin].children.length; degree--) {
174174
cousin = nodes[cousin].children[0]
175175
}

0 commit comments

Comments
 (0)