attachment.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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. if (!WebUploader.Uploader.support()) {
  138. $('#filePickerReady').after($('<div>').html(lang.errorNotSupport)).hide();
  139. return;
  140. } else if (!editor.getOpt('fileActionName')) {
  141. $('#filePickerReady').after($('<div>').html(lang.errorLoadConfig)).hide();
  142. return;
  143. }
  144. uploader = _this.uploader = WebUploader.create({
  145. pick: {
  146. id: '#filePickerReady',
  147. label: lang.uploadSelectFile
  148. },
  149. dnd: '#dndArea',
  150. disableGlobalDnd: true,
  151. paste :document.body,
  152. swf: '../../third-party/webuploader/Uploader.swf',
  153. server: actionUrl,
  154. fileVal: editor.getOpt('fileFieldName'),
  155. duplicate: true,
  156. fileSingleSizeLimit: fileMaxSize,
  157. compress: false
  158. });
  159. uploader.addButton({
  160. id: '#filePickerBlock'
  161. });
  162. uploader.addButton({
  163. id: '#filePickerBtn',
  164. label: lang.uploadAddFile
  165. });
  166. setState('pedding');
  167. // 当有文件添加进来时执行,负责view的创建
  168. function addFile(file) {
  169. var $li = $('<li id="' + file.id + '">' +
  170. '<p class="title">' + file.name + '</p>' +
  171. '<p class="imgWrap"></p>' +
  172. '<p class="progress"><span></span></p>' +
  173. '</li>'),
  174. $btns = $('<div class="file-panel">' +
  175. '<span class="cancel">' + lang.uploadDelete + '</span>' +
  176. '<span class="rotateRight">' + lang.uploadTurnRight + '</span>' +
  177. '<span class="rotateLeft">' + lang.uploadTurnLeft + '</span></div>').appendTo($li),
  178. $prgress = $li.find('p.progress span'),
  179. $wrap = $li.find('p.imgWrap'),
  180. $info = $('<p class="error"></p>').hide().appendTo($li),
  181. showError = function (code) {
  182. switch (code) {
  183. case 'exceed_size':
  184. text = lang.errorExceedSize;
  185. break;
  186. case 'interrupt':
  187. text = lang.errorInterrupt;
  188. break;
  189. case 'http':
  190. text = lang.errorHttp;
  191. break;
  192. case 'not_allow_type':
  193. text = lang.errorFileType;
  194. break;
  195. default:
  196. text = lang.errorUploadRetry;
  197. break;
  198. }
  199. $info.text(text).show();
  200. };
  201. if (file.getStatus() === 'invalid') {
  202. showError(file.statusText);
  203. } else {
  204. $wrap.text(lang.uploadPreview);
  205. if ('|png|jpg|jpeg|bmp|gif|'.indexOf('|'+file.ext.toLowerCase()+'|') == -1) {
  206. $wrap.empty().addClass('notimage').append('<i class="file-preview file-type-' + file.ext.toLowerCase() + '"></i>' +
  207. '<span class="file-title" title="' + file.name + '">' + file.name + '</span>');
  208. } else {
  209. if (browser.ie && browser.version <= 7) {
  210. $wrap.text(lang.uploadNoPreview);
  211. } else {
  212. uploader.makeThumb(file, function (error, src) {
  213. if (error || !src) {
  214. $wrap.text(lang.uploadNoPreview);
  215. } else {
  216. var $img = $('<img src="' + src + '">');
  217. $wrap.empty().append($img);
  218. $img.on('error', function () {
  219. $wrap.text(lang.uploadNoPreview);
  220. });
  221. }
  222. }, thumbnailWidth, thumbnailHeight);
  223. }
  224. }
  225. percentages[ file.id ] = [ file.size, 0 ];
  226. file.rotation = 0;
  227. /* 检查文件格式 */
  228. if (!file.ext || acceptExtensions.indexOf(file.ext.toLowerCase()) == -1) {
  229. showError('not_allow_type');
  230. uploader.removeFile(file);
  231. }
  232. }
  233. file.on('statuschange', function (cur, prev) {
  234. if (prev === 'progress') {
  235. $prgress.hide().width(0);
  236. } else if (prev === 'queued') {
  237. $li.off('mouseenter mouseleave');
  238. $btns.remove();
  239. }
  240. // 成功
  241. if (cur === 'error' || cur === 'invalid') {
  242. showError(file.statusText);
  243. percentages[ file.id ][ 1 ] = 1;
  244. } else if (cur === 'interrupt') {
  245. showError('interrupt');
  246. } else if (cur === 'queued') {
  247. percentages[ file.id ][ 1 ] = 0;
  248. } else if (cur === 'progress') {
  249. $info.hide();
  250. $prgress.css('display', 'block');
  251. } else if (cur === 'complete') {
  252. }
  253. $li.removeClass('state-' + prev).addClass('state-' + cur);
  254. });
  255. $li.on('mouseenter', function () {
  256. $btns.stop().animate({height: 30});
  257. });
  258. $li.on('mouseleave', function () {
  259. $btns.stop().animate({height: 0});
  260. });
  261. $btns.on('click', 'span', function () {
  262. var index = $(this).index(),
  263. deg;
  264. switch (index) {
  265. case 0:
  266. uploader.removeFile(file);
  267. return;
  268. case 1:
  269. file.rotation += 90;
  270. break;
  271. case 2:
  272. file.rotation -= 90;
  273. break;
  274. }
  275. if (supportTransition) {
  276. deg = 'rotate(' + file.rotation + 'deg)';
  277. $wrap.css({
  278. '-webkit-transform': deg,
  279. '-mos-transform': deg,
  280. '-o-transform': deg,
  281. 'transform': deg
  282. });
  283. } else {
  284. $wrap.css('filter', 'progid:DXImageTransform.Microsoft.BasicImage(rotation=' + (~~((file.rotation / 90) % 4 + 4) % 4) + ')');
  285. }
  286. });
  287. $li.insertBefore($filePickerBlock);
  288. }
  289. // 负责view的销毁
  290. function removeFile(file) {
  291. var $li = $('#' + file.id);
  292. delete percentages[ file.id ];
  293. updateTotalProgress();
  294. $li.off().find('.file-panel').off().end().remove();
  295. }
  296. function updateTotalProgress() {
  297. var loaded = 0,
  298. total = 0,
  299. spans = $progress.children(),
  300. percent;
  301. $.each(percentages, function (k, v) {
  302. total += v[ 0 ];
  303. loaded += v[ 0 ] * v[ 1 ];
  304. });
  305. percent = total ? loaded / total : 0;
  306. spans.eq(0).text(Math.round(percent * 100) + '%');
  307. spans.eq(1).css('width', Math.round(percent * 100) + '%');
  308. updateStatus();
  309. }
  310. function setState(val, files) {
  311. if (val != state) {
  312. var stats = uploader.getStats();
  313. $upload.removeClass('state-' + state);
  314. $upload.addClass('state-' + val);
  315. switch (val) {
  316. /* 未选择文件 */
  317. case 'pedding':
  318. $queue.addClass('element-invisible');
  319. $statusBar.addClass('element-invisible');
  320. $placeHolder.removeClass('element-invisible');
  321. $progress.hide(); $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(); $info.show();
  330. $upload.text(lang.uploadStart);
  331. uploader.refresh();
  332. break;
  333. /* 上传中 */
  334. case 'uploading':
  335. $progress.show(); $info.hide();
  336. $upload.text(lang.uploadPause);
  337. break;
  338. /* 暂停上传 */
  339. case 'paused':
  340. $progress.show(); $info.hide();
  341. $upload.text(lang.uploadContinue);
  342. break;
  343. case 'confirm':
  344. $progress.show(); $info.hide();
  345. $upload.text(lang.uploadStart);
  346. stats = uploader.getStats();
  347. if (stats.successNum && !stats.uploadFailNum) {
  348. setState('finish');
  349. return;
  350. }
  351. break;
  352. case 'finish':
  353. $progress.hide(); $info.show();
  354. if (stats.uploadFailNum) {
  355. $upload.text(lang.uploadRetry);
  356. } else {
  357. $upload.text(lang.uploadStart);
  358. }
  359. break;
  360. }
  361. state = val;
  362. updateStatus();
  363. }
  364. if (!_this.getQueueCount()) {
  365. $upload.addClass('disabled')
  366. } else {
  367. $upload.removeClass('disabled')
  368. }
  369. }
  370. function updateStatus() {
  371. var text = '', stats;
  372. if (state === 'ready') {
  373. text = lang.updateStatusReady.replace('_', fileCount).replace('_KB', WebUploader.formatSize(fileSize));
  374. } else if (state === 'confirm') {
  375. stats = uploader.getStats();
  376. if (stats.uploadFailNum) {
  377. text = lang.updateStatusConfirm.replace('_', stats.successNum).replace('_', stats.successNum);
  378. }
  379. } else {
  380. stats = uploader.getStats();
  381. text = lang.updateStatusFinish.replace('_', fileCount).
  382. replace('_KB', WebUploader.formatSize(fileSize)).
  383. replace('_', stats.successNum);
  384. if (stats.uploadFailNum) {
  385. text += lang.updateStatusError.replace('_', stats.uploadFailNum);
  386. }
  387. }
  388. $info.html(text);
  389. }
  390. uploader.on('fileQueued', function (file) {
  391. fileCount++;
  392. fileSize += file.size;
  393. if (fileCount === 1) {
  394. $placeHolder.addClass('element-invisible');
  395. $statusBar.show();
  396. }
  397. addFile(file);
  398. });
  399. uploader.on('fileDequeued', function (file) {
  400. fileCount--;
  401. fileSize -= file.size;
  402. removeFile(file);
  403. updateTotalProgress();
  404. });
  405. uploader.on('filesQueued', function (file) {
  406. if (!uploader.isInProgress() && (state == 'pedding' || state == 'finish' || state == 'confirm' || state == 'ready')) {
  407. setState('ready');
  408. }
  409. updateTotalProgress();
  410. });
  411. uploader.on('all', function (type, files) {
  412. switch (type) {
  413. case 'uploadFinished':
  414. setState('confirm', files);
  415. break;
  416. case 'startUpload':
  417. /* 添加额外的GET参数 */
  418. var params = utils.serializeParam(editor.queryCommandValue('serverparam')) || '',
  419. url = utils.formatUrl(actionUrl + (actionUrl.indexOf('?') == -1 ? '?':'&') + 'encode=utf-8&' + params);
  420. uploader.option('server', url);
  421. setState('uploading', files);
  422. break;
  423. case 'stopUpload':
  424. setState('paused', files);
  425. break;
  426. }
  427. });
  428. uploader.on('uploadBeforeSend', function (file, data, header) {
  429. //这里可以通过data对象添加POST参数
  430. header['X_Requested_With'] = 'XMLHttpRequest';
  431. });
  432. uploader.on('uploadProgress', function (file, percentage) {
  433. var $li = $('#' + file.id),
  434. $percent = $li.find('.progress span');
  435. $percent.css('width', percentage * 100 + '%');
  436. percentages[ file.id ][ 1 ] = percentage;
  437. updateTotalProgress();
  438. });
  439. uploader.on('uploadSuccess', function (file, ret) {
  440. var $file = $('#' + file.id);
  441. try {
  442. var responseText = (ret._raw || ret),
  443. json = utils.str2json(responseText);
  444. if (json.state == 'SUCCESS') {
  445. _this.fileList.push(json);
  446. $file.append('<span class="success"></span>');
  447. } else {
  448. $file.find('.error').text(json.state).show();
  449. }
  450. } catch (e) {
  451. $file.find('.error').text(lang.errorServerUpload).show();
  452. }
  453. });
  454. uploader.on('uploadError', function (file, code) {
  455. });
  456. uploader.on('error', function (code, file) {
  457. if (code == 'Q_TYPE_DENIED' || code == 'F_EXCEED_SIZE') {
  458. addFile(file);
  459. }
  460. });
  461. uploader.on('uploadComplete', function (file, ret) {
  462. });
  463. $upload.on('click', function () {
  464. if ($(this).hasClass('disabled')) {
  465. return false;
  466. }
  467. if (state === 'ready') {
  468. uploader.upload();
  469. } else if (state === 'paused') {
  470. uploader.upload();
  471. } else if (state === 'uploading') {
  472. uploader.stop();
  473. }
  474. });
  475. $upload.addClass('state-' + state);
  476. updateTotalProgress();
  477. },
  478. getQueueCount: function () {
  479. var file, i, status, readyFile = 0, files = this.uploader.getFiles();
  480. for (i = 0; file = files[i++]; ) {
  481. status = file.getStatus();
  482. if (status == 'queued' || status == 'uploading' || status == 'progress') readyFile++;
  483. }
  484. return readyFile;
  485. },
  486. getInsertList: function () {
  487. var i, link, data, list = [],
  488. prefix = editor.getOpt('fileUrlPrefix');
  489. for (i = 0; i < this.fileList.length; i++) {
  490. data = this.fileList[i];
  491. link = data.url;
  492. list.push({
  493. title: data.original || link.substr(link.lastIndexOf('/') + 1),
  494. url: prefix + link
  495. });
  496. }
  497. return list;
  498. }
  499. };
  500. /* 在线附件 */
  501. function OnlineFile(target) {
  502. this.container = utils.isString(target) ? document.getElementById(target) : target;
  503. this.init();
  504. }
  505. OnlineFile.prototype = {
  506. init: function () {
  507. this.initContainer();
  508. this.initEvents();
  509. this.initData();
  510. },
  511. /* 初始化容器 */
  512. initContainer: function () {
  513. this.container.innerHTML = '';
  514. this.list = document.createElement('ul');
  515. this.clearFloat = document.createElement('li');
  516. domUtils.addClass(this.list, 'list');
  517. domUtils.addClass(this.clearFloat, 'clearFloat');
  518. this.list.appendChild(this.clearFloat);
  519. this.container.appendChild(this.list);
  520. },
  521. /* 初始化滚动事件,滚动到地步自动拉取数据 */
  522. initEvents: function () {
  523. var _this = this;
  524. /* 滚动拉取图片 */
  525. domUtils.on($G('fileList'), 'scroll', function(e){
  526. var panel = this;
  527. if (panel.scrollHeight - (panel.offsetHeight + panel.scrollTop) < 10) {
  528. _this.getFileData();
  529. }
  530. });
  531. /* 选中图片 */
  532. domUtils.on(this.list, 'click', function (e) {
  533. var target = e.target || e.srcElement,
  534. li = target.parentNode;
  535. if (li.tagName.toLowerCase() == 'li') {
  536. if (domUtils.hasClass(li, 'selected')) {
  537. domUtils.removeClasses(li, 'selected');
  538. } else {
  539. domUtils.addClass(li, 'selected');
  540. }
  541. }
  542. });
  543. },
  544. /* 初始化第一次的数据 */
  545. initData: function () {
  546. /* 拉取数据需要使用的值 */
  547. this.state = 0;
  548. this.listSize = editor.getOpt('fileManagerListSize');
  549. this.listIndex = 0;
  550. this.listEnd = false;
  551. /* 第一次拉取数据 */
  552. this.getFileData();
  553. },
  554. /* 向后台拉取图片列表数据 */
  555. getFileData: function () {
  556. var _this = this;
  557. if(!_this.listEnd && !this.isLoadingData) {
  558. this.isLoadingData = true;
  559. ajax.request(editor.getActionUrl(editor.getOpt('fileManagerActionName')), {
  560. timeout: 100000,
  561. data: utils.extend({
  562. start: this.listIndex,
  563. size: this.listSize
  564. }, editor.queryCommandValue('serverparam')),
  565. method: 'get',
  566. onsuccess: function (r) {
  567. try {
  568. var json = eval('(' + r.responseText + ')');
  569. if (json.state == 'SUCCESS') {
  570. _this.pushData(json.list);
  571. _this.listIndex = parseInt(json.start) + parseInt(json.list.length);
  572. if(_this.listIndex >= json.total) {
  573. _this.listEnd = true;
  574. }
  575. _this.isLoadingData = false;
  576. }
  577. } catch (e) {
  578. if(r.responseText.indexOf('ue_separate_ue') != -1) {
  579. var list = r.responseText.split(r.responseText);
  580. _this.pushData(list);
  581. _this.listIndex = parseInt(list.length);
  582. _this.listEnd = true;
  583. _this.isLoadingData = false;
  584. }
  585. }
  586. },
  587. onerror: function () {
  588. _this.isLoadingData = false;
  589. }
  590. });
  591. }
  592. },
  593. /* 添加图片到列表界面上 */
  594. pushData: function (list) {
  595. var i, item, img, filetype, preview, icon, _this = this,
  596. urlPrefix = editor.getOpt('fileManagerUrlPrefix');
  597. for (i = 0; i < list.length; i++) {
  598. if(list[i] && list[i].url) {
  599. item = document.createElement('li');
  600. icon = document.createElement('span');
  601. filetype = list[i].url.substr(list[i].url.lastIndexOf('.') + 1);
  602. if ( "png|jpg|jpeg|gif|bmp".indexOf(filetype) != -1 ) {
  603. preview = document.createElement('img');
  604. domUtils.on(preview, 'load', (function(image){
  605. return function(){
  606. _this.scale(image, image.parentNode.offsetWidth, image.parentNode.offsetHeight);
  607. };
  608. })(preview));
  609. preview.width = 113;
  610. preview.setAttribute('src', urlPrefix + list[i].url + (list[i].url.indexOf('?') == -1 ? '?noCache=':'&noCache=') + (+new Date()).toString(36) );
  611. } else {
  612. var ic = document.createElement('i'),
  613. textSpan = document.createElement('span');
  614. textSpan.innerHTML = list[i].url.substr(list[i].url.lastIndexOf('/') + 1);
  615. preview = document.createElement('div');
  616. preview.appendChild(ic);
  617. preview.appendChild(textSpan);
  618. domUtils.addClass(preview, 'file-wrapper');
  619. domUtils.addClass(textSpan, 'file-title');
  620. domUtils.addClass(ic, 'file-type-' + filetype);
  621. domUtils.addClass(ic, 'file-preview');
  622. }
  623. domUtils.addClass(icon, 'icon');
  624. item.setAttribute('data-url', urlPrefix + list[i].url);
  625. if (list[i].original) {
  626. item.setAttribute('data-title', list[i].original);
  627. }
  628. item.appendChild(preview);
  629. item.appendChild(icon);
  630. this.list.insertBefore(item, this.clearFloat);
  631. }
  632. }
  633. },
  634. /* 改变图片大小 */
  635. scale: function (img, w, h, type) {
  636. var ow = img.width,
  637. oh = img.height;
  638. if (type == 'justify') {
  639. if (ow >= oh) {
  640. img.width = w;
  641. img.height = h * oh / ow;
  642. img.style.marginLeft = '-' + parseInt((img.width - w) / 2) + 'px';
  643. } else {
  644. img.width = w * ow / oh;
  645. img.height = h;
  646. img.style.marginTop = '-' + parseInt((img.height - h) / 2) + 'px';
  647. }
  648. } else {
  649. if (ow >= oh) {
  650. img.width = w * ow / oh;
  651. img.height = h;
  652. img.style.marginLeft = '-' + parseInt((img.width - w) / 2) + 'px';
  653. } else {
  654. img.width = w;
  655. img.height = h * oh / ow;
  656. img.style.marginTop = '-' + parseInt((img.height - h) / 2) + 'px';
  657. }
  658. }
  659. },
  660. getInsertList: function () {
  661. var i, lis = this.list.children, list = [];
  662. for (i = 0; i < lis.length; i++) {
  663. if (domUtils.hasClass(lis[i], 'selected')) {
  664. var url = lis[i].getAttribute('data-url');
  665. var title = lis[i].getAttribute('data-title') || url.substr(url.lastIndexOf('/') + 1);
  666. list.push({
  667. title: title,
  668. url: url
  669. });
  670. }
  671. }
  672. return list;
  673. }
  674. };
  675. })();