This repository has been archived by the owner on Jul 9, 2019. It is now read-only.
/
ui.js
239 lines (208 loc) · 8.23 KB
/
ui.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
PutIO.UI = {
_instantApply : Application.prefs.get("browser.preferences.instantApply"),
/* Functions to deal with property files */
_getStringBundle : function() {
return document.getElementById("putioStringBundle");
},
_getString : function(key) {
let strbundle = this._getStringBundle();
try {
return strbundle.getString(key);
} catch (e) {
return key;
}
},
_getFormattedString : function(key, argv) {
if (typeof(argv) === "undefined" || argv === null || argv.length === 0) {
return this._getString(key);
} else {
let strbundle = this._getStringBundle();
try {
return strbundle.getFormattedString(key,argv);
} catch (e) {
return key;
}
}
},
/* END Functions to deal with property files */
/* Functions for dialogs */
_openDialog : function(url, name, options, isModal) {
if (options.length > 0) {
options += ",";
}
if (null === this._dialog || typeof(this._dialog) === "undefined" || this._dialog.closed) {
this._dialog = window.openDialog(
url,
name,
"centerscreen,chrome,resizable," + options + (isModal ? (this._instantApply.value ? "dialog=no" : "modal") : "")
);
}
this._dialog.focus();
},
/* END Functions for dialogs */
/* Functions for windows and tabs */
// https://developer.mozilla.org/en/Code_snippets/Tabbed_browser#Reusing_tabs
_openAndReuseOneTab : function(url) {
let wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
let browserEnumerator = wm.getEnumerator("navigator:browser");
let encodedUrl = encodeURI(url);
let found, index, browserWin, tabbrowser, currentTab;
for (found = false, index = 0, tabbrowser = browserEnumerator.getNext().gBrowser;
index < tabbrowser.tabContainer.childNodes.length && !found;
index++) {
// Get the next tab
currentTab = tabbrowser.tabContainer.childNodes[index];
// Does this tab contain our custom attribute?
if (currentTab.hasAttribute("putioWindow")) {
// Yes--select it.
tabbrowser.selectedTab = currentTab;
found = true;
break;
}
}
if (!found) {
// Our tab isn't open. Open it now.
browserEnumerator = wm.getEnumerator("navigator:browser");
tabbrowser = browserEnumerator.getNext().gBrowser;
// Create and select tab
let newTab = tabbrowser.addTab(encodedUrl);
newTab.setAttribute("putioWindow", "putio");
tabbrowser.selectedTab = newTab;
}
tabbrowser.ownerDocument.defaultView.focus();
tabbrowser.loadURI(encodedUrl);
},
/* END Functions for windows and tabs */
/* Functions for hiding the contextual menu items */
// https://developer.mozilla.org/en/XUL/PopupGuide/Extensions#Showing_and_hiding_context_menu_items
_putioContextMenupopupShowHidden: function(event) {
let foundAnchorElement = gContextMenu.onLink;
document.getElementById("putioContextMenupopupSaveLink").hidden = !foundAnchorElement;
document.getElementById("putioContextMenupopupSaveLinkSeparator").hidden = !foundAnchorElement;
},
/* END Functions for hiding the contextual menu items */
init : function() {
let that = this;
this._instantApply = Application.prefs.get("browser.preferences.instantApply");
// Save Link from the contextual menu item
document.getElementById("putioContextMenupopupSaveLink").addEventListener("command", function () {
PutIO.BrowserOverlay.saveLink();
}, true);
// Set show/hide setting for contextual menu item
document.getElementById("contentAreaContextMenu").addEventListener("popupshowing", this._putioContextMenupopupShowHidden, false);
// open "Your Files" web page from the contextual menu item
document.getElementById("putioContextMenupopupYourFiles").addEventListener("command", function () {
that.openYourFiles();
}, true);
// open "Active Transfers" web page from the contextual menu item
document.getElementById("putioContextMenupopupActiveTransfers").addEventListener("command", function () {
that.openActiveTransfers();
}, true);
// open Help webpage from the contextual menu item
document.getElementById("putioContextMenupopupHelp").addEventListener("command", function () {
that.openHelp();
}, true);
// open About dialog from the contextual menu item
document.getElementById("putioContextMenupopupAbout").addEventListener("command", function () {
that.openAbout();
}, true);
// open Preferences dialog from the Tools menu item
document.getElementById("putioToolsMenupopupPreferences").addEventListener("command", function () {
that.openPreferences();
}, true);
// open "Your Files" web page from the Tools menu item
document.getElementById("putioToolsMenupopupYourFiles").addEventListener("command", function () {
that.openYourFiles();
}, true);
// open "Active Transfers" web page from the Tools menu item
document.getElementById("putioToolsMenupopupActiveTransfers").addEventListener("command", function () {
that.openActiveTransfers();
}, true);
// open Help webpage from the contextual menu item
document.getElementById("putioToolsMenupopupHelp").addEventListener("command", function () {
that.openHelp();
}, true);
// open About dialog from the Tools menu item
document.getElementById("putioToolsMenupopupAbout").addEventListener("command", function () {
that.openAbout();
}, true);
},
openAbout : function() {
this._openDialog(
"chrome://putio/content/about/about.xul",
"putioAboutWindow",
"",
false
);
},
openPreferences : function() {
this._openDialog(
"chrome://putio/content/preferences/preferences.xul",
"putioPreferencesDialog",
"titlebar,toolbar",
true
);
},
openActiveTransfers : function() {
this._openAndReuseOneTab("https://put.io/transfers");
},
openYourFiles : function() {
this._openAndReuseOneTab("https://put.io/your-files");
},
openHelp : function() {
this._openAndReuseOneTab("https://github.com/yuhui/Put.io-add-on-for-Firefox/wiki/Help");
},
addNotification : function(notificationWindow, labelWithArgv, value, priority, buttons) {
let that = this;
let notificationBox = getNotificationBox(notificationWindow);
let notificationLabel = this._getFormattedString(labelWithArgv[0], labelWithArgv[1]);
let notificationValue = value;
let notificationButtons = new Array();
if (buttons.length > 0) {
let button;
for (let i = 0; i < buttons.length; i++) {
switch (buttons[i]) {
case "active transfers":
button = {
label : this._getString("putio.saveLink.notification.activeTransfersButton.label"),
accessKey : this._getString("putio.saveLink.notification.activeTransfersButton.accessKey"),
popup : null,
callback : function() {
that.openActiveTransfers();
that.removeNotification(notificationWindow);
}
}
break;
case "preferences":
button = {
label : this._getString("putio.preferences.notification.openPreferencesButton.label"),
accessKey : this._getString("putio.preferences.notification.openPreferencesButton.accessKey"),
popup : null,
callback : function() {
that.openPreferences();
}
}
break;
}
notificationButtons[i] = button;
}
}
notificationBox.appendNotification(
notificationLabel,
notificationValue,
"chrome://putio/skin/images/icon_16x16.png",
notificationBox[priority],
notificationButtons
);
},
removeNotification : function(notificationWindow) {
let notificationBox = getNotificationBox(notificationWindow);
if (notificationBox.currentNotification !== null) {
thisNotification = notificationBox.removeCurrentNotification();
}
},
replaceNotification : function(notificationWindow, labelWithArgv, value, priority, buttons) {
this.removeNotification(notificationWindow);
this.addNotification(notificationWindow, labelWithArgv, value, priority, buttons);
}
};