Skip to content

Commit

Permalink
[cmd:undo,redo] fix #2115 implement the Undo/Redo command
Browse files Browse the repository at this point in the history
  • Loading branch information
nao-pon committed Aug 1, 2017
1 parent 41be332 commit e283354
Show file tree
Hide file tree
Showing 17 changed files with 444 additions and 55 deletions.
2 changes: 2 additions & 0 deletions css/toolbar.css
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@
.elfinder-button-icon-fullscreen { background-position: 0 -784px; }
.elfinder-button-icon-unfullscreen{ background-position: 0 -800px; }
.elfinder-button-icon-empty { background-position: 0 -848px; }
.elfinder-button-icon-undo { background-position: 0 -864px; }
.elfinder-button-icon-redo { background-position: 0 -880px; }

/* button with dropdown menu*/
.elfinder .elfinder-menubutton { overflow:visible; }
Expand Down
Binary file modified img/src/toolbar.xcf
Binary file not shown.
Binary file modified img/toolbar.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 53 additions & 22 deletions js/commands/chmod.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
elFinder.prototype.commands.chmod = function() {
this.updateOnSelect = false;
var fm = this.fm,
level = {
0 : 'owner',
1 : 'group',
2 : 'other'
},
msg = {
read : fm.i18n('read'),
write : fm.i18n('write'),
execute : fm.i18n('execute'),
perm : fm.i18n('perm'),
kind : fm.i18n('kind'),
files : fm.i18n('files')
},
isPerm = function(perm){
return (!isNaN(parseInt(perm, 8) && parseInt(perm, 8) <= 511) || perm.match(/^([r-][w-][x-]){3}$/i));
};
level = {
0 : 'owner',
1 : 'group',
2 : 'other'
},
msg = {
read : fm.i18n('read'),
write : fm.i18n('write'),
execute : fm.i18n('execute'),
perm : fm.i18n('perm'),
kind : fm.i18n('kind'),
files : fm.i18n('files')
},
isPerm = function(perm){
return (!isNaN(parseInt(perm, 8) && parseInt(perm, 8) <= 511) || perm.match(/^([r-][w-][x-]){3}$/i));
};

this.tpl = {
main : '<div class="ui-helper-clearfix elfinder-info-title"><span class="elfinder-cwd-icon {class} ui-corner-all"/>{title}</div>'
Expand Down Expand Up @@ -85,24 +85,50 @@ elFinder.prototype.commands.chmod = function() {
return buttons;
},
save = function() {
var perm = $.trim($('#'+id+'-perm').val());
var perm = $.trim($('#'+id+'-perm').val()),
reqData;

if (!isPerm(perm)) return false;

dialog.elfinderdialog('close');

reqData = {
cmd : 'chmod',
targets : hashes,
mode : perm
};
fm.request({
data : {
cmd : 'chmod',
targets : hashes,
mode : perm
},
data : reqData,
notify : {type : 'chmod', cnt : cnt}
})
.fail(function(error) {
dfrd.reject(error);
})
.done(function(data) {
if (data.changed && data.changed.length) {
data.undo = {
cmd : 'chmod',
callback : function() {
var reqs = [];
$.each(prevVals, function(perm, hashes) {
reqs.push(fm.request({
data : {cmd : 'chmod', targets : hashes, mode : perm},
notify : {type : 'undo', cnt : hashes.length}
}));
});
return $.when.apply(null, reqs);
}
};
data.redo = {
cmd : 'chmod',
callback : function() {
return fm.request({
data : reqData,
notify : {type : 'redo', cnt : hashes.length}
})
}
};
}
dfrd.resolve(data);
});
},
Expand Down Expand Up @@ -148,6 +174,10 @@ elFinder.prototype.commands.chmod = function() {
var len = files.length;
for (var i2 = 0; i2 < len; i2++) {
chk = getPerm(files[i2].perm);;
if (! prevVals[chk]) {
prevVals[chk] = [];
}
prevVals[chk].push(files[i2].hash);
ret = '';
for (var i = 0; i < 3; i++){
_chk = parseInt(chk.slice(i, i+1), 8);
Expand Down Expand Up @@ -238,6 +268,7 @@ elFinder.prototype.commands.chmod = function() {
close : function() { $(this).elfinderdialog('destroy'); }
},
dialog = fm.getUI().find('#'+id),
prevVals = {},
tmb = '', title, dataTable;

if (dialog.length) {
Expand Down
81 changes: 72 additions & 9 deletions js/commands/paste.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ elFinder.prototype.commands.paste = function() {
}),
copy = function(files) {
return files.length && fm._commands.duplicate
? fm.exec('duplicate', files)
? fm.getCommand('duplicate').exec(files)
: $.Deferred().resolve();
},
paste = function(files) {
Expand Down Expand Up @@ -191,17 +191,18 @@ elFinder.prototype.commands.paste = function() {
cnt = files.length,
groups = {},
args = [],
src;
src, targets, reqData;

if (!cnt) {
return dfrd.resolve();
}

src = files[0].phash;
files = $.map(files, function(f) { return f.hash; });
//src = files[0].phash;
targets = $.map(files, function(f) { return f.hash; });

reqData = {cmd : 'paste', dst : dst.hash, targets : targets, cut : cut ? 1 : 0, renames : renames, hashes : hashes, suffix : fm.options.backupSuffix}
fm.request({
data : {cmd : 'paste', dst : dst.hash, targets : files, cut : cut ? 1 : 0, src : src, renames : renames, hashes : hashes, suffix : fm.options.backupSuffix},
data : reqData,
notify : {type : cmd, cnt : cnt},
navigate : {
toast : opts.noToast? {} : {
Expand All @@ -215,8 +216,61 @@ elFinder.prototype.commands.paste = function() {
}
}
})
.done(function() {
dfrd.resolve();
.done(function(data) {
var dsts = {},
added = data.added && data.added.length? data.added : null;
if (cut && added /*&& (!data.removed || data.removed.length === 0)*/) {
// undo
$.each(files, function(i, f) {
var phash = f.phash,
srcHash = function(name) {
var hash;
$.each(added, function(i, f) {
if (f.name === name) {
hash = f.hash;
return false;
}
});
return hash;
},
shash = srcHash(f.name);
if (shash) {
if (dsts[phash]) {
dsts[phash].push(shash);
} else {
dsts[phash] = [ shash ];
}
} else {
dsts = {};
return false;
}
});
if (Object.keys(dsts).length) {
data.undo = {
cmd : 'move',
callback : function() {
var reqs = [];
$.each(dsts, function(dst, targets) {
reqs.push(fm.request({
data : {cmd : 'paste', dst : dst, targets : targets, cut : 1},
notify : {type : 'undo', cnt : targets.length}
}));
});
return $.when.apply(null, reqs);
}
};
data.redo = {
cmd : 'move',
callback : function() {
return fm.request({
data : reqData,
notify : {type : 'redo', cnt : cnt}
});
}
};
}
}
dfrd.resolve(data);
})
.fail(function() {
dfrd.reject();
Expand Down Expand Up @@ -308,12 +362,21 @@ elFinder.prototype.commands.paste = function() {
return dfrd;
}

return $.when(
$.when(
copy(fcopy),
paste(fpaste)
).always(function() {
)
.done(function(cr, pr) {
dfrd.resolve(pr.undo? pr : void(0));
})
.fail(function() {
dfrd.reject();
})
.always(function() {
cut && fm.clipboard([]);
});

return dfrd;
}

};
22 changes: 21 additions & 1 deletion js/commands/rename.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,27 @@ elFinder.prototype.commands.rename = function() {
}
})
.done(function(data) {
dfrd.resolve();
if (data.added && data.added.length) {
data.undo = {
cmd : 'rename',
callback : function() {
return fm.request({
data : {cmd : 'rename', target : data.added[0].hash, name : file.name},
notify : {type : 'undo', cnt : 1}
});
}
};
data.redo = {
cmd : 'rename',
callback : function() {
return fm.request({
data : {cmd : 'rename', target : file.hash, name : name},
notify : {type : 'rename', cnt : 1}
});
}
};
}
dfrd.resolve(data);
if (incwd) {
fm.exec('open', data.added[0].hash);
}
Expand Down
6 changes: 3 additions & 3 deletions js/commands/restore.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@

this.linkedCmds = ['copy', 'paste', 'mkdir', 'rm'];
this.updateOnSelect = false;
this.shortcuts = [{
pattern : 'ctrl+z'
}];
//this.shortcuts = [{
// pattern : 'ctrl+z'
//}];

this.getstate = function(sel, e) {
sel = sel || fm.selected();
Expand Down
55 changes: 51 additions & 4 deletions js/commands/rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ elFinder.prototype.commands.rm = function() {
fm.unlockfiles({files : targets});
})
.done(function(data) {
var margeRes = function(data) {
var margeRes = function(data, phash, reqData) {
var undo, prevUndo, redo, prevRedo;
$.each(data, function(k, v) {
if (Array.isArray(v)) {
if (res[k]) {
Expand All @@ -170,6 +171,39 @@ elFinder.prototype.commands.rm = function() {
if (data.sync) {
res.sync = 1;
}
if (data.added && data.added.length) {
undo = function() {
var targets = $.map(data.added, function(f) { return f.hash; });
return fm.request({
data : {cmd : 'paste', dst : phash, targets : targets, cut : 1},
notify : {type : 'undo', cnt : targets.length}
});
};
redo = function() {
return fm.request({
data : reqData,
notify : {type : 'redo', cnt : targets.length}
});
};
if (res.undo) {
prevUndo = res.undo;
res.undo = function() {
undo();
prevUndo();
};
} else {
res.undo = undo;
}
if (res.redo) {
prevRedo = res.redo;
res.redo = function() {
redo();
prevRedo();
};
} else {
res.redo = redo;
}
}
},
err = ['errTrash'],
res = {},
Expand All @@ -185,9 +219,11 @@ elFinder.prototype.commands.rm = function() {
fm.notify({type : 'trash', cnt : 1, hideCnt : true, progress : prgSt});
}, fm.notifyDelay);
$.each(dsts, function(dir, files) {
var reqData;
if (hashes[dir]) {
reqData = {cmd : 'paste', dst : hashes[dir], targets : files, cut : 1};
fm.request({
data : {cmd : 'paste', dst : hashes[dir], targets : files, cut : 1},
data : reqData,
preventDefault : true
})
.fail(function(error) {
Expand All @@ -196,9 +232,10 @@ elFinder.prototype.commands.rm = function() {
}
})
.done(function(data) {
var phash = fm.file(files[0]).phash;
data = fm.normalize(data);
fm.updateCache(data);
margeRes(data);
margeRes(data, phash, reqData);
if (data.warning) {
err = err.concat(data.warning);
delete data.warning;
Expand Down Expand Up @@ -242,6 +279,16 @@ elFinder.prototype.commands.rm = function() {
}
}
res._noSound = true;
if (res.undo && res.redo) {
res.undo = {
cmd : 'trash',
callback : res.undo,
};
res.redo = {
cmd : 'trash',
callback : res.redo
};
}
dfrd.resolve(res);
} else {
dfrd.reject(err);
Expand Down Expand Up @@ -334,7 +381,7 @@ elFinder.prototype.commands.rm = function() {
}
e.stopPropagation();
e.preventDefault();
self.exec(void(0), {forceRm : true});
fm.exec('rm', void(0), {_userAction: true, forceRm : true});
})
};
}
Expand Down

0 comments on commit e283354

Please sign in to comment.