filemanager.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*******************************************************************************
  2. * KindEditor - WYSIWYG HTML Editor for Internet
  3. * Copyright (C) 2006-2011 kindsoft.net
  4. *
  5. * @author Roddy <luolonghao@gmail.com>
  6. * @site http://www.kindsoft.net/
  7. * @licence http://www.kindsoft.net/license.php
  8. *******************************************************************************/
  9. KindEditor.plugin('filemanager', function(K) {
  10. var self = this, name = 'filemanager',
  11. fileManagerJson = K.undef(self.fileManagerJson, self.basePath + 'php/file_manager_json.php'),
  12. imgPath = self.pluginsPath + name + '/images/',
  13. lang = self.lang(name + '.');
  14. function makeFileTitle(filename, filesize, datetime) {
  15. return filename + ' (' + Math.ceil(filesize / 1024) + 'KB, ' + datetime + ')';
  16. }
  17. function bindTitle(el, data) {
  18. if (data.is_dir) {
  19. el.attr('title', data.filename);
  20. } else {
  21. el.attr('title', makeFileTitle(data.filename, data.filesize, data.datetime));
  22. }
  23. }
  24. self.plugin.filemanagerDialog = function(options) {
  25. var width = K.undef(options.width, 650),
  26. height = K.undef(options.height, 510),
  27. dirName = K.undef(options.dirName, ''),
  28. viewType = K.undef(options.viewType, 'VIEW').toUpperCase(), // "LIST" or "VIEW"
  29. clickFn = options.clickFn;
  30. var html = [
  31. '<div style="padding:10px 20px;">',
  32. // header start
  33. '<div class="ke-plugin-filemanager-header">',
  34. // left start
  35. '<div class="ke-left">',
  36. '<img class="ke-inline-block" name="moveupImg" src="' + imgPath + 'go-up.gif" width="16" height="16" border="0" alt="" /> ',
  37. '<a class="ke-inline-block" name="moveupLink" href="javascript:;">' + lang.moveup + '</a>',
  38. '</div>',
  39. // right start
  40. '<div class="ke-right">',
  41. lang.viewType + ' <select class="ke-inline-block" name="viewType">',
  42. '<option value="VIEW">' + lang.viewImage + '</option>',
  43. '<option value="LIST">' + lang.listImage + '</option>',
  44. '</select> ',
  45. lang.orderType + ' <select class="ke-inline-block" name="orderType">',
  46. '<option value="NAME">' + lang.fileName + '</option>',
  47. '<option value="SIZE">' + lang.fileSize + '</option>',
  48. '<option value="TYPE">' + lang.fileType + '</option>',
  49. '</select>',
  50. '</div>',
  51. '<div class="ke-clearfix"></div>',
  52. '</div>',
  53. // body start
  54. '<div class="ke-plugin-filemanager-body"></div>',
  55. '</div>'
  56. ].join('');
  57. var dialog = self.createDialog({
  58. name : name,
  59. width : width,
  60. height : height,
  61. title : self.lang(name),
  62. body : html
  63. }),
  64. div = dialog.div,
  65. bodyDiv = K('.ke-plugin-filemanager-body', div),
  66. moveupImg = K('[name="moveupImg"]', div),
  67. moveupLink = K('[name="moveupLink"]', div),
  68. viewServerBtn = K('[name="viewServer"]', div),
  69. viewTypeBox = K('[name="viewType"]', div),
  70. orderTypeBox = K('[name="orderType"]', div);
  71. function reloadPage(path, order, func) {
  72. var param = 'path=' + path + '&order=' + order + '&dir=' + dirName;
  73. dialog.showLoading(self.lang('ajaxLoading'));
  74. K.ajax(K.addParam(fileManagerJson, param + '&' + new Date().getTime()), function(data) {
  75. dialog.hideLoading();
  76. func(data);
  77. });
  78. }
  79. var elList = [];
  80. function bindEvent(el, result, data, createFunc) {
  81. var fileUrl = K.formatUrl(result.current_url + data.filename, 'absolute'),
  82. dirPath = encodeURIComponent(result.current_dir_path + data.filename + '/');
  83. if (data.is_dir) {
  84. el.click(function(e) {
  85. reloadPage(dirPath, orderTypeBox.val(), createFunc);
  86. });
  87. } else if (data.is_photo) {
  88. el.click(function(e) {
  89. clickFn.call(this, fileUrl, data.filename);
  90. });
  91. } else {
  92. el.click(function(e) {
  93. clickFn.call(this, fileUrl, data.filename);
  94. });
  95. }
  96. elList.push(el);
  97. }
  98. function createCommon(result, createFunc) {
  99. // remove events
  100. K.each(elList, function() {
  101. this.unbind();
  102. });
  103. moveupLink.unbind();
  104. viewTypeBox.unbind();
  105. orderTypeBox.unbind();
  106. // add events
  107. if (result.current_dir_path) {
  108. moveupLink.click(function(e) {
  109. reloadPage(result.moveup_dir_path, orderTypeBox.val(), createFunc);
  110. });
  111. }
  112. function changeFunc() {
  113. if (viewTypeBox.val() == 'VIEW') {
  114. reloadPage(result.current_dir_path, orderTypeBox.val(), createView);
  115. } else {
  116. reloadPage(result.current_dir_path, orderTypeBox.val(), createList);
  117. }
  118. }
  119. viewTypeBox.change(changeFunc);
  120. orderTypeBox.change(changeFunc);
  121. bodyDiv.html('');
  122. }
  123. function createList(result) {
  124. createCommon(result, createList);
  125. var table = document.createElement('table');
  126. table.className = 'ke-table';
  127. table.cellPadding = 0;
  128. table.cellSpacing = 0;
  129. table.border = 0;
  130. bodyDiv.append(table);
  131. var fileList = result.file_list;
  132. for (var i = 0, len = fileList.length; i < len; i++) {
  133. var data = fileList[i], row = K(table.insertRow(i));
  134. row.mouseover(function(e) {
  135. K(this).addClass('ke-on');
  136. })
  137. .mouseout(function(e) {
  138. K(this).removeClass('ke-on');
  139. });
  140. var iconUrl = imgPath + (data.is_dir ? 'folder-16.gif' : 'file-16.gif'),
  141. img = K('<img src="' + iconUrl + '" width="16" height="16" alt="' + data.filename + '" align="absmiddle" />'),
  142. cell0 = K(row[0].insertCell(0)).addClass('ke-cell ke-name').append(img).append(document.createTextNode(' ' + data.filename));
  143. if (!data.is_dir || data.has_file) {
  144. row.css('cursor', 'pointer');
  145. cell0.attr('title', data.filename);
  146. bindEvent(cell0, result, data, createList);
  147. } else {
  148. cell0.attr('title', lang.emptyFolder);
  149. }
  150. K(row[0].insertCell(1)).addClass('ke-cell ke-size').html(data.is_dir ? '-' : Math.ceil(data.filesize / 1024) + 'KB');
  151. K(row[0].insertCell(2)).addClass('ke-cell ke-datetime').html(data.datetime);
  152. }
  153. }
  154. function createView(result) {
  155. createCommon(result, createView);
  156. var fileList = result.file_list;
  157. for (var i = 0, len = fileList.length; i < len; i++) {
  158. var data = fileList[i],
  159. div = K('<div class="ke-inline-block ke-item"></div>');
  160. bodyDiv.append(div);
  161. var photoDiv = K('<div class="ke-inline-block ke-photo"></div>')
  162. .mouseover(function(e) {
  163. K(this).addClass('ke-on');
  164. })
  165. .mouseout(function(e) {
  166. K(this).removeClass('ke-on');
  167. });
  168. div.append(photoDiv);
  169. var fileUrl = result.current_url + data.filename,
  170. iconUrl = data.is_dir ? imgPath + 'folder-64.gif' : (data.is_photo ? fileUrl : imgPath + 'file-64.gif');
  171. var img = K('<img src="' + iconUrl + '" width="80" height="80" alt="' + data.filename + '" />');
  172. if (!data.is_dir || data.has_file) {
  173. photoDiv.css('cursor', 'pointer');
  174. bindTitle(photoDiv, data);
  175. bindEvent(photoDiv, result, data, createView);
  176. } else {
  177. photoDiv.attr('title', lang.emptyFolder);
  178. }
  179. photoDiv.append(img);
  180. div.append('<div class="ke-name" title="' + data.filename + '">' + data.filename + '</div>');
  181. }
  182. }
  183. viewTypeBox.val(viewType);
  184. reloadPage('', orderTypeBox.val(), viewType == 'VIEW' ? createView : createList);
  185. return dialog;
  186. }
  187. });