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
|
/* 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 { unload } = require("unload+");
const menuitemNS = require("sdk/core/namespace").ns();
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'] },
label: { is: ["string"] },
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'] }
});
}
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 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] = ovrewrites[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 (!isBrowser(window) || menuitemNS(self).destroyed) 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;
// add the menutiem to the ui
updateMenuitemParent(menuitem, options, function(id) window.document.getElementById(id));
menuitem.addEventListener("command", function() {
if (!self.disabled)
emit(self, 'command');
}, true);
// add unloader
let unloader = function unloader() {
menuitem.parentNode && menuitem.parentNode.removeChild(menuitem);
menuitems[menuitems_i] = null;
};
menuitemNS(self).unloaders.push(function() {
remover();
unloader();
});
let remover = unload(unloader, window);
}
});
return {menuitems: menuitems};
}
function updateMenuitemParent(menuitem, options, $) {
// add the menutiem to the ui
if (Array.isArray(options.menuid)) {
let ids = options.menuid;
for (var len = ids.length, i = 0; i < len; i++) {
if (tryParent($(ids[i]), menuitem, options.insertbefore))
return true;
}
}
else {
return tryParent($(options.menuid), menuitem, options.insertbefore);
}
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));
});
}
function tryParent(parent, menuitem, before) {
if (parent) parent.insertBefore(menuitem, insertBefore(parent, before));
return !!parent;
}
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;
}
function MenuitemExport(options) {
return Menuitem(options);
}
MenuitemExport.FIRST_CHILD = 1;
exports.Menuitem = MenuitemExport;
|