1 /** 2 * Constructs a new, empty panel with default properties. Panels, with the 3 * exception of the root panel, are not typically constructed directly; instead, 4 * they are added to an existing panel or mark via {@link pv.Mark#add}. 5 * 6 * @class Represents a container mark. Panels allow repeated or nested 7 * structures, commonly used in small multiple displays where a small 8 * visualization is tiled to facilitate comparison across one or more 9 * dimensions. Other types of visualizations may benefit from repeated and 10 * possibly overlapping structure as well, such as stacked area charts. Panels 11 * can also offset the position of marks to provide padding from surrounding 12 * content. 13 * 14 * <p>All Protovis displays have at least one panel; this is the root panel to 15 * which marks are rendered. The box model properties (four margins, width and 16 * height) are used to offset the positions of contained marks. The data 17 * property determines the panel count: a panel is generated once per associated 18 * datum. When nested panels are used, property functions can declare additional 19 * arguments to access the data associated with enclosing panels. 20 * 21 * <p>Panels can be rendered inline, facilitating the creation of sparklines. 22 * This allows designers to reuse browser layout features, such as text flow and 23 * tables; designers can also overlay HTML elements such as rich text and 24 * images. 25 * 26 * <p>All panels have a <tt>children</tt> array (possibly empty) containing the 27 * child marks in the order they were added. Panels also have a <tt>root</tt> 28 * field which points to the root (outermost) panel; the root panel's root field 29 * points to itself. 30 * 31 * <p>See also the <a href="../../api/">Protovis guide</a>. 32 * 33 * @extends pv.Bar 34 */ 35 pv.Panel = function() { 36 pv.Bar.call(this); 37 38 /** 39 * The child marks; zero or more {@link pv.Mark}s in the order they were 40 * added. 41 * 42 * @see #add 43 * @type pv.Mark[] 44 */ 45 this.children = []; 46 this.root = this; 47 48 /** 49 * The internal $dom field is set by the Protovis loader; see lang/init.js. It 50 * refers to the script element that contains the Protovis specification, so 51 * that the panel knows where in the DOM to insert the generated SVG element. 52 * 53 * @private 54 */ 55 this.$dom = pv.Panel.$dom; 56 }; 57 58 pv.Panel.prototype = pv.extend(pv.Bar) 59 .property("canvas"); 60 61 pv.Panel.prototype.type = "panel"; 62 63 /** 64 * The canvas element; either the string ID of the canvas element in the current 65 * document, or a reference to the canvas element itself. If null, a canvas 66 * element will be created and inserted into the document at the location of the 67 * script element containing the current Protovis specification. This property 68 * only applies to root panels and is ignored on nested panels. 69 * 70 * <p>Note: the "canvas" element here refers to a <tt>div</tt> (or other suitable 71 * HTML container element), <i>not</i> a <tt>canvas</tt> element. The name of 72 * this property is a historical anachronism from the first implementation that 73 * used HTML 5 canvas, rather than SVG. 74 * 75 * @type string 76 * @name pv.Panel.prototype.canvas 77 */ 78 79 /** 80 * Default properties for panels. By default, the margins are zero, the fill 81 * style is transparent. 82 * 83 * @type pv.Panel 84 */ 85 pv.Panel.prototype.defaults = new pv.Panel() 86 .extend(pv.Bar.prototype.defaults) 87 .fillStyle(null); 88 89 /** 90 * Returns an anchor with the specified name. This method is overridden since 91 * the behavior of Panel anchors is slightly different from normal anchors: 92 * adding to an anchor adds to the anchor target's, rather than the anchor 93 * target's parent. To avoid double margins, we override the anchor's proto so 94 * that the margins are zero. 95 * 96 * @param {string} name the anchor name; either a string or a property function. 97 * @returns {pv.Anchor} the new anchor. 98 */ 99 pv.Panel.prototype.anchor = function(name) { 100 101 /* A "view" of this panel whose margins appear to be zero. */ 102 function z() { return 0; } 103 z.prototype = this; 104 z.prototype.left = z.prototype.right = z.prototype.top = z.prototype.bottom = z; 105 106 var anchor = pv.Bar.prototype.anchor.call(new z(), name) 107 .data(function(d) { return [d]; }); 108 anchor.parent = this; 109 return anchor; 110 }; 111 112 /** 113 * Adds a new mark of the specified type to this panel. Unlike the normal 114 * {@link Mark#add} behavior, adding a mark to a panel does not cause the mark 115 * to inherit from the panel. Since the contained marks are offset by the panel 116 * margins already, inheriting properties is generally undesirable; of course, 117 * it is always possible to change this behavior by calling {@link Mark#extend} 118 * explicitly. 119 * 120 * @param {function} type the type of the new mark to add. 121 * @returns {pv.Mark} the new mark. 122 */ 123 pv.Panel.prototype.add = function(type) { 124 var child = new type(); 125 child.parent = this; 126 child.root = this.root; 127 child.childIndex = this.children.length; 128 this.children.push(child); 129 return child; 130 }; 131 132 /** @private TODO */ 133 pv.Panel.prototype.bind = function() { 134 pv.Mark.prototype.bind.call(this); 135 for (var i = 0; i < this.children.length; i++) { 136 this.children[i].bind(); 137 } 138 }; 139 140 /** 141 * @private Evaluates all of the properties for this panel for the specified 142 * instance <tt>s</tt> in the scene graph, including recursively building the 143 * scene graph for child marks. 144 * 145 * @param s a node in the scene graph; the instance of the panel to build. 146 * @see Mark#scene 147 */ 148 pv.Panel.prototype.buildInstance = function(s) { 149 pv.Bar.prototype.buildInstance.call(this, s); 150 if (!s.children) s.children = []; 151 152 /* 153 * Build each child, passing in the parent (this panel) scene graph node. The 154 * child mark's scene is initialized from the corresponding entry in the 155 * existing scene graph, such that properties from the previous build can be 156 * reused; this is largely to facilitate the recycling of SVG elements. 157 */ 158 for (var i = 0; i < this.children.length; i++) { 159 this.children[i].scene = s.children[i]; // possibly undefined 160 this.children[i].build(); 161 } 162 163 /* 164 * Once the child marks have been built, the new scene graph nodes are removed 165 * from the child marks and placed into the scene graph. The nodes cannot 166 * remain on the child nodes because this panel (or a parent panel) may be 167 * instantiated multiple times! 168 */ 169 for (var i = 0; i < this.children.length; i++) { 170 s.children[i] = this.children[i].scene; 171 delete this.children[i].scene; 172 } 173 174 /* Delete any expired child scenes, should child marks have been removed. */ 175 s.children.length = this.children.length; 176 }; 177 178 /** 179 * @private Computes the implied properties for this panel for the specified 180 * instance <tt>s</tt> in the scene graph. Panels have two implied 181 * properties:<ul> 182 * 183 * <li>The <tt>canvas</tt> property references the DOM element, typically a DIV, 184 * that contains the SVG element that is used to display the visualization. This 185 * property may be specified as a string, referring to the unique ID of the 186 * element in the DOM. The string is converted to a reference to the DOM 187 * element. The width and height of the SVG element is inferred from this DOM 188 * element. If no canvas property is specified, a new SVG element is created and 189 * inserted into the document, using the panel dimensions; see 190 * {@link #createCanvas}. 191 * 192 * <li>The <tt>children</tt> array, while not a property per se, contains the 193 * scene graph for each child mark. This array is initialized to be empty, and 194 * is populated above in {@link #buildInstance}. 195 * 196 * </ul>The current implementation creates the SVG element, if necessary, during 197 * the build phase; in the future, it may be preferrable to move this to the 198 * update phase, although then the canvas property would be undefined. In 199 * addition, DOM inspection is necessary to define the implied width and height 200 * properties that may be inferred from the DOM. 201 * 202 * @param s a node in the scene graph; the instance of the panel to build. 203 */ 204 pv.Panel.prototype.buildImplied = function(s) { 205 if (!this.parent) { 206 var c = s.canvas; 207 if (c) { 208 if (typeof c == "string") c = document.getElementById(c); 209 210 /* Clear the container if it's not associated with this panel. */ 211 if (c.$panel != this) { 212 c.$panel = this; 213 c.innerHTML = ""; 214 } 215 216 /* If width and height weren't specified, inspect the container. */ 217 var w, h; 218 if (s.width == null) { 219 w = parseFloat(pv.css(c, "width")); 220 s.width = w - s.left - s.right; 221 } 222 if (s.height == null) { 223 h = parseFloat(pv.css(c, "height")); 224 s.height = h - s.top - s.bottom; 225 } 226 } else if (s.$canvas) { 227 228 /* 229 * If the canvas property is null, and we previously created a canvas for 230 * this scene node, reuse the previous canvas rather than creating a new 231 * one. 232 */ 233 c = s.$canvas; 234 } else { 235 236 /** 237 * Returns the last element in the current document's body. The canvas 238 * element is appended to this last element if another DOM element has not 239 * already been specified via the <tt>$dom</tt> field. 240 */ 241 function lastElement() { 242 var node = document.body; 243 while (node.lastChild && node.lastChild.tagName) { 244 node = node.lastChild; 245 } 246 return (node == document.body) ? node : node.parentNode; 247 } 248 249 /* Insert a new container into the DOM. */ 250 c = s.$canvas = document.createElement("span"); 251 this.$dom // script element for text/javascript+protovis 252 ? this.$dom.parentNode.insertBefore(c, this.$dom) 253 : lastElement().appendChild(c); 254 } 255 s.canvas = c; 256 } 257 pv.Bar.prototype.buildImplied.call(this, s); 258 }; 259