090.folders.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /** This file is part of KCFinder project
  2. *
  3. * @desc Folder 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. _.initFolders = function() {
  13. $('#folders').scroll(function() {
  14. _.menu.hide();
  15. }).disableTextSelect();
  16. $('div.folder > a').unbind().click(function() {
  17. _.menu.hide();
  18. return false;
  19. });
  20. $('div.folder > a > span.brace').unbind().click(function() {
  21. if ($(this).hasClass('opened') || $(this).hasClass('closed'))
  22. _.expandDir($(this).parent());
  23. });
  24. $('div.folder > a > span.folder').unbind().click(function() {
  25. _.changeDir($(this).parent());
  26. }).rightClick(function(el, e) {
  27. _.menuDir($(el).parent(), e);
  28. });
  29. if ($.mobile) {
  30. $('div.folder > a > span.folder').on('taphold', function() {
  31. _.menuDir($(this).parent(), {
  32. pageX: $(this).offset().left + 1,
  33. pageY: $(this).offset().top + $(this).outerHeight()
  34. });
  35. });
  36. }
  37. };
  38. _.setTreeData = function(data, path) {
  39. if (!path)
  40. path = "";
  41. else if (path.length && (path.substr(path.length - 1, 1) != '/'))
  42. path += "/";
  43. path += data.name;
  44. var selector = '#folders a[href="kcdir:/' + $.$.escapeDirs(path) + '"]';
  45. $(selector).data({
  46. name: data.name,
  47. path: path,
  48. readable: data.readable,
  49. writable: data.writable,
  50. removable: data.removable,
  51. hasDirs: data.hasDirs
  52. });
  53. $(selector + ' span.folder').addClass(data.current ? 'current' : 'regular');
  54. if (data.dirs && data.dirs.length) {
  55. $(selector + ' span.brace').addClass('opened');
  56. $.each(data.dirs, function(i, cdir) {
  57. _.setTreeData(cdir, path + "/");
  58. });
  59. } else if (data.hasDirs)
  60. $(selector + ' span.brace').addClass('closed');
  61. };
  62. _.buildTree = function(root, path) {
  63. if (!path) path = "";
  64. path += root.name;
  65. var cdir, html = '<div class="folder"><a href="kcdir:/' + $.$.escapeDirs(path) + '"><span class="brace">&nbsp;</span><span class="folder">' + $.$.htmlData(root.name) + '</span></a>';
  66. if (root.dirs) {
  67. html += '<div class="folders">';
  68. for (var i = 0; i < root.dirs.length; i++) {
  69. cdir = root.dirs[i];
  70. html += _.buildTree(cdir, path + "/");
  71. }
  72. html += '</div>';
  73. }
  74. html += '</div>';
  75. return html;
  76. };
  77. _.expandDir = function(dir) {
  78. var path = dir.data('path');
  79. if (dir.children('.brace').hasClass('opened')) {
  80. dir.parent().children('.folders').hide(500, function() {
  81. if (path == _.dir.substr(0, path.length))
  82. _.changeDir(dir);
  83. });
  84. dir.children('.brace').removeClass('opened').addClass('closed');
  85. } else {
  86. if (dir.parent().children('.folders').get(0)) {
  87. dir.parent().children('.folders').show(500);
  88. dir.children('.brace').removeClass('closed').addClass('opened');
  89. } else if (!$('#loadingDirs').get(0)) {
  90. dir.parent().append('<div id="loadingDirs">' + _.label("Loading folders...") + '</div>');
  91. $('#loadingDirs').hide().show(200, function() {
  92. $.ajax({
  93. type: "post",
  94. dataType: "json",
  95. url: _.getURL("expand"),
  96. data: {dir: path},
  97. async: false,
  98. success: function(data) {
  99. $('#loadingDirs').hide(200, function() {
  100. $('#loadingDirs').detach();
  101. });
  102. if (_.check4errors(data))
  103. return;
  104. var html = "";
  105. $.each(data.dirs, function(i, cdir) {
  106. html += '<div class="folder"><a href="kcdir:/' + $.$.escapeDirs(path + '/' + cdir.name) + '"><span class="brace">&nbsp;</span><span class="folder">' + $.$.htmlData(cdir.name) + '</span></a></div>';
  107. });
  108. if (html.length) {
  109. dir.parent().append('<div class="folders">' + html + '</div>');
  110. var folders = $(dir.parent().children('.folders').first());
  111. folders.hide();
  112. $(folders).show(500);
  113. $.each(data.dirs, function(i, cdir) {
  114. _.setTreeData(cdir, path);
  115. });
  116. }
  117. if (data.dirs.length)
  118. dir.children('.brace').removeClass('closed').addClass('opened');
  119. else
  120. dir.children('.brace').removeClass('opened closed');
  121. _.initFolders();
  122. _.initDropUpload();
  123. },
  124. error: function() {
  125. $('#loadingDirs').detach();
  126. _.alert(_.label("Unknown error."));
  127. }
  128. });
  129. });
  130. }
  131. }
  132. };
  133. _.changeDir = function(dir) {
  134. if (dir.children('span.folder').hasClass('regular')) {
  135. $('div.folder > a > span.folder').removeClass('current regular').addClass('regular');
  136. dir.children('span.folder').removeClass('regular').addClass('current');
  137. $('#files').html(_.label("Loading files..."));
  138. $.ajax({
  139. type: "post",
  140. dataType: "json",
  141. url: _.getURL("chDir"),
  142. data: {dir: dir.data('path')},
  143. async: false,
  144. success: function(data) {
  145. if (_.check4errors(data))
  146. return;
  147. _.files = data.files;
  148. _.orderFiles();
  149. _.dir = dir.data('path');
  150. _.dirWritable = data.dirWritable;
  151. _.setTitle("KCFinder: /" + _.dir);
  152. _.statusDir();
  153. },
  154. error: function() {
  155. $('#files').html(_.label("Unknown error."));
  156. }
  157. });
  158. }
  159. };
  160. _.statusDir = function() {
  161. var i = 0, size = 0;
  162. for (; i < _.files.length; i++)
  163. size += _.files[i].size;
  164. size = _.humanSize(size);
  165. $('#fileinfo').html(_.files.length + " " + _.label("files") + " (" + size + ")");
  166. };
  167. _.refreshDir = function(dir) {
  168. var path = dir.data('path');
  169. if (dir.children('.brace').hasClass('opened') || dir.children('.brace').hasClass('closed'))
  170. dir.children('.brace').removeClass('opened').addClass('closed');
  171. dir.parent().children('.folders').first().detach();
  172. if (path == _.dir.substr(0, path.length))
  173. _.changeDir(dir);
  174. _.expandDir(dir);
  175. return true;
  176. };