Dotclear

source: admin/js/jsUpload/jquery.fileupload-process.js @ 1144:4af82896ca3d

Revision 1144:4af82896ca3d, 5.2 KB checked in by Nicolas <nikrou77@…>, 12 years ago (diff)

Remplacement de l'upload utilisant swfupload par le plugin jQuery-File-Upload

Todo:

  • Gestion des suppressions
  • Gestion des annulations
  • Gestion des mises de l'interface sans rechargement de la page
  • Simplification (moins de javascript) ?
Line 
1/*
2 * jQuery File Upload Processing Plugin 1.1
3 * https://github.com/blueimp/jQuery-File-Upload
4 *
5 * Copyright 2012, Sebastian Tschan
6 * https://blueimp.net
7 *
8 * Licensed under the MIT license:
9 * http://www.opensource.org/licenses/MIT
10 */
11
12/*jslint nomen: true, unparam: true */
13/*global define, window */
14
15(function (factory) {
16    'use strict';
17    if (typeof define === 'function' && define.amd) {
18        // Register as an anonymous AMD module:
19        define([
20            'jquery',
21            './jquery.fileupload'
22        ], factory);
23    } else {
24        // Browser globals:
25        factory(
26            window.jQuery
27        );
28    }
29}(function ($) {
30    'use strict';
31
32    var originalAdd = $.blueimp.fileupload.prototype.options.add;
33
34    // The File Upload Processing plugin extends the fileupload widget
35    // with file processing functionality:
36    $.widget('blueimp.fileupload', $.blueimp.fileupload, {
37
38        options: {
39            // The list of processing actions:
40            processQueue: [
41                /*
42                {
43                    action: 'log',
44                    type: 'debug'
45                }
46                */
47            ],
48            add: function (e, data) {
49                var $this = $(this);
50                data.process(function () {
51                    return $this.fileupload('process', data);
52                });
53                originalAdd.call(this, e, data);
54            }
55        },
56
57        processActions: {
58            /*
59            log: function (data, options) {
60                console[options.type](
61                    'Processing "' + data.files[data.index].name + '"'
62                );
63            }
64            */
65        },
66
67        _processFile: function (data) {
68            var that = this,
69                dfd = $.Deferred().resolveWith(that, [data]),
70                chain = dfd.promise();
71            this._trigger('process', null, data);
72            $.each(data.processQueue, function (i, settings) {
73                var func = function (data) {
74                    return that.processActions[settings.action].call(
75                        that,
76                        data,
77                        settings
78                    );
79                };
80                chain = chain.pipe(func, settings.always && func);
81            });
82            chain
83                .done(function () {
84                    that._trigger('processdone', null, data);
85                    that._trigger('processalways', null, data);
86                })
87                .fail(function () {
88                    that._trigger('processfail', null, data);
89                    that._trigger('processalways', null, data);
90                });
91            return chain;
92        },
93
94        // Replaces the settings of each processQueue item that
95        // are strings starting with an "@", using the remaining
96        // substring as key for the option map,
97        // e.g. "@autoUpload" is replaced with options.autoUpload:
98        _transformProcessQueue: function (options) {
99            var processQueue = [];
100            $.each(options.processQueue, function () {
101                var settings = {};
102                $.each(this, function (key, value) {
103                    if ($.type(value) === 'string' &&
104                            value.charAt(0) === '@') {
105                        settings[key] = options[value.slice(1)];
106                    } else {
107                        settings[key] = value;
108                    }
109                });
110                processQueue.push(settings);
111            });
112            options.processQueue = processQueue;
113        },
114
115        // Returns the number of files currently in the processsing queue:
116        processing: function () {
117            return this._processing;
118        },
119
120        // Processes the files given as files property of the data parameter,
121        // returns a Promise object that allows to bind callbacks:
122        process: function (data) {
123            var that = this,
124                options = $.extend({}, this.options, data);
125            if (options.processQueue && options.processQueue.length) {
126                this._transformProcessQueue(options);
127                if (this._processing === 0) {
128                    this._trigger('processstart');
129                }
130                $.each(data.files, function (index, file) {
131                    var opts = index ? $.extend({}, options) : options,
132                        func = function () {
133                            return that._processFile(opts);
134                        };
135                    opts.index = index;
136                    that._processing += 1;
137                    that._processingQueue = that._processingQueue.pipe(func, func)
138                        .always(function () {
139                            that._processing -= 1;
140                            if (that._processing === 0) {
141                                that._trigger('processstop');
142                            }
143                        });
144                });
145            }
146            return this._processingQueue;
147        },
148
149        _create: function () {
150            this._super();
151            this._processing = 0;
152            this._processingQueue = $.Deferred().resolveWith(this)
153                .promise();
154        }
155
156    });
157
158}));
Note: See TracBrowser for help on using the repository browser.

Sites map