attachment.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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. if (file.ext && acceptExtensions.indexOf(file.ext.toLowerCase()) != -1 && file.size <= fileMaxSize) {
  389. fileCount++;
  390. fileSize += file.size;
  391. }
  392. if (fileCount === 1) {
  393. $placeHolder.addClass('element-invisible');
  394. $statusBar.show();
  395. }
  396. addFile(file);
  397. });
  398. uploader.on('fileDequeued', function (file) {
  399. if (file.ext && acceptExtensions.indexOf(file.ext.toLowerCase()) != -1 && file.size <= fileMaxSize) {
  400. fileCount--;
  401. fileSize -= file.size;
  402. }
  403. removeFile(file);
  404. updateTotalProgress();
  405. });
  406. uploader.on('filesQueued', function (file) {
  407. if (!uploader.isInProgress() && (state == 'pedding' || state == 'finish' || state == 'confirm' || state == 'ready')) {
  408. setState('ready');
  409. }
  410. updateTotalProgress();
  411. });
  412. uploader.on('all', function (type, files) {
  413. switch (type) {
  414. case 'uploadFinished':
  415. setState('confirm', files);
  416. break;
  417. case 'startUpload':
  418. /* 添加额外的GET参数 */
  419. var params = utils.serializeParam(editor.queryCommandValue('serverparam')) || '',
  420. url = utils.formatUrl(actionUrl + (actionUrl.indexOf('?') == -1 ? '?':'&') + 'encode=utf-8&' + params);
  421. uploader.option('server', url);
  422. setState('uploading', files);
  423. break;
  424. case 'stopUpload':
  425. setState('paused', files);
  426. break;
  427. }
  428. });
  429. uploader.on('uploadBeforeSend', function (file, data, header) {
  430. //这里可以通过data对象添加POST参数
  431. if (actionUrl.toLowerCase().indexOf('jsp') != -1) {
  432. header['X_Requested_With'] = 'XMLHttpRequest';
  433. }
  434. });
  435. uploader.on('uploadProgress', function (file, percentage) {
  436. var $li = $('#' + file.id),
  437. $percent = $li.find('.progress span');
  438. $percent.css('width', percentage * 100 + '%');
  439. percentages[ file.id ][ 1 ] = percentage;
  440. updateTotalProgress();
  441. });
  442. uploader.on('uploadSuccess', function (file, ret) {
  443. var $file = $('#' + file.id);
  444. try {
  445. var responseText = (ret._raw || ret),
  446. json = utils.str2json(responseText);
  447. if (json.state == 'SUCCESS') {
  448. _this.fileList.push(json);
  449. $file.append('<span class="success"></span>');
  450. } else {
  451. $file.find('.error').text(json.state).show();
  452. }
  453. } catch (e) {
  454. $file.find('.error').text(lang.errorServerUpload).show();
  455. }
  456. });
  457. uploader.on('uploadError', function (file, code) {
  458. });
  459. uploader.on('error', function (code, file) {
  460. if (code == 'Q_TYPE_DENIED' || code == 'F_EXCEED_SIZE') {
  461. addFile(file);
  462. }
  463. });
  464. uploader.on('uploadComplete', function (file, ret) {
  465. });
  466. $upload.on('click', function () {
  467. if ($(this).hasClass('disabled')) {
  468. return false;
  469. }
  470. if (state === 'ready') {
  471. uploader.upload();
  472. } else if (state === 'paused') {
  473. uploader.upload();
  474. } else if (state === 'uploading') {
  475. uploader.stop();
  476. }
  477. });
  478. $upload.addClass('state-' + state);
  479. updateTotalProgress();
  480. },
  481. getQueueCount: function () {
  482. var file, i, status, readyFile = 0, files = this.uploader.getFiles();
  483. for (i = 0; file = files[i++]; ) {
  484. status = file.getStatus();
  485. if (status == 'queued' || status == 'uploading' || status == 'progress') readyFile++;
  486. }
  487. return readyFile;
  488. },
  489. getInsertList: function () {
  490. var i, link, data, list = [],
  491. prefix = editor.getOpt('fileUrlPrefix');
  492. for (i = 0; i < this.fileList.length; i++) {
  493. data = this.fileList[i];
  494. link = data.url;
  495. list.push({
  496. title: data.original || link.substr(link.lastIndexOf('/') + 1),
  497. url: prefix + link
  498. });
  499. }
  500. return list;
  501. }
  502. };
  503. /* 在线附件 */
  504. function OnlineFile(target) {
  505. this.container = utils.isString(target) ? document.getElementById(target) : target;
  506. this.init();
  507. }
  508. OnlineFile.prototype = {
  509. init: function () {
  510. this.initContainer();
  511. this.initEvents();
  512. this.initData();
  513. },
  514. /* 初始化容器 */
  515. initContainer: function () {
  516. this.container.innerHTML = '';
  517. this.list = document.createElement('ul');
  518. this.clearFloat = document.createElement('li');
  519. domUtils.addClass(this.list, 'list');
  520. domUtils.addClass(this.clearFloat, 'clearFloat');
  521. this.list.appendChild(this.clearFloat);
  522. this.container.appendChild(this.list);
  523. },
  524. /* 初始化滚动事件,滚动到地步自动拉取数据 */
  525. initEvents: function () {
  526. var _this = this;
  527. /* 滚动拉取图片 */
  528. domUtils.on($G('fileList'), 'scroll', function(e){
  529. var panel = this;
  530. if (panel.scrollHeight - (panel.offsetHeight + panel.scrollTop) < 10) {
  531. _this.getFileData();
  532. }
  533. });
  534. /* 选中图片 */
  535. domUtils.on(this.list, 'click', function (e) {
  536. var target = e.target || e.srcElement,
  537. li = target.parentNode;
  538. if (li.tagName.toLowerCase() == 'li') {
  539. if (domUtils.hasClass(li, 'selected')) {
  540. domUtils.removeClasses(li, 'selected');
  541. } else {
  542. domUtils.addClass(li, 'selected');
  543. }
  544. }
  545. });
  546. },
  547. /* 初始化第一次的数据 */
  548. initData: function () {
  549. /* 拉取数据需要使用的值 */
  550. this.state = 0;
  551. this.listSize = editor.getOpt('fileManagerListSize');
  552. this.listIndex = 0;
  553. this.listEnd = false;
  554. /* 第一次拉取数据 */
  555. this.getFileData();
  556. },
  557. /* 向后台拉取图片列表数据 */
  558. getFileData: function () {
  559. var _this = this;
  560. if(!_this.listEnd && !this.isLoadingData) {
  561. this.isLoadingData = true;
  562. ajax.request(editor.getActionUrl(editor.getOpt('fileManagerActionName')), {
  563. timeout: 100000,
  564. data: utils.extend({
  565. start: this.listIndex,
  566. size: this.listSize
  567. }, editor.queryCommandValue('serverparam')),
  568. method: 'get',
  569. onsuccess: function (r) {
  570. try {
  571. var json = eval('(' + r.responseText + ')');
  572. if (json.state == 'SUCCESS') {
  573. _this.pushData(json.list);
  574. _this.listIndex = parseInt(json.start) + parseInt(json.list.length);
  575. if(_this.listIndex >= json.total) {
  576. _this.listEnd = true;
  577. }
  578. _this.isLoadingData = false;
  579. }
  580. } catch (e) {
  581. if(r.responseText.indexOf('ue_separate_ue') != -1) {
  582. var list = r.responseText.split(r.responseText);
  583. _this.pushData(list);
  584. _this.listIndex = parseInt(list.length);
  585. _this.listEnd = true;
  586. _this.isLoadingData = false;
  587. }
  588. }
  589. },
  590. onerror: function () {
  591. _this.isLoadingData = false;
  592. }
  593. });
  594. }
  595. },
  596. /* 添加图片到列表界面上 */
  597. pushData: function (list) {
  598. var i, item, img, filetype, preview, icon, _this = this,
  599. urlPrefix = editor.getOpt('fileManagerUrlPrefix');
  600. for (i = 0; i < list.length; i++) {
  601. if(list[i] && list[i].url) {
  602. item = document.createElement('li');
  603. icon = document.createElement('span');
  604. filetype = list[i].url.substr(list[i].url.lastIndexOf('.') + 1);
  605. if ( "png|jpg|jpeg|gif|bmp".indexOf(filetype) != -1 ) {
  606. preview = document.createElement('img');
  607. domUtils.on(preview, 'load', (function(image){
  608. return function(){
  609. _this.scale(image, image.parentNode.offsetWidth, image.parentNode.offsetHeight);
  610. };
  611. })(preview));
  612. preview.width = 113;
  613. preview.setAttribute('src', urlPrefix + list[i].url + (list[i].url.indexOf('?') == -1 ? '?noCache=':'&noCache=') + (+new Date()).toString(36) );
  614. } else {
  615. var ic = document.createElement('i'),
  616. textSpan = document.createElement('span');
  617. textSpan.innerHTML = list[i].url.substr(list[i].url.lastIndexOf('/') + 1);
  618. preview = document.createElement('div');
  619. preview.appendChild(ic);
  620. preview.appendChild(textSpan);
  621. domUtils.addClass(preview, 'file-wrapper');
  622. domUtils.addClass(textSpan, 'file-title');
  623. domUtils.addClass(ic, 'file-type-' + filetype);
  624. domUtils.addClass(ic, 'file-preview');
  625. }
  626. domUtils.addClass(icon, 'icon');
  627. item.setAttribute('data-url', urlPrefix + list[i].url);
  628. if (list[i].original) {
  629. item.setAttribute('data-title', list[i].original);
  630. }
  631. item.appendChild(preview);
  632. item.appendChild(icon);
  633. this.list.insertBefore(item, this.clearFloat);
  634. }
  635. }
  636. },
  637. /* 改变图片大小 */
  638. scale: function (img, w, h, type) {
  639. var ow = img.width,
  640. oh = img.height;
  641. if (type == 'justify') {
  642. if (ow >= oh) {
  643. img.width = w;
  644. img.height = h * oh / ow;
  645. img.style.marginLeft = '-' + parseInt((img.width - w) / 2) + 'px';
  646. } else {
  647. img.width = w * ow / oh;
  648. img.height = h;
  649. img.style.marginTop = '-' + parseInt((img.height - h) / 2) + 'px';
  650. }
  651. } else {
  652. if (ow >= oh) {
  653. img.width = w * ow / oh;
  654. img.height = h;
  655. img.style.marginLeft = '-' + parseInt((img.width - w) / 2) + 'px';
  656. } else {
  657. img.width = w;
  658. img.height = h * oh / ow;
  659. img.style.marginTop = '-' + parseInt((img.height - h) / 2) + 'px';
  660. }
  661. }
  662. },
  663. getInsertList: function () {
  664. var i, lis = this.list.children, list = [];
  665. for (i = 0; i < lis.length; i++) {
  666. if (domUtils.hasClass(lis[i], 'selected')) {
  667. var url = lis[i].getAttribute('data-url');
  668. var title = lis[i].getAttribute('data-title') || url.substr(url.lastIndexOf('/') + 1);
  669. list.push({
  670. title: title,
  671. url: url
  672. });
  673. }
  674. }
  675. return list;
  676. }
  677. };
  678. })();