-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptions.js
212 lines (176 loc) · 7.64 KB
/
Options.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets, window */
define(function (require, exports, module) {
"use strict";
const { HAS_NODE, NS, TITLE, PREFS_PREFIX, CONFIG, OPTIONS } = require("CONSTANTS");
const Dialogs = brackets.getModule("widgets/Dialogs");
const PreferencesManager = brackets.getModule("preferences/PreferencesManager");
const prefs = PreferencesManager.getExtensionPrefs(PREFS_PREFIX);
const ID = {
getId: ((counter) => () => (NS + "__options-dialog-id-" + counter++))(1)
};
const REGISTERED_OPTIONS = [];
function getOptionsContent() {
return `<form onsubmit="event.preventDefault();">${
REGISTERED_OPTIONS
.map(item => item.template())
.join(`<div style="height: 15px"></div>`)
}</form>`;
}
function registerDivider() {
REGISTERED_OPTIONS.push({
template: () => `<hr style="margin: 0; border-bottom: none; opacity: 0.25;">`
});
}
function registerTitle(content) {
REGISTERED_OPTIONS.push({
template: () => `<h3 style="margin: 0; line-height: normal;">${content}</h3>`
});
}
function registerText(prefName, options = {}) {
const pref = prefs.getPreference(prefName);
const id = ID.getId();
REGISTERED_OPTIONS.push({
prefName,
template: () => {
const currentValue = prefs.get(prefName);
return `
<div data-pref-name="${prefName}">
<label for="${id}">${options.description ?? pref.description}${options.restartRequired ? " <em>(Requires restart.)</em>": ""}</label>
<input style="margin: 0px" id="${id}" type="text" name="${prefName}" value="${currentValue}">
<small style="display: block; margin-top: 2px; font-style: italic; opacity: 0.7;" ${options.note ? "": "hidden"}>${options.note ?? ""}</small>
</div>`;
},
onSave(formData) {
const value = formData.get(prefName);
if (value !== prefs.get(prefName)) {
prefs.set(prefName, value);
}
}
});
}
function registerSelect(prefName, options = {}) {
const pref = prefs.getPreference(prefName);
const id = ID.getId();
REGISTERED_OPTIONS.push({
prefName,
template: () => {
const currentValue = prefs.get(prefName);
return `
<div data-pref-name="${prefName}">
<label for="${id}">${options.description ?? pref.description}${options.restartRequired ? " <em>(Requires restart.)</em>": ""}</label>
<select style="margin: 0px" id="${id}" name="${prefName}">
${pref.values.map(item => `
<option ${item === currentValue ? "selected": ""} value="${item}">
${item}
</option>
`)}
</select>
<small style="display: block; margin-top: 2px; font-style: italic; opacity: 0.7;" ${options.note ? "": "hidden"}>${options.note ?? ""}</small>
</div>
`;
},
onSave(formData) {
let value = formData.get(prefName);
if (pref.type === "number") {
value = parseFloat(value);
value = isNaN(value) ? pref.initial: value;
}
if (value !== prefs.get(prefName)) {
prefs.set(prefName, value);
}
}
});
}
prefs.definePreference(CONFIG.CODE_GLOBAL, "array", [], {
description: HAS_NODE ?
`Array of saved global codes. Format: { "name": String, "code": String, "cmd?": String, "env?": "phoenix" | "node" }`:
`Array of saved global codes. Format: { "name": String, "code": String, "cmd?": String }`
});
prefs.definePreference(CONFIG.CODE_LOCAL, "string", CONFIG.DEFAULT.CODE_LOCAL, {
description: "Relative path to local codes (no slash at the start and at the end). (Requires restart.)"
});
registerText(CONFIG.CODE_LOCAL, {
description: "Relative path to local codes.",
note: "No slash at the start and at the end.",
restartRequired: true
});
prefs.definePreference(CONFIG.GLOBAL_FIRST, "string", OPTIONS.YES, {
description: "Show global codes first?",
values: [OPTIONS.YES, OPTIONS.NO]
});
registerSelect(CONFIG.GLOBAL_FIRST);
prefs.definePreference(CONFIG.SHOW_EXAMPLES, "string", OPTIONS.YES, {
description: "Show code examples?",
values: [OPTIONS.YES, OPTIONS.NO]
});
registerSelect(CONFIG.SHOW_EXAMPLES);
prefs.definePreference(CONFIG.SEARCH_CODE, "string", OPTIONS.YES, {
description: "Search in the global codes content?",
values: [OPTIONS.YES, OPTIONS.NO]
});
registerSelect(CONFIG.SEARCH_CODE);
prefs.definePreference(CONFIG.AUTO_OPTIONS, "string", OPTIONS.ON_SELECTION_CHANGE, {
description: "When to auto-set options (XML mode) for execution.",
values: [OPTIONS.ON_SELECTION_CHANGE, OPTIONS.ON_EDITOR_CHANGE, OPTIONS.ON_OPEN, OPTIONS.NEVER]
});
registerSelect(CONFIG.AUTO_OPTIONS);
prefs.definePreference(CONFIG.INDENTATION, "number", 4, {
description: "Number of spaces for indentation in the code editor.",
values: [4, 2]
});
registerSelect(CONFIG.INDENTATION);
prefs.definePreference(CONFIG.MENU_APPEARANCE, "string", OPTIONS.SUBMENU, {
description: "How to show commands in the Edit menu. (Requires restart.)",
values: [OPTIONS.SUBMENU, OPTIONS.ITEMS]
});
registerSelect(CONFIG.MENU_APPEARANCE, {
description: "How to show commands in the Edit menu.",
restartRequired: true
});
exports.showUI = function showUI() {
if (!prefs) return null;
const content = getOptionsContent();
const btns = [
{
className: Dialogs.DIALOG_BTN_CLASS_PRIMARY,
id: Dialogs.DIALOG_BTN_OK,
text: "Save"
},
{
className: Dialogs.DIALOG_BTN_CLASS_LEFT,
id: Dialogs.DIALOG_BTN_CANCEL,
text: "Cancel"
}
];
const dialog = Dialogs.showModalDialog(NS, TITLE + ": Options", content, btns);
const $dialog = dialog.getElement();
const form = $dialog.find("form")[0];
$dialog.on("keyup." + NS, "input", function (event) {
if (event.key === "Enter") {
$dialog.find(`[data-button-id="${Dialogs.DIALOG_BTN_OK}"]`).click();
}
});
dialog.done(function (btnId) {
$dialog.off("." + NS);
if (btnId === Dialogs.DIALOG_BTN_OK) {
const formData = new FormData(form);
REGISTERED_OPTIONS.forEach(item => {
if (item.onSave) {
item.onSave(formData);
}
});
}
});
return dialog;
};
exports.onChange = function onChange(prop, fn) {
return prefs?.on("change", prop, () => fn(prefs?.get(prop)));
};
exports.get = function get(prop) {
return prefs?.get(prop);
};
exports.set = function set(prop, value) {
return prefs?.set(prop, value);
};
});