From 88713a9643d97c4ff770cebbd838f0fb9175b1fa Mon Sep 17 00:00:00 2001 From: coderaiser Date: Sun, 11 May 2014 15:14:12 -0400 Subject: [PATCH] feature(config) mv apiURL to CloudFunc --- HELP.md | 3 +- lib/client.js | 12 ++++++- lib/client/rest.js | 64 ++++++++++++++++------------------- lib/client/storage/_github.js | 31 ++++++++--------- lib/cloudfunc.js | 1 + lib/server/rest.js | 2 +- 6 files changed, 59 insertions(+), 54 deletions(-) diff --git a/HELP.md b/HELP.md index 64e5cf1f..60e827c6 100644 --- a/HELP.md +++ b/HELP.md @@ -170,9 +170,8 @@ All main configuration could be done via `json/config.json`. "auth" : false, /* enable http authentication */ "username" : "root", /* username for authentication */ "password" : "toor", /* password hash in sha-1 for authentication*/ - "apiURL" : "/api/v1", "appCache" : false, /* cache files for offline use */ - "analytics" : true, /* google analytics support */ + "analytics" : true, /* google analytics support */ "diff" : true, /* when save - send patch, not whole file */ "zip" : true, /* zip text before send / unzip before save */ "notifications" : false, /* show notifications when tab is not active*/ diff --git a/lib/client.js b/lib/client.js index e0b621cd..f234ae6c 100644 --- a/lib/client.js +++ b/lib/client.js @@ -318,7 +318,17 @@ var Util, DOM, CloudFunc; } this.setConfig = function(config) { Config = config; }; - this.getConfig = getSystemFile(Config, '/api/v1/config'); + this.getConfig = function(callback) { + Util.ifExec(Config, callback, function() { + var RESTful = DOM.RESTful; + + RESTful.Config.read(function(config) { + Config = config; + callback(config); + }); + }); + }; + this.getModules = getSystemFile(Modules, DIR_JSON + 'modules.json'); this.getExt = getSystemFile(Extensions, DIR_JSON + 'ext.json'); this.getFileTemplate = getSystemFile(FileTemplate, DIR_HTML_FS + 'file.html'); diff --git a/lib/client/rest.js b/lib/client/rest.js index 7b1269cf..aa499f64 100644 --- a/lib/client/rest.js +++ b/lib/client/rest.js @@ -127,41 +127,37 @@ var Util, DOM, CloudFunc, CloudCmd; }; function sendRequest(params) { - var p = params; - - Images.showLoad(p.imgPosition); + var p = params, + apiURL = CloudFunc.apiURL, + data, + isString = Util.isString(p.data), + isArrayBuffer = Util.isArrayBuffer(p.data), + isFile = Util.isFile(p.data); - CloudCmd.getConfig(function(config) { - var data, - isString = Util.isString(p.data), - isArrayBuffer = Util.isArrayBuffer(p.data), - isFile = Util.isFile(p.data); - - if (Util.isString(p.url)) - p.url = decodeURI(p.url); - - if (p.data && !isString && !isArrayBuffer && !isFile) - data = Util.stringifyJSON(p.data); - else - data = p.data; - - p.url = config && config.apiURL + p.url; - - DOM.ajax({ - method : p.method, - url : p.url, - data : data, - error : Images.showError, - success : function(data) { - Images.hide(); - - if (!p.doNotLog) - Util.log(data); - - Util.exec(p.callback, data); - } - }); - }); + if (Util.isString(p.url)) + p.url = decodeURI(p.url); + + if (p.data && !isString && !isArrayBuffer && !isFile) + data = Util.stringifyJSON(p.data); + else + data = p.data; + + p.url = apiURL + p.url; + + DOM.ajax({ + method : p.method, + url : p.url, + data : data, + error : Images.showError, + success : function(data) { + Images.hide(); + + if (!p.doNotLog) + Util.log(data); + + Util.exec(p.callback, data); + } + }); } } })(Util, DOM, CloudFunc); diff --git a/lib/client/storage/_github.js b/lib/client/storage/_github.js index 32f088b6..d6aecf31 100644 --- a/lib/client/storage/_github.js +++ b/lib/client/storage/_github.js @@ -54,6 +54,7 @@ var CloudCmd, Util, DOM, CloudFunc, Github, cb; GitHub.autorize = function(callback, code) { Storage.get('token', function(token) { var isContain, + apiURL = CloudFunc.apiURL, URL = '//' + window.location.host + '/auth/github'; if (token) { @@ -68,22 +69,20 @@ var CloudCmd, Util, DOM, CloudFunc, Github, cb; if (!isContain) DOM.openWindow(URL); else - CloudCmd.getConfig(function(config) { - DOM.ajax({ - type : 'put', - url : config && config.apiURL + '/auth', - data : Util.rmStr(code, '?code='), - success : function(data) { - if (data && data.token) { - token = data.token; - - GitHub.Login(token); - Storage.set('token', token); - Util.exec(callback); - } else - Util.log('Worning: token not getted...'); - } - }); + DOM.ajax({ + type : 'put', + url : apiURL + '/auth', + data : Util.rmStr(code, '?code='), + success : function(data) { + if (data && data.token) { + token = data.token; + + GitHub.Login(token); + Storage.set('token', token); + Util.exec(callback); + } else + Util.log('Worning: token not getted...'); + } }); } }); diff --git a/lib/cloudfunc.js b/lib/cloudfunc.js index b2e43e04..5711930a 100644 --- a/lib/cloudfunc.js +++ b/lib/cloudfunc.js @@ -41,6 +41,7 @@ var Util; /* название css-класа кнопки обновления файловой структуры*/ this.REFRESHICON = 'refresh-icon'; + this.apiURL = '/api/v1'; /* id панелей с файлами */ this.PANEL_LEFT = 'js-left'; this.PANEL_RIGHT = 'js-right'; diff --git a/lib/server/rest.js b/lib/server/rest.js index 7f8502c1..397e7640 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -75,7 +75,7 @@ var apiURL, name, ret; if (request && response) { - apiURL = main.config.apiURL; + apiURL = CloudFunc.apiURL; name = main.getPathName(request); ret = Util.isContainStr(name, apiURL);