Commit d9af6f79 authored by Stan Hu's avatar Stan Hu

Change the replace placeholder to use the filename

Dynamically adjust placedholder for uploads and fix Dropzone event handlers

Override error handler to prevent error messages from being inserted underneath image preview

Fix tests

Use regexp instead of startsWith for better browser compatibility

Remove duplicate code in _replace.html.haml and use one template

Remove files upon error and retain alert messages until user adds a new file
parent 0a8ef29b
...@@ -20,27 +20,42 @@ class @BlobFileDropzone ...@@ -20,27 +20,42 @@ class @BlobFileDropzone
headers: headers:
"X-CSRF-Token": $("meta[name=\"csrf-token\"]").attr("content") "X-CSRF-Token": $("meta[name=\"csrf-token\"]").attr("content")
success: (header, response) -> init: ->
window.location.href = response.filePath this.on 'addedfile', (file) ->
$('.dropzone-alerts').html('').hide()
commit_message = form.find('#commit_message')[0]
if /^Upload/.test(commit_message.placeholder)
commit_message.placeholder = 'Upload ' + file.name
return return
error: (temp, errorMessage) -> this.on 'removedfile', (file) ->
stripped = $("<div/>").html(errorMessage).text(); commit_message = form.find('#commit_message')[0]
$('.dropzone-alerts').html('Error uploading file: \"' + stripped + '\"').show()
if /^Upload/.test(commit_message.placeholder)
commit_message.placeholder = 'Upload new file'
return return
maxfilesexceeded: (file) -> this.on 'success', (header, response) ->
@removeFile file window.location.href = response.filePath
return return
removedfile: (file) -> this.on 'maxfilesexceeded', (file) ->
$('.dropzone-previews')[0].removeChild(file.previewTemplate) @removeFile file
$('.dropzone-alerts').html('').hide() return
return true
sending: (file, xhr, formData) -> this.on 'sending', (file, xhr, formData) ->
formData.append('commit_message', form.find('#commit_message').val()) formData.append('commit_message', form.find('#commit_message').val())
return return
# Override behavior of adding error underneath preview
error: (file, errorMessage) ->
stripped = $("<div/>").html(errorMessage).text();
$('.dropzone-alerts').html('Error uploading file: \"' + stripped + '\"').show()
@removeFile file
return
) )
submitButton = form.find('#submit-all')[0] submitButton = form.find('#submit-all')[0]
......
...@@ -18,6 +18,12 @@ class Projects::BlobController < Projects::ApplicationController ...@@ -18,6 +18,12 @@ class Projects::BlobController < Projects::ApplicationController
before_action :after_edit_path, only: [:edit, :update] before_action :after_edit_path, only: [:edit, :update]
def new def new
@title = 'Upload'
@placeholder = 'Upload new file'
@button_title = 'Upload file'
@form_path = namespace_project_create_blob_path(@project.namespace, @project, @id)
@method = :post
commit unless @repository.empty? commit unless @repository.empty?
end end
...@@ -40,6 +46,11 @@ class Projects::BlobController < Projects::ApplicationController ...@@ -40,6 +46,11 @@ class Projects::BlobController < Projects::ApplicationController
end end
def show def show
@title = "Replace #{@blob.name}"
@placeholder = @title
@button_title = 'Replace file'
@form_path = namespace_project_update_blob_path(@project.namespace, @project, @id)
@method = :put
end end
def edit def edit
......
...@@ -18,5 +18,5 @@ ...@@ -18,5 +18,5 @@
- if allowed_tree_edit? - if allowed_tree_edit?
.btn-group{ role: "group" } .btn-group{ role: "group" }
%button.btn.btn-default{ 'data-target' => '#modal-replace-blob', 'data-toggle' => 'modal' } Replace %button.btn.btn-default{ 'data-target' => '#modal-upload-blob', 'data-toggle' => 'modal' } Replace
%button.btn.btn-remove{ 'data-target' => '#modal-remove-blob', 'data-toggle' => 'modal' } Remove %button.btn.btn-remove{ 'data-target' => '#modal-remove-blob', 'data-toggle' => 'modal' } Remove
#modal-replace-blob.modal
.modal-dialog
.modal-content
.modal-header
%a.close{href: "#", "data-dismiss" => "modal"} ×
%h3.page-title Replace #{@blob.name}
%p.light
From branch
%strong= @ref
.modal-body
= form_tag namespace_project_update_blob_path(@project.namespace, @project, @id), method: :put, class: 'blob-file-upload-form-js form-horizontal' do
.dropzone
.dropzone-previews.blob-upload-dropzone-previews
%p.dz-message.light
Attach a file by drag &amp; drop or
= link_to 'click to upload', '#', class: "markdown-selector"
%br
.dropzone-alerts{class: "alert alert-danger data", "data-dismiss" => "alert", style: "display:none"}
= render 'shared/commit_message_container', params: params,
placeholder: 'Replace file'
.form-group
.col-sm-offset-2.col-sm-10
= button_tag 'Replace file', class: 'btn btn-small btn-primary btn-replace-file', id: 'submit-all'
= link_to "Cancel", '#', class: "btn btn-cancel", "data-dismiss" => "modal"
:coffeescript
disableButtonIfEmptyField $('.blob-file-upload-form-js').find('#commit_message'), '.btn-replace-file'
new BlobFileDropzone($('.blob-file-upload-form-js'), 'put')
...@@ -3,26 +3,26 @@ ...@@ -3,26 +3,26 @@
.modal-content .modal-content
.modal-header .modal-header
%a.close{href: "#", "data-dismiss" => "modal"} × %a.close{href: "#", "data-dismiss" => "modal"} ×
%h3.page-title Upload %h3.page-title #{@title}
%p.light %p.light
From branch From branch
%strong= @ref %strong= @ref
.modal-body .modal-body
= form_tag namespace_project_create_blob_path(@project.namespace, @project, @id), method: :post, class: 'blob-file-upload-form-js form-horizontal' do = form_tag @form_path, method: @method, class: 'blob-file-upload-form-js form-horizontal' do
.dropzone .dropzone
.dropzone-previews.blob-upload-dropzone-previews .dropzone-previews.blob-upload-dropzone-previews
%p.dz-message.light %p.dz-message.light
Attach a file by drag &amp; drop or Attach a file by drag &amp; drop or
= link_to 'click to upload', '#', class: "markdown-selector" = link_to 'click to upload', '#', class: "markdown-selector"
%br %br
.dropzone-alerts{class: "alert alert-danger data", "data-dismiss" => "alert", style: "display:none"} .dropzone-alerts{class: "alert alert-danger data", style: "display:none"}
= render 'shared/commit_message_container', params: params, = render 'shared/commit_message_container', params: params,
placeholder: 'Upload new file' placeholder: @placeholder
.form-group .form-group
.col-sm-offset-2.col-sm-10 .col-sm-offset-2.col-sm-10
= button_tag 'Upload file', class: 'btn btn-small btn-primary btn-upload-file', id: 'submit-all' = button_tag @button_title, class: 'btn btn-small btn-primary btn-upload-file', id: 'submit-all'
= link_to "Cancel", '#', class: "btn btn-cancel", "data-dismiss" => "modal" = link_to "Cancel", '#', class: "btn btn-cancel", "data-dismiss" => "modal"
:coffeescript :coffeescript
disableButtonIfEmptyField $('.blob-file-upload-form-js').find('#commit_message'), '.btn-upload-file' disableButtonIfEmptyField $('.blob-file-upload-form-js').find('#commit_message'), '.btn-upload-file'
new BlobFileDropzone($('.blob-file-upload-form-js'), 'post') new BlobFileDropzone($('.blob-file-upload-form-js'), '#{@method}')
...@@ -10,4 +10,4 @@ ...@@ -10,4 +10,4 @@
- if allowed_tree_edit? - if allowed_tree_edit?
= render 'projects/blob/remove' = render 'projects/blob/remove'
= render 'projects/blob/replace' = render 'projects/blob/upload'
...@@ -38,8 +38,8 @@ Feature: Project Source Browse Files ...@@ -38,8 +38,8 @@ Feature: Project Source Browse Files
Scenario: I can upload file and commit Scenario: I can upload file and commit
Given I click on "new file" link in repo Given I click on "new file" link in repo
Then I can see new file page Then I can see new file page
And I can see "upload existing one" And I can see "upload an existing one"
And I click on "upload existing one" And I click on "upload"
And I upload a new text file And I upload a new text file
And I fill the upload file commit message And I fill the upload file commit message
And I click on "Upload file" And I click on "Upload file"
......
...@@ -119,12 +119,12 @@ class Spinach::Features::ProjectSourceBrowseFiles < Spinach::FeatureSteps ...@@ -119,12 +119,12 @@ class Spinach::Features::ProjectSourceBrowseFiles < Spinach::FeatureSteps
expect(page).to have_content "Commit message" expect(page).to have_content "Commit message"
end end
step 'I can see "upload existing one"' do step 'I can see "upload an existing one"' do
expect(page).to have_content "upload existing one" expect(page).to have_content "upload an existing one"
end end
step 'I click on "upload existing one"' do step 'I click on "upload"' do
click_link 'upload existing one' click_link 'upload'
end end
step 'I click on "Upload file"' do step 'I click on "Upload file"' do
...@@ -150,7 +150,7 @@ class Spinach::Features::ProjectSourceBrowseFiles < Spinach::FeatureSteps ...@@ -150,7 +150,7 @@ class Spinach::Features::ProjectSourceBrowseFiles < Spinach::FeatureSteps
end end
step 'I fill the replace file commit message' do step 'I fill the replace file commit message' do
page.within('#modal-replace-blob') do page.within('#modal-upload-blob') do
fill_in :commit_message, with: 'Replacement file commit message' fill_in :commit_message, with: 'Replacement file commit message'
end end
end end
......
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