Commit c2ef6a53 by David Baumgold

PDF textbook upload: show success message for 2 seconds following successful upload

parent cae5c324
...@@ -140,6 +140,12 @@ describe "CMS.Models.FileUpload", -> ...@@ -140,6 +140,12 @@ describe "CMS.Models.FileUpload", ->
beforeEach -> beforeEach ->
@model = new CMS.Models.FileUpload() @model = new CMS.Models.FileUpload()
it "is unfinished by default", ->
expect(@model.get("finished")).toBeFalsy()
it "is not uploading by default", ->
expect(@model.get("uploading")).toBeFalsy()
it "is valid by default", -> it "is valid by default", ->
expect(@model.isValid()).toBeTruthy() expect(@model.isValid()).toBeTruthy()
......
...@@ -340,9 +340,9 @@ describe "CMS.Views.UploadDialog", -> ...@@ -340,9 +340,9 @@ describe "CMS.Views.UploadDialog", ->
request.respond(200, {"Content-Type": "application/json"}, request.respond(200, {"Content-Type": "application/json"},
'{"displayname": "starfish", "url": "/uploaded/starfish.pdf"}') '{"displayname": "starfish", "url": "/uploaded/starfish.pdf"}')
expect(@model.get("uploading")).toBeFalsy() expect(@model.get("uploading")).toBeFalsy()
expect(@model.get("finished")).toBeTruthy()
expect(@chapter.get("name")).toEqual("starfish") expect(@chapter.get("name")).toEqual("starfish")
expect(@chapter.get("asset_path")).toEqual("/uploaded/starfish.pdf") expect(@chapter.get("asset_path")).toEqual("/uploaded/starfish.pdf")
expect(@view.remove).toHaveBeenCalled()
it "can handle upload errors", -> it "can handle upload errors", ->
@view.upload() @view.upload()
...@@ -350,3 +350,46 @@ describe "CMS.Views.UploadDialog", -> ...@@ -350,3 +350,46 @@ describe "CMS.Views.UploadDialog", ->
expect(@model.get("title")).toMatch(/error/) expect(@model.get("title")).toMatch(/error/)
expect(@view.remove).not.toHaveBeenCalled() expect(@view.remove).not.toHaveBeenCalled()
describe "CMS.Views.UploadDialog timing", ->
tpl = readFixtures("upload-dialog.underscore")
beforeEach ->
setFixtures($("<script>", {id: "upload-dialog-tpl", type: "text/template"}).text(tpl))
appendSetFixtures($("<script>", {id: "system-feedback-tpl", type: "text/template"}).text(feedbackTpl))
window.UPLOAD_ASSET_CALLBACK_URL = "/upload"
@requests = requests = []
@xhr = sinon.useFakeXMLHttpRequest()
@xhr.onCreate = (xhr) -> requests.push(xhr)
@model = new CMS.Models.FileUpload()
@chapter = new CMS.Models.Chapter()
@view = new CMS.Views.UploadDialog({model: @model, chapter: @chapter})
spyOn(@view, 'remove').andCallThrough()
# create mock file input, so that we aren't subject to browser restrictions
@mockFiles = []
mockFileInput = jasmine.createSpy('mockFileInput')
mockFileInput.files = @mockFiles
jqMockFileInput = jasmine.createSpyObj('jqMockFileInput', ['get', 'replaceWith'])
jqMockFileInput.get.andReturn(mockFileInput)
realMethod = @view.$
spyOn(@view, "$").andCallFake (selector) ->
if selector == "input[type=file]"
jqMockFileInput
else
realMethod.apply(this, arguments)
@clock = sinon.useFakeTimers()
afterEach ->
delete window.UPLOAD_ASSET_CALLBACK_URL
@xhr.restore()
@clock.restore()
it "removes itself after two seconds on successful upload", ->
@view.upload()
@requests[0].respond(200, {"Content-Type": "application/json"},
'{"displayname": "starfish", "url": "/uploaded/starfish.pdf"}')
expect(@view.remove).not.toHaveBeenCalled()
@clock.tick(2001)
expect(@view.remove).toHaveBeenCalled()
...@@ -97,7 +97,8 @@ CMS.Models.FileUpload = Backbone.Model.extend({ ...@@ -97,7 +97,8 @@ CMS.Models.FileUpload = Backbone.Model.extend({
"selectedFile": null, "selectedFile": null,
"uploading": false, "uploading": false,
"uploadedBytes": 0, "uploadedBytes": 0,
"totalBytes": 0 "totalBytes": 0,
"finished": false
}, },
validate: function(attrs, options) { validate: function(attrs, options) {
if(attrs.selectedFile && attrs.selectedFile.type !== "application/pdf") { if(attrs.selectedFile && attrs.selectedFile.type !== "application/pdf") {
......
...@@ -243,7 +243,8 @@ CMS.Views.ChapterEdit = Backbone.View.extend({ ...@@ -243,7 +243,8 @@ CMS.Views.ChapterEdit = Backbone.View.extend({
CMS.Views.UploadDialog = Backbone.View.extend({ CMS.Views.UploadDialog = Backbone.View.extend({
options: { options: {
shown: true shown: true,
successMessageTimeout: 2000 // 2 seconds
}, },
initialize: function() { initialize: function() {
this.template = _.template($("#upload-dialog-tpl").text()); this.template = _.template($("#upload-dialog-tpl").text());
...@@ -263,6 +264,7 @@ CMS.Views.UploadDialog = Backbone.View.extend({ ...@@ -263,6 +264,7 @@ CMS.Views.UploadDialog = Backbone.View.extend({
uploading: this.model.get('uploading'), uploading: this.model.get('uploading'),
uploadedBytes: this.model.get('uploadedBytes'), uploadedBytes: this.model.get('uploadedBytes'),
totalBytes: this.model.get('totalBytes'), totalBytes: this.model.get('totalBytes'),
finished: this.model.get('finished'),
error: this.model.get('error') error: this.model.get('error')
})); }));
// ideally, we'd like to tell the browser to pre-populate the // ideally, we'd like to tell the browser to pre-populate the
...@@ -326,7 +328,10 @@ CMS.Views.UploadDialog = Backbone.View.extend({ ...@@ -326,7 +328,10 @@ CMS.Views.UploadDialog = Backbone.View.extend({
}); });
}, },
success: function(response, statusText, xhr, form) { success: function(response, statusText, xhr, form) {
this.model.set('uploading', false); this.model.set({
uploading: false,
finished: true
});
var chapter = this.options.chapter; var chapter = this.options.chapter;
if(chapter) { if(chapter) {
var options = {}; var options = {};
...@@ -336,7 +341,10 @@ CMS.Views.UploadDialog = Backbone.View.extend({ ...@@ -336,7 +341,10 @@ CMS.Views.UploadDialog = Backbone.View.extend({
options.asset_path = response.url; options.asset_path = response.url;
chapter.set(options); chapter.set(options);
} }
this.remove(); var that = this;
this.removalTimeout = setTimeout(function() {
that.hide().remove();
}, this.options.successMessageTimeout);
}, },
error: function() { error: function() {
this.model.set({ this.model.set({
......
...@@ -22,6 +22,9 @@ ...@@ -22,6 +22,9 @@
<progress></progress> <progress></progress>
<% } %> <% } %>
<% } %> <% } %>
<% if(finished) { %>
<strong><%= gettext("Success!") %></strong>
<% } %>
</div> </div>
<div class="actions"> <div class="actions">
<h3 class="sr"><%= gettext('Form Actions') %></h3> <h3 class="sr"><%= gettext('Form Actions') %></h3>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment