Skip to content

Commit

Permalink
[cmd:edit] fix #2020 add "Save As" button
Browse files Browse the repository at this point in the history
  • Loading branch information
nao-pon committed May 24, 2017
1 parent 4af28b0 commit 37ed059
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 20 deletions.
59 changes: 46 additions & 13 deletions js/commands/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,10 @@ elFinder.prototype.commands.edit = function() {
dialog = function(id, file, content, encoding) {

var dfrd = $.Deferred(),
ta,
old,
save = function() {
save = function(hash) {
ta.editor && ta.editor.save(ta[0], ta.editor.instance);
old = ta.getContent();
dfrd.notifyWith(ta, [selEncoding? selEncoding.val():void(0)]);
dfrd.notifyWith(ta, [selEncoding? selEncoding.val():void(0), hash]);
},
cancel = function() {
ta.elfinderdialog('close');
Expand All @@ -68,20 +66,44 @@ elFinder.prototype.commands.edit = function() {
save();
cancel();
},
saveAs = function() {
var prevOld = old,
fail = function() {
old = prevOld;
dialogNode.fadeIn();
};

self.mime = file.mime;
self.prefix = file.name;
self.requestCmd = 'mkfile';
self.nextAction = { cmd: 'edit', msg: 'cmdedit' };
dialogNode.fadeOut();
$.proxy(fm.res('mixin', 'make'), self)()
.done(function(data) {
if (data.added && data.added.length) {
save(data.added[0].hash);
dialogNode.show();
cancel();
} else {
fail();
}
})
.fail(fail);
},
changed = function() {
ta.editor && ta.editor.save(ta[0], ta.editor.instance);
return (old !== ta.getContent());
},
opts = {
title : fm.escape(file.name),
width : self.options.dialogWidth || 450,
width : self.options.dialogWidth || (Math.min(650, $(window).width() * .9)),
buttons : {},
allowMaximize : true,
btnHoverFocus : false,
closeOnEscape : false,
close : function() {
var close = function(){
dfrd.reject();
dfrd.resolve();
ta.editor && ta.editor.close(ta[0], ta.editor.instance);
ta.elfinderdialog('destroy');
};
Expand All @@ -100,7 +122,13 @@ elFinder.prototype.commands.edit = function() {
cancel : {
label : 'btnClose',
callback : close
}
},
buttons : [{
label : 'btnSaveAs',
callback : function() {
saveAs();
}
}]
});
} else {
close();
Expand Down Expand Up @@ -158,8 +186,7 @@ elFinder.prototype.commands.edit = function() {
}
return false;
},
selEncoding,
extEditor;
ta, old, dialogNode, selEncoding, extEditor;

$.each(self.options.editors || [], function(i, editor) {
if (mimeMatch(file.mime, editor.mimes || null)
Expand Down Expand Up @@ -290,13 +317,14 @@ elFinder.prototype.commands.edit = function() {

opts.buttons[fm.i18n('btnSave')] = save;
opts.buttons[fm.i18n('btnSaveClose')] = savecl;
opts.buttons[fm.i18n('btnSaveAs')] = saveAs;
opts.buttons[fm.i18n('btnCancel')] = cancel;

fm.dialog(ta, opts)
dialogNode = fm.dialog(ta, opts)
.attr('id', id)
.on('keydown keyup keypress', function(e) {
e.stopPropagation();
});
}).closest('.ui-dialog');
return dfrd.promise();
},

Expand Down Expand Up @@ -366,8 +394,14 @@ elFinder.prototype.commands.edit = function() {
});
} else {
dialog(id, file, data.content, data.encoding)
.progress(function(encoding) {
.done(function(data) {
dfrd.resolve(data);
})
.progress(function(encoding, newHash) {
var ta = this;
if (newHash) {
hash = newHash;
}
fm.request({
options : {type : 'post'},
data : {
Expand All @@ -384,7 +418,6 @@ elFinder.prototype.commands.edit = function() {
})
.done(function(data) {
data.changed && data.changed.length && fm.change(data);
dfrd.resolve(data);
setTimeout(function(){
ta.focus();
ta.editor && ta.editor.focus(ta[0], ta.editor.instance);
Expand Down
12 changes: 7 additions & 5 deletions js/elFinder.resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ elFinder.prototype.resources = {

mixin : {
make : function() {
var fm = this.fm,
var self = this,
fm = this.fm,
cmd = this.name,
req = this.requestCmd || cmd,
wz = fm.getUI('workzone'),
org = (this.origin && this.origin === 'navbar')? 'tree' : 'cwd',
ui = fm.getUI(org),
Expand Down Expand Up @@ -197,7 +199,7 @@ elFinder.prototype.resources = {
fm.lockfiles({files : [id]});

fm.request({
data : Object.assign({cmd : cmd, name : name, target : phash}, data || {}),
data : Object.assign({cmd : req, name : name, target : phash}, data || {}),
notify : {type : cmd, cnt : 1},
preventFail : true,
syncOnFail : true
Expand All @@ -215,11 +217,11 @@ elFinder.prototype.resources = {
dirhash = item.hash,
newItem = ui.find('#'+fm[find](dirhash));
if (sel && move) {
fm.one(cmd+'done', function() {
fm.one(req+'done', function() {
fm.exec('paste', dirhash);
});
}
fm.one(cmd+'done', function() {
fm.one(req+'done', function() {
var acts = {
'directory' : { cmd: 'open', msg: 'cmdopendir' },
'text/plain': { cmd: 'edit', msg: 'cmdedit' },
Expand All @@ -228,7 +230,7 @@ elFinder.prototype.resources = {
act, extNode;
newItem = ui.find('#'+fm[find](item.hash));
if (data.added.length === 1) {
act = acts[item.mime] || acts['default'];
act = self.nextAction || acts[item.mime] || acts['default'];
extNode = $('<div/>').append(
$('<button type="button" class="ui-button ui-widget ui-state-default ui-corner-all elfinder-tabstop"><span class="ui-button-text">'
+fm.i18n(act.msg)
Expand Down
1 change: 1 addition & 0 deletions js/i18n/elfinder.LANG.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
'btnRenameAll' : 'Rename(All)', // from v2.1.24 added 6.4.2017
'btnPrevious' : 'Prev ($1/$2)', // from v2.1.24 added 11.5.2017
'btnNext' : 'Next ($1/$2)', // from v2.1.24 added 11.5.2017
'btnSaveAs' : 'Save As', // from v2.1.25 added 24.5.2017

/******************************** notifications ********************************/
'ntfopen' : 'Open folder',
Expand Down
3 changes: 2 additions & 1 deletion js/i18n/elfinder.en.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* English translation
* @author Troex Nevelin <[email protected]>
* @version 2017-05-23
* @version 2017-05-24
*/
// elfinder.en.js is integrated into elfinder.(full|min).js by jake build
if (typeof elFinder === 'function' && elFinder.prototype.i18) {
Expand Down Expand Up @@ -169,6 +169,7 @@ if (typeof elFinder === 'function' && elFinder.prototype.i18) {
'btnRenameAll' : 'Rename(All)', // from v2.1.24 added 6.4.2017
'btnPrevious' : 'Prev ($1/$2)', // from v2.1.24 added 11.5.2017
'btnNext' : 'Next ($1/$2)', // from v2.1.24 added 11.5.2017
'btnSaveAs' : 'Save As', // from v2.1.25 added 24.5.2017

/******************************** notifications ********************************/
'ntfopen' : 'Open folder',
Expand Down
3 changes: 2 additions & 1 deletion js/i18n/elfinder.jp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Japanese translation
* @author Tomoaki Yoshida <[email protected]>
* @author Naoki Sawada <[email protected]>
* @version 2017-05-23
* @version 2017-05-24
*/
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
Expand Down Expand Up @@ -177,6 +177,7 @@
'btnRenameAll' : 'リネーム(全て)', // from v2.1.24 added 6.4.2017
'btnPrevious' : '前へ ($1/$2)', // from v2.1.24 added 11.5.2017
'btnNext' : '次へ ($1/$2)', // from v2.1.24 added 11.5.2017
'btnSaveAs' : '別名保存', // from v2.1.25 added 24.5.2017

/******************************** notifications ********************************/
'ntfopen' : 'フォルダーを開いています',
Expand Down

0 comments on commit 37ed059

Please sign in to comment.