bfc72533278efa1d7a65240215dcf6b146f893e8.svn-base 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
;(function ( $, window, document, undefined ) {

    /*
     * A jQuery Plugin Boilerplate
     *
     * https://github.com/johndugan/jquery-plugin-boilerplate/blob/master/jquery.plugin-boilerplate.js
     * https://john-dugan.com/jquery-plugin-boilerplate-explained/
     */

    var pluginName = "exopiteFineUploader";

    // The actual plugin constructor
    function Plugin( element, options ) {

        this.element = element;
        this._name = pluginName;
        this.$element = $( element );
        this.init();

    }

    Plugin.prototype = {

        init: function() {

            // console.log( 'maxsize: ' + this.$element.data('maxsize') );
            // console.log( 'allowedExtensions: ' + this.$element.data('mimetypes') );

            var ajaxUrl = this.$element.data('ajaxurl');

            this.$element.fineUploader({
                template: 'qq-template-manual-trigger',
                request: {
                    endpoint: ajaxUrl,
                    // Admin AJAX Param
                    params: {
                        action: 'exopite-sof-file_uploader',
                        postId: this.$element.data('postid')
                    },
                    paramsInBody: true
                },
                deleteFile: {
                    /**
                     * Delete file on AJAX request with qquuid
                     *
                     * @link https://docs.fineuploader.com/features/delete.html
                     * @link https://docs.fineuploader.com/branch/master/api/options.html#deleteFile
                     */
                    method: 'POST',
                    endpoint: ajaxUrl,
                    params: {
                        action: 'exopite-sof-file_uploader',
                    },
                    enabled             : this.$element.data('delete-enabled'),
                    forceConfirm        : this.$element.data('delete-force-confirm')
                },
                retry: {
                    enableAuto          : this.$element.data('retry-enable-auto'),
                    maxAutoAttempts     : this.$element.data('retry-max-auto-attempts'),
                    autoAttemptDelay    : this.$element.data('retry-auto-attempt-delay'),
                },
                validation: {
                    allowedExtensions: this.$element.data('mimetypes').split(','),
                    sizeLimit: this.$element.data('maxsize'),
                    itemLimit: this.$element.data('filecount')
                },
                autoUpload: this.$element.data('auto-upload'),
                debug: true
            });

            this.bindEvents();

        },

       // Bind events that trigger methods
        bindEvents: function() {
            var plugin = this;

            plugin.$element.find( '.trigger-upload' ).on( 'click'+'.'+plugin._name, function() {

                plugin.$element.fineUploader('uploadStoredFiles');

            });

        },

    };

    $.fn[pluginName] = function ( options ) {
        return this.each(function () {
            if (!$.data(this, "plugin_" + pluginName)) {
                $.data(this, "plugin_" + pluginName,
                new Plugin( this, options ));
            }
        });
    };

    $( document ).ready(function() {

        $('.qq-template').exopiteFineUploader();

    });

})( jQuery, window, document );