diff --git a/html/auth/github.html b/html/auth/github.html index 67f57fee..cd3b0ee5 100644 --- a/html/auth/github.html +++ b/html/auth/github.html @@ -9,9 +9,7 @@ var opener = window.opener; if (opener) { - var CloudCmd = opener.CloudCmd; - - CloudCmd.getModules(function(error, modules) { + DOM.Files.get('modules', function(error, modules) { var Util = opener.Util, Storage = Util.findObjByNameInArr(modules, 'storage'), GitHub = Util.findObjByNameInArr(Storage, 'GitHub'), diff --git a/html/fs/index.html b/html/fs/index.html index f97345e2..7138bf08 100644 --- a/html/fs/index.html +++ b/html/fs/index.html @@ -49,6 +49,7 @@ client + 'load', client + 'notify', client + 'storage', + client + 'files', 'client', client + 'listeners', client + 'key' diff --git a/lib/client.js b/lib/client.js index c5bb3cad..79717c3a 100644 --- a/lib/client.js +++ b/lib/client.js @@ -6,12 +6,9 @@ var Util, DOM, CloudFunc; scope.CloudCmd = new CloudCmdProto(Util, DOM, CloudFunc); function CloudCmdProto(Util, DOM, CloudFunc) { - var Key, Config, Modules, Extensions, - FileTemplate, PathTemplate, Listeners, - DIR_HTML = '/html/', - DIR_HTML_FS = DIR_HTML + 'fs/', - DIR_JSON = '/json/', - + var Key, + Listeners, + Files = DOM.Files, Images = DOM.Images, Info = DOM.CurrentInfo, CloudCmd = this, @@ -97,12 +94,13 @@ var Util, DOM, CloudFunc; */ function loadModule(params) { var name, path, func, doBefore, slash, afterSlash, - isContain; + funcName, isContain; if (params) { name = params.name, path = params.path, func = params.func, + funcName = params.funcName, doBefore = params.dobefore; if (path && !name) { @@ -137,7 +135,7 @@ var Util, DOM, CloudFunc; }); }; - CloudCmd[name].show = CloudCmd[name]; + CloudCmd[name][funcName] = CloudCmd[name]; } } } @@ -203,7 +201,7 @@ var Util, DOM, CloudFunc; }); }); - CloudCmd.getModules(function(error, modules) { + DOM.Files.get('modules', function(error, modules) { var storageObj, mod, path, STORAGE = 'storage', showLoad = Images.showLoad.bind(Images), @@ -215,8 +213,11 @@ var Util, DOM, CloudFunc; }, load = function(name, path, func) { + var isTmpl = path === 'template'; + loadModule({ name : name, + funcName : isTmpl ? 'get' : 'show', path : path, dobefore : func }); @@ -262,7 +263,7 @@ var Util, DOM, CloudFunc; Listeners.initKeysPanel(); - CloudCmd.getConfig(function(error, config) { + Files.get('config', function(error, config) { var localStorage = config.localStorage, dirPath = DOM.getCurrentDirPath(); @@ -283,55 +284,6 @@ var Util, DOM, CloudFunc; callback(); } - this.getTemplate = function(data, name, callback) { - var path = DIR_HTML + name +'.html', - func = getSystemFile(data, path); - - func(callback); - }; - - function getSystemFile(global, url) { - var get = function (callback) { - var success = Util.exec.with(callback, null); - - Util.exec.if(global, success, function() { - DOM.load.ajax({ - url : url, - success : function(local) { - global = local; - success(local); - }, - error : function(error) { - callback(error); - } - }); - }); - }; - - return get; - } - - this.setConfig = function(config) { Config = config; }; - this.getConfig = function(callback) { - var func = Util.exec.with(callback, null); - - Util.exec.if(Config, func, function(callback) { - 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'); - this.getPathTemplate = getSystemFile(PathTemplate, DIR_HTML_FS + 'path.html'); - this.getPathLinkTemplate = getSystemFile(PathTemplate, DIR_HTML_FS + 'pathLink.html'); - this.getLinkTemplate = getSystemFile(PathTemplate, DIR_HTML_FS + 'link.html'); - this.execFromModule = function(moduleName, funcName, params) { var obj = CloudCmd[moduleName], isObj = Util.isObject(obj); @@ -408,14 +360,9 @@ var Util, DOM, CloudFunc; /* getting current element if was refresh */ path = DOM.getCurrentDirPath(panel), wasRefresh = path === json.path, - funcs = [ - CloudCmd.getFileTemplate, - CloudCmd.getPathTemplate, - CloudCmd.getPathLinkTemplate, - CloudCmd.getLinkTemplate - ]; + names = ['file', 'path', 'link', 'pathLink']; - Util.exec.parallel(funcs, function(error, templFile, templPath, templPathLink, templLink) { + Files.get(names, function(error, templFile, templPath, templLink, templPathLink) { var n, found, varCurrent, varName, current, dir = DOM.getCurrentDirName(), name = DOM.getCurrentName(), diff --git a/lib/client/config.js b/lib/client/config.js index 34be1a84..e0d55f31 100644 --- a/lib/client/config.js +++ b/lib/client/config.js @@ -50,7 +50,7 @@ var CloudCmd, Util, DOM; if (!Template) Template = template; - CloudCmd.getConfig(function(error, config) { + DOM.Files.get('config', function(error, config) { var div, data, inputs, inputFirst, focus, obj; @@ -136,9 +136,10 @@ var CloudCmd, Util, DOM; } CONFIG[name] = data; - CloudCmd.setConfig(CONFIG); - Config.write(obj); + CloudCmd.set('config', CONFIG, function() { + Config.write(obj); + }); } function onKey(event) { diff --git a/lib/client/dom.js b/lib/client/dom.js index 285cf612..f97da08a 100644 --- a/lib/client/dom.js +++ b/lib/client/dom.js @@ -334,10 +334,9 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; } this.loadRemote = function(name, callback) { - var getConfig = CloudCmd.getConfig, - getModules = CloudCmd.getModules; + var Files = DOM.Files; - Util.exec.parallel([getConfig, getModules], function(error, config, modules) { + Files.get(['config', 'modules'], function(error, config, modules) { var remoteTmpls, local, remote, load = DOM.load, online = config.online && navigator.onLine, @@ -1217,7 +1216,7 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; * @param callback */ this.saveDataToStorage = function(name, data, hash, callback) { - CloudCmd.getConfig(function(error, config) { + DOM.Files.get('config', function(error, config) { var allowed = config.localStorage, isDir = DOM.isCurrentIsDir(), nameHash = name + '-hash', @@ -1250,7 +1249,7 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; * @param callback */ this.getDataFromStorage = function(name, callback) { - CloudCmd.getConfig(function(error, config) { + DOM.Files.get('config', function(error, config) { var Storage = DOM.Storage, nameHash = name + '-hash', nameData = name + '-data', @@ -1581,7 +1580,7 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; }; this.getType = function(name, callback) { - CloudCmd.getExt(function(extensions) { + DOM.Files.get('ext', function(error, extensions) { var index, ext = Util.getExt(name); diff --git a/lib/client/edit.js b/lib/client/edit.js index fc4ecf00..df66e931 100644 --- a/lib/client/edit.js +++ b/lib/client/edit.js @@ -230,7 +230,7 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch, Zip, MenuI var path = Info.path, value = Ace.getValue(); - CloudCmd.getConfig(function(error, config) { + DOM.Files.get('config', function(error, config) { var isDiff = config.diff, isZip = config.zip; diff --git a/lib/client/files.js b/lib/client/files.js new file mode 100644 index 00000000..d722f38d --- /dev/null +++ b/lib/client/files.js @@ -0,0 +1,108 @@ +/* load and store templates data */ + +var Util, DOM; + +(function(Util, DOM) { + + var DOMProto = Object.getPrototypeOf(DOM); + + DOMProto.Files = new FilesProto(Util, DOM); + + function FilesProto(Util, DOM) { + var Files = this, + funcs = [], + Data = {}, + DIR_HTML = '/html/', + DIR_HTML_FS = DIR_HTML + 'fs/', + DIR_JSON = '/json/'; + + this.get = function(name, callback) { + var type = Util.getType(name); + + Util.checkArgs(arguments, ['name', 'callback']); + + switch(type) { + case 'string': + getModule(name, callback); + break; + + case 'array': + funcs = name.map(function(name) { + return function(callback) { + Files.get(name, callback); + }; + }); + + Util.exec.parallel(funcs, callback); + break; + } + + return Files; + }; + + this.set = function(name, data, callback) { + Util.checkArgs(arguments, [name, data]); + + Data[name] = data; + callback(null); + + return Files; + }; + + function getModule(name, callback) { + var path, + json = 'config|modules|ext', + html = 'file|path|link|pathLink', + + regExpHTML = new RegExp(html), + regExpJSON = new RegExp(json), + + isHTML = name.match(regExpHTML), + isJSON = name.match(regExpJSON); + + if (isHTML) { + path = DIR_HTML_FS + name + '.html', + getSystemFile(Data[name], path , callback); + } else if (isJSON) { + if (name === 'config') { + getConfig(callback); + } else { + path = DIR_JSON + name + '.json'; + getSystemFile(Data[name], path, callback); + } + } else { + throw(new Error('Wrong file name: ' + name)); + } + } + + function getSystemFile(global, url, callback) { + var success = Util.exec.with(callback, null); + + Util.exec.if(global, success, function() { + DOM.load.ajax({ + url : url, + success : function(local) { + global = local; + success(local); + }, + error : function(error) { + callback(error); + } + }); + }); + } + + function getConfig(callback) { + var func = Util.exec.with(callback, null); + + Util.exec.if(Data.config, func, function(callback) { + var RESTful = DOM.RESTful; + + RESTful.Config.read(function(config) { + Data.config = config; + callback(config); + }); + }); + } + } +})(Util, DOM); diff --git a/lib/client/listeners.js b/lib/client/listeners.js index c4f25b68..b362f934 100644 --- a/lib/client/listeners.js +++ b/lib/client/listeners.js @@ -9,7 +9,7 @@ var Util, DOM, CloudCmd; var Info = DOM.CurrentInfo, Storage = DOM.Storage, Events = DOM.Events, - getConfig = CloudCmd.getConfig, + Files = DOM.Files, EventsFiles = { 'mousedown' : Util.exec.with(execIfNotUL, setCurrentFileByEvent), 'contextmenu' : onContextMenu, @@ -20,7 +20,7 @@ var Util, DOM, CloudCmd; }; this.analytics = function() { - getConfig(function(error, config) { + Files.get('config', function(error, config) { var analytics = config.analytics, online = config.online; @@ -241,7 +241,7 @@ var Util, DOM, CloudCmd; } function appStorage() { - getConfig(function(error, config) { + Files.get('config', function(error, config) { var isAppStorage = config.appStorage, appStorage = window.applicationStorage; diff --git a/lib/client/menu.js b/lib/client/menu.js index c66fc03f..27dd60b0 100644 --- a/lib/client/menu.js +++ b/lib/client/menu.js @@ -183,7 +183,7 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO; } function getUploadTo(callback) { - CloudCmd.getModules(function(error, modules) { + DOM.Files.get('modules', function(error, modules) { var menu = {}, storage = Util.findObjByNameInArr(modules, 'storage'), items = Util.getNamesFromObjArray(storage) || []; diff --git a/lib/client/notify.js b/lib/client/notify.js index 9576771e..501e1aca 100644 --- a/lib/client/notify.js +++ b/lib/client/notify.js @@ -26,7 +26,7 @@ var Util, DOM, CloudCmd; }); this.send = function(msg) { - CloudCmd.getConfig(function(error, config) { + DOM.Files.get('config', function(error, config) { var notify, notifications = config.notifications, focus = window.focus.bind(window), diff --git a/lib/client/storage/_dropbox.js b/lib/client/storage/_dropbox.js index 2dba3947..88bc0bcc 100644 --- a/lib/client/storage/_dropbox.js +++ b/lib/client/storage/_dropbox.js @@ -64,7 +64,7 @@ var CloudCmd, Util, DOM, CloudFunc, Dropbox, cb, Client; * @param pData = {key, secret} */ this.login = function(callback) { - CloudCmd.getModules(function(error, modules){ + DOM.Files.get('modules', function(error, modules){ var url = CloudCmd.HOST + '/html/auth/dropbox.html', storage = Util.findObjByNameInArr(modules, 'storage'), dropbox = Util.findObjByNameInArr(storage, 'DropBox'), diff --git a/lib/client/storage/_dropbox_chooser.js b/lib/client/storage/_dropbox_chooser.js index 1a3d948b..48b38de5 100644 --- a/lib/client/storage/_dropbox_chooser.js +++ b/lib/client/storage/_dropbox_chooser.js @@ -23,7 +23,7 @@ var Util, CloudCmd, DOM, Dropbox; function load(){ Util.time('dropbox load'); - CloudCmd.getConfig(function(error, config){ + DOM.Files.get('config', function(error, config){ var element = DOM.load({ src : CHOOSER_API, notAppend : true, diff --git a/lib/client/storage/_filepicker.js b/lib/client/storage/_filepicker.js index e9457d0b..51ab2a65 100644 --- a/lib/client/storage/_filepicker.js +++ b/lib/client/storage/_filepicker.js @@ -49,7 +49,7 @@ var CloudCmd, Util, DOM, filepicker; Util.time('filepicker load'); DOM.load.js('//api.filepicker.io/v1/filepicker.js', function() { - CloudCmd.getModules(function(error, modules) { + DOM.Files.get('modules', function(error, modules) { var storage = Util.findObjByNameInArr(modules, 'storage'), picker = Util.findObjByNameInArr(storage, 'FilePicker'), key = picker && picker.key; diff --git a/lib/client/storage/_gdrive.js b/lib/client/storage/_gdrive.js index 98bc1945..7cd79bdf 100644 --- a/lib/client/storage/_gdrive.js +++ b/lib/client/storage/_gdrive.js @@ -16,7 +16,7 @@ var CloudCmd, Util, DOM, gapi; var lUrl = 'https://apis.google.com/js/client.js'; DOM.load.js(lUrl, function() { - CloudCmd.getModules(function(error, modules) { + DOM.Files.get('modules', function(error, modules) { var storage = Util.findObjByNameInArr(modules, 'storage'), gDrive = Util.findObjByNameInArr(storage, 'GDrive'), gDriveId = gDrive && gDrive.id, diff --git a/lib/client/storage/_skydrive.js b/lib/client/storage/_skydrive.js index f4d0500e..95b4655d 100644 --- a/lib/client/storage/_skydrive.js +++ b/lib/client/storage/_skydrive.js @@ -25,7 +25,7 @@ var CloudCmd, Util, DOM, WL; } function auth() { - CloudCmd.getModules(function(error, modules){ + DOM.Files.get('modules', function(error, modules){ var lStorage = Util.findObjByNameInArr(modules, 'storage'), lSkyDrive = Util.findObjByNameInArr(lStorage, 'SkyDrive'), lSkyDriveKey = lSkyDrive && lSkyDrive.id; diff --git a/lib/client/storage/_vk.js b/lib/client/storage/_vk.js index 4efd5043..de53e56d 100644 --- a/lib/client/storage/_vk.js +++ b/lib/client/storage/_vk.js @@ -32,7 +32,7 @@ var CloudCmd, Util, DOM, VK; } function auth(callback) { - CloudCmd.getConfig(function(error, config){ + DOM.Files.get('config', function(error, config){ var lDOCUMENTS_ACCESS = 131072; VK.init({ apiId: config.vk_id});