-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
144 lines (114 loc) · 3.68 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict'
const electron = require('electron')
const fs = require('fs')
const events = require('events')
const vdom = require('virtual-dom')
const hyperx = require('hyperx')
const main = require('main-loop')
const hx = hyperx(vdom.h)
module.exports = function (opts) {
const self = new events.EventEmitter()
const root = opts.root
const filter = opts.filter
const renderRoot = opts.renderRoot || ((hx, children) => {
return hx`<div class="tree-view">${children}</div>`
})
const renderItem = opts.renderItem || ((hx, data, children, loadHook, clickElem, createChild) => {
return hx`<div class="elem" loaded=${loadHook}>
<a href="#" class="header" onclick=${clickElem}>
<div>
${children.length === 0 ? '' : hx`<img src="${__dirname + '/images/chevron.png'}" class="chevron" />`}
<span>${opts.label ? opts.label(data) : data.name}</span>
</div>
</a>
<ul>${children.map(createChild)}</ul>
</div>`
})
const renderChild = opts.renderChild || ((hx, children) => {
return hx`<li>${children}</li>`
})
var selected = null
var selectedDom = null
// injecting css
const browser = opts.browser || electron.remote.getCurrentWindow()
const data = fs.readFileSync(`${__dirname}/style.css`, 'utf8')
browser.webContents.insertCSS(data)
const traverse = data => {
let children = opts.children ? opts.children(data) : (data.children || [])
if (filter) {
children = children.filter(filter)
}
var elem = null
var LoadHook = function () {}
LoadHook.prototype.hook = function (node) { elem = node }
const clickElem = event => {
if (selected === data) {
elem.classList.remove('selected')
self.emit('deselected', data)
selected = null
selectedDom = null
} else {
elem.classList.add('selected')
self.emit('selected', data)
if (selected) self.emit('deselected', selected)
selected = data
selectedDom = elem
}
const elems = opts.container.querySelectorAll('.tree-view .elem')
for (let i = 0; i < elems.length; ++i) {
elems[i].classList.remove('active')
}
elem.classList.add('active')
}
return createItem()
function createItem () {
const loadHook = new LoadHook()
return renderItem(hx, data, children, loadHook, clickElem, createChild)
}
function createChild (c) {
return renderChild(hx, traverse(c))
}
}
const render = self.render = state => {
return renderRoot(hx, traverse(state.root))
}
const loop = self.loop = main({ root }, render, vdom)
opts.container.appendChild(loop.target)
const selectTraverse = (current, node, dom) => {
if (current === node) {
dom.classList.add('selected')
dom.classList.add('active')
self.emit('selected', node)
if (selected) {
self.emit('deselected', selected)
selectedDom.classList.remove('active')
selectedDom.classList.remove('selected')
}
selected = node
selectedDom = dom
return true
}
let children = opts.children ? opts.children(current)
: (current.children || [])
if (filter) {
children = children.filter(filter)
}
if (children) {
var shouldSelect = false
children.forEach((c, ix) => {
const didFound = selectTraverse(c, node
, dom.querySelectorAll(':scope > ul > li > .elem')[ix])
if (didFound) shouldSelect = true
})
if (shouldSelect) {
dom.classList.add('selected')
}
return shouldSelect
}
}
self.select = node => {
selectTraverse(root, node
, opts.container.querySelector('.tree-view > .elem'))
}
return self
}