Debian Codegraph @

browserpass-extension/src/options/interface.js

raw
1 module.exports = Interface;
2
3 const m = require("mithril");
4
5 /**
6 * Options main interface
7 *
8 * @since 3.0.0
9 *
10 * @param object settings Settings object
11 * @param function saveSettings Function to save settings
12 * @param function clearUsageData Function to clear usage data
13 * @return void
14 */
15 function Interface(settings, saveSettings, clearUsageData) {
16 // public methods
17 this.attach = attach;
18 this.view = view;
19
20 // fields
21 this.settings = settings;
22 this.saveSettings = saveSettings;
23 this.saveEnabled = false;
24 this.clearUsageData = clearUsageData;
25 }
26
27 /**
28 * Attach the interface on the given element
29 *
30 * @since 3.0.0
31 *
32 * @param DOMElement element Target element
33 * @return void
34 */
35 function attach(element) {
36 m.mount(element, this);
37 }
38
39 /**
40 * Generates vnodes for render
41 *
42 * @since 3.0.0
43 *
44 * @param function ctl Controller
45 * @param object params Runtime params
46 * @return []Vnode
47 */
48 function view(ctl, params) {
49 var nodes = [];
50 nodes.push(m("h3", "Basic settings"));
51 nodes.push(
52 createCheckbox.call(
53 this,
54 "autoSubmit",
55 "Automatically submit forms after filling (not recommended)"
56 )
57 );
58 nodes.push(
59 createCheckbox.call(this, "enableOTP", "Enable support for OTP tokens (not recommended)")
60 );
61 nodes.push(createInput.call(this, "username", "Default username", "john.smith"));
62 nodes.push(createInput.call(this, "gpgPath", "Custom gpg binary", "/path/to/gpg"));
63
64 nodes.push(m("h3", "Theme"));
65 nodes.push(
66 createDropdown.call(this, "theme", [
67 m("option", { value: "dark" }, "Dark"),
68 m("option", { value: "light" }, "Light"),
69 ])
70 );
71
72 nodes.push(m("h3", "Custom store locations"));
73 nodes.push(
74 m("div", { class: "notice" }, "(this overrides default store and $PASSWORD_STORE_DIR)")
75 );
76 for (var storeId in this.settings.stores) {
77 nodes.push(createCustomStore.call(this, storeId));
78 }
79 nodes.push(
80 m(
81 "a.add-store",
82 {
83 onclick: () => {
84 addEmptyStore(this.settings.stores);
85 this.saveEnabled = true;
86 },
87 },
88 "Add store"
89 )
90 );
91
92 if (typeof this.error !== "undefined") {
93 nodes.push(m("div.error", this.error.message));
94 }
95 if (this.settings.hasOwnProperty("hostError")) {
96 let hostError = this.settings.hostError;
97 nodes.push(m("div.error", hostError.params.message));
98 }
99
100 nodes.push(
101 m(
102 "button.save",
103 {
104 disabled: !this.saveEnabled,
105 onclick: async () => {
106 try {
107 this.settings = await this.saveSettings(this.settings);
108 this.error = undefined;
109 } catch (e) {
110 this.error = e;
111 }
112 this.saveEnabled = false;
113 m.redraw();
114 },
115 },
116 "Save"
117 )
118 );
119
120 nodes.push(
121 m(
122 "button.clearUsageData",
123 {
124 onclick: async () => {
125 try {
126 await this.clearUsageData();
127 this.error = undefined;
128 } catch (e) {
129 this.error = e;
130 }
131 m.redraw();
132 },
133 },
134 "Clear usage data"
135 )
136 );
137 return nodes;
138 }
139
140 /**
141 * Generates vnode for a input setting
142 *
143 * @since 3.0.0
144 *
145 * @param string key Settings key
146 * @param string title Settings title
147 * @param string placeholder Settings placeholder
148 * @return Vnode
149 */
150 function createInput(key, title, placeholder) {
151 return m("div.option", { class: key }, [
152 m("label", [
153 title,
154 m("input[type=text]", {
155 value: this.settings[key],
156 placeholder: placeholder,
157 onchange: (e) => {
158 this.settings[key] = e.target.value;
159 this.saveEnabled = true;
160 },
161 }),
162 ]),
163 ]);
164 }
165
166 /**
167 * Generates vnode for a dropdown setting
168 *
169 * @since 3.3.1
170 *
171 * @param string key Settings key
172 * @param array options Array of objects with value and text fields
173 * @return Vnode
174 */
175 function createDropdown(key, options) {
176 return m(
177 "select",
178 {
179 value: this.settings[key],
180 onchange: (e) => {
181 this.settings[key] = e.target.value;
182 this.saveEnabled = true;
183 },
184 },
185 options
186 );
187 }
188
189 /**
190 * Generates vnode for a checkbox setting
191 *
192 * @since 3.0.0
193 *
194 * @param string key Settings key
195 * @param string title Label for the checkbox
196 * @return Vnode
197 */
198 function createCheckbox(key, title) {
199 return m("div.option", { class: key }, [
200 m("label", [
201 m("input[type=checkbox]", {
202 title: title,
203 checked: this.settings[key],
204 onchange: (e) => {
205 this.settings[key] = e.target.checked;
206 this.saveEnabled = true;
207 },
208 }),
209 title,
210 ]),
211 ]);
212 }
213
214 /**
215 * Generates vnode for a custom store configuration
216 *
217 * @since 3.0.0
218 *
219 * @param string storeId Store ID
220 * @return Vnode
221 */
222 function createCustomStore(storeId) {
223 let store = this.settings.stores[storeId];
224
225 return m("div.option.custom-store", { class: "store-" + store.name }, [
226 m("input[type=text].name", {
227 title: "The name for this password store",
228 value: store.name,
229 placeholder: "name",
230 onchange: (e) => {
231 store.name = e.target.value;
232 this.saveEnabled = true;
233 },
234 }),
235 m("input[type=text].path", {
236 title: "The full path to this password store",
237 value: store.path,
238 placeholder: "/path/to/store",
239 onchange: (e) => {
240 store.path = e.target.value;
241 this.saveEnabled = true;
242 },
243 }),
244 m("input[type=text].bgColor", {
245 title: "Badge background color",
246 value: store.bgColor,
247 placeholder: "#626262",
248 onchange: (e) => {
249 store.bgColor = e.target.value;
250 this.saveEnabled = true;
251 },
252 }),
253 m("input[type=text].color", {
254 title: "Badge text color",
255 value: store.color,
256 placeholder: "#c4c4c4",
257 onchange: (e) => {
258 store.color = e.target.value;
259 this.saveEnabled = true;
260 },
261 }),
262 m(
263 "a.remove",
264 {
265 title: "Remove this password store",
266 onclick: () => {
267 delete this.settings.stores[storeId];
268 this.saveEnabled = true;
269 },
270 },
271 "[X]"
272 ),
273 ]);
274 }
275
276 /**
277 * Generates new store ID
278 *
279 * @since 3.0.0
280 *
281 * @return string new store ID
282 */
283 function newId() {
284 return Math.random().toString(36).substr(2, 9);
285 }
286
287 /**
288 * Generates a new empty store
289 *
290 * @since 3.0.0
291 *
292 * @param []object stores List of stores to add a new store to
293 * @return void
294 */
295 function addEmptyStore(stores) {
296 let store = { id: newId(), name: "", path: "" };
297 stores[store.id] = store;
298 }