Skip to content
This repository has been archived by the owner on Nov 20, 2018. It is now read-only.

feature: adding setStatus to the public api list #1739

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ bin/
src

npm-debug.log
Vagrantfile

test/dev/handlers/s3/composer.lock
test/dev/handlers/traditional/files
Expand Down
69 changes: 53 additions & 16 deletions client/js/uploader.basic.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,28 @@
return this._uploadData.uuidChanged(id, newUuid);
},

/**
* Expose the internal status of a file id to the public api for manual state changes
* @public
*
* @param {Number} id,
* @param {String} newStatus
*
* @todo Implement the remaining methods
*/
setStatus: function(id, newStatus) {
switch (newStatus) {
case qq.status.DELETED:
this._handleDeleteSuccess(id);
break;
case qq.status.DELETE_FAILED:
this._handleDeleteFailed(id);
break;
default:
this.log("Method setStatus called on '" + name + "' not implemented yet for " + newStatus);

This comment was marked as spam.

}
},

uploadStoredFiles: function() {
if (this._storedIds.length === 0) {
this._itemError("noFilesError");
Expand Down Expand Up @@ -1067,6 +1089,35 @@
});
},

_handleDeleteSuccess: function(id) {
var name = this.getName(id);

this._netUploadedOrQueued--;
this._netUploaded--;
this._handler.expunge(id);
this._uploadData.setStatus(id, qq.status.DELETED);
this.log("Delete request for '" + name + "' has succeeded.");
},

_handleDeleteFailed: function(id, xhrOrXdr) {
var name = this.getName(id);

this._uploadData.setStatus(id, qq.status.DELETE_FAILED);
this.log("Delete request for '" + name + "' has failed.", "error");

// Check first if xhrOrXdr is actually passed since manual status changes don't handle this.
if (xhrOrXdr) {

This comment was marked as spam.

// For error reporting, we only have access to the response status if this is not
// an `XDomainRequest`.
if (xhrOrXdr.withCredentials === undefined) {
this._options.callbacks.onError(id, name, "Delete request failed", xhrOrXdr);
}
else {
this._options.callbacks.onError(id, name, "Delete request failed with response code " + xhrOrXdr.status, xhrOrXdr);
}
}
},

// Creates an extra button element
_initExtraButton: function(spec) {
var button = this._createUploadButton({
Expand Down Expand Up @@ -1420,24 +1471,10 @@
var name = this.getName(id);

if (isError) {
this._uploadData.setStatus(id, qq.status.DELETE_FAILED);
this.log("Delete request for '" + name + "' has failed.", "error");

// For error reporting, we only have access to the response status if this is not
// an `XDomainRequest`.
if (xhrOrXdr.withCredentials === undefined) {
this._options.callbacks.onError(id, name, "Delete request failed", xhrOrXdr);
}
else {
this._options.callbacks.onError(id, name, "Delete request failed with response code " + xhrOrXdr.status, xhrOrXdr);
}
this._handleDeleteFailed(id, xhrOrXdr);
}
else {
this._netUploadedOrQueued--;
this._netUploaded--;
this._handler.expunge(id);
this._uploadData.setStatus(id, qq.status.DELETED);
this.log("Delete request for '" + name + "' has succeeded.");
this._handleDeleteSuccess(id);
}
},

Expand Down
101 changes: 101 additions & 0 deletions test/unit/set-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* globals describe, beforeEach, qq, qqtest, assert, helpme, it */

describe("set-status.js", function () {
"use strict";

var testUploadEndpoint = "/test/upload",
fileTestHelper = helpme.setupFileTests();

var initialFiles = [{
name: "left.jpg",
uuid: "e109af57-848b-4c2a-bca8-051374d01db1"
}, {
name: "right.jpg",
uuid: "949d16c3-727a-4c3c-8c0f-23404dcd6f3b"
}];

it("testing status change of DELETED with initialFiles", function () {
var uploader = new qq.FineUploaderBasic();
uploader.addInitialFiles(initialFiles);

var uploaderFiles = uploader.getUploads();
var file = uploaderFiles[0];

uploader.setStatus(file.id, qq.status.DELETED);

uploaderFiles = uploader.getUploads();
file = uploaderFiles[0];

assert.equal(qq.status.DELETED, file.status);
});

it("testing status change of DELETE_FAILED with initialFiles", function () {
var uploader = new qq.FineUploaderBasic();
uploader.addInitialFiles(initialFiles);

var uploaderFiles = uploader.getUploads();
var file = uploaderFiles[1];

uploader.setStatus(file.id, qq.status.DELETE_FAILED);

uploaderFiles = uploader.getUploads();
file = uploaderFiles[1];

assert.equal(qq.status.DELETE_FAILED, file.status);
});

it("testing status change of DELETED with mock uploader", function () {
var uploader = new qq.FineUploaderBasic({
autoUpload: true,
request: {
endpoint: testUploadEndpoint
}
});

qqtest.downloadFileAsBlob("up.jpg", "image/jpeg").then(function(blob) {
fileTestHelper.mockXhr();

uploader.addFiles({name: "test", blob: blob});
uploader.uploadStoredFiles();
fileTestHelper.getRequests()[0].respond(201, null, JSON.stringify({success: true}));
});

var uploaderFiles = uploader.getUploads();

This comment was marked as spam.

This comment was marked as spam.

var file = uploaderFiles[0];

uploader.setStatus(file.id, qq.status.DELETED);

uploaderFiles = uploader.getUploads();
file = uploaderFiles[0];

assert.equal(qq.status.DELETED, file.status);
});

it("testing status change of DELETED with mock uploader", function () {
var uploader = new qq.FineUploaderBasic({
autoUpload: true,
request: {
endpoint: testUploadEndpoint
}
});

qqtest.downloadFileAsBlob("up.jpg", "image/jpeg").then(function(blob) {
fileTestHelper.mockXhr();

uploader.addFiles({name: "test", blob: blob});
uploader.uploadStoredFiles();
fileTestHelper.getRequests()[0].respond(201, null, JSON.stringify({success: true}));
});

var uploaderFiles = uploader.getUploads();
var file = uploaderFiles[0];

uploader.setStatus(file.id, qq.status.DELETE_FAILED);

uploaderFiles = uploader.getUploads();
file = uploaderFiles[0];

assert.equal(qq.status.DELETE_FAILED, file.status);
});

});