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 Multiple Dygraphs palettes, grouped by global, series, etc..
24 * @author konigsberg@google.com (Robert Konigsberg)
27 function MultiPalette() {
30 this.filterBar
= null;
31 // This is meant to be overridden by a palette host.
32 this.activePalette
= null;
33 this.onchange
= function() {};
37 MultiPalette
.optionSetValues
= {
44 MultiPalette
.prototype.create
= function(parentElement
) {
47 this.root
= $("<div>").addClass("palette").appendTo(parentElement
);
48 var header
= $("<div>").addClass("header").appendTo(this.root
);
49 // Selector for series and axes.
50 var selectorRow
= $("<div>").appendTo(header
);
51 var optionSelector
= $("<select>")
53 self
.activate(optionSelector
.val());
57 .append($("<span>").text("Option Set:"))
58 .append(optionSelector
)
63 .css("float", "right")
64 .css("padding-right", "8px")
65 .click(function() { self
.showHash(); })));
67 var filter
= function() {
68 $.each(self
.palettes
, function(key
, value
) {
69 value
.filter(self
.filterBar
.val());
73 this.filterBar
= $("<input>", { type
: "search" })
77 header
.append($("<div>")
78 .append($("<span>").text("Filter:"))
79 .append($("<span>").append(this.filterBar
))
84 .css("float", "right")
85 .css("padding-right", "8px")
86 .click(function() { self
.onchange(); }))));
88 $.each(MultiPalette
.optionSetValues
, function(key
, value
) {
90 .append($("<option></option>")
93 var palette
= new Palette(key
);
94 palette
.create(self
.root
);
95 palette
.root
.style
.display
= "none";
96 palette
.onchange
= function() {
99 self
.palettes
[key
] = palette
;
102 this.activate("global");
105 MultiPalette
.prototype.activate
= function(key
) {
106 if (this.activePalette
) {
107 this.activePalette
.root
.style
.display
= "none";
109 this.activePalette
= this.palettes
[key
];
110 this.activePalette
.root
.style
.display
= "block";
113 MultiPalette
.prototype.showHash
= function() {
114 var hash
= this.read();
115 var textarea
= new TextArea();
116 textarea
.cancel
.style
.display
= "none";
119 * JSON.stringify isn't built to be nice to functions. The following fixes
122 * First, val.toString only does part of the work, turning it into
123 * "function () {\n alert(\"p-click!\");\n}",
125 * {start,end}Marker make the surrounding quotes easy to find, and then
126 * remove them. It also converts the instances of \n and \" so the
132 var startMarker
= "<~%!<";
133 var endMarker
= ">!%~>";
134 var replacer
= function(key
, val
) {
135 if (typeof val
=== 'function') {
136 return startMarker
+ val
.toString() + endMarker
;
140 var text
= JSON
.stringify(hash
, replacer
, 2);
143 var start
= text
.indexOf(startMarker
);
144 var end
= text
.indexOf(endMarker
);
148 var substring
= text
.substring(start
+ startMarker
.length
, end
);
149 while(substring
.indexOf("\\n") >= 0) {
150 substring
= substring
.replace("\\n", "\n");
152 while(substring
.indexOf("\\\"") >= 0) {
153 substring
= substring
.replace("\\\"", "\"");
155 text
= text
.substring(0, start
- 1)
157 + text
.substring(end
+ endMarker
.length
+ 1);
159 textarea
.show("options", text
);
165 MultiPalette
.prototype.read
= function() {
166 var results
= this.palettes
.global
.read();
168 var clearIfEmpty
= function(hash
, key
) {
170 if ($.isEmptyObject(val
)) {
174 results
.axes
.x
= this.palettes
.x
.read();
175 results
.axes
.y
= this.palettes
.y
.read();
176 results
.axes
.y2
= this.palettes
.y2
.read();
177 clearIfEmpty(results
.axes
, "x");
178 clearIfEmpty(results
.axes
, "y");
179 clearIfEmpty(results
.axes
, "y2");
180 clearIfEmpty(results
, "axes");
185 * Write to palette from hash.
187 MultiPalette
.prototype.write
= function(hash
) {
188 this.palettes
.global
.write(hash
);
189 if (hash
.hasOwnProperty("axes")) {
190 var axes
= hash
.axes
;
191 this.palettes
.x
.write(axes
["x"]);
192 this.palettes
.y
.write(axes
["y"]);
193 this.palettes
.y2
.write(axes
["y2"]);