forked from YAL-GMEdit/toggle-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toggle-tree.js
113 lines (101 loc) · 4 KB
/
toggle-tree.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
(function () {
var Preferences = $gmedit["ui.Preferences"];
var FileWrap = $gmedit["electron.FileWrap"];
var moveHamburger = true;
function toggleView() {
var tree = document.querySelector("#tree-td");
if (tree) {
tree.style.display = tree.style.display == "none" ? "" : "none";
updateHamburgerLocation();
GMEdit_Splitter.syncMain();
} else {
console.error("Could not find resource tree view");
}
}
function updateHamburgerLocation() {
var currTRV = FileWrap.readConfigSync("config", Preferences.path).toggleResourceView;
var tree = document.querySelector("#tree-td");
var hamburger = document.querySelector(".hamburger");
var buttonSapce = document.querySelector("#button-slot");
// Free up white space for tabs
// Check preferences to see if hamberger is to be moved
if (currTRV.moveHamburger) {
buttonSapce.style.display = tree.style.display == "none" ? "" : "none";
// Move the hamburger based on toggle state
if (tree.style.display == "") {
// Resource tree is shown, move hamburger next to project name
var projectName = document.querySelector(".project-name");
projectName.appendChild(hamburger);
// Re-query button, otherwise button will be duplicated
document.querySelector(".hamburger").classList.remove("hamburger-hidden");
document.querySelector(".chrome-tabs-content").classList.remove("chrome-tabs-content-reduced");
} else {
// Resource tree is hidden, move hamburger next to tabs
buttonSapce.appendChild(hamburger);
// Re-query button, otherwise button will be duplicated
document.querySelector(".hamburger").classList.add("hamburger-hidden");
document.querySelector(".chrome-tabs-content").classList.add("chrome-tabs-content-reduced");
}
} else {
buttonSapce.style.display = "none";
// if hamberger isn't supposed to be moved, force into original position
var projectName = document.querySelector(".project-name");
projectName.appendChild(hamburger);
// Re-query button, otherwise button will be duplicated
document.querySelector(".hamburger").classList.remove("hamburger-hidden");
document.querySelector(".chrome-tabs-content").classList.remove("chrome-tabs-content-reduced");
}
}
function init() {
// Initialize new elements
document.querySelector(".system-buttons").classList.add("hamburger");
var buttonSlot = document.createElement("div")
// Default button slot to hidden since resource tree is shown by default
buttonSlot.style.display = "none";
buttonSlot.id = "button-slot"
document.querySelector("#tabs").prepend(buttonSlot);
// Prepare toggled resource tree preferences
function prepareTRT() {
var currentPrefs = Preferences.current.toggleResourceView;
if (!currentPrefs) currentPrefs = Preferences.current.toggleResourceView = {};
return currentPrefs;
}
function opt(ov, name, def) {
if (!ov) return def;
var val = ov[name];
return val !== undefined ? val : def;
}
AceCommands.add({
name: "toggleResourceView",
bindKey: "Ctrl-Shift-B",
exec: function (editor) {
toggleView();
var currPrefs = FileWrap.readConfigSync("config", Preferences.path);
if (currPrefs) {
var currTRT = currPrefs.toggleResourceView;
if (currTRT == null) currTRT = currPrefs.toggleResourceView = {};
FileWrap.writeConfigSync("config", Preferences.path, currPrefs);
}
}
});
AceCommands.addToPalette({
name: "Toggle Resource View",
exec: "toggleResourceView"
});
GMEdit.on("preferencesBuilt", function (e) {
var out = e.target.querySelector('.plugin-settings[for="toggle-tree"]');
var currTRT = Preferences.current.toggleResourceView;
moveHamburger = opt(currTRT, "moveHamburger", true);
Preferences.addCheckbox(out, "Show hamburger when resource tree is closed", opt(currTRT, "moveHamburger", true), function (val) {
var currTRT = prepareTRT();
showFuncArgs = currTRT.moveHamburger = val;
Preferences.save();
updateHamburgerLocation();
forceRefresh();
});
});
}
GMEdit.register("toggle-tree", {
init: init
});
})();