Created
August 12, 2013 13:07
-
-
Save Voronchuk/6210654 to your computer and use it in GitHub Desktop.
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
# Previewer for image uploading | |
handleFileSelect = (evt) -> | |
files = evt.target.files | |
# Loop through the FileList and render image files as thumbnails. | |
for f in files | |
if !f.type.match 'image.*' then continue | |
output = $(evt.target).parent().siblings('output').find('.js-image-preview') | |
reader = new FileReader() | |
output.children('.newImage').remove() | |
# Closure to capture the file information. | |
reader.onload = do (theFile = f) -> | |
(e) -> | |
image = document.createElement 'li' | |
image.setAttribute 'class', 'u-posR Col u-imgHeight newImage' | |
image.innerHTML = [ | |
'<img class="thumb" src="', e.target.result,'" title="', escape(theFile.name), '"/>', | |
'<input type="hidden" name="save-new-image-', (output.children('.newImage').length + 1), '" value="', escape(theFile.name), '">', | |
'<button class="btn btn-mini btn-danger u-posUpperRight u-zIndex--1 js-delete-post-image"><i class="icon-remove icon-white"></i></button>' | |
].join('') | |
output.children().remove() if !$(evt.target).hasClass('multiple') | |
output.append(image) | |
# Read in the image file as a data URL. | |
reader.readAsDataURL f | |
$('.js-file-upload').change handleFileSelect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment