080.files.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /** This file is part of KCFinder project
  2. *
  3. * @desc File related functionality
  4. * @package KCFinder
  5. * @version 3.12
  6. * @author Pavel Tzonkov <sunhater@sunhater.com>
  7. * @copyright 2010-2014 KCFinder Project
  8. * @license http://opensource.org/licenses/GPL-3.0 GPLv3
  9. * @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
  10. * @link http://kcfinder.sunhater.com
  11. */
  12. _.initFiles = function() {
  13. $(document).unbind('keydown').keydown(function(e) {
  14. return !_.selectAll(e);
  15. });
  16. $('#files').unbind().scroll(function() {
  17. _.menu.hide();
  18. }).disableTextSelect();
  19. $('.file').unbind().click(function(e) {
  20. _.selectFile($(this), e);
  21. }).rightClick(function(el, e) {
  22. _.menuFile($(el), e);
  23. }).dblclick(function() {
  24. _.returnFile($(this));
  25. });
  26. if ($.mobile)
  27. $('.file').on('taphold', function() {
  28. _.menuFile($(this), {
  29. pageX: $(this).offset().left,
  30. pageY: $(this).offset().top + $(this).outerHeight()
  31. });
  32. });
  33. $.each(_.shows, function(i, val) {
  34. $('#files .file div.' + val).css('display', ($.$.kuki.get('show' + val) == "off") ? "none" : "block");
  35. });
  36. _.statusDir();
  37. };
  38. _.showFiles = function(callBack, selected) {
  39. _.fadeFiles();
  40. setTimeout(function() {
  41. var c = $('<div></div>');
  42. $.each(_.files, function(i, file) {
  43. var f, icon,
  44. stamp = file.size + "|" + file.mtime;
  45. // List
  46. if ($.$.kuki.get('view') == "list") {
  47. if (!i) c.html('<table></table>');
  48. icon = $.$.getFileExtension(file.name);
  49. if (file.thumb)
  50. icon = ".image";
  51. else if (!icon.length || !file.smallIcon)
  52. icon = ".";
  53. icon = "themes/" + _.theme + "/img/files/small/" + icon + ".png";
  54. f = $('<tr class="file"><td class="name thumb"></td><td class="time"></td><td class="size"></td><td><div class="sizer">.</div></td></tr>');
  55. f.appendTo(c.find('table'));
  56. // Thumbnails
  57. } else {
  58. if (file.thumb)
  59. icon = _.getURL('thumb') + "&file=" + encodeURIComponent(file.name) + "&dir=" + encodeURIComponent(_.dir) + "&stamp=" + stamp;
  60. else if (file.smallThumb) {
  61. icon = _.uploadURL + "/" + _.dir + "/" + encodeURIComponent(file.name);
  62. icon = $.$.escapeDirs(icon).replace(/\'/g, "%27");
  63. } else {
  64. icon = file.bigIcon ? $.$.getFileExtension(file.name) : ".";
  65. if (!icon.length) icon = ".";
  66. icon = "themes/" + _.theme + "/img/files/big/" + icon + ".png";
  67. }
  68. f = $('<div class="file"><div class="thumb"></div><div class="name"></div><div class="time"></div><div class="size"></div><div class="sizer"></div></div>');
  69. f.appendTo(c);
  70. }
  71. f.find('.thumb').css({backgroundImage: 'url("' + icon + '")'});
  72. f.find('.name').html($.$.htmlData(file.name));
  73. f.find('.time').html(file.date);
  74. f.find('.size').html(_.humanSize(file.size));
  75. f.find('.sizer').html((file.width)+'x'+(file.height));
  76. f.data(file);
  77. if ((file.name === selected) || $.$.inArray(file.name, selected))
  78. f.addClass('selected');
  79. });
  80. c.css({opacity:'', filter:''});
  81. $('#files').html(c);
  82. if (callBack) callBack();
  83. _.initFiles();
  84. }, 200);
  85. };
  86. _.selectFile = function(file, e) {
  87. // Click with Ctrl, Meta or Shift key
  88. if (e.ctrlKey || e.metaKey || e.shiftKey) {
  89. // Click with Shift key
  90. if (e.shiftKey && !file.hasClass('selected')) {
  91. var f = file.prev();
  92. while (f.get(0) && !f.hasClass('selected')) {
  93. f.addClass('selected');
  94. f = f.prev();
  95. }
  96. }
  97. file.toggleClass('selected');
  98. // Update statusbar
  99. var files = $('.file.selected').get(),
  100. size = 0, data;
  101. if (!files.length)
  102. _.statusDir();
  103. else {
  104. $.each(files, function(i, cfile) {
  105. size += $(cfile).data('size');
  106. });
  107. size = _.humanSize(size);
  108. if (files.length > 1)
  109. $('#fileinfo').html(files.length + " " + _.label("selected files") + " (" + size + ")");
  110. else {
  111. data = $(files[0]).data();
  112. $('#fileinfo').html($.$.htmlData(data.name) + " (" + _.humanSize(data.size) + ", " + data.date + ")");
  113. }
  114. }
  115. // Normal click
  116. } else {
  117. data = file.data();
  118. $('.file').removeClass('selected');
  119. file.addClass('selected');
  120. $('#fileinfo').html($.$.htmlData(data.name) + " (" + _.humanSize(data.size) + ", " + data.date + ")");
  121. }
  122. };
  123. _.selectAll = function(e) {
  124. if ((!e.ctrlKey && !e.metaKey) || ((e.keyCode != 65) && (e.keyCode != 97))) // Ctrl-A
  125. return false;
  126. var files = $('.file'),
  127. size = 0;
  128. if (files.length) {
  129. files.addClass('selected').each(function() {
  130. size += $(this).data('size');
  131. });
  132. $('#fileinfo').html(files.length + " " + _.label("selected files") + " (" + _.humanSize(size) + ")");
  133. }
  134. return true;
  135. };
  136. _.returnFile = function(file) {
  137. var button, win, fileURL = file.substr
  138. ? file : _.uploadURL + "/" + _.dir + "/" + file.data('name');
  139. fileURL = $.$.escapeDirs(fileURL);
  140. if (_.opener.name == "ckeditor") {
  141. _.opener.CKEditor.object.tools.callFunction(_.opener.CKEditor.funcNum, fileURL, "");
  142. window.close();
  143. } else if (_.opener.name == "fckeditor") {
  144. window.opener.SetUrl(fileURL) ;
  145. window.close() ;
  146. } else if (_.opener.name == "tinymce") {
  147. win = tinyMCEPopup.getWindowArg('window');
  148. win.document.getElementById(tinyMCEPopup.getWindowArg('input')).value = fileURL;
  149. if (win.getImageData) win.getImageData();
  150. if (typeof(win.ImageDialog) != "undefined") {
  151. if (win.ImageDialog.getImageData)
  152. win.ImageDialog.getImageData();
  153. if (win.ImageDialog.showPreviewImage)
  154. win.ImageDialog.showPreviewImage(fileURL);
  155. }
  156. tinyMCEPopup.close();
  157. } else if (_.opener.name == "tinymce4") {
  158. win = (window.opener ? window.opener : window.parent);
  159. $(win.document).find('#' + _.opener.TinyMCE.field).val(fileURL);
  160. win.tinyMCE.activeEditor.windowManager.close();
  161. } else if (_.opener.callBack) {
  162. if (window.opener && window.opener.KCFinder) {
  163. _.opener.callBack(fileURL);
  164. window.close();
  165. }
  166. if (window.parent && window.parent.KCFinder) {
  167. button = $('#toolbar a[href="kcact:maximize"]');
  168. if (button.hasClass('selected'))
  169. _.maximize(button);
  170. _.opener.callBack(fileURL);
  171. }
  172. } else if (_.opener.callBackMultiple) {
  173. if (window.opener && window.opener.KCFinder) {
  174. _.opener.callBackMultiple([fileURL]);
  175. window.close();
  176. }
  177. if (window.parent && window.parent.KCFinder) {
  178. button = $('#toolbar a[href="kcact:maximize"]');
  179. if (button.hasClass('selected'))
  180. _.maximize(button);
  181. _.opener.callBackMultiple([fileURL]);
  182. }
  183. }
  184. };
  185. _.returnFiles = function(files) {
  186. if (_.opener.callBackMultiple && files.length) {
  187. var rfiles = [];
  188. $.each(files, function(i, file) {
  189. rfiles[i] = _.uploadURL + "/" + _.dir + "/" + $(file).data('name');
  190. rfiles[i] = $.$.escapeDirs(rfiles[i]);
  191. });
  192. _.opener.callBackMultiple(rfiles);
  193. if (window.opener) window.close()
  194. }
  195. };
  196. _.returnThumbnails = function(files) {
  197. if (_.opener.callBackMultiple) {
  198. var rfiles = [], j = 0;
  199. $.each(files, function(i, file) {
  200. if ($(file).data('thumb')) {
  201. rfiles[j] = _.thumbsURL + "/" + _.dir + "/" + $(file).data('name');
  202. rfiles[j] = $.$.escapeDirs(rfiles[j++]);
  203. }
  204. });
  205. _.opener.callBackMultiple(rfiles);
  206. if (window.opener) window.close()
  207. }
  208. };