From accdd13fde537eee6514086d0e9df35c3cd2c312 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 25 Feb 2013 07:59:05 -0500 Subject: [PATCH] added ability save files on Ctrl+S in CodeMirror --- ChangeLog | 2 + lib/client/dom.js | 86 +++--- lib/client/editor/_codemirror.js | 440 ++++++++++++++++--------------- lib/server/rest.js | 5 +- 4 files changed, 272 insertions(+), 261 deletions(-) diff --git a/ChangeLog b/ChangeLog index 28b942fc..dac26d39 100644 --- a/ChangeLog +++ b/ChangeLog @@ -176,6 +176,8 @@ generation of json of directory listing. * Added ability to upload files. +* Added ability save files on Ctrl+S in CodeMirror. + 2012.12.12, Version 0.1.8 diff --git a/lib/client/dom.js b/lib/client/dom.js index 412f7ac2..9e39beaf 100644 --- a/lib/client/dom.js +++ b/lib/client/dom.js @@ -229,54 +229,52 @@ var CloudCommander, Util, * @param pParams */ DOM.ajax = function(pParams){ - var lType = pParams.type || 'GET', - lData = pParams.data, - lSuccess_f = pParams.success; - - if(!XMLHTTP) - XMLHTTP = new XMLHttpRequest(); - - XMLHTTP.open(lType, pParams.url, true); - XMLHTTP.send(lData); - - if( !Util.isFunction(lSuccess_f) ){ - Util.log('error in DOM.ajax onSuccess:'); - Util.log(pParams); - } - - XMLHTTP.onreadystatechange = function(pEvent){ - if (XMLHTTP.readyState === 4 /* Complete */){ - var lJqXHR = pEvent.target, - lType = XMLHTTP.getResponseHeader('content-type'); - - if (XMLHTTP.status === 200 /* OK */){ - var lData = lJqXHR.response; + var lRet = Util.checkObjTrue(pParams, ['url', 'success']); + if(lRet){ + var p = pParams, + lType = p.type || p.method || 'GET'; + + if(!XMLHTTP) + XMLHTTP = new XMLHttpRequest(); + + XMLHTTP.open(lType, pParams.url, true); + XMLHTTP.send(p.data); + + XMLHTTP.onreadystatechange = function(pEvent){ + if (XMLHTTP.readyState === 4 /* Complete */){ + var lJqXHR = pEvent.target, + lType = XMLHTTP.getResponseHeader('content-type'); - /* If it's json - parse it as json */ - if(lType && Util.isContainStr(lType, 'application/json') ){ - var lResult = Util.tryCatch(function(){ - lData = Util.parseJSON(lJqXHR.response); - }); + if (XMLHTTP.status === 200 /* OK */){ + var lData = lJqXHR.response; - if( Util.log(lResult) ) - lData = lJqXHR.response; + /* If it's json - parse it as json */ + if(lType && Util.isContainStr(lType, 'application/json') ){ + var lResult = Util.tryCatch(function(){ + lData = Util.parseJSON(lJqXHR.response); + }); + + if( Util.log(lResult) ) + lData = lJqXHR.response; + } + if( Util.isFunction(p.success) ) + p.success(lData, lJqXHR.statusText, lJqXHR); + } + /* file not found or connection lost */ + else{ + /* if html given or something like thet + * getBack just status of result + */ + if(lType && lType.indexOf('text/plain') !== 0) + lJqXHR.responseText = lJqXHR.statusText; + + Util.exec(p.error, lJqXHR); } - - lSuccess_f(lData, lJqXHR.statusText, lJqXHR); } - /* file not found or connection lost */ - else{ - /* if html given or something like thet - * getBack just status of result - */ - if(lType && lType.indexOf('text/plain') !== 0) - lJqXHR.responseText = lJqXHR.statusText; - - Util.exec(pParams.error, lJqXHR); - } - } - }; - }; + }; + } + return lRet; + }; /** * Обьект для работы с кэшем diff --git a/lib/client/editor/_codemirror.js b/lib/client/editor/_codemirror.js index 580f9734..3caae0e0 100644 --- a/lib/client/editor/_codemirror.js +++ b/lib/client/editor/_codemirror.js @@ -1,214 +1,226 @@ -var CloudCommander, Util, DOM, CodeMirror; -/* object contains editors CodeMirror */ - (function(CloudCmd, Util, DOM){ - 'use strict'; - - var KeyBinding = CloudCommander.KeyBinding, - CodeMirrorEditor = {}, - FM, - CodeMirrorElement, - CodeMirrorLoaded = false, - /* indicator says CodeMirror still loads */ - Loading = false, - ReadOnly = false, - - CallBacks = [ - hide, - initCodeMirror, - show, - load - ]; - - CloudCmd.Editor = { - get : (function(){ - return this.CodeMirror; - }) - }; - - /* private functions */ - - function setCSS(){ - var lPosition = DOM.getPanel().id, - lRet = DOM.cssSet({ - id : 'editor', - inner : '.CodeMirror{' + - 'font-family' + ': \'Droid Sans Mono\';' + - 'font-size' + ': 15px;' + - '}' + - '.CodeMirror-scroll{' + - 'height' + ':' + CloudCmd.HEIGHT + 'px' + - '}' + - '#CodeMirrorEditor{' + - 'float' + ':' + lPosition + - '}' - }); - - return lRet; - } - - /** - * function initialize CodeMirror - * @param {value, callback} - */ - function initCodeMirror(pParams){ - if(!FM) - FM = DOM.getFM(); - - var lCSS = setCSS(); - - CodeMirrorElement = DOM.anyload({ - name : 'div', - id : 'CodeMirrorEditor', - className : 'panel', - parent : FM - }); - - var lEditor = CodeMirrorEditor.CodeMirror = new CodeMirror(CodeMirrorElement,{ - mode : 'javascript', - value : pParams && pParams.data && pParams.data.data, - theme : 'night', - lineNumbers : true, - //переносим длинные строки - lineWrapping: false, - autofocus : true, - extraKeys: { - //Сохранение - 'Esc': function(){ - Util.exec(pParams); - DOM.remove(lCSS, document.head); - }, - 'Ctrl-S': function(){ - Util.log(lEditor.getValue()); - } - }, - readOnly : ReadOnly - }); - } - - /** - * function loads CodeMirror js and css files - */ - function load(pCallBack){ - console.time('codemirror load'); - var lDir = CloudCmd.LIBDIRCLIENT + 'editor/codemirror/', - lFiles = - [ - [ - lDir + 'codemirror.css', - lDir + 'theme/night.css', - lDir + 'mode/javascript.js', - ], - - lDir + 'codemirror.js' - ]; - - DOM.anyLoadOnLoad(lFiles, function(){ - console.timeEnd('codemirror load'); - CodeMirrorLoaded = true; - Util.exec(pCallBack); - }); - } - - /** - * function shows CodeMirror editor - */ - function show(pCallBack){ - - /* if CodeMirror function show already - * called do not call it again - * if f4 key pressed couple times - */ - if(Loading) - return; - - /* checking is this link is to directory - * when folder view is no need to edit data - */ - if ( DOM.getCurrentSize() === '' ) - ReadOnly = true; - - Loading = true; - - var lFalseLoading = function(){ Loading = false; }; - - setTimeout(lFalseLoading, 400); - /* reading data from current file */ - DOM.getCurrentData({ - error : lFalseLoading, - success : function(data){ - if( DOM.hidePanel() ){ - Util.exec(pCallBack, data); - KeyBinding.unSet(); - } - - DOM.Images.hideLoad(); - lFalseLoading(); - } - }); - } - - /** - * function hides CodeMirror editor - */ - function hide() { - var lElem = CodeMirrorElement; - KeyBinding.set(); - - if(lElem && FM) - FM.removeChild(lElem); - - DOM.showPanel(); - } - - /** - * function calls all CodeMirror editor functions - */ - CodeMirrorEditor.show = function(){ - DOM.Images.showLoad(); - Util.loadOnLoad( CallBacks ); - }; - - /** - * function hides CodeMirror editor - */ - CodeMirrorEditor.hide = hide; - - /** - * function bind keys - */ - CloudCmd.Editor.init = function(pReadOnly){ - ReadOnly = pReadOnly; - - CodeMirrorEditor.show(); - CallBacks.pop(); - - var lKeyListener = function(pEvent){ - /* если клавиши можно обрабатывать */ - if( KeyBinding.get() ){ - /* if f4 or f3 pressed */ - var lF3 = CloudCmd.KEY.F3, - lF4 = CloudCmd.KEY.F4; - - if(!pEvent.shiftKey) - switch(pEvent.keyCode) - { - case lF4: - ReadOnly = false; - CodeMirrorEditor.show(); - break; - case lF3: - ReadOnly = true; - CodeMirrorEditor.show(); - break; - } - } - }; - - /* добавляем обработчик клавишь */ - DOM.addKeyListener( lKeyListener ); - DOM.setButtonKey('f4', CodeMirrorEditor.show); - }; - - CloudCmd.Editor.CodeMirror = CodeMirrorEditor; - -})(CloudCommander, Util, DOM); \ No newline at end of file +var CloudCommander, Util, DOM, CloudFunc, CodeMirror; +/* object contains editors CodeMirror */ + (function(CloudCmd, Util, DOM, CloudFunc){ + 'use strict'; + + var KeyBinding = CloudCommander.KeyBinding, + CodeMirrorEditor = {}, + FM, + CodeMirrorElement, + CodeMirrorLoaded = false, + /* indicator says CodeMirror still loads */ + Loading = false, + ReadOnly = false, + + CallBacks = [ + hide, + initCodeMirror, + show, + load + ]; + + CloudCmd.Editor = { + get : (function(){ + return this.CodeMirror; + }) + }; + + /* private functions */ + + function setCSS(){ + var lPosition = DOM.getPanel().id, + lRet = DOM.cssSet({ + id : 'editor', + inner : '.CodeMirror{' + + 'font-family' + ': \'Droid Sans Mono\';' + + 'font-size' + ': 15px;' + + '}' + + '.CodeMirror-scroll{' + + 'height' + ':' + CloudCmd.HEIGHT + 'px' + + '}' + + '#CodeMirrorEditor{' + + 'float' + ':' + lPosition + + '}' + }); + + return lRet; + } + + /** + * function initialize CodeMirror + * @param {value, callback} + */ + function initCodeMirror(pParams){ + if(!FM) + FM = DOM.getFM(); + + var lCSS = setCSS(); + + CodeMirrorElement = DOM.anyload({ + name : 'div', + id : 'CodeMirrorEditor', + className : 'panel', + parent : FM + }); + + var lEditor = CodeMirrorEditor.CodeMirror = new CodeMirror(CodeMirrorElement,{ + mode : 'javascript', + value : pParams && pParams.data && pParams.data.data, + theme : 'night', + lineNumbers : true, + //переносим длинные строки + lineWrapping: false, + autofocus : true, + extraKeys: { + //Сохранение + 'Esc': function(){ + Util.exec(pParams); + DOM.remove(lCSS, document.head); + }, + 'Ctrl-S': function(){ + CloudCmd.getConfig(function(pConfig){ + var lURL = pConfig && pConfig.api_url + DOM.getCurrentPath(); + + DOM.ajax({ + method : 'put', + url : lURL, + data : lEditor.getValue(), + error : DOM.Images.showError, + success : function(pData){ + Util.log(pData); + } + }); + }); + } + }, + readOnly : ReadOnly + }); + } + + /** + * function loads CodeMirror js and css files + */ + function load(pCallBack){ + console.time('codemirror load'); + var lDir = CloudCmd.LIBDIRCLIENT + 'editor/codemirror/', + lFiles = + [ + [ + lDir + 'codemirror.css', + lDir + 'theme/night.css', + lDir + 'mode/javascript.js', + ], + + lDir + 'codemirror.js' + ]; + + DOM.anyLoadOnLoad(lFiles, function(){ + console.timeEnd('codemirror load'); + CodeMirrorLoaded = true; + Util.exec(pCallBack); + }); + } + + /** + * function shows CodeMirror editor + */ + function show(pCallBack){ + + /* if CodeMirror function show already + * called do not call it again + * if f4 key pressed couple times + */ + if(Loading) + return; + + /* checking is this link is to directory + * when folder view is no need to edit data + */ + if ( DOM.getCurrentSize() === '' ) + ReadOnly = true; + + Loading = true; + + var lFalseLoading = function(){ Loading = false; }; + + setTimeout(lFalseLoading, 400); + /* reading data from current file */ + DOM.getCurrentData({ + error : lFalseLoading, + success : function(data){ + if( DOM.hidePanel() ){ + Util.exec(pCallBack, data); + KeyBinding.unSet(); + } + + DOM.Images.hideLoad(); + lFalseLoading(); + } + }); + } + + /** + * function hides CodeMirror editor + */ + function hide() { + var lElem = CodeMirrorElement; + KeyBinding.set(); + + if(lElem && FM) + FM.removeChild(lElem); + + DOM.showPanel(); + } + + /** + * function calls all CodeMirror editor functions + */ + CodeMirrorEditor.show = function(){ + DOM.Images.showLoad(); + Util.loadOnLoad( CallBacks ); + }; + + /** + * function hides CodeMirror editor + */ + CodeMirrorEditor.hide = hide; + + /** + * function bind keys + */ + CloudCmd.Editor.init = function(pReadOnly){ + ReadOnly = pReadOnly; + + CodeMirrorEditor.show(); + CallBacks.pop(); + + var lKeyListener = function(pEvent){ + /* если клавиши можно обрабатывать */ + if( KeyBinding.get() ){ + /* if f4 or f3 pressed */ + var lF3 = CloudCmd.KEY.F3, + lF4 = CloudCmd.KEY.F4; + + if(!pEvent.shiftKey) + switch(pEvent.keyCode) + { + case lF4: + ReadOnly = false; + CodeMirrorEditor.show(); + break; + case lF3: + ReadOnly = true; + CodeMirrorEditor.show(); + break; + } + } + }; + + /* добавляем обработчик клавишь */ + DOM.addKeyListener( lKeyListener ); + DOM.setButtonKey('f4', CodeMirrorEditor.show); + }; + + CloudCmd.Editor.CodeMirror = CodeMirrorEditor; + +})(CloudCommander, Util, DOM, CloudFunc); \ No newline at end of file diff --git a/lib/server/rest.js b/lib/server/rest.js index c5e8ee8b..c851ba1a 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -107,7 +107,7 @@ if(lRet){ var p = pParams; p.name = Util.removeStr(pParams.name, CloudFunc.FS) || '/'; - + console.log(p.name); switch(p.method){ case 'GET': fs.stat(p.name, function(pError, pStat){ @@ -132,7 +132,6 @@ break; case 'PUT': - console.log(p.request.url); var lQuery = main.getQuery(p.request); p.name = Util.removeStr(p.name, '?dir'); @@ -152,7 +151,7 @@ }); p.request.on('end', function(){ - main.sendResponse(pParams, 'file ' + p.name + ' created'); + main.sendResponse(pParams, 'writed: ' + p.name); }); //p.request.pipe(process.stdout);