mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 18:55:26 +00:00
added ability save files on Ctrl+S in CodeMirror
This commit is contained in:
parent
d51965bd3d
commit
accdd13fde
4 changed files with 272 additions and 261 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
};
|
||||
|
||||
/**
|
||||
* Обьект для работы с кэшем
|
||||
|
|
|
|||
|
|
@ -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() === '<dir>' )
|
||||
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);
|
||||
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() === '<dir>' )
|
||||
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);
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue