upload.js 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. /******/ (function(modules) { // webpackBootstrap
  2. /******/ // The module cache
  3. /******/ var installedModules = {};
  4. /******/
  5. /******/ // The require function
  6. /******/ function __webpack_require__(moduleId) {
  7. /******/
  8. /******/ // Check if module is in cache
  9. /******/ if(installedModules[moduleId]) {
  10. /******/ return installedModules[moduleId].exports;
  11. /******/ }
  12. /******/ // Create a new module (and put it into the cache)
  13. /******/ var module = installedModules[moduleId] = {
  14. /******/ i: moduleId,
  15. /******/ l: false,
  16. /******/ exports: {}
  17. /******/ };
  18. /******/
  19. /******/ // Execute the module function
  20. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  21. /******/
  22. /******/ // Flag the module as loaded
  23. /******/ module.l = true;
  24. /******/
  25. /******/ // Return the exports of the module
  26. /******/ return module.exports;
  27. /******/ }
  28. /******/
  29. /******/
  30. /******/ // expose the modules object (__webpack_modules__)
  31. /******/ __webpack_require__.m = modules;
  32. /******/
  33. /******/ // expose the module cache
  34. /******/ __webpack_require__.c = installedModules;
  35. /******/
  36. /******/ // define getter function for harmony exports
  37. /******/ __webpack_require__.d = function(exports, name, getter) {
  38. /******/ if(!__webpack_require__.o(exports, name)) {
  39. /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
  40. /******/ }
  41. /******/ };
  42. /******/
  43. /******/ // define __esModule on exports
  44. /******/ __webpack_require__.r = function(exports) {
  45. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  46. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  47. /******/ }
  48. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  49. /******/ };
  50. /******/
  51. /******/ // create a fake namespace object
  52. /******/ // mode & 1: value is a module id, require it
  53. /******/ // mode & 2: merge all properties of value into the ns
  54. /******/ // mode & 4: return value when already ns object
  55. /******/ // mode & 8|1: behave like require
  56. /******/ __webpack_require__.t = function(value, mode) {
  57. /******/ if(mode & 1) value = __webpack_require__(value);
  58. /******/ if(mode & 8) return value;
  59. /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  60. /******/ var ns = Object.create(null);
  61. /******/ __webpack_require__.r(ns);
  62. /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  63. /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  64. /******/ return ns;
  65. /******/ };
  66. /******/
  67. /******/ // getDefaultExport function for compatibility with non-harmony modules
  68. /******/ __webpack_require__.n = function(module) {
  69. /******/ var getter = module && module.__esModule ?
  70. /******/ function getDefault() { return module['default']; } :
  71. /******/ function getModuleExports() { return module; };
  72. /******/ __webpack_require__.d(getter, 'a', getter);
  73. /******/ return getter;
  74. /******/ };
  75. /******/
  76. /******/ // Object.prototype.hasOwnProperty.call
  77. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  78. /******/
  79. /******/ // __webpack_public_path__
  80. /******/ __webpack_require__.p = "/";
  81. /******/
  82. /******/
  83. /******/ // Load entry module and return exports
  84. /******/ return __webpack_require__(__webpack_require__.s = 3);
  85. /******/ })
  86. /************************************************************************/
  87. /******/ ({
  88. /***/ "./resources/assets/dcat/extra/upload.js":
  89. /*!***********************************************!*\
  90. !*** ./resources/assets/dcat/extra/upload.js ***!
  91. \***********************************************/
  92. /*! no static exports found */
  93. /***/ (function(module, exports) {
  94. (function (w, $) {
  95. function Uploader(opts) {
  96. opts = $.extend({
  97. wrapper: '.web-uploader',
  98. // 图片显示容器选择器
  99. addFileButton: '.add-file-button',
  100. // 继续添加按钮选择器
  101. isImage: false,
  102. preview: [],
  103. // 数据预览
  104. deleteUrl: '',
  105. deleteData: {},
  106. thumbHeight: 160,
  107. disabled: false,
  108. // 禁止任何上传编辑
  109. autoUpdateColumn: false,
  110. disableRemove: false,
  111. // 禁止删除图片,允许替换
  112. dimensions: {// width: 100, // 图片宽限制
  113. // height: 100, // 图片高限制
  114. // min_width: 100, //
  115. // min_height: 100,
  116. // max_width: 100,
  117. // max_height: 100,
  118. // ratio: 3/2, // 宽高比
  119. },
  120. lang: {
  121. exceed_size: '文件大小超出',
  122. interrupt: '上传暂停',
  123. upload_failed: '上传失败,请重试',
  124. selected_files: '选中:num个文件,共:size。',
  125. selected_has_failed: '已成功上传:success个文件,:fail个文件上传失败,<a class="retry" href="javascript:"";">重新上传</a>失败文件或<a class="ignore" href="javascript:"";">忽略</a>',
  126. selected_success: '共:num个(:size),已上传:success个。',
  127. dot: ',',
  128. failed_num: '失败:fail个。',
  129. pause_upload: '暂停上传',
  130. go_on_upload: '继续上传',
  131. start_upload: '开始上传',
  132. upload_success_message: '已成功上传:success个文件',
  133. go_on_add: '继续添加',
  134. Q_TYPE_DENIED: '对不起,不允许上传此类型文件',
  135. Q_EXCEED_NUM_LIMIT: '对不起,已超出文件上传数量限制,最多只能上传:num个文件',
  136. F_EXCEED_SIZE: '对不起,当前选择的文件过大',
  137. Q_EXCEED_SIZE_LIMIT: '对不起,已超出文件大小限制',
  138. F_DUPLICATE: '文件重复'
  139. },
  140. upload: {
  141. // web-uploader配置
  142. formData: {
  143. _id: null // 唯一id
  144. },
  145. thumb: {
  146. width: 160,
  147. height: 160,
  148. quality: 70,
  149. allowMagnify: true,
  150. crop: true,
  151. preserveHeaders: false,
  152. // 为空的话则保留原有图片格式。
  153. // 否则强制转换成指定的类型。
  154. // IE 8下面 base64 大小不能超过 32K 否则预览失败,而非 jpeg 编码的图片很可
  155. // 能会超过 32k, 所以这里设置成预览的时候都是 image/jpeg
  156. type: 'image/jpeg'
  157. }
  158. }
  159. }, opts);
  160. var $selector = $(opts.selector),
  161. updateColumn = opts.upload.formData.upload_column || 'webup' + Math.floor(Math.random() * 10000),
  162. elementName = opts.elementName;
  163. if (typeof opts.upload.formData._id == "undefined" || !opts.upload.formData._id) {
  164. opts.upload.formData._id = updateColumn + Math.floor(Math.random() * 10000);
  165. }
  166. var Dcat = w.Dcat,
  167. $wrap,
  168. // 展示图片
  169. showImg = opts.isImage,
  170. // 图片容器
  171. $queue,
  172. // 状态栏,包括进度和控制按钮
  173. $statusBar,
  174. // 文件总体选择信息。
  175. $info,
  176. // 上传按钮
  177. $upload,
  178. // 没选择文件之前的内容。
  179. $placeHolder,
  180. $progress,
  181. // 已上传文件数量
  182. originalFilesNum = Dcat.helpers.len(opts.preview),
  183. // 上传表单
  184. $input = $selector.find('input[name="' + elementName + '"]'),
  185. // 获取文件视图选择器
  186. getFileViewSelector = function getFileViewSelector(fileId) {
  187. return elementName.replace(/[\[\]]*/g, '_') + '-' + fileId;
  188. },
  189. getFileView = function getFileView(fileId) {
  190. return $('#' + getFileViewSelector(fileId));
  191. },
  192. // 继续添加按钮选择器
  193. addFileButtonSelector = opts.addFileButton,
  194. // 临时存储上传失败的文件,key为file id
  195. faildFiles = {},
  196. // 临时存储添加到form表单的文件
  197. formFiles = {},
  198. // 添加的文件数量
  199. fileCount = 0,
  200. // 添加的文件总大小
  201. fileSize = 0,
  202. // 可能有pedding, ready, uploading, confirm, done.
  203. state = 'pedding',
  204. // 所有文件的进度信息,key为file id
  205. percentages = {},
  206. // 判断浏览器是否支持图片的base64
  207. isSupportBase64 = function () {
  208. var data = new Image();
  209. var support = true;
  210. data.onload = data.onerror = function () {
  211. if (this.width != 1 || this.height != 1) {
  212. support = false;
  213. }
  214. };
  215. data.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
  216. return support;
  217. }(),
  218. // 检测是否已经安装flash,检测flash的版本
  219. flashVersion = function () {
  220. var version;
  221. try {
  222. version = navigator.plugins['Shockwave Flash'];
  223. version = version.description;
  224. } catch (ex) {
  225. try {
  226. version = new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version');
  227. } catch (ex2) {
  228. version = '0.0';
  229. }
  230. }
  231. version = version.match(/\d+/g);
  232. return parseFloat(version[0] + '.' + version[1], 10);
  233. }(),
  234. // 判断是否是图片
  235. isImage = function isImage(file) {
  236. return file.type.match(/^image/);
  237. },
  238. // 翻译
  239. lang = Dcat.Translator(opts.lang),
  240. __ = lang.trans.bind(lang),
  241. // WebUploader实例
  242. uploader; // 当有文件添加进来时执行,负责view的创建
  243. function addFile(file) {
  244. var size = WebUploader.formatSize(file.size),
  245. $li,
  246. $btns;
  247. if (showImg) {
  248. $li = $('<li id="' + getFileViewSelector(file.id) + '" title="' + file.name + '" style="margin:7px">' + '<p class="file-type">' + (file.ext.toUpperCase() || 'FILE') + '</p>' + '<p class="imgWrap "></p>' + '<p class="title" style="">' + file.name + '</p>' + '<p class="title" style=\'margin-bottom:20px;\'>(<b>' + size + '</b>)</p>' + '</li>');
  249. $btns = $('<div class="file-panel">' + '<a class=\'btn btn-sm btn-white\' data-file-act="cancel"><i class="fa fa-close red-dark" style=\'font-size:13px\'></i></a>' + '<a class=\'btn btn-sm btn-white\' data-file-act="delete" style="display: none"><i class="feather icon-trash red-dark" style=\'font-size:13px\'></i></a>' + '<a class=\'btn btn-sm btn-white\' data-file-act="preview" ><i class="feather icon-zoom-in"></i></a>' + '</div>').appendTo($li);
  250. } else {
  251. $li = $('<li id="' + getFileViewSelector(file.id) + '" title="' + file.name + '">' + '<p class="title" style="display:block"><i class=\'feather icon-check green _success\' style=\'font-weight:bold;font-size:17px;display:none\'></i>' + file.name + ' (' + size + ')</p>' + '</li>');
  252. $btns = $('<span data-file-act="cancel" class="_act" style="font-size:13px"><i class=\'feather icon-x red-dark\'></i></span>' + '<span data-file-act="delete" class="_act" style="display:none"><i class=\'feather icon-trash red-dark\'></i></span>').appendTo($li);
  253. }
  254. var $wrap = $li.find('p.imgWrap'),
  255. $info = $('<p class="error"></p>'),
  256. showError = function showError(code, file) {
  257. var text = '';
  258. switch (code) {
  259. case 'exceed_size':
  260. text = __('exceed_size');
  261. break;
  262. case 'interrupt':
  263. text = __('interrupt');
  264. break;
  265. default:
  266. text = __('upload_failed');
  267. break;
  268. }
  269. faildFiles[file.id] = file;
  270. $info.text(text).appendTo($li);
  271. };
  272. $li.appendTo($queue);
  273. if (file.getStatus() === 'invalid') {
  274. showError(file.statusText, file);
  275. } else {
  276. if (showImg) {
  277. var image = uploader.makeThumb(file, function (error, src) {
  278. var img;
  279. $wrap.empty();
  280. if (error) {
  281. $li.find('.title').show();
  282. $li.find('.file-type').show();
  283. return;
  284. }
  285. if (isSupportBase64) {
  286. img = $('<img src="' + src + '">');
  287. $wrap.append(img);
  288. } else {
  289. $li.find('.file-type').show();
  290. }
  291. });
  292. try {
  293. image.once('load', function () {
  294. file._info = file._info || image.info();
  295. file._meta = file._meta || image.meta();
  296. var width = file._info.width,
  297. height = file._info.height;
  298. if (!validateDimensions(file)) {
  299. Dcat.error('The image dimensions is invalid.');
  300. uploader.removeFile(file);
  301. return false;
  302. }
  303. image.resize(width, height);
  304. });
  305. } catch (e) {
  306. // 不是图片
  307. return setTimeout(function () {
  308. uploader.removeFile(file);
  309. }, 10);
  310. }
  311. }
  312. percentages[file.id] = [file.size, 0];
  313. file.rotation = 0;
  314. }
  315. file.on('statuschange', function (cur, prev) {
  316. if (prev === 'progress') {// $prgress.hide().width(0);
  317. } else if (prev === 'queued') {
  318. $btns.find('[data-file-act="cancel"]').hide();
  319. $btns.find('[data-file-act="delete"]').show();
  320. } // 成功
  321. if (cur === 'error' || cur === 'invalid') {
  322. showError(file.statusText, file);
  323. percentages[file.id][1] = 1;
  324. } else if (cur === 'interrupt') {
  325. showError('interrupt', file);
  326. } else if (cur === 'queued') {
  327. percentages[file.id][1] = 0;
  328. } else if (cur === 'progress') {
  329. $info.remove(); // $prgress.css('display', 'block');
  330. } else if (cur === 'complete') {
  331. if (showImg) {
  332. $li.append('<span class="success"><em></em><i class="feather icon-check"></i></span>');
  333. } else {
  334. $li.find('._success').show();
  335. }
  336. }
  337. $li.removeClass('state-' + prev).addClass('state-' + cur);
  338. });
  339. var $act = showImg ? $btns.find('a') : $btns;
  340. $act.on('click', function () {
  341. var index = $(this).data('file-act');
  342. switch (index) {
  343. case 'cancel':
  344. uploader.removeFile(file);
  345. return;
  346. case 'deleteurl':
  347. case 'delete':
  348. if (opts.disableRemove) {
  349. return uploader.removeFile(file);
  350. }
  351. var post = opts.deleteData;
  352. post.key = file.serverId;
  353. if (!post.key) {
  354. return uploader.removeFile(file);
  355. }
  356. post._column = updateColumn;
  357. Dcat.loading();
  358. $.post(opts.deleteUrl, post, function (result) {
  359. Dcat.loading(false);
  360. if (result.status) {
  361. deleteInput(file.serverId);
  362. uploader.removeFile(file);
  363. return;
  364. }
  365. Dcat.error(result.message || 'Remove file failed.');
  366. });
  367. break;
  368. case 'preview':
  369. Dcat.helpers.previewImage($wrap.find('img').attr('src'), null, file.name);
  370. break;
  371. }
  372. });
  373. } // 图片宽高验证
  374. function validateDimensions(file) {
  375. // The image dimensions is invalid.
  376. if (!showImg || !isImage(file) || !Dcat.helpers.len(opts.dimensions)) return true;
  377. var dimensions = opts.dimensions,
  378. width = file._info.width,
  379. height = file._info.height,
  380. isset = Dcat.helpers.isset;
  381. if (isset(dimensions, 'width') && dimensions['width'] != width || isset(dimensions, 'min_width') && dimensions['min_width'] > width || isset(dimensions, 'max_width') && dimensions['max_width'] < width || isset(dimensions, 'height') && dimensions['height'] != height || isset(dimensions, 'min_height') && dimensions['min_height'] > height || isset(dimensions, 'max_height') && dimensions['max_height'] < height || isset(dimensions, 'ratio') && dimensions['ratio'] != width / height) {
  382. return false;
  383. }
  384. return true;
  385. } // 负责view的销毁
  386. function removeUploadFile(file) {
  387. var $li = getFileView(file.id);
  388. delete percentages[file.id];
  389. updateTotalProgress();
  390. $li.off().find('.file-panel').off().end().remove();
  391. }
  392. function updateTotalProgress() {
  393. var loaded = 0,
  394. total = 0,
  395. $bar = $progress.find('.progress-bar'),
  396. percent;
  397. $.each(percentages, function (k, v) {
  398. total += v[0];
  399. loaded += v[0] * v[1];
  400. });
  401. percent = total ? loaded / total : 0;
  402. percent = Math.round(percent * 100) + '%';
  403. $bar.text(percent);
  404. $bar.css('width', percent);
  405. updateStatusText();
  406. }
  407. function updateStatusText() {
  408. var text = '',
  409. stats;
  410. if (!uploader) {
  411. return;
  412. }
  413. if (state === 'ready') {
  414. stats = uploader.getStats();
  415. if (fileCount) {
  416. text = __('selected_files', {
  417. num: fileCount,
  418. size: WebUploader.formatSize(fileSize)
  419. });
  420. } else {
  421. showSuccess();
  422. }
  423. } else if (state === 'confirm') {
  424. stats = uploader.getStats();
  425. if (stats.uploadFailNum) {
  426. text = __('selected_has_failed', {
  427. success: stats.successNum,
  428. fail: stats.uploadFailNum
  429. });
  430. }
  431. } else {
  432. showSuccess();
  433. }
  434. function showSuccess() {
  435. stats = uploader.getStats();
  436. if (stats.successNum) {
  437. text = __('selected_success', {
  438. num: fileCount,
  439. size: WebUploader.formatSize(fileSize),
  440. success: stats.successNum
  441. });
  442. }
  443. if (stats.uploadFailNum) {
  444. text += (text ? __('dot') : '') + __('failed_num', {
  445. fail: stats.uploadFailNum
  446. });
  447. }
  448. }
  449. $info.html(text);
  450. } // 上传文件后修改字段值
  451. function updateFileColumn() {
  452. var values = getInput(),
  453. num = uploader.getStats().successNum,
  454. form = $.extend({}, opts.formData);
  455. if (!num || !values || !opts.autoUpdateColumn) {
  456. return;
  457. }
  458. form[updateColumn] = values.join(',');
  459. delete form['upload_column'];
  460. $.post(opts.server, form);
  461. }
  462. function setState(val, args) {
  463. var stats;
  464. args = args || {};
  465. if (val === state) {
  466. return;
  467. }
  468. if ($upload) {
  469. $upload.removeClass('state-' + state);
  470. $upload.addClass('state-' + val);
  471. }
  472. state = val;
  473. switch (state) {
  474. case 'pedding':
  475. if (opts.disabled) return;
  476. $placeHolder.removeClass('element-invisible');
  477. $queue.hide();
  478. $statusBar.addClass('element-invisible');
  479. if (showImg) {
  480. $wrap.removeAttr('style');
  481. $wrap.find('.queueList').removeAttr('style');
  482. }
  483. refreshButton();
  484. break;
  485. case 'ready':
  486. $placeHolder.addClass('element-invisible');
  487. $selector.find(addFileButtonSelector).removeClass('element-invisible');
  488. $queue.show();
  489. if (!opts.disabled) {
  490. $statusBar.removeClass('element-invisible');
  491. }
  492. refreshButton();
  493. if (showImg) {
  494. $wrap.find('.queueList').css({
  495. 'border': '1px solid #d3dde5',
  496. 'padding': '5px'
  497. }); // $wrap.find('.queueList').removeAttr('style');
  498. }
  499. break;
  500. case 'uploading':
  501. $selector.find(addFileButtonSelector).addClass('element-invisible');
  502. $progress.show();
  503. $upload.text(__('pause_upload'));
  504. break;
  505. case 'paused':
  506. $progress.show();
  507. $upload.text(__('go_on_upload'));
  508. break;
  509. case 'confirm':
  510. if (uploader) {
  511. $progress.hide();
  512. $selector.find(addFileButtonSelector).removeClass('element-invisible');
  513. $upload.text(__('start_upload'));
  514. stats = uploader.getStats();
  515. if (stats.successNum && !stats.uploadFailNum) {
  516. setState('finish');
  517. return;
  518. }
  519. }
  520. break;
  521. case 'finish':
  522. if (uploader) {
  523. stats = uploader.getStats();
  524. if (stats.successNum) {
  525. Dcat.success(__('upload_success_message', {
  526. success: stats.successNum
  527. }));
  528. } else {
  529. // 没有成功的图片,重设
  530. state = 'done';
  531. location.reload();
  532. }
  533. }
  534. break;
  535. case 'decrOriginalFileNum':
  536. if (originalFilesNum > 0) originalFilesNum--;
  537. break;
  538. case 'incrOriginalFileNum':
  539. originalFilesNum++;
  540. break;
  541. case 'decrFileNumLimit':
  542. // 减少上传文件数量限制
  543. if (!uploader) {
  544. return;
  545. }
  546. var ofl = uploader.option('fileNumLimit'),
  547. num = args.num || 1;
  548. if (ofl == '-1') ofl = 0;
  549. num = ofl >= num ? ofl - num : 0;
  550. if (num == 0) num = '-1';
  551. uploader.option('fileNumLimit', num);
  552. break;
  553. case 'incrFileNumLimit':
  554. // 增加上传文件数量限制
  555. if (!uploader) {
  556. return;
  557. }
  558. var ofl = uploader.option('fileNumLimit'),
  559. num = args.num || 1;
  560. if (ofl == '-1') ofl = 0;
  561. num = ofl + num;
  562. uploader.option('fileNumLimit', num);
  563. break;
  564. case 'init':
  565. // 初始化
  566. $upload.addClass('state-' + state);
  567. updateTotalProgress();
  568. if (originalFilesNum || opts.disabled) {
  569. $placeHolder.addClass('element-invisible');
  570. if (!opts.disabled) {
  571. $statusBar.show();
  572. } else {
  573. $wrap.addClass('disabled');
  574. }
  575. setState('ready');
  576. } else if (showImg) {
  577. $wrap.removeAttr('style');
  578. $wrap.find('.queueList').css('margin', '0');
  579. }
  580. refreshButton();
  581. break;
  582. }
  583. updateStatusText();
  584. } // 移除form表单的文件
  585. function removeFormFile(fileId) {
  586. if (!fileId) return;
  587. var file = formFiles[fileId];
  588. deleteInput(fileId);
  589. delete formFiles[fileId];
  590. if (uploader && !file.fake) {
  591. uploader.removeFile(file);
  592. }
  593. setState('decrOriginalFileNum');
  594. setState('incrFileNumLimit');
  595. if (!Dcat.helpers.len(formFiles) && !Dcat.helpers.len(percentages)) {
  596. setState('pedding');
  597. }
  598. } // 获取表单值
  599. function getInput() {
  600. var val = $input.val();
  601. return val ? val.split(',') : [];
  602. } // 新增表单值
  603. function addInput(id) {
  604. var val = getInput();
  605. val.push(id);
  606. setInput(val);
  607. } // 设置表单值
  608. function setInput(arr) {
  609. arr = arr.filter(function (v, k, self) {
  610. return self.indexOf(v) === k;
  611. }).filter(function (v) {
  612. return v ? true : false;
  613. });
  614. $input.val(arr.join(','));
  615. } // 删除表单值
  616. function deleteInput(id) {
  617. if (!id) {
  618. return $input.val('');
  619. }
  620. setInput(getInput().filter(function (v) {
  621. return v != id;
  622. }));
  623. } // 重新计算按钮定位
  624. function refreshButton() {
  625. uploader.refresh();
  626. } // 添加上传成功文件到表单区域
  627. function appendUploadedFileForm(file) {
  628. var html = "";
  629. html += "<li title='" + file.serverPath + "'>";
  630. if (showImg) {
  631. html += "<p class='imgWrap'>";
  632. html += " <img src='" + file.serverUrl + "'>";
  633. html += "</p>";
  634. } else if (!opts.disabled) {
  635. html += '<p class="_act" data-file-act=\'delete\' data-id="' + file.serverId + '"><i class=\'ti-trash red-dark\'></i></p>';
  636. }
  637. html += "<p class='title' style=''><i class='feather icon-check' style='color:white;font-weight:bold;font-size:17px;display:none'></i>";
  638. html += file.serverPath;
  639. html += "</p>";
  640. if (showImg) {
  641. html += "<p class='title' style='margin-bottom:20px;'>&nbsp;</p>";
  642. html += "<div class='file-panel' >";
  643. if (!opts.disabled) {
  644. html += "<a class='btn btn-sm btn-white' data-file-act='deleteurl' data-id='" + file.serverId + "'><i class='feather icon-trash red-dark' style='font-size:13px'></i></a>";
  645. }
  646. html += "<a class='btn btn-sm btn-white' data-file-act='preview' data-url='" + file.serverUrl + "' ><i class='feather icon-zoom-in'></i></a>";
  647. html += "</div>";
  648. }
  649. html += "</li>";
  650. html = $(html);
  651. if (!showImg) {
  652. html.find('.file-type').show();
  653. html.find('.title').show();
  654. $wrap.css('background', 'transparent');
  655. }
  656. var deleteFile = function deleteFile() {
  657. var fileId = $(this).data('id'),
  658. post = opts.deleteData;
  659. if (opts.disableRemove) {
  660. html.remove();
  661. return removeFormFile(fileId);
  662. }
  663. post.key = fileId;
  664. post._column = updateColumn;
  665. Dcat.loading();
  666. $.post(opts.deleteUrl, post, function (result) {
  667. Dcat.loading(false);
  668. if (result.status) {
  669. // 移除
  670. html.remove();
  671. removeFormFile(fileId);
  672. return;
  673. }
  674. Dcat.error(result.message || 'Remove file failed.');
  675. });
  676. }; // 删除按钮点击事件
  677. html.find('[data-file-act="deleteurl"]').click(deleteFile);
  678. html.find('[data-file-act="delete"]').click(deleteFile); // 放大图片
  679. html.find('[data-file-act="preview"]').click(function () {
  680. var url = $(this).data('url');
  681. Dcat.helpers.previewImage(url);
  682. });
  683. setState('incrOriginalFileNum');
  684. setState('decrFileNumLimit');
  685. formFiles[file.serverId] = file;
  686. addInput(file.serverId);
  687. $queue.append(html);
  688. if (showImg) {
  689. setTimeout(function () {
  690. html.css('margin', '7px');
  691. }, 80);
  692. }
  693. } // 初始化web-uploader
  694. function build() {
  695. $wrap = $selector.find(opts.wrapper); // 图片容器
  696. $queue = $('<ul class="filelist"></ul>').appendTo($wrap.find('.queueList')); // 状态栏,包括进度和控制按钮
  697. $statusBar = $wrap.find('.statusBar'); // 文件总体选择信息。
  698. $info = $statusBar.find('.info'); // 上传按钮
  699. $upload = $wrap.find('.uploadBtn'); // 没选择文件之前的内容。
  700. $placeHolder = $wrap.find('.placeholder');
  701. $progress = $statusBar.find('.upload-progress').hide(); // IE;
  702. supportIe(); // 实例化
  703. uploader = WebUploader.create(opts.upload); // 拖拽时不接受 js, txt 文件。
  704. uploader.on('dndAccept', function (items) {
  705. var denied = false,
  706. len = items.length,
  707. i = 0,
  708. // 修改js类型
  709. unAllowed = 'text/plain;application/javascript ';
  710. for (; i < len; i++) {
  711. // 如果在列表里面
  712. if (~unAllowed.indexOf(items[i].type)) {
  713. denied = true;
  714. break;
  715. }
  716. }
  717. return !denied;
  718. });
  719. if (opts.upload.fileNumLimit > 1 && !opts.disabled) {
  720. // 添加“添加文件”的按钮,
  721. uploader.addButton({
  722. id: addFileButtonSelector,
  723. label: '<i class="glyphicon glyphicon-folder-open"></i> &nbsp;' + __('go_on_add')
  724. });
  725. }
  726. uploader.onUploadProgress = function (file, percentage) {
  727. percentages[file.id][1] = percentage;
  728. updateTotalProgress();
  729. };
  730. uploader.onBeforeFileQueued = function (file) {};
  731. uploader.onFileQueued = function (file) {
  732. fileCount++;
  733. fileSize += file.size;
  734. if (fileCount === 1) {
  735. $placeHolder.addClass('element-invisible');
  736. $statusBar.show();
  737. }
  738. addFile(file);
  739. setState('ready');
  740. updateTotalProgress();
  741. }; // 删除文件事件监听
  742. uploader.onFileDequeued = function (file) {
  743. fileCount--;
  744. fileSize -= file.size;
  745. if (!fileCount && !Dcat.helpers.len(formFiles)) {
  746. setState('pedding');
  747. }
  748. removeUploadFile(file);
  749. };
  750. uploader.on('all', function (type, obj, reason) {
  751. switch (type) {
  752. case 'uploadFinished':
  753. setState('confirm');
  754. updateFileColumn();
  755. break;
  756. case 'startUpload':
  757. setState('uploading');
  758. break;
  759. case 'stopUpload':
  760. setState('paused');
  761. break;
  762. case 'uploadAccept':
  763. // 上传失败,返回false
  764. if (reason && reason.error) {
  765. Dcat.error(reason.error.message);
  766. faildFiles[obj.file.id] = obj.file;
  767. return false;
  768. }
  769. if (reason.merge) {
  770. // 分片上传
  771. return;
  772. } // 上传成功,保存新文件名和路径到file对象
  773. obj.file.serverId = reason.id;
  774. obj.file.serverName = reason.name;
  775. obj.file.serverPath = reason.path;
  776. obj.file.serverUrl = reason.url || null;
  777. addInput(reason.id);
  778. if (!showImg) {
  779. var $li = getFileView(obj.file.id);
  780. $li.find('._act').hide();
  781. $li.find('[data-file-act="delete"]').show();
  782. }
  783. break;
  784. }
  785. });
  786. uploader.onError = function (code) {
  787. switch (code) {
  788. case 'Q_TYPE_DENIED':
  789. Dcat.error(__('Q_TYPE_DENIED'));
  790. break;
  791. case 'Q_EXCEED_NUM_LIMIT':
  792. Dcat.error(__('Q_EXCEED_NUM_LIMIT', {
  793. num: opts.upload.fileNumLimit
  794. }));
  795. break;
  796. case 'F_EXCEED_SIZE':
  797. Dcat.error(__('F_EXCEED_SIZE'));
  798. break;
  799. case 'Q_EXCEED_SIZE_LIMIT':
  800. Dcat.error(__('Q_EXCEED_SIZE_LIMIT'));
  801. break;
  802. case 'F_DUPLICATE':
  803. Dcat.warning(__('F_DUPLICATE'));
  804. break;
  805. default:
  806. Dcat.error('Error: ' + code);
  807. }
  808. };
  809. $upload.on('click', function () {
  810. if ($(this).hasClass('disabled')) {
  811. return false;
  812. }
  813. if (state === 'ready') {
  814. uploader.upload();
  815. } else if (state === 'paused') {
  816. uploader.upload();
  817. } else if (state === 'uploading') {
  818. uploader.stop();
  819. }
  820. });
  821. $info.on('click', '.retry', function () {
  822. uploader.retry();
  823. });
  824. $info.on('click', '.ignore', function () {
  825. for (var i in faildFiles) {
  826. uploader.removeFile(i, true);
  827. delete faildFiles[i];
  828. }
  829. });
  830. setState('init');
  831. } // 预览
  832. function preview() {
  833. for (var i in opts.preview) {
  834. var path = opts.preview[i].path,
  835. ext;
  836. if (path.indexOf('.')) {
  837. ext = path.split('.').pop();
  838. }
  839. appendUploadedFileForm({
  840. serverId: opts.preview[i].id,
  841. serverUrl: opts.preview[i].url,
  842. serverPath: path,
  843. ext: ext,
  844. fake: 1
  845. });
  846. }
  847. }
  848. this.uploader = uploader;
  849. this.options = opts;
  850. this.build = build;
  851. this.preview = preview;
  852. this.setState = setState;
  853. this.refreshButton = refreshButton;
  854. this.getFileView = getFileView;
  855. this.getFileViewSelector = getFileViewSelector;
  856. this.addFileView = addFile;
  857. this.removeUploadFileView = removeUploadFile;
  858. this.isImage = isImage;
  859. this.getColumn = function () {
  860. return updateColumn;
  861. };
  862. function supportIe() {
  863. if (!WebUploader.Uploader.support('flash') && WebUploader.browser.ie) {
  864. // flash 安装了但是版本过低。
  865. if (flashVersion) {
  866. (function (container) {
  867. window['expressinstallcallback'] = function (state) {
  868. switch (state) {
  869. case 'Download.Cancelled':
  870. break;
  871. case 'Download.Failed':
  872. Dcat.error('Install failed!');
  873. break;
  874. default:
  875. Dcat.success('Install Success!');
  876. break;
  877. }
  878. delete window['expressinstallcallback'];
  879. };
  880. var swf = './expressInstall.swf'; // insert flash object
  881. var html = '<object type="application/' + 'x-shockwave-flash" data="' + swf + '" ';
  882. if (WebUploader.browser.ie) {
  883. html += 'classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ';
  884. }
  885. html += 'width="100%" height="100%" style="outline:0">' + '<param name="movie" value="' + swf + '" />' + '<param name="wmode" value="transparent" />' + '<param name="allowscriptaccess" value="always" />' + '</object>';
  886. container.html(html);
  887. })($wrap); // 压根就没有安转。
  888. } else {
  889. $wrap.html('<a href="http://www.adobe.com/go/getflashplayer" target="_blank" border="0"><img alt="get flash player" src="http://www.adobe.com/macromedia/style_guide/images/160x41_Get_Flash_Player.jpg" /></a>');
  890. }
  891. return;
  892. } else if (!WebUploader.Uploader.support()) {
  893. Dcat.error('Web Uploader 不支持您的浏览器!');
  894. return;
  895. }
  896. }
  897. return this;
  898. }
  899. Dcat.Uploader = Uploader;
  900. })(window, jQuery);
  901. /***/ }),
  902. /***/ 3:
  903. /*!*****************************************************!*\
  904. !*** multi ./resources/assets/dcat/extra/upload.js ***!
  905. \*****************************************************/
  906. /*! no static exports found */
  907. /***/ (function(module, exports, __webpack_require__) {
  908. module.exports = __webpack_require__(/*! F:\p\dcat-admin-github\dcat-admin\resources\assets\dcat\extra\upload.js */"./resources/assets/dcat/extra/upload.js");
  909. /***/ })
  910. /******/ });