-
-
Notifications
You must be signed in to change notification settings - Fork 165
/
menu.js
330 lines (319 loc) · 8.57 KB
/
menu.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
* **** Create a modal menu system to allow customization of values ****
*/
/**
* Userscript menu options
* @typedef ghMenu~options
* @type {Object[]}
* @property {String} name - Option label
* @property {String} type - Option input type, e.g. "text", "number" or
* "checkbox"
* @property {Function} get - Option get value function
* @property {Function} set - Option set value function
* @property {Object[]} options -
*/
/* Add menu modal
* Example set up
ghMenu.open(
"Popup Title",
[{
name: "Title",
type: "text",
get: () => GM_getValue("title"),
set: value => GM_setValue("title", value)
}, {
name: "Border width (px)",
type: "number",
get: () => GM_getValue("border-width"),
set: value => GM_setValue("border-width", value)
}, {
name: "Is enabled?",
type: "checkbox",
get: () => GM_getValue("enabled"),
set: value => GM_setValue("enabled", value)
}, {
name: "Background Color",
type: "color",
get: () => GM_getValue("bkg-color"),
set: value => GM_setValue("bkg-color", value)
}, {
name: "Widget enabled",
type: "checkbox",
get: () => GM_getValue("widget-is-enabled"),
set: value => GM_setValue("widget-is-enabled", value)
}, {
name: "Image choice",
type: "select",
get: () => GM_getValue("img-choice"),
set: value => GM_setValue("img-choice", value),
options: [
{ label: "Car", value: "/images/car.jpg" },
{ label: "Jet", value: "/images/jet.jpg" },
{ label: "Cat", value: "/images/cat.jpg" }
]
}]
);
*/
const ghMenu = {
/**
* Initialize menu - adds styling
*/
init: () => {
if (!$("#ghmenu-style")) {
make({
el: "style",
id: "ghmenu-style",
textContent: `
#ghmenu, #ghmenu summary { cursor: default; }
#ghmenu summary:before { cursor: pointer; }
#ghmenu-inner input[type="color"] { border: 0; padding: 0 }
#ghmenu-inner ::-webkit-color-swatch-wrapper { border: 0; padding: 0; }
#ghmenu-inner ::-moz-color-swatch-wrapper { border: 0; padding: 0; }
}
`,
appendTo: "body"
});
}
},
/**
* Open the menu modal
* @param {String} title - Modal menu header text
* @param {ghMenu~options} options - Array of options to include in the modal
*/
open: (title, options) => {
if (!$("#ghmenu")) {
ghMenu._createMenu(title);
ghMenu._options = options;
}
ghMenu._title = title;
ghMenu._addContent(options);
},
/**
* Close menu modal
* @param {MouseEvent|KeyboardEvent} event
*/
close: event => {
if (event) {
event.preventDefault();
}
const menu = $("#ghmenu");
if (menu) {
menu.remove();
}
},
/**
* Append more options to an open modal menu, or open a modal menu
* @param {ghMenu~options} options
*/
append: options => {
const menu = $("#ghmenu");
if (menu) {
ghMenu._appendContent(options);
} else {
ghMenu.open("", options);
}
},
/**
* Refresh modal menu
*/
refresh: () => {
ghMenu._addContent(ghMenu._options);
},
/**
* form types to add to menu:
* - text, number, checkbox, color or radio (WIP) input
* - select (WIP)
* - group (WIP)
*/
_types: {
_input: (type, eventType, opts) => {
const elm = make({
el: "input",
id: `${opts.id}-input`,
className: `ghmenu-${type} ${type === "checkbox"
? "m-2"
: "form-control input-block width-full"
}`,
attrs: {
type,
value: opts.get()
},
});
const handler = e => opts.set(type === "checkbox"
? e.target.checked
: e.target.value
);
on(elm, eventType, handler);
return elm;
},
text: opts => ghMenu._types._input("text", "input", opts),
number: opts => ghMenu._types._input("number", "input", opts),
checkbox: opts => ghMenu._types._input("checkbox", "change", opts),
color: opts => ghMenu._types._input("color", "change", opts),
radio: opts => {}, // TO DO
select: opts => {
const elm = make({
el: "select",
className: "width-full ghmenu-select",
attrs: {
value: opts.get()
}
}, opts.options.map(obj => (
make({
el: "option",
text: obj.label,
attrs: {
value: obj.value
}
})
)));
on(elm, "change", e => opts.set(e.target.value));
return elm;
},
/* TO DO
* - add multiple?
* colors: ['#000', '#fff']
* guideline: { width: '.2', color: '#a00', chars: 80 }
* - link to more details/docs?
*/
group: opts => {
const group = opts.group;
if (Array.isArray(group) && group.length) {
const fragment = document.createDocumentFragment();
fragment.appendChild(make({ el: "strong", text: opts.name }));
group.forEach(entry => {
const row = make({
className: "Box-row d-flex flex-row pr-0"
}, [
ghMenu._createLabel(entry.id, entry.name),
make({
id,
className: `ml-2 no-wrap${
// align checkbox to right edge
opt.type === "checkbox" ? " d-flex flex-justify-end" : ""
}`,
})
])
})
}
},
},
_options: [],
/**
* Create modal menu
*/
_createMenu: () => {
make({
el: "details",
id: "ghmenu",
className: "details-reset details-overlay details-overlay-dark lh-default text-gray-dark",
attrs: {
open: true
},
html: `
<summary role="button" aria-label="Close dialog" />
<details-dialog
id="ghmenu-dialog"
class="Box Box--overlay d-flex flex-column anim-fade-in fast container-xl"
role="dialog"
aria-modal="true"
tab-index="-1"
>
<div class="readability-extra d-flex flex-auto flex-column overflow-hidden">
<div class="Box-header">
<h2 id="ghmenu-title" class="Box-title"></h2>
</div>
<div class="Box-body p-0 overflow-scroll">
<div class="container-lg p-responsive advanced-search-form">
<fieldset id="ghmenu-inner" class="pb-2 mb-2 min-width-0" />
</div>
</div>
</div>
<button id="ghmenu-close-menu" class="Box-btn-octicon m-0 btn-octicon position-absolute right-0 top-0" type="button" aria-label="Close dialog" data-close-dialog="">
<svg class="octicon octicon-x" viewBox="0 0 12 16" version="1.1" width="12" height="16" aria-hidden="true">
<path fill-rule="evenodd" d="M7.48 8l3.75 3.75-1.48 1.48L6 9.48l-3.75 3.75-1.48-1.48L4.52 8 .77 4.25l1.48-1.48L6 6.52l3.75-3.75 1.48 1.48L7.48 8z" />
</svg>
</button>
</details-dialog>`,
appendTo: "body"
});
on($("#ghmenu-close-menu"), "click", e => ghMenu.close(e), { once: true });
on($("#ghmenu summary"), "click", e => {
e.preventDefault();
e.stopPropagation();
const target = e.target;
if (target && !target.closest("#ghmenu-dialog")) {
ghMenu.close(e);
}
});
},
/**
* Create input or select label
* @param {String} id - Unique ID to match up input & label
* @param {String} text - Input label text
* @example `<dt><label for="{ID}-input">{NAME}</label></dt>`
* @returns {HTMLElement}
*/
_createLabel: (id, text) =>
make({ el: "dt" }, [
make({
el: "label",
className: "flex-auto",
text,
attrs: {
for: `${id}-input`
}
})
]),
/**
* Add content to menu modal (clears out existing content)
* @param {ghMenu~options} options
*/
_addContent: options => {
const menu = $("#ghmenu-inner");
if (menu) {
menu.innerHTML = "";
ghMenu._appendContent(options);
}
},
/**
* Append content to menu modal (does not clear out existing content)
* @param {ghMenu~options} options
*/
_appendContent: options => {
const container = $("#ghmenu-inner");
if (container) {
// update title, if needed
$("#ghmenu-title").textContent = ghMenu._title;
const fragment = document.createDocumentFragment();
options.forEach((opt, indx) => {
const id = `ghmenu-${opt.name.replace(/\s/g, "")}-${indx}`;
const output = opt.type === "group"
? ghMenu._types.group({ ...opt, id })
: make({
el: "dl",
className: "form-group flattened d-flex d-md-block flex-column border-bottom my-0 py-2",
}, [
ghMenu._createLabel(id, opt.name),
make({
el: "dd",
id,
className: opt.type === "checkbox"
? "d-flex flex-justify-end"
: "",
}, [
ghMenu._types[opt.type || "text"]({ ...opt, id })
])
]);
fragment.appendChild(output);
});
container.appendChild(fragment);
}
}
};
/*
TESTING
ghMenu.init();
ghMenu.open('Testing',[{name:'test color',type:'color',get:()=>'#ddd',set:v=>console.log('setting to',v)},{name:'test number',type:'checkbox',get:()=>true,set:v=>console.log('setting cb to',v)}]);
ghMenu.append([{name:"Image choice",type:"select",get:()=>"Car",set:v=>console.log("img-choice", v),options:[{label:"Car",value:"/images/car.jpg"},{label:"Jet",value:"/images/jet.jpg"},{label:"Cat",value:"/images/cat.jpg"}]}]);
*/