Skip to content

Commit

Permalink
#137, #139, GUI: Update configuration file format to spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajvincent committed Dec 1, 2017
1 parent 7d0893c commit 76b4326
Show file tree
Hide file tree
Showing 14 changed files with 398 additions and 441 deletions.
21 changes: 13 additions & 8 deletions docs/gui/DistortionsGUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const DistortionsGUI = window.DistortionsGUI = {
for (let prop in rulesMap) {
if (rulesMap[prop] instanceof DistortionsRules)
data.rules[prop] = rulesMap[prop].configurationAsJSON();
else if (prop in ["source", "sourceGraphIndex"])
else if (prop === "source")
data[prop] = rulesMap[prop];
else
data.rules[prop] = rulesMap[prop];
Expand All @@ -135,7 +135,8 @@ const DistortionsGUI = window.DistortionsGUI = {
// XXX ajvincent Need to let the GUI know this value name is taken
return;

const valueFromSource = graph.valueGetterEditor.getValue();
let valueFromSource = graph.valueGetterEditor.getValue();

await DistortionsManager.BlobLoader.addNamedValue(valueName, valueFromSource);

const panel = document.createElement("section");
Expand All @@ -152,12 +153,16 @@ const DistortionsGUI = window.DistortionsGUI = {

const value = DistortionsManager.BlobLoader.valuesByName.get(panel.dataset.valueName);
const rules = this.buildDistortions(panel, value);
DistortionsManager.valueNameToRulesMap.set(
panel.dataset.hash, {
"value": rules,
"source": valueFromSource,
}
);
const distortionsSet = {
"about": {
"valueName": valueName,
"isFunction": (typeof value === "function"),
"getExample": valueFromSource.split("\n").slice(1, -2).join("\n"),
},
"value": rules,
};
DistortionsManager.valueNameToRulesMap.set(panel.dataset.hash, distortionsSet);
graph.distortionMaps.push(distortionsSet);

OuterGridManager.panels.appendChild(panel);

Expand Down
5 changes: 1 addition & 4 deletions docs/gui/DistortionsRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,8 @@ DistortionsRules.prototype = {
throw new Error("Not implemented!");
},

configurationAsJSON: function() {
exportJSON: function() {
const rv = {
formatVersion: "0.8.2",
dataVersion: "0.1",

/*
filterOwnKeys: [] || null,
inheritOwnKeys: false,
Expand Down
6 changes: 3 additions & 3 deletions docs/gui/HandlerNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ const HandlerNames = window.HandlerNames = {
* @param config {JSONObject} The configuration.
*/
importConfig: function(config) {
for (let i = 0; i < config.graphNames.length; i++) {
this.setRow(i, config.graphNames[i], config.graphSymbolLists.includes(i));
for (let i = 0; i < config.graphs.length; i++) {
this.setRow(i, config.graphs[i].name, config.graphs[i].isSymbol);
}

const range = document.createRange();
const delButton = this.grid.getElementsByTagName("button")[
config.graphNames.length
config.graphs.length
];
range.setEndBefore(this.grid.lastChild);
range.setStartAfter(delButton);
Expand Down
13 changes: 10 additions & 3 deletions docs/gui/OuterGridManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,15 @@ const OuterGridManager = window.OuterGridManager = {
// Update the cached configuration
{
const [graphNames, graphSymbolLists] = HandlerNames.serializableNames();
config.graphNames = graphNames;
config.graphSymbolLists = graphSymbolLists;
if (!Array.isArray(config.graphs)) {
config.graphs = [];
}
while (config.graphs.length < graphNames.length)
config.graphs.push({});
graphNames.forEach(function(name, index) {
config.graphs[index].name = name;
config.graphs[index].isSymbol = graphSymbolLists.includes(index);
});
}

// Define our object graph managers
Expand All @@ -126,8 +133,8 @@ const OuterGridManager = window.OuterGridManager = {
this.graphNamesCache.controllers.push(new ObjectGraphManager());
}
const controller = this.graphNamesCache.controllers[i];
controller.importJSON(config.graphs[i]);
controller.setGraphName(name);
controller.readDistortionsData(config.distortionsByGraph[i]);
}

const deadControllers = this.graphNamesCache.controllers.slice(names.length);
Expand Down
88 changes: 82 additions & 6 deletions docs/gui/mainPanels/graphs.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
function ObjectGraphManager() {
this.radioClass = `graphpanel-${ObjectGraphManager.instanceCount}`;
ObjectGraphManager.instanceCount++;
this.distortions = [];

this.distortionMaps = [];
this.passThroughEditor = null;
this.valueGetterEditor = null;

this.jsonBase = null;

this.buildUI();
}
ObjectGraphManager.instanceCount = 0;
Expand Down Expand Up @@ -32,7 +35,58 @@ ObjectGraphManager.prototype.buildUI = function() {
this.radio.addEventListener("change", this, true);
}

ObjectGraphManager.prototype.setGraphName = function(name) {
ObjectGraphManager.prototype.importJSON = function(data) {
this.jsonBase = {
"name": data.name,
"isSymbol": data.isSymbol
};
};

ObjectGraphManager.prototype.exportJSON = function(graphIndex) {
const rv = {
"name": this.jsonBase.name,
"isSymbol": this.jsonBase.isSymbol,
"passThroughSource": null,
"passThroughEnabled": null,
"primordialsPass": false,
"distortions": [],
};

if (this.passThroughEditor) {
let lines = this.passThroughEditor.getValue().split("\n");
lines = lines.slice(2, -6);
rv.passThroughSource = lines.join("\n");

rv.passThroughEnabled = this.passThroughCheckbox.checked;
rv.primordialsPass = this.primordialsCheckbox.checked;
}
else {
const config = MembranePanel.cachedConfig;
const lastGraph = Array.isArray(config.graphs) ? config.graphs[graphIndex] : null;
if (lastGraph) {
rv.passThroughSource = lastGraph.passThroughSource;
rv.passThroughEnabled = lastGraph.passThroughEnabled;
rv.primordialsPass = lastGraph.primordialsPass;
}
}

this.distortionMaps.forEach(function(dm) {
let d = {}, keys = Reflect.ownKeys(dm);
keys.forEach(function(key) {
if (key === "about") {
d[key] = dm[key];
return;
}
d[key] = dm[key].exportJSON();
});

rv.distortions.push(d);
});

return rv;
};

ObjectGraphManager.prototype.setGraphName = function(name, jsonName, isSymbol) {
this.groupLabel.firstChild.nodeValue = name;
if (!this.groupLabel.parentNode) {
OuterGridManager.addGraphUI(
Expand All @@ -41,10 +95,6 @@ ObjectGraphManager.prototype.setGraphName = function(name) {
}
};

ObjectGraphManager.prototype.readDistortionsData = function(data) {

};

ObjectGraphManager.prototype.remove = function() {

};
Expand Down Expand Up @@ -88,6 +138,32 @@ ObjectGraphManager.prototype.handlePassThrough = function(event) {
CodeMirrorManager.setEditorEnabled(this.passThroughEditor, checked);
};

ObjectGraphManager.prototype.getPassThrough = function(fullSource = false) {
if (!this.passThroughCheckbox.checked)
return null;

if (!this.passThroughEditor) {
const config = MembranePanel.cachedConfig;
if (Array.isArray(config.graphs)) {
let index = OuterGridManager.graphNamesCache.controllers.indexOf(this);
if (config.graphs[index])
return config.graphs[index].passThrough;
}
return null;
}

if (!fullSource) {
let lines = this.passThroughEditor.getValue().split("\n");
lines = lines.slice(2, -6);
return lines.join("\n");
}

let value = this.passThroughEditor.getValue();
value = value.substr(value.indexOf("("));
value = value.replace(/;\n$/, ",\n");
return value;
};

ObjectGraphManager.prototype.handleEvent = function(event) {
if (event.target === this.primordialsCheckbox)
return this.handlePrimordials(event);
Expand Down
88 changes: 24 additions & 64 deletions docs/gui/mainPanels/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ window.LoadPanel = {
}
},

validateDistortions:
function(instructions, index, graphIndex, graphsTotal) {
const errorPrefix = `config.distortionsByGraph[${graphIndex}][${index}]`;
validateDistortions: function(instructions, index, graphIndex) {
const errorPrefix = `config.graphs[${graphIndex}].distortions[${index}]`;
function requireType(field, type) {
if (typeof instructions[field] !== type)
throw new Error(`${errorPrefix}.${field} must be of type ${type}`);
Expand All @@ -49,22 +48,6 @@ window.LoadPanel = {
requireType("rules", "object");

// XXX ajvincent We're not going to attempt parsing instructions.source now.

{
const max = graphsTotal - 1;
if (!Number.isInteger(instructions.sourceGraphIndex) ||
(instructions.sourceGraphIndex < 0) ||
(instructions.sourceGraphIndex >= graphsTotal))
throw new Error(
`${errorPrefix}.sourceGraphIndex must be an integer from 0 to ${max}`
);
}

if (instructions.sourceGraphIndex === graphIndex)
throw new Error(
`${errorPrefix}.sourceGraphIndex cannot be the target graph index ${graphIndex}`
);

const rulesMembers = ["value"];
if (instructions.isFunction) {
/*
Expand Down Expand Up @@ -135,15 +118,14 @@ window.LoadPanel = {
}

var config = {
commonFiles: [],
passThrough: null,
graphNames: [],
graphSymbolLists: [],
distortionsByGraph: [],
"configurationSetup": {},
"membrane": {},
"graphs": []
};
if (!this.configFileInput.files.length && (
!this.testMode || !this.testMode.configSource))
return config;

try {
{
let p, jsonAsText;
Expand All @@ -161,55 +143,33 @@ window.LoadPanel = {

// Validate the configuration.
{
if (!Array.isArray(config.graphNames))
throw new Error("config.graphNames must be an array of strings");

if (!Array.isArray(config.graphSymbolLists) ||
!config.graphSymbolLists.every(function(key, index) {
let rv = (Number.isInteger(key) && (0 <= key) &&
(key < config.graphNames.length));
if (rv && (index > 0))
rv = key > config.graphSymbolLists[index - 1];
return rv;
}))
throw new Error(
"config.graphSymbolLists must be an ordered array of unique " +
"non-negative integers, each member of which is less than " +
"config.graphNames.length"
);
if (!Array.isArray(config.graphs))
throw new Error("config.graphs must be an array of objects");

let stringKeys = new Set();
config.graphNames.forEach((key, index) => {
if (typeof key !== "string")
throw new Error("config.graphNames must be an array of strings");
if (!(config.graphSymbolLists.includes(index))) {
if (stringKeys.has(key)) {
config.graphs.forEach((graph, graphIndex) => {
if (typeof graph.name !== "string")
throw new Error(`config.graphs[${graphIndex}].name must be a string`);
if (typeof graph.isSymbol !== "boolean")
throw new Error(`config.graphs[${graphIndex}].isSymbol must be a boolean`);
if (!graph.isSymbol) {
if (stringKeys.has(graph.name)) {
throw new Error(
`config.graphNames[${index}] = "${key}", ` +
"but this string name appears earlier in config.graphNames"
`config.graphs[${graphIndex}].name = "${graph.name}", ` +
"but this name appears earlier in config.graphs, and neither name is a symbol"
);
}
stringKeys.add(key);
stringKeys.add(graph.name);
}
});

if (!Array.isArray(config.distortionsByGraph) ||
(config.distortionsByGraph.length != config.graphNames.length) ||
!config.distortionsByGraph.every(Array.isArray))
{
throw new Error(
`config.distortionsByGraph must be an array with length ` +
config.graphNames.length + ` of arrays`
);
}
if (!Array.isArray(graph.distortions)) {
throw new Error(`config.graphs[${graphIndex}].distortions must be an array`);
}

config.distortionsByGraph.forEach(function(items, graphIndex) {
items.forEach(function(item, index) {
this.validateDistortions(
item, index, graphIndex, config.graphNames.length
);
graph.distortions.forEach(function(item, index) {
this.validateDistortions(item, index, graphIndex);
}, this);
}, this);
});
}

HandlerNames.importConfig(config);
Expand Down
10 changes: 7 additions & 3 deletions docs/gui/mainPanels/membrane.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ window.MembranePanel = {
return this.handlePassThrough(event);
},

getPassThrough: function() {
if (!this.passThroughCheckbox.checked)
return null;
getPassThrough: function(fullSource = false) {
if (!fullSource) {
let lines = this.passThroughEditor.getValue().split("\n");
lines = lines.slice(2, -6);
return lines.join("\n");
}

let value = this.passThroughEditor.getValue();
value = value.substr(value.indexOf("("));
value = value.replace(/;\n$/, ",\n");
Expand Down
Loading

0 comments on commit 76b4326

Please sign in to comment.