-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmenuWithDelayedInitialization.js
More file actions
56 lines (49 loc) · 1.55 KB
/
menuWithDelayedInitialization.js
File metadata and controls
56 lines (49 loc) · 1.55 KB
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
// https://github.com/Infocatcher/Custom_Buttons/blob/master/code_snippets/menuWithDelayedInitialization.js
// Example menu button for Custom Buttons
// Code for "code" and "initialization" sections (see comments)
// (c) Infocatcher 2013
// version 0.1.1 - 2013-02-08
//== "Code" section:
if(!event.target) { // Button's hotkey pressed
LOG("hotkey");
this.open = true;
return;
}
if(event.target != this) {
LOG("code() from child node");
return;
}
LOG("code()");
var mp = this.mp; // See initialization
addEventListener("command", function(e) {
// Button shouldn't handle "command" event from child nodes
e.stopPropagation();
}, false, mp);
// Create menu contents (just an example):
var df = document.createDocumentFragment();
for(var i = 1; i <= 10; ++i) {
var mi = document.createElement("menuitem");
mi.setAttribute("label", "Item #" + i);
mi.setAttribute("oncommand", "this.parentNode.parentNode.handleCommand(" + i + ");");
df.appendChild(mi);
}
mp.appendChild(df);
this.handleCommand = function(i) {
alert("Command #" + i);
};
//============================
//== "Initialization" section:
LOG("init()");
this.type = "menu";
this.orient = "horizontal";
var mp = this.mp = document.createElement("menupopup");
mp.setAttribute("onpopupshowing", "this.parentNode.initMenuOnce();");
this.appendChild(mp);
this.initMenuOnce = function() {
LOG("onpopupshowing");
delete this.initMenuOnce;
// Looks like Gecko bug: handler can't be removed using removeAttribute()
mp.setAttribute("onpopupshowing", "");
mp.removeAttribute("onpopupshowing");
this.doCommand();
};