Skip to content
This repository was archived by the owner on Aug 22, 2022. It is now read-only.

Commit 98e5c1b

Browse files
committed
add ios6 support
1 parent f11638f commit 98e5c1b

File tree

7 files changed

+134
-83
lines changed

7 files changed

+134
-83
lines changed

css/webuploader.css

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
.webuploader-container {
22
position: relative;
33
}
4+
.webuploader-element-invisible {
5+
position: absolute !important;
6+
clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
7+
clip: rect(1px,1px,1px,1px);
8+
}
49
.webuploader-pick {
510
position: relative;
611
display: inline-block;
@@ -21,13 +26,3 @@
2126
pointer-events:none;
2227
}
2328

24-
.webuploader-dnd {
25-
width: 400px;
26-
height: 400px;
27-
border: 3px solid #ddd;
28-
}
29-
30-
.webuploader-dnd-over {
31-
border-style: dashed;
32-
}
33-

src/base.js

+21
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,27 @@ define([
101101
return ret;
102102
})( navigator.userAgent ),
103103

104+
/**
105+
* @description 操作系统检查结果。
106+
*
107+
* * `android` 如果在android浏览器环境下,此值为对应的android版本号,否则为`undefined`。
108+
* * `ios` 如果在ios浏览器环境下,此值为对应的ios版本号,否则为`undefined`。
109+
* @property {Object} [os]
110+
*/
111+
os: (function( ua ) {
112+
var ret = {},
113+
114+
// osx = !!ua.match( /\(Macintosh\; Intel / ),
115+
android = ua.match( /(?:Android);?[\s\/]+([\d.]+)?/ ),
116+
ios = ua.match( /(?:iPad|iPod|iPhone).*OS\s([\d_]+)/ );
117+
118+
// osx && (ret.osx = true);
119+
android && (ret.android = parseFloat( android[ 1 ] ));
120+
ios && (ret.ios = parseFloat( ios[ 1 ].replace( /_/g, '.' ) ));
121+
122+
return ret;
123+
})( navigator.userAgent ),
124+
104125
/**
105126
* 实现类与类之间的继承。
106127
* @method inherits

src/lib/file.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
/**
2+
* 为了统一化Flash的File和HTML5的File而存在。
3+
* 以至于要调用Flash里面的File,也可以像调用HTML5版本的File一下。
24
* @fileOverview File
35
*/
46
define([
57
'../base',
68
'./blob'
79
], function( Base, Blob ) {
810

9-
var uid = 0,
11+
var uid = 1,
1012
rExt = /\.([^.]+)$/;
1113

1214
function File( ruid, file ) {
@@ -16,6 +18,16 @@ define([
1618
this.name = file.name || ('untitled' + uid++);
1719
ext = rExt.exec( file.name ) ? RegExp.$1.toLowerCase() : '';
1820

21+
// todo 支持其他类型文件的转换。
22+
23+
// 如果有mimetype, 但是文件名里面没有找出后缀规律
24+
if ( !ext && this.type ) {
25+
ext = /\/(jpg|jpeg|png|gif|bmp)$/i.exec( this.type ) ?
26+
RegExp.$1.toLowerCase() : '';
27+
this.name += '.' + ext;
28+
}
29+
30+
// 如果没有指定mimetype, 但是知道文件后缀。
1931
if ( !this.type && ~'jpg,jpeg,png,gif,bmp'.indexOf( ext ) ) {
2032
this.type = 'image/' + ext;
2133
}

src/runtime/client.js

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ define([
5858
// already connected.
5959
if ( runtime ) {
6060
throw new Error('already connected!');
61-
return;
6261
}
6362

6463
deferred.done( cb );

src/runtime/html5/filepicker.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@ define([
1919
arr, i, len, mouseHandler;
2020

2121
input.attr( 'type', 'file' );
22-
23-
input.css({
24-
position: 'absolute',
25-
clip: 'rect(1px,1px,1px,1px)'
26-
});
22+
input.attr( 'name', opts.name );
23+
input.addClass('webuploader-element-invisible');
2724

2825
lable.on( 'click', function() {
2926
input.trigger('click');

src/runtime/html5/image.js

+91-65
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
* @fileOverview Image
33
*/
44
define([
5+
'../../base',
56
'./runtime',
67
'./util',
78
'./imagemeta'
8-
], function( Html5Runtime, Util, ImageMeta ) {
9+
], function( Base, Html5Runtime, Util, ImageMeta ) {
910

1011
var BLANK = 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D';
1112

@@ -27,7 +28,7 @@ define([
2728
};
2829

2930
// 读取meta信息。
30-
if ( !me._metas && ~'image/jpegimage/jpg'.indexOf( me.type ) ) {
31+
if ( !me._metas && 'image/jpeg' === me.type ) {
3132
ImageMeta.parse( me._blob, function( error, ret ) {
3233
me._metas = ret;
3334
me.owner.trigger('load');
@@ -251,101 +252,126 @@ define([
251252
}
252253
},
253254

254-
_renderImageToCanvas: function( canvas, img, x, y, w, h ) {
255-
canvas.getContext('2d').drawImage( img, x, y, w, h );
256-
}
255+
// https://github.com/stomita/ios-imagefile-megapixel/
256+
// blob/master/src/megapix-image.js
257+
_renderImageToCanvas: (function() {
257258

258-
/*_renderImageToCanvas: (function() {
259-
var subsampled, vertSquashRatio;
259+
// 如果不是ios6, 不需要这么复杂!
260+
if ( !Base.os.ios || ~~Base.os.ios !== 6 ) {
261+
return function( canvas, img, x, y, w, h ) {
262+
canvas.getContext('2d').drawImage( img, x, y, w, h );
263+
};
264+
}
260265

261-
// Detect subsampling in loaded image.
262-
// In iOS, larger images than 2M pixels may be subsampled in rendering.
263-
function detectSubsampling(img) {
266+
/**
267+
* Detect subsampling in loaded image.
268+
* In iOS, larger images than 2M pixels may be
269+
* subsampled in rendering.
270+
*/
271+
function detectSubsampling( img ) {
264272
var iw = img.naturalWidth,
265-
ih = img.naturalHeight;
266-
if (iw * ih > 1024 * 1024) { // subsampling may happen over megapixel image
267-
var canvas = document.createElement('canvas');
273+
ih = img.naturalHeight,
274+
canvas, ctx;
275+
276+
// subsampling may happen overmegapixel image
277+
if ( iw * ih > 1024 * 1024 ) {
278+
canvas = document.createElement('canvas');
268279
canvas.width = canvas.height = 1;
269-
var ctx = canvas.getContext('2d');
270-
ctx.drawImage(img, -iw + 1, 0);
280+
ctx = canvas.getContext('2d');
281+
ctx.drawImage( img, -iw + 1, 0 );
282+
271283
// subsampled image becomes half smaller in rendering size.
272-
// check alpha channel value to confirm image is covering edge pixel or not.
273-
// if alpha value is 0 image is not covering, hence subsampled.
274-
return ctx.getImageData(0, 0, 1, 1).data[3] === 0;
284+
// check alpha channel value to confirm image is covering
285+
// edge pixel or not. if alpha value is 0
286+
// image is not covering, hence subsampled.
287+
return ctx.getImageData( 0, 0, 1, 1 ).data[ 3 ] === 0;
275288
} else {
276289
return false;
277290
}
278291
}
279292

280293

281-
// Detecting vertical squash in loaded image.
282-
// Fixes a bug which squash image vertically while drawing into canvas for some images.
283-
function detectVerticalSquash(img, iw, ih) {
284-
var canvas = document.createElement('canvas');
294+
/**
295+
* Detecting vertical squash in loaded image.
296+
* Fixes a bug which squash image vertically while drawing into
297+
* canvas for some images.
298+
*/
299+
function detectVerticalSquash( img, iw, ih ) {
300+
var canvas = document.createElement('canvas'),
301+
ctx = canvas.getContext('2d'),
302+
sy = 0,
303+
ey = ih,
304+
py = ih,
305+
data, alpha, ratio;
306+
307+
285308
canvas.width = 1;
286309
canvas.height = ih;
287-
var ctx = canvas.getContext('2d');
288-
ctx.drawImage(img, 0, 0);
289-
var data = ctx.getImageData(0, 0, 1, ih).data;
290-
// search image edge pixel position in case it is squashed vertically.
291-
var sy = 0;
292-
var ey = ih;
293-
var py = ih;
294-
while (py > sy) {
295-
var alpha = data[(py - 1) * 4 + 3];
296-
if (alpha === 0) {
310+
ctx.drawImage( img, 0, 0 );
311+
data = ctx.getImageData( 0, 0, 1, ih ).data;
312+
313+
// search image edge pixel position in case
314+
// it is squashed vertically.
315+
while ( py > sy ) {
316+
alpha = data[ (py - 1) * 4 + 3 ];
317+
318+
if ( alpha === 0 ) {
297319
ey = py;
298320
} else {
299321
sy = py;
300322
}
323+
301324
py = (ey + sy) >> 1;
302325
}
303-
var ratio = (py / ih);
326+
327+
ratio = (py / ih);
304328
return (ratio === 0) ? 1 : ratio;
305329
}
306330

307-
return function( canvas, img, x, y, w, h ) {
308-
309-
310-
var iw = img.naturalWidth, ih = img.naturalHeight;
311-
var width = w, height = h;
312-
var ctx = canvas.getContext('2d');
313-
ctx.save();
331+
return function( canvas, img, x, y, width, height ) {
332+
var iw = img.naturalWidth,
333+
ih = img.naturalHeight,
334+
ctx = canvas.getContext('2d'),
335+
subsampled = detectSubsampling( img ),
336+
doSquash = this.type === 'image/jpeg',
337+
d = 1024,
338+
sy = 0,
339+
dy = 0,
340+
tmpCanvas, tmpCtx, vertSquashRatio, dw, dh, sx, dx;
314341

315-
subsampled = typeof subsampled === 'undefined' ? detectSubsampling( img ) : subsampled;
316342
if ( subsampled ) {
317343
iw /= 2;
318344
ih /= 2;
319345
}
320346

321-
var d = 1024; // size of tiling canvas
322-
var tmpCanvas = document.createElement('canvas');
347+
ctx.save();
348+
tmpCanvas = document.createElement('canvas');
323349
tmpCanvas.width = tmpCanvas.height = d;
324-
var tmpCtx = tmpCanvas.getContext('2d');
325-
326-
vertSquashRatio = vertSquashRatio || detectVerticalSquash(img, iw, ih);
327-
console.log( vertSquashRatio );
328-
329-
var dw = Math.ceil(d * width / iw);
330-
var dh = Math.ceil(d * height / ih / vertSquashRatio);
331-
var sy = 0;
332-
var dy = 0;
333-
while (sy < ih) {
334-
var sx = 0;
335-
var dx = 0;
336-
while (sx < iw) {
337-
tmpCtx.clearRect(0, 0, d, d);
338-
tmpCtx.drawImage(img, x - sx, y - sy );
339-
ctx.drawImage(tmpCanvas, 0, 0, d, d, dx, dy, dw, dh);
340-
sx += d;
341-
dx += dw;
342-
}
343-
sy += d;
344-
dy += dh;
350+
351+
tmpCtx = tmpCanvas.getContext('2d');
352+
vertSquashRatio = doSquash ?
353+
detectVerticalSquash( img, iw, ih ) : 1;
354+
355+
dw = Math.ceil( d * width / iw );
356+
dh = Math.ceil( d * height / ih / vertSquashRatio );
357+
358+
while ( sy < ih ) {
359+
sx = 0;
360+
dx = 0;
361+
while ( sx < iw ) {
362+
tmpCtx.clearRect( 0, 0, d, d );
363+
tmpCtx.drawImage( img, -sx, -sy );
364+
ctx.drawImage( tmpCanvas, 0, 0, d, d,
365+
x + dx, y + dy, dw, dh );
366+
sx += d;
367+
dx += dw;
368+
}
369+
sy += d;
370+
dy += dh;
345371
}
346372
ctx.restore();
347373
tmpCanvas = tmpCtx = null;
348374
};
349-
})()*/
375+
})()
350376
});
351377
});

src/widgets/queue.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,13 @@ define([
5555
me.queue = new Queue();
5656
me.stats = me.queue.stats;
5757

58+
// 如果当前不是html5运行时,那就算了。
5859
if ( this.request('predict-runtime-type') !== 'html5' ) {
5960
return;
6061
}
6162

6263
deferred = Base.Deferred();
63-
runtime = new RuntimeClient( 'Placeholder' );
64+
runtime = new RuntimeClient('Placeholder');
6465
runtime.connectRuntime({
6566
runtimeOrder: 'html5'
6667
}, function() {

0 commit comments

Comments
 (0)