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