diff --git a/lib/client.js b/lib/client.js index e2ff0784..c35b1738 100644 --- a/lib/client.js +++ b/lib/client.js @@ -267,9 +267,13 @@ var Util, DOM, CloudFunc; dirPath = CloudFunc.rmLastSlash(dirPath) || '/'; - if (!Storage.get(dirPath)) - Storage.set(dirPath, getJSONfromFileTable()); + Storage.get(dirPath, function(data) { + if (!data) { + data = getJSONfromFileTable(); + Storage.set(dirPath, data); + } }); + }); Util.exec(CloudCmd.Key); Util.exec(pCallBack); @@ -336,9 +340,7 @@ var Util, DOM, CloudFunc; * { refresh, nohistory } - необходимость обновить данные о каталоге */ this.ajaxLoad = function(path, options) { - var json, str, - ret = options && options.refresh, - SLASH = '/', + var SLASH = '/', dirPath = DOM.getCurrentDirPath(), fsPath = decodeURI(path), noJSONPath = Util.removeStr(fsPath, '?json' ), @@ -372,37 +374,24 @@ var Util, DOM, CloudFunc; * если стоит поле обязательной перезагрузки - * перезагружаемся */ - if (!ret) { - str = Storage.get(cleanPath); + Storage.get(cleanPath, function(json) { + var ret = options && options.refresh; - if (!str) - ret = true; - else { - json = Util.parseJSON(str); + if (!ret && json) { + json = Util.parseJSON(json); CloudCmd.createFileTable(json); setTitle(); - } - } - - if (ret) - DOM.getCurrentFileContent({ - url : fsPath, - dataType : 'json', - success : function(data) { - var str = Util.stringifyJSON(data), - MAX_SIZE = 50000; - - setTitle(); - - CloudCmd.createFileTable(data); - Util.log(str.length); - - /* если размер данных не очень бошьой * - * сохраняем их в кэше */ - if (str.length < MAX_SIZE) - Storage.set(cleanPath, str); - } - }); + } else + DOM.getCurrentFileContent({ + url : fsPath, + dataType : 'json', + success : function(json) { + setTitle(); + CloudCmd.createFileTable(json); + Storage.set(cleanPath, json); + } + }); + }); }; /** diff --git a/lib/client/dom.js b/lib/client/dom.js index e57f3077..cada95bd 100644 --- a/lib/client/dom.js +++ b/lib/client/dom.js @@ -1340,18 +1340,20 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; */ this.checkStorageHash = function(name, callback) { DOM.loadCurrentHash(function(loadHash) { - var equal, error, - nameHash = name + '-hash', - Storage = DOM.Storage, - storeHash = Storage.get(name + '-hash'), - isContain = Util.isContainStr(loadHash, 'error'); + var Storage = DOM.Storage; - if (isContain) - error = loadHash; - else if (loadHash === storeHash) - equal = true; - - Util.exec(callback, error, equal, loadHash); + Storage.get(name + '-hash', function(storeHash) { + var equal, error, + nameHash = name + '-hash', + isContain = Util.isContainStr(loadHash, 'error'); + + if (isContain) + error = loadHash; + else if (loadHash === storeHash) + equal = true; + + Util.exec(callback, error, equal, loadHash); + }); }); }; @@ -1407,10 +1409,20 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; if (!allowed || isDir) Util.exec(callback); else { - data = Storage.get(nameData); - hash = Storage.get(nameHash); + Util.asyncCall([ + function(callback) { + Storage.get(nameData, function(data) { + Util.exec(callback, data); + }); + }, + function(callback) { + Storage.get(nameHash, function(hash) { + Util.exec(callback, hash); + }); + } + ], callback); + - Util.exec(callback, data, hash); } }); }; diff --git a/lib/client/storage.js b/lib/client/storage.js index 4915885b..fbfe8b61 100644 --- a/lib/client/storage.js +++ b/lib/client/storage.js @@ -29,56 +29,57 @@ var Util, DOM; }; /** remove element */ - this.remove = function(item) { - var ret = this; + this.remove = function(item, callback) { + var ret = Allowed; - if (Allowed) + if (ret) localStorage.removeItem(item); + + Util.exec(callback, ret); - return ret; + return this; }; /** если доступен localStorage и * в нём есть нужная нам директория - * записываем данные в него */ - this.set = function(name, data) { - var ret = this; + this.set = function(name, data, callback) { + var str, ret = Allowed && name; + + if (Util.isObject(data)) + str = Util.stringifyJSON(data); if (Allowed && name) - localStorage.setItem(name, data); + localStorage.setItem(name, str || data); - return ret; + Util.exec(callback, ret); + + return this; }, /** Если доступен Storage принимаем из него данные*/ - this.get = function(name) { + this.get = function(name, callback) { var ret; if (Allowed) ret = localStorage.getItem(name); + + Util.exec(callback, ret); return ret; }, - /* get all Storage from local storage */ - this.getAll = function() { - var ret = null; - - if (Allowed) - ret = localStorage; - - return ret; - }; - /** функция чистит весь кэш для всех каталогов*/ - this.clear = function() { - var ret = this; + this.clear = function(callback) { + var ret = Allowed; - if (Allowed) + if (ret) localStorage.clear(); - return ret; + Util.exec(callback, ret); + + return this; }; } })(Util, DOM); diff --git a/lib/client/storage/_github.js b/lib/client/storage/_github.js index 419024ac..b128cb79 100644 --- a/lib/client/storage/_github.js +++ b/lib/client/storage/_github.js @@ -51,40 +51,42 @@ var CloudCmd, Util, DOM, CloudFunc, Github, cb; } - GitHub.autorize = function(pCallBack, pCode) { - var lCode, lToken = Storage.get('token'); - - if (lToken) { - GitHub.Login(lToken); - Util.exec(pCallBack); - } - else { - lCode = pCode || window.location.search; - - if (lCode || Util.isContainStr(lCode, '?code=') ) - CloudCmd.getConfig(function(pConfig) { - DOM.ajax({ - type : 'put', - url : pConfig && pConfig.apiURL + '/auth', - data : Util.removeStr(lCode, '?code='), - success : function(pData) { - if (pData && pData.token) { - lToken = pData.token; - - GitHub.Login(lToken); - Storage.set('token', lToken); - Util.exec(pCallBack); + GitHub.autorize = function(callback, code) { + Storage.get('token', function(token) { + var code, isContain, + URL = '//' + window.location.host + '/auth/github'; + + if (token) { + GitHub.Login(token); + Util.exec(callback); + } else { + if (!code) + code = window.location.search; + + isContain = Util.isContainStr(code, '?code='); + + if (!isContain) + DOM.openWindow(URL); + else + CloudCmd.getConfig(function(config) { + DOM.ajax({ + type : 'put', + url : config && config.apiURL + '/auth', + data : Util.removeStr(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...'); } - else - Util.log('Worning: token not getted...'); - } + }); }); - }); - else{ - var lUrl = '//' + window.location.host + '/auth/github'; - DOM.openWindow(lUrl); - } - } + } + }); }; GitHub.getUserData = function(pCallBack) {