Moved tooltip to its own file.
[dygraphs.git] / experimental / palette / palette.js
1 // Copyright (c) 2011 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 Dygraphs options palette.
23 *
24 * @author konigsberg@google.com (Robert Konigsberg)
25 */
26 "use strict";
27
28 function Palette() {
29 this.model = {};
30 this.onchange = function() {};
31 this.filterBar = null;
32 }
33
34 Palette.createChild = function(type, parentElement) {
35 var element = document.createElement(type);
36 parentElement.appendChild(element);
37 return element;
38 };
39
40 Palette.prototype.create = function(document, parentElement) {
41 var palette = this;
42
43 var table = Palette.createChild("div", parentElement);
44 table.className = "palette";
45 table.width="300px";
46
47 this.tooltip = new Tooltip();
48
49 var row = Palette.createChild("div", table);
50 row.style.visibility = "visible";
51 row.className = "header";
52
53 Palette.createChild("span", row).innerText = "Filter:";
54 this.filterBar = Palette.createChild("input", Palette.createChild("span", row));
55 this.filterBar.type = "search";
56 this.filterBar.onkeyup = function() {
57 palette.filter(palette.filterBar.value)
58 };
59 this.filterBar.onclick = this.filterBar.onkeyup;
60 var go = document.createElement("button");
61 Palette.createChild("span", row).appendChild(go);
62 go.innerText = "Redraw"
63 go.onclick = function() {
64 palette.onchange();
65 };
66
67 for (var opt in opts) {
68 try {
69 if (opts.hasOwnProperty(opt)) {
70 var type = opts[opt].type;
71 var isFunction = type.indexOf("function(") == 0;
72 var row = Palette.createChild("div", table);
73 row.onmouseover = function(source, title, type, body, e) {
74 return function(e) {
75 palette.tooltip.show(source, e, title, type, body);
76 };
77 } (row, opt, type, Dygraph.OPTIONS_REFERENCE[opt].description);
78 row.onmouseout = function() { palette.tooltip.hide(); };
79
80 var div = Palette.createChild("span", row);
81 div.innerText = opt;
82 div.className = "name";
83
84 var value = Palette.createChild("span", row);
85 value.className = "option";
86
87 if (isFunction) {
88 var input = Palette.createChild("button", value);
89 input.onclick = function(opt, palette) {
90 return function(event) {
91 var entry = palette.model[opt];
92 var inputValue = entry.functionString;
93 if (inputValue == null || inputValue.length == 0) {
94 inputValue = opts[opt].type + "{ }";
95 }
96 var value = prompt("enter function", inputValue);
97 if (value != null) {
98 if (value.length == 0) {
99 value = null;
100 }
101 if (value != inputValue) {
102 entry.functionString = value;
103 entry.input.innerText = value ? "defined" : "not defined";
104 palette.onchange();
105 }
106 }
107 }
108 }(opt, this);
109 } else {
110 var input = Palette.createChild("input", value);
111 input.onkeypress = function(event) {
112 var keycode = event.which;
113 if (keycode == 13 || keycode == 8) {
114 palette.onchange();
115 }
116 }
117
118 input.type="text";
119 }
120 this.model[opt] = { input: input, row: row };
121 }
122 } catch(err) {
123 throw "For option " + opt + ":" + err;
124 }
125 }
126 this.filter("");
127 }
128
129 // TODO: replace semicolon parsing with comma parsing, and supporting quotes.
130 Palette.parseStringArray = function(value) {
131 if (value == null || value.length == 0) {
132 return null;
133 }
134 return value.split(";");
135 }
136
137 Palette.parseBooleanArray = function(value) {
138 if (value == null || value.length == 0) {
139 return null;
140 }
141 return value.split(',').map(function(x) { return x.trim() == "true"; });
142 }
143
144 Palette.parseFloatArray = function(value) {
145 if (value == null || value.length == 0) {
146 return null;
147 }
148 return value.split(',').map(function(x) { return parseFloat(x); });
149 }
150
151 Palette.parseIntArray = function(value) {
152 if (value == null || value.length == 0) {
153 return null;
154 }
155 return value.split(',').map(function(x) { return parseInt(x); });
156 }
157
158 Palette.prototype.read = function() {
159 var results = {};
160 for (var opt in this.model) {
161 if (this.model.hasOwnProperty(opt)) {
162 var type = opts[opt].type;
163 var isFunction = type.indexOf("function(") == 0;
164 var input = this.model[opt].input;
165 var value = isFunction ? this.model[opt].functionString : input.value;
166 if (value && value.length != 0) {
167 if (type == "boolean") {
168 results[opt] = value == "true";
169 } else if (type == "int") {
170 results[opt] = parseInt(value);
171 } else if (type == "float") {
172 results[opt] = parseFloat(value);
173 } else if (type == "array<string>") {
174 results[opt] = Palette.parseStringArray(value);
175 } else if (type == "array<float>") {
176 results[opt] = Palette.parseFloatArray(value);
177 } else if (type == "array<boolean>") {
178 results[opt] = Palette.parseBooleanArray(value);
179 } else if (type == "array<Date>") {
180 results[opt] = Palette.parseIntArray(value);
181 } else if (isFunction) {
182 var localVariable = null;
183 eval("localVariable = " + value);
184 results[opt] = localVariable;
185 } else {
186 results[opt] = value;
187 }
188 }
189 }
190 }
191 return results;
192 }
193
194 /**
195 * Write to input elements.
196 */
197 Palette.prototype.write = function(hash) {
198 var results = {};
199 for (var opt in this.model) {
200 // && hash.hasOwnProperty(opt)
201 if (this.model.hasOwnProperty(opt)) {
202 var input = this.model[opt].input;
203 var type = opts[opt].type;
204 var value = hash[opt];
205 if (type == "array<string>") {
206 if (value) {
207 input.value = value.join("; ");
208 }
209 } else if (type.indexOf("array") == 0) {
210 if (value) {
211 input.value = value.join(", ");
212 }
213 } else if (type.indexOf("function(") == 0) {
214 input.innerText = value ? "defined" : "not defined";
215 this.model[opt].functionString = value ? value.toString() : null;
216 } else {
217 if (value) {
218 input.value = value;
219 }
220 }
221 }
222 }
223 }
224
225 Palette.prototype.filter = function(pattern) {
226 pattern = pattern.toLowerCase();
227 var even = true;
228 for (var opt in this.model) {
229 if (this.model.hasOwnProperty(opt)) {
230 var row = this.model[opt].row;
231 var matches = opt.toLowerCase().indexOf(pattern) >= 0;
232 row.style.display = matches ? "block" : "none";
233 if (matches) {
234 row.className = even ? "even" : "odd";
235 even = !even;
236 }
237 }
238 }
239 }