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)
30 this.onchange
= function() {};
31 this.filterBar
= null;
34 Palette
.createChild
= function(type
, parentElement
) {
35 var element
= document
.createElement(type
);
36 parentElement
.appendChild(element
);
40 Palette
.prototype.create
= function(document
, parentElement
) {
43 var table
= Palette
.createChild("div", parentElement
);
44 table
.className
= "palette";
47 this.tooltip
= new Tooltip();
49 var row
= Palette
.createChild("div", table
);
50 row
.style
.visibility
= "visible";
51 row
.className
= "header";
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
)
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() {
67 for (var opt
in opts
) {
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
) {
75 palette
.tooltip
.show(source
, e
, title
, type
, body
);
77 } (row
, opt
, type
, Dygraph
.OPTIONS_REFERENCE
[opt
].description
);
78 row
.onmouseout
= function() { palette
.tooltip
.hide(); };
80 var div
= Palette
.createChild("span", row
);
82 div
.className
= "name";
84 var value
= Palette
.createChild("span", row
);
85 value
.className
= "option";
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
+ "{ }";
96 var value
= prompt("enter function", inputValue
);
98 if (value
.length
== 0) {
101 if (value
!= inputValue
) {
102 entry
.functionString
= value
;
103 entry
.input
.innerText
= value
? "defined" : "not defined";
110 var input
= Palette
.createChild("input", value
);
111 input
.onkeypress
= function(event
) {
112 var keycode
= event
.which
;
113 if (keycode
== 13 || keycode
== 8) {
120 this.model
[opt
] = { input
: input
, row
: row
};
123 throw "For option " + opt
+ ":" + err
;
129 // TODO: replace semicolon parsing with comma parsing, and supporting quotes.
130 Palette
.parseStringArray
= function(value
) {
131 if (value
== null || value
.length
== 0) {
134 return value
.split(";");
137 Palette
.parseBooleanArray
= function(value
) {
138 if (value
== null || value
.length
== 0) {
141 return value
.split(',').map(function(x
) { return x
.trim() == "true"; });
144 Palette
.parseFloatArray
= function(value
) {
145 if (value
== null || value
.length
== 0) {
148 return value
.split(',').map(function(x
) { return parseFloat(x
); });
151 Palette
.parseIntArray
= function(value
) {
152 if (value
== null || value
.length
== 0) {
155 return value
.split(',').map(function(x
) { return parseInt(x
); });
158 Palette
.prototype.read
= function() {
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
;
186 results
[opt
] = value
;
195 * Write to input elements.
197 Palette
.prototype.write
= function(hash
) {
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>") {
207 input
.value
= value
.join("; ");
209 } else if (type
.indexOf("array") == 0) {
211 input
.value
= value
.join(", ");
213 } else if (type
.indexOf("function(") == 0) {
214 input
.innerText
= value
? "defined" : "not defined";
215 this.model
[opt
].functionString
= value
? value
.toString() : null;
225 Palette
.prototype.filter
= function(pattern
) {
226 pattern
= pattern
.toLowerCase();
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";
234 row
.className
= even
? "even" : "odd";