Edit File by line
/home/barbar84/www/wp-conte.../plugins/file-man.../applicat.../library/js/commands
File: restore.js
/**
[0] Fix | Delete
* @class elFinder command "restore"
[1] Fix | Delete
* Restore items from the trash
[2] Fix | Delete
*
[3] Fix | Delete
* @author Naoki Sawada
[4] Fix | Delete
**/
[5] Fix | Delete
(elFinder.prototype.commands.restore = function() {
[6] Fix | Delete
"use strict";
[7] Fix | Delete
var self = this,
[8] Fix | Delete
fm = this.fm,
[9] Fix | Delete
fakeCnt = 0,
[10] Fix | Delete
getFilesRecursively = function(files) {
[11] Fix | Delete
var dfd = $.Deferred(),
[12] Fix | Delete
dirs = [],
[13] Fix | Delete
results = [],
[14] Fix | Delete
reqs = [],
[15] Fix | Delete
phashes = [],
[16] Fix | Delete
getFile;
[17] Fix | Delete
[18] Fix | Delete
dfd._xhrReject = function() {
[19] Fix | Delete
$.each(reqs, function() {
[20] Fix | Delete
this && this.reject && this.reject();
[21] Fix | Delete
});
[22] Fix | Delete
getFile && getFile._xhrReject();
[23] Fix | Delete
};
[24] Fix | Delete
[25] Fix | Delete
$.each(files, function(i, f) {
[26] Fix | Delete
f.mime === 'directory'? dirs.push(f) : results.push(f);
[27] Fix | Delete
});
[28] Fix | Delete
[29] Fix | Delete
if (dirs.length) {
[30] Fix | Delete
$.each(dirs, function(i, d) {
[31] Fix | Delete
reqs.push(fm.request({
[32] Fix | Delete
data : {cmd : 'open', target : d.hash},
[33] Fix | Delete
preventDefault : true,
[34] Fix | Delete
asNotOpen : true
[35] Fix | Delete
}));
[36] Fix | Delete
phashes[i] = d.hash;
[37] Fix | Delete
});
[38] Fix | Delete
$.when.apply($, reqs).fail(function() {
[39] Fix | Delete
dfd.reject();
[40] Fix | Delete
}).done(function() {
[41] Fix | Delete
var items = [];
[42] Fix | Delete
$.each(arguments, function(i, r) {
[43] Fix | Delete
var files;
[44] Fix | Delete
if (r.files) {
[45] Fix | Delete
if (r.files.length) {
[46] Fix | Delete
items = items.concat(r.files);
[47] Fix | Delete
} else {
[48] Fix | Delete
items.push({
[49] Fix | Delete
hash: 'fakefile_' + (fakeCnt++),
[50] Fix | Delete
phash: phashes[i],
[51] Fix | Delete
mime: 'fakefile',
[52] Fix | Delete
name: 'fakefile',
[53] Fix | Delete
ts: 0
[54] Fix | Delete
});
[55] Fix | Delete
}
[56] Fix | Delete
}
[57] Fix | Delete
});
[58] Fix | Delete
fm.cache(items);
[59] Fix | Delete
getFile = getFilesRecursively(items).done(function(res) {
[60] Fix | Delete
results = results.concat(res);
[61] Fix | Delete
dfd.resolve(results);
[62] Fix | Delete
});
[63] Fix | Delete
});
[64] Fix | Delete
} else {
[65] Fix | Delete
dfd.resolve(results);
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
return dfd;
[69] Fix | Delete
},
[70] Fix | Delete
restore = function(dfrd, files, targets, ops) {
[71] Fix | Delete
var rHashes = {},
[72] Fix | Delete
others = [],
[73] Fix | Delete
found = false,
[74] Fix | Delete
dirs = [],
[75] Fix | Delete
opts = ops || {},
[76] Fix | Delete
id = +new Date(),
[77] Fix | Delete
tm, getFile;
[78] Fix | Delete
[79] Fix | Delete
fm.lockfiles({files : targets});
[80] Fix | Delete
[81] Fix | Delete
dirs = $.map(files, function(f) {
[82] Fix | Delete
return f.mime === 'directory'? f.hash : null;
[83] Fix | Delete
});
[84] Fix | Delete
[85] Fix | Delete
dfrd.done(function() {
[86] Fix | Delete
dirs && fm.exec('rm', dirs, {forceRm : true, quiet : true});
[87] Fix | Delete
}).always(function() {
[88] Fix | Delete
fm.unlockfiles({files : targets});
[89] Fix | Delete
});
[90] Fix | Delete
[91] Fix | Delete
tm = setTimeout(function() {
[92] Fix | Delete
fm.notify({type : 'search', id : id, cnt : 1, hideCnt : true, cancel : function() {
[93] Fix | Delete
getFile && getFile._xhrReject();
[94] Fix | Delete
dfrd.reject();
[95] Fix | Delete
}});
[96] Fix | Delete
}, fm.notifyDelay);
[97] Fix | Delete
[98] Fix | Delete
fakeCnt = 0;
[99] Fix | Delete
getFile = getFilesRecursively(files).always(function() {
[100] Fix | Delete
tm && clearTimeout(tm);
[101] Fix | Delete
fm.notify({type : 'search', id: id, cnt : -1, hideCnt : true});
[102] Fix | Delete
}).fail(function() {
[103] Fix | Delete
dfrd.reject('errRestore', 'errFileNotFound');
[104] Fix | Delete
}).done(function(res) {
[105] Fix | Delete
var errFolderNotfound = ['errRestore', 'errFolderNotFound'],
[106] Fix | Delete
dirTop = '';
[107] Fix | Delete
[108] Fix | Delete
if (res.length) {
[109] Fix | Delete
$.each(res, function(i, f) {
[110] Fix | Delete
var phash = f.phash,
[111] Fix | Delete
pfile,
[112] Fix | Delete
srcRoot, tPath;
[113] Fix | Delete
while(phash) {
[114] Fix | Delete
if (srcRoot = fm.trashes[phash]) {
[115] Fix | Delete
if (! rHashes[srcRoot]) {
[116] Fix | Delete
if (found) {
[117] Fix | Delete
// Keep items of other trash
[118] Fix | Delete
others.push(f.hash);
[119] Fix | Delete
return null; // continue $.each
[120] Fix | Delete
}
[121] Fix | Delete
rHashes[srcRoot] = {};
[122] Fix | Delete
found = true;
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
tPath = fm.path(f.hash).substr(fm.path(phash).length).replace(/\\/g, '/');
[126] Fix | Delete
tPath = tPath.replace(/\/[^\/]+?$/, '');
[127] Fix | Delete
if (tPath === '') {
[128] Fix | Delete
tPath = '/';
[129] Fix | Delete
}
[130] Fix | Delete
if (!rHashes[srcRoot][tPath]) {
[131] Fix | Delete
rHashes[srcRoot][tPath] = [];
[132] Fix | Delete
}
[133] Fix | Delete
if (f.mime === 'fakefile') {
[134] Fix | Delete
fm.updateCache({removed:[f.hash]});
[135] Fix | Delete
} else {
[136] Fix | Delete
rHashes[srcRoot][tPath].push(f.hash);
[137] Fix | Delete
}
[138] Fix | Delete
if (!dirTop || dirTop.length > tPath.length) {
[139] Fix | Delete
dirTop = tPath;
[140] Fix | Delete
}
[141] Fix | Delete
break;
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
// Go up one level for next check
[145] Fix | Delete
pfile = fm.file(phash);
[146] Fix | Delete
[147] Fix | Delete
if (!pfile) {
[148] Fix | Delete
phash = false;
[149] Fix | Delete
// Detection method for search results
[150] Fix | Delete
$.each(fm.trashes, function(ph) {
[151] Fix | Delete
var file = fm.file(ph),
[152] Fix | Delete
filePath = fm.path(ph);
[153] Fix | Delete
if ((!file.volumeid || f.hash.indexOf(file.volumeid) === 0) && fm.path(f.hash).indexOf(filePath) === 0) {
[154] Fix | Delete
phash = ph;
[155] Fix | Delete
return false;
[156] Fix | Delete
}
[157] Fix | Delete
});
[158] Fix | Delete
} else {
[159] Fix | Delete
phash = pfile.phash;
[160] Fix | Delete
}
[161] Fix | Delete
}
[162] Fix | Delete
});
[163] Fix | Delete
if (found) {
[164] Fix | Delete
$.each(rHashes, function(src, dsts) {
[165] Fix | Delete
var dirs = Object.keys(dsts),
[166] Fix | Delete
cnt = dirs.length;
[167] Fix | Delete
fm.request({
[168] Fix | Delete
data : {cmd : 'mkdir', target : src, dirs : dirs},
[169] Fix | Delete
notify : {type : 'chkdir', cnt : cnt},
[170] Fix | Delete
preventFail : true
[171] Fix | Delete
}).fail(function(error) {
[172] Fix | Delete
dfrd.reject(error);
[173] Fix | Delete
fm.unlockfiles({files : targets});
[174] Fix | Delete
}).done(function(data) {
[175] Fix | Delete
var cmdPaste, hashes;
[176] Fix | Delete
[177] Fix | Delete
if (hashes = data.hashes) {
[178] Fix | Delete
cmdPaste = fm.getCommand('paste');
[179] Fix | Delete
if (cmdPaste) {
[180] Fix | Delete
// wait until file cache made
[181] Fix | Delete
fm.one('mkdirdone', function() {
[182] Fix | Delete
var hasErr = false;
[183] Fix | Delete
$.each(dsts, function(dir, files) {
[184] Fix | Delete
if (hashes[dir]) {
[185] Fix | Delete
if (files.length) {
[186] Fix | Delete
if (fm.file(hashes[dir])) {
[187] Fix | Delete
fm.clipboard(files, true);
[188] Fix | Delete
fm.exec('paste', [ hashes[dir] ], {_cmd : 'restore', noToast : (opts.noToast || dir !== dirTop)})
[189] Fix | Delete
.done(function(data) {
[190] Fix | Delete
if (data && (data.error || data.warning)) {
[191] Fix | Delete
hasErr = true;
[192] Fix | Delete
}
[193] Fix | Delete
})
[194] Fix | Delete
.fail(function() {
[195] Fix | Delete
hasErr = true;
[196] Fix | Delete
})
[197] Fix | Delete
.always(function() {
[198] Fix | Delete
if (--cnt < 1) {
[199] Fix | Delete
dfrd[hasErr? 'reject' : 'resolve']();
[200] Fix | Delete
if (others.length) {
[201] Fix | Delete
// Restore items of other trash
[202] Fix | Delete
fm.exec('restore', others);
[203] Fix | Delete
}
[204] Fix | Delete
}
[205] Fix | Delete
});
[206] Fix | Delete
} else {
[207] Fix | Delete
dfrd.reject(errFolderNotfound);
[208] Fix | Delete
}
[209] Fix | Delete
} else {
[210] Fix | Delete
if (--cnt < 1) {
[211] Fix | Delete
dfrd.resolve();
[212] Fix | Delete
if (others.length) {
[213] Fix | Delete
// Restore items of other trash
[214] Fix | Delete
fm.exec('restore', others);
[215] Fix | Delete
}
[216] Fix | Delete
}
[217] Fix | Delete
}
[218] Fix | Delete
}
[219] Fix | Delete
});
[220] Fix | Delete
});
[221] Fix | Delete
} else {
[222] Fix | Delete
dfrd.reject(['errRestore', 'errCmdNoSupport', '(paste)']);
[223] Fix | Delete
}
[224] Fix | Delete
} else {
[225] Fix | Delete
dfrd.reject(errFolderNotfound);
[226] Fix | Delete
}
[227] Fix | Delete
});
[228] Fix | Delete
});
[229] Fix | Delete
} else {
[230] Fix | Delete
dfrd.reject(errFolderNotfound);
[231] Fix | Delete
}
[232] Fix | Delete
} else {
[233] Fix | Delete
dfrd.reject('errFileNotFound');
[234] Fix | Delete
dirs && fm.exec('rm', dirs, {forceRm : true, quiet : true});
[235] Fix | Delete
}
[236] Fix | Delete
});
[237] Fix | Delete
};
[238] Fix | Delete
[239] Fix | Delete
// for to be able to overwrite
[240] Fix | Delete
this.restore = restore;
[241] Fix | Delete
[242] Fix | Delete
this.linkedCmds = ['copy', 'paste', 'mkdir', 'rm'];
[243] Fix | Delete
this.updateOnSelect = false;
[244] Fix | Delete
[245] Fix | Delete
this.init = function() {
[246] Fix | Delete
// re-assign for extended command
[247] Fix | Delete
self = this;
[248] Fix | Delete
fm = this.fm;
[249] Fix | Delete
};
[250] Fix | Delete
[251] Fix | Delete
this.getstate = function(sel, e) {
[252] Fix | Delete
sel = sel || fm.selected();
[253] Fix | Delete
return sel.length && $.grep(sel, function(h) {var f = fm.file(h); return f && ! f.locked && ! fm.isRoot(f)? true : false; }).length == sel.length
[254] Fix | Delete
? 0 : -1;
[255] Fix | Delete
};
[256] Fix | Delete
[257] Fix | Delete
this.exec = function(hashes, opts) {
[258] Fix | Delete
var dfrd = $.Deferred()
[259] Fix | Delete
.fail(function(error) {
[260] Fix | Delete
error && fm.error(error);
[261] Fix | Delete
}),
[262] Fix | Delete
files = self.files(hashes);
[263] Fix | Delete
[264] Fix | Delete
if (! files.length) {
[265] Fix | Delete
return dfrd.reject();
[266] Fix | Delete
}
[267] Fix | Delete
[268] Fix | Delete
$.each(files, function(i, file) {
[269] Fix | Delete
if (fm.isRoot(file)) {
[270] Fix | Delete
return !dfrd.reject(['errRestore', file.name]);
[271] Fix | Delete
}
[272] Fix | Delete
if (file.locked) {
[273] Fix | Delete
return !dfrd.reject(['errLocked', file.name]);
[274] Fix | Delete
}
[275] Fix | Delete
});
[276] Fix | Delete
[277] Fix | Delete
if (dfrd.state() === 'pending') {
[278] Fix | Delete
this.restore(dfrd, files, hashes, opts);
[279] Fix | Delete
}
[280] Fix | Delete
[281] Fix | Delete
return dfrd;
[282] Fix | Delete
};
[283] Fix | Delete
[284] Fix | Delete
}).prototype = { forceLoad : true }; // this is required command
[285] Fix | Delete
[286] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function