Properly handle multiple data selections.
[dygraphs.git] / experimental / palette / index.js
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
29 var Index = {};
30
31 Index.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("&");
35 for (var i = 0;i < vars.length; i++) {
36 var pair = vars[i].split("=");
37 args[pair[0]] = pair[1];
38 }
39 return args;
40 }
41
42 Index.draw = function(element, data, options) {
43 element.innerHTML = "";
44 element.removeAttribute("style");
45 var g = new Dygraph(
46 element,
47 data,
48 options
49 );
50
51 // These don't work yet.
52 g.updateOptions({
53 labelsDiv: 'status',
54 });
55 }
56
57 Index.addMessage = function(text) {
58 var messages = document.getElementById("messages");
59 messages.innerText = messages.innerText + text + "\n";
60 }
61
62 Index.start = function() {
63 var variables = Index.splitVariables();
64 var sampleName = variables["sample"];
65 if (!(sampleName)) {
66 sampleName = "interestingShapes";
67 }
68 var sampleIndex = Samples.indexOf(sampleName);
69 var sample = Samples.data[sampleIndex];
70 var data = sample.data;
71 var redraw = function() {
72 Index.draw(document.getElementById("graph"), data, palette.read());
73 }
74
75 var selector = document.getElementById("selector").getElementsByTagName("select")[0];
76 for (var idx in Samples.data) {
77 var entry = Samples.data[idx];
78 var option = document.createElement("option");
79 option.value = entry.id;
80 option.innerText = entry.title;
81 selector.appendChild(option);
82 }
83 selector.onchange = function() {
84 var id = selector.options[selector.selectedIndex].value;
85 var url = document.URL;
86 var qmIndex = url.indexOf("?");
87 if (qmIndex >= 0) {
88 url = url.substring(0, qmIndex);
89 }
90 url = url + "?sample=" + id;
91 for (var idx in variables) {
92 if (idx != "sample") {
93 url = url + "&" + idx + "=" + variables[idx];
94 }
95 }
96 window.location = url;
97 }
98 selector.selectedIndex = sampleIndex;
99 var palette = new Palette();
100 palette.create(document, document.getElementById("optionsPalette"));
101 palette.write(sample.options);
102 palette.onchange = redraw;
103 palette.filterBar.focus();
104 redraw();
105
106 for (var opt in Dygraph.OPTIONS_REFERENCE) {
107 if (!(opt in opts)) {
108 var entry = Dygraph.OPTIONS_REFERENCE[opt];
109 console.warn("missing option: " + opt + " of type " + entry.type);
110 }
111 }
112 }