| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 | (function (root, factory) {  if (typeof define === 'function' && define.amd) {    // AMD. Register as an anonymous module.    define('simple-uploader', ["jquery",      "simple-module"], function ($, SimpleModule) {      return (root.returnExportsGlobal = factory($, SimpleModule));    });  } else if (typeof exports === 'object') {    // Node. Does not work with strict CommonJS, but    // only CommonJS-like enviroments that support module.exports,    // like Node.    module.exports = factory(require("jquery"),      require("simple-module"));  } else {    root.simple = root.simple || {};    root.simple['uploader'] = factory(jQuery,      SimpleModule);  }}(this, function ($, SimpleModule) {var Uploader, uploader,  __hasProp = {}.hasOwnProperty,  __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };Uploader = (function(_super) {  __extends(Uploader, _super);  function Uploader() {    return Uploader.__super__.constructor.apply(this, arguments);  }  Uploader.count = 0;  Uploader.prototype.opts = {    url: '',    params: null,    fileKey: 'upload_file',    connectionCount: 3  };  Uploader.prototype._init = function() {    this.files = [];    this.queue = [];    this.id = ++Uploader.count;    this.on('uploadcomplete', (function(_this) {      return function(e, file) {        _this.files.splice($.inArray(file, _this.files), 1);        if (_this.queue.length > 0 && _this.files.length < _this.opts.connectionCount) {          return _this.upload(_this.queue.shift());        } else {          return _this.uploading = false;        }      };    })(this));    return $(window).on('beforeunload.uploader-' + this.id, (function(_this) {      return function(e) {        if (!_this.uploading) {          return;        }        e.originalEvent.returnValue = _this._t('leaveConfirm');        return _this._t('leaveConfirm');      };    })(this));  };  Uploader.prototype.generateId = (function() {    var id;    id = 0;    return function() {      return id += 1;    };  })();  Uploader.prototype.upload = function(file, opts) {    var f, key, _i, _len;    if (opts == null) {      opts = {};    }    if (file == null) {      return;    }    if ($.isArray(file)) {      for (_i = 0, _len = file.length; _i < _len; _i++) {        f = file[_i];        this.upload(f, opts);      }    } else if ($(file).is('input:file')) {      key = $(file).attr('name');      if (key) {        opts.fileKey = key;      }      this.upload($.makeArray($(file)[0].files), opts);    } else if (!file.id || !file.obj) {      file = this.getFile(file);    }    if (!(file && file.obj)) {      return;    }    $.extend(file, opts);    if (this.files.length >= this.opts.connectionCount) {      this.queue.push(file);      return;    }    if (this.triggerHandler('beforeupload', [file]) === false) {      return;    }    this.files.push(file);    this._xhrUpload(file);    return this.uploading = true;  };  Uploader.prototype.getFile = function(fileObj) {    var name, _ref, _ref1;    if (fileObj instanceof window.File || fileObj instanceof window.Blob) {      name = (_ref = fileObj.fileName) != null ? _ref : fileObj.name;    } else {      return null;    }    return {      id: this.generateId(),      url: this.opts.url,      params: this.opts.params,      fileKey: this.opts.fileKey,      name: name,      size: (_ref1 = fileObj.fileSize) != null ? _ref1 : fileObj.size,      ext: name ? name.split('.').pop().toLowerCase() : '',      obj: fileObj    };  };  Uploader.prototype._xhrUpload = function(file) {    var formData, k, v, _ref;    formData = new FormData();    formData.append(file.fileKey, file.obj);    formData.append("original_filename", file.name);    if (file.params) {      _ref = file.params;      for (k in _ref) {        v = _ref[k];        formData.append(k, v);      }    }    return file.xhr = $.ajax({      url: file.url,      data: formData,      dataType: 'json',      processData: false,      contentType: false,      type: 'POST',      headers: {        'X-File-Name': encodeURIComponent(file.name)      },      xhr: function() {        var req;        req = $.ajaxSettings.xhr();        if (req) {          req.upload.onprogress = (function(_this) {            return function(e) {              return _this.progress(e);            };          })(this);        }        return req;      },      progress: (function(_this) {        return function(e) {          if (!e.lengthComputable) {            return;          }          return _this.trigger('uploadprogress', [file, e.loaded, e.total]);        };      })(this),      error: (function(_this) {        return function(xhr, status, err) {          return _this.trigger('uploaderror', [file, xhr, status]);        };      })(this),      success: (function(_this) {        return function(result) {          _this.trigger('uploadprogress', [file, file.size, file.size]);          return _this.trigger('uploadsuccess', [file, result]);        };      })(this),      complete: (function(_this) {        return function(xhr, status) {          return _this.trigger('uploadcomplete', [file, xhr.responseText]);        };      })(this)    });  };  Uploader.prototype.cancel = function(file) {    var f, _i, _len, _ref;    if (!file.id) {      _ref = this.files;      for (_i = 0, _len = _ref.length; _i < _len; _i++) {        f = _ref[_i];        if (f.id === file * 1) {          file = f;          break;        }      }    }    this.trigger('uploadcancel', [file]);    if (file.xhr) {      file.xhr.abort();    }    return file.xhr = null;  };  Uploader.prototype.readImageFile = function(fileObj, callback) {    var fileReader, img;    if (!$.isFunction(callback)) {      return;    }    img = new Image();    img.onload = function() {      return callback(img);    };    img.onerror = function() {      return callback();    };    if (window.FileReader && FileReader.prototype.readAsDataURL && /^image/.test(fileObj.type)) {      fileReader = new FileReader();      fileReader.onload = function(e) {        return img.src = e.target.result;      };      return fileReader.readAsDataURL(fileObj);    } else {      return callback();    }  };  Uploader.prototype.destroy = function() {    var file, _i, _len, _ref;    this.queue.length = 0;    _ref = this.files;    for (_i = 0, _len = _ref.length; _i < _len; _i++) {      file = _ref[_i];      this.cancel(file);    }    $(window).off('.uploader-' + this.id);    return $(document).off('.uploader-' + this.id);  };  Uploader.i18n = {    'zh-CN': {      leaveConfirm: '正在上传文件,如果离开上传会自动取消'    }  };  Uploader.locale = 'zh-CN';  return Uploader;})(SimpleModule);uploader = function(opts) {  return new Uploader(opts);};return uploader;}));
 |