imce_mkdir.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. (function($) {
  2. //add hook:load. process mkdir form
  3. imce.hooks.load.push(function () {
  4. if (!(imce.mkdirForm = imce.el('imce-mkdir-form'))) return;
  5. var form = $(imce.mkdirForm);
  6. //clean up fieldsets
  7. form.find('fieldset').each(function() {
  8. this.removeChild(this.firstChild);
  9. $(this).after(this.childNodes);
  10. }).remove();
  11. imce.mkdirOps = {};
  12. form.find('input:submit').each(function(i) {
  13. var dop = this.id.substr(5);
  14. $(imce.mkdirOps[dop] = this).click(function() {imce.dopSubmit(dop); return false;});
  15. });
  16. imce.opAdd({name: 'mngdir', title: Drupal.t('Directory'), content: form});
  17. imce.mkdirRefreshOps();
  18. //add hook:navigate. set dirops visibility
  19. imce.hooks.navigate.push(function (data, olddir, cached) {
  20. imce.mkdirRefreshOps();
  21. });
  22. // add subdir selector
  23. imce.mkdirSubSelector();
  24. });
  25. //change dirops states.
  26. imce.mkdirRefreshOps = function () {
  27. var perm, func = 'opDisable';
  28. for (var op in imce.mkdirOps) {
  29. if (perm = imce.conf.perm[op]) func = 'opEnable';
  30. $(imce.mkdirOps[op])[perm ? 'show' : 'hide']();
  31. }
  32. imce[func]('mngdir');
  33. };
  34. //successful mkdir
  35. imce.mkdirSuccess = function (response) {
  36. if (response.data) {
  37. if (response.data.diradded) imce.dirSubdirs(imce.conf.dir, response.data.diradded);
  38. if (response.data.dirremoved) imce.rmdirSubdirs(imce.conf.dir, response.data.dirremoved);
  39. imce.mkdirSSBuild && imce.mkdirSSBuild();
  40. }
  41. if (response.messages) imce.resMsgs(response.messages);
  42. };
  43. //validate default dops(mkdir, rmdir)
  44. imce.dopValidate = function(dop) {
  45. var dirname = imce.el('edit-dirname').value, dir = imce.conf.dir, branch = imce.tree[dir], newdir = (dir == '.' ? '' : dir +'/') + dirname;
  46. switch (dop) {
  47. case 'mkdir':
  48. if (imce.conf.mkdirnum && branch.ul && branch.ul.childNodes.length >= imce.conf.mkdirnum) {
  49. return imce.setMessage(Drupal.t('You are not alllowed to create more than %num directories.', {'%num': imce.conf.mkdirnum}), 'error');
  50. }
  51. if (dirname.search(/^[A-Za-z0-9_\-]+$/) == -1) {
  52. return imce.setMessage(Drupal.t('%dirname is not a valid directory name. It should contain only alphanumeric characters, hyphen and underscore.', {'%dirname': dirname}), 'error');
  53. }
  54. if (imce.tree[newdir]) {
  55. return imce.setMessage(Drupal.t('Subdirectory %dir already exists.', {'%dir': dirname}), 'error');
  56. }
  57. return true;
  58. case 'rmdir':
  59. if (!imce.tree[newdir]) {
  60. return imce.setMessage(Drupal.t('Subdirectory %dir does not exist.', {'%dir': dirname}), 'error');
  61. }
  62. return confirm(Drupal.t('Are you sure want to delete this subdirectory with all directories and files in it?'));
  63. }
  64. var func = dop +'DopValidate';
  65. if (imce[func]) return imce[func](dop);
  66. return true;
  67. };
  68. //submit directory operations
  69. imce.dopSubmit = function(dop) {
  70. if (!imce.dopValidate(dop)) return false;
  71. var func = dop +'DopSubmit';
  72. if (imce[func]) return imce[func](dop);
  73. imce.fopLoading(dop, true);
  74. $.ajax(imce.dopSettings(dop));
  75. };
  76. //ajax settings for directory operations
  77. imce.dopSettings = function (dop) {
  78. return {url: imce.ajaxURL(dop), type: 'POST', dataType: 'json', success: imce.mkdirSuccess, complete: function (response) {imce.fopLoading(dop, false); imce.mkdirForm.reset();}, data: $(imce.mkdirForm).serialize()};
  79. };
  80. //remove subdirectories
  81. imce.rmdirSubdirs = function(dir, subdirs) {
  82. var branch = imce.tree[dir];
  83. if (branch.ul && subdirs && subdirs.length) {
  84. var prefix = dir == '.' ? '' : dir +'/';
  85. for (var i in subdirs) {
  86. var subdir = prefix + subdirs[i];
  87. if (imce.tree[subdir]) {
  88. $(imce.tree[subdir].li).remove();
  89. delete imce.tree[subdir];
  90. if (imce.cache[subdir]) {
  91. $(imce.cache[subdir].files).remove();
  92. delete imce.cache[subdir];
  93. }
  94. }
  95. }
  96. if (!$('li', branch.ul).size()) {
  97. $(branch.ul).remove();
  98. $(branch.li).removeClass('expanded').addClass('leaf');
  99. delete branch.ul;
  100. }
  101. }
  102. };
  103. // visual sub directory selector
  104. imce.mkdirSubSelector = function () {
  105. var ie7 = $('html').is('.ie-7');
  106. var $inp = $(imce.el('edit-dirname'));
  107. // create selector
  108. var $subsel = $(imce.newEl('div')).attr({id: 'subdir-selector'}).css('display', 'none').appendTo(document.body);
  109. // create selector button
  110. var $button = $(imce.newEl('a')).attr({id: 'subdir-selector-button', href: '#'}).click(function() {
  111. var offset = $inp.offset();
  112. offset.top += $inp.outerHeight();
  113. $subsel.css(offset).slideDown('normal', itemfocus);
  114. $(document).mouseup(hide);
  115. ie7 && $subsel.css('width', 'auto') && $subsel.width($subsel[0].offsetWidth);
  116. return false;
  117. }).insertAfter($inp[0]);
  118. // focus on first subdir item
  119. var itemfocus = function(){$subsel.children().eq(0).focus()};
  120. // hide selector
  121. var hide = function(e){
  122. if (e.target != $subsel[0]) {
  123. $subsel.hide();
  124. $(document).unbind('mouseup', hide);
  125. }
  126. };
  127. // adjust newdir input
  128. var newdir = imce.el('edit-newdirname');
  129. newdir && $(newdir).css('marginRight', parseFloat($(newdir).css('marginRight')) + parseFloat($button.css('width')));
  130. // subdir click
  131. var subclick = function() {
  132. $inp.val(this.title.substr(this.title.lastIndexOf('/') + 1)).focus();
  133. $subsel.hide();
  134. return false;
  135. };
  136. // subdir process
  137. var subproc = function(i, a) {
  138. $(imce.newEl('a')).attr({href: '#', title: a.title}).html(a.innerHTML).click(subclick).appendTo($subsel[0]);
  139. };
  140. // navigation hook
  141. var navhook = imce.mkdirSSBuild = function() {
  142. var branch = imce.tree[imce.conf.dir];
  143. $subsel.empty();
  144. if (branch.ul && branch.ul.firstChild) {
  145. $(branch.ul).children('li').children('a').each(subproc);
  146. $button.css('visibility', 'visible');
  147. }
  148. else {
  149. $button.css('visibility', 'hidden');
  150. }
  151. };
  152. imce.hooks.navigate.push(navhook);
  153. navhook();
  154. };
  155. })(jQuery);