A Pen by Dorian Karter on CodePen.
Last active
August 29, 2015 14:17
-
-
Save dkarter/0bae1d744de96b0973e6 to your computer and use it in GitHub Desktop.
Handlebars +jQuery Multiple File Upload
This file contains hidden or 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
<script id="file-upload-template" type="text/x-handlebars-template"> | |
<li class="removable_row"> | |
<div class="file-field"> | |
<label class="file required" for="{{field_id}}"><abbr title="required">*</abbr> Attachment</label> | |
<input class="file required" id="{{field_id}}" name="{{field_name}}" type="file" /> | |
</div> | |
{{#if allowRemove}} | |
<i alt="Remove" title="Remove" class="fa fa-trash remove-row-link" /> | |
{{/if}} | |
</li> | |
</script> | |
<div class="file-uploader"> | |
</div> | |
<div class="file-uploader"> | |
</div> | |
This file contains hidden or 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
(function(handlebars) { | |
function parse_template(id, context) { | |
var source = $('#' + id).html(); | |
return handlebars.compile(source)(context); | |
} | |
function addFileInput($fileList, allowRemove) { | |
var context = { | |
field_name: 'unique_field' + new Date().getTime(), | |
field_id: 'this_is_the_id' + new Date().getTime(), | |
allowRemove: allowRemove | |
}; | |
var template = parse_template('file-upload-template', context); | |
$fileList.append(template); | |
$('.remove-row-link').click(function(e) { | |
e.preventDefault(); | |
$(this).parents('li').remove(); | |
}); | |
} | |
$.fn.multipleFileUploader = function (settings) { | |
$(this).each(function(index, el) { | |
var defaults = { | |
addButtonCaption: 'Add another file', | |
initialRequired: true, | |
initialFieldCount: 1, | |
containerClass: 'mfu-file-list-container', | |
addButtonClass: 'mfu-add-new', | |
fileListClass: 'mfu-file-list', | |
$uploaderControl: $(el) | |
}; | |
var options = $.extend(defaults, settings); | |
options.$uploaderControl.addClass(options.containerClass); | |
createAddButton(options); | |
createFileList(options); | |
addInitialFields(options); | |
}); | |
}; | |
function getFileList (options) { | |
return options.$uploaderControl.find('.' + options.fileListClass); | |
} | |
function createFileList (options) { | |
var $fileList = $('<ul />') | |
.addClass(options.fileListClass); | |
return options.$uploaderControl.prepend($fileList); | |
} | |
function createAddButton (options) { | |
var $addButton = $('<button></button>') | |
.addClass(options.addButtonClass) | |
.text(options.addButtonCaption); | |
options.$uploaderControl.append($addButton); | |
return $addButton.click(function() { | |
addFileInput(getFileList(options), true); | |
}); | |
} | |
function addInitialFields (options) { | |
for (var i = 0; i < options.initialFieldCount; i++) { | |
addFileInput(getFileList(options), options.initialRequired); | |
} | |
} | |
$(document).ready(function(){ | |
$('.file-uploader').multipleFileUploader(); | |
}); | |
})(Handlebars); |
This file contains hidden or 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
.mfu-file-list { | |
list-style: none; | |
width: 500px; | |
.file-field, | |
.remove-row-link, | |
input[type="file"] { | |
display: inline-block; | |
} | |
.remove-row-link { | |
position: relative; | |
top: 3px; | |
cursor: pointer; | |
} | |
abbr[title="required"] { | |
color: #ff0000; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment