From 83e2ba042adfe02c1589e48b3ea3c45c9fc4f4df Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 2 Oct 2014 04:42:26 -0400 Subject: [PATCH] feature(edit) add setValue, setMode, setModeForPath --- lib/client/edit.js | 98 +++++++++++++++++++++++++++++----------------- 1 file changed, 63 insertions(+), 35 deletions(-) diff --git a/lib/client/edit.js b/lib/client/edit.js index f5f6c7c3..9a336d38 100644 --- a/lib/client/edit.js +++ b/lib/client/edit.js @@ -4,7 +4,7 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch, Zip, MenuI CloudCmd.Edit = EditProto; - function EditProto() { + function EditProto(callback) { var Name = 'Edit', Loading = false, DIR = CloudCmd.LIBDIRCLIENT + 'edit/', @@ -41,19 +41,17 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch, Zip, MenuI } }; - function init() { + function init(callback) { Loading = true; Util.exec.series([ CloudCmd.View, load, - Edit.show + Edit.show.bind(null, callback) ]); } - this.show = function() { - var mode, htmlMode, jsMode, isHTML, isJS, modesByName, - name = Info.name, - isDir = Info.isDir; + this.show = function(callback) { + var func = Util.exec.ret(callback); if (!Loading) { Images.showLoad(); @@ -78,40 +76,26 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch, Zip, MenuI }); } - modesByName = Modelist.modesByName; - - if (isDir) - mode = modesByName.json.mode; - else - mode = Modelist.getModeForPath(name).mode; - - htmlMode = modesByName.html.mode; - jsMode = modesByName.javascript.mode; - - isHTML = mode === htmlMode; - isJS = mode === jsMode; - - Session.setMode(mode); - setUseOfWorker(mode); - setEmmet(isHTML); - - Info.getData(function(error, data) { - var UndoManager = ace.require('ace/undomanager').UndoManager; + getData(function(error, data) { + Edit.setValue(data); - Value = data; + ConfigView.beforeShow = func; - if (isJS && Session.getUseWorker()) - setJsHintConfig(); - - Ace.setValue(Value); CloudCmd.View.show(Element, ConfigView); - - Session.setUndoManager(new UndoManager()); }); } }; - this.hide = function() { + this.setValue = function(value) { + var UndoManager = ace.require('ace/undomanager').UndoManager; + + Value = value; + + Ace.setValue(value); + Session.setUndoManager(new UndoManager()); + }; + + this.hide = function() { CloudCmd.View.hide(); }; @@ -127,6 +111,50 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch, Zip, MenuI Ace.gotoLine(number); }; + function getData(callback) { + var name = Info.name, + isDir = Info.isDir; + + Util.checkArgs(arguments, ['callback']); + + if (isDir) + Edit.setMode('json'); + else + Edit.setModeForPath(name); + + Info.getData(function(error, data) { + callback(error, data); + }); + } + + this.setModeForPath = function(name) { + var modesByName = Modelist.modesByName, + mode = Modelist.getModeForPath(name).mode, + + htmlMode = modesByName.html.mode, + jsMode = modesByName.javascript.mode, + + isHTML = mode === htmlMode, + isJS = mode === jsMode; + + Session.setMode(mode); + setUseOfWorker(mode); + setEmmet(isHTML); + + if (isJS && Session.getUseWorker()) + setJsHintConfig(); + }; + + this.setMode = function(mode) { + var ext, + modesByName = Modelist.modesByName; + + if (modesByName[mode]) { + ext = modesByName[mode].extensions.split('|')[0]; + Edit.setModeForPath('.' + ext); + } + }; + function isChanged() { var is, value = Ace.getValue(), @@ -489,7 +517,7 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch, Zip, MenuI }, HIDE_TIME); }; - init(); + init(callback); } })(CloudCmd, Util, DOM, CloudFunc);