Bug in the function that splits variables.
[dygraphs.git] / experimental / palette / index.js
CommitLineData
20590000
RK
1// Copyright (c) 2012 Google, Inc.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21/**
22 * @fileoverview Javascript to run index.html.
23 *
24 * @author konigsberg@google.com (Robert Konigsberg)
25 */
26
27"use strict";
28
36d4fabf
RK
29var Index = {};
30
31Index.splitVariables = function() { // http://www.idealog.us/2006/06/javascript_to_p.html
32 var query = window.location.search.substring(1);
33 var args = {};
34 var vars = query.split("&");
3d8093fa
RK
35 for (var i = 0; i < vars.length; i++) {
36 if (vars[i].length > 0) {
37 var pair = vars[i].split("=");
38 args[pair[0]] = pair[1];
39 }
20590000 40 }
36d4fabf 41 return args;
20590000
RK
42}
43
36d4fabf
RK
44Index.draw = function(element, data, options) {
45 element.innerHTML = "";
46 element.removeAttribute("style");
47 var g = new Dygraph(
48 element,
49 data,
50 options
51 );
52
53 // These don't work yet.
54 g.updateOptions({
55 labelsDiv: 'status',
56 });
57}
58
59Index.addMessage = function(text) {
20590000
RK
60 var messages = document.getElementById("messages");
61 messages.innerText = messages.innerText + text + "\n";
62}
63
36d4fabf
RK
64Index.start = function() {
65 var variables = Index.splitVariables();
3dcf1f1f
RK
66 var sampleName = variables["sample"];
67 if (!(sampleName)) {
68 sampleName = "interestingShapes";
36d4fabf 69 }
2b3ebdd9
RK
70 var sampleIndex = Samples.indexOf(sampleName);
71 var sample = Samples.data[sampleIndex];
36d4fabf 72 var data = sample.data;
20590000 73 var redraw = function() {
36d4fabf 74 Index.draw(document.getElementById("graph"), data, palette.read());
20590000
RK
75 }
76
2b3ebdd9
RK
77 var selector = document.getElementById("selector").getElementsByTagName("select")[0];
78 for (var idx in Samples.data) {
79 var entry = Samples.data[idx];
80 var option = document.createElement("option");
81 option.value = entry.id;
82 option.innerText = entry.title;
83 selector.appendChild(option);
84 }
85 selector.onchange = function() {
86 var id = selector.options[selector.selectedIndex].value;
87 var url = document.URL;
88 var qmIndex = url.indexOf("?");
89 if (qmIndex >= 0) {
90 url = url.substring(0, qmIndex);
91 }
92 url = url + "?sample=" + id;
93 for (var idx in variables) {
94 if (idx != "sample") {
95 url = url + "&" + idx + "=" + variables[idx];
96 }
97 }
98 window.location = url;
99 }
100 selector.selectedIndex = sampleIndex;
20590000
RK
101 var palette = new Palette();
102 palette.create(document, document.getElementById("optionsPalette"));
36d4fabf 103 palette.write(sample.options);
20590000
RK
104 palette.onchange = redraw;
105 palette.filterBar.focus();
106 redraw();
107
108 for (var opt in Dygraph.OPTIONS_REFERENCE) {
109 if (!(opt in opts)) {
110 var entry = Dygraph.OPTIONS_REFERENCE[opt];
111 console.warn("missing option: " + opt + " of type " + entry.type);
112 }
113 }
114}