Path: blob/master/webroot/rsrc/js/core/FileUpload.js
12241 views
/**1* @requires javelin-install2* javelin-dom3* phabricator-notification4* @provides phabricator-file-upload5* @javelin6*/78JX.install('PhabricatorFileUpload', {910construct : function() {11this._notification = new JX.Notification();12},1314properties : {15name: null,16totalBytes: null,17uploadedBytes: null,18rawFileObject: null,19allocatedPHID: null,20ID: null,21PHID: null,22URI: null,23status: null,24markup: null,25targetNode: null,26error: null27},2829members : {30_notification : null,31_chunks: null,32_isResume: false,3334addUploadedBytes: function(bytes) {35var uploaded = this.getUploadedBytes();36this.setUploadedBytes(uploaded + bytes);37return this;38},3940setChunks: function(chunks) {41var chunk;42for (var ii = 0; ii < chunks.length; ii++) {43chunk = chunks[ii];44if (chunk.complete) {45this.addUploadedBytes(chunk.byteEnd - chunk.byteStart);46this._isResume = true;47}48}4950this._chunks = chunks;5152return this;53},5455getChunks: function() {56return this._chunks;57},5859getRemainingChunks: function() {60var chunks = this.getChunks();6162var result = [];63for (var ii = 0; ii < chunks.length; ii++) {64if (!chunks[ii].complete) {65result.push(chunks[ii]);66}67}6869return result;70},7172didCompleteChunk: function(chunk) {73var chunks = this.getRemainingChunks();74for (var ii = 0; ii < chunks.length; ii++) {75if (chunks[ii].byteStart == chunk.byteStart) {76if (chunks[ii].byteEnd == chunk.byteEnd) {77if (!chunks[ii].complete) {78chunks[ii].complete = true;79}80break;81}82}83}8485return this;86},8788update : function() {89if (!this._notification) {90return;91}9293this._notification94.setDuration(0)95.show();9697var content;9899// TODO: This stuff needs some work for translations.100101switch (this.getStatus()) {102case 'done':103var link = JX.$N('a', {href: this.getURI()}, 'F' + this.getID());104105content = [106JX.$N('strong', {}, ['Upload Complete (', link, ')']),107JX.$N('br'),108this.getName()109];110111this._notification112.setContent(content)113.alterClassName('jx-notification-done', true)114.setDuration(12000);115this._notification = null;116break;117case 'error':118content = [119JX.$N('strong', {}, 'Upload Failure'),120JX.$N('br'),121this.getName(),122JX.$N('br'),123JX.$N('br'),124this.getError()125];126127this._notification128.setContent(content)129.alterClassName('jx-notification-error', true);130this._notification = null;131break;132case 'allocate':133content = 'Allocating "' + this.getName() + '"...';134this._notification135.setContent(content);136break;137case 'chunks':138content = 'Loading chunks for "' + this.getName() + '"...';139this._notification140.setContent(content);141break;142default:143var info = '';144if (this.getTotalBytes()) {145var p = this._renderPercentComplete();146var f = this._renderFileSize();147info = p + ' of ' + f;148}149150var head;151if (this._isResume) {152head = 'Resuming:';153} else if (this._chunks) {154head = 'Uploading chunks:';155} else {156head = 'Uploading:';157}158159info = [160JX.$N('strong', {}, this.getName()),161JX.$N('br'),162head + ' ' + info];163164this._notification165.setContent(info);166break;167}168169return this;170},171_renderPercentComplete : function() {172if (!this.getTotalBytes()) {173return null;174}175var ratio = this.getUploadedBytes() / this.getTotalBytes();176return parseInt(100 * ratio, 10) + '%';177},178_renderFileSize : function() {179if (!this.getTotalBytes()) {180return null;181}182183var s = 3;184var n = this.getTotalBytes();185while (s && n >= 1000) {186n = Math.round(n / 100);187n = n / 10;188s--;189}190191s = ['GB', 'MB', 'KB', 'bytes'][s];192return n + ' ' + s;193}194}195196});197198199