forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.js
More file actions
103 lines (88 loc) · 2.73 KB
/
Copy pathtransform.js
File metadata and controls
103 lines (88 loc) · 2.73 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
var m = require("../../")
module.exports.intersection = require("lodash.intersection")
module.exports.pluckFn = function(key) {
return function(obj) {
var keys = key.split("."),
value = obj
for (var i = 0, l = keys.length; i < l; i++) {
if (!value) return
value = value[keys[i]]
}
return value
}
}
module.exports.identity = function(value) {
return value
}
module.exports.testSet = function() {
var testSet = {}
var state = (testSet.state = m.observable({
root: null,
renderedNodes: m.observable.array(),
collapsed: new m.map() // KM: ideally, I would like to use a set
}))
var stats = (testSet.stats = {
refCount: 0
})
var TreeNode = (testSet.TreeNode = function(name, extensions) {
this.children = m.observable.array()
this.icon = m.observable("folder")
this.parent = null // not observed
this.name = name // not observed
// optional extensions
if (extensions) {
for (var key in extensions) {
this[key] = extensions[key]
}
}
})
TreeNode.prototype.addChild = function(node) {
node.parent = this
this.children.push(node)
}
TreeNode.prototype.addChildren = function(nodes) {
var _this = this
nodes.map(function(node) {
node.parent = _this
})
this.children.splice.apply(this.children, [this.children.length, 0].concat(nodes))
}
TreeNode.prototype.path = function() {
var node = this,
parts = []
while (node) {
parts.push(node.name)
node = node.parent
}
return parts.join("/")
}
TreeNode.prototype.map = function(iteratee, results) {
results = results || []
results.push(iteratee(this))
this.children.forEach(function(child) {
child.map(iteratee, results)
})
return results
}
TreeNode.prototype.find = function(predicate) {
if (predicate(this)) return this
var result
for (var i = 0, l = this.children.length; i < l; i++) {
result = this.children[i].find(predicate)
if (result) return result
}
return null
}
var DisplayNode = (testSet.DisplayNode = function(node) {
stats.refCount++
this.node = node
})
DisplayNode.prototype.destroy = function() {
stats.refCount--
}
DisplayNode.prototype.toggleCollapsed = function() {
var path = this.node.path()
state.collapsed.has(path) ? state.collapsed.delete(path) : state.collapsed.set(path, true) // KM: ideally, I would like to use a set
}
return testSet
}