| 1 |
// Copyright 2007 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
|
| 16 |
/** |
| 17 |
* @fileoverview Thin wrappers around the DOM element returned from |
| 18 |
* the different draw methods of the graphics. This is the SVG implementation. |
| 19 |
* @author [email protected] (Erik Arvidsson) |
| 20 |
*/ |
| 21 |
|
| 22 |
goog.provide('goog.graphics.SvgEllipseElement'); |
| 23 |
goog.provide('goog.graphics.SvgGroupElement'); |
| 24 |
goog.provide('goog.graphics.SvgImageElement'); |
| 25 |
goog.provide('goog.graphics.SvgPathElement'); |
| 26 |
goog.provide('goog.graphics.SvgRectElement'); |
| 27 |
goog.provide('goog.graphics.SvgTextElement'); |
| 28 |
|
| 29 |
|
| 30 |
goog.require('goog.dom'); |
| 31 |
goog.require('goog.graphics.EllipseElement'); |
| 32 |
goog.require('goog.graphics.GroupElement'); |
| 33 |
goog.require('goog.graphics.ImageElement'); |
| 34 |
goog.require('goog.graphics.PathElement'); |
| 35 |
goog.require('goog.graphics.RectElement'); |
| 36 |
goog.require('goog.graphics.TextElement'); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* Thin wrapper for SVG group elements. |
| 42 |
* You should not construct objects from this constructor. The graphics |
| 43 |
* will return the object for you. |
| 44 |
* @param {Element} element The DOM element to wrap. |
| 45 |
* @param {goog.graphics.SvgGraphics} graphics The graphics creating |
| 46 |
* this element. |
| 47 |
* @constructor |
| 48 |
* @extends {goog.graphics.GroupElement} |
| 49 |
* @deprecated goog.graphics is deprecated. It existed to abstract over browser |
| 50 |
* differences before the canvas tag was widely supported. See |
| 51 |
* http://en.wikipedia.org/wiki/Canvas_element for details. |
| 52 |
* @final |
| 53 |
*/ |
| 54 |
goog.graphics.SvgGroupElement = function(element, graphics) { |
| 55 |
goog.graphics.GroupElement.call(this, element, graphics); |
| 56 |
}; |
| 57 |
goog.inherits(goog.graphics.SvgGroupElement, goog.graphics.GroupElement); |
| 58 |
|
| 59 |
|
| 60 |
/** |
| 61 |
* Remove all drawing elements from the group. |
| 62 |
* @override |
| 63 |
*/ |
| 64 |
goog.graphics.SvgGroupElement.prototype.clear = function() { |
| 65 |
goog.dom.removeChildren(this.getElement()); |
| 66 |
}; |
| 67 |
|
| 68 |
|
| 69 |
/** |
| 70 |
* Set the size of the group element. |
| 71 |
* @param {number|string} width The width of the group element. |
| 72 |
* @param {number|string} height The height of the group element. |
| 73 |
* @override |
| 74 |
*/ |
| 75 |
goog.graphics.SvgGroupElement.prototype.setSize = function(width, height) { |
| 76 |
this.getGraphics().setElementAttributes( |
| 77 |
this.getElement(), {'width': width, 'height': height}); |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
/** |
| 83 |
* Thin wrapper for SVG ellipse elements. |
| 84 |
* This is an implementation of the goog.graphics.EllipseElement interface. |
| 85 |
* You should not construct objects from this constructor. The graphics |
| 86 |
* will return the object for you. |
| 87 |
* @param {Element} element The DOM element to wrap. |
| 88 |
* @param {goog.graphics.SvgGraphics} graphics The graphics creating |
| 89 |
* this element. |
| 90 |
* @param {goog.graphics.Stroke?} stroke The stroke to use for this element. |
| 91 |
* @param {goog.graphics.Fill?} fill The fill to use for this element. |
| 92 |
* @constructor |
| 93 |
* @extends {goog.graphics.EllipseElement} |
| 94 |
* @final |
| 95 |
*/ |
| 96 |
goog.graphics.SvgEllipseElement = function(element, graphics, stroke, fill) { |
| 97 |
goog.graphics.EllipseElement.call(this, element, graphics, stroke, fill); |
| 98 |
}; |
| 99 |
goog.inherits(goog.graphics.SvgEllipseElement, goog.graphics.EllipseElement); |
| 100 |
|
| 101 |
|
| 102 |
/** |
| 103 |
* Update the center point of the ellipse. |
| 104 |
* @param {number} cx Center X coordinate. |
| 105 |
* @param {number} cy Center Y coordinate. |
| 106 |
* @override |
| 107 |
*/ |
| 108 |
goog.graphics.SvgEllipseElement.prototype.setCenter = function(cx, cy) { |
| 109 |
this.getGraphics().setElementAttributes( |
| 110 |
this.getElement(), {'cx': cx, 'cy': cy}); |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
/** |
| 115 |
* Update the radius of the ellipse. |
| 116 |
* @param {number} rx Radius length for the x-axis. |
| 117 |
* @param {number} ry Radius length for the y-axis. |
| 118 |
* @override |
| 119 |
*/ |
| 120 |
goog.graphics.SvgEllipseElement.prototype.setRadius = function(rx, ry) { |
| 121 |
this.getGraphics().setElementAttributes( |
| 122 |
this.getElement(), {'rx': rx, 'ry': ry}); |
| 123 |
}; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
/** |
| 128 |
* Thin wrapper for SVG rectangle elements. |
| 129 |
* This is an implementation of the goog.graphics.RectElement interface. |
| 130 |
* You should not construct objects from this constructor. The graphics |
| 131 |
* will return the object for you. |
| 132 |
* @param {Element} element The DOM element to wrap. |
| 133 |
* @param {goog.graphics.SvgGraphics} graphics The graphics creating |
| 134 |
* this element. |
| 135 |
* @param {goog.graphics.Stroke?} stroke The stroke to use for this element. |
| 136 |
* @param {goog.graphics.Fill?} fill The fill to use for this element. |
| 137 |
* @constructor |
| 138 |
* @extends {goog.graphics.RectElement} |
| 139 |
* @final |
| 140 |
*/ |
| 141 |
goog.graphics.SvgRectElement = function(element, graphics, stroke, fill) { |
| 142 |
goog.graphics.RectElement.call(this, element, graphics, stroke, fill); |
| 143 |
}; |
| 144 |
goog.inherits(goog.graphics.SvgRectElement, goog.graphics.RectElement); |
| 145 |
|
| 146 |
|
| 147 |
/** |
| 148 |
* Update the position of the rectangle. |
| 149 |
* @param {number} x X coordinate (left). |
| 150 |
* @param {number} y Y coordinate (top). |
| 151 |
* @override |
| 152 |
*/ |
| 153 |
goog.graphics.SvgRectElement.prototype.setPosition = function(x, y) { |
| 154 |
this.getGraphics().setElementAttributes(this.getElement(), {'x': x, 'y': y}); |
| 155 |
}; |
| 156 |
|
| 157 |
|
| 158 |
/** |
| 159 |
* Update the size of the rectangle. |
| 160 |
* @param {number} width Width of rectangle. |
| 161 |
* @param {number} height Height of rectangle. |
| 162 |
* @override |
| 163 |
*/ |
| 164 |
goog.graphics.SvgRectElement.prototype.setSize = function(width, height) { |
| 165 |
this.getGraphics().setElementAttributes( |
| 166 |
this.getElement(), {'width': width, 'height': height}); |
| 167 |
}; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
/** |
| 172 |
* Thin wrapper for SVG path elements. |
| 173 |
* This is an implementation of the goog.graphics.PathElement interface. |
| 174 |
* You should not construct objects from this constructor. The graphics |
| 175 |
* will return the object for you. |
| 176 |
* @param {Element} element The DOM element to wrap. |
| 177 |
* @param {goog.graphics.SvgGraphics} graphics The graphics creating |
| 178 |
* this element. |
| 179 |
* @param {goog.graphics.Stroke?} stroke The stroke to use for this element. |
| 180 |
* @param {goog.graphics.Fill?} fill The fill to use for this element. |
| 181 |
* @constructor |
| 182 |
* @extends {goog.graphics.PathElement} |
| 183 |
* @final |
| 184 |
*/ |
| 185 |
goog.graphics.SvgPathElement = function(element, graphics, stroke, fill) { |
| 186 |
goog.graphics.PathElement.call(this, element, graphics, stroke, fill); |
| 187 |
}; |
| 188 |
goog.inherits(goog.graphics.SvgPathElement, goog.graphics.PathElement); |
| 189 |
|
| 190 |
|
| 191 |
/** |
| 192 |
* Update the underlying path. |
| 193 |
* @param {!goog.graphics.Path} path The path object to draw. |
| 194 |
* @override |
| 195 |
*/ |
| 196 |
goog.graphics.SvgPathElement.prototype.setPath = function(path) { |
| 197 |
/** @suppress {missingRequire} goog.graphics.SvgGraphics */ |
| 198 |
this.getGraphics().setElementAttributes( |
| 199 |
this.getElement(), {'d': goog.graphics.SvgGraphics.getSvgPath(path)}); |
| 200 |
}; |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
/** |
| 205 |
* Thin wrapper for SVG text elements. |
| 206 |
* This is an implementation of the goog.graphics.TextElement interface. |
| 207 |
* You should not construct objects from this constructor. The graphics |
| 208 |
* will return the object for you. |
| 209 |
* @param {Element} element The DOM element to wrap. |
| 210 |
* @param {goog.graphics.SvgGraphics} graphics The graphics creating |
| 211 |
* this element. |
| 212 |
* @param {goog.graphics.Stroke?} stroke The stroke to use for this element. |
| 213 |
* @param {goog.graphics.Fill?} fill The fill to use for this element. |
| 214 |
* @constructor |
| 215 |
* @extends {goog.graphics.TextElement} |
| 216 |
* @final |
| 217 |
*/ |
| 218 |
goog.graphics.SvgTextElement = function(element, graphics, stroke, fill) { |
| 219 |
goog.graphics.TextElement.call(this, element, graphics, stroke, fill); |
| 220 |
}; |
| 221 |
goog.inherits(goog.graphics.SvgTextElement, goog.graphics.TextElement); |
| 222 |
|
| 223 |
|
| 224 |
/** |
| 225 |
* Update the displayed text of the element. |
| 226 |
* @param {string} text The text to draw. |
| 227 |
* @override |
| 228 |
*/ |
| 229 |
goog.graphics.SvgTextElement.prototype.setText = function(text) { |
| 230 |
// This is actually SVGTextElement but we don't have it in externs. |
| 231 |
/** @type {!Text} */ (this.getElement().firstChild).data = text; |
| 232 |
}; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
/** |
| 237 |
* Thin wrapper for SVG image elements. |
| 238 |
* This is an implementation of the goog.graphics.ImageElement interface. |
| 239 |
* You should not construct objects from this constructor. The graphics |
| 240 |
* will return the object for you. |
| 241 |
* @param {Element} element The DOM element to wrap. |
| 242 |
* @param {goog.graphics.SvgGraphics} graphics The graphics creating |
| 243 |
* this element. |
| 244 |
* @constructor |
| 245 |
* @extends {goog.graphics.ImageElement} |
| 246 |
* @final |
| 247 |
*/ |
| 248 |
goog.graphics.SvgImageElement = function(element, graphics) { |
| 249 |
goog.graphics.ImageElement.call(this, element, graphics); |
| 250 |
}; |
| 251 |
goog.inherits(goog.graphics.SvgImageElement, goog.graphics.ImageElement); |
| 252 |
|
| 253 |
|
| 254 |
/** |
| 255 |
* Update the position of the image. |
| 256 |
* @param {number} x X coordinate (left). |
| 257 |
* @param {number} y Y coordinate (top). |
| 258 |
* @override |
| 259 |
*/ |
| 260 |
goog.graphics.SvgImageElement.prototype.setPosition = function(x, y) { |
| 261 |
this.getGraphics().setElementAttributes(this.getElement(), {'x': x, 'y': y}); |
| 262 |
}; |
| 263 |
|
| 264 |
|
| 265 |
/** |
| 266 |
* Update the size of the image. |
| 267 |
* @param {number} width Width of image. |
| 268 |
* @param {number} height Height of image. |
| 269 |
* @override |
| 270 |
*/ |
| 271 |
goog.graphics.SvgImageElement.prototype.setSize = function(width, height) { |
| 272 |
this.getGraphics().setElementAttributes( |
| 273 |
this.getElement(), {'width': width, 'height': height}); |
| 274 |
}; |
| 275 |
|
| 276 |
|
| 277 |
/** |
| 278 |
* Update the source of the image. |
| 279 |
* @param {string} src Source of the image. |
| 280 |
* @override |
| 281 |
*/ |
| 282 |
goog.graphics.SvgImageElement.prototype.setSource = function(src) { |
| 283 |
this.getGraphics().setElementAttributes( |
| 284 |
this.getElement(), {'xlink:href': src}); |
| 285 |
}; |