attachment.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /**
  2. * User: Jinqn
  3. * Date: 14-04-08
  4. * Time: 下午16:34
  5. * 上传图片对话框逻辑代码,包括tab: 远程图片/上传图片/在线图片/搜索图片
  6. */
  7. (function () {
  8. var uploadFile,
  9. onlineFile;
  10. window.onload = function () {
  11. initTabs();
  12. initButtons();
  13. };
  14. /* 初始化tab标签 */
  15. function initTabs() {
  16. var tabs = $G('tabhead').children;
  17. for (var i = 0; i < tabs.length; i++) {
  18. domUtils.on(tabs[i], "click", function (e) {
  19. var target = e.target || e.srcElement;
  20. setTabFocus(target.getAttribute('data-content-id'));
  21. });
  22. }
  23. setTabFocus('upload');
  24. }
  25. /* 初始化tabbody */
  26. function setTabFocus(id) {
  27. if (!id) return;
  28. var i, bodyId, tabs = $G('tabhead').children;
  29. for (i = 0; i < tabs.length; i++) {
  30. bodyId = tabs[i].getAttribute('data-content-id')
  31. if (bodyId == id) {
  32. domUtils.addClass(tabs[i], 'focus');
  33. domUtils.addClass($G(bodyId), 'focus');
  34. } else {
  35. domUtils.removeClasses(tabs[i], 'focus');
  36. domUtils.removeClasses($G(bodyId), 'focus');
  37. }
  38. }
  39. switch (id) {
  40. case 'upload':
  41. uploadFile = uploadFile || new UploadFile('queueList');
  42. break;
  43. case 'online':
  44. onlineFile = onlineFile || new OnlineFile('fileList');
  45. break;
  46. }
  47. }
  48. /* 初始化onok事件 */
  49. function initButtons() {
  50. dialog.onok = function () {
  51. var list = [], id, tabs = $G('tabhead').children;
  52. for (var i = 0; i < tabs.length; i++) {
  53. if (domUtils.hasClass(tabs[i], 'focus')) {
  54. id = tabs[i].getAttribute('data-content-id');
  55. break;
  56. }
  57. }
  58. switch (id) {
  59. case 'upload':
  60. list = uploadFile.getInsertList();
  61. var count = uploadFile.getQueueCount();
  62. if (count) {
  63. $('.info', '#queueList').html('<span style="color:red;">' + '还有2个未上传文件'.replace(/[\d]/, count) + '</span>');
  64. return false;
  65. }
  66. break;
  67. case 'online':
  68. list = onlineFile.getInsertList();
  69. break;
  70. }
  71. editor.execCommand('insertfile', list);
  72. };
  73. }
  74. /* 上传附件 */
  75. function UploadFile(target) {
  76. this.$wrap = target.constructor == String ? $('#' + target) : $(target);
  77. this.init();
  78. }
  79. UploadFile.prototype = {
  80. init: function () {
  81. this.fileList = [];
  82. this.initContainer();
  83. this.initUploader();
  84. },
  85. initContainer: function () {
  86. this.$queue = this.$wrap.find('.filelist');
  87. },
  88. /* 初始化容器 */
  89. initUploader: function () {
  90. var _this = this,
  91. $ = jQuery, // just in case. Make sure it's not an other libaray.
  92. $wrap = _this.$wrap,
  93. // 图片容器
  94. $queue = $wrap.find('.filelist'),
  95. // 状态栏,包括进度和控制按钮
  96. $statusBar = $wrap.find('.statusBar'),
  97. // 文件总体选择信息。
  98. $info = $statusBar.find('.info'),
  99. // 上传按钮
  100. $upload = $wrap.find('.uploadBtn'),
  101. // 上传按钮
  102. $filePickerBtn = $wrap.find('.filePickerBtn'),
  103. // 上传按钮
  104. $filePickerBlock = $wrap.find('.filePickerBlock'),
  105. // 没选择文件之前的内容。
  106. $placeHolder = $wrap.find('.placeholder'),
  107. // 总体进度条
  108. $progress = $statusBar.find('.progress').hide(),
  109. // 添加的文件数量
  110. fileCount = 0,
  111. // 添加的文件总大小
  112. fileSize = 0,
  113. // 优化retina, 在retina下这个值是2
  114. ratio = window.devicePixelRatio || 1,
  115. // 缩略图大小
  116. thumbnailWidth = 113 * ratio,
  117. thumbnailHeight = 113 * ratio,
  118. // 可能有pedding, ready, uploading, confirm, done.
  119. state = '',
  120. // 所有文件的进度信息,key为file id
  121. percentages = {},
  122. supportTransition = (function () {
  123. var s = document.createElement('p').style,
  124. r = 'transition' in s ||
  125. 'WebkitTransition' in s ||
  126. 'MozTransition' in s ||
  127. 'msTransition' in s ||
  128. 'OTransition' in s;
  129. s = null;
  130. return r;
  131. })(),
  132. // WebUploader实例
  133. uploader,
  134. actionUrl = editor.getActionUrl(editor.getOpt('fileActionName')),
  135. fileMaxSize = editor.getOpt('fileMaxSize'),
  136. acceptExtensions = (editor.getOpt('fileAllowFiles') || []).join('').replace(/\./g, ',').replace(/^[,]/, '');
  137. ;
  138. if (!WebUploader.Uploader.support()) {
  139. $('#filePickerReady').after($('<div>').html(lang.errorNotSupport)).hide();
  140. return;
  141. } else if (!editor.getOpt('fileActionName')) {
  142. $('#filePickerReady').after($('<div>').html(lang.errorLoadConfig)).hide();
  143. return;
  144. }
  145. uploader = _this.uploader = WebUploader.create({
  146. pick: {
  147. id: '#filePickerReady',
  148. label: lang.uploadSelectFile
  149. },
  150. swf: '../../third-party/webuploader/Uploader.swf',
  151. server: actionUrl,
  152. fileVal: editor.getOpt('fileFieldName'),
  153. duplicate: true,
  154. fileSingleSizeLimit: fileMaxSize,
  155. headers: editor.getOpt('serverHeaders') || {},
  156. compress: false
  157. });
  158. uploader.addButton({
  159. id: '#filePickerBlock'
  160. });
  161. uploader.addButton({
  162. id: '#filePickerBtn',
  163. label: lang.uploadAddFile
  164. });
  165. setState('pedding');
  166. // 当有文件添加进来时执行,负责view的创建
  167. function addFile(file) {
  168. var $li = $('<li id="' + file.id + '">' +
  169. '<p class="title">' + file.name + '</p>' +
  170. '<p class="imgWrap"></p>' +
  171. '<p class="progress"><span></span></p>' +
  172. '</li>'),
  173. $btns = $('<div class="file-panel">' +
  174. '<span class="cancel">' + lang.uploadDelete + '</span>' +
  175. '<span class="rotateRight">' + lang.uploadTurnRight + '</span>' +
  176. '<span class="rotateLeft">' + lang.uploadTurnLeft + '</span></div>').appendTo($li),
  177. $prgress = $li.find('p.progress span'),
  178. $wrap = $li.find('p.imgWrap'),
  179. $info = $('<p class="error"></p>').hide().appendTo($li),
  180. showError = function (code) {
  181. switch (code) {
  182. case 'exceed_size':
  183. text = lang.errorExceedSize;
  184. break;
  185. case 'interrupt':
  186. text = lang.errorInterrupt;
  187. break;
  188. case 'http':
  189. text = lang.errorHttp;
  190. break;
  191. case 'not_allow_type':
  192. text = lang.errorFileType;
  193. break;
  194. default:
  195. text = lang.errorUploadRetry;
  196. break;
  197. }
  198. $info.text(text).show();
  199. };
  200. if (file.getStatus() === 'invalid') {
  201. showError(file.statusText);
  202. } else {
  203. $wrap.text(lang.uploadPreview);
  204. if ('|png|jpg|jpeg|bmp|gif|'.indexOf('|' + file.ext.toLowerCase() + '|') == -1) {
  205. $wrap.empty().addClass('notimage').append('<i class="file-preview file-type-' + file.ext.toLowerCase() + '"></i>' +
  206. '<span class="file-title" title="' + file.name + '">' + file.name + '</span>');
  207. } else {
  208. if (browser.ie && browser.version <= 7) {
  209. $wrap.text(lang.uploadNoPreview);
  210. } else {
  211. uploader.makeThumb(file, function (error, src) {
  212. if (error || !src) {
  213. $wrap.text(lang.uploadNoPreview);
  214. } else {
  215. var $img = $('<img src="' + src + '">');
  216. $wrap.empty().append($img);
  217. $img.on('error', function () {
  218. $wrap.text(lang.uploadNoPreview);
  219. });
  220. }
  221. }, thumbnailWidth, thumbnailHeight);
  222. }
  223. }
  224. percentages[file.id] = [file.size, 0];
  225. file.rotation = 0;
  226. /* 检查文件格式 */
  227. if (!file.ext || acceptExtensions.indexOf(file.ext.toLowerCase()) == -1) {
  228. showError('not_allow_type');
  229. uploader.removeFile(file);
  230. }
  231. }
  232. file.on('statuschange', function (cur, prev) {
  233. if (prev === 'progress') {
  234. $prgress.hide().width(0);
  235. } else if (prev === 'queued') {
  236. $li.off('mouseenter mouseleave');
  237. $btns.remove();
  238. }
  239. // 成功
  240. if (cur === 'error' || cur === 'invalid') {
  241. showError(file.statusText);
  242. percentages[file.id][1] = 1;
  243. } else if (cur === 'interrupt') {
  244. showError('interrupt');
  245. } else if (cur === 'queued') {
  246. percentages[file.id][1] = 0;
  247. } else if (cur === 'progress') {
  248. $info.hide();
  249. $prgress.css('display', 'block');
  250. } else if (cur === 'complete') {
  251. }
  252. $li.removeClass('state-' + prev).addClass('state-' + cur);
  253. });
  254. $li.on('mouseenter', function () {
  255. $btns.stop().animate({height: 30});
  256. });
  257. $li.on('mouseleave', function () {
  258. $btns.stop().animate({height: 0});
  259. });
  260. $btns.on('click', 'span', function () {
  261. var index = $(this).index(),
  262. deg;
  263. switch (index) {
  264. case 0:
  265. uploader.removeFile(file);
  266. return;
  267. case 1:
  268. file.rotation += 90;
  269. break;
  270. case 2:
  271. file.rotation -= 90;
  272. break;
  273. }
  274. if (supportTransition) {
  275. deg = 'rotate(' + file.rotation + 'deg)';
  276. $wrap.css({
  277. '-webkit-transform': deg,
  278. '-mos-transform': deg,
  279. '-o-transform': deg,
  280. 'transform': deg
  281. });
  282. } else {
  283. $wrap.css('filter', 'progid:DXImageTransform.Microsoft.BasicImage(rotation=' + (~~((file.rotation / 90) % 4 + 4) % 4) + ')');
  284. }
  285. });
  286. $li.insertBefore($filePickerBlock);
  287. }
  288. // 负责view的销毁
  289. function removeFile(file) {
  290. var $li = $('#' + file.id);
  291. delete percentages[file.id];
  292. updateTotalProgress();
  293. $li.off().find('.file-panel').off().end().remove();
  294. }
  295. function updateTotalProgress() {
  296. var loaded = 0,
  297. total = 0,
  298. spans = $progress.children(),
  299. percent;
  300. $.each(percentages, function (k, v) {
  301. total += v[0];
  302. loaded += v[0] * v[1];
  303. });
  304. percent = total ? loaded / total : 0;
  305. spans.eq(0).text(Math.round(percent * 100) + '%');
  306. spans.eq(1).css('width', Math.round(percent * 100) + '%');
  307. updateStatus();
  308. }
  309. function setState(val, files) {
  310. if (val != state) {
  311. var stats = uploader.getStats();
  312. $upload.removeClass('state-' + state);
  313. $upload.addClass('state-' + val);
  314. switch (val) {
  315. /* 未选择文件 */
  316. case 'pedding':
  317. $queue.addClass('element-invisible');
  318. $statusBar.addClass('element-invisible');
  319. $placeHolder.removeClass('element-invisible');
  320. $progress.hide();
  321. $info.hide();
  322. uploader.refresh();
  323. break;
  324. /* 可以开始上传 */
  325. case 'ready':
  326. $placeHolder.addClass('element-invisible');
  327. $queue.removeClass('element-invisible');
  328. $statusBar.removeClass('element-invisible');
  329. $progress.hide();
  330. $info.show();
  331. $upload.text(lang.uploadStart);
  332. uploader.refresh();
  333. break;
  334. /* 上传中 */
  335. case 'uploading':
  336. $progress.show();
  337. $info.hide();
  338. $upload.text(lang.uploadPause);
  339. break;
  340. /* 暂停上传 */
  341. case 'paused':
  342. $progress.show();
  343. $info.hide();
  344. $upload.text(lang.uploadContinue);
  345. break;
  346. case 'confirm':
  347. $progress.show();
  348. $info.hide();
  349. $upload.text(lang.uploadStart);
  350. stats = uploader.getStats();
  351. if (stats.successNum && !stats.uploadFailNum) {
  352. setState('finish');
  353. return;
  354. }
  355. break;
  356. case 'finish':
  357. $progress.hide();
  358. $info.show();
  359. if (stats.uploadFailNum) {
  360. $upload.text(lang.uploadRetry);
  361. } else {
  362. $upload.text(lang.uploadStart);
  363. }
  364. break;
  365. }
  366. state = val;
  367. updateStatus();
  368. }
  369. if (!_this.getQueueCount()) {
  370. $upload.addClass('disabled')
  371. } else {
  372. $upload.removeClass('disabled')
  373. }
  374. }
  375. function updateStatus() {
  376. var text = '', stats;
  377. if (state === 'ready') {
  378. text = lang.updateStatusReady.replace('_', fileCount).replace('_KB', WebUploader.formatSize(fileSize));
  379. } else if (state === 'confirm') {
  380. stats = uploader.getStats();
  381. if (stats.uploadFailNum) {
  382. text = lang.updateStatusConfirm.replace('_', stats.successNum).replace('_', stats.successNum);
  383. }
  384. } else {
  385. stats = uploader.getStats();
  386. text = lang.updateStatusFinish.replace('_', fileCount).replace('_KB', WebUploader.formatSize(fileSize)).replace('_', stats.successNum);
  387. if (stats.uploadFailNum) {
  388. text += lang.updateStatusError.replace('_', stats.uploadFailNum);
  389. }
  390. }
  391. $info.html(text);
  392. }
  393. uploader.on('fileQueued', function (file) {
  394. if (file.ext && acceptExtensions.indexOf(file.ext.toLowerCase()) != -1 && file.size <= fileMaxSize) {
  395. fileCount++;
  396. fileSize += file.size;
  397. }
  398. if (fileCount === 1) {
  399. $placeHolder.addClass('element-invisible');
  400. $statusBar.show();
  401. }
  402. addFile(file);
  403. });
  404. uploader.on('fileDequeued', function (file) {
  405. if (file.ext && acceptExtensions.indexOf(file.ext.toLowerCase()) != -1 && file.size <= fileMaxSize) {
  406. fileCount--;
  407. fileSize -= file.size;
  408. }
  409. removeFile(file);
  410. updateTotalProgress();
  411. });
  412. uploader.on('filesQueued', function (file) {
  413. if (!uploader.isInProgress() && (state == 'pedding' || state == 'finish' || state == 'confirm' || state == 'ready')) {
  414. setState('ready');
  415. }
  416. updateTotalProgress();
  417. });
  418. uploader.on('all', function (type, files) {
  419. switch (type) {
  420. case 'uploadFinished':
  421. setState('confirm', files);
  422. break;
  423. case 'startUpload':
  424. /* 添加额外的GET参数 */
  425. var params = utils.serializeParam(editor.queryCommandValue('serverparam')) || '',
  426. url = utils.formatUrl(actionUrl + (actionUrl.indexOf('?') == -1 ? '?' : '&') + 'encode=utf-8&' + params);
  427. uploader.option('server', url);
  428. setState('uploading', files);
  429. break;
  430. case 'stopUpload':
  431. setState('paused', files);
  432. break;
  433. }
  434. });
  435. uploader.on('uploadBeforeSend', function (file, data, header) {
  436. //这里可以通过data对象添加POST参数
  437. if (actionUrl.toLowerCase().indexOf('jsp') != -1) {
  438. header['X_Requested_With'] = 'XMLHttpRequest';
  439. }
  440. });
  441. uploader.on('uploadProgress', function (file, percentage) {
  442. var $li = $('#' + file.id),
  443. $percent = $li.find('.progress span');
  444. $percent.css('width', percentage * 100 + '%');
  445. percentages[file.id][1] = percentage;
  446. updateTotalProgress();
  447. });
  448. uploader.on('uploadSuccess', function (file, ret) {
  449. var $file = $('#' + file.id);
  450. try {
  451. var responseText = (ret._raw || ret),
  452. json = utils.str2json(responseText);
  453. if (json.state == 'SUCCESS') {
  454. _this.fileList.push(json);
  455. $file.append('<span class="success"></span>');
  456. // 触发上传附件事件
  457. editor.fireEvent("uploadsuccess", {
  458. res: json,
  459. type: 'file'
  460. });
  461. } else {
  462. $file.find('.error').text(json.state).show();
  463. }
  464. } catch (e) {
  465. $file.find('.error').text(lang.errorServerUpload).show();
  466. }
  467. });
  468. uploader.on('uploadError', function (file, code) {
  469. });
  470. uploader.on('error', function (code, file) {
  471. if (code == 'Q_TYPE_DENIED' || code == 'F_EXCEED_SIZE') {
  472. addFile(file);
  473. }
  474. });
  475. uploader.on('uploadComplete', function (file, ret) {
  476. });
  477. $upload.on('click', function () {
  478. if ($(this).hasClass('disabled')) {
  479. return false;
  480. }
  481. if (state === 'ready') {
  482. uploader.upload();
  483. } else if (state === 'paused') {
  484. uploader.upload();
  485. } else if (state === 'uploading') {
  486. uploader.stop();
  487. }
  488. });
  489. $upload.addClass('state-' + state);
  490. updateTotalProgress();
  491. },
  492. getQueueCount: function () {
  493. var file, i, status, readyFile = 0, files = this.uploader.getFiles();
  494. for (i = 0; file = files[i++];) {
  495. status = file.getStatus();
  496. if (status == 'queued' || status == 'uploading' || status == 'progress') readyFile++;
  497. }
  498. return readyFile;
  499. },
  500. getInsertList: function () {
  501. var i, link, data, list = [],
  502. prefix = editor.getOpt('fileUrlPrefix');
  503. for (i = 0; i < this.fileList.length; i++) {
  504. data = this.fileList[i];
  505. link = data.url;
  506. list.push({
  507. title: data.original || link.substr(link.lastIndexOf('/') + 1),
  508. url: prefix + link
  509. });
  510. }
  511. return list;
  512. }
  513. };
  514. /* 在线附件 */
  515. function OnlineFile(target) {
  516. this.container = utils.isString(target) ? document.getElementById(target) : target;
  517. this.init();
  518. }
  519. OnlineFile.prototype = {
  520. init: function () {
  521. this.initContainer();
  522. this.initEvents();
  523. this.initData();
  524. },
  525. /* 初始化容器 */
  526. initContainer: function () {
  527. this.container.innerHTML = '';
  528. this.list = document.createElement('ul');
  529. this.clearFloat = document.createElement('li');
  530. domUtils.addClass(this.list, 'list');
  531. domUtils.addClass(this.clearFloat, 'clearFloat');
  532. this.list.appendChild(this.clearFloat);
  533. this.container.appendChild(this.list);
  534. },
  535. /* 初始化滚动事件,滚动到地步自动拉取数据 */
  536. initEvents: function () {
  537. var _this = this;
  538. /* 滚动拉取图片 */
  539. domUtils.on($G('fileList'), 'scroll', function (e) {
  540. var panel = this;
  541. if (panel.scrollHeight - (panel.offsetHeight + panel.scrollTop) < 10) {
  542. _this.getFileData();
  543. }
  544. });
  545. /* 选中图片 */
  546. domUtils.on(this.list, 'click', function (e) {
  547. var target = e.target || e.srcElement,
  548. li = target.parentNode;
  549. if (li.tagName.toLowerCase() == 'li') {
  550. if (domUtils.hasClass(li, 'selected')) {
  551. domUtils.removeClasses(li, 'selected');
  552. } else {
  553. domUtils.addClass(li, 'selected');
  554. }
  555. }
  556. });
  557. },
  558. /* 初始化第一次的数据 */
  559. initData: function () {
  560. /* 拉取数据需要使用的值 */
  561. this.state = 0;
  562. this.listSize = editor.getOpt('fileManagerListSize');
  563. this.listIndex = 0;
  564. this.listEnd = false;
  565. /* 第一次拉取数据 */
  566. this.getFileData();
  567. },
  568. /* 向后台拉取图片列表数据 */
  569. getFileData: function () {
  570. var _this = this;
  571. if (!_this.listEnd && !this.isLoadingData) {
  572. this.isLoadingData = true;
  573. ajax.request(editor.getActionUrl(editor.getOpt('fileManagerActionName')), {
  574. timeout: 100000,
  575. data: utils.extend({
  576. start: this.listIndex,
  577. size: this.listSize
  578. }, editor.queryCommandValue('serverparam')),
  579. headers: editor.options.serverHeaders || {},
  580. method: 'get',
  581. onsuccess: function (r) {
  582. try {
  583. var json = eval('(' + r.responseText + ')');
  584. if (json.state == 'SUCCESS') {
  585. _this.pushData(json.list);
  586. _this.listIndex = parseInt(json.start) + parseInt(json.list.length);
  587. if (_this.listIndex >= json.total) {
  588. _this.listEnd = true;
  589. }
  590. _this.isLoadingData = false;
  591. }
  592. } catch (e) {
  593. if (r.responseText.indexOf('ue_separate_ue') != -1) {
  594. var list = r.responseText.split(r.responseText);
  595. _this.pushData(list);
  596. _this.listIndex = parseInt(list.length);
  597. _this.listEnd = true;
  598. _this.isLoadingData = false;
  599. }
  600. }
  601. },
  602. onerror: function () {
  603. _this.isLoadingData = false;
  604. }
  605. });
  606. }
  607. },
  608. /* 添加图片到列表界面上 */
  609. pushData: function (list) {
  610. var i, item, img, filetype, preview, icon, _this = this,
  611. urlPrefix = editor.getOpt('fileManagerUrlPrefix');
  612. for (i = 0; i < list.length; i++) {
  613. if (list[i] && list[i].url) {
  614. item = document.createElement('li');
  615. icon = document.createElement('span');
  616. filetype = list[i].url.substr(list[i].url.lastIndexOf('.') + 1);
  617. if ("png|jpg|jpeg|gif|bmp".indexOf(filetype) != -1) {
  618. preview = document.createElement('img');
  619. domUtils.on(preview, 'load', (function (image) {
  620. return function () {
  621. _this.scale(image, image.parentNode.offsetWidth, image.parentNode.offsetHeight);
  622. };
  623. })(preview));
  624. preview.width = 113;
  625. preview.setAttribute('src', urlPrefix + list[i].url + (list[i].url.indexOf('?') == -1 ? '?noCache=' : '&noCache=') + (+new Date()).toString(36));
  626. } else {
  627. var ic = document.createElement('i'),
  628. textSpan = document.createElement('span');
  629. textSpan.innerHTML = list[i].original || list[i].url.substr(list[i].url.lastIndexOf('/') + 1);
  630. preview = document.createElement('div');
  631. preview.appendChild(ic);
  632. preview.appendChild(textSpan);
  633. domUtils.addClass(preview, 'file-wrapper');
  634. domUtils.addClass(textSpan, 'file-title');
  635. domUtils.addClass(ic, 'file-type-' + filetype);
  636. domUtils.addClass(ic, 'file-preview');
  637. }
  638. domUtils.addClass(icon, 'icon');
  639. item.setAttribute('data-url', urlPrefix + list[i].url);
  640. if (list[i].original) {
  641. item.setAttribute('data-title', list[i].original);
  642. }
  643. item.appendChild(preview);
  644. item.appendChild(icon);
  645. this.list.insertBefore(item, this.clearFloat);
  646. }
  647. }
  648. },
  649. /* 改变图片大小 */
  650. scale: function (img, w, h, type) {
  651. var ow = img.width,
  652. oh = img.height;
  653. if (type == 'justify') {
  654. if (ow >= oh) {
  655. img.width = w;
  656. img.height = h * oh / ow;
  657. img.style.marginLeft = '-' + parseInt((img.width - w) / 2) + 'px';
  658. } else {
  659. img.width = w * ow / oh;
  660. img.height = h;
  661. img.style.marginTop = '-' + parseInt((img.height - h) / 2) + 'px';
  662. }
  663. } else {
  664. if (ow >= oh) {
  665. img.width = w * ow / oh;
  666. img.height = h;
  667. img.style.marginLeft = '-' + parseInt((img.width - w) / 2) + 'px';
  668. } else {
  669. img.width = w;
  670. img.height = h * oh / ow;
  671. img.style.marginTop = '-' + parseInt((img.height - h) / 2) + 'px';
  672. }
  673. }
  674. },
  675. getInsertList: function () {
  676. var i, lis = this.list.children, list = [];
  677. for (i = 0; i < lis.length; i++) {
  678. if (domUtils.hasClass(lis[i], 'selected')) {
  679. var url = lis[i].getAttribute('data-url');
  680. var title = lis[i].getAttribute('data-title') || url.substr(url.lastIndexOf('/') + 1);
  681. list.push({
  682. title: title,
  683. url: url
  684. });
  685. }
  686. }
  687. return list;
  688. }
  689. };
  690. })();