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
= Palette
.createChild("button", Palette
.createChild("span", row
));
61 go
.innerText
= "Redraw"
62 go
.onclick
= function() {
66 var tmp
= Palette
.createChild("button", Palette
.createChild("span", row
));
67 tmp
.innerText
= "Copy"
68 tmp
.onclick
= function() {
69 var textarea
= new TextArea();
70 textarea
.show("Now is the time for all good men\nto come to the aid of their country");
73 for (var opt
in opts
) {
75 if (opts
.hasOwnProperty(opt
)) {
76 var type
= opts
[opt
].type
;
77 var isFunction
= type
.indexOf("function(") == 0;
78 var row
= Palette
.createChild("div", table
);
79 row
.onmouseover
= function(source
, title
, type
, body
, e
) {
81 palette
.tooltip
.show(source
, e
, title
, type
, body
);
83 } (row
, opt
, type
, Dygraph
.OPTIONS_REFERENCE
[opt
].description
);
84 row
.onmouseout
= function() { palette
.tooltip
.hide(); };
86 var div
= Palette
.createChild("span", row
);
88 div
.className
= "name";
90 var value
= Palette
.createChild("span", row
);
91 value
.className
= "option";
94 var input
= Palette
.createChild("button", value
);
95 input
.onclick
= function(opt
, palette
) {
96 return function(event
) {
97 var entry
= palette
.model
[opt
];
98 var inputValue
= entry
.functionString
;
99 if (inputValue
== null || inputValue
.length
== 0) {
100 inputValue
= opts
[opt
].type
+ "{\n\n}";
102 var textarea
= new TextArea();
103 textarea
.show("enter function", inputValue
);
104 textarea
.okCallback
= function(value
) {
105 if (value
!= inputValue
) {
106 entry
.functionString
= value
;
107 entry
.input
.innerText
= value
? "defined" : "not defined";
114 var input
= Palette
.createChild("input", value
);
115 input
.onkeypress
= function(event
) {
116 var keycode
= event
.which
;
117 if (keycode
== 13 || keycode
== 8) {
124 this.model
[opt
] = { input
: input
, row
: row
};
127 throw "For option " + opt
+ ":" + err
;
133 // TODO: replace semicolon parsing with comma parsing, and supporting quotes.
134 Palette
.parseStringArray
= function(value
) {
135 if (value
== null || value
.length
== 0) {
138 return value
.split(";");
141 Palette
.parseBooleanArray
= function(value
) {
142 if (value
== null || value
.length
== 0) {
145 return value
.split(',').map(function(x
) { return x
.trim() == "true"; });
148 Palette
.parseFloatArray
= function(value
) {
149 if (value
== null || value
.length
== 0) {
152 return value
.split(',').map(function(x
) { return parseFloat(x
); });
155 Palette
.parseIntArray
= function(value
) {
156 if (value
== null || value
.length
== 0) {
159 return value
.split(',').map(function(x
) { return parseInt(x
); });
162 Palette
.prototype.read
= function() {
164 for (var opt
in this.model
) {
165 if (this.model
.hasOwnProperty(opt
)) {
166 var type
= opts
[opt
].type
;
167 var isFunction
= type
.indexOf("function(") == 0;
168 var input
= this.model
[opt
].input
;
169 var value
= isFunction
? this.model
[opt
].functionString
: input
.value
;
170 if (value
&& value
.length
!= 0) {
171 if (type
== "boolean") {
172 results
[opt
] = value
== "true";
173 } else if (type
== "int") {
174 results
[opt
] = parseInt(value
);
175 } else if (type
== "float") {
176 results
[opt
] = parseFloat(value
);
177 } else if (type
== "array<string>") {
178 results
[opt
] = Palette
.parseStringArray(value
);
179 } else if (type
== "array<float>") {
180 results
[opt
] = Palette
.parseFloatArray(value
);
181 } else if (type
== "array<boolean>") {
182 results
[opt
] = Palette
.parseBooleanArray(value
);
183 } else if (type
== "array<Date>") {
184 results
[opt
] = Palette
.parseIntArray(value
);
185 } else if (isFunction
) {
186 var localVariable
= null;
187 eval("localVariable = " + value
);
188 results
[opt
] = localVariable
;
190 results
[opt
] = value
;
199 * Write to input elements.
201 Palette
.prototype.write
= function(hash
) {
203 for (var opt
in this.model
) {
204 // && hash.hasOwnProperty(opt)
205 if (this.model
.hasOwnProperty(opt
)) {
206 var input
= this.model
[opt
].input
;
207 var type
= opts
[opt
].type
;
208 var value
= hash
[opt
];
209 if (type
== "array<string>") {
211 input
.value
= value
.join("; ");
213 } else if (type
.indexOf("array") == 0) {
215 input
.value
= value
.join(", ");
217 } else if (type
.indexOf("function(") == 0) {
218 input
.innerText
= value
? "defined" : "not defined";
219 this.model
[opt
].functionString
= value
? value
.toString() : null;
229 Palette
.prototype.filter
= function(pattern
) {
230 pattern
= pattern
.toLowerCase();
232 for (var opt
in this.model
) {
233 if (this.model
.hasOwnProperty(opt
)) {
234 var row
= this.model
[opt
].row
;
235 var matches
= opt
.toLowerCase().indexOf(pattern
) >= 0;
236 row
.style
.display
= matches
? "block" : "none";
238 row
.className
= even
? "even" : "odd";