|
373 | 373 | * Q.Uploader.js 文件上传管理器 1.0 |
374 | 374 | * https://github.com/devin87/web-uploader |
375 | 375 | * author:devin87@qq.com |
376 | | -* update:2015/10/26 13:48 |
| 376 | +* update:2016/04/22 14:58 |
377 | 377 | */ |
378 | 378 | (function (window, undefined) { |
379 | 379 | "use strict"; |
|
695 | 695 | resetInput: function () { |
696 | 696 | var self = this, |
697 | 697 |
|
698 | | - boxInput = self.boxInput, |
699 | | - target = self.target, |
700 | | - |
701 | | - inputWidth = target.offsetWidth, |
702 | | - inputHeight = target.offsetHeight; |
| 698 | + boxInput = self.boxInput; |
703 | 699 |
|
704 | | - boxInput.innerHTML = '<input type="file" name="' + self.upName + '" style="' + (self.clickTrigger ? 'visibility: hidden;' : 'width:' + inputWidth + 'px;height:' + inputHeight + 'px;font-size:100px;') + '"' + (self.multiple ? ' multiple="multiple"' : '') + '>'; |
705 | | - boxInput.style.width = inputWidth + "px"; |
706 | | - boxInput.style.height = inputHeight + "px"; |
| 700 | + boxInput.innerHTML = '<input type="file" name="' + self.upName + '" style="' + (self.clickTrigger ? 'visibility: hidden;' : 'font-size:100px;') + '"' + (self.multiple ? ' multiple="multiple"' : '') + '>'; |
707 | 701 |
|
708 | 702 | var inputFile = getFirst(boxInput); |
709 | 703 |
|
|
717 | 711 |
|
718 | 712 | self.inputFile = inputFile; |
719 | 713 |
|
720 | | - self.updatePos(); |
721 | | - |
722 | | - return self; |
| 714 | + return self.updatePos(); |
723 | 715 | }, |
724 | 716 | //更新上传按钮坐标(for ie) |
725 | 717 | updatePos: function (has_more_uploader) { |
726 | | - if (this.clickTrigger) return; |
| 718 | + var self = this; |
| 719 | + if (self.clickTrigger) return self; |
| 720 | + |
| 721 | + var getPos = self.getPos || getOffset, |
727 | 722 |
|
728 | | - var getPos = this.getPos || getOffset; |
| 723 | + boxInput = self.boxInput, |
| 724 | + inputFile = getFirst(boxInput), |
| 725 | + target = self.target, |
| 726 | + |
| 727 | + inputWidth = target.offsetWidth, |
| 728 | + inputHeight = target.offsetHeight, |
729 | 729 |
|
730 | | - var boxInput = this.boxInput, |
731 | | - target = this.target, |
| 730 | + pos = inputWidth == 0 ? { left: -10000, top: -10000 } : getPos(target); |
732 | 731 |
|
733 | | - pos = target.offsetWidth == 0 ? { left: -10000, top: -10000 } : getPos(target); |
| 732 | + boxInput.style.width = inputFile.style.width = inputWidth + "px"; |
| 733 | + boxInput.style.height = inputFile.style.height = inputHeight + "px"; |
734 | 734 |
|
735 | 735 | boxInput.style.left = pos.left + "px"; |
736 | 736 | boxInput.style.top = pos.top + "px"; |
737 | 737 |
|
738 | 738 | //多用于选项卡切换中上传按钮位置重复的情况 |
739 | 739 | if (has_more_uploader) boxInput.style.zIndex = ++UPLOAD_HTML4_ZINDEX; |
| 740 | + |
| 741 | + return self; |
740 | 742 | }, |
741 | 743 | //触发ops上定义的回调方法,优先触发异步回调(以Async结尾) |
742 | 744 | fire: function (action, arg, callback) { |
|
1112 | 1114 | /* |
1113 | 1115 | * Q.Uploader.Image.js 图片上传管理器界面 |
1114 | 1116 | * author:devin87@qq.com |
1115 | | -* update:2015/10/27 09:26 |
| 1117 | +* update:2016/04/22 15:37 |
1116 | 1118 | */ |
1117 | 1119 | (function (window, undefined) { |
1118 | 1120 | "use strict"; |
|
1193 | 1195 | var Blob = window.Blob || window.WebkitBlob || window.MozBlob, |
1194 | 1196 | BlobBuilder = window.WebKitBlobBuilder || window.MozBlobBuilder; |
1195 | 1197 |
|
| 1198 | + //是否支持图片缩放 |
1196 | 1199 | var support_image_scale = (function () { |
1197 | 1200 | if (!window.FileReader || !window.atob || !(Blob || BlobBuilder)) return false; |
1198 | 1201 |
|
|
1276 | 1279 | //是否支持图片缩放 |
1277 | 1280 | Uploader.support.imageScale = support_image_scale; |
1278 | 1281 |
|
| 1282 | + Uploader.previewImage = previewImage; |
| 1283 | + Uploader.scaleImage = scaleImage; |
| 1284 | + |
| 1285 | + |
1279 | 1286 | //实现默认的UI接口 |
1280 | 1287 | Uploader.extend({ |
1281 | 1288 | //初始化 |
|
1302 | 1309 | return types == "*" || types.indexOf(type) != -1; |
1303 | 1310 | }, |
1304 | 1311 |
|
| 1312 | + //预览并压缩图片 |
| 1313 | + previewImage: function (boxImage, task, ops) { |
| 1314 | + var self = this, |
| 1315 | + scale_data = task.scale || ops.scale, |
| 1316 | + support_scale = scale_data && self.supportScale(task.ext); |
| 1317 | + |
| 1318 | + if (support_scale) task.skip = true; |
| 1319 | + |
| 1320 | + previewImage(boxImage, task, function (src) { |
| 1321 | + self.fire("preview", { task: task, src: src }); |
| 1322 | + |
| 1323 | + if (!src || !support_scale) return; |
| 1324 | + |
| 1325 | + scaleImage(src, get_image_mimetype(task.ext), scale_data, function (base64, mimetype) { |
| 1326 | + if (!base64) return; |
| 1327 | + |
| 1328 | + var blob = dataURLtoBlob(base64, mimetype); |
| 1329 | + task.blob = blob; |
| 1330 | + task.skip = false; |
| 1331 | + self.list.push(task); |
| 1332 | + |
| 1333 | + self.fire("scale", { task: task, base64: base64, type: mimetype, blob: blob }); |
| 1334 | + |
| 1335 | + if (self.auto) self.start(); |
| 1336 | + }); |
| 1337 | + }); |
| 1338 | + |
| 1339 | + return self; |
| 1340 | + }, |
| 1341 | + |
1305 | 1342 | //绘制任务UI |
1306 | 1343 | draw: function (task) { |
1307 | 1344 | var self = this, |
|
1330 | 1367 | boxProgress = getFirst(boxProgressbar), |
1331 | 1368 | boxDetail = getNext(boxProgressbar); |
1332 | 1369 |
|
1333 | | - var scale_data = task.scale || ops.scale, |
1334 | | - support_scale = scale_data && self.supportScale(task.ext); |
1335 | | - |
1336 | | - if (support_scale) task.skip = true; |
1337 | | - |
1338 | | - previewImage(boxImage, task, function (src) { |
1339 | | - self.fire("preview", src); |
1340 | | - |
1341 | | - if (!src || !support_scale) return; |
1342 | | - |
1343 | | - scaleImage(src, get_image_mimetype(task.ext), scale_data, function (base64, mimetype) { |
1344 | | - if (!base64) return; |
1345 | | - |
1346 | | - var blob = dataURLtoBlob(base64, mimetype); |
1347 | | - task.blob = blob; |
1348 | | - task.skip = false; |
1349 | | - self.list.push(task); |
1350 | | - if (self.auto) self.start(); |
1351 | | - }); |
1352 | | - }); |
1353 | | - |
1354 | 1370 | setOpacity(boxProgressbar, 0.3); |
1355 | 1371 | setOpacity(boxProgress, 0.5); |
1356 | 1372 |
|
|
1361 | 1377 | //添加到视图中 |
1362 | 1378 | boxView.appendChild(box); |
1363 | 1379 |
|
1364 | | - //---------------- 更新UI ---------------- |
1365 | | - self.update(task); |
| 1380 | + //---------------- 预览图片并更新UI ---------------- |
| 1381 | + self.previewImage(boxImage, task, ops).update(task); |
1366 | 1382 | }, |
1367 | 1383 |
|
1368 | 1384 | //更新任务界面 |
|
0 commit comments