Skip to content

Commit 26a35d3

Browse files
committed
Compile all modules in one with browserify.
Generate qiniu-uploader.js with sub modules.
1 parent 8fd3762 commit 26a35d3

9 files changed

+237
-38
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
/node_modules
1+
/node_modules
2+
/demo

Gruntfile.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ module.exports = function(grunt) {
1111
all: ['Gruntfile.js', 'src/**/*.js']
1212
},
1313

14-
babel: {
15-
options: {
16-
sourceMap: true
17-
},
14+
browserify: {
1815
dist: {
16+
options: {
17+
transform: [["babelify", { stage: 0 }]]
18+
},
1919
files: {
20-
'dist/qiniu-uploader.js': 'src/qiniu-uploader.js'
20+
"dist/qiniu-uploader.js": "src/qiniu-uploader.js"
2121
}
2222
}
2323
},
@@ -44,8 +44,9 @@ module.exports = function(grunt) {
4444
},
4545
});
4646

47-
grunt.registerTask('build', ['jshint', 'babel', 'uglify']);
48-
grunt.registerTask('default', ['build']);
47+
grunt.registerTask('build', ['jshint', 'browserify', 'uglify']);
48+
grunt.registerTask('default', ['build', 'watch']);
4949

5050
grunt.loadNpmTasks('grunt-contrib-watch');
51+
grunt.loadNpmTasks("grunt-browserify");
5152
};

dist/qiniu-uploader.js

+135-15
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,158 @@
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";
23

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+
});
47

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"); } }
69

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"); } }
840

941
var _profile = require('./profile');
1042

1143
var _profile2 = _interopRequireDefault(_profile);
1244

45+
var singleton = Symbol();
46+
var singletonEnforcer = Symbol();
47+
1348
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];
1653

1754
_classCallCheck(this, QiniuUploader);
1855

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 = [];
2062

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+
}
2669
}
2770
}
2871

2972
_createClass(QiniuUploader, [{
30-
key: 'register',
73+
key: "register",
3174
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+
}
33150
}
34151
}]);
35152

36153
return QiniuUploader;
37154
})();
38-
//# sourceMappingURL=qiniu-uploader.js.map
155+
156+
window.QiniuUploader = QiniuUploader;
157+
158+
},{"./profile":1}]},{},[2]);

dist/qiniu-uploader.js.map

-1
This file was deleted.

dist/qiniu-uploader.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/qiniu-uploader.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
},
1818
"homepage": "https://github.com/FellowPlusDev/FFQiniuUploader#readme",
1919
"devDependencies": {
20+
"babelify": "^6.3.0",
2021
"grunt": "^0.4.5",
21-
"grunt-babel": "^5.0.3",
22+
"grunt-browserify": "^4.0.1",
2223
"grunt-contrib-jshint": "^0.11.3",
2324
"grunt-contrib-uglify": "^0.9.2",
2425
"grunt-contrib-watch": "^0.6.1",

src/profile.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class Profile {
1+
export default class Profile {
22
constructor(uploader, options={}) {
33
this.uploader = uploader;
44

@@ -9,7 +9,8 @@ class Profile {
99
this.uptokenUrl = options.uptokenUrl;
1010

1111
this.public = true;
12-
if (options.has('public'))
12+
if (options.public !== undefined) {
1313
this.public = options.public;
14+
}
1415
}
1516
}

0 commit comments

Comments
 (0)