From b83001028323735a03eeb6fe14965de2bbbf3eec Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 13 Nov 2012 08:42:25 -0500 Subject: [PATCH] added functions DOM.addKeyListener and Util.loadOnLoad --- ChangeLog | 2 + client.js | 14 ++-- lib/client/dom.js | 139 ++++++++++++++++++------------- lib/client/editor/_codemirror.js | 71 +++++++--------- lib/client/ie.js | 24 +++++- lib/client/menu.js | 31 +++---- lib/client/storage/_github.js | 2 +- lib/client/terminal.js | 41 ++++----- lib/client/viewer.js | 53 ++++-------- lib/util.js | 22 +++-- server.js | 2 +- 11 files changed, 205 insertions(+), 196 deletions(-) diff --git a/ChangeLog b/ChangeLog index f0c48918..138ab0c1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -77,6 +77,8 @@ dom.js and util.js. * Fixed bug with navigation thrue path panel. +* Added function DOM.addKeyListener and Util.loadOnLoad + 2012.10.01, Version 0.1.7 diff --git a/client.js b/client.js index 8f322972..77f3c359 100644 --- a/client.js +++ b/client.js @@ -50,7 +50,12 @@ var CloudClient = { */ HEIGHT : 0, MIN_ONE_PANEL_WIDTH : 1155, - OLD_BROWSER : false + OLD_BROWSER : false, + + HOST : (function(){ + var lLocation = document.location; + return lLocation.protocol + '//' + lLocation.host; + })() }; @@ -586,10 +591,9 @@ CloudClient._changeLinks = function(pPanelID){ }while(lTag !== 'LI'); DOM.setCurrentFile(pElement); - }; - - var lLocation = document.location, - lUrl = lLocation.protocol + '//' + lLocation.host; + }, + + lUrl = cloudcmd.HOST; for(var i = 0, n = a.length; i < n ; i++) { diff --git a/lib/client/dom.js b/lib/client/dom.js index d8ae4c79..b58b79ca 100644 --- a/lib/client/dom.js +++ b/lib/client/dom.js @@ -5,6 +5,8 @@ var CloudCommander, $, Util, DOM; DOM = {}; + /* PRIVATE */ + function getCurrentFile(){ return CloudCommander.CURRENT_FILE; } @@ -64,68 +66,89 @@ var CloudCommander, $, Util, DOM; return lRet_b; }; + /** + * safe add event listener + * @param pType + * @param pListener + * @param pUseCapture + */ + DOM.addListener = function(pType, pListener, pUseCapture){ + return document.addEventListener( + pType, + pListener, + pUseCapture || false + ); + }; + + /** + * safe add event keydown listener + * @param pType + * @param pListener + * @param pUseCapture + */ + DOM.addKeyListener = function(pListener, pUseCapture){ + return DOM.addListener('keydown', pListener, pUseCapture); + }; + /* Load file countent thrue ajax */ - DOM.ajax = function(pParams){ + DOM.ajax = function(pParams){ /* if on webkit */ - if(window.XMLHttpRequest){ - if(!XMLHTTP) - XMLHTTP = new XMLHttpRequest(); - - var lMethod = 'GET'; - if(pParams.method) - lMethod = pParams.method; - - XMLHTTP.open(lMethod, pParams.url, true); - XMLHTTP.send(null); - - var lSuccess_f = pParams.success; - if(typeof lSuccess_f !== 'function') - console.log('error in DOM.ajax onSuccess:', pParams); - - XMLHTTP.onreadystatechange = function(pEvent){ - if (XMLHTTP.readyState === 4 /* Complete */){ - var lJqXHR = pEvent.target; - var lContentType = XMLHTTP.getResponseHeader('content-type'); + if(!XMLHTTP) + XMLHTTP = new XMLHttpRequest(); + + var lMethod = 'GET'; + if(pParams.method) + lMethod = pParams.method; + + XMLHTTP.open(lMethod, pParams.url, true); + XMLHTTP.send(null); - if (XMLHTTP.status === 200 /* OK */){ - var lData = lJqXHR.response; - - /* If it's json - parse it as json */ - if(lContentType && - lContentType.indexOf('application/json') === 0){ - try{ - lData = JSON.parse(lJqXHR.response); - } - catch(pError) { - /* if could not parse */ - console.log('Error: could not parse' + - 'json from server from url: ' + - pParams.url); - - lData = lJqXHR.response; - } + var lSuccess_f = pParams.success; + if(typeof lSuccess_f !== 'function') + console.log('error in DOM.ajax onSuccess:', pParams); + + XMLHTTP.onreadystatechange = function(pEvent){ + if (XMLHTTP.readyState === 4 /* Complete */){ + var lJqXHR = pEvent.target; + var lContentType = XMLHTTP.getResponseHeader('content-type'); + + if (XMLHTTP.status === 200 /* OK */){ + var lData = lJqXHR.response; + + /* If it's json - parse it as json */ + if(lContentType && + lContentType.indexOf('application/json') === 0){ + try{ + lData = JSON.parse(lJqXHR.response); + } + catch(pError) { + /* if could not parse */ + console.log('Error: could not parse' + + 'json from server from url: ' + + pParams.url); + + lData = lJqXHR.response; } - - lSuccess_f(lData, lJqXHR.statusText, lJqXHR); - } - else/* file not found or connection lost */{ - var lError_f = pParams.error; - - /* if html given or something like it - * getBack just status of result - */ - if(lContentType && - lContentType.indexOf('text/plain') !== 0){ - lJqXHR.responseText = lJqXHR.statusText; } - - if(typeof lError_f === 'function') - lError_f(lJqXHR); - } + + lSuccess_f(lData, lJqXHR.statusText, lJqXHR); } - }; - } - else $.ajax(pParams); + else/* file not found or connection lost */{ + var lError_f = pParams.error; + + /* if html given or something like it + * getBack just status of result + */ + if(lContentType && + lContentType.indexOf('text/plain') !== 0){ + lJqXHR.responseText = lJqXHR.statusText; + } + + if(typeof lError_f === 'function') + lError_f(lJqXHR); + } + } + }; }; /* @@ -384,8 +407,8 @@ var CloudCommander, $, Util, DOM; }); }; - DOM.socketLoad = function(){ - DOM.jsload('lib/client/socket.js'); + DOM.socketLoad = function(pCallBack){ + DOM.jsload('lib/client/socket.js', pCallBack); }; /* DOM */ diff --git a/lib/client/editor/_codemirror.js b/lib/client/editor/_codemirror.js index f4711e1d..ce04852d 100644 --- a/lib/client/editor/_codemirror.js +++ b/lib/client/editor/_codemirror.js @@ -1,7 +1,5 @@ var CloudCommander, Util, DOM, CloudFunc, CodeMirror; -/* object contains editors CodeMirror - * and later will be Ace - */ +/* object contains editors CodeMirror */ (function(){ "use strict"; var cloudcmd = CloudCommander, @@ -27,8 +25,12 @@ var CloudCommander, Util, DOM, CloudFunc, CodeMirror; /** * function initialize CodeMirror + * @param {value, callback} */ - function initCodeMirror(pValue){ + function initCodeMirror(pData){ + if(!pData) + pData = {}; + if(!FM) FM = DOM.getById('fm'); @@ -41,15 +43,17 @@ var CloudCommander, Util, DOM, CloudFunc, CodeMirror; CodeMirrorEditor.CodeMirror = new CodeMirror(CodeMirrorElement,{ mode : 'javascript', - value : pValue, + value : pData.data, theme : 'night', lineNumbers : true, - //переносим длинные строки + //переносим длинные строки lineWrapping: false, autofocus : true, extraKeys: { //Сохранение - "Esc": CodeMirrorEditor.hide + 'Esc': function(){ + Util.exec(pData.callback); + } }, readOnly : ReadOnly }); @@ -58,7 +62,7 @@ var CloudCommander, Util, DOM, CloudFunc, CodeMirror; /** * function loads CodeMirror js and css files */ - function load(){ + function load(pCallBack){ console.time('codemirror load'); var lDir = CodeMirrorEditor.dir; @@ -88,8 +92,8 @@ var CloudCommander, Util, DOM, CloudFunc, CodeMirror; function(){ console.timeEnd('codemirror load'); - CodeMirrorLoaded = true; - CodeMirrorEditor.show(); + CodeMirrorLoaded = true; + Util.exec(pCallBack); } ); } @@ -97,13 +101,7 @@ var CloudCommander, Util, DOM, CloudFunc, CodeMirror; /** * function shows CodeMirror editor */ - CodeMirrorEditor.show = function(pReadOnly){ - if( Util.isBoolean(pReadOnly) ) - ReadOnly = pReadOnly; - - /* if CodeMirrorEditor is not loaded - loading him */ - if(!CodeMirrorLoaded) - return load(); + CodeMirrorEditor.show = function(pCallBack){ /* if CodeMirror function show already * called do not call it again @@ -118,7 +116,7 @@ var CloudCommander, Util, DOM, CloudFunc, CodeMirror; lA = lA.href; /* убираем адрес хоста*/ - lA = '/' + lA.replace(document.location.href,''); + lA = lA.replace(cloudcmd.HOST, ''); /* checking is this link is to directory */ var lSize = DOM.getByClass('size', lCurrentFile); @@ -155,17 +153,17 @@ var CloudCommander, Util, DOM, CloudFunc, CodeMirror; return DOM.Images.showError(jqXHR); }, - success:function(data, textStatus, jqXHR){ + success:function(data, textStatus, jqXHR){ /* if we got json - show it */ if(typeof data === 'object') data = JSON.stringify(data, null, 4); var lHided = DOM.hidePanel(); if(lHided){ - initCodeMirror(data); + Util.exec(pCallBack, data); /* removing keyBinding if set */ - KeyBinding.unSet(); + KeyBinding.unSet(); } DOM.Images.hideLoad(); @@ -194,44 +192,39 @@ var CloudCommander, Util, DOM, CloudFunc, CodeMirror; cloudcmd.Editor.Keys = function(pReadOnly){ ReadOnly = pReadOnly; - /* loading js and css of CodeMirror */ - CodeMirrorEditor.show(); + var lShowCodemirror = [ + CodeMirrorEditor.hide, + initCodeMirror, + CodeMirrorEditor.show, + load + ]; + Util.loadOnLoad( lShowCodemirror ); + lShowCodemirror.pop(); - var key_event = function(pEvent){ - + var lKeyListener = function(pEvent){ /* если клавиши можно обрабатывать */ if( KeyBinding.get() ){ /* if f4 or f3 pressed */ var lF3 = cloudcmd.KEY.F3, - lF4 = cloudcmd.KEY.F4, - lShow = Util.bind( CodeMirrorEditor.show, CodeMirrorEditor ); + lF4 = cloudcmd.KEY.F4; if(!pEvent.shiftKey) switch(pEvent.keyCode) { case lF4: ReadOnly = false; - lShow(); + Util.loadOnLoad( lShowCodemirror ); break; case lF3: ReadOnly = true; - lShow(); + Util.loadOnLoad( lShowCodemirror ); break; } } }; /* добавляем обработчик клавишь */ - if (document.addEventListener) - document.addEventListener('keydown', key_event, false); - - else{ - var lFunc = document.onkeydown; - document.onkeydown = function(){ - Util.exec(lFunc); - key_event(); - }; - } + DOM.addKeyListener(lKeyListener); }; cloudcmd.Editor.CodeMirror = CodeMirrorEditor; diff --git a/lib/client/ie.js b/lib/client/ie.js index 8d956b5b..e64f715a 100644 --- a/lib/client/ie.js +++ b/lib/client/ie.js @@ -1,5 +1,5 @@ /* script, fixes ie */ -var CloudCommander, DOM, $; +var Util, DOM, $; (function(){ "use strict"; @@ -26,12 +26,28 @@ var CloudCommander, DOM, $; .appendTo(pParams_o.parent || document.head); }; } - + /* setting function context (this) */ DOM.bind = function(pFunction, pContext){ return $.proxy(pFunction, pContext); }; - + + if (!document.addEventListener) + /** + * safe add event listener on ie + * @param pType + * @param pListener + */ + DOM.addListener = function(pType, pListener){ + var lType = 'on' + pType, + lFunc = document[lType]; + + document[lType] = function(){ + Util.exec(lFunc); + pListener(); + }; + }; + if(!document.getElementsByClassName){ DOM.getByClass = function(pClass, pElement){ var lClass = '.' + pClass, @@ -123,4 +139,6 @@ var CloudCommander, DOM, $; }; } + if(!window.XMLHttpRequest) + DOM.ajax = $.ajax; })(); \ No newline at end of file diff --git a/lib/client/menu.js b/lib/client/menu.js index e285c92e..3f13f8df 100644 --- a/lib/client/menu.js +++ b/lib/client/menu.js @@ -101,10 +101,9 @@ var CloudCommander, Util, DOM, CloudFunc, $; } /** function loads css and js of Menu - * @param pParent - this - * @param pPosition - position of menu + * @param pCallBack */ - function load(){ + function load(pCallBack){ console.time('menu load'); var lDir = Menu.dir; @@ -114,7 +113,7 @@ var CloudCommander, Util, DOM, CloudFunc, $; lDir + 'contextMenu.css'], function(){ console.timeEnd('menu load'); - Menu.show(); + Util.exec(pCallBack); }); } @@ -201,11 +200,16 @@ var CloudCommander, Util, DOM, CloudFunc, $; Menu.Keys = function(pPosition){ Position = pPosition; - var lFunc = document.oncontextmenu; + DOM.jqueryLoad(function(){ + Util.loadOnLoad([Menu.show, load]); + }); + + var lFunc = document.oncontextmenu; document.oncontextmenu = function(){ Util.exec(lFunc); return Menu.ENABLED; }; + var key_event = (function(pEvent){ /* если клавиши можно обрабатывать */ @@ -215,6 +219,7 @@ var CloudCommander, Util, DOM, CloudFunc, $; var lCurrent = DOM.getCurrentFile(); if(lCurrent) $(lCurrent).contextMenu(); + pEvent.preventDefault(); } } @@ -223,21 +228,7 @@ var CloudCommander, Util, DOM, CloudFunc, $; }); /* добавляем обработчик клавишь */ - if (document.addEventListener) - document.addEventListener('keydown', key_event.bind(this),false); - - else{ - lFunc = document.onkeydown; - document.onkeydown = function(){ - Util.exec(lFunc); - key_event(); - }; - - /* showing context menu preview*/ - Menu.show(); - } - - DOM.jqueryLoad( load ); + DOM.addKeyListener( key_event ); }; cloudcmd.Menu = Menu; diff --git a/lib/client/storage/_github.js b/lib/client/storage/_github.js index 325b9be2..7bd9e6dc 100644 --- a/lib/client/storage/_github.js +++ b/lib/client/storage/_github.js @@ -93,7 +93,7 @@ var CloudCommander, Util, DOM, $, Github; cloudcmd.Storage.Keys = function(){ DOM.jqueryLoad(function(){ - Util.loadOnLoad([ init, load, setConfig ]); + Util.loadOnLoad([ init, setConfig, load ]); }); }; diff --git a/lib/client/terminal.js b/lib/client/terminal.js index 8a0f15d4..d91bcf43 100644 --- a/lib/client/terminal.js +++ b/lib/client/terminal.js @@ -1,4 +1,4 @@ -var CloudCommander, DOM, $; +var CloudCommander, Util, DOM, $; /* object contains terminal jqconsole */ (function(){ @@ -19,16 +19,12 @@ var CloudCommander, DOM, $; /** * function loads jquery-terminal */ - function load(){ + function load(pCallBack){ console.time('terminal load'); var lDir = 'lib/client/terminal/jquery-terminal/jquery.'; + DOM.cssLoad(lDir + 'terminal.css'); - DOM.cssLoad(lDir + 'terminal.css'); - - DOM.socketLoad(); - - DOM.anyLoadOnLoad([ lDir + 'terminal.js', lDir + 'mousewheel.js'], @@ -52,7 +48,7 @@ var CloudCommander, DOM, $; $(window).unbind('resize'); - JqueryTerminal.show(); + Util.exec(pCallBack); }); } @@ -85,9 +81,7 @@ var CloudCommander, DOM, $; if( DOM.hidePanel() ){ Hidden = false; DOM.show(TerminalId); - KeyBinding.unSet(); - Term.resume(); } }; @@ -111,12 +105,18 @@ var CloudCommander, DOM, $; */ cloudcmd.Terminal.Keys = function(){ /* loading js and css*/ - DOM.jqueryLoad( load ); - - var key_event = function(event){ + DOM.jqueryLoad(function(){ + Util.loadOnLoad([ + JqueryTerminal.show, + load, + DOM.socketLoad + ]); + }); + + /* добавляем обработчик клавишь */ + DOM.addKeyListener(function(){ /* если клавиши можно обрабатывать */ - if(Hidden && - KeyBinding.get() && + if(Hidden && KeyBinding.get() && event.keyCode === cloudcmd.KEY.TRA){ JqueryTerminal.show(); event.preventDefault(); @@ -124,16 +124,7 @@ var CloudCommander, DOM, $; else if(!Hidden && event.keyCode === cloudcmd.KEY.ESC) JqueryTerminal.hide(); - - - }; - - /* добавляем обработчик клавишь */ - if (document.addEventListener) - document.addEventListener('keydown', key_event, false); - - else - document.onkeypress = key_event; + }); }; cloudcmd.Terminal.JqueryTerminal = JqueryTerminal; diff --git a/lib/client/viewer.js b/lib/client/viewer.js index 2b54c23b..79cb91f7 100644 --- a/lib/client/viewer.js +++ b/lib/client/viewer.js @@ -141,7 +141,7 @@ var CloudCommander, Util, DOM, CloudFunc, $; var lLink = pA.href; /* убираем адрес хоста */ - lLink = '/' + lLink.replace(document.location.href,''); + lLink = lLink.replace(cloudcmd.HOST, ''); if (lLink.indexOf(CloudFunc.NOJS) === CloudFunc.FS.length) lLink = lLink.replace(CloudFunc.NOJS, ''); @@ -176,6 +176,7 @@ var CloudCommander, Util, DOM, CloudFunc, $; * function shows FancyBox */ FancyBox.show = function(pCallBack){ + DOM.Images.hideLoad(); set(); var lConfig = FancyBox.getConfig(), @@ -196,43 +197,21 @@ var CloudCommander, Util, DOM, CloudFunc, $; }; cloudcmd.Viewer.Keys = function(){ - var lCallBack_f = function(){ - var lF3 = cloudcmd.KEY.F3, - key_event = function(pEvent){ - /* если клавиши можно обрабатывать */ - if( KeyBinding.get() ) - if(pEvent.keyCode === lF3 && pEvent.shiftKey){ - var lCurrentFile = DOM.getCurrentFile(); - FancyBox.show(lCurrentFile); - - pEvent.preventDefault(); - } - }; - - /* добавляем обработчик клавишь */ - if (document.addEventListener) - document.addEventListener('keydown', key_event, false); - - else{ - var lFunc; - if(typeof document.onkeydown === 'function') - lFunc = document.onkeydown; - - document.onkeydown = function(){ - if(lFunc) - lFunc(); - - key_event(); - }; - } - - /* showing images preview*/ - FancyBox.show(); - DOM.Images.hideLoad(); - }; - DOM.jqueryLoad(function(){ - FancyBox.load(lCallBack_f); + Util.loadOnLoad([FancyBox.show, FancyBox.load]); + }); + + /* добавляем обработчик клавишь */ + DOM.addKeyListener( function(){ + var lF3 = cloudcmd.KEY.F3; + + /* если клавиши можно обрабатывать */ + if( KeyBinding.get() && + event.keyCode === lF3 && + event.shiftKey ){ + FancyBox.show( DOM.getCurrentFile() ); + event.preventDefault(); + } }); }; diff --git a/lib/util.js b/lib/util.js index ede2fd25..27d40745 100644 --- a/lib/util.js +++ b/lib/util.js @@ -58,16 +58,24 @@ var Util, exports; return lRet; }; - Util.loadOnLoad = function(pFunc_a){ - if( Util.isArray(pFunc_a) ) { - var lFunc = pFunc_a.pop(); + Util.loadOnLoad = function(pFunc_a, pData){ + if( Util.isArray(pFunc_a) && pFunc_a.length) { + var lFunc_a = [].concat(pFunc_a), + lFunc = lFunc_a.pop(), + lCallBack = function(pData){ + return Util.loadOnLoad(lFunc_a, pData); + }; - Util.exec(lFunc, function(){ - return Util.loadOnLoad(pFunc_a); - }); + if(pData) + pData = { + data : pData, + callback : lCallBack + }; + + Util.exec(lFunc , pData || lCallBack); } else - return Util.exec(pFunc_a); + return Util.exec(lFunc_a); }; /** diff --git a/server.js b/server.js index 17095c61..e6d13a03 100644 --- a/server.js +++ b/server.js @@ -379,7 +379,7 @@ CloudServer._controller = function(pReq, pRes) }, isAllowd_b = (lCheck_f('js') && lMin_o.js) || - (lCheck_f('css') && lMin_o.css) || + (lCheck_f('css') && lMin_o.css) || (lCheck_f('html') && lMin_o.html); if(isAllowd_b){