upload.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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=\"".concat(getFileViewSelector(file.id), "\" title=\"").concat(file.name, "\" >\n <p class=\"file-type\">").concat(file.ext.toUpperCase() || 'FILE', "</p>\n <p class=\"imgWrap \"></p>\n <p class=\"title\" style=\"\">").concat(file.name, "</p>\n <p class=\"title\" style=\"margin-bottom:20px;\">(<b>").concat(size, "</b>)</p>\n </li>"));
  249. $btns = $("<div class=\"file-panel\">\n <a class=\"btn btn-sm btn-white\" data-file-act=\"cancel\"><i class=\"fa fa-close red-dark\" style=\"font-size:13px\"></i></a>\n <a class=\"btn btn-sm btn-white\" data-file-act=\"delete\" style=\"display: none\">\n <i class=\"feather icon-trash red-dark\" style=\"font-size:13px\"></i></a>\n <a class=\"btn btn-sm btn-white\" data-file-act=\"preview\" ><i class=\"feather icon-zoom-in\"></i></a>\n </div>").appendTo($li);
  250. } else {
  251. $li = $("\n <li id=\"".concat(getFileViewSelector(file.id), "\" title=\"").concat(file.nam, "\">\n <p class=\"title\" style=\"display:block\">\n <i class=\"feather icon-check green _success icon-success\"></i>\n ").concat(file.name, " (").concat(size, ")\n </p>\n </li>\n "));
  252. $btns = $("\n<span data-file-act=\"cancel\" class=\"file-action\" style=\"font-size:13px\">\n <i class=\"feather icon-x red-dark\"></i>\n</span>\n<span data-file-act=\"delete\" class=\"file-action\" style=\"display:none\">\n <i class=\"feather icon-trash red-dark\"></i>\n</span>\n").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. setTimeout(function () {
  274. $li.css({
  275. margin: '5px'
  276. });
  277. }, 50);
  278. if (file.getStatus() === 'invalid') {
  279. showError(file.statusText, file);
  280. } else {
  281. if (showImg) {
  282. var image = uploader.makeThumb(file, function (error, src) {
  283. var img;
  284. $wrap.empty();
  285. if (error) {
  286. $li.find('.title').show();
  287. $li.find('.file-type').show();
  288. return;
  289. }
  290. if (isSupportBase64) {
  291. img = $('<img src="' + src + '">');
  292. $wrap.append(img);
  293. } else {
  294. $li.find('.file-type').show();
  295. }
  296. });
  297. try {
  298. image.once('load', function () {
  299. file._info = file._info || image.info();
  300. file._meta = file._meta || image.meta();
  301. var width = file._info.width,
  302. height = file._info.height;
  303. if (!validateDimensions(file)) {
  304. Dcat.error('The image dimensions is invalid.');
  305. uploader.removeFile(file);
  306. return false;
  307. }
  308. image.resize(width, height);
  309. });
  310. } catch (e) {
  311. // 不是图片
  312. return setTimeout(function () {
  313. uploader.removeFile(file);
  314. }, 10);
  315. }
  316. }
  317. percentages[file.id] = [file.size, 0];
  318. file.rotation = 0;
  319. }
  320. file.on('statuschange', function (cur, prev) {
  321. if (prev === 'progress') {// $prgress.hide().width(0);
  322. } else if (prev === 'queued') {
  323. $btns.find('[data-file-act="cancel"]').hide();
  324. $btns.find('[data-file-act="delete"]').show();
  325. } // 成功
  326. if (cur === 'error' || cur === 'invalid') {
  327. showError(file.statusText, file);
  328. percentages[file.id][1] = 1;
  329. } else if (cur === 'interrupt') {
  330. showError('interrupt', file);
  331. } else if (cur === 'queued') {
  332. percentages[file.id][1] = 0;
  333. } else if (cur === 'progress') {
  334. $info.remove(); // $prgress.css('display', 'block');
  335. } else if (cur === 'complete') {
  336. if (showImg) {
  337. $li.append('<span class="success"><em></em><i class="feather icon-check"></i></span>');
  338. } else {
  339. $li.find('._success').show();
  340. }
  341. }
  342. $li.removeClass('state-' + prev).addClass('state-' + cur);
  343. });
  344. var $act = showImg ? $btns.find('a') : $btns;
  345. $act.on('click', function () {
  346. var index = $(this).data('file-act');
  347. switch (index) {
  348. case 'cancel':
  349. uploader.removeFile(file);
  350. return;
  351. case 'deleteurl':
  352. case 'delete':
  353. if (opts.disableRemove) {
  354. return uploader.removeFile(file);
  355. }
  356. var post = opts.deleteData;
  357. post.key = file.serverId;
  358. if (!post.key) {
  359. return uploader.removeFile(file);
  360. }
  361. post._column = updateColumn;
  362. Dcat.loading();
  363. $.post(opts.deleteUrl, post, function (result) {
  364. Dcat.loading(false);
  365. if (result.status) {
  366. deleteInput(file.serverId);
  367. uploader.removeFile(file);
  368. return;
  369. }
  370. Dcat.error(result.message || 'Remove file failed.');
  371. });
  372. break;
  373. case 'preview':
  374. Dcat.helpers.previewImage($wrap.find('img').attr('src'), null, file.name);
  375. break;
  376. }
  377. });
  378. } // 图片宽高验证
  379. function validateDimensions(file) {
  380. // The image dimensions is invalid.
  381. if (!showImg || !isImage(file) || !Dcat.helpers.len(opts.dimensions)) return true;
  382. var dimensions = opts.dimensions,
  383. width = file._info.width,
  384. height = file._info.height,
  385. isset = Dcat.helpers.isset;
  386. 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) {
  387. return false;
  388. }
  389. return true;
  390. } // 负责view的销毁
  391. function removeUploadFile(file) {
  392. var $li = getFileView(file.id);
  393. delete percentages[file.id];
  394. updateTotalProgress();
  395. $li.off().find('.file-panel').off().end().remove();
  396. }
  397. function updateTotalProgress() {
  398. var loaded = 0,
  399. total = 0,
  400. $bar = $progress.find('.progress-bar'),
  401. percent;
  402. $.each(percentages, function (k, v) {
  403. total += v[0];
  404. loaded += v[0] * v[1];
  405. });
  406. percent = total ? loaded / total : 0;
  407. percent = Math.round(percent * 100) + '%';
  408. $bar.text(percent);
  409. $bar.css('width', percent);
  410. updateStatusText();
  411. }
  412. function updateStatusText() {
  413. var text = '',
  414. stats;
  415. if (!uploader) {
  416. return;
  417. }
  418. if (state === 'ready') {
  419. stats = uploader.getStats();
  420. if (fileCount) {
  421. text = __('selected_files', {
  422. num: fileCount,
  423. size: WebUploader.formatSize(fileSize)
  424. });
  425. } else {
  426. showSuccess();
  427. }
  428. } else if (state === 'confirm') {
  429. stats = uploader.getStats();
  430. if (stats.uploadFailNum) {
  431. text = __('selected_has_failed', {
  432. success: stats.successNum,
  433. fail: stats.uploadFailNum
  434. });
  435. }
  436. } else {
  437. showSuccess();
  438. }
  439. function showSuccess() {
  440. stats = uploader.getStats();
  441. if (stats.successNum) {
  442. text = __('selected_success', {
  443. num: fileCount,
  444. size: WebUploader.formatSize(fileSize),
  445. success: stats.successNum
  446. });
  447. }
  448. if (stats.uploadFailNum) {
  449. text += (text ? __('dot') : '') + __('failed_num', {
  450. fail: stats.uploadFailNum
  451. });
  452. }
  453. }
  454. $info.html(text);
  455. } // 上传文件后修改字段值
  456. function updateFileColumn() {
  457. var values = getInput(),
  458. num = uploader.getStats().successNum,
  459. form = $.extend({}, opts.formData);
  460. if (!num || !values || !opts.autoUpdateColumn) {
  461. return;
  462. }
  463. form[updateColumn] = values.join(',');
  464. delete form['upload_column'];
  465. $.post(opts.server, form);
  466. }
  467. function setState(val, args) {
  468. var stats;
  469. args = args || {};
  470. if (val === state) {
  471. return;
  472. }
  473. if ($upload) {
  474. $upload.removeClass('state-' + state);
  475. $upload.addClass('state-' + val);
  476. }
  477. state = val;
  478. switch (state) {
  479. case 'pedding':
  480. if (opts.disabled) return;
  481. $placeHolder.removeClass('element-invisible');
  482. $queue.hide();
  483. $statusBar.addClass('element-invisible');
  484. if (showImg) {
  485. $wrap.removeAttr('style');
  486. $wrap.find('.queueList').removeAttr('style');
  487. }
  488. refreshButton();
  489. break;
  490. case 'ready':
  491. $placeHolder.addClass('element-invisible');
  492. $selector.find(addFileButtonSelector).removeClass('element-invisible');
  493. $queue.show();
  494. if (!opts.disabled) {
  495. $statusBar.removeClass('element-invisible');
  496. }
  497. refreshButton();
  498. if (showImg) {
  499. $wrap.find('.queueList').css({
  500. 'border': '1px solid #d3dde5',
  501. 'padding': '5px'
  502. }); // $wrap.find('.queueList').removeAttr('style');
  503. }
  504. break;
  505. case 'uploading':
  506. $selector.find(addFileButtonSelector).addClass('element-invisible');
  507. $progress.show();
  508. $upload.text(__('pause_upload'));
  509. break;
  510. case 'paused':
  511. $progress.show();
  512. $upload.text(__('go_on_upload'));
  513. break;
  514. case 'confirm':
  515. if (uploader) {
  516. $progress.hide();
  517. $selector.find(addFileButtonSelector).removeClass('element-invisible');
  518. $upload.text(__('start_upload'));
  519. stats = uploader.getStats();
  520. if (stats.successNum && !stats.uploadFailNum) {
  521. setState('finish');
  522. return;
  523. }
  524. }
  525. break;
  526. case 'finish':
  527. if (uploader) {
  528. stats = uploader.getStats();
  529. if (stats.successNum) {
  530. Dcat.success(__('upload_success_message', {
  531. success: stats.successNum
  532. }));
  533. } else {
  534. // 没有成功的图片,重设
  535. state = 'done';
  536. location.reload();
  537. }
  538. }
  539. break;
  540. case 'decrOriginalFileNum':
  541. if (originalFilesNum > 0) originalFilesNum--;
  542. break;
  543. case 'incrOriginalFileNum':
  544. originalFilesNum++;
  545. break;
  546. case 'decrFileNumLimit':
  547. // 减少上传文件数量限制
  548. if (!uploader) {
  549. return;
  550. }
  551. var ofl = uploader.option('fileNumLimit'),
  552. num = args.num || 1;
  553. if (ofl == '-1') ofl = 0;
  554. num = ofl >= num ? ofl - num : 0;
  555. if (num == 0) num = '-1';
  556. uploader.option('fileNumLimit', num);
  557. break;
  558. case 'incrFileNumLimit':
  559. // 增加上传文件数量限制
  560. if (!uploader) {
  561. return;
  562. }
  563. var ofl = uploader.option('fileNumLimit'),
  564. num = args.num || 1;
  565. if (ofl == '-1') ofl = 0;
  566. num = ofl + num;
  567. uploader.option('fileNumLimit', num);
  568. break;
  569. case 'init':
  570. // 初始化
  571. $upload.addClass('state-' + state);
  572. updateTotalProgress();
  573. if (originalFilesNum || opts.disabled) {
  574. $placeHolder.addClass('element-invisible');
  575. if (!opts.disabled) {
  576. $statusBar.show();
  577. } else {
  578. $wrap.addClass('disabled');
  579. }
  580. setState('ready');
  581. } else if (showImg) {
  582. $wrap.removeAttr('style');
  583. $wrap.find('.queueList').css('margin', '0');
  584. }
  585. refreshButton();
  586. break;
  587. }
  588. updateStatusText();
  589. } // 移除form表单的文件
  590. function removeFormFile(fileId) {
  591. if (!fileId) return;
  592. var file = formFiles[fileId];
  593. deleteInput(fileId);
  594. delete formFiles[fileId];
  595. if (uploader && !file.fake) {
  596. uploader.removeFile(file);
  597. }
  598. setState('decrOriginalFileNum');
  599. setState('incrFileNumLimit');
  600. if (!Dcat.helpers.len(formFiles) && !Dcat.helpers.len(percentages)) {
  601. setState('pedding');
  602. }
  603. } // 获取表单值
  604. function getInput() {
  605. var val = $input.val();
  606. return val ? val.split(',') : [];
  607. } // 新增表单值
  608. function addInput(id) {
  609. var val = getInput();
  610. val.push(id);
  611. setInput(val);
  612. } // 设置表单值
  613. function setInput(arr) {
  614. arr = arr.filter(function (v, k, self) {
  615. return self.indexOf(v) === k;
  616. }).filter(function (v) {
  617. return v ? true : false;
  618. });
  619. $input.val(arr.join(','));
  620. } // 删除表单值
  621. function deleteInput(id) {
  622. if (!id) {
  623. return $input.val('');
  624. }
  625. setInput(getInput().filter(function (v) {
  626. return v != id;
  627. }));
  628. } // 重新计算按钮定位
  629. function refreshButton() {
  630. uploader.refresh();
  631. } // 添加上传成功文件到表单区域
  632. function appendUploadedFileForm(file) {
  633. var html = "";
  634. html += "<li title='" + file.serverPath + "'>";
  635. if (showImg) {
  636. html += "<p class='imgWrap'><img src='".concat(file.serverUrl, "'></p>");
  637. } else if (!opts.disabled) {
  638. html += "<p class=\"file-action\" data-file-act=\"delete\" data-id=\"".concat(file.serverId, "\"><i class=\"feather icon-trash red-dark\"></i></p>");
  639. }
  640. html += "<p class='title' style=''><i class='feather icon-check text-white icon-success' style='color: #fff;'></i>";
  641. html += file.serverPath;
  642. html += "</p>";
  643. if (showImg) {
  644. html += "<p class='title' style='margin-bottom:20px;'>&nbsp;</p>";
  645. html += "<div class='file-panel' >";
  646. if (!opts.disabled) {
  647. html += "<a class='btn btn-sm btn-white' data-file-act='deleteurl' data-id='".concat(file.serverId, "'><i class='feather icon-trash red-dark' style='font-size:13px'></i></a>");
  648. }
  649. html += "<a class='btn btn-sm btn-white' data-file-act='preview' data-url='".concat(file.serverUrl, "' ><i class='feather icon-zoom-in'></i></a>");
  650. html += "</div>";
  651. }
  652. html += "</li>";
  653. html = $(html);
  654. if (!showImg) {
  655. html.find('.file-type').show();
  656. html.find('.title').show();
  657. $wrap.css('background', 'transparent');
  658. }
  659. var deleteFile = function deleteFile() {
  660. var fileId = $(this).data('id'),
  661. post = opts.deleteData;
  662. if (opts.disableRemove) {
  663. html.remove();
  664. return removeFormFile(fileId);
  665. }
  666. post.key = fileId;
  667. post._column = updateColumn;
  668. Dcat.loading();
  669. $.post(opts.deleteUrl, post, function (result) {
  670. Dcat.loading(false);
  671. if (result.status) {
  672. // 移除
  673. html.remove();
  674. removeFormFile(fileId);
  675. return;
  676. }
  677. Dcat.error(result.message || 'Remove file failed.');
  678. });
  679. }; // 删除按钮点击事件
  680. html.find('[data-file-act="deleteurl"]').click(deleteFile);
  681. html.find('[data-file-act="delete"]').click(deleteFile); // 放大图片
  682. html.find('[data-file-act="preview"]').click(function () {
  683. var url = $(this).data('url');
  684. Dcat.helpers.previewImage(url);
  685. });
  686. setState('incrOriginalFileNum');
  687. setState('decrFileNumLimit');
  688. formFiles[file.serverId] = file;
  689. addInput(file.serverId);
  690. $queue.append(html);
  691. if (showImg) {
  692. setTimeout(function () {
  693. html.css('margin', '5px');
  694. }, 80);
  695. }
  696. } // 初始化web-uploader
  697. function build() {
  698. $wrap = $selector.find(opts.wrapper); // 图片容器
  699. $queue = $('<ul class="filelist"></ul>').appendTo($wrap.find('.queueList')); // 状态栏,包括进度和控制按钮
  700. $statusBar = $wrap.find('.statusBar'); // 文件总体选择信息。
  701. $info = $statusBar.find('.info'); // 上传按钮
  702. $upload = $wrap.find('.uploadBtn'); // 没选择文件之前的内容。
  703. $placeHolder = $wrap.find('.placeholder');
  704. $progress = $statusBar.find('.upload-progress').hide(); // IE;
  705. supportIe(); // 实例化
  706. uploader = WebUploader.create(opts.upload); // 拖拽时不接受 js, txt 文件。
  707. uploader.on('dndAccept', function (items) {
  708. var denied = false,
  709. len = items.length,
  710. i = 0,
  711. // 修改js类型
  712. unAllowed = 'text/plain;application/javascript ';
  713. for (; i < len; i++) {
  714. // 如果在列表里面
  715. if (~unAllowed.indexOf(items[i].type)) {
  716. denied = true;
  717. break;
  718. }
  719. }
  720. return !denied;
  721. });
  722. if (opts.upload.fileNumLimit > 1 && !opts.disabled) {
  723. // 添加“添加文件”的按钮,
  724. uploader.addButton({
  725. id: addFileButtonSelector,
  726. label: '<i class="feather icon-folder"></i> &nbsp;' + __('go_on_add')
  727. });
  728. }
  729. uploader.onUploadProgress = function (file, percentage) {
  730. percentages[file.id][1] = percentage;
  731. updateTotalProgress();
  732. };
  733. uploader.onBeforeFileQueued = function (file) {};
  734. uploader.onFileQueued = function (file) {
  735. fileCount++;
  736. fileSize += file.size;
  737. if (fileCount === 1) {
  738. $placeHolder.addClass('element-invisible');
  739. $statusBar.show();
  740. }
  741. addFile(file);
  742. setState('ready');
  743. updateTotalProgress();
  744. }; // 删除文件事件监听
  745. uploader.onFileDequeued = function (file) {
  746. fileCount--;
  747. fileSize -= file.size;
  748. if (!fileCount && !Dcat.helpers.len(formFiles)) {
  749. setState('pedding');
  750. }
  751. removeUploadFile(file);
  752. };
  753. uploader.on('all', function (type, obj, reason) {
  754. switch (type) {
  755. case 'uploadFinished':
  756. setState('confirm');
  757. updateFileColumn();
  758. break;
  759. case 'startUpload':
  760. setState('uploading');
  761. break;
  762. case 'stopUpload':
  763. setState('paused');
  764. break;
  765. case 'uploadAccept':
  766. // 上传失败,返回false
  767. if (reason && reason.error) {
  768. Dcat.error(reason.error.message);
  769. faildFiles[obj.file.id] = obj.file;
  770. return false;
  771. }
  772. if (reason.merge) {
  773. // 分片上传
  774. return;
  775. } // 上传成功,保存新文件名和路径到file对象
  776. obj.file.serverId = reason.id;
  777. obj.file.serverName = reason.name;
  778. obj.file.serverPath = reason.path;
  779. obj.file.serverUrl = reason.url || null;
  780. addInput(reason.id);
  781. if (!showImg) {
  782. var $li = getFileView(obj.file.id);
  783. $li.find('.file-action').hide();
  784. $li.find('[data-file-act="delete"]').show();
  785. }
  786. break;
  787. }
  788. });
  789. uploader.onError = function (code) {
  790. switch (code) {
  791. case 'Q_TYPE_DENIED':
  792. Dcat.error(__('Q_TYPE_DENIED'));
  793. break;
  794. case 'Q_EXCEED_NUM_LIMIT':
  795. Dcat.error(__('Q_EXCEED_NUM_LIMIT', {
  796. num: opts.upload.fileNumLimit
  797. }));
  798. break;
  799. case 'F_EXCEED_SIZE':
  800. Dcat.error(__('F_EXCEED_SIZE'));
  801. break;
  802. case 'Q_EXCEED_SIZE_LIMIT':
  803. Dcat.error(__('Q_EXCEED_SIZE_LIMIT'));
  804. break;
  805. case 'F_DUPLICATE':
  806. Dcat.warning(__('F_DUPLICATE'));
  807. break;
  808. default:
  809. Dcat.error('Error: ' + code);
  810. }
  811. };
  812. $upload.on('click', function () {
  813. if ($(this).hasClass('disabled')) {
  814. return false;
  815. }
  816. if (state === 'ready') {
  817. uploader.upload();
  818. } else if (state === 'paused') {
  819. uploader.upload();
  820. } else if (state === 'uploading') {
  821. uploader.stop();
  822. }
  823. });
  824. $info.on('click', '.retry', function () {
  825. uploader.retry();
  826. });
  827. $info.on('click', '.ignore', function () {
  828. for (var i in faildFiles) {
  829. uploader.removeFile(i, true);
  830. delete faildFiles[i];
  831. }
  832. });
  833. setState('init');
  834. } // 预览
  835. function preview() {
  836. for (var i in opts.preview) {
  837. var path = opts.preview[i].path,
  838. ext;
  839. if (path.indexOf('.')) {
  840. ext = path.split('.').pop();
  841. }
  842. appendUploadedFileForm({
  843. serverId: opts.preview[i].id,
  844. serverUrl: opts.preview[i].url,
  845. serverPath: path,
  846. ext: ext,
  847. fake: 1
  848. });
  849. }
  850. }
  851. this.uploader = uploader;
  852. this.options = opts;
  853. this.build = build;
  854. this.preview = preview;
  855. this.setState = setState;
  856. this.refreshButton = refreshButton;
  857. this.getFileView = getFileView;
  858. this.getFileViewSelector = getFileViewSelector;
  859. this.addFileView = addFile;
  860. this.removeUploadFileView = removeUploadFile;
  861. this.isImage = isImage;
  862. this.getColumn = function () {
  863. return updateColumn;
  864. };
  865. function supportIe() {
  866. if (!WebUploader.Uploader.support('flash') && WebUploader.browser.ie) {
  867. // flash 安装了但是版本过低。
  868. if (flashVersion) {
  869. (function (container) {
  870. window['expressinstallcallback'] = function (state) {
  871. switch (state) {
  872. case 'Download.Cancelled':
  873. break;
  874. case 'Download.Failed':
  875. Dcat.error('Install failed!');
  876. break;
  877. default:
  878. Dcat.success('Install Success!');
  879. break;
  880. }
  881. delete window['expressinstallcallback'];
  882. };
  883. var swf = './expressInstall.swf'; // insert flash object
  884. var html = "<object type=\"application/x-shockwave-flash\" data=\"".concat(swf, "\" ");
  885. if (WebUploader.browser.ie) {
  886. html += 'classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ';
  887. }
  888. html += "width=\"100%\" height=\"100%\" style=\"outline:0\">\n <param name=\"movie\" value=\"".concat(swf, "\" />\n <param name=\"wmode\" value=\"transparent\" />\n <param name=\"allowscriptaccess\" value=\"always\" />\n </object>");
  889. container.html(html);
  890. })($wrap);
  891. } else {
  892. $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>');
  893. }
  894. return;
  895. } else if (!WebUploader.Uploader.support()) {
  896. Dcat.error('您的浏览器不支持Web Uploader!');
  897. return;
  898. }
  899. }
  900. return this;
  901. }
  902. Dcat.Uploader = Uploader;
  903. })(window, jQuery);
  904. /***/ }),
  905. /***/ 3:
  906. /*!*****************************************************!*\
  907. !*** multi ./resources/assets/dcat/extra/upload.js ***!
  908. \*****************************************************/
  909. /*! no static exports found */
  910. /***/ (function(module, exports, __webpack_require__) {
  911. module.exports = __webpack_require__(/*! D:\php-project\laravel\laraveladmin\github-test\pck-dcat-admin\dcat-admin\resources\assets\dcat\extra\upload.js */"./resources/assets/dcat/extra/upload.js");
  912. /***/ })
  913. /******/ });