From ebeffdc16e75f0a911bedf5f135518bda255b651 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 28 Jan 2013 08:27:54 -0500 Subject: [PATCH] refactored --- html/auth/github.html | 13 +++--- json/config.json | 8 ---- json/modules.json | 21 +++++++-- lib/client.js | 46 ++++++++------------ lib/client/menu.js | 13 +----- lib/client/storage/_dropbox.js | 8 +++- lib/client/storage/_github.js | 25 ++++------- lib/util.js | 78 ++++++++++++++++++++++++++++++++-- 8 files changed, 133 insertions(+), 79 deletions(-) diff --git a/html/auth/github.html b/html/auth/github.html index 7ba927d0..5d202d2b 100644 --- a/html/auth/github.html +++ b/html/auth/github.html @@ -3,22 +3,25 @@ \ No newline at end of file diff --git a/json/config.json b/json/config.json index d68b757e..6eb996bf 100644 --- a/json/config.json +++ b/json/config.json @@ -8,14 +8,6 @@ "html" : true, "img" : true }, - "github":{ - "key" : "891c251b925e4e967fa9", - "secret" : "afe9bed1e810c5dc44c4c2a953fc6efb1e5b0545" - }, - "dropbox_key" : "0nd3ssnp5fp7tqs", - "dropbox_secret" : "r61lxpchmk8l06o", - "dropbox_encoded_key" : "DkMz4FYHQTA=|GW6pf2dONkrGvckMwBsl1V1vysrCPktPiUWN7UpDjw==", - "dropbox_chooser_key" : "o7d6llji052vijk", "gdrive_id" : "255175681917", "vk_id" : "3336188", "logs" : false, diff --git a/json/modules.json b/json/modules.json index 026bf9d7..2275b95b 100644 --- a/json/modules.json +++ b/json/modules.json @@ -2,10 +2,23 @@ "editor/_codemirror", "menu", "viewer", - "terminal",{ - "storage":[ - "DropBox", - "GitHub", + "terminal", { + "name": "storage", + "data": [{ + "name": "DropBox", + "data":{ + "key" : "0nd3ssnp5fp7tqs", + "secret" : "r61lxpchmk8l06o", + "encodedKey" : "DkMz4FYHQTA=|GW6pf2dONkrGvckMwBsl1V1vysrCPktPiUWN7UpDjw==", + "chooserKey" : "o7d6llji052vijk" + } + }, { + "name": "GitHub", + "data": { + "key" : "891c251b925e4e967fa9", + "secret" : "afe9bed1e810c5dc44c4c2a953fc6efb1e5b0545" + } + }, "GDrive", "VK", "SkyDrive" diff --git a/lib/client.js b/lib/client.js index ad97296e..be63e052 100644 --- a/lib/client.js +++ b/lib/client.js @@ -230,19 +230,15 @@ function initModules(pCallBack){ CloudCmd.getModules(function(pModules){ pModules = pModules || []; + DOM.addListener('contextmenu', function(pEvent){ + CloudCmd.Menu.ENABLED || DOM.preventDefault(pEvent); + }, document); + var lStorage = 'storage', lShowLoadFunc = Util.retFunc( DOM.Images.showLoad ), - lDisableMenuFunc = function(){ - var lFunc = document.oncontextmenu; - document.oncontextmenu = function(){ - Util.exec(lFunc); - return CloudCmd.Menu.ENABLED || false; - }; - }, lDoBefore = { 'editor/_codemirror' : lShowLoadFunc, - 'menu' : lDisableMenuFunc, 'viewer' : lShowLoadFunc }, @@ -254,34 +250,26 @@ function initModules(pCallBack){ }); }; - lDisableMenuFunc(); - - - for(var i = 0, n = pModules.length; i < n ; i++){ var lModule = pModules[i]; - if(Util.isObject(lModule)){ - for(var lProp in lModule) - if(lProp === lStorage){ - var m = lModule[lProp].length, - lMod = lModule[lProp]; - - for(var j = 0; j < m; j++){ - var lName = lMod[j], - lPath = lStorage + '/_' + lName.toLowerCase(); - - lLoad(lName, lPath); - } - - break; - } - } - else + if(Util.isString(lModule)) lLoad(null, lModule, lDoBefore[lModule]); } + var lStorageObj = Util.findObjByNameInArr( pModules, lStorage ), + lMod = Util.getNamesFromObjArray( lStorageObj.data ); + + for(i = 0, n = lMod.length; i < n; i++){ + var lName = lMod[i], + lPath = lStorage + '/_' + lName.toLowerCase(); + + lLoad(lName, lPath); + } + + Util.exec(pCallBack); + }); } diff --git a/lib/client/menu.js b/lib/client/menu.js index c4821f0d..11ba9dee 100644 --- a/lib/client/menu.js +++ b/lib/client/menu.js @@ -40,17 +40,8 @@ var CloudCommander, Util, DOM, $; */ function setUploadToItemNames(pCallBack){ CloudCmd.getModules(function(pModules){ - var lStorage = 'storage', - lItems = pModules[lStorage]; - - for(var i = 0, n = pModules.length; i < n; i++ ){ - lItems = pModules[i][lStorage]; - - if(lItems) - break; - } - - UploadToItemNames = lItems || []; + var lStorageObj = Util.findObjByNameInArr( pModules, 'storage' ); + UploadToItemNames = Util.getNamesFromObjArray( lStorageObj.data ) || []; Util.exec(pCallBack); }); diff --git a/lib/client/storage/_dropbox.js b/lib/client/storage/_dropbox.js index cc3fec7e..001a1759 100644 --- a/lib/client/storage/_dropbox.js +++ b/lib/client/storage/_dropbox.js @@ -49,9 +49,13 @@ var CloudCommander, Util, DOM, Dropbox, cb, Client; * @param pData = {key, secret} */ DropBoxStore.login = function(pCallBack){ - CloudCmd.getConfig(function(pConfig){ + CloudCmd.getModules(function(pModules){ + var lStorage = Util.findObjByNameInArr(pModules, 'storage'), + lDropBox = Util.findObjByNameInArr(lStorage.data, 'DropBox'), + lDropBoxKey = lDropBox && lDropBox.data.encodedKey; + Client = new Dropbox.Client({ - key: pConfig.dropbox_encoded_key + key: lDropBoxKey }); //Client.authDriver(new Dropbox.Drivers.Redirect({rememberUser: true})); diff --git a/lib/client/storage/_github.js b/lib/client/storage/_github.js index 8a3ab44a..0efd0e21 100644 --- a/lib/client/storage/_github.js +++ b/lib/client/storage/_github.js @@ -5,9 +5,6 @@ var CloudCommander, Util, DOM, $, Github, cb; "use strict"; var Cache = DOM.Cache, - - APIURL, - AuthURL, GithubLocal, User, GitHubStore = {}; @@ -73,19 +70,15 @@ var CloudCommander, Util, DOM, $, Github, cb; } }; - GitHubStore.getUserData = function(pCallBack){ - var lName, - - lShowUserInfo = function(pError, pData){ - if(!pError){ - lName = pData.name; - Util.log('Hello ' + lName + ' :)!'); - } - else - DOM.Cache.remove('token'); - }; - - User.show(null, lShowUserInfo); + GitHubStore.getUserData = function(pCallBack){ + User.show(null, function(pError, pData){ + if(!pError){ + var lName = pData.name; + Util.log('Hello ' + lName + ' :)!'); + } + else + DOM.Cache.remove('token'); + }); Util.exec(pCallBack); }; diff --git a/lib/util.js b/lib/util.js index e0bf8bf0..ade393c6 100644 --- a/lib/util.js +++ b/lib/util.js @@ -119,12 +119,30 @@ Util = exports || {}; * @param pArg */ Util.log = function(pArg){ - var lArg = pArg, - lConsole = Scope.console, + var lConsole = Scope.console, lDate = '[' + Util.getDate() + '] '; if(lConsole && pArg) - lConsole.log(lDate, lArg); + lConsole.log(lDate, pArg); + + return pArg; + }; + + /** + * function log pArg if it's not empty + * @param pArg + */ + Util.logError = function(pArg){ + var lConsole = Scope.console, + lDate = '[' + Util.getDate() + '] '; + + if(lConsole && pArg){ + var lMsg = pArg.message; + if( lMsg ) + lDate += pArg.message + ' '; + + lConsole.error(lDate, pArg); + } return pArg; }; @@ -451,7 +469,7 @@ Util = exports || {}; lRet = Util.tryCatch(pTryFunc); - return Util.log(lRet); + return Util.logError(lRet); }; /** @@ -520,6 +538,58 @@ Util = exports || {}; return lRet; }; + /** + * get Object Property Names + * @pObj + */ + Util.getObjPropNames = function(pObj){ + var lRet = [], + i = 0; + + for(var lName in pObj) + lRet[i++] = lName; + + return lRet; + }; + + /** + * get values from Object Array name properties + * or + * @pObj + */ + Util.getNamesFromObjArray = function(pArr){ + var lRet = []; + + if(pArr) + for(var i = 0, n = pArr.length; i < n; i++) + lRet[i] = pArr[i].name || pArr[i]; + + return lRet; + }; + + /** + * find object by name in arrray + * or + * @pObj + */ + Util.findObjByNameInArr = function(pArr, pObjName){ + var lRet, + lSearch; + + if(pArr){ + for(var i = 0, n = pArr.length; i < n; i++ ){ + lSearch = pArr[i].name; + + if(lSearch === pObjName) + break; + } + + lRet = pArr[i]; + } + + return lRet; + }; + /** * Gets current time in format hh:mm:ss */