-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathPanelSubMenu.uc.js
163 lines (138 loc) · 6.23 KB
/
PanelSubMenu.uc.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
(function(){
var toolbarbuttons = [
{
label: '测试菜单1',
oncommand: '',
}
];
// 来自 User Agent Overrider 扩展
const ToolbarManager = (function() {
/**
* Remember the button position.
* This function Modity from addon-sdk file lib/sdk/widget.js, and
* function BrowserWindow.prototype._insertNodeInToolbar
*/
let layoutWidget = function(document, button, isFirstRun) {
// Add to the customization palette
let toolbox = document.getElementById('navigator-toolbox');
toolbox.palette.appendChild(button);
// Search for widget toolbar by reading toolbar's currentset attribute
let container = null;
let toolbars = document.getElementsByTagName('toolbar');
let id = button.getAttribute('id');
for (let i = 0; i < toolbars.length; i += 1) {
let toolbar = toolbars[i];
if (toolbar.getAttribute('currentset').indexOf(id) !== -1) {
container = toolbar;
}
}
// if widget isn't in any toolbar, default add it next to searchbar
if (!container) {
if (isFirstRun) {
container = document.getElementById('nav-bar');
} else {
return;
}
}
// Now retrieve a reference to the next toolbar item
// by reading currentset attribute on the toolbar
let nextNode = null;
let currentSet = container.getAttribute('currentset');
let ids = (currentSet === '__empty') ? [] : currentSet.split(',');
let idx = ids.indexOf(id);
if (idx !== -1) {
for (let i = idx; i < ids.length; i += 1) {
nextNode = document.getElementById(ids[i]);
if (nextNode) {
break;
}
}
}
// Finally insert our widget in the right toolbar and in the right position
container.insertItem(id, nextNode, null, false);
// Update DOM in order to save position
// in this toolbar. But only do this the first time we add it to the toolbar
if (ids.indexOf(id) === -1) {
container.setAttribute('currentset', container.currentSet);
document.persist(container.id, 'currentset');
}
};
let addWidget = function(window, widget, isFirstRun) {
try {
layoutWidget(window.document, widget, isFirstRun);
} catch(error) {
trace(error);
}
};
let removeWidget = function(window, widgetId) {
try {
let widget = window.document.getElementById(widgetId);
widget.parentNode.removeChild(widget);
} catch(error) {
trace(error);
}
};
let exports = {
addWidget: addWidget,
removeWidget: removeWidget,
};
return exports;
})();
window.PanelCustomButton1 = {
customViewID: 'PanelUI-customView',
init: function() {
let button = $C('toolbarbutton', {
id: 'mPanelSubMenu-button1',
class: 'toolbarbutton-1 chromeclass-toolbar-additional',
label: '测试按钮',
"cui-areatype": "menu-panel",
"cui-anchorid": "PanelUI-menu-button",
tooltiptext: '',
oncommand: 'PanelCustomButton1.showSubView(this);',
image: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAACYElEQVRIid3Vy04aYRjGcdfehHt3X+Z+vI4mTWzSRVMV5CAICiJyFBSlakUQz4gowwxCQySxFcUiVGk1iIOjw+OmmYRqPr+0u77Jf/v8lm9X1391odgXzEbzmFnJI7icQ+DzIaaXsvAvivAtiPCGBXjmM3DP8XCFeEzNpOEMHsAZ2IdjOgWHPwVCSA/Hcd0vArPRPP684tXrHV0BR5eA3bsHQkgfIaTnRWAmknsGhLO/mCpctmHzJMFxXD8hpPdFILh8+AwIZX8yla+1Me7apQPTS1koitIBBMQ6U9magrGpBB3wL2Yhy3IH4BOvmBKqCqyTO3TA90mEJEkdgEv8wRRffYTFsU0HvGEBjUajA3AINab2Lx5gtm/RAc9cBtfX1x2ATagylazIMNk26YA7xKNer6vj7TZgES6YSlRkjIxv0AHXbBq1Wk0dVxTAJFSY2jq/h9H6CuAMHqBSqaDdBh4fgYcHQJ/5ztTGeQsGyzodmAzso1wuq+OyDGgz50zFyy3ozWt0wOFPoVQqqeP398BA5oypWFmCzhSnAxO+PRwfH0OWgVYLkCTgA3/KVORMgta4SgfsniSKxaI6fncHvOdLTC2d3kFjiNEBm3sXhUIBkgQ0m8DtLfCOP2FqodTEkC5KB8amEsjlcup4owG8TX9jav6kicHhFTpgdSYgiqI6fnMDvEl/ZSp0cosBTYQOWCZ3MDqxDbN9CybbJkbGN2G0bsBgWYd+dA06cxzDpji0xlVoDDFo9FEM6aIY1K5gQBPBx6FlOkAI6SGE9HEc1/+3UV8mx3Hdv5Hef6jj6T8BraHEXgQsuRQAAAAASUVORK5CYII='
});
ToolbarManager.addWidget(window, button, false);
this.addSubView();
},
addSubView: function() {
let panelView = $C('panelview', {
id: PanelCustomButton1.customViewID,
flex: 1,
class: 'PanelUI-subview'
});
panelView.appendChild($C('label', {
value: '测试',
class: 'panel-subview-header'
}));
let vbox = $C('vbox', {
id: 'PanelUI-customItems',
class: 'panel-subview-body'
});
try {
for (let button of toolbarbuttons) {
vbox.appendChild($C('toolbarbutton', button));
}
} catch (e) {
// console.error('添加按钮错误', e);
}
panelView.appendChild(vbox);
$('PanelUI-multiView').appendChild(panelView);
},
showSubView: function(aAnchor) {
let popup = $('PanelUI-popup');
popup.addEventListener('popuphiding', function(event){
event.preventDefault();
event.stopPropagation();
popup.removeEventListener('popuphiding', arguments.callee, false);
}, false);
PanelUI.multiView.showSubView(PanelCustomButton1.customViewID, aAnchor);
},
};
PanelCustomButton1.init();
function $(id) document.getElementById(id);
function $C(name, attr) {
var el = document.createElement(name);
if (attr) Object.keys(attr).forEach(function(n) el.setAttribute(n, attr[n]));
return el;
}
})()