forked from kafene/menuitem
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
328 lines (281 loc) · 8.32 KB
/
index.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
const windowUtils = require("sdk/deprecated/window-utils");
const { Class } = require("sdk/core/heritage");
const { validateOptions } = require("sdk/deprecated/api-utils");
const { on, emit, once, off } = require("sdk/event/core");
const { isBrowser } = require("sdk/window/utils");
const { EventTarget } = require('sdk/event/target');
const menuitemNS = require("sdk/core/namespace").ns();
const { unload } = require('pathfinder/lib/addon/unload');
const NS_XUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
function MenuitemOptions(options) {
return validateOptions(options, {
id: { is: ['string'] },
menuid: { is: ['undefined', 'string'] },
insertbefore: { is: ['undefined', 'string', 'object', 'number'] },
insertafter: { is: ['undefined', 'string', 'object', 'number'] },
separatorbefore: { is: ['undefined', 'boolean', 'string', 'object'] },
separatorafter: { is: ['undefined', 'boolean', 'string', 'object'] },
label: { is: ["string"] },
include: { is: ['string', 'undefined'] },
image: { is: ['string', 'undefined'] },
disabled: { is: ["undefined", "boolean"], map: function(v) !!v},
accesskey: { is: ["undefined", "string"] },
key: { is: ["undefined", "string"] },
checked: { is: ['undefined', 'boolean'] },
className: { is: ["undefined", "string"] },
onCommand: { is: ['undefined', 'function'] },
useChrome: { map: function(v) !!v }
});
}
let Menuitem = Class({
extends: EventTarget,
initialize: function(options) {
options = menuitemNS(this).options = MenuitemOptions(options);
EventTarget.prototype.initialize.call(this, options);
menuitemNS(this).destroyed = false;
menuitemNS(this).unloaders = [];
menuitemNS(this).menuitems = addMenuitems(this, options).menuitems;
},
get id() menuitemNS(this).options.id,
get label() menuitemNS(this).options.label,
set label(val) updateProperty(this, 'label', val),
get image() menuitemNS(this).options.image,
set image(val) updateProperty(this, 'image', val),
get checked() menuitemNS(this).options.checked,
set checked(val) updateProperty(this, 'checked', !!val),
get disabled() menuitemNS(this).options.disabled,
set disabled(val) updateProperty(this, 'disabled', !!val),
get key() menuitemNS(this).options.key,
set key(val) updateProperty(this, 'key', val),
clone: function (overwrites) {
let opts = Object.clone(menuitemNS(this).options);
for (let key in overwrites) {
opts[key] = overwrites[key];
}
return Menuitem(opts);
},
get menuid() menuitemNS(this).options.menuid,
set menuid(val) {
let options = menuitemNS(this).options;
options.menuid = val;
forEachMI(function(menuitem, i, $) {
updateMenuitemParent(menuitem, options, $);
});
},
destroy: function() {
if (!menuitemNS(this).destroyed) {
menuitemNS(this).destroyed = true;
menuitemNS(this).unloaders.forEach(function(u) u());
menuitemNS(this).unloaders = null;
menuitemNS(this).menuitems = null;
}
return true;
}
});
function addMenuitems(self, options) {
let menuitems = [];
// setup window tracker
windowUtils.WindowTracker({
onTrack: function (window) {
if (menuitemNS(self).destroyed) return;
if (options.include) {
if (options.include != window.location) return;
}
else if (!isBrowser(window)) {
return;
}
// add the new menuitem to a menu
var menuitem = updateMenuitemAttributes(
window.document.createElementNS(NS_XUL, "menuitem"), options);
var menuitems_i = menuitems.push(menuitem) - 1;
var sepBefore, sepAfter;
if (options.separatorbefore)
sepBefore = createSeparator(options.separatorbefore, window);
if (options.separatorafter)
sepAfter = createSeparator(options.separatorafter, window);
var insertF = item => updateMenuitemParent(item, options, function(id) window.document.getElementById(id));
insertF(menuitem);
sepBefore && menuitem.parentNode.insertBefore(sepBefore, menuitem);
sepAfter && menuitem.parentNode.insertBefore(sepAfter, menuitem.nextSibling);
menuitem.addEventListener("command", function() {
if (!self.disabled)
emit(self, 'command', options.useChrome ? window : null);
}, true);
// add unloader
let unloader = function unloader() {
let parent = menuitem.parentNode;
if (parent) {
parent.removeChild(menuitem);
sepBefore && parent.removeChild(sepBefore);
sepAfter && parent.removeChild(sepAfter);
}
menuitems[menuitems_i] = null;
};
menuitemNS(self).unloaders.push(function() {
remover();
unloader();
});
let remover = unload(unloader, window);
}
});
return { menuitems: menuitems };
}
/**
* add the menutiem to the ui
* @param menuitem
* @param options
* @param $
* @returns {*}
*/
function updateMenuitemParent(menuitem, options, $)
{
// add the menutiem to the ui
if (!options.menuid)
{
return false;
}
let ids = Array.isArray(options.menuid) ? options.menuid : [options.menuid];
let menuid = null;
for (var len = ids.length, i = 0; i < len; i++)
{
if ($(ids[i]))
{
menuid = $(ids[i]);
}
else
{
continue;
}
if (options.insertafter !== void(0) && tryParent(menuid, menuitem, options.insertafter, true))
{
return true;
}
if (options.insertbefore !== void(0) && tryParent(menuid, menuitem, options.insertbefore, false))
{
return true;
}
}
if (menuid)
{
return tryParent(menuid, menuitem);
}
return false;
}
function updateMenuitemAttributes(menuitem, options) {
menuitem.setAttribute("id", options.id);
menuitem.setAttribute("label", options.label);
if (options.accesskey)
menuitem.setAttribute("accesskey", options.accesskey);
if (options.key)
menuitem.setAttribute("key", options.key);
menuitem.setAttribute("disabled", !!options.disabled);
if (options.image) {
menuitem.classList.add("menuitem-iconic");
menuitem.style.listStyleImage = "url('" + options.image + "')";
}
if (options.checked)
menuitem.setAttribute('checked', options.checked);
if (options.className)
options.className.split(/\s+/).forEach(function(name) menuitem.classList.add(name));
return menuitem;
}
function updateProperty(menuitem, key, val) {
menuitemNS(menuitem).options[key] = val;
forEachMI(function(menuitem) {
menuitem.setAttribute(key, val);
}, menuitem);
return val;
}
function forEachMI(callback, menuitem) {
menuitemNS(menuitem).menuitems.forEach(function(mi, i) {
if (!mi) return;
callback(mi, i, function(id) mi.ownerDocument.getElementById(id));
});
}
/**
*
* @param parent
* @param menuitem
* @param before
* @param insertafter
* @returns {boolean}
*/
function tryParent(parent, menuitem, before, insertafter)
{
if (parent)
{
if (before)
{
var node = null;
if (insertafter)
{
if (node = insertBefore(parent, before))
{
node = node.nextSibling;
}
}
else
{
node = insertBefore(parent, before);
}
if (node)
{
parent.insertBefore(menuitem, node);
return true;
}
else if (insertafter === false || insertafter === true)
{
return false;
}
}
parent.appendChild(menuitem);
return true;
}
return false;
}
function insertBefore(parent, insertBefore) {
if (typeof insertBefore == "number") {
switch (insertBefore) {
case MenuitemExport.FIRST_CHILD:
return parent.firstChild;
}
return null;
}
else if (typeof insertBefore == "string") {
return parent.querySelector("#" + insertBefore);
}
return insertBefore;
}
/**
*
* @param data - ['undefined', 'boolean', 'string', 'object']
* @param window
* @returns {*}
*/
function createSeparator(data, window)
{
let elem;
if (typeof data === 'object')
{
elem = data;
}
else
{
elem = window.document.createElementNS(NS_XUL, "menuseparator");
}
if (typeof data === 'string')
{
elem.id = data;
}
return elem;
}
function MenuitemExport(options) {
return Menuitem(options);
}
MenuitemExport.FIRST_CHILD = 1;
exports.Menuitem = MenuitemExport;
exports.createSeparator = createSeparator;