/* global _ */
/*
* cf.fileUploads(fieldSelector)
* Arguments: A jQuery selector for the div wrapper of the squiz question which contains the file upload fields.
*
*/
var onEnqForm
((function(cf) {
cf.fileUploads = function(field) {
var base = this
base.$wrapper = $(field)
base.$attachButton = base.$wrapper.find('.saw-attach-file')
base.$fileList = base.$wrapper.find('ul.file-list')
base.$fileUploadFields = base.$wrapper.find('.form-upload-file-fields')
var VALID_EXTENSIONS = ['.jpg', '.jpeg', '.pdf', '.gif', '.doc', '.docx', '.png', '.tiff']
base.findUnsusedFileInput = function() {
var input = _.find(base.$wrapper.find('.form-upload-file-fields input:file'), function(inp) {
return $(inp).val() === ''
})
if (typeof input === 'undefined') {
return false
}
return input
}
base.humanFileSize = function(bytes, si) {
var thresh = si ? 1000 : 1024
if (Math.abs(bytes) < thresh) {
return bytes + ' B'
}
var units = si
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
var u = -1
do {
bytes /= thresh
++u
} while (Math.abs(bytes) >= thresh && u < units.length - 1)
return bytes.toFixed(1) + ' ' + units[u]
}
base.validateExtension = function(input) {
var fileName = $(input).val()
return (new RegExp('(' + VALID_EXTENSIONS.join('|').replace(/\./g, '\\.') + ')$', 'i')).test(fileName)
}
base.validateFiles = function() {
var errors = {
file_size: true,
file_number: true,
file_extension: true
}
// Check file extensions
_.each(base.$fileUploadFields.find('input:file'), function(input) {
if (!base.validateExtension(input) && $(input).val() !== '') {
errors.file_extension = 'Attachments must be one of the following file types: ' + VALID_EXTENSIONS.join(', ')
}
})
// Only allow as many files as there are file fields (generally 4)
if (base.findUnsusedFileInput() === false) {
base.$wrapper.find('.saw-attach-file').addClass('disabled').prop('disabled', true)
} else {
base.$wrapper.find('.saw-attach-file').removeClass('disabled').prop('disabled', false)
}
var validation = {}
validation[base.$wrapper.data('question')] = errors
cf.addValidationErrors(validation)
}
// bind Attach button to opening the file fields
base.$attachButton.on('click', function() {
var fileField = base.findUnsusedFileInput()
if (fileField === false) {
var a = {}
a[base.$wrapper.data('question')] = { file_number: 'Sorry, a maxiumum of four files are allowed.' }
cf.addValidationErrors(a)
}
$(fileField).trigger('click')
})
// Update file list when a file is attached
base.$fileUploadFields.find('input:file').on('change', function() {
if ($(this).val() !== '') {
var fieldDiv = document.createElement('li')
$(fieldDiv)
.data('file-id', $(this).attr('id'))
.html('' +
_.last($(this).val().split('\\')) +
' (' + base.humanFileSize(this.files[0].size, true) +
')Remove attachment
')
base.$fileList.append($(fieldDiv))
base.$wrapper.find('.no-photos').addClass('hide')
if ($(window).width < 640) { // hack for getting sizing right consistantly on ios etc
base.$fileList.css('width', parseInt($(window).width()) - 20)
}
}
base.validateFiles()
})
// remove the attachement from the file list when the remove button is clicked
base.$wrapper.on('click', 'a.saw-file-remove', function() {
var $li = $(this).parents('li')
base.$fileUploadFields.find('input:file#' + $li.data('file-id')).val('')
$li.remove()
base.$wrapper.find('.saw-attach-file').removeClass('disabled').prop('disabled', false)
if (base.$fileList.find('li').length === 0) {
base.$wrapper.find('.no-photos').removeClass('hide')
}
base.validateFiles()
})
base.$fileUploadFields.find('input:file').trigger('change')
}
})(onEnqForm || (onEnqForm = {})))