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
, className
) {
35 var element
= document
.createElement(type
);
36 parentElement
.appendChild(element
);
38 element
.className
= className
;
43 Palette
.prototype.create
= function(document
, parentElement
) {
46 var table
= Palette
.createChild("div", parentElement
, "palette");
49 this.tooltip
= new Tooltip();
51 var row
= Palette
.createChild("div", table
, "header");
52 row
.style
.visibility
= "visible";
54 Palette
.createChild("span", row
).textContent
= "Filter:";
55 this.filterBar
= Palette
.createChild("input", Palette
.createChild("span", row
));
56 this.filterBar
.type
= "search";
57 this.filterBar
.onkeyup
= function() {
58 palette
.filter(palette
.filterBar
.value
)
60 this.filterBar
.onclick
= this.filterBar
.onkeyup
;
61 var go
= Palette
.createChild("button", Palette
.createChild("span", row
));
62 go
.textContent
= "Redraw"
63 go
.onclick
= function() {
68 var tmp
= Palette
.createChild("button", Palette
.createChild("span", row
));
69 tmp
.textContent
= "Copy"
70 tmp
.onclick
= function() {
71 var textarea
= new TextArea();
72 textarea
.show("header", "Now is the time for all good men\nto come to the aid of their country");
74 tmp
.style
.display
= "none";
76 for (var opt
in opts
) {
78 if (opts
.hasOwnProperty(opt
)) {
79 var type
= opts
[opt
].type
;
80 var isFunction
= type
.indexOf("function(") == 0;
81 var row
= Palette
.createChild("div", table
);
82 row
.onmouseover
= function(source
, title
, type
, body
, e
) {
84 palette
.tooltip
.show(source
, e
, title
, type
, body
);
86 } (row
, opt
, type
, Dygraph
.OPTIONS_REFERENCE
[opt
].description
);
87 row
.onmouseout
= function() { palette
.tooltip
.hide(); };
89 var div
= Palette
.createChild("span", row
, "name");
90 div
.textContent
= opt
;
92 var value
= Palette
.createChild("span", row
, "option");
95 var input
= Palette
.createChild("button", value
);
96 input
.onclick
= function(opt
, palette
) {
97 return function(event
) {
98 var entry
= palette
.model
[opt
];
99 var inputValue
= entry
.functionString
;
100 if (inputValue
== null || inputValue
.length
== 0) {
101 inputValue
= opts
[opt
].type
+ "{\n\n}";
103 var textarea
= new TextArea();
104 textarea
.show(opt
, inputValue
);
105 textarea
.okCallback
= function(value
) {
106 if (value
!= inputValue
) {
107 entry
.functionString
= value
;
108 entry
.input
.textContent
= value
? "defined" : "not defined";
115 var input
= Palette
.createChild("input", value
, "textInput");
116 if (type
== "boolean") {
118 input
.maxlength
= "5";
120 input
.onkeypress
= function(event
) {
121 var keycode
= event
.which
;
122 if (keycode
== 13 || keycode
== 8) {
129 this.model
[opt
] = { input
: input
, row
: row
};
132 throw "For option " + opt
+ ":" + err
;
138 // TODO: replace semicolon parsing with comma parsing, and supporting quotes.
139 Palette
.parseStringArray
= function(value
) {
140 if (value
== null || value
.length
== 0) {
143 return value
.split(";");
146 Palette
.parseBooleanArray
= function(value
) {
147 if (value
== null || value
.length
== 0) {
150 return value
.split(',').map(function(x
) { return x
.trim() == "true"; });
153 Palette
.parseFloatArray
= function(value
) {
154 if (value
== null || value
.length
== 0) {
157 return value
.split(',').map(function(x
) { return parseFloat(x
); });
160 Palette
.parseIntArray
= function(value
) {
161 if (value
== null || value
.length
== 0) {
164 return value
.split(',').map(function(x
) { return parseInt(x
); });
167 Palette
.prototype.read
= function() {
169 for (var opt
in this.model
) {
170 if (this.model
.hasOwnProperty(opt
)) {
171 var type
= opts
[opt
].type
;
172 var isFunction
= type
.indexOf("function(") == 0;
173 var input
= this.model
[opt
].input
;
174 var value
= isFunction
? this.model
[opt
].functionString
: input
.value
;
175 if (value
&& value
.length
!= 0) {
176 if (type
== "boolean") {
177 results
[opt
] = value
== "true";
178 } else if (type
== "int") {
179 results
[opt
] = parseInt(value
);
180 } else if (type
== "float") {
181 results
[opt
] = parseFloat(value
);
182 } else if (type
== "array<string>") {
183 results
[opt
] = Palette
.parseStringArray(value
);
184 } else if (type
== "array<float>") {
185 results
[opt
] = Palette
.parseFloatArray(value
);
186 } else if (type
== "array<boolean>") {
187 results
[opt
] = Palette
.parseBooleanArray(value
);
188 } else if (type
== "array<Date>") {
189 results
[opt
] = Palette
.parseIntArray(value
);
190 } else if (isFunction
) {
191 var localVariable
= null;
192 eval("localVariable = " + value
);
193 results
[opt
] = localVariable
;
195 results
[opt
] = value
;
204 * Write to input elements.
206 Palette
.prototype.write
= function(hash
) {
208 for (var opt
in this.model
) {
209 // && hash.hasOwnProperty(opt)
210 if (this.model
.hasOwnProperty(opt
)) {
211 var input
= this.model
[opt
].input
;
212 var type
= opts
[opt
].type
;
213 var value
= hash
[opt
];
214 if (type
== "array<string>") {
216 input
.value
= value
.join("; ");
218 } else if (type
.indexOf("array") == 0) {
220 input
.value
= value
.join(", ");
222 } else if (type
.indexOf("function(") == 0) {
223 input
.textContent
= value
? "defined" : "not defined";
224 this.model
[opt
].functionString
= value
? value
.toString() : null;
234 Palette
.prototype.filter
= function(pattern
) {
235 pattern
= pattern
.toLowerCase();
237 for (var opt
in this.model
) {
238 if (this.model
.hasOwnProperty(opt
)) {
239 var row
= this.model
[opt
].row
;
240 var matches
= opt
.toLowerCase().indexOf(pattern
) >= 0;
241 row
.style
.display
= matches
? "block" : "none";
243 row
.className
= even
? "even" : "odd";