SVGGraphic.js revision 7947db4b7d8682ea81598e3a4283e659a8103be6
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Graphic is a simple drawing api that allows for basic drawing operations.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @class Graphic
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @constructor
a89ad754cce3cfc8aee71760e10217b54020360dTripp SVGGraphic.superclass.constructor.apply(this, arguments);
a89ad754cce3cfc8aee71760e10217b54020360dTripp * Key value pairs in which a shape instance is associated with its id.
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @attribute 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.
7947db4b7d8682ea81598e3a4283e659a8103be6Tripp * @attribute contentBounds
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @type Object
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @readOnly
a89ad754cce3cfc8aee71760e10217b54020360dTripp getter: function()
a89ad754cce3cfc8aee71760e10217b54020360dTripp * The html element that represents to coordinate system of the Graphic instance.
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @attribute coordPlaneNode
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @type HTMLElement
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @readOnly
a89ad754cce3cfc8aee71760e10217b54020360dTripp getter: function()
a89ad754cce3cfc8aee71760e10217b54020360dTripp * Indicates the pointer-events setting for the svg:svg element.
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @attribute pointerEvents
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @type String
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Gets the current position of the node's parentNode in page coordinates.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method getXY
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @return Array The XY position of the shape.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp getXY: function()
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Indicates whether or not the instance will automatically redraw after a change is made to a shape.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * This property will get set to false when batching operations.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property autoDraw
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type Boolean
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @default true
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Initializes the class.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method initializer
a89ad754cce3cfc8aee71760e10217b54020360dTripp initializer: function() {
7947db4b7d8682ea81598e3a4283e659a8103be6Tripp this._coordPlaneNode = Y.one(DOCUMENT.createElement('div'));
7947db4b7d8682ea81598e3a4283e659a8103be6Tripp this._coordPlaneNode.setStyle("position", "absolute");
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Removes all nodes.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method destroy
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp destroy: function()
a89ad754cce3cfc8aee71760e10217b54020360dTripp if(this._coordPlaneNode && this._coordPlaneNode.parentNode)
a89ad754cce3cfc8aee71760e10217b54020360dTripp this._coordPlaneNode.parentNode.removeChild(this._coordPlaneNode);
a89ad754cce3cfc8aee71760e10217b54020360dTripp * Removes all shape instances from the dom.
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @method removeAllShapes
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Removes all child nodes.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method _removeChildren
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param {HTMLElement} node
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Toggles visibility
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method _toggleVisible
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param {HTMLElement} node element to toggle
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param {Boolean} val indicates visibilitye
7947db4b7d8682ea81598e3a4283e659a8103be6Tripp this._coordPlaneNode.setStyle("visibility", visibility);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Clears the graphics object.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method clear
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp clear: function() {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Sets the size of the graphics object.
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @method _updateContentSize
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param w {Number} width to set for the instance.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param h {Number} height to set for the instance.
a89ad754cce3cfc8aee71760e10217b54020360dTripp _updateContentSize: function(w, h) {
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;
a89ad754cce3cfc8aee71760e10217b54020360dTripp * Creates a contentNode element
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method _createGraphics
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp _createGraphics: function() {
a89ad754cce3cfc8aee71760e10217b54020360dTripp contentNode.setAttribute("pointer-events", pointerEvents);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Creates a graphic node
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method _createGraphicNode
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param {String} type node type to create
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param {String} pe specified pointer-events value
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @return HTMLElement
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp var node = document.createElementNS("http://www.w3.org/2000/svg", "svg:" + type),
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if(type !== "defs" && type !== "stop" && type !== "linearGradient" && type != "radialGradient")
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Adds a shape instance to the graphic instance.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method addShape
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
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @param {Shape|String}
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Generates a shape instance by type.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method getShape
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param {String} type type of shape to generate.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param {Object} cfg attributes for the shape
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @return Shape
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * When overflow is set to true, by default, the viewBox will resize to greater values but not values. (for performance)
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * When resizing the viewBox down is desirable, set the resizeDown value to true.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property resizeDown
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type Boolean
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.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp this.autoDraw = false;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp this._frag = null;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp this.autoDraw = true;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Updates the size of the graphics container and the position of its children.
a89ad754cce3cfc8aee71760e10217b54020360dTripp * @method updateContentBox
a89ad754cce3cfc8aee71760e10217b54020360dTripp queue = this.resizeDown ? this._shapes : this._redrawQueue,
a89ad754cce3cfc8aee71760e10217b54020360dTripp this._contentNode.setAttribute("viewBox", "" + box.left + " " + box.top + " " + box.width + " " + box.height + "");
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Adds a shape to the redraw queue.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method addToRedrawQueue
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param shape {SVGShape}
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Returns a reference to a gradient definition based on an id and type.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method getGradientNode
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @key {String} id that references the gradient definition
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type {String} description of the gradient type
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @return HTMLElement
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if(gradients.hasOwnProperty(key) && gradients[key].tagName.indexOf(type) > -1)