Commit 8e47219a by zubair-arbi

trigger change event for file field while uploading pdf

STUD-1775
parent f1bc7a16
...@@ -64,6 +64,32 @@ define ["js/models/uploads", "js/views/uploads", "js/models/chapter", "js/spec_h ...@@ -64,6 +64,32 @@ define ["js/models/uploads", "js/views/uploads", "js/models/chapter", "js/spec_h
expect(@view.$el).toContain("#upload_error") expect(@view.$el).toContain("#upload_error")
expect(@view.$(".action-upload")).toHaveClass("disabled") expect(@view.$(".action-upload")).toHaveClass("disabled")
it "should render an error with an invalid file type after a correct file type selected", ->
correctFile = {name: "fake.pdf", "type": "application/pdf"}
inCorrectFile = {name: "fake.png", "type": "image/png"}
event = {}
@view.render()
event.target = {"files": [correctFile]}
@view.selectFile(event)
expect(@view.$el).toContain("input[type=file]")
expect(@view.$el).not.toContain("#upload_error")
expect(@view.$(".action-upload")).not.toHaveClass("disabled")
realMethod = @model.set
spyOn(@model, "set").andCallFake (data) ->
if data.selectedFile != undefined
this.attributes.selectedFile = data.selectedFile
this.changed = {}
else
realMethod.apply(this, arguments)
event.target = {"files": [inCorrectFile]}
@view.selectFile(event)
expect(@view.$el).toContain("input[type=file]")
expect(@view.$el).toContain("#upload_error")
expect(@view.$(".action-upload")).toHaveClass("disabled")
describe "Uploads", -> describe "Uploads", ->
beforeEach -> beforeEach ->
@clock = sinon.useFakeTimers() @clock = sinon.useFakeTimers()
......
...@@ -61,9 +61,15 @@ define(["jquery", "underscore", "gettext", "js/views/modals/base_modal", "jquery ...@@ -61,9 +61,15 @@ define(["jquery", "underscore", "gettext", "js/views/modals/base_modal", "jquery
}, },
selectFile: function(e) { selectFile: function(e) {
var selectedFile = e.target.files[0] || null;
this.model.set({ this.model.set({
selectedFile: e.target.files[0] || null selectedFile: selectedFile
}); });
// This change event triggering necessary for FireFox, because the browser don't
// consider change of File object (file input field) as a change in model.
if (selectedFile && $.isEmptyObject(this.model.changed)){
this.model.trigger('change');
}
}, },
upload: function(e) { upload: function(e) {
......
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