Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/webroot/rsrc/js/core/FileUpload.js
12241 views
1
/**
2
* @requires javelin-install
3
* javelin-dom
4
* phabricator-notification
5
* @provides phabricator-file-upload
6
* @javelin
7
*/
8
9
JX.install('PhabricatorFileUpload', {
10
11
construct : function() {
12
this._notification = new JX.Notification();
13
},
14
15
properties : {
16
name: null,
17
totalBytes: null,
18
uploadedBytes: null,
19
rawFileObject: null,
20
allocatedPHID: null,
21
ID: null,
22
PHID: null,
23
URI: null,
24
status: null,
25
markup: null,
26
targetNode: null,
27
error: null
28
},
29
30
members : {
31
_notification : null,
32
_chunks: null,
33
_isResume: false,
34
35
addUploadedBytes: function(bytes) {
36
var uploaded = this.getUploadedBytes();
37
this.setUploadedBytes(uploaded + bytes);
38
return this;
39
},
40
41
setChunks: function(chunks) {
42
var chunk;
43
for (var ii = 0; ii < chunks.length; ii++) {
44
chunk = chunks[ii];
45
if (chunk.complete) {
46
this.addUploadedBytes(chunk.byteEnd - chunk.byteStart);
47
this._isResume = true;
48
}
49
}
50
51
this._chunks = chunks;
52
53
return this;
54
},
55
56
getChunks: function() {
57
return this._chunks;
58
},
59
60
getRemainingChunks: function() {
61
var chunks = this.getChunks();
62
63
var result = [];
64
for (var ii = 0; ii < chunks.length; ii++) {
65
if (!chunks[ii].complete) {
66
result.push(chunks[ii]);
67
}
68
}
69
70
return result;
71
},
72
73
didCompleteChunk: function(chunk) {
74
var chunks = this.getRemainingChunks();
75
for (var ii = 0; ii < chunks.length; ii++) {
76
if (chunks[ii].byteStart == chunk.byteStart) {
77
if (chunks[ii].byteEnd == chunk.byteEnd) {
78
if (!chunks[ii].complete) {
79
chunks[ii].complete = true;
80
}
81
break;
82
}
83
}
84
}
85
86
return this;
87
},
88
89
update : function() {
90
if (!this._notification) {
91
return;
92
}
93
94
this._notification
95
.setDuration(0)
96
.show();
97
98
var content;
99
100
// TODO: This stuff needs some work for translations.
101
102
switch (this.getStatus()) {
103
case 'done':
104
var link = JX.$N('a', {href: this.getURI()}, 'F' + this.getID());
105
106
content = [
107
JX.$N('strong', {}, ['Upload Complete (', link, ')']),
108
JX.$N('br'),
109
this.getName()
110
];
111
112
this._notification
113
.setContent(content)
114
.alterClassName('jx-notification-done', true)
115
.setDuration(12000);
116
this._notification = null;
117
break;
118
case 'error':
119
content = [
120
JX.$N('strong', {}, 'Upload Failure'),
121
JX.$N('br'),
122
this.getName(),
123
JX.$N('br'),
124
JX.$N('br'),
125
this.getError()
126
];
127
128
this._notification
129
.setContent(content)
130
.alterClassName('jx-notification-error', true);
131
this._notification = null;
132
break;
133
case 'allocate':
134
content = 'Allocating "' + this.getName() + '"...';
135
this._notification
136
.setContent(content);
137
break;
138
case 'chunks':
139
content = 'Loading chunks for "' + this.getName() + '"...';
140
this._notification
141
.setContent(content);
142
break;
143
default:
144
var info = '';
145
if (this.getTotalBytes()) {
146
var p = this._renderPercentComplete();
147
var f = this._renderFileSize();
148
info = p + ' of ' + f;
149
}
150
151
var head;
152
if (this._isResume) {
153
head = 'Resuming:';
154
} else if (this._chunks) {
155
head = 'Uploading chunks:';
156
} else {
157
head = 'Uploading:';
158
}
159
160
info = [
161
JX.$N('strong', {}, this.getName()),
162
JX.$N('br'),
163
head + ' ' + info];
164
165
this._notification
166
.setContent(info);
167
break;
168
}
169
170
return this;
171
},
172
_renderPercentComplete : function() {
173
if (!this.getTotalBytes()) {
174
return null;
175
}
176
var ratio = this.getUploadedBytes() / this.getTotalBytes();
177
return parseInt(100 * ratio, 10) + '%';
178
},
179
_renderFileSize : function() {
180
if (!this.getTotalBytes()) {
181
return null;
182
}
183
184
var s = 3;
185
var n = this.getTotalBytes();
186
while (s && n >= 1000) {
187
n = Math.round(n / 100);
188
n = n / 10;
189
s--;
190
}
191
192
s = ['GB', 'MB', 'KB', 'bytes'][s];
193
return n + ' ' + s;
194
}
195
}
196
197
});
198
199