forked from refinery/refinerycms
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
189 additions
and
3 deletions.
There are no files selected for viewing
144 changes: 144 additions & 0 deletions
144
images/app/assets/javascript/refinery/images.js.coffee.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
$(document).ready -> | ||
images = new Images | ||
$("#new_image").submit (e) -> | ||
if window.FileList && FormData | ||
e.preventDefault() | ||
images.upload_all() | ||
$("#image_image").change (e) -> | ||
images.file_list = e.target.files | ||
|
||
|
||
class Images | ||
# TODO: | ||
# - add a progress bar for each file maybe? | ||
# - refresh images index after closing the dialog box... | ||
constructor: -> | ||
@authenticity_token = $("meta[name=\"csrf-token\"]").attr("content") | ||
@form_action = $("#new_image").attr("action") | ||
@cache_dom() | ||
|
||
cache_dom: -> | ||
@error_list = $("#error_list") | ||
@success_list = $("#flash") | ||
@error_explanation = $("#errorExplanation") | ||
@upload_progress = $("#upload_progress") | ||
|
||
before_upload_all: -> | ||
@files = [] | ||
@errors = [] | ||
@total_files = @file_list.length | ||
@uploaded = 0 | ||
@error_explanation.hide() | ||
|
||
upload_all: -> | ||
@before_upload_all() | ||
for file in @file_list | ||
@validate(file) | ||
@files.push file | ||
return @render_errors() if @errors.length > 0 | ||
$("#submit_button").hide() | ||
$(".field").hide() | ||
@progress "<%= ::I18n.t("refinery.admin.images.form.uploading")%>" | ||
@upload_next() | ||
|
||
after_upload_all: -> | ||
$(".save-loader").hide() | ||
@upload_progress.hide() | ||
@success_list.show() | ||
if @errors.length > 0 | ||
@render_errors() | ||
else | ||
if getParameterByName("modal") == "true" | ||
window.parent.document.getElementById("dialog_frame").contentDocument.location.reload(true); | ||
else | ||
@set_flash_and_reload($("#inner_flash_container").html()) | ||
|
||
validate: (file) -> | ||
max_file_size = <%= Refinery::Images.max_image_size %> | ||
if file.size > max_file_size | ||
@errors.push "#{file.name} <%= ::I18n.t("refinery.admin.images.form.should_be_smaller")%> #{max_file_size} <%= ::I18n.t("refinery.admin.images.form.bytes_in_size")%>" | ||
# TODO: set valid file types based on image model validation | ||
valid_file_types = ["image/jpeg", "image/png", "image/gif", "image/tiff"] | ||
if valid_file_types.indexOf(file.type) == -1 | ||
@errors.push "#{file.name} <%= ::I18n.t("refinery.admin.images.form.image_type")%>" | ||
|
||
upload_next: -> | ||
@do_upload(@files[0]) unless @files.length == 0 | ||
if @total_files > 0 && @files.length == 0 | ||
@after_upload_all() | ||
|
||
do_upload: (file) => | ||
form = new FormData(); | ||
form.append "image[image]", file | ||
form.append "authenticity_token", @authenticity_token | ||
_this = @ # save current scope so we can use current object methods within $.ajax | ||
$.ajax | ||
type: 'POST' | ||
url: @form_action | ||
data: form | ||
contentType: false | ||
processData: false | ||
success: (data) -> | ||
_this.post_success(file) | ||
error: (data) -> | ||
_this.post_error(file, data.responseText) | ||
|
||
set_flash_and_reload: (message) -> | ||
form = new FormData(); | ||
form.append "notice", message | ||
_this = @ # save current scope so we can use current object methods within $.ajax | ||
$.ajax | ||
type: 'POST' | ||
url: "/refinery/images/set_flash" | ||
data: form | ||
contentType: false | ||
processData: false | ||
success: (data) -> | ||
window.parent.location.reload(true) | ||
error: (data) -> | ||
window.parent.location.reload(true) | ||
|
||
after_do_upload: (file) -> | ||
@files.remove file | ||
@upload_next() | ||
|
||
post_success: (file) -> | ||
@progress file.name + " <%= ::I18n.t("refinery.admin.images.form.uploaded")%>" | ||
@success_list.append("#{file.name} <br />") | ||
@update_progress file | ||
@after_do_upload file | ||
|
||
post_error: (file, errors) -> | ||
errors = JSON.parse(errors).image | ||
@update_progress file, errors | ||
for e in errors | ||
@errors.push "#{file.name}: #{e}" | ||
@after_do_upload file | ||
|
||
render_errors: -> | ||
$(".save-loader").hide() | ||
@error_list.html("") | ||
for error in @errors | ||
@error_list.append("<li>#{error}</li>") | ||
@error_explanation.show() | ||
|
||
update_progress: (file, error = null) -> | ||
@uploaded++ | ||
message = error || "<%= ::I18n.t("refinery.admin.images.form.last_complete")%>: #{file.name}" | ||
@progress "#{@uploaded}/#{@total_files} <%= ::I18n.t("refinery.admin.images.form.uploaded")%> | #{message}" | ||
|
||
progress: (text) -> | ||
@upload_progress.html(text); | ||
|
||
# adds a remove method to Array | ||
Array::remove = (e) -> @[t..t] = [] if (t = @indexOf(e)) > -1 | ||
|
||
getParameterByName = (name) -> | ||
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]") | ||
regexS = "[\\?&]" + name + "=([^&#]*)" | ||
regex = new RegExp(regexS) | ||
results = regex.exec(window.location.search) | ||
unless results? | ||
"" | ||
else | ||
decodeURIComponent results[1].replace(/\+/g, " ") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters