PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/themes/shared/js/filemanager.js

https://github.com/soonick/poMMo
JavaScript | 486 lines | 401 code | 85 blank | 0 comment | 49 complexity | e4088995258a6485090f38b12f917e72 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. var PommoFileManager = {
  2. ckeditorCallback: 0,
  3. field: '',
  4. language: {},
  5. hostname: '',
  6. imageDirectory: 'uploadedimages/data/',
  7. load: function(ckeditorCallback, field, hostname, language) {
  8. this.ckeditorCallback = ckeditorCallback;
  9. this.field = field;
  10. this.language = language;
  11. this.hostname = hostname;
  12. }
  13. }
  14. $(document).ready(function() {
  15. PommoDialog.init(false, {ajax: false});
  16. $('#column-left').tree({
  17. data: {
  18. type: 'json',
  19. async: true,
  20. opts: {
  21. method: 'post',
  22. url: 'ajax/filemanager.php?action=directory'
  23. }
  24. },
  25. selected: 'top',
  26. ui: {
  27. theme_name: 'classic',
  28. animation: 300
  29. },
  30. types: {
  31. 'default': {
  32. clickable: true,
  33. creatable: false,
  34. renameable: false,
  35. deletable: false,
  36. draggable: false,
  37. max_children: -1,
  38. max_depth: -1,
  39. valid_children: 'all'
  40. }
  41. },
  42. callback: {
  43. beforedata: function(NODE, TREE_OBJ) {
  44. if (NODE == false) {
  45. TREE_OBJ.settings.data.opts.static = [
  46. {
  47. data: 'images',
  48. attributes: {
  49. 'id': 'top',
  50. 'directory': ''
  51. },
  52. state: 'closed'
  53. }
  54. ];
  55. return { 'directory': '' }
  56. } else {
  57. TREE_OBJ.settings.data.opts.static = false;
  58. return { 'directory': $(NODE).attr('directory') }
  59. }
  60. },
  61. onselect: function (NODE, TREE_OBJ) {
  62. $.ajax({
  63. url: 'ajax/filemanager.php?action=files',
  64. type: 'post',
  65. data: 'directory=' + encodeURIComponent($(NODE).attr('directory')),
  66. dataType: 'json',
  67. success: function(json) {
  68. html = '<div>';
  69. if (json) {
  70. for (i = 0; i < json.length; i++) {
  71. name = '';
  72. filename = json[i]['filename'];
  73. for (j = 0; j < filename.length; j = j + 15) {
  74. name += filename.substr(j, 15) + '<br />';
  75. }
  76. name += json[i]['size'];
  77. html += '<a>' + name + '<input type="hidden" name="image" value="' + json[i]['file'] + '" /></a>';
  78. }
  79. }
  80. html += '</div>';
  81. $('#column-right').html(html);
  82. $('#column-right a').each(function(index, element) {
  83. $.ajax({
  84. url: 'ajax/filemanager.php?action=image&image=' + encodeURIComponent($(element).find('input[name=\'image\']').attr('value')),
  85. dataType: 'html',
  86. success: function(html) {
  87. $(element).prepend('<img src="' + html + '" title="" style="display: none;" /><br />');
  88. $(element).find('img').fadeIn();
  89. }
  90. });
  91. });
  92. },
  93. error: function(xhr, ajaxOptions, thrownError) {
  94. alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
  95. }
  96. });
  97. }
  98. }
  99. });
  100. $('#column-right a').live('click', function() {
  101. if ($(this).attr('class') == 'selected') {
  102. $(this).removeAttr('class');
  103. } else {
  104. $('#column-right a').removeAttr('class');
  105. $(this).attr('class', 'selected');
  106. }
  107. });
  108. $('#column-right a').live('dblclick', function() {
  109. var fileRelativePath = encodeURIComponent($(this).find('input[name=\'image\']').attr('value'));
  110. fileRelativePath = fileRelativePath.replace(/^%2F/, ""); // Remove prepended slash
  111. fileRelativePath = fileRelativePath.replace(/%2F/g, "/"); // Decode back slashes
  112. if (PommoFileManager.ckeditorCallback) {
  113. window.opener.CKEDITOR.tools.callFunction(PommoFileManager.ckeditorCallback, PommoFileManager.hostname + PommoFileManager.imageDirectory + fileRelativePath);
  114. self.close();
  115. } else {
  116. parent.$('#' + PommoFileManager.field).attr('value', PommoFileManager.hostname + PommoFileManager.imageDirectory + fileRelativePath);
  117. parent.$('#dialog').dialog('close');
  118. parent.$('#dialog').remove();
  119. }
  120. });
  121. $('#create').bind('click', function() {
  122. var tree = $.tree.focused();
  123. if (tree.selected) {
  124. $('#folderDialog').jqmShow();
  125. $('#folderDialog .textInput').val('');
  126. } else {
  127. alert(PommoFileManager.language.error_directory);
  128. }
  129. });
  130. $('#folderDialog input[type=\'button\']').bind('click', function() {
  131. var tree = $.tree.focused();
  132. $.ajax({
  133. url: 'ajax/filemanager.php?action=create',
  134. type: 'post',
  135. data: 'directory=' + encodeURIComponent($(tree.selected).attr('directory')) + '&name=' + encodeURIComponent($('#folderDialog input[name=\'name\']').val()),
  136. dataType: 'json',
  137. success: function(json) {
  138. if (json.success) {
  139. $('#folderDialog').jqmHide();
  140. tree.refresh(tree.selected);
  141. alert(json.success);
  142. } else {
  143. alert(json.error);
  144. }
  145. },
  146. error: function(xhr, ajaxOptions, thrownError) {
  147. alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
  148. }
  149. });
  150. });
  151. $('#delete').bind('click', function() {
  152. path = $('#column-right a.selected').find('input[name=\'image\']').attr('value');
  153. if (path) {
  154. var fileName = path.substr(path.lastIndexOf('/') + 1);
  155. if (confirm("Are you sure you want to delete '" + fileName + "'?")) {
  156. $.ajax({
  157. url: 'ajax/filemanager.php?action=delete',
  158. type: 'post',
  159. data: 'path=' + encodeURIComponent(path),
  160. dataType: 'json',
  161. success: function(json) {
  162. if (json.success) {
  163. var tree = $.tree.focused();
  164. tree.select_branch(tree.selected);
  165. alert(json.success);
  166. }
  167. if (json.error) {
  168. alert(json.error);
  169. }
  170. },
  171. error: function(xhr, ajaxOptions, thrownError) {
  172. alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
  173. }
  174. });
  175. }
  176. } else {
  177. var tree = $.tree.focused();
  178. if (tree.selected) {
  179. var directoryName = $(tree.selected).attr('directory');
  180. directoryName = directoryName.substr(directoryName.lastIndexOf('/') + 1);
  181. if (confirm("Are you sure you want to delete the folder: '" + directoryName + "'?")) {
  182. $.ajax({
  183. url: 'ajax/filemanager.php?action=delete',
  184. type: 'post',
  185. data: 'path=' + encodeURIComponent($(tree.selected).attr('directory')),
  186. dataType: 'json',
  187. success: function(json) {
  188. if (json.success) {
  189. tree.select_branch(tree.parent(tree.selected));
  190. tree.refresh(tree.selected);
  191. alert(json.success);
  192. }
  193. if (json.error) {
  194. alert(json.error);
  195. }
  196. },
  197. error: function(xhr, ajaxOptions, thrownError) {
  198. alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
  199. }
  200. });
  201. }
  202. } else {
  203. alert(PommoFileManager.language.error_select);
  204. }
  205. }
  206. });
  207. $('#move').bind('click', function() {
  208. $('#moveDialog').jqmShow();
  209. $('#moveDialog select[name=\'to\']').load('ajax/filemanager.php?action=folders');
  210. });
  211. $('#moveDialog input[type=\'button\']').bind('click', function() {
  212. path = $('#column-right a.selected').find('input[name=\'image\']').attr('value');
  213. if (path) {
  214. $.ajax({
  215. url: 'ajax/filemanager.php?action=move',
  216. type: 'post',
  217. data: 'from=' + encodeURIComponent(path) + '&to=' + encodeURIComponent($('#moveDialog select[name=\'to\']').val()),
  218. dataType: 'json',
  219. success: function(json) {
  220. if (json.success) {
  221. $('#moveDialog').jqmHide();
  222. var tree = $.tree.focused();
  223. tree.select_branch(tree.selected);
  224. alert(json.success);
  225. }
  226. if (json.error) {
  227. alert(json.error);
  228. }
  229. },
  230. error: function(xhr, ajaxOptions, thrownError) {
  231. alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
  232. }
  233. });
  234. } else {
  235. var tree = $.tree.focused();
  236. $.ajax({
  237. url: 'ajax/filemanager.php?action=move',
  238. type: 'post',
  239. data: 'from=' + encodeURIComponent($(tree.selected).attr('directory')) + '&to=' + encodeURIComponent($('#moveDialog select[name=\'to\']').val()),
  240. dataType: 'json',
  241. success: function(json) {
  242. if (json.success) {
  243. $('#moveDialog').jqmHide();
  244. tree.select_branch('#top');
  245. tree.refresh(tree.selected);
  246. alert(json.success);
  247. }
  248. if (json.error) {
  249. alert(json.error);
  250. }
  251. },
  252. error: function(xhr, ajaxOptions, thrownError) {
  253. alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
  254. }
  255. });
  256. }
  257. });
  258. $('#copy').bind('click', function() {
  259. $('#copyDialog').jqmShow();
  260. $('#copyDialog .textInput').val('');
  261. });
  262. $('#copyDialog input[type=\'button\']').bind('click', function() {
  263. path = $('#column-right a.selected').find('input[name=\'image\']').attr('value');
  264. if (path) {
  265. $.ajax({
  266. url: 'ajax/filemanager.php?action=copy',
  267. type: 'post',
  268. data: 'path=' + encodeURIComponent(path) + '&name=' + encodeURIComponent($('#copyDialog input[name=\'name\']').val()),
  269. dataType: 'json',
  270. success: function(json) {
  271. if (json.success) {
  272. $('#copyDialog').jqmHide();
  273. var tree = $.tree.focused();
  274. tree.select_branch(tree.selected);
  275. alert(json.success);
  276. }
  277. if (json.error) {
  278. alert(json.error);
  279. }
  280. },
  281. error: function(xhr, ajaxOptions, thrownError) {
  282. alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
  283. }
  284. });
  285. } else {
  286. var tree = $.tree.focused();
  287. $.ajax({
  288. url: 'ajax/filemanager.php?action=copy',
  289. type: 'post',
  290. data: 'path=' + encodeURIComponent($(tree.selected).attr('directory')) + '&name=' + encodeURIComponent($('#copyDialog input[name=\'name\']').val()),
  291. dataType: 'json',
  292. success: function(json) {
  293. if (json.success) {
  294. $('#copyDialog').jqmHide();
  295. tree.select_branch(tree.parent(tree.selected));
  296. tree.refresh(tree.selected);
  297. alert(json.success);
  298. }
  299. if (json.error) {
  300. alert(json.error);
  301. }
  302. },
  303. error: function(xhr, ajaxOptions, thrownError) {
  304. alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
  305. }
  306. });
  307. }
  308. });
  309. $('#rename').bind('click', function() {
  310. var fileName = $('#column-right a.selected').find('input[name=\'image\']').attr('value');
  311. if (fileName) {
  312. fileName = fileName.substr(fileName.lastIndexOf('/') + 1);
  313. fileName = fileName.substr(0, fileName.lastIndexOf('.'));
  314. } else {
  315. fileName = $($.tree.focused().selected).attr('directory');
  316. fileName = fileName.substr(fileName.lastIndexOf('/') + 1);
  317. }
  318. $('#renameDialog .textInput').val(fileName);
  319. $('#renameDialog').jqmShow();
  320. });
  321. $('#renameDialog input[type=\'button\']').bind('click', function() {
  322. path = $('#column-right a.selected').find('input[name=\'image\']').attr('value');
  323. if (path) {
  324. $.ajax({
  325. url: 'ajax/filemanager.php?action=rename',
  326. type: 'post',
  327. data: 'path=' + encodeURIComponent(path) + '&name=' + encodeURIComponent($('#renameDialog input[name=\'name\']').val()),
  328. dataType: 'json',
  329. success: function(json) {
  330. if (json.success) {
  331. $('#renameDialog').jqmHide();
  332. var tree = $.tree.focused();
  333. tree.select_branch(tree.selected);
  334. alert(json.success);
  335. }
  336. if (json.error) {
  337. alert(json.error);
  338. }
  339. },
  340. error: function(xhr, ajaxOptions, thrownError) {
  341. alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
  342. }
  343. });
  344. } else {
  345. var tree = $.tree.focused();
  346. $.ajax({
  347. url: 'ajax/filemanager.php?action=rename',
  348. type: 'post',
  349. data: 'path=' + encodeURIComponent($(tree.selected).attr('directory')) + '&name=' + encodeURIComponent($('#renameDialog input[name=\'name\']').val()),
  350. dataType: 'json',
  351. success: function(json) {
  352. if (json.success) {
  353. $('#renameDialog').jqmHide();
  354. tree.select_branch(tree.parent(tree.selected));
  355. tree.refresh(tree.selected);
  356. alert(json.success);
  357. }
  358. if (json.error) {
  359. alert(json.error);
  360. }
  361. },
  362. error: function(xhr, ajaxOptions, thrownError) {
  363. alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
  364. }
  365. });
  366. }
  367. });
  368. new AjaxUpload('#upload', {
  369. action: 'ajax/filemanager.php?action=upload',
  370. name: 'image',
  371. autoSubmit: false,
  372. responseType: 'json',
  373. onChange: function(file, extension) {
  374. var tree = $.tree.focused();
  375. if (tree.selected) {
  376. this.setData({'directory': $(tree.selected).attr('directory')});
  377. } else {
  378. this.setData({'directory': ''});
  379. }
  380. this.submit();
  381. },
  382. onSubmit: function(file, extension) {
  383. $('#upload').append('<img src="themes/shared/images/loader.gif" class="loading" style="padding-left: 5px;" />');
  384. },
  385. onComplete: function(file, json) {
  386. if (json.success) {
  387. var tree = $.tree.focused();
  388. tree.select_branch(tree.selected);
  389. alert(json.success);
  390. }
  391. if (json.error) {
  392. alert(json.error);
  393. }
  394. $('.loading').remove();
  395. }
  396. });
  397. $('#refresh').bind('click', function() {
  398. var tree = $.tree.focused();
  399. tree.refresh(tree.selected);
  400. });
  401. });