X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=experimental%2Fpalette%2Findex.js;h=8aeced1e544494b19a50c6fc028920f5a4d34ea5;hb=7a6088783a85f965087c0551120c9612d58e832a;hp=74359c94ea8be9d8f6705c6fad7677aa0bf7a8d3;hpb=3dcf1f1f6ade5ff7465ebcffc52c1dd93f9d0b72;p=dygraphs.git diff --git a/experimental/palette/index.js b/experimental/palette/index.js index 74359c9..8aeced1 100644 --- a/experimental/palette/index.js +++ b/experimental/palette/index.js @@ -32,13 +32,21 @@ Index.splitVariables = function() { // http://www.idealog.us/2006/06/javascript_ var query = window.location.search.substring(1); var args = {}; var vars = query.split("&"); - for (var i = 0;i < vars.length; i++) { - var pair = vars[i].split("="); - args[pair[0]] = pair[1]; + for (var i = 0; i < vars.length; i++) { + if (vars[i].length > 0) { + var pair = vars[i].split("="); + args[pair[0]] = pair[1]; + } } return args; } +/** + * Draw the graph. + * @param {Object} element the display element + * @param {Object} data the data to be shown + * @param {Object} options the options hash. + */ Index.draw = function(element, data, options) { element.innerHTML = ""; element.removeAttribute("style"); @@ -56,28 +64,60 @@ Index.draw = function(element, data, options) { Index.addMessage = function(text) { var messages = document.getElementById("messages"); - messages.innerText = messages.innerText + text + "\n"; + messages.textContent = messages.textContent + text + "\n"; } +/** + * Start up the palette system. + */ Index.start = function() { var variables = Index.splitVariables(); - var sampleName = variables["sample"]; - if (!(sampleName)) { - sampleName = "interestingShapes"; - } - var sample = Samples[sampleName]; + var sampleName = variables["sample"] || "interestingShapes"; + var sampleIndex = Samples.indexOf(sampleName); + var sample = Samples.data[sampleIndex]; var data = sample.data; var redraw = function() { Index.draw(document.getElementById("graph"), data, palette.read()); } - var palette = new Palette(); - palette.create(document, document.getElementById("optionsPalette")); + // Selector is the drop-down for selecting a set of data. + + // Popupate the selector with the set of data samples + var selector = document.getElementById("selector").getElementsByTagName("select")[0]; + for (var idx in Samples.data) { + var entry = Samples.data[idx]; + var option = document.createElement("option"); + option.value = entry.id; + option.textContent = entry.title; + selector.appendChild(option); + } + selector.onchange = function() { + var id = selector.options[selector.selectedIndex].value; + var url = document.URL; + var qmIndex = url.indexOf("?"); + if (qmIndex >= 0) { + url = url.substring(0, qmIndex); + } + url = url + "?sample=" + id; + for (var idx in variables) { + if (idx != "sample") { + url = url + "&" + idx + "=" + variables[idx]; + } + } + window.location = url; + } + selector.selectedIndex = sampleIndex; + + // Palette contains the widget that builds options. + var palette = new MultiPalette(); + palette.create(document.getElementById("optionsPalette")); palette.write(sample.options); palette.onchange = redraw; palette.filterBar.focus(); + redraw(); + // Find all new options which we don't implement here in the palette. for (var opt in Dygraph.OPTIONS_REFERENCE) { if (!(opt in opts)) { var entry = Dygraph.OPTIONS_REFERENCE[opt];