attachment.js 29KB

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