Skip to content

Commit

Permalink
Query string settings support. Closes @22
Browse files Browse the repository at this point in the history
  • Loading branch information
mon committed Oct 19, 2016
1 parent 3265fb9 commit 378e415
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ var defaults = {
## Settings object
See [HuesSettings.js](./src/js/HuesSettings.js#L29) for the possible options you can put into the `defaults` object.

## Query string
Any setting that can go in the `defaults` object can also be dynamically specified in the URL.
For example: http://0x40.mon.im/custom.html?packs=BIOS.zip,kitchen.zip&currentUI=v4.20

There are two special settings here:
* `firstSong` can just be written as `song`.
* Anything given as `packs` or `respacks` will be appended to the respacks specified in the `defaults` object, as opposed to overwriting them.

## Building
Install [Node.js](https://nodejs.org/en/), v5 preferred.
Install the required packages for the build:
Expand Down
40 changes: 40 additions & 0 deletions src/js/HuesSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const defaultSettings = {
// Location relative to root - where do the audio/zip workers live
// This is required because Web Workers need an absolute path
workersPath : "lib/workers/",
// ONLY USED FOR QUERY STRINGS this will be prepended to any respacks
// passed in as a ?packs=query
respackPath : "respacks/",
// Debugging var, for loading zips or not
load : true,
// Debug, play first song automatically?
Expand Down Expand Up @@ -85,6 +88,7 @@ const ephemeralSettings = [
"autoplay",
"overwriteLocal",
"respacks",
"respackPath",
"firstSong",
"firstImage",
"disableRemoteResources",
Expand Down Expand Up @@ -255,6 +259,42 @@ class HuesSettings {
}

this.defaults = defaults;

// Override with our query string
let querySettings = this.getQuerySettings();
this.defaults.respacks = this.defaults.respacks.concat(querySettings.respacks);
for(let attr in querySettings) {
if(querySettings.hasOwnProperty(attr) && attr != "respacks") {
this.defaults[attr] = querySettings[attr];

if(ephemeralSettings.indexOf(attr) == -1) {
// TODO: Everything that checks localStorage for settings should
// change to get() to preserve local changes
localStorage[attr] = querySettings[attr];
}
}
}
}

getQuerySettings() {
let results = {};
results.respacks = [];
let query = window.location.search.substring(1);
let vars = query.split("&");
for (let i=0;i<vars.length;i++) {
let pair = vars[i].split("=");
if(pair[0] == "packs" || pair[0] == "respacks"){
let packs = pair[1].split(",");
for(let j = 0; j < packs.length; j++) {
results.respacks.push(this.defaults.respackPath + packs[j]);
}
} else if(pair[0] == "song") { // alias for firstSong
results.firstSong = pair[1];
} else {
results[pair[0]] = pair[1];
}
}
return results;
}

initUI(huesWin) {
Expand Down

0 comments on commit 378e415

Please sign in to comment.