SVGGraphic.js revision a75ebc38c1de401b679953a9b87bd323f0f48d02
0fdefaa9ca017edfb76b736c825b34186f33045aTripp * <a href="http://www.w3.org/TR/SVG/">SVG</a> implementation of the <a href="Graphic.html">`Graphic`</a> class.
0fdefaa9ca017edfb76b736c825b34186f33045aTripp * `SVGGraphic` is not intended to be used directly. Instead, use the <a href="Graphic.html">`Graphic`</a> class.
0fdefaa9ca017edfb76b736c825b34186f33045aTripp * If the browser has <a href="http://www.w3.org/TR/SVG/">SVG</a> capabilities, the <a href="Graphic.html">`Graphic`</a>
0fdefaa9ca017edfb76b736c825b34186f33045aTripp * class will point to the `SVGGraphic` class.
a75ebc38c1de401b679953a9b87bd323f0f48d02Tripp * @module graphics
66ca16dd76367c074fe4df1dcf7b555489a9bf85Tripp * @class SVGGraphic
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @constructor
a89ad754cce3cfc8aee71760e10217b54020360dTripp SVGGraphic.superclass.constructor.apply(this, arguments);
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * Whether or not to render the `Graphic` automatically after to a specified parent node after init. This can be a Node instance or a CSS selector string.
c093c1aed867e18aa4778708592e1ceb45d18cffTripp * @config render
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @type Node | String
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Unique id for class instance.
c093c1aed867e18aa4778708592e1ceb45d18cffTripp * @config id
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @type String
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp valueFn: function()
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp return Y.guid();
a89ad754cce3cfc8aee71760e10217b54020360dTripp * Key value pairs in which a shape instance is associated with its id.
c093c1aed867e18aa4778708592e1ceb45d18cffTripp * @config shapes
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @type Object
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @readOnly
a89ad754cce3cfc8aee71760e10217b54020360dTripp getter: function()
a89ad754cce3cfc8aee71760e10217b54020360dTripp return this._shapes;
a89ad754cce3cfc8aee71760e10217b54020360dTripp * Object containing size and coordinate data for the content of a Graphic in relation to the coordSpace node.
c093c1aed867e18aa4778708592e1ceb45d18cffTripp * @config contentBounds
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @type Object
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @readOnly
a89ad754cce3cfc8aee71760e10217b54020360dTripp getter: function()
a89ad754cce3cfc8aee71760e10217b54020360dTripp * The html element that represents to coordinate system of the Graphic instance.
c093c1aed867e18aa4778708592e1ceb45d18cffTripp * @config node
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @type HTMLElement
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @readOnly
a89ad754cce3cfc8aee71760e10217b54020360dTripp getter: function()
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp return this._node;
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * Indicates the width of the `Graphic`.
c093c1aed867e18aa4778708592e1ceb45d18cffTripp * @config width
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @type Number
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * Indicates the height of the `Graphic`.
c093c1aed867e18aa4778708592e1ceb45d18cffTripp * @config height
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @type Number
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Determines how the size of instance is calculated. If true, the width and height are determined by the size of the contents.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * If false, the width and height values are either explicitly set or determined by the size of the parent node's dimensions.
c093c1aed867e18aa4778708592e1ceb45d18cffTripp * @config autoSize
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @type Boolean
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @default false
bf801d6851ecf7ed14742ef3639a077daecb5cf8Tripp * The contentBounds will resize to greater values but not to smaller values. (for performance)
a4d2446149b07f9e5c32947091dcbcf4d2eee765Tripp * When resizing the contentBounds down is desirable, set the resizeDown value to true.
c093c1aed867e18aa4778708592e1ceb45d18cffTripp * @config resizeDown
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @type Boolean
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp getter: function()
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp return this._resizeDown;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Indicates the x-coordinate for the instance.
c093c1aed867e18aa4778708592e1ceb45d18cffTripp * @config x
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @type Number
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp getter: function()
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp return this._x;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Indicates the y-coordinate for the instance.
c093c1aed867e18aa4778708592e1ceb45d18cffTripp * @config y
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @type Number
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp getter: function()
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp return this._y;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Indicates whether or not the instance will automatically redraw after a change is made to a shape.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * This property will get set to false when batching operations.
c093c1aed867e18aa4778708592e1ceb45d18cffTripp * @config autoDraw
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @type Boolean
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @default true
a75ebc38c1de401b679953a9b87bd323f0f48d02Tripp // Indicates the pointer-events setting for the svg:svg element.
a75ebc38c1de401b679953a9b87bd323f0f48d02Tripp // @config pointerEvents
a75ebc38c1de401b679953a9b87bd323f0f48d02Tripp // @type String
cec703a844d9691646231634fe709f4ea41d278aTripp * Storage for `x` attribute.
cec703a844d9691646231634fe709f4ea41d278aTripp * @property _x
cec703a844d9691646231634fe709f4ea41d278aTripp * @type Number
cec703a844d9691646231634fe709f4ea41d278aTripp * Storage for `y` attribute.
cec703a844d9691646231634fe709f4ea41d278aTripp * @property _y
cec703a844d9691646231634fe709f4ea41d278aTripp * @type Number
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Gets the current position of the graphic instance in page coordinates.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method getXY
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @return Array The XY position of the shape.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp getXY: function()
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @property _resizeDown
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @type Boolean
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Initializes the class.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method initializer
a89ad754cce3cfc8aee71760e10217b54020360dTripp initializer: function() {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Adds the graphics node to the dom.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method render
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param {HTMLElement} parentNode node in which to render the graphics node into.
a89ad754cce3cfc8aee71760e10217b54020360dTripp w = this.get("width") || parseInt(parentNode.getComputedStyle("width"), 10),
a89ad754cce3cfc8aee71760e10217b54020360dTripp h = this.get("height") || parseInt(parentNode.getComputedStyle("height"), 10);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp return this;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Removes all nodes.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @method destroy
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp destroy: function()
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Generates a shape instance by type.
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @method addShape
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @param {Object} cfg attributes for the shape
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @return Shape
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Adds a shape instance to the graphic instance.
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @method _appendShape
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param {Shape} shape The shape instance to be added to the graphic.
a89ad754cce3cfc8aee71760e10217b54020360dTripp * Removes a shape instance from from the graphic instance.
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @method removeShape
66ca16dd76367c074fe4df1dcf7b555489a9bf85Tripp * @param {Shape|String} shape The instance or id of the shape to be removed.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Removes all shape instances from the dom.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @method removeAllShapes
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Removes all child nodes.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @method _removeChildren
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @param {HTMLElement} node
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Clears the graphics object.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @method clear
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp clear: function() {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Toggles visibility
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @method _toggleVisible
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @param {Boolean} val indicates visibilitye
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * Returns a shape class. Used by `addShape`.
cec703a844d9691646231634fe709f4ea41d278aTripp * @method _getShapeClass
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @param {Shape | String} val Indicates which shape class.
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @return Function
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * Look up for shape classes. Used by `addShape` to retrieve a class for instantiation.
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @property _shapeClass
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @type Object
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Returns a shape based on the id of its dom node.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method getShapeById
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param {String} id Dom id of the shape's node attribute.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @return Shape
9eaaa502227248d304ac9170902697d02158c1d9Tripp * Allows for creating multiple shapes in order to batch appending and redraw operations.
9eaaa502227248d304ac9170902697d02158c1d9Tripp * @method batch
9eaaa502227248d304ac9170902697d02158c1d9Tripp * @param {Function} method Method to execute.
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * Returns a document fragment to for attaching shapes.
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @method _getDocFrag
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @return DocumentFragment
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp return this._frag;
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * Redraws all shapes.
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @method _redraw
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp _redraw: function()
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp var box = this.get("resizeDown") ? this._getUpdatedContentBounds() : this._contentBounds;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp this._contentNode.setAttribute("viewBox", "" + box.left + " " + box.top + " " + box.width + " " + box.height + "");
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp this._frag = null;
0fdefaa9ca017edfb76b736c825b34186f33045aTripp * Adds a shape to the redraw queue and calculates the contentBounds. Used internally
0fdefaa9ca017edfb76b736c825b34186f33045aTripp * by `Shape` instances.
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @method addToRedrawQueue
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @param shape {SVGShape}
0fdefaa9ca017edfb76b736c825b34186f33045aTripp * @protected
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp box.left = box.left < shapeBox.left ? box.left : shapeBox.left;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp box.top = box.top < shapeBox.top ? box.top : shapeBox.top;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp box.right = box.right > shapeBox.right ? box.right : shapeBox.right;
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp box.bottom = box.bottom > shapeBox.bottom ? box.bottom : shapeBox.bottom;
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * Recalculates and returns the `contentBounds` for the `Graphic` instance.
e0caea9528bfbb244d27129aa9dea5aebc07fc18Tripp * @method _getUpdatedContentBounds
b57ff76ab2ce5f3017d61855f13ed04ab46a965cTripp * @return {Object}
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Creates a contentNode element
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @method _createGraphics
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp _createGraphics: function() {
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp contentNode.setAttribute("pointer-events", pointerEvents);
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * Creates a graphic node
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @method _createGraphicNode
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @param {String} type node type to create
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @param {String} pe specified pointer-events value
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp * @return HTMLElement
b79c07ef87dd1a48a03fc33a91c37d04f3addae2Tripp var node = DOCUMENT.createElementNS("http://www.w3.org/2000/svg", "svg:" + type),
c7ba96d16d58075a9ab8d5c1e46c6c83ce11cb4eTripp if(type !== "defs" && type !== "stop" && type !== "linearGradient" && type != "radialGradient")
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Returns a reference to a gradient definition based on an id and type.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method getGradientNode
66ca16dd76367c074fe4df1dcf7b555489a9bf85Tripp * @param {String} key id that references the gradient definition
66ca16dd76367c074fe4df1dcf7b555489a9bf85Tripp * @param {String} type description of the gradient type
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @return HTMLElement
0fdefaa9ca017edfb76b736c825b34186f33045aTripp * @protected
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if(gradients.hasOwnProperty(key) && gradients[key].tagName.indexOf(type) > -1)