Subversion-Projekte lars-tiefland.ci

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
776 lars 1
/*
2
 * jQuery File Upload Processing Plugin
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
/* jshint nomen:false */
13
/* global define, require, 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 if (typeof exports === 'object') {
24
        // Node/CommonJS:
25
        factory(require('jquery'));
26
    } else {
27
        // Browser globals:
28
        factory(
29
            window.jQuery
30
        );
31
    }
32
}(function ($) {
33
    'use strict';
34
 
35
    var originalAdd = $.blueimp.fileupload.prototype.options.add;
36
 
37
    // The File Upload Processing plugin extends the fileupload widget
38
    // with file processing functionality:
39
    $.widget('blueimp.fileupload', $.blueimp.fileupload, {
40
 
41
        options: {
42
            // The list of processing actions:
43
            processQueue: [
44
                /*
45
                {
46
                    action: 'log',
47
                    type: 'debug'
48
                }
49
                */
50
            ],
51
            add: function (e, data) {
52
                var $this = $(this);
53
                data.process(function () {
54
                    return $this.fileupload('process', data);
55
                });
56
                originalAdd.call(this, e, data);
57
            }
58
        },
59
 
60
        processActions: {
61
            /*
62
            log: function (data, options) {
63
                console[options.type](
64
                    'Processing "' + data.files[data.index].name + '"'
65
                );
66
            }
67
            */
68
        },
69
 
70
        _processFile: function (data, originalData) {
71
            var that = this,
72
                dfd = $.Deferred().resolveWith(that, [data]),
73
                chain = dfd.promise();
74
            this._trigger('process', null, data);
75
            $.each(data.processQueue, function (i, settings) {
76
                var func = function (data) {
77
                    if (originalData.errorThrown) {
78
                        return $.Deferred()
79
                                .rejectWith(that, [originalData]).promise();
80
                    }
81
                    return that.processActions[settings.action].call(
82
                        that,
83
                        data,
84
                        settings
85
                    );
86
                };
87
                chain = chain.pipe(func, settings.always && func);
88
            });
89
            chain
90
                .done(function () {
91
                    that._trigger('processdone', null, data);
92
                    that._trigger('processalways', null, data);
93
                })
94
                .fail(function () {
95
                    that._trigger('processfail', null, data);
96
                    that._trigger('processalways', null, data);
97
                });
98
            return chain;
99
        },
100
 
101
        // Replaces the settings of each processQueue item that
102
        // are strings starting with an "@", using the remaining
103
        // substring as key for the option map,
104
        // e.g. "@autoUpload" is replaced with options.autoUpload:
105
        _transformProcessQueue: function (options) {
106
            var processQueue = [];
107
            $.each(options.processQueue, function () {
108
                var settings = {},
109
                    action = this.action,
110
                    prefix = this.prefix === true ? action : this.prefix;
111
                $.each(this, function (key, value) {
112
                    if ($.type(value) === 'string' &&
113
                            value.charAt(0) === '@') {
114
                        settings[key] = options[
115
                            value.slice(1) || (prefix ? prefix +
116
                                key.charAt(0).toUpperCase() + key.slice(1) : key)
117
                        ];
118
                    } else {
119
                        settings[key] = value;
120
                    }
121
 
122
                });
123
                processQueue.push(settings);
124
            });
125
            options.processQueue = processQueue;
126
        },
127
 
128
        // Returns the number of files currently in the processsing queue:
129
        processing: function () {
130
            return this._processing;
131
        },
132
 
133
        // Processes the files given as files property of the data parameter,
134
        // returns a Promise object that allows to bind callbacks:
135
        process: function (data) {
136
            var that = this,
137
                options = $.extend({}, this.options, data);
138
            if (options.processQueue && options.processQueue.length) {
139
                this._transformProcessQueue(options);
140
                if (this._processing === 0) {
141
                    this._trigger('processstart');
142
                }
143
                $.each(data.files, function (index) {
144
                    var opts = index ? $.extend({}, options) : options,
145
                        func = function () {
146
                            if (data.errorThrown) {
147
                                return $.Deferred()
148
                                        .rejectWith(that, [data]).promise();
149
                            }
150
                            return that._processFile(opts, data);
151
                        };
152
                    opts.index = index;
153
                    that._processing += 1;
154
                    that._processingQueue = that._processingQueue.pipe(func, func)
155
                        .always(function () {
156
                            that._processing -= 1;
157
                            if (that._processing === 0) {
158
                                that._trigger('processstop');
159
                            }
160
                        });
161
                });
162
            }
163
            return this._processingQueue;
164
        },
165
 
166
        _create: function () {
167
            this._super();
168
            this._processing = 0;
169
            this._processingQueue = $.Deferred().resolveWith(this)
170
                .promise();
171
        }
172
 
173
    });
174
 
175
}));