forked from ovity/octotree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.tree.js
More file actions
187 lines (160 loc) · 5.34 KB
/
Copy pathview.tree.js
File metadata and controls
187 lines (160 loc) · 5.34 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
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
class TreeView {
constructor($dom, store, adapter) {
this.store = store
this.adapter = adapter
this.$view = $dom.find('.octotree_treeview')
this.$tree = this.$view.find('.octotree_view_body')
.on('click.jstree', '.jstree-open>a', ({target}) => this.$jstree.close_node(target))
.on('click.jstree', '.jstree-closed>a', ({target}) => this.$jstree.open_node(target))
.on('click', this._onItemClick.bind(this))
.jstree({
core: { multiple: false, worker: false, themes : { responsive : false } },
plugins: ['wholerow']
})
}
get $jstree() {
return this.$tree.jstree(true)
}
show(repo, token) {
const $jstree = this.$jstree
$jstree.settings.core.data = (node, cb) => {
const loadAll = this.adapter.canLoadEntireTree() &&
this.store.get(STORE.LOADALL)
node = !loadAll && (node.id === '#' ? {path: ''} : node.original)
this.adapter.loadCodeTree({repo, token, node}, (err, treeData) => {
if (err) {
$(this).trigger(EVENT.FETCH_ERROR, [err])
}
else {
treeData = this._sort(treeData)
if (loadAll) {
treeData = this._collapse(treeData)
}
cb(treeData)
}
})
}
this.$tree.one('refresh.jstree', () => {
this.syncSelection()
$(this).trigger(EVENT.VIEW_READY)
})
this._showHeader(repo)
$jstree.refresh(true)
}
_showHeader(repo) {
const adapter = this.adapter
this.$view.find('.octotree_view_header')
.html(
'<div class="octotree_header_repo">' +
'<a href="/' + repo.username + '">' + repo.username +'</a>' +
' / ' +
'<a data-pjax href="/' + repo.username + '/' + repo.reponame + '">' + repo.reponame +'</a>' +
'</div>' +
'<div class="octotree_header_branch">' +
this._deXss(repo.branch) +
'</div>'
)
.on('click', 'a[data-pjax]', function (event) {
event.preventDefault()
const href = $(this).attr('href'); /* a.href always return absolute URL, don't want that */
const newTab = event.shiftKey || event.ctrlKey || event.metaKey
newTab ? adapter.openInNewTab(href) : adapter.selectFile(href)
})
}
_deXss(str) {
return str && str.replace(/[<>'"&]/g, '-')
}
_sort(folder) {
folder.sort((a, b) => {
if (a.type === b.type) return a.text === b.text ? 0 : a.text < b.text ? -1 : 1
return a.type === 'blob' ? 1 : -1
})
folder.forEach((item) => {
if (item.type === 'tree' && item.children !== true && item.children.length > 0) {
this._sort(item.children)
}
})
return folder
}
_collapse(folder) {
return folder.map((item) => {
if (item.type === 'tree') {
item.children = this._collapse(item.children)
if (item.children.length === 1 && item.children[0].type === 'tree') {
const onlyChild = item.children[0]
onlyChild.text = item.text + '/' + onlyChild.text
return onlyChild
}
}
return item
})
}
_onItemClick(event) {
let $target = $(event.target)
let download = false
// handle middle click
if (event.which === 2) return
// handle icon click, fix #122
if ($target.is('i.jstree-icon')) {
$target = $target.parent()
download = true
}
if (!$target.is('a.jstree-anchor')) return
// refocus after complete so that keyboard navigation works, fix #158
const refocusAfterCompletion = () => {
$(document).one('pjax:success page:load', () => {
this.$jstree.get_container().focus()
})
}
const adapter = this.adapter
const newTab = event.shiftKey || event.ctrlKey || event.metaKey
const href = $target.attr('href')
const $icon = $target.children().length
? $target.children(':first')
: $target.siblings(':first') // handles child links in submodule
if ($icon.hasClass('commit')) {
refocusAfterCompletion()
newTab ? adapter.openInNewTab(href) : adapter.selectSubmodule(href)
}
else if ($icon.hasClass('blob')) {
if (download) {
adapter.downloadFile(href, $target.text())
}
else {
refocusAfterCompletion()
newTab ? adapter.openInNewTab(href) : adapter.selectFile(href)
}
}
}
syncSelection() {
const $jstree = this.$jstree
if (!$jstree) return
// converts /username/reponame/object_type/branch/path to path
const path = decodeURIComponent(location.pathname)
const match = path.match(/(?:[^\/]+\/){4}(.*)/)
if (!match) return
const currentPath = match[1]
const loadAll = this.adapter.canLoadEntireTree() &&
this.store.get(STORE.LOADALL)
selectPath(loadAll ? [currentPath] : breakPath(currentPath))
// converts ['a/b'] to ['a', 'a/b']
function breakPath(fullPath) {
return fullPath.split('/').reduce((res, path, idx) => {
res.push(idx === 0 ? path : (res[idx-1] + '/' + path))
return res
}, [])
}
function selectPath(paths, index = 0) {
const nodeId = NODE_PREFIX + paths[index]
if ($jstree.get_node(nodeId)) {
$jstree.deselect_all()
$jstree.select_node(nodeId)
$jstree.open_node(nodeId, () => {
if (++index < paths.length) {
selectPath(paths, index)
}
})
}
}
}
}