file_upload.js 7.66 KiB
/*
 * Copyright (C) 2015 IRSTEA
 * All rights reserved.
 */
(function($) {
    var formatFileSize = function(size, precision) {
            if(typeof(precision) === "undefined") {
                precision = 2;
            if(size > 1000000000) {
                return (size/1000000000).toFixed(precision) + ' Gio';
            if(size > 1000000) {
                return (size/1000000).toFixed(precision) + ' Mio';
            if(size > 1000) {
                return (size/1000).toFixed(precision)+ ' Kio';
            return size + ' o';
        formatBitrate = function(rate) {
            return formatFileSize(rate) + '/s';
    $.blueimp.fileupload.prototype._formatFileSize = formatFileSize;
    $.blueimp.fileupload.prototype._formatBitrate = formatBitrate;
    /** Plugin irsteaFileUpload.
    $.fn.irsteaFileUpload = function(options) {
        var $this             = $(this),
            $button           = $this.find('.fileinput-button'),
            $entries          = $this.find('.fileinput-entries'),
            createUrl         = options.createUrl,
            uploadPrototype   = options.uploadPrototype,
            downloadPrototype = options.downloadPrototype,
            csrfQuery         = '?token=' + options.csrfToken;
        delete options.createUrl;
        delete options.uploadPrototype;
        delete options.downloadPrototype;
        delete options.csrfToken;
        if(options.acceptFileTypes) {
            options.acceptFileTypes = new RegExp('^' + options.acceptFileTypes + '$');
        var showError = function(entry, message) {
                var $this = $(entry);
                if(message) {
                    $this.find('.error').text(message);
                $this.addClass('alert alert-danger');
                $this.find('.error').show();
                $this.find('.icon')
                    .removeClass('fa-circle-o-notch fa-spin fa-file-o')
                    .addClass('fa-exclamation-triangle');
            updateDisplay = function() {
                var hasEntry = false;
                $entries.find('.fileinput-entry').each(function() {
                    var data = $(this).data('data');
                    if(data && data.files.error) {
                        showError(this, data.files[0].error);
                    hasEntry = true;
                });
                $button.toggle(options.multiple || !hasEntry);