65ef612749a55c7ef1b716b2071d12ed29e5a6c4
1 // Copyright (c) 2011 Google, Inc.
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:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
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
22 * @fileoverview Dygraphs options palette.
24 * @author konigsberg@google.com (Robert Konigsberg)
29 * scope is either "global", "series", "x", "y" or "y2".
31 function Palette(scope
) {
32 // Contains pair of "input" (the input object) and "row" (the parent row)
33 // Also contains functionString.
35 // This is meant to be overridden by a palette host.
36 this.onchange
= function() {};
41 Palette
.createChild
= function(type
, parentElement
, className
) {
42 var element
= document
.createElement(type
);
43 parentElement
.appendChild(element
);
45 element
.className
= className
;
50 Palette
.prototype.create
= function(parentElement
) {
53 var table
= Palette
.createChild("div", parentElement
[0], "palette");
57 this.tooltip
= new Tooltip();
60 var header
= Palette
.createChild("div", table
, "header");
61 header
.style
.visibility
= "visible";
63 // One row per option.
64 for (var opt
in opts
) {
66 if (opts
.hasOwnProperty(opt
)) {
67 var type
= opts
[opt
].type
;
69 var scope
= opts
[opt
].scope
|| [ "global" ]; // Scope can be empty, infer "global" only.
70 var valid
= scope
[0] == "*" || $.inArray(this.scope
, scope
) >= 0;
75 var isFunction
= type
.indexOf("function(") == 0;
76 var row
= Palette
.createChild("div", table
);
77 row
.onmouseover
= function(source
, title
, type
, body
) {
79 palette
.tooltip
.show(source
, title
, type
, body
);
81 } (row
, opt
, type
, Dygraph
.OPTIONS_REFERENCE
[opt
].description
);
82 row
.onmouseout
= function() { palette
.tooltip
.hide(); };
84 var div
= Palette
.createChild("span", row
, "name");
85 div
.textContent
= opt
;
87 var value
= Palette
.createChild("span", row
, "option");
90 var input
= Palette
.createChild("button", value
);
91 input
.onclick
= function(opt
, palette
) {
92 return function(event
) {
93 var entry
= palette
.model
[opt
];
94 var inputValue
= entry
.functionString
;
95 if (inputValue
== null || inputValue
.length
== 0) {
96 inputValue
= opts
[opt
].type
+ "{\n\n}";
98 var textarea
= new TextArea();
99 textarea
.show(opt
, inputValue
);
100 textarea
.okCallback
= function(value
) {
101 if (value
!= inputValue
) {
102 entry
.functionString
= value
;
103 entry
.input
.textContent
= value
? "defined" : "not defined";
109 } else if (type
== "boolean") {
110 var input
= Palette
.createChild("button", value
);
111 input
.onclick
= function(e
) {
113 if (btn
.value
== "none") {
114 Palette
.populateBooleanButton(btn
, "true");
115 } else if (btn
.value
== "true") {
116 Palette
.populateBooleanButton(btn
, "false");
118 Palette
.populateBooleanButton(btn
, "none");
123 var input
= Palette
.createChild("input", value
, "textInput");
125 input
.onkeypress
= function(event
) {
126 var keycode
= event
.which
;
127 if (keycode
== 13 || keycode
== 8) {
132 this.model
[opt
] = { input
: input
, row
: row
};
135 throw "For option " + opt
+ ":" + err
;
141 // TODO: replace semicolon parsing with comma parsing, and supporting quotes.
142 Palette
.parseStringArray
= function(value
) {
143 if (value
== null || value
.length
== 0) {
146 return value
.split(";");
149 Palette
.parseBooleanArray
= function(value
) {
150 if (value
== null || value
.length
== 0) {
153 return value
.split(',').map(function(x
) { return x
.trim() == "true"; });
156 Palette
.parseFloatArray
= function(value
) {
157 if (value
== null || value
.length
== 0) {
160 return value
.split(',').map(function(x
) { return parseFloat(x
); });
163 Palette
.parseIntArray
= function(value
) {
164 if (value
== null || value
.length
== 0) {
167 return value
.split(',').map(function(x
) { return parseInt(x
); });
170 Palette
.prototype.read
= function() {
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
;
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;
183 if (value
== "true") {
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
;
206 results
[opt
] = value
;
215 * Write to input elements.
217 Palette
.prototype.write
= function(hash
) {
219 for (var opt
in this.model
) {
220 if (this.model
.hasOwnProperty(opt
)) {
221 var input
= this.model
[opt
].input
;
222 var type
= opts
[opt
].type
;
223 var value
= hash
[opt
];
224 if (type
== "boolean") {
225 var text
= value
== true ? "true" : (value
== false ? "false" : "none");
226 Palette
.populateBooleanButton(input
, text
);
227 } else if (type
== "array<string>") {
229 input
.value
= value
.join("; ");
231 } else if (type
.indexOf("array") == 0) {
233 input
.value
= value
.join(", ");
235 } else if (type
.indexOf("function(") == 0) {
236 input
.textContent
= value
? "defined" : "not defined";
237 this.model
[opt
].functionString
= value
? value
.toString() : null;
239 if (value
!= undefined
) {
247 Palette
.populateBooleanButton
= function(button
, value
) {
248 button
.innerHTML
= value
;
249 button
.value
= value
;
252 Palette
.prototype.filter
= function(pattern
) {
253 pattern
= pattern
.toLowerCase();
255 for (var opt
in this.model
) {
256 if (this.model
.hasOwnProperty(opt
)) {
257 var row
= this.model
[opt
].row
;
258 var matches
= opt
.toLowerCase().indexOf(pattern
) >= 0;
259 row
.style
.display
= matches
? "block" : "none";
261 row
.className
= even
? "even" : "odd";