Subversion-Projekte lars-tiefland.webanos.zeldi.de

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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