-
Notifications
You must be signed in to change notification settings - Fork 37
/
popup.js
115 lines (102 loc) · 3.75 KB
/
popup.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
var port;
chrome.tabs.getSelected(null, function(tab) {
port = chrome.tabs.connect(tab.id, {name: "pullrequest"});
});
function collapse() {
var path = document.getElementById('path').value;
port.postMessage({collapse: path});
}
function expand() {
var path = document.getElementById('path').value;
port.postMessage({expand: path});
}
function collapseTree() {
$('#tree').jstree('close_all');
}
function expandTree() {
$('#tree').jstree('open_all');
}
document.addEventListener('DOMContentLoaded', function () {
document.querySelector('#collapse').addEventListener('click', collapse);
document.querySelector('#expand').addEventListener('click', expand);
document.querySelector('#collapse-tree').addEventListener('click', collapseTree);
document.querySelector('#expand-tree').addEventListener('click', expandTree);
});
function Node(id, parent, text, icon) {
this.id = id;
this.parent = parent || '#';
this.text = text;
if (icon) {
this.icon = icon;
}
}
function convertPathsToNodes(paths) {
var nodes = {};
$.each(paths, function(index, path) {
var sections = path.split('/');
var node = new Node(sections[0], '#', sections[0], sections.length > 1 ? undefined : 'jstree-file');
nodes[node.id] = node;
if (sections.length == 1) {
return;
}
for (var i = 1; i < sections.length; i++) {
var id = sections.slice(0, i + 1).join('/');
var parent = sections.slice(0, i).join('/');
nodes[id] = new Node(id, parent, sections[i], i == sections.length - 1 ? 'jstree-file' : undefined);
}
});
var i = 0;
var nodes_flattened = [];
for (var node in nodes) {
nodes_flattened[i++] = nodes[node];
}
return nodes_flattened;
}
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {getPaths: true}, function(response) {
if (response.paths) {
var nodes = convertPathsToNodes(response.paths);
$('#tree').jstree({
'core' : {
'data' : nodes,
'animation': 0,
'themes' : {
'responsive' : false,
'variant' : 'small'
}
},
"plugins" : [ "wholerow", "contextmenu" ],
"contextmenu": {
"items": function ($node) {
var items = {
"Collapse": {
"label": "Collapse Diff",
"action": function (obj) {
port.postMessage({collapse: '^' + $node.id});
}
},
"Expand": {
"label": "Expand Diff",
"action": function (obj) {
port.postMessage({expand: '^' + $node.id});
}
},
"Goto": {
"label": "Jump to Diff",
"action": function(obj) {
port.postMessage({goto: $node.id});
}
}
};
if ($node.icon != 'jstree-file') {
delete items.Goto;
}
return items;
}
}
}).on("select_node.jstree", function (e, data) {
return data.instance.toggle_node(data.node);
});
}
});
});