| 2 |
|
|
| 3 |
//add hook:load. process mkdir form |
//add hook:load. process mkdir form |
| 4 |
imce.hooks.load.push(function () { |
imce.hooks.load.push(function () { |
| 5 |
var form = $(imce.el('imce-mkdir-form')); |
if (!(imce.mkdirForm = imce.el('imce-mkdir-form'))) return; |
| 6 |
|
var form = $(imce.mkdirForm); |
| 7 |
//clean up fieldsets |
//clean up fieldsets |
| 8 |
form.find('fieldset').each(function() { |
form.find('fieldset').each(function() { |
| 9 |
this.removeChild(this.firstChild); |
this.removeChild(this.firstChild); |
| 10 |
$(this).after(this.childNodes); |
$(this).after(this.childNodes); |
| 11 |
}).remove(); |
}).remove(); |
| 12 |
//set ajax |
imce.mkdirOps = {}; |
| 13 |
form.ajaxForm({ |
form.find('input:submit').each(function(i) { |
| 14 |
dataType: 'json', |
var dop = this.id.substr(5); |
| 15 |
beforeSubmit: imce.mkdirValidate, |
$(imce.mkdirOps[dop] = this).click(function() {imce.dopSubmit(dop); return false;}); |
| 16 |
success: imce.mkdirSuccess, |
}); |
| 17 |
complete: function () {imce.fopLoading('mkdir', false);}, |
imce.opAdd({name: 'mngdir', title: Drupal.t('Manage directories'), content: form}); |
| 18 |
resetForm: true |
imce.mkdirRefreshOps(); |
| 19 |
|
//add hook:navigate. set dirops visibility |
| 20 |
|
imce.hooks.navigate.push(function (data, olddir, cached) { |
| 21 |
|
imce.mkdirRefreshOps(); |
| 22 |
}); |
}); |
|
//add op |
|
|
imce.opAdd({name: 'mkdir', title: Drupal.t('New directory'), content: form.removeClass('imce-hide')}); |
|
|
if (!imce.conf.perm.mkdir) imce.opDisable('mkdir'); |
|
| 23 |
}); |
}); |
| 24 |
|
|
| 25 |
//validate mkdir form |
//change dirops states. |
| 26 |
imce.mkdirValidate = function (data, form, options) { |
imce.mkdirRefreshOps = function () { |
| 27 |
var dirname = data[0].value, dir = imce.conf.dir, branch = imce.tree[dir]; |
var perm, func = 'opDisable'; |
| 28 |
if (imce.conf.mkdirnum && branch.ul && branch.ul.childNodes.length >= imce.conf.mkdirnum) { |
for (var op in imce.mkdirOps) { |
| 29 |
return imce.setMessage(Drupal.t('You are not alllowed to create more than %num directories.', {'%num': imce.conf.mkdirnum}), 'error'); |
if (perm = imce.conf.perm[op]) func = 'opEnable'; |
| 30 |
} |
$(imce.mkdirOps[op])[perm ? 'show' : 'hide'](); |
|
if (dirname.search(/^[A-Za-z0-9_\-]+$/) == -1) { |
|
|
return imce.setMessage(Drupal.t('Directory name may contain only alphanumeric characters, hyphen and underscore.'), 'error'); |
|
|
} |
|
|
var newdir = (dir == '.' ? '' : dir +'/') + escape(dirname); |
|
|
if (imce.tree[newdir]) { |
|
|
return imce.setMessage(Drupal.t('Directory %dir already exists.', {'%dir': newdir}), 'error'); |
|
| 31 |
} |
} |
| 32 |
options.url = imce.ajaxURL('mkdir'); |
imce[func]('mngdir'); |
|
imce.fopLoading('mkdir', true); |
|
|
return true; |
|
| 33 |
}; |
}; |
| 34 |
|
|
| 35 |
//successful mkdir |
//successful mkdir |
| 36 |
imce.mkdirSuccess = function (response) { |
imce.mkdirSuccess = function (response) { |
| 37 |
if (response.data) imce.dirSubdirs(imce.conf.dir, response.data); |
if (response.data) { |
| 38 |
|
if (response.data.diradded) imce.dirSubdirs(imce.conf.dir, response.data.diradded); |
| 39 |
|
if (response.data.dirremoved) imce.rmdirSubdirs(imce.conf.dir, response.data.dirremoved); |
| 40 |
|
} |
| 41 |
if (response.messages) imce.resMsgs(response.messages); |
if (response.messages) imce.resMsgs(response.messages); |
| 42 |
|
}; |
| 43 |
|
|
| 44 |
|
//validate default dops(mkdir, rmdir) |
| 45 |
|
imce.dopValidate = function(dop) { |
| 46 |
|
var dirname = imce.el('edit-dirname').value, dir = imce.conf.dir, branch = imce.tree[dir], newdir = (dir == '.' ? '' : dir +'/') + dirname; |
| 47 |
|
switch (dop) { |
| 48 |
|
case 'mkdir': |
| 49 |
|
if (imce.conf.mkdirnum && branch.ul && branch.ul.childNodes.length >= imce.conf.mkdirnum) { |
| 50 |
|
return imce.setMessage(Drupal.t('You are not alllowed to create more than %num directories.', {'%num': imce.conf.mkdirnum}), 'error'); |
| 51 |
|
} |
| 52 |
|
if (dirname.search(/^[A-Za-z0-9_\-]+$/) == -1) { |
| 53 |
|
return imce.setMessage(Drupal.t('%dirname is not a valid directory name. It should contain only alphanumeric characters, hyphen and underscore.', {'%dirname': dirname}), 'error'); |
| 54 |
|
} |
| 55 |
|
if (imce.tree[newdir]) { |
| 56 |
|
return imce.setMessage(Drupal.t('Subdirectory %dir already exists.', {'%dir': dirname}), 'error'); |
| 57 |
|
} |
| 58 |
|
return true; |
| 59 |
|
case 'rmdir': |
| 60 |
|
if (!imce.tree[newdir]) { |
| 61 |
|
return imce.setMessage(Drupal.t('Subdirectory %dir does not exist.', {'%dir': dirname}), 'error'); |
| 62 |
|
} |
| 63 |
|
return confirm(Drupal.t('Are you sure want to delete this subdirectory with all directories and files in it?')); |
| 64 |
|
} |
| 65 |
|
|
| 66 |
|
var func = dop +'DopValidate'; |
| 67 |
|
if (imce[func]) return imce[func](dop); |
| 68 |
|
return true; |
| 69 |
|
}; |
| 70 |
|
|
| 71 |
|
//submit directory operations |
| 72 |
|
imce.dopSubmit = function(dop) { |
| 73 |
|
if (!imce.dopValidate(dop)) return false; |
| 74 |
|
var func = dop +'DopSubmit'; |
| 75 |
|
if (imce[func]) return imce[func](dop); |
| 76 |
|
imce.fopLoading(dop, true); |
| 77 |
|
$.ajax(imce.dopSettings(dop)); |
| 78 |
|
}; |
| 79 |
|
|
| 80 |
|
//ajax settings for directory operations |
| 81 |
|
imce.dopSettings = function (dop) { |
| 82 |
|
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()}; |
| 83 |
|
}; |
| 84 |
|
|
| 85 |
|
//remove subdirectories |
| 86 |
|
imce.rmdirSubdirs = function(dir, subdirs) { |
| 87 |
|
var branch = imce.tree[dir]; |
| 88 |
|
if (branch.ul && subdirs && subdirs.length) { |
| 89 |
|
var prefix = dir == '.' ? '' : dir +'/'; |
| 90 |
|
for (var i in subdirs) { |
| 91 |
|
var subdir = prefix + subdirs[i]; |
| 92 |
|
if (imce.tree[subdir]) { |
| 93 |
|
$(imce.tree[subdir].li).remove(); |
| 94 |
|
delete imce.tree[subdir]; |
| 95 |
|
if (imce.cache[subdir]) { |
| 96 |
|
$(imce.cache[subdir].files).remove(); |
| 97 |
|
delete imce.cache[subdir]; |
| 98 |
|
} |
| 99 |
|
} |
| 100 |
|
} |
| 101 |
|
if (!$('li', branch.ul).size()) { |
| 102 |
|
$(branch.ul).remove(); |
| 103 |
|
$(branch.li).removeClass('expanded').addClass('leaf'); |
| 104 |
|
} |
| 105 |
|
} |
| 106 |
}; |
}; |