diff --git a/client/buffer.js b/client/buffer.js index 94025433..e3f5b4a9 100644 --- a/client/buffer.js +++ b/client/buffer.js @@ -11,7 +11,6 @@ function BufferProto(Util, DOM, CloudCmd) { var Storage = DOM.Storage, - Files = DOM.Files, Info = DOM.CurrentInfo, json = Util.json, @@ -58,22 +57,13 @@ }); } - function isEnabled(callback) { - Files.get('config', function(error, config) { - if (error) - showMessage(error); - else - callback(config.buffer); - }); - } - function callIfEnabled(callback) { - isEnabled(function(is) { - if (is) - callback(); - else - showMessage('Buffer disabled in config!'); - }); + var is = CloudCmd.config('buffer'); + + if (is) + return callback(); + + showMessage('Buffer disabled in config!'); } function copy() { diff --git a/client/client.js b/client/client.js index 27c5b426..8cfdf591 100644 --- a/client/client.js +++ b/client/client.js @@ -157,44 +157,51 @@ var Util, DOM, CloudFunc, join; * выполняет весь функционал по * инициализации */ - this.init = function(prefix) { - var func = function() { - Util.exec.series([ - initModules, - baseInit, - loadPlugins, - function() { - CloudCmd.route(location.hash); - } - ]); - }, - - funcBefore = function(callback) { - var src = prefix + '/join:' + [ - CloudCmd.LIBDIRCLIENT + 'polyfill.js', - '/modules/domtokenlist-shim/dist/domtokenlist.min.js', - ].join(':'); - - DOM.loadJquery(function() { - DOM.load.js(src, callback); - }); - }; + this.init = function(config) { + var func = function() { + Util.exec.series([ + initModules, + baseInit, + loadPlugins, + function() { + CloudCmd.route(location.hash); + } + ]); + }; - CloudCmd.PREFIX = prefix; + var funcBefore = function(callback) { + var src = prefix + '/join:' + [ + CloudCmd.LIBDIRCLIENT + 'polyfill.js', + '/modules/domtokenlist-shim/dist/domtokenlist.min.js', + ].join(':'); + + DOM.loadJquery(function() { + DOM.load.js(src, callback); + }); + }; + + var prefix = config.prefix; + + CloudCmd.PREFIX = prefix; CloudCmd.PREFIX_URL = prefix + CloudFunc.apiURL; - DOM.Files.get('config', function(error, config) { - var options = { - htmlDialogs: !error && config.htmlDialogs - }; - - if (config.onePanelMode) - CloudCmd.MIN_ONE_PANEL_WIDTH = Infinity; - - if (error) - CloudCmd.log(error); - - DOM.Dialog = new DOM.Dialog(prefix, options); + CloudCmd.config = function(key) { + return config[key]; + }; + + CloudCmd._config = function(key, value) { + /* + * should be called from config.js only + * after successful update on server + */ + config[key] = value; + }; + + if (config.onePanelMode) + CloudCmd.MIN_ONE_PANEL_WIDTH = Infinity; + + DOM.Dialog = new DOM.Dialog(prefix, { + htmlDialogs: config.htmlDialogs }); Util.exec.if(document.body.scrollIntoViewIfNeeded, func, funcBefore); @@ -213,11 +220,11 @@ var Util, DOM, CloudFunc, join; if (!Array.isArray(urls)) throw Error('urls should be array!'); - urls = urls.map(function(url) { + var noPrefixUrls = urls.map(function(url) { return url.replace(prefix, ''); }); - return prefix + join(urls); + return prefix + join(noPrefixUrls); }; this.route = function(path) { @@ -419,20 +426,12 @@ var Util, DOM, CloudFunc, join; CloudCmd.log('reading dir: "' + path + '";'); - Files.get('config', function(error, config) { - var dirStorage, - Dialog = DOM.Dialog; - - if (error) - Dialog.alert(TITLE, error); - else - dirStorage = config.dirStorage; - - if (dirStorage) - Storage.get(path, create); - else - create(); - }); + var dirStorage = CloudCmd.config(dirStorage); + + if (!dirStorage) + return create(); + + Storage.get(path, create); } /** diff --git a/client/cloudcmd.js b/client/cloudcmd.js index dbccfaa2..e5ab763b 100644 --- a/client/cloudcmd.js +++ b/client/cloudcmd.js @@ -5,8 +5,8 @@ var CloudCmd; CloudCmd = load; - function load(prefix) { - prefix = prefix || ''; + function load(config) { + var prefix = config.prefix || ''; var modules = '/modules/'; var client = 'client/'; @@ -53,7 +53,7 @@ var CloudCmd; var urlFiles = getJoinURL(allFiles); createScript(prefix + urlFiles, function() { - CloudCmd.init(prefix); + CloudCmd.init(config); }); } diff --git a/client/config.js b/client/config.js index 6f4b460f..dddd1897 100644 --- a/client/config.js +++ b/client/config.js @@ -9,6 +9,7 @@ var CloudCmd, Util, DOM, io; CloudCmd.Config = ConfigProto; function ConfigProto() { + var config = CloudCmd.config; var Loading = true, Key = CloudCmd.Key, Dialog = DOM.Dialog, @@ -51,61 +52,58 @@ var CloudCmd, Util, DOM, io; } function initSocket(error) { - var socket, - href = getHost(), - prefix = CloudCmd.PREFIX, + var href = getHost(); + var prefix = CloudCmd.PREFIX, FIVE_SECONDS = 5000, - save = function(data) { + save = function(data) { + onSave(data); socket.send(data); }; - if (!error) { - socket = io.connect(href + prefix + '/config', { - 'max reconnection attempts' : Math.pow(2, 32), - 'reconnection limit' : FIVE_SECONDS, - path: prefix + '/socket.io' - }); - - authCheck(socket); - - socket.on('connect', function() { - Config.save = save; - }); - - socket.on('config', function(config) { - DOM.Storage.setAllowed(config.localStorage); - }); - - socket.on('message', function(data) { - onSave(data); - }); - - socket.on('log', function(msg) { - CloudCmd.log(msg); - }); - - socket.on('disconnect', function() { - Config.save = saveHttp; - }); - - socket.on('err', function(error) { - Dialog.alert(TITLE, error); - }); - } + if (error) + return; + + var socket = io.connect(href + prefix + '/config', { + 'max reconnection attempts' : Math.pow(2, 32), + 'reconnection limit' : FIVE_SECONDS, + path: prefix + '/socket.io' + }); + + authCheck(socket); + + socket.on('connect', function() { + Config.save = save; + }); + + socket.on('config', function(config) { + DOM.Storage.setAllowed(config.localStorage); + }); + + socket.on('message', function(data) { + onSave(data); + }); + + socket.on('log', function(msg) { + CloudCmd.log(msg); + }); + + socket.on('disconnect', function() { + Config.save = saveHttp; + }); + + socket.on('err', function(error) { + Dialog.alert(TITLE, error); + }); } function authCheck(socket) { - Files.get('config', function(error, config) { - if (error) - return Dialog.alert(TITLE, error); - - if (config.auth) { - socket.emit('auth', config.username, config.password); - - socket.on('reject', function() { - Dialog.alert(TITLE, 'Wrong credentials!'); - }); - } + if (!config('auth')) + return; + + socket.emit('auth', config('username'), config('password')); + + socket.on('reject', function() { + Dialog.alert(TITLE, 'Wrong credentials!'); }); } @@ -214,6 +212,7 @@ var CloudCmd, Util, DOM, io; Object.keys(obj).forEach(function(name) { var data = obj[name]; + CloudCmd._config(name, data); input.setValue(name, data, Element); }); @@ -296,15 +295,10 @@ var CloudCmd, Util, DOM, io; } } - DOM.Files.get('config', function(error, config) { - if (error) - return Dialog.alert(TITLE, error); - - if (!config.configDialog) - return; - - init(); - }); + if (!CloudCmd.config('configDialog')) + return; + + init(); } })(CloudCmd, Util, DOM); diff --git a/client/dom.js b/client/dom.js index 0d81327b..ad0ee155 100644 --- a/client/dom.js +++ b/client/dom.js @@ -5,6 +5,7 @@ var CloudCmd, Util, DOM, CloudFunc; /* global rendy */ /* global itype */ + /* global exec */ var DOMFunc = function() {}, DOMProto, @@ -293,11 +294,11 @@ var CloudCmd, Util, DOM, CloudFunc; if (o.name && window[o.name]) callback(); else - Files.get(['config', 'modules'], function(error, config, modules) { + Files.get('modules', function(error, modules) { var remoteTmpls, local, remote, load = DOM.load, prefix = CloudCmd.PREFIX, - online = config.online && navigator.onLine, + online = CloudCmd.config('online') && navigator.onLine, remoteObj = Util.findObjByNameInArr(modules, 'remote'), module = Util.findObjByNameInArr(remoteObj, name), @@ -1185,31 +1186,30 @@ var CloudCmd, Util, DOM, CloudFunc; * * @param name * @param data + * @param hash * @param callback */ this.saveDataToStorage = function(name, data, hash, callback) { - DOM.Files.get('config', function(error, config) { - var allowed = config.localStorage, - isDir = DOM.isCurrentIsDir(), - nameHash = name + '-hash', - nameData = name + '-data'; + var allowed = CloudCmd.config('localStorage'); + var isDir = DOM.isCurrentIsDir(); + var nameHash = name + '-hash'; + var nameData = name + '-data'; + + if (!allowed || isDir) + return Util.exec(callback); + + Util.exec.if(hash, function() { + var Storage = DOM.Storage; - if (!allowed || isDir) - Util.exec(callback); - else - Util.exec.if(hash, function() { - var Storage = DOM.Storage; - - Storage.set(nameHash, hash); - Storage.set(nameData, data); - - Util.exec(callback, hash); - }, function(callback) { - DOM.loadCurrentHash(function(error, loadHash) { - hash = loadHash; - callback(); - }); - }); + Storage.set(nameHash, hash); + Storage.set(nameData, data); + + Util.exec(callback, hash); + }, function(callback) { + DOM.loadCurrentHash(function(error, loadHash) { + hash = loadHash; + callback(); + }); }); }; @@ -1221,32 +1221,22 @@ var CloudCmd, Util, DOM, CloudFunc; * @param callback */ this.getDataFromStorage = function(name, callback) { - DOM.Files.get('config', function(error, config) { - var Storage = DOM.Storage, - nameHash = name + '-hash', - nameData = name + '-data', - allowed = config.localStorage, - isDir = DOM.isCurrentIsDir(); - - if (!allowed || isDir) - Util.exec(callback); - else { - Util.exec.parallel([ - function(callback) { - Storage.get(nameData, callback); - }, - function(callback) { - Storage.get(nameHash, callback); - } - ], callback); - } - }); + var Storage = DOM.Storage; + var nameHash = name + '-hash'; + var nameData = name + '-data'; + var allowed = CloudCmd.config('localStorage'); + var isDir = DOM.isCurrentIsDir(); + + if (!allowed || isDir) + return Util.exec(callback); + + Util.exec.parallel([ + exec.with(Storage.get, nameData, callback), + exec.with(Storage.get, nameHash, callback), + ], callback); }; - /** function getting FM - * @param pPanel_o = {active: true} - */ - this.getFM = function() { + this.getFM = function() { return this.getPanel().parentElement; }; diff --git a/client/edit-file.js b/client/edit-file.js index f658f88a..cc7f43e6 100644 --- a/client/edit-file.js +++ b/client/edit-file.js @@ -8,6 +8,7 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format; var Dialog = DOM.Dialog; var exec = Util.exec; var EditFile = this; + var config = CloudCmd.config; var Menu, @@ -88,17 +89,12 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format; } function authCheck(spawn) { - DOM.Files.get('config', function(error, config) { - if (error) - return Dialog.alert(TITLE, error); - - if (!config.auth) - return; - - spawn.emit('auth', config.username, config.password); - spawn.on('reject', function() { - Dialog.alert(TITLE, 'Wrong credentials!'); - }); + if (!config('auth')) + return; + + spawn.emit('auth', config('username'), config('password')); + spawn.on('reject', function() { + Dialog.alert(TITLE, 'Wrong credentials!'); }); } diff --git a/client/edit-names.js b/client/edit-names.js index 1a646b94..be6828a5 100644 --- a/client/edit-names.js +++ b/client/edit-names.js @@ -96,8 +96,9 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO; .split('\n'); var reject = Promise.reject.bind(Promise); + var root = CloudCmd.config('root'); - getRoot() + Promise.resolve(root) .then(rename(dir, from, to)) .then(function(res) { if (res.status === 404) @@ -198,17 +199,6 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO; }); } - function getRoot() { - return new Promise(function(resolve, reject) { - DOM.Files.get('config', function(error, config) { - if (error) - return reject(error); - - resolve(config.root); - }); - }); - } - function isChanged() { var editor = CloudCmd.Edit.getEditor(); var msg = 'Apply new names?'; diff --git a/client/edit.js b/client/edit.js index a21386ff..c5b71e72 100644 --- a/client/edit.js +++ b/client/edit.js @@ -10,16 +10,12 @@ var CloudCmd, Util, DOM, CloudFunc; function EditProto(callback) { var Name = 'Edit'; var Loading = true; + var EditorName = CloudCmd.config('editor'); - var Dialog = DOM.Dialog, - exec = Util.exec, - Element, - - EditorName = 'edward', + var exec = Util.exec; + var Element, editor, - TITLE = 'Edit', - ConfigView = { afterShow: function() { editor @@ -40,7 +36,6 @@ var CloudCmd, Util, DOM, CloudFunc; exec.series([ CloudCmd.View, - getConfig, function(callback) { loadFiles(element, callback); }, @@ -107,17 +102,6 @@ var CloudCmd, Util, DOM, CloudFunc; CloudCmd.View.hide(); }; - function getConfig(callback) { - DOM.Files.get('config', function(error, config) { - if (error) - Dialog.alert(TITLE, error); - else if (config.editor) - EditorName = config.editor; - - callback(); - }); - } - function loadFiles(element, callback) { var prefix = CloudCmd.PREFIX; var prefixName = prefix + '/' + EditorName; diff --git a/client/konsole.js b/client/konsole.js index 36be98e0..806ed1d7 100644 --- a/client/konsole.js +++ b/client/konsole.js @@ -2,6 +2,7 @@ /* global Util */ /* global DOM */ /* global Console */ +/* global exec */ (function(CloudCmd, Util, DOM) { 'use strict'; @@ -9,21 +10,21 @@ CloudCmd.Konsole = ConsoleProto; function ConsoleProto() { - var Name = 'Konsole', + var config = CloudCmd.config; + var Name = 'Konsole', TITLE = 'Console', Element, Loaded, Images = DOM.Images, Dialog = DOM.Dialog, - exec = Util.exec, Konsole = this; function init() { Images.show.load('top'); - Util.exec.series([ + exec.series([ CloudCmd.View, load, create, @@ -68,7 +69,7 @@ Console(Element, options, function(spawn) { spawn.on('connect', exec.with(authCheck, spawn)); - Util.exec(callback); + exec(callback); }); Console.addShortCuts({ @@ -83,17 +84,13 @@ } function authCheck(spawn) { - DOM.Files.get('config', function(error, config) { - if (error) - return Dialog.alert(TITLE, error); - - if (config.auth) { - spawn.emit('auth', config.username, config.password); - - spawn.on('reject', function() { - Dialog.alert(TITLE, 'Wrong credentials!'); - }); - } + if (!config('auth')) + return; + + spawn.emit('auth', config('username'), config('password')); + + spawn.on('reject', function() { + Dialog.alert(TITLE, 'Wrong credentials!'); }); } @@ -102,7 +99,7 @@ CloudCmd.View.show(Element, { afterShow: function() { Console.focus(); - Util.exec(callback); + exec(callback); } }); }; @@ -117,22 +114,17 @@ } else { Loaded = true; Util.timeEnd(Name + ' load'); - Util.exec(callback); + exec(callback); } }); Util.time(Name + ' load'); } - DOM.Files.get('config', function(error, config) { - if (error) - return Dialog.alert(TITLE, error); - - if (!config.console) - return; - - init(); - }); + if (!CloudCmd.config('console')) + return; + + init(); } })(CloudCmd, Util, DOM); diff --git a/client/menu.js b/client/menu.js index bfb43d53..b7768045 100644 --- a/client/menu.js +++ b/client/menu.js @@ -6,6 +6,7 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO; CloudCmd.Menu = MenuProto; function MenuProto(position) { + var config = CloudCmd.config; var Buffer = DOM.Buffer, Info = DOM.CurrentInfo, Loading = true, @@ -105,17 +106,6 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO; } } - function isAuth(callback) { - DOM.Files.get('config', function(error, config) { - var is = config.auth; - - if (error) - DOM.alert(TITLE, error); - - callback(is); - }); - } - function getOptions(notFile) { var name, func, options; @@ -166,45 +156,44 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO; } function loadFileMenuData(callback) { - isAuth(function(is) { - var show = function(name) { - CloudCmd[name].show(); + var is = CloudCmd.config('auth'); + var show = function(name) { + CloudCmd[name].show(); + }, + Dialog = DOM.Dialog, + menuData = getMenuData(is), + menu = { + 'View' : curry(show, 'View'), + 'Edit' : curry(show, 'Edit'), + 'Rename' : function() { + setTimeout(DOM.renameCurrent, 100); }, - Dialog = DOM.Dialog, - menuData = getMenuData(is), - menu = { - 'View' : curry(show, 'View'), - 'Edit' : curry(show, 'Edit'), - 'Rename' : function() { - setTimeout(DOM.renameCurrent, 100); - }, - 'Delete' : function() { - CloudCmd.Operation.show('delete'); - }, - 'Pack' : function() { - CloudCmd.Operation.show('pack'); - }, - 'Extract' : function() { - CloudCmd.Operation.show('extract'); - }, - 'Download' : preDownload, - 'Upload To Cloud': curry(uploadTo, 'Cloud'), - 'Cut' : function() { - isCurrent(Buffer.cut, function() { - Dialog.alert.noFiles(TITLE); - }); - }, - 'Copy' : function() { - isCurrent(Buffer.copy, function() { - Dialog.alert.noFiles(TITLE); - }); - }, - }; - - Util.copyObj(menu, menuData); - - callback(is, menu); - }); + 'Delete' : function() { + CloudCmd.Operation.show('delete'); + }, + 'Pack' : function() { + CloudCmd.Operation.show('pack'); + }, + 'Extract' : function() { + CloudCmd.Operation.show('extract'); + }, + 'Download' : preDownload, + 'Upload To Cloud': curry(uploadTo, 'Cloud'), + 'Cut' : function() { + isCurrent(Buffer.cut, function() { + Dialog.alert.noFiles(TITLE); + }); + }, + 'Copy' : function() { + isCurrent(Buffer.copy, function() { + Dialog.alert.noFiles(TITLE); + }); + }, + }; + + Util.copyObj(menu, menuData); + + callback(is, menu); } function isCurrent(yesFn, noFn) { @@ -282,9 +271,7 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO; } function preDownload() { - DOM.Files.get('config', function(e, config) { - download(config && config.packer); - }); + download(config('packer')); } function download(type) { diff --git a/client/notify.js b/client/notify.js index 76beee4f..48d8ebe8 100644 --- a/client/notify.js +++ b/client/notify.js @@ -1,10 +1,12 @@ +/* global CloudCmd */ var Util, DOM; (function(Util, DOM) { 'use strict'; - var Notify = Util.extendProto(NotifyProto), - DOMProto = Object.getPrototypeOf(DOM); + var config = CloudCmd.config; + var Notify = Util.extendProto(NotifyProto); + var DOMProto = Object.getPrototypeOf(DOM); Util.extend(DOMProto, { Notify: Notify @@ -26,20 +28,18 @@ var Util, DOM; }); this.send = function(msg) { - DOM.Files.get('config', function(error, config) { - var notify, - notifications = config.notifications, - focus = window.focus.bind(window), - granted = Notify.check(); - - if (notifications && granted && Show) { - notify = new Notification(msg, { - icon: '/img/favicon/favicon-notify.png' - }); - - Events.addClick(notify, focus); - } - }); + var notify, + notifications = config('notifications'), + focus = window.focus.bind(window), + granted = Notify.check(); + + if (notifications && granted && Show) { + notify = new Notification(msg, { + icon: '/img/favicon/favicon-notify.png' + }); + + Events.addClick(notify, focus); + } }; this.check = function () { diff --git a/client/operation.js b/client/operation.js index 8f5c0deb..942ca275 100644 --- a/client/operation.js +++ b/client/operation.js @@ -16,6 +16,7 @@ function OperationProto(operation, data) { var Name = 'Operation', TITLE = CloudCmd.TITLE, + config = CloudCmd.config, Loaded, RESTful = DOM.RESTful, @@ -40,18 +41,12 @@ Util.exec.series([ DOM.loadSocket, function(callback) { - var Files = DOM.Files; + if (config('progress')) + load(function(callback) { + create(CloudCmd.PREFIX, callback); + }); - Files.get('config', function(error, config) { - if (error) - Dialog.alert('Config', error); - else if (config.progress) - load(function(callback) { - create(CloudCmd.PREFIX, callback); - }); - - callback(); - }); + callback(); }, function() { Loaded = true; @@ -62,21 +57,15 @@ } function authCheck(spawn, ok) { - DOM.Files.get('config', function(error, config) { - if (error) - return Dialog.alert(TITLE, error); + if (!config('auth')) + return ok(); - if (!config.auth) { - ok(); - } else { - spawn.on('accept', ok); - spawn.on('reject', function() { - Dialog.alert(TITLE, 'Wrong credentials!'); - }); - - spawn.emit('auth', config.username, config.password); - } + spawn.on('accept', ok); + spawn.on('reject', function() { + Dialog.alert(TITLE, 'Wrong credentials!'); }); + + spawn.emit('auth', config('username'), config('password')); } function _initSpero(prefix, fn) { @@ -142,15 +131,10 @@ } function _initPacker(prefix, fn) { - DOM.Files.get('config', function(e, config) { - if (e) - return CloudCmd.log(e); + if (config('packer') === 'zip') + return _setPacker(prefix, 'salam', salam, fn); - if (config.packer === 'zip') - return _setPacker(prefix, 'salam', salam, fn); - - _setPacker(prefix, 'ishtar', ishtar, fn); - }); + _setPacker(prefix, 'ishtar', ishtar, fn); } function create(prefix) { @@ -264,17 +248,13 @@ }; this.pack = function() { - DOM.Files.get('config', function(e, config) { - var zip = config && config.packer === 'zip'; - twopack('pack', zip ? 'zip' : 'tar'); - }); + var isZip = config('packer') === 'zip'; + twopack('pack', isZip ? 'zip' : 'tar'); }; this.extract = function() { - DOM.Files.get('config', function(e, config) { - var zip = config && config.packer === 'zip'; - twopack('extract', zip ? 'zip' : 'tar'); - }); + var isZip = config('packer') === 'zip'; + twopack('extract', isZip ? 'zip' : 'tar'); }; /** diff --git a/html/index.html b/html/index.html index 403ed18a..535013ab 100644 --- a/html/index.html +++ b/html/index.html @@ -33,7 +33,7 @@ diff --git a/server/config.js b/server/config.js index 88a7407b..cc3d3a57 100644 --- a/server/config.js +++ b/server/config.js @@ -65,7 +65,10 @@ module.exports.listen = (socket, authCheck) => { function manage(key, value) { if (!key) return; - + + if (key === '*') + return config; + if (value === undefined) return config[key]; diff --git a/server/route.js b/server/route.js index 3ecd776d..6fc3e0ad 100644 --- a/server/route.js +++ b/server/route.js @@ -56,10 +56,6 @@ module.exports = (req, res, next) => { * additional processing of index file */ function indexProcessing(options) { - let from; - let to; - let left = ''; - let right = ''; const keysPanel = '