Skip to content

Commit

Permalink
Don't save UI options to BS options
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane Osbourne committed Jan 26, 2015
1 parent bd26694 commit 55b6cad
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 55 deletions.
37 changes: 37 additions & 0 deletions lib/UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,43 @@ UI.prototype.getServer = function (cb) {
});
};

/**
* @param name
* @param value
* @returns {Map|*}
*/
UI.prototype.setOption = function (name, value) {
var ui = this;
ui.options = ui.options.set(name, value);
return ui.options;
};

/**
* @param path
* @param value
* @returns {Map|*}
*/
UI.prototype.setOptionIn = function (path, value) {
this.options = this.options.setIn(path, value);
return this.options;
};

/**
* @param fn
*/
UI.prototype.setMany = function (fn) {
this.options = this.options.withMutations(fn);
return this.options;
};

/**
* @param path
* @returns {any|*}
*/
UI.prototype.getOptionIn = function (path) {
return this.options.getIn(path);
};

/**
* Run each setup task in sequence
* @param ui
Expand Down
11 changes: 6 additions & 5 deletions lib/plugins/remote-debug/remote-debug.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@
*/
function RemoteDebugController(options, Socket, pagesConfig) {

var ctrl = this;
ctrl.options = options.bs;
var ctrl = this;
ctrl.options = options.bs;
ctrl.uiOptions = options.ui;
ctrl.section = pagesConfig[SECTION_NAME];
ctrl.items = [
{
name: "weinre",
title: "Remote Debugger (weinre)",
item: ctrl.options["weinre"],
item: ctrl.uiOptions["weinre"],
tagline: "",
hidden: "<a href=\"%s\" target=\"_blank\">Access remote debugger (opens in a new tab)</a></p>".replace("%s", ctrl.options["weinre"].clientUrl)
hidden: "<a href=\"%s\" target=\"_blank\">Access remote debugger (opens in a new tab)</a></p>".replace("%s", ctrl.uiOptions["weinre"].clientUrl)
},
{
name: "pesticide",
name: "pesticide",
title: "CSS Outlining",
item: ctrl.options["clientFiles"]["pesticide"],
tagline: "Add simple CSS outlines to all elements. (powered by <a href=\"http://pesticide.io\">Pesticide.io</a>)"
Expand Down
74 changes: 37 additions & 37 deletions lib/plugins/remote-debug/remote-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ var clientScripts = Immutable.Map();
*/
module.exports = {
/**
* @param cp
* @param ui
* @param bs
*/
"plugin": function (cp, bs) {
"plugin": function (ui, bs) {

var hostUrl = getHostUrl(cp, bs);
var hostUrl = getHostUrl(ui, bs);

weinreTargetUrl.hostname = hostUrl.hostname;
weinreClientUrl.hostname = hostUrl.hostname;

bs.setOption(WEINRE_NAME, Immutable.fromJS({
ui.setOption(WEINRE_NAME, Immutable.fromJS({
name: WEINRE_NAME,
active: false,
url: false,
Expand All @@ -57,15 +57,15 @@ module.exports = {
port: WEINRE_PORT
}));

cp.socket.on("connection", function (client) {
client.on("cp:weinre:toggle", toggleWeinre.bind(null, cp.socket, cp.clients, cp, bs));
client.on("cp:clientfile:enable", enableClientFile.bind(null, cp));
client.on("cp:clientfile:disable", disableClientFile.bind(null, cp.clients, cp, bs));
ui.socket.on("connection", function (client) {
client.on("cp:weinre:toggle", toggleWeinre.bind(null, ui.socket, ui.clients, ui, bs));
client.on("cp:clientfile:enable", enableClientFile.bind(null, ui));
client.on("cp:clientfile:disable", disableClientFile.bind(null, ui.clients, ui, bs));
});

cp.clients.on("connection", function (client) {
ui.clients.on("connection", function (client) {
clientScripts.map(function (item) {
cp.addElement(client, item);
ui.addElement(client, item);
});
});
},
Expand Down Expand Up @@ -96,11 +96,11 @@ module.exports = {

/**
* Get a suitable host URL for weinre
* @param cp
* @param ui
* @param bs
* @returns {*}
*/
function getHostUrl(cp, bs) {
function getHostUrl(ui, bs) {

var url = bs.getOptionIn(["urls", "external"]);

Expand All @@ -114,50 +114,50 @@ function getHostUrl(cp, bs) {
/**
* If it's snippet mode, create a url that contains host
* @param targetPath
* @param cp
* @param ui
* @param bs
*/
function getRemoteUrl(targetPath, cp, bs) {
function getRemoteUrl(targetPath, ui, bs) {
if (bs.options.get("mode") === "snippet") {
return ["//", getHostUrl(cp, bs).host, targetPath].join("");
return ["//", getHostUrl(ui, bs).host, targetPath].join("");
}
return targetPath;
}

/**
* @param cp
* @param ui
* @param file
*/
function enableClientFile (cp, file) {
clientScripts = cp.enableElement(clientScripts, file);
function enableClientFile (ui, file) {
clientScripts = ui.enableElement(clientScripts, file);
}

/**
*
*/
function disableClientFile (clients, cp, bs, file) {
clientScripts = cp.disableElement(clientScripts, file);
function disableClientFile (clients, ui, bs, file) {
clientScripts = ui.disableElement(clientScripts, file);
}

/**
* @param socket
* @param clients
* @param cp
* @param ui
* @param bs
* @param value
*/
function toggleWeinre (socket, clients, cp, bs, value) {
function toggleWeinre (socket, clients, ui, bs, value) {

if (value !== true) {
value = false;
}

if (value) {

var _debugger = enableWeinre(cp, bs);
var _debugger = enableWeinre(ui, bs);

// set the state of weinre
bs.setMany(function (item) {
ui.setMany(function (item) {
item.setIn([WEINRE_NAME, "active"], true);
item.setIn([WEINRE_NAME, "url"], _debugger.url);
item.setIn([WEINRE_NAME, "active"], true);
Expand All @@ -167,25 +167,25 @@ function toggleWeinre (socket, clients, cp, bs, value) {
socket.emit("cp:weinre:enabled", _debugger);
var fileitem = {
type: "js",
src: bs.getOptionIn([WEINRE_NAME, "targetUrl"]),
src: ui.getOptionIn([WEINRE_NAME, "targetUrl"]),
id: WEINRE_ELEM_ID
};

// Add the element to all clients
cp.addElement(clients, fileitem);
ui.addElement(clients, fileitem);

// Save for page refreshes
clientScripts = clientScripts.set("weinre", fileitem);

} else {

// Stop it
disableWeinre(cp, bs);
disableWeinre(ui, bs);

clientScripts = clientScripts.remove("weinre");

// Reset the state
bs.setOptionIn([WEINRE_NAME, "active"], false, {silent: false}); // Force a reload here
ui.setOptionIn([WEINRE_NAME, "active"], false, {silent: false}); // Force a reload here

// Let the UI know
socket.emit("cp:weinre:disabled");
Expand All @@ -198,27 +198,27 @@ function toggleWeinre (socket, clients, cp, bs, value) {

/**
* Enable the debugger
* @param cp
* @param ui
* @param bs
* @returns {{url: string, port: number}}
*/
function enableWeinre (cp, bs) {
function enableWeinre (ui, bs) {

if (weinreApp) {
weinreApp.close();
weinreApp = false;
}

var port = bs.options.getIn([WEINRE_NAME, "port"]);
var port = ui.getOptionIn([WEINRE_NAME, "port"]);

var logger = require(path.resolve(__dirname, "../../../node_modules", "weinre", "lib", "utils.js"));

logger.log = function (message) {
cp.logger.debug("[weinre]: %s", message);
ui.logger.debug("[weinre]: %s", message);
};

var weinre = require("weinre");
var external = getHostUrl(cp, bs);
var external = getHostUrl(ui, bs);

weinreApp = weinre.run({
httpPort: port,
Expand All @@ -228,18 +228,18 @@ function enableWeinre (cp, bs) {
readTimeout: 5,
deathTimeout: 15 });

return bs.options.get(WEINRE_NAME).toJS();
return ui.options.get(WEINRE_NAME).toJS();
}

/**
* @param cp
* @param ui
* @param bs
* @returns {any|*}
*/
function disableWeinre (cp, bs) {
function disableWeinre (ui, bs) {
if (weinreApp) {
weinreApp.close();
weinreApp = false;
}
return bs.options.get(WEINRE_NAME).toJS();
return ui.options.get(WEINRE_NAME).toJS();
}
23 changes: 11 additions & 12 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ var indexPage = publicFile(config.defaults.indexPage);
var css = publicFile(config.defaults.public.css);
var header = staticFile(config.defaults.components.header);
var footer = staticFile(config.defaults.components.footer);

var zlib = require('zlib');
var zlib = require("zlib");

/**
* CWD directory helper for static dir
Expand Down Expand Up @@ -88,7 +87,7 @@ function startServer(controlPanel, opts) {
.replace("%templates%", controlPanel.templates)
.replace("%svg%", svg)
.replace("%header%", header)
.replace(/%footer%/g, footer)
.replace(/%footer%/g, footer) // multiple footers
.replace("%appjs%", jsFilename)
.replace("%appcss%", cssFilename)
);
Expand Down Expand Up @@ -142,15 +141,15 @@ function insertPageMarkupFromHooks(app, pages, markup) {
app.use(function (req, res, next) {

if (req.url === "/" || pages[req.url.slice(1)]) {
res.writeHead(200, {'Content-Type': 'text/html', 'Content-Encoding': 'gzip'});
res.writeHead(200, {"Content-Type": "text/html", "Content-Encoding": "gzip"});
if (!cached) {
var buf = new Buffer(markup, 'utf-8'); // Choose encoding for the string.
zlib.gzip(buf, function (_, result) { // The callback will give you the
var buf = new Buffer(markup, "utf-8");
zlib.gzip(buf, function (_, result) {
cached = result;
res.end(result); // result, so just send it.
res.end(result);
});
} else {
res.end(cached); // result, so just send it.
res.end(cached);
}
} else {
next();
Expand All @@ -176,7 +175,7 @@ function serveFile(path, type, string) {

res.writeHead(200, {
"Content-Type": typemap[type],
"Content-Encoding": 'gzip',
"Content-Encoding": "gzip",
"Cache-Control": "public, max-age=2592000000",
"Expires": new Date(Date.now() + 2592000000).toUTCString()
});
Expand All @@ -189,7 +188,7 @@ function serveFile(path, type, string) {
gzipCache[path] = result;
res.end(result);
});
}
};
}

/**
Expand All @@ -215,8 +214,8 @@ function getClientJs (cp) {
* @param length
*/
function md5(src, length) {
var crypto = require('crypto');
var hash = crypto.createHash('md5').update(src, 'utf8').digest('hex');
var crypto = require("crypto");
var hash = crypto.createHash("md5").update(src, "utf8").digest("hex");
return hash.slice(0, length);
}

Expand Down
2 changes: 1 addition & 1 deletion public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ function OptionsService(Socket) {
all: function () {
return Socket.getData("options");
}
}
};
}
},{"../module":6}],13:[function(require,module,exports){
/**
Expand Down

0 comments on commit 55b6cad

Please sign in to comment.