/
Filefield.js
79 lines (70 loc) · 2.33 KB
/
Filefield.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
class FirefoxFilefield extends FirefoxBasetext {
connectedCallback() {
super.connectedCallback()
this.innerHTML = `
<xul:stringbundle anonid="bundle" src="chrome://global/locale/filefield.properties"></xul:stringbundle>
<xul:hbox class="fileFieldContentBox" align="center" flex="1" inherits="disabled">
<xul:image class="fileFieldIcon" inherits="src=image,disabled"></xul:image>
<xul:textbox class="fileFieldLabel" inherits="value=label,disabled,accesskey,tabindex,aria-labelledby" flex="1" readonly="true"></xul:textbox>
</xul:hbox>
`;
this._file = null;
this._setupEventListeners();
}
set label(val) {
this.setAttribute("label", val);
var elt = document.getAnonymousElementByAttribute(this, "class", "fileFieldLabel");
return (elt.value = val);
}
get label() {
return this.getAttribute('label');
}
set file(val) {
this._file = val;
if (val) {
this.image = this._getIconURLForFile(val);
this.label = this._getDisplayNameForFile(val);
} else {
this.removeAttribute("image");
var bundle = document.getAnonymousElementByAttribute(this, "anonid", "bundle");
this.label = bundle.getString("downloadHelperNoneSelected");
}
return val;
}
get file() {
return this._file
}
_getDisplayNameForFile(aFile) {
if (/Win/.test(navigator.platform)) {
var lfw = aFile.QueryInterface(Ci.nsILocalFileWin);
try {
return lfw.getVersionInfoField("FileDescription");
} catch (e) {
// fall through to the filename
}
} else if (/Mac/.test(navigator.platform)) {
var lfm = aFile.QueryInterface(Ci.nsILocalFileMac);
try {
return lfm.bundleDisplayName;
} catch (e) {
// fall through to the file name
}
}
var ios = Cc["@mozilla.org/network/io-service;1"]
.getService(Ci.nsIIOService);
var url = ios.newFileURI(aFile).QueryInterface(Ci.nsIURL);
return url.fileName;
}
_getIconURLForFile(aFile) {
if (!aFile)
return "";
var ios = Cc["@mozilla.org/network/io-service;1"]
.getService(Ci.nsIIOService);
var fph = ios.getProtocolHandler("file")
.QueryInterface(Ci.nsIFileProtocolHandler);
var urlspec = fph.getURLSpecFromFile(aFile);
return "moz-icon://" + urlspec + "?size=16";
}
_setupEventListeners() {
}
}