|
1 |
| -'use strict'; |
| 1 | +(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ |
| 2 | +"use strict"; |
2 | 3 |
|
3 |
| -var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); |
| 4 | +Object.defineProperty(exports, "__esModule", { |
| 5 | + value: true |
| 6 | +}); |
4 | 7 |
|
5 |
| -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } |
| 8 | +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } |
6 | 9 |
|
7 |
| -function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } |
| 10 | +var Profile = function Profile(uploader) { |
| 11 | + var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; |
| 12 | + |
| 13 | + _classCallCheck(this, Profile); |
| 14 | + |
| 15 | + this.uploader = uploader; |
| 16 | + |
| 17 | + this.name = options.name; |
| 18 | + this.bucket = options.bucket; |
| 19 | + this.domain = options.domain; |
| 20 | + this.ssl = options.ssl || false; |
| 21 | + this.uptokenUrl = options.uptokenUrl; |
| 22 | + |
| 23 | + this["public"] = true; |
| 24 | + if (options["public"] !== undefined) { |
| 25 | + this["public"] = options["public"]; |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +exports["default"] = Profile; |
| 30 | +module.exports = exports["default"]; |
| 31 | + |
| 32 | +},{}],2:[function(require,module,exports){ |
| 33 | +"use strict"; |
| 34 | + |
| 35 | +var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); |
| 36 | + |
| 37 | +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } |
| 38 | + |
| 39 | +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } |
8 | 40 |
|
9 | 41 | var _profile = require('./profile');
|
10 | 42 |
|
11 | 43 | var _profile2 = _interopRequireDefault(_profile);
|
12 | 44 |
|
| 45 | +var singleton = Symbol(); |
| 46 | +var singletonEnforcer = Symbol(); |
| 47 | + |
13 | 48 | var QiniuUploader = (function () {
|
14 |
| - function QiniuUploader() { |
15 |
| - var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; |
| 49 | + function QiniuUploader(enforcer) { |
| 50 | + var _this = this; |
| 51 | + |
| 52 | + var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; |
16 | 53 |
|
17 | 54 | _classCallCheck(this, QiniuUploader);
|
18 | 55 |
|
19 |
| - uptokenUrl = options.uptokenUrl; |
| 56 | + if (enforcer != singletonEnforcer) { |
| 57 | + throw "Cannot construct singleton"; |
| 58 | + } else { |
| 59 | + this.uptokenUrl = options.uptokenUrl; |
| 60 | + this.profiles = []; |
| 61 | + this.files = []; |
20 | 62 |
|
21 |
| - // Register profiles if any. |
22 |
| - if (Array.isArray(options.profiles)) { |
23 |
| - options.profiles.forEach(function (profile) { |
24 |
| - register(profile); |
25 |
| - }); |
| 63 | + // Register profiles if any. |
| 64 | + if (Array.isArray(options.profiles)) { |
| 65 | + options.profiles.forEach(function (profile) { |
| 66 | + _this.register(profile); |
| 67 | + }); |
| 68 | + } |
26 | 69 | }
|
27 | 70 | }
|
28 | 71 |
|
29 | 72 | _createClass(QiniuUploader, [{
|
30 |
| - key: 'register', |
| 73 | + key: "register", |
31 | 74 | value: function register(profile) {
|
32 |
| - profiles[profile.name] = new _profile2['default'](uploader, profile); |
| 75 | + this.profiles[profile.name] = new _profile2["default"](this, profile); |
| 76 | + } |
| 77 | + }, { |
| 78 | + key: "_makeInput", |
| 79 | + value: function _makeInput(element, profile) { |
| 80 | + var uploader = this; |
| 81 | + var input = document.createElement('input'); |
| 82 | + input.setAttribute('type', 'file'); |
| 83 | + |
| 84 | + Object.assign(input.style, { |
| 85 | + visibility: 'hidden', |
| 86 | + position: 'absolute', |
| 87 | + width: '1px', |
| 88 | + height: '1px' |
| 89 | + }); |
| 90 | + |
| 91 | + document.body.appendChild(input); |
| 92 | + input.addEventListener('change', function (e) { |
| 93 | + if (e.target.value) { |
| 94 | + uploader.addFiles(e.target.files, element, profile); |
| 95 | + } |
| 96 | + |
| 97 | + // Destroy the file input |
| 98 | + input.parentNode.removeChild(input); |
| 99 | + }, false); |
| 100 | + |
| 101 | + return input; |
| 102 | + } |
| 103 | + }, { |
| 104 | + key: "addFiles", |
| 105 | + value: function addFiles(files, element, profile) { |
| 106 | + for (var i = 0; i < files.length; i++) { |
| 107 | + var file = files[i]; |
| 108 | + |
| 109 | + this.files.push(file); |
| 110 | + |
| 111 | + var event = new CustomEvent('qinu:file:added', { |
| 112 | + bubbles: true, |
| 113 | + detail: { |
| 114 | + 'uploader': this, |
| 115 | + 'profile': profile |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + element.dispatchEvent(event); |
| 120 | + } |
| 121 | + } |
| 122 | + }, { |
| 123 | + key: "browse", |
| 124 | + value: function browse(element, profileName) { |
| 125 | + var profile = this.profiles[profileName]; |
| 126 | + if (!profile) throw "Profile '" + profileName + "' not registered"; |
| 127 | + |
| 128 | + var input = this._makeInput(element, profile); |
| 129 | + input.click(); |
| 130 | + } |
| 131 | + }], [{ |
| 132 | + key: "init", |
| 133 | + value: function init() { |
| 134 | + var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; |
| 135 | + |
| 136 | + if (this[singleton]) { |
| 137 | + throw 'Uploader has initialized.'; |
| 138 | + } |
| 139 | + |
| 140 | + this[singleton] = new QiniuUploader(singletonEnforcer, options); |
| 141 | + } |
| 142 | + }, { |
| 143 | + key: "instance", |
| 144 | + get: function get() { |
| 145 | + if (this[singleton]) { |
| 146 | + return this[singleton]; |
| 147 | + } else { |
| 148 | + throw "Uploader not configured."; |
| 149 | + } |
33 | 150 | }
|
34 | 151 | }]);
|
35 | 152 |
|
36 | 153 | return QiniuUploader;
|
37 | 154 | })();
|
38 |
| -//# sourceMappingURL=qiniu-uploader.js.map |
| 155 | + |
| 156 | +window.QiniuUploader = QiniuUploader; |
| 157 | + |
| 158 | +},{"./profile":1}]},{},[2]); |
0 commit comments