Add series customization to the palette. Requires some bug fixes. Nice, nice.
[dygraphs.git] / experimental / palette / palette.js
CommitLineData
20590000
RK
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
58a18b02
RK
28/**
29 * scope is either "global", "series", "x", "y" or "y2".
30 */
31function Palette(scope) {
32 // Contains pair of "input" (the input object) and "row" (the parent row)
7a608878 33 // Also contains functionString.
20590000 34 this.model = {};
58a18b02 35 // This is meant to be overridden by a palette host.
20590000 36 this.onchange = function() {};
58a18b02
RK
37 this.scope = scope;
38 this.root = null;
20590000
RK
39}
40
909bae9b 41Palette.createChild = function(type, parentElement, className) {
59a80f4a
RK
42 var element = document.createElement(type);
43 parentElement.appendChild(element);
909bae9b
RK
44 if (className) {
45 element.className = className;
46 }
59a80f4a
RK
47 return element;
48};
49
58a18b02 50Palette.prototype.create = function(parentElement) {
20590000 51 var palette = this;
20590000 52
58a18b02
RK
53 var table = Palette.createChild("div", parentElement[0], "palette");
54 this.root = table;
59a80f4a 55 table.width="300px";
20590000 56
59a80f4a 57 this.tooltip = new Tooltip();
20590000 58
58a18b02
RK
59 // Build the header
60 var header = Palette.createChild("div", table, "header");
61 header.style.visibility = "visible";
20590000 62
ce50b6e8 63 // CURRENTLY HIDDEN.
58a18b02 64 var tmp = Palette.createChild("button", Palette.createChild("span", header));
21285cf9 65 tmp.textContent = "Copy"
d95dc014
RK
66 tmp.onclick = function() {
67 var textarea = new TextArea();
ce50b6e8 68 textarea.show("header", "Now is the time for all good men\nto come to the aid of their country");
d95dc014 69 };
ce50b6e8 70 tmp.style.display = "none";
d95dc014 71
58a18b02 72 // One row per option.
20590000
RK
73 for (var opt in opts) {
74 try {
75 if (opts.hasOwnProperty(opt)) {
76 var type = opts[opt].type;
58a18b02
RK
77
78 var scope = opts[opt].scope || [ "global" ]; // Scope can be empty, infer "global" only.
79 var valid = scope[0] == "*" || $.inArray(this.scope, scope) >= 0;
80 if (!valid) {
81 continue;
82 }
83
20590000 84 var isFunction = type.indexOf("function(") == 0;
59a80f4a 85 var row = Palette.createChild("div", table);
58a18b02
RK
86 row.onmouseover = function(source, title, type, body) {
87 return function() {
88 palette.tooltip.show(source, title, type, body);
59a80f4a
RK
89 };
90 } (row, opt, type, Dygraph.OPTIONS_REFERENCE[opt].description);
91 row.onmouseout = function() { palette.tooltip.hide(); };
92
909bae9b 93 var div = Palette.createChild("span", row, "name");
21285cf9 94 div.textContent = opt;
59a80f4a 95
909bae9b 96 var value = Palette.createChild("span", row, "option");
20590000
RK
97
98 if (isFunction) {
59a80f4a 99 var input = Palette.createChild("button", value);
20590000
RK
100 input.onclick = function(opt, palette) {
101 return function(event) {
102 var entry = palette.model[opt];
103 var inputValue = entry.functionString;
104 if (inputValue == null || inputValue.length == 0) {
d95dc014 105 inputValue = opts[opt].type + "{\n\n}";
20590000 106 }
24cfd518
RK
107 var textarea = new TextArea();
108 textarea.show(opt, inputValue);
109 textarea.okCallback = function(value) {
20590000
RK
110 if (value != inputValue) {
111 entry.functionString = value;
7a608878 112 entry.input.textContent = value ? "defined" : "not defined";
20590000
RK
113 palette.onchange();
114 }
115 }
116 }
117 }(opt, this);
7a608878
RK
118 } else if (type == "boolean") {
119 var input = Palette.createChild("button", value);
120 input.onclick = function(e) {
121 var btn = e.target;
122 if (btn.value == "none") {
123 Palette.populateBooleanButton(btn, "true");
124 } else if (btn.value == "true") {
125 Palette.populateBooleanButton(btn, "false");
126 } else {
127 Palette.populateBooleanButton(btn, "none");
128 }
129 palette.onchange();
130 };
20590000 131 } else {
21285cf9 132 var input = Palette.createChild("input", value, "textInput");
7a608878 133 input.type="text";
20590000
RK
134 input.onkeypress = function(event) {
135 var keycode = event.which;
136 if (keycode == 13 || keycode == 8) {
137 palette.onchange();
138 }
139 }
20590000
RK
140 }
141 this.model[opt] = { input: input, row: row };
142 }
143 } catch(err) {
144 throw "For option " + opt + ":" + err;
145 }
146 }
147 this.filter("");
148}
149
150// TODO: replace semicolon parsing with comma parsing, and supporting quotes.
151Palette.parseStringArray = function(value) {
152 if (value == null || value.length == 0) {
153 return null;
154 }
155 return value.split(";");
156}
157
158Palette.parseBooleanArray = function(value) {
159 if (value == null || value.length == 0) {
160 return null;
161 }
162 return value.split(',').map(function(x) { return x.trim() == "true"; });
163}
164
165Palette.parseFloatArray = function(value) {
166 if (value == null || value.length == 0) {
167 return null;
168 }
169 return value.split(',').map(function(x) { return parseFloat(x); });
170}
171
172Palette.parseIntArray = function(value) {
173 if (value == null || value.length == 0) {
174 return null;
175 }
176 return value.split(',').map(function(x) { return parseInt(x); });
177}
178
179Palette.prototype.read = function() {
180 var results = {};
181 for (var opt in this.model) {
182 if (this.model.hasOwnProperty(opt)) {
183 var type = opts[opt].type;
184 var isFunction = type.indexOf("function(") == 0;
185 var input = this.model[opt].input;
186 var value = isFunction ? this.model[opt].functionString : input.value;
187 if (value && value.length != 0) {
188 if (type == "boolean") {
7a608878
RK
189 if (value == "false") {
190 results[opt] = false;
191 }
192 if (value == "true") {
193 results[opt] = true;
194 }
195 // Ignore value == "none"
20590000
RK
196 } else if (type == "int") {
197 results[opt] = parseInt(value);
198 } else if (type == "float") {
199 results[opt] = parseFloat(value);
200 } else if (type == "array<string>") {
201 results[opt] = Palette.parseStringArray(value);
202 } else if (type == "array<float>") {
203 results[opt] = Palette.parseFloatArray(value);
204 } else if (type == "array<boolean>") {
205 results[opt] = Palette.parseBooleanArray(value);
206 } else if (type == "array<Date>") {
207 results[opt] = Palette.parseIntArray(value);
208 } else if (isFunction) {
209 var localVariable = null;
210 eval("localVariable = " + value);
211 results[opt] = localVariable;
212 } else {
213 results[opt] = value;
214 }
215 }
216 }
217 }
218 return results;
219}
220
221/**
222 * Write to input elements.
223 */
224Palette.prototype.write = function(hash) {
225 var results = {};
226 for (var opt in this.model) {
20590000
RK
227 if (this.model.hasOwnProperty(opt)) {
228 var input = this.model[opt].input;
229 var type = opts[opt].type;
230 var value = hash[opt];
7a608878
RK
231 if (type == "boolean") {
232 var text = value == true ? "true" : (value == false ? "false" : "none");
233 Palette.populateBooleanButton(input, text);
234 } else if (type == "array<string>") {
20590000
RK
235 if (value) {
236 input.value = value.join("; ");
237 }
238 } else if (type.indexOf("array") == 0) {
239 if (value) {
240 input.value = value.join(", ");
241 }
242 } else if (type.indexOf("function(") == 0) {
21285cf9 243 input.textContent = value ? "defined" : "not defined";
20590000
RK
244 this.model[opt].functionString = value ? value.toString() : null;
245 } else {
246 if (value) {
247 input.value = value;
248 }
249 }
250 }
251 }
252}
253
7a608878
RK
254Palette.populateBooleanButton = function(button, value) {
255 button.innerHTML = value;
256 button.value = value;
257}
258
20590000
RK
259Palette.prototype.filter = function(pattern) {
260 pattern = pattern.toLowerCase();
59a80f4a 261 var even = true;
20590000
RK
262 for (var opt in this.model) {
263 if (this.model.hasOwnProperty(opt)) {
264 var row = this.model[opt].row;
59a80f4a
RK
265 var matches = opt.toLowerCase().indexOf(pattern) >= 0;
266 row.style.display = matches ? "block" : "none";
267 if (matches) {
268 row.className = even ? "even" : "odd";
269 even = !even;
270 }
20590000
RK
271 }
272 }
273}