(function($){ "use strict"; var UploadFile = {}; UploadFile = { form: null, fileQeue: [], init: function() { this.form = $('.anzac-dropzone'); this.createDropZone(); this.loadServerFiles(); this.attachCropButtons(); }, loadServerFiles: function() { if ( !server_files ) return; $.each( server_files, function(index, file) { file.from_the_server = true; file.server_id = file.id; UploadFile.myDropzone.emit("addedfile", file); UploadFile.myDropzone.createThumbnailFromUrl(file, file.url); UploadFile.myDropzone.emit("complete", file); UploadFile.createHiddenField(file, file); } ); var existingFileCount = server_files.length; }, getConfig: function(param, fallback) { var fallback = fallback || null; return param in ANZAC_UPLOAD_CONFIG ? ANZAC_UPLOAD_CONFIG[param] : fallback; }, getSizeMessage: function() { var message = "The file needs to be at least " + UploadFile.minWidth + "x" + UploadFile.minHeight; return message; }, createDropZone: function() { Dropzone.autoDiscover = false; UploadFile.minWidth = parseInt(this.getConfig('min_width', 800)); UploadFile.minHeight = parseInt(this.getConfig('min_height', 600)); this.form.dropzone( { maxFilesize: this.getConfig('filesize', 5), paramName: this.getConfig('field_name'), maxFiles: parseInt(this.getConfig('max_files', 5)), acceptedFiles: this.getConfig('accepted_files', 'image/*'), url: ajaxurl, addRemoveLinks: true, dictDefaultMessage: "Click or Drop files here to upload", dictRemoveFileConfirmation: "Do you really want to remove this file?", params: { action: 'anzac-ajax_file_upload', form_id: this.getConfig('form_id'), id: this.getConfig('id'), entry_id: this.getConfig('entry_id') }, accept: function(file, done) { if ( file.cropped ) { file.doneAccept = done; return; } if ( this.files.length > this.options.maxFiles ) { done(this.options.dictMaxFilesExceeded.replace("{{maxFiles}}", this.options.maxFiles)); return; } var message = UploadFile.getSizeMessage(); file.acceptDimensions = function() { done(); }; file.rejectDimensions = function() { done(message); this.just_client = true; }; }, removedfile: function(file) { if (file.field_id) $('#' + file.field_id).remove(); if (file.full_path) UploadFile.deleteFileInServer(file); if ( file.server_id ) $('#img-container-' + file.server_id).remove(); UploadFile.removeFileQeue(file); var _ref; return (_ref = file.previewElement) != null ? $(_ref).remove() : void 0; }, init: function() { UploadFile.myDropzone = this; this.on( "thumbnail", function(file) { if ( !file.rejectDimensions ) return; if ( !UploadFile.checkFileSize(file) ) { file.rejectDimensions(); } else { file.acceptDimensions(); } } ); this.on( "success", function(file, response) { UploadFile.createHiddenField(file, response); if ( file.from_the_server ) return; var cropLink = $('') .attr('href', 'javascript:void;') .addClass('dz-remove') .html('Crop'); var _this = this; cropLink.on( "click", function(e) { e.preventDefault(); e.stopPropagation(); UploadFile.cropFile(file, this); } ); $(file.previewElement).append(cropLink); if ( !file.cropped ) { UploadFile.qeueFileCrop(file, cropLink); } } ); } } ); }, checkFileSize: function(file) { if (file.width < UploadFile.minWidth || file.height < UploadFile.minHeight ) { return false; } else { return true; } }, qeueFileCrop: function(file, link) { if ( !this.fileQeue ) this.fileQeue = []; this.fileQeue.push([file, link]); if (this.fileQeue.length <= 1) this.triggerFileQeue(); }, triggerFileQeue: function() { if (!this.fileQeue.length || this.croppingTriggered) return; for ( var i = 0; i < this.fileQeue.length; i++ ) { var item = this.fileQeue[i]; if ( !item.accepted ) { this.fileQeue.splice(0, 1); continue; } else { break; } } var self = this; this.croppingTriggered = true; window.setTimeout( function() { item.push(false); self.cropFile.apply(self, item); }, 1000 ); }, nextFileCrop: function(file) { this.removeFileQeue(file); this.croppingTriggered = false; this.triggerFileQeue(); }, removeFileQeue: function(file) { var self = this; this.fileQeue.forEach( function(items, index) { var element = items[0]; if (file.field_id == element.field_id) { self.fileQeue.splice(index, 1); return false; } } ); }, createHiddenField: function(file, response) { var name = UploadFile.getConfig('field_name') + '[]'; var id = 'upload-' + (new Date()).getTime(); var hiddenPath = $(''); hiddenPath.attr('type', 'hidden') .val(response.file) .attr('id', id) .attr('name', name); file.field_id = id; file.full_path = response.file; file.server_name = response.name; $('#anzac-form_upload').append(hiddenPath); }, deleteFileInServer: function( file ) { $.post( ajaxurl, {action: "anzac-delete_upload", path: file.full_path}, function(){} ); }, cropFile: function(file, link, cancel) { $(link).html('Loading...'); var self = this; try { var cachedFile = file; var $img = $(''); $img.data('file', cachedFile); var reader = new FileReader(); reader.onloadend = function () { $('.formidable-image-container').html($img); $img.attr('src', reader.result).css('max-width', '100%'); var ratio = 2/3; $img.cropper( { autoCropArea: 1, movable: true, viewMode: 1, zoom: 1, cropBoxResizable: true, minContainerWidth: 1000, minCropBoxWidth: UploadFile.minWidth, minCropBoxHeight: UploadFile.minHeight } ); $img.on( 'crop.cropper', function(event) { if (this.detectOrientation) return; this.detectOrientation = true; $(this).cropper('setAspectRatio', ratio); } ); $img.on( 'crop.cropper', function(e) { var data = e; var currentWidth = Math.round(data.width); var currentHeight = Math.round(data.height); var isValid = UploadFile.checkFileSize(data); $('#formidable-image-width').val(currentWidth); $('#formidable-image-height').val(currentHeight); var fields = "#formidable-image-width,#formidable-image-height"; if ( isValid ) { $(fields).removeClass('invalid-field'); $('.formidable-crop-upload').removeAttr('disabled').val('Crop'); } else { $(fields).addClass('invalid-field'); $('.formidable-crop-upload').attr('disabled', 'disabled').val('Invalid dimensions'); } } ); $img.cropper('setAspectRatio', ratio); tb_show("Crop modal","#TB_inline?width=1000&height=550&inlineId=crop-modal"); if (cancel === false) { $('.formidable-cancel-crop').hide(); $('#TB_closeWindowButton').hide(); } else { $('.formidable-cancel-crop').show(); } $(link).html('Crop'); $('#formidable-image-zoom-down').trigger('click'); }; reader.readAsDataURL(file); } catch ( e ) { console.log(e); } }, isPortrait: function(img) { var w = img.naturalWidth || img.width, h = img.naturalHeight || img.height; return (h > w); }, attachCropButtons: function() { $('.formidable-crop-upload').on( 'click', function() { $(this).html('Saving...').attr('disabled', true); var $img = $(".formidable-image-container").find('img.cropper-hidden'); var cachedFile = $img.data('file'); $img.cropper('getCroppedCanvas').toBlob( UploadFile.attachToBlob($img, cachedFile), cachedFile.type ); } ); $('.formidable-cancel-crop').on( 'click', function() { var $img = $(".formidable-image-container").find('img.cropper-hidden'); var cachedFile = $img.data('file'); tb_remove(); $img.cropper('destroy'); UploadFile.nextFileCrop(cachedFile); } ); $('#formidable-image-zoom-up').on( 'click', function() { var $img = $(".formidable-image-container").find('img.cropper-hidden'); $img.cropper('zoom', 0.1); } ); $('.formidable-remove-file').on( 'click', function() { var $img = $(".formidable-image-container").find('img.cropper-hidden'); var cachedFile = $img.data('file'); tb_remove(); $img.cropper('destroy'); UploadFile.myDropzone.removeFile(cachedFile); UploadFile.nextFileCrop(cachedFile); } ); $('#formidable-image-zoom-down').on( 'click', function() { var $img = $(".formidable-image-container").find('img.cropper-hidden'); $img.cropper('zoom', -0.1); } ); $('#formidable-image-min-width').val(UploadFile.minWidth); $('#formidable-image-min-height').val(UploadFile.minHeight); }, attachToBlob: function($img, cachedFile) { return function(blob) { var newFile = blob; var propertiesToCopy = ['name', 'field_id', 'full_path', 'id']; UploadFile.copyProperties(cachedFile, newFile, propertiesToCopy); newFile.cropped = true; newFile.acceptDimensions = function() { $img.cropper('destroy'); tb_remove(); UploadFile.nextFileCrop(cachedFile); if ( newFile.doneAccept ) newFile.doneAccept(); }; newFile.rejectDimensions = function() { UploadFile.myDropzone.removeFile(this); alert(UploadFile.getSizeMessage()); }; UploadFile.myDropzone.removeFile(cachedFile); UploadFile.myDropzone.addFile(newFile); $('.formidable-crop-upload').removeAttr('disabled').html('Crop'); }; }, copyProperties: function(file, newFile, props) { props.forEach( function(element, index) { if (element in file) { newFile[element] = file[element]; } } ); }, dataURItoBlob: function(dataURI) { var binary = atob(dataURI.split(',')[1]); var array = []; for(var i = 0; i < binary.length; i++) { array.push(binary.charCodeAt(i)); } return new Blob([new Uint8Array(array)], {type: 'image/jpeg'}); } }; $(document).ready(function(){ UploadFile.init(); }); })(jQuery);