From 37ed059a796a725b52e9cab6580dcca9cb043141 Mon Sep 17 00:00:00 2001 From: nao-pon Date: Wed, 24 May 2017 22:13:13 +0900 Subject: [PATCH] [cmd:edit] fix #2020 add "Save As" button --- js/commands/edit.js | 59 +++++++++++++++++++++++++++++++--------- js/elFinder.resources.js | 12 ++++---- js/i18n/elfinder.LANG.js | 1 + js/i18n/elfinder.en.js | 3 +- js/i18n/elfinder.jp.js | 3 +- 5 files changed, 58 insertions(+), 20 deletions(-) diff --git a/js/commands/edit.js b/js/commands/edit.js index c63fafeeb8..dcbdbd12e1 100644 --- a/js/commands/edit.js +++ b/js/commands/edit.js @@ -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'); @@ -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'); }; @@ -100,7 +122,13 @@ elFinder.prototype.commands.edit = function() { cancel : { label : 'btnClose', callback : close - } + }, + buttons : [{ + label : 'btnSaveAs', + callback : function() { + saveAs(); + } + }] }); } else { close(); @@ -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) @@ -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(); }, @@ -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 : { @@ -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); diff --git a/js/elFinder.resources.js b/js/elFinder.resources.js index a79d07f0a2..e95ec99af4 100644 --- a/js/elFinder.resources.js +++ b/js/elFinder.resources.js @@ -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), @@ -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 @@ -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' }, @@ -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 = $('
').append( $('