From fd2334b14bec84cee7d79bcfb193e004593d68ac Mon Sep 17 00:00:00 2001 From: nightwing Date: Mon, 20 Feb 2017 00:52:45 +0400 Subject: [PATCH] allow configuring setting file names from url --- plugins/c9.core/settings.js | 61 ++++++++++--------- .../views/standalone.html.ejs | 15 ++--- plugins/c9.vfs.standalone/www/ide.html | 19 +++--- 3 files changed, 50 insertions(+), 45 deletions(-) diff --git a/plugins/c9.core/settings.js b/plugins/c9.core/settings.js index 69e85033..f3ffcc61 100644 --- a/plugins/c9.core/settings.js +++ b/plugins/c9.core/settings.js @@ -33,7 +33,7 @@ define(function(require, exports, module) { var resetSettings = options.reset || c9.location.match(/reset=([\w\|]*)/) && RegExp.$1; var develMode = c9.location.indexOf("devel=1") > -1; - var debugMode = c9.location.indexOf("debug=2") > -1; + var debugMode = c9.location.indexOf("debug=2") > -1; var testing = options.testing; var debug = options.debug; @@ -56,6 +56,13 @@ define(function(require, exports, module) { var diff = 0; // TODO should we allow this to be undefined and get NaN in timestamps? var userData; + var skipCloud = {}; + c9.location.replace(/[&?](state|project|user)=([\w]+)/g, function(_, type, val) { + if (!val) return; + PATH[type] = PATH[type].replace(/.settings$/, function() { return "." + val + ".settings"; }); + skipCloud[type] = true; + }); + var inited = false; function loadSettings(json) { if (!json) { @@ -71,40 +78,36 @@ define(function(require, exports, module) { for (var type in json) { if (typeof json[type] == "string") { - if (json[type].charAt(0) == "<") { + try { + json[type] = JSON.parse(json[type]); + } catch (e) { json[type] = TEMPLATE[type]; } - else { - try { - json[type] = JSON.parse(json[type]); - } catch (e) { - json[type] = TEMPLATE[type]; - } - } } } } - - if (!json) { - var info = {}; - var count = KEYS.length; - - KEYS.forEach(function(type) { - fs.readFile(PATH[type], function(err, data) { - try { - info[type] = err ? {} : JSON.parse(data); - } catch (e) { - console.error("Invalid Settings Read for ", - type, ": ", data); - info[type] = {}; - } - - if (--count === 0) - loadSettings(info); - }); + + var count = KEYS.length; + + KEYS.forEach(function(type) { + if (!skipCloud[type] && json) + return --count; + fs.readFile(PATH[type], function(err, data) { + if (!json) json = {}; + try { + json[type] = err ? {} : JSON.parse(data); + } catch (e) { + console.error("Invalid Settings Read for ", + type, ": ", data); + json[type] = {}; + } + + if (--count === 0) + loadSettings(json); }); + }); + if (count > 0) return; - } } read(json); @@ -192,7 +195,7 @@ define(function(require, exports, module) { if (standalone || type == "project") { fs.writeFile(PATH[type], json, forceSync, function(err) {}); - if (standalone && !saveToCloud[type]) + if (standalone && !saveToCloud[type] || skipCloud[type]) return; // We're done } diff --git a/plugins/c9.vfs.standalone/views/standalone.html.ejs b/plugins/c9.vfs.standalone/views/standalone.html.ejs index b99c86fd..1468c436 100644 --- a/plugins/c9.vfs.standalone/views/standalone.html.ejs +++ b/plugins/c9.vfs.standalone/views/standalone.html.ejs @@ -103,6 +103,7 @@ function done(){ var vfs = app.services.vfs; var c9 = app.services.c9; + var settings = app.services.settings; c9.ready(); c9.totalLoadTime = Date.now() - start; @@ -110,13 +111,13 @@ console.warn("Total Load Time: ", Date.now() - start); if (window.hideLoader) { - if (vfs.connected) - window.hideLoader(); - else { - vfs.once("connect", function(){ - window.hideLoader(); - }); - } + var waitVfs = function(fn) { + vfs.connected ? fn() : vfs.once("connect", fn); + }; + var waitSettings = function(fn) { + settings.inited ? fn() : settings.once("read", fn); + }; + waitVfs(waitSettings.bind(null, window.hideLoader)); } } }, function loadError(mod) { diff --git a/plugins/c9.vfs.standalone/www/ide.html b/plugins/c9.vfs.standalone/www/ide.html index 5a720d14..a8753678 100644 --- a/plugins/c9.vfs.standalone/www/ide.html +++ b/plugins/c9.vfs.standalone/www/ide.html @@ -148,7 +148,7 @@ app.lut[(options.packagePath || "").replace(/^.*\/home\/.c9\//, "")] = options; }); - app.on("ready", function(){ + app.on("ready", function() { if (app.services.configure) app.services.configure.services = app.services; @@ -165,9 +165,10 @@ }); // For Development only - function done(){ + function done() { var vfs = app.services.vfs; var c9 = app.services.c9; + var settings = app.services.settings; c9.ready(); c9.totalLoadTime = Date.now() - start; @@ -175,13 +176,13 @@ console.warn("Total Load Time: ", Date.now() - start); if (window.hideLoader) { - if (vfs.connected) - window.hideLoader(); - else { - vfs.once("connect", function(){ - window.hideLoader(); - }); - } + var waitVfs = function(fn) { + vfs.connected ? fn() : vfs.once("connect", fn); + }; + var waitSettings = function(fn) { + settings.inited ? fn() : settings.once("read", fn); + }; + waitVfs(waitSettings.bind(null, window.hideLoader)); } } }, function loadError(mod) {