API Docs for: 3.13.0
Show:

File: graphics/js/SVGShape.js

  1. /**
  2. * <a href="http://www.w3.org/TR/SVG/">SVG</a> implementation of the <a href="Shape.html">`Shape`</a> class.
  3. * `SVGShape` is not intended to be used directly. Instead, use the <a href="Shape.html">`Shape`</a> class.
  4. * If the browser has <a href="http://www.w3.org/TR/SVG/">SVG</a> capabilities, the <a href="Shape.html">`Shape`</a>
  5. * class will point to the `SVGShape` class.
  6. *
  7. * @module graphics
  8. * @class SVGShape
  9. * @constructor
  10. * @param {Object} cfg (optional) Attribute configs
  11. */
  12. SVGShape = function()
  13. {
  14. this._transforms = [];
  15. this.matrix = new Y.Matrix();
  16. this._normalizedMatrix = new Y.Matrix();
  17. SVGShape.superclass.constructor.apply(this, arguments);
  18. };
  19.  
  20. SVGShape.NAME = "shape";
  21.  
  22. Y.extend(SVGShape, Y.GraphicBase, Y.mix({
  23. /**
  24. * Storage for x attribute.
  25. *
  26. * @property _x
  27. * @protected
  28. */
  29. _x: 0,
  30.  
  31. /**
  32. * Storage for y attribute.
  33. *
  34. * @property _y
  35. * @protected
  36. */
  37. _y: 0,
  38.  
  39. /**
  40. * Init method, invoked during construction.
  41. * Calls `initializer` method.
  42. *
  43. * @method init
  44. * @protected
  45. */
  46. init: function()
  47. {
  48. this.initializer.apply(this, arguments);
  49. },
  50.  
  51. /**
  52. * Initializes the shape
  53. *
  54. * @private
  55. * @method initializer
  56. */
  57. initializer: function(cfg)
  58. {
  59. var host = this,
  60. graphic = cfg.graphic,
  61. data = this.get("data");
  62. host.createNode();
  63. if(graphic)
  64. {
  65. host._setGraphic(graphic);
  66. }
  67. if(data)
  68. {
  69. host._parsePathData(data);
  70. }
  71. host._updateHandler();
  72. },
  73.  
  74. /**
  75. * Set the Graphic instance for the shape.
  76. *
  77. * @method _setGraphic
  78. * @param {Graphic | Node | HTMLElement | String} render This param is used to determine the graphic instance. If it is a
  79. * `Graphic` instance, it will be assigned to the `graphic` attribute. Otherwise, a new Graphic instance will be created
  80. * and rendered into the dom element that the render represents.
  81. * @private
  82. */
  83. _setGraphic: function(render)
  84. {
  85. var graphic;
  86. if(render instanceof Y.SVGGraphic)
  87. {
  88. this._graphic = render;
  89. }
  90. else
  91. {
  92. graphic = new Y.SVGGraphic({
  93. render: render
  94. });
  95. graphic._appendShape(this);
  96. this._graphic = graphic;
  97. }
  98. },
  99.  
  100. /**
  101. * Add a class name to each node.
  102. *
  103. * @method addClass
  104. * @param {String} className the class name to add to the node's class attribute
  105. */
  106. addClass: function(className)
  107. {
  108. var node = this.node;
  109. node.className.baseVal = Y_LANG.trim([node.className.baseVal, className].join(' '));
  110. },
  111.  
  112. /**
  113. * Removes a class name from each node.
  114. *
  115. * @method removeClass
  116. * @param {String} className the class name to remove from the node's class attribute
  117. */
  118. removeClass: function(className)
  119. {
  120. var node = this.node,
  121. classString = node.className.baseVal;
  122. classString = classString.replace(new RegExp(className + ' '), className).replace(new RegExp(className), '');
  123. node.className.baseVal = classString;
  124. },
  125.  
  126. /**
  127. * Gets the current position of the node in page coordinates.
  128. *
  129. * @method getXY
  130. * @return Array The XY position of the shape.
  131. */
  132. getXY: function()
  133. {
  134. var graphic = this._graphic,
  135. parentXY = graphic.getXY(),
  136. x = this._x,
  137. y = this._y;
  138. return [parentXY[0] + x, parentXY[1] + y];
  139. },
  140.  
  141. /**
  142. * Set the position of the shape in page coordinates, regardless of how the node is positioned.
  143. *
  144. * @method setXY
  145. * @param {Array} Contains x & y values for new position (coordinates are page-based)
  146. */
  147. setXY: function(xy)
  148. {
  149. var graphic = this._graphic,
  150. parentXY = graphic.getXY();
  151. this._x = xy[0] - parentXY[0];
  152. this._y = xy[1] - parentXY[1];
  153. this.set("transform", this.get("transform"));
  154. },
  155.  
  156. /**
  157. * Determines whether the node is an ancestor of another HTML element in the DOM hierarchy.
  158. *
  159. * @method contains
  160. * @param {SVGShape | HTMLElement} needle The possible node or descendent
  161. * @return Boolean Whether or not this shape is the needle or its ancestor.
  162. */
  163. contains: function(needle)
  164. {
  165. var node = needle instanceof Y.Node ? needle._node : needle;
  166. return node === this.node;
  167. },
  168.  
  169. /**
  170. * Compares nodes to determine if they match.
  171. * Node instances can be compared to each other and/or HTMLElements.
  172. * @method compareTo
  173. * @param {HTMLElement | Node} refNode The reference node to compare to the node.
  174. * @return {Boolean} True if the nodes match, false if they do not.
  175. */
  176. compareTo: function(refNode) {
  177. var node = this.node;
  178.  
  179. return node === refNode;
  180. },
  181.  
  182. /**
  183. * Test if the supplied node matches the supplied selector.
  184. *
  185. * @method test
  186. * @param {String} selector The CSS selector to test against.
  187. * @return Boolean Wheter or not the shape matches the selector.
  188. */
  189. test: function(selector)
  190. {
  191. return Y.Selector.test(this.node, selector);
  192. },
  193.  
  194. /**
  195. * Value function for fill attribute
  196. *
  197. * @private
  198. * @method _getDefaultFill
  199. * @return Object
  200. */
  201. _getDefaultFill: function() {
  202. return {
  203. type: "solid",
  204. opacity: 1,
  205. cx: 0.5,
  206. cy: 0.5,
  207. fx: 0.5,
  208. fy: 0.5,
  209. r: 0.5
  210. };
  211. },
  212.  
  213. /**
  214. * Value function for stroke attribute
  215. *
  216. * @private
  217. * @method _getDefaultStroke
  218. * @return Object
  219. */
  220. _getDefaultStroke: function()
  221. {
  222. return {
  223. weight: 1,
  224. dashstyle: "none",
  225. color: "#000",
  226. opacity: 1.0
  227. };
  228. },
  229.  
  230. /**
  231. * Creates the dom node for the shape.
  232. *
  233. * @method createNode
  234. * @return HTMLElement
  235. * @private
  236. */
  237. createNode: function()
  238. {
  239. var host = this,
  240. node = DOCUMENT.createElementNS("http://www.w3.org/2000/svg", "svg:" + this._type),
  241. id = host.get("id"),
  242. name = host.name,
  243. concat = host._camelCaseConcat,
  244. pointerEvents = host.get("pointerEvents");
  245. host.node = node;
  246. host.addClass(
  247. _getClassName(SHAPE) +
  248. " " +
  249. _getClassName(concat(IMPLEMENTATION, SHAPE)) +
  250. " " +
  251. _getClassName(name) +
  252. " " +
  253. _getClassName(concat(IMPLEMENTATION, name))
  254. );
  255. if(id)
  256. {
  257. node.setAttribute("id", id);
  258. }
  259. if(pointerEvents)
  260. {
  261. node.setAttribute("pointer-events", pointerEvents);
  262. }
  263. if(!host.get("visible"))
  264. {
  265. Y.DOM.setStyle(node, "visibility", "hidden");
  266. }
  267. },
  268.  
  269.  
  270. /**
  271. * Overrides default `on` method. Checks to see if its a dom interaction event. If so,
  272. * return an event attached to the `node` element. If not, return the normal functionality.
  273. *
  274. * @method on
  275. * @param {String} type event type
  276. * @param {Object} callback function
  277. * @private
  278. */
  279. on: function(type, fn)
  280. {
  281. if(Y.Node.DOM_EVENTS[type])
  282. {
  283. return Y.on(type, fn, "#" + this.get("id"));
  284. }
  285. return Y.on.apply(this, arguments);
  286. },
  287.  
  288. /**
  289. * Adds a stroke to the shape node.
  290. *
  291. * @method _strokeChangeHandler
  292. * @private
  293. */
  294. _strokeChangeHandler: function()
  295. {
  296. var node = this.node,
  297. stroke = this.get("stroke"),
  298. strokeOpacity,
  299. dashstyle,
  300. dash,
  301. linejoin;
  302. if(stroke && stroke.weight && stroke.weight > 0)
  303. {
  304. linejoin = stroke.linejoin || "round";
  305. strokeOpacity = parseFloat(stroke.opacity);
  306. dashstyle = stroke.dashstyle || "none";
  307. dash = Y_LANG.isArray(dashstyle) ? dashstyle.toString() : dashstyle;
  308. stroke.color = stroke.color || "#000000";
  309. stroke.weight = stroke.weight || 1;
  310. stroke.opacity = Y_LANG.isNumber(strokeOpacity) ? strokeOpacity : 1;
  311. stroke.linecap = stroke.linecap || "butt";
  312. node.setAttribute("stroke-dasharray", dash);
  313. node.setAttribute("stroke", stroke.color);
  314. node.setAttribute("stroke-linecap", stroke.linecap);
  315. node.setAttribute("stroke-width", stroke.weight);
  316. node.setAttribute("stroke-opacity", stroke.opacity);
  317. if(linejoin === "round" || linejoin === "bevel")
  318. {
  319. node.setAttribute("stroke-linejoin", linejoin);
  320. }
  321. else
  322. {
  323. linejoin = parseInt(linejoin, 10);
  324. if(Y_LANG.isNumber(linejoin))
  325. {
  326. node.setAttribute("stroke-miterlimit", Math.max(linejoin, 1));
  327. node.setAttribute("stroke-linejoin", "miter");
  328. }
  329. }
  330. }
  331. else
  332. {
  333. node.setAttribute("stroke", "none");
  334. }
  335. },
  336.  
  337. /**
  338. * Adds a fill to the shape node.
  339. *
  340. * @method _fillChangeHandler
  341. * @private
  342. */
  343. _fillChangeHandler: function()
  344. {
  345. var node = this.node,
  346. fill = this.get("fill"),
  347. fillOpacity,
  348. type;
  349. if(fill)
  350. {
  351. type = fill.type;
  352. if(type === "linear" || type === "radial")
  353. {
  354. this._setGradientFill(fill);
  355. node.setAttribute("fill", "url(#grad" + this.get("id") + ")");
  356. }
  357. else if(!fill.color)
  358. {
  359. node.setAttribute("fill", "none");
  360. }
  361. else
  362. {
  363. fillOpacity = parseFloat(fill.opacity);
  364. fillOpacity = Y_LANG.isNumber(fillOpacity) ? fillOpacity : 1;
  365. node.setAttribute("fill", fill.color);
  366. node.setAttribute("fill-opacity", fillOpacity);
  367. }
  368. }
  369. else
  370. {
  371. node.setAttribute("fill", "none");
  372. }
  373. },
  374.  
  375. /**
  376. * Creates a gradient fill
  377. *
  378. * @method _setGradientFill
  379. * @param {String} type gradient type
  380. * @private
  381. */
  382. _setGradientFill: function(fill) {
  383. var offset,
  384. opacity,
  385. color,
  386. stopNode,
  387. newStop,
  388. isNumber = Y_LANG.isNumber,
  389. graphic = this._graphic,
  390. type = fill.type,
  391. gradientNode = graphic.getGradientNode("grad" + this.get("id"), type),
  392. stops = fill.stops,
  393. w = this.get("width"),
  394. h = this.get("height"),
  395. rotation = fill.rotation || 0,
  396. radCon = Math.PI/180,
  397. tanRadians = parseFloat(parseFloat(Math.tan(rotation * radCon)).toFixed(8)),
  398. i,
  399. len,
  400. def,
  401. stop,
  402. x1 = "0%",
  403. x2 = "100%",
  404. y1 = "0%",
  405. y2 = "0%",
  406. cx = fill.cx,
  407. cy = fill.cy,
  408. fx = fill.fx,
  409. fy = fill.fy,
  410. r = fill.r,
  411. stopNodes = [];
  412. if(type === "linear")
  413. {
  414. cx = w/2;
  415. cy = h/2;
  416. if(Math.abs(tanRadians) * w/2 >= h/2)
  417. {
  418. if(rotation < 180)
  419. {
  420. y1 = 0;
  421. y2 = h;
  422. }
  423. else
  424. {
  425. y1 = h;
  426. y2 = 0;
  427. }
  428. x1 = cx - ((cy - y1)/tanRadians);
  429. x2 = cx - ((cy - y2)/tanRadians);
  430. }
  431. else
  432. {
  433. if(rotation > 90 && rotation < 270)
  434. {
  435. x1 = w;
  436. x2 = 0;
  437. }
  438. else
  439. {
  440. x1 = 0;
  441. x2 = w;
  442. }
  443. y1 = ((tanRadians * (cx - x1)) - cy) * -1;
  444. y2 = ((tanRadians * (cx - x2)) - cy) * -1;
  445. }
  446.  
  447. x1 = Math.round(100 * x1/w);
  448. x2 = Math.round(100 * x2/w);
  449. y1 = Math.round(100 * y1/h);
  450. y2 = Math.round(100 * y2/h);
  451.  
  452. //Set default value if not valid
  453. x1 = isNumber(x1) ? x1 : 0;
  454. x2 = isNumber(x2) ? x2 : 100;
  455. y1 = isNumber(y1) ? y1 : 0;
  456. y2 = isNumber(y2) ? y2 : 0;
  457.  
  458. gradientNode.setAttribute("spreadMethod", "pad");
  459. gradientNode.setAttribute("width", w);
  460. gradientNode.setAttribute("height", h);
  461. gradientNode.setAttribute("x1", x1 + "%");
  462. gradientNode.setAttribute("x2", x2 + "%");
  463. gradientNode.setAttribute("y1", y1 + "%");
  464. gradientNode.setAttribute("y2", y2 + "%");
  465. }
  466. else
  467. {
  468. gradientNode.setAttribute("cx", (cx * 100) + "%");
  469. gradientNode.setAttribute("cy", (cy * 100) + "%");
  470. gradientNode.setAttribute("fx", (fx * 100) + "%");
  471. gradientNode.setAttribute("fy", (fy * 100) + "%");
  472. gradientNode.setAttribute("r", (r * 100) + "%");
  473. }
  474.  
  475. len = stops.length;
  476. def = 0;
  477. for(i = 0; i < len; ++i)
  478. {
  479. if(this._stops && this._stops.length > 0)
  480. {
  481. stopNode = this._stops.shift();
  482. newStop = false;
  483. }
  484. else
  485. {
  486. stopNode = graphic._createGraphicNode("stop");
  487. newStop = true;
  488. }
  489. stop = stops[i];
  490. opacity = stop.opacity;
  491. color = stop.color;
  492. offset = stop.offset || i/(len - 1);
  493. offset = Math.round(offset * 100) + "%";
  494. opacity = isNumber(opacity) ? opacity : 1;
  495. opacity = Math.max(0, Math.min(1, opacity));
  496. def = (i + 1) / len;
  497. stopNode.setAttribute("offset", offset);
  498. stopNode.setAttribute("stop-color", color);
  499. stopNode.setAttribute("stop-opacity", opacity);
  500. if(newStop)
  501. {
  502. gradientNode.appendChild(stopNode);
  503. }
  504. stopNodes.push(stopNode);
  505. }
  506. while(this._stops && this._stops.length > 0)
  507. {
  508. gradientNode.removeChild(this._stops.shift());
  509. }
  510. this._stops = stopNodes;
  511. },
  512.  
  513. _stops: null,
  514.  
  515. /**
  516. * Sets the value of an attribute.
  517. *
  518. * @method set
  519. * @param {String|Object} name The name of the attribute. Alternatively, an object of key value pairs can
  520. * be passed in to set multiple attributes at once.
  521. * @param {Any} value The value to set the attribute to. This value is ignored if an object is received as
  522. * the name param.
  523. */
  524. set: function()
  525. {
  526. var host = this;
  527. AttributeLite.prototype.set.apply(host, arguments);
  528. if(host.initialized)
  529. {
  530. host._updateHandler();
  531. }
  532. },
  533.  
  534. /**
  535. * Specifies a 2d translation.
  536. *
  537. * @method translate
  538. * @param {Number} x The value to transate on the x-axis.
  539. * @param {Number} y The value to translate on the y-axis.
  540. */
  541. translate: function()
  542. {
  543. this._addTransform("translate", arguments);
  544. },
  545.  
  546. /**
  547. * Translates the shape along the x-axis. When translating x and y coordinates,
  548. * use the `translate` method.
  549. *
  550. * @method translateX
  551. * @param {Number} x The value to translate.
  552. */
  553. translateX: function()
  554. {
  555. this._addTransform("translateX", arguments);
  556. },
  557.  
  558. /**
  559. * Translates the shape along the y-axis. When translating x and y coordinates,
  560. * use the `translate` method.
  561. *
  562. * @method translateY
  563. * @param {Number} y The value to translate.
  564. */
  565. translateY: function()
  566. {
  567. this._addTransform("translateY", arguments);
  568. },
  569.  
  570. /**
  571. * Skews the shape around the x-axis and y-axis.
  572. *
  573. * @method skew
  574. * @param {Number} x The value to skew on the x-axis.
  575. * @param {Number} y The value to skew on the y-axis.
  576. */
  577. skew: function()
  578. {
  579. this._addTransform("skew", arguments);
  580. },
  581.  
  582. /**
  583. * Skews the shape around the x-axis.
  584. *
  585. * @method skewX
  586. * @param {Number} x x-coordinate
  587. */
  588. skewX: function()
  589. {
  590. this._addTransform("skewX", arguments);
  591. },
  592.  
  593. /**
  594. * Skews the shape around the y-axis.
  595. *
  596. * @method skewY
  597. * @param {Number} y y-coordinate
  598. */
  599. skewY: function()
  600. {
  601. this._addTransform("skewY", arguments);
  602. },
  603.  
  604. /**
  605. * Rotates the shape clockwise around it transformOrigin.
  606. *
  607. * @method rotate
  608. * @param {Number} deg The degree of the rotation.
  609. */
  610. rotate: function()
  611. {
  612. this._addTransform("rotate", arguments);
  613. },
  614.  
  615. /**
  616. * Specifies a 2d scaling operation.
  617. *
  618. * @method scale
  619. * @param {Number} val
  620. */
  621. scale: function()
  622. {
  623. this._addTransform("scale", arguments);
  624. },
  625.  
  626. /**
  627. * Adds a transform to the shape.
  628. *
  629. * @method _addTransform
  630. * @param {String} type The transform being applied.
  631. * @param {Array} args The arguments for the transform.
  632. * @private
  633. */
  634. _addTransform: function(type, args)
  635. {
  636. args = Y.Array(args);
  637. this._transform = Y_LANG.trim(this._transform + " " + type + "(" + args.join(", ") + ")");
  638. args.unshift(type);
  639. this._transforms.push(args);
  640. if(this.initialized)
  641. {
  642. this._updateTransform();
  643. }
  644. },
  645.  
  646. /**
  647. * Applies all transforms.
  648. *
  649. * @method _updateTransform
  650. * @private
  651. */
  652. _updateTransform: function()
  653. {
  654. var isPath = this._type === "path",
  655. node = this.node,
  656. key,
  657. transform,
  658. transformOrigin,
  659. x,
  660. y,
  661. tx,
  662. ty,
  663. matrix = this.matrix,
  664. normalizedMatrix = this._normalizedMatrix,
  665. i,
  666. len = this._transforms.length;
  667.  
  668. if(isPath || (this._transforms && this._transforms.length > 0))
  669. {
  670. x = this._x;
  671. y = this._y;
  672. transformOrigin = this.get("transformOrigin");
  673. tx = x + (transformOrigin[0] * this.get("width"));
  674. ty = y + (transformOrigin[1] * this.get("height"));
  675. //need to use translate for x/y coords
  676. if(isPath)
  677. {
  678. //adjust origin for custom shapes
  679. if(!(this instanceof Y.SVGPath))
  680. {
  681. tx = this._left + (transformOrigin[0] * this.get("width"));
  682. ty = this._top + (transformOrigin[1] * this.get("height"));
  683. }
  684. normalizedMatrix.init({dx: x + this._left, dy: y + this._top});
  685. }
  686. normalizedMatrix.translate(tx, ty);
  687. for(i = 0; i < len; ++i)
  688. {
  689. key = this._transforms[i].shift();
  690. if(key)
  691. {
  692. normalizedMatrix[key].apply(normalizedMatrix, this._transforms[i]);
  693. matrix[key].apply(matrix, this._transforms[i]);
  694. }
  695. if(isPath)
  696. {
  697. this._transforms[i].unshift(key);
  698. }
  699. }
  700. normalizedMatrix.translate(-tx, -ty);
  701. transform = "matrix(" + normalizedMatrix.a + "," +
  702. normalizedMatrix.b + "," +
  703. normalizedMatrix.c + "," +
  704. normalizedMatrix.d + "," +
  705. normalizedMatrix.dx + "," +
  706. normalizedMatrix.dy + ")";
  707. }
  708. this._graphic.addToRedrawQueue(this);
  709. if(transform)
  710. {
  711. node.setAttribute("transform", transform);
  712. }
  713. if(!isPath)
  714. {
  715. this._transforms = [];
  716. }
  717. },
  718.  
  719. /**
  720. * Draws the shape.
  721. *
  722. * @method _draw
  723. * @private
  724. */
  725. _draw: function()
  726. {
  727. var node = this.node;
  728. node.setAttribute("width", this.get("width"));
  729. node.setAttribute("height", this.get("height"));
  730. node.setAttribute("x", this._x);
  731. node.setAttribute("y", this._y);
  732. node.style.left = this._x + "px";
  733. node.style.top = this._y + "px";
  734. this._fillChangeHandler();
  735. this._strokeChangeHandler();
  736. this._updateTransform();
  737. },
  738.  
  739. /**
  740. * Updates `Shape` based on attribute changes.
  741. *
  742. * @method _updateHandler
  743. * @private
  744. */
  745. _updateHandler: function()
  746. {
  747. this._draw();
  748. },
  749.  
  750. /**
  751. * Storage for the transform attribute.
  752. *
  753. * @property _transform
  754. * @type String
  755. * @private
  756. */
  757. _transform: "",
  758.  
  759. /**
  760. * Returns the bounds for a shape.
  761. *
  762. * Calculates the a new bounding box from the original corner coordinates (base on size and position) and the transform matrix.
  763. * The calculated bounding box is used by the graphic instance to calculate its viewBox.
  764. *
  765. * @method getBounds
  766. * @return Object
  767. */
  768. getBounds: function()
  769. {
  770. var type = this._type,
  771. stroke = this.get("stroke"),
  772. w = this.get("width"),
  773. h = this.get("height"),
  774. x = type === "path" ? 0 : this._x,
  775. y = type === "path" ? 0 : this._y,
  776. wt = 0;
  777. if(type !== "path")
  778. {
  779. if(stroke && stroke.weight)
  780. {
  781. wt = stroke.weight;
  782. }
  783. w = (x + w + wt) - (x - wt);
  784. h = (y + h + wt) - (y - wt);
  785. x -= wt;
  786. y -= wt;
  787. }
  788. return this._normalizedMatrix.getContentRect(w, h, x, y);
  789. },
  790.  
  791. /**
  792. * Places the shape above all other shapes.
  793. *
  794. * @method toFront
  795. */
  796. toFront: function()
  797. {
  798. var graphic = this.get("graphic");
  799. if(graphic)
  800. {
  801. graphic._toFront(this);
  802. }
  803. },
  804.  
  805. /**
  806. * Places the shape underneath all other shapes.
  807. *
  808. * @method toFront
  809. */
  810. toBack: function()
  811. {
  812. var graphic = this.get("graphic");
  813. if(graphic)
  814. {
  815. graphic._toBack(this);
  816. }
  817. },
  818.  
  819. /**
  820. * Parses path data string and call mapped methods.
  821. *
  822. * @method _parsePathData
  823. * @param {String} val The path data
  824. * @private
  825. */
  826. _parsePathData: function(val)
  827. {
  828. var method,
  829. methodSymbol,
  830. args,
  831. commandArray = Y.Lang.trim(val.match(SPLITPATHPATTERN)),
  832. i,
  833. len,
  834. str,
  835. symbolToMethod = this._pathSymbolToMethod;
  836. if(commandArray)
  837. {
  838. this.clear();
  839. len = commandArray.length || 0;
  840. for(i = 0; i < len; i = i + 1)
  841. {
  842. str = commandArray[i];
  843. methodSymbol = str.substr(0, 1);
  844. args = str.substr(1).match(SPLITARGSPATTERN);
  845. method = symbolToMethod[methodSymbol];
  846. if(method)
  847. {
  848. if(args)
  849. {
  850. this[method].apply(this, args);
  851. }
  852. else
  853. {
  854. this[method].apply(this);
  855. }
  856. }
  857. }
  858. this.end();
  859. }
  860. },
  861.  
  862. /**
  863. * Destroys the shape instance.
  864. *
  865. * @method destroy
  866. */
  867. destroy: function()
  868. {
  869. var graphic = this.get("graphic");
  870. if(graphic)
  871. {
  872. graphic.removeShape(this);
  873. }
  874. else
  875. {
  876. this._destroy();
  877. }
  878. },
  879.  
  880. /**
  881. * Implementation for shape destruction
  882. *
  883. * @method destroy
  884. * @protected
  885. */
  886. _destroy: function()
  887. {
  888. if(this.node)
  889. {
  890. Y.Event.purgeElement(this.node, true);
  891. if(this.node.parentNode)
  892. {
  893. this.node.parentNode.removeChild(this.node);
  894. }
  895. this.node = null;
  896. }
  897. }
  898. }, Y.SVGDrawing.prototype));
  899.  
  900. SVGShape.ATTRS = {
  901. /**
  902. * An array of x, y values which indicates the transformOrigin in which to rotate the shape. Valid values range between 0 and 1 representing a
  903. * fraction of the shape's corresponding bounding box dimension. The default value is [0.5, 0.5].
  904. *
  905. * @config transformOrigin
  906. * @type Array
  907. */
  908. transformOrigin: {
  909. valueFn: function()
  910. {
  911. return [0.5, 0.5];
  912. }
  913. },
  914.  
  915. /**
  916. * <p>A string containing, in order, transform operations applied to the shape instance. The `transform` string can contain the following values:
  917. *
  918. * <dl>
  919. * <dt>rotate</dt><dd>Rotates the shape clockwise around it transformOrigin.</dd>
  920. * <dt>translate</dt><dd>Specifies a 2d translation.</dd>
  921. * <dt>skew</dt><dd>Skews the shape around the x-axis and y-axis.</dd>
  922. * <dt>scale</dt><dd>Specifies a 2d scaling operation.</dd>
  923. * <dt>translateX</dt><dd>Translates the shape along the x-axis.</dd>
  924. * <dt>translateY</dt><dd>Translates the shape along the y-axis.</dd>
  925. * <dt>skewX</dt><dd>Skews the shape around the x-axis.</dd>
  926. * <dt>skewY</dt><dd>Skews the shape around the y-axis.</dd>
  927. * <dt>matrix</dt><dd>Specifies a 2D transformation matrix comprised of the specified six values.</dd>
  928. * </dl>
  929. * </p>
  930. * <p>Applying transforms through the transform attribute will reset the transform matrix and apply a new transform. The shape class also contains
  931. * corresponding methods for each transform that will apply the transform to the current matrix. The below code illustrates how you might use the
  932. * `transform` attribute to instantiate a recangle with a rotation of 45 degrees.</p>
  933. var myRect = new Y.Rect({
  934. type:"rect",
  935. width: 50,
  936. height: 40,
  937. transform: "rotate(45)"
  938. };
  939. * <p>The code below would apply `translate` and `rotate` to an existing shape.</p>
  940.  
  941. myRect.set("transform", "translate(40, 50) rotate(45)");
  942. * @config transform
  943. * @type String
  944. */
  945. transform: {
  946. setter: function(val)
  947. {
  948. this.matrix.init();
  949. this._normalizedMatrix.init();
  950. this._transforms = this.matrix.getTransformArray(val);
  951. this._transform = val;
  952. return val;
  953. },
  954.  
  955. getter: function()
  956. {
  957. return this._transform;
  958. }
  959. },
  960.  
  961. /**
  962. * Unique id for class instance.
  963. *
  964. * @config id
  965. * @type String
  966. */
  967. id: {
  968. valueFn: function()
  969. {
  970. return Y.guid();
  971. },
  972.  
  973. setter: function(val)
  974. {
  975. var node = this.node;
  976. if(node)
  977. {
  978. node.setAttribute("id", val);
  979. }
  980. return val;
  981. }
  982. },
  983.  
  984. /**
  985. * Indicates the x position of shape.
  986. *
  987. * @config x
  988. * @type Number
  989. */
  990. x: {
  991. getter: function()
  992. {
  993. return this._x;
  994. },
  995.  
  996. setter: function(val)
  997. {
  998. var transform = this.get("transform");
  999. this._x = val;
  1000. if(transform)
  1001. {
  1002. this.set("transform", transform);
  1003. }
  1004. }
  1005. },
  1006.  
  1007. /**
  1008. * Indicates the y position of shape.
  1009. *
  1010. * @config y
  1011. * @type Number
  1012. */
  1013. y: {
  1014. getter: function()
  1015. {
  1016. return this._y;
  1017. },
  1018.  
  1019. setter: function(val)
  1020. {
  1021. var transform = this.get("transform");
  1022. this._y = val;
  1023. if(transform)
  1024. {
  1025. this.set("transform", transform);
  1026. }
  1027. }
  1028. },
  1029.  
  1030. /**
  1031. * Indicates the width of the shape
  1032. *
  1033. * @config width
  1034. * @type Number
  1035. */
  1036. width: {
  1037. value: 0
  1038. },
  1039.  
  1040. /**
  1041. * Indicates the height of the shape
  1042. *
  1043. * @config height
  1044. * @type Number
  1045. */
  1046. height: {
  1047. value: 0
  1048. },
  1049.  
  1050. /**
  1051. * Indicates whether the shape is visible.
  1052. *
  1053. * @config visible
  1054. * @type Boolean
  1055. */
  1056. visible: {
  1057. value: true,
  1058.  
  1059. setter: function(val){
  1060. var visibility = val ? "visible" : "hidden";
  1061. if(this.node)
  1062. {
  1063. this.node.style.visibility = visibility;
  1064. }
  1065. return val;
  1066. }
  1067. },
  1068.  
  1069. /**
  1070. * Contains information about the fill of the shape.
  1071. * <dl>
  1072. * <dt>color</dt><dd>The color of the fill.</dd>
  1073. * <dt>opacity</dt><dd>Number between 0 and 1 that indicates the opacity of the fill. The default value is 1.</dd>
  1074. * <dt>type</dt><dd>Type of fill.
  1075. * <dl>
  1076. * <dt>solid</dt><dd>Solid single color fill. (default)</dd>
  1077. * <dt>linear</dt><dd>Linear gradient fill.</dd>
  1078. * <dt>radial</dt><dd>Radial gradient fill.</dd>
  1079. * </dl>
  1080. * </dd>
  1081. * </dl>
  1082. * <p>If a `linear` or `radial` is specified as the fill type. The following additional property is used:
  1083. * <dl>
  1084. * <dt>stops</dt><dd>An array of objects containing the following properties:
  1085. * <dl>
  1086. * <dt>color</dt><dd>The color of the stop.</dd>
  1087. * <dt>opacity</dt><dd>Number between 0 and 1 that indicates the opacity of the stop. The default value is 1.
  1088. * Note: No effect for IE 6 - 8</dd>
  1089. * <dt>offset</dt><dd>Number between 0 and 1 indicating where the color stop is positioned.</dd>
  1090. * </dl>
  1091. * </dd>
  1092. * <p>Linear gradients also have the following property:</p>
  1093. * <dt>rotation</dt><dd>Linear gradients flow left to right by default. The rotation property allows you to change the
  1094. * flow by rotation. (e.g. A rotation of 180 would make the gradient pain from right to left.)</dd>
  1095. * <p>Radial gradients have the following additional properties:</p>
  1096. * <dt>r</dt><dd>Radius of the gradient circle.</dd>
  1097. * <dt>fx</dt><dd>Focal point x-coordinate of the gradient.</dd>
  1098. * <dt>fy</dt><dd>Focal point y-coordinate of the gradient.</dd>
  1099. * <dt>cx</dt><dd>
  1100. * <p>The x-coordinate of the center of the gradient circle. Determines where the color stop begins. The default value 0.5.</p>
  1101. * <p><strong>Note: </strong>Currently, this property is not implemented for corresponding `CanvasShape` and
  1102. * `VMLShape` classes which are used on Android or IE 6 - 8.</p>
  1103. * </dd>
  1104. * <dt>cy</dt><dd>
  1105. * <p>The y-coordinate of the center of the gradient circle. Determines where the color stop begins. The default value 0.5.</p>
  1106. * <p><strong>Note: </strong>Currently, this property is not implemented for corresponding `CanvasShape` and `VMLShape`
  1107. * classes which are used on Android or IE 6 - 8.</p>
  1108. * </dd>
  1109. * </dl>
  1110. *
  1111. * @config fill
  1112. * @type Object
  1113. */
  1114. fill: {
  1115. valueFn: "_getDefaultFill",
  1116.  
  1117. setter: function(val)
  1118. {
  1119. var fill,
  1120. tmpl = this.get("fill") || this._getDefaultFill();
  1121. fill = (val) ? Y.merge(tmpl, val) : null;
  1122. if(fill && fill.color)
  1123. {
  1124. if(fill.color === undefined || fill.color === "none")
  1125. {
  1126. fill.color = null;
  1127. }
  1128. }
  1129. return fill;
  1130. }
  1131. },
  1132.  
  1133. /**
  1134. * Contains information about the stroke of the shape.
  1135. * <dl>
  1136. * <dt>color</dt><dd>The color of the stroke.</dd>
  1137. * <dt>weight</dt><dd>Number that indicates the width of the stroke.</dd>
  1138. * <dt>opacity</dt><dd>Number between 0 and 1 that indicates the opacity of the stroke. The default value is 1.</dd>
  1139. * <dt>dashstyle</dt>Indicates whether to draw a dashed stroke. When set to "none", a solid stroke is drawn. When set
  1140. * to an array, the first index indicates the length of the dash. The second index indicates the length of gap.
  1141. * <dt>linecap</dt><dd>Specifies the linecap for the stroke. The following values can be specified:
  1142. * <dl>
  1143. * <dt>butt (default)</dt><dd>Specifies a butt linecap.</dd>
  1144. * <dt>square</dt><dd>Specifies a sqare linecap.</dd>
  1145. * <dt>round</dt><dd>Specifies a round linecap.</dd>
  1146. * </dl>
  1147. * </dd>
  1148. * <dt>linejoin</dt><dd>Specifies a linejoin for the stroke. The following values can be specified:
  1149. * <dl>
  1150. * <dt>round (default)</dt><dd>Specifies that the linejoin will be round.</dd>
  1151. * <dt>bevel</dt><dd>Specifies a bevel for the linejoin.</dd>
  1152. * <dt>miter limit</dt><dd>An integer specifying the miter limit of a miter linejoin. If you want to specify a linejoin
  1153. * of miter, you simply specify the limit as opposed to having separate miter and miter limit values.</dd>
  1154. * </dl>
  1155. * </dd>
  1156. * </dl>
  1157. *
  1158. * @config stroke
  1159. * @type Object
  1160. */
  1161. stroke: {
  1162. valueFn: "_getDefaultStroke",
  1163.  
  1164. setter: function(val)
  1165. {
  1166. var tmpl = this.get("stroke") || this._getDefaultStroke(),
  1167. wt;
  1168. if(val && val.hasOwnProperty("weight"))
  1169. {
  1170. wt = parseInt(val.weight, 10);
  1171. if(!isNaN(wt))
  1172. {
  1173. val.weight = wt;
  1174. }
  1175. }
  1176. return (val) ? Y.merge(tmpl, val) : null;
  1177. }
  1178. },
  1179.  
  1180. // Only implemented in SVG
  1181. // Determines whether the instance will receive mouse events.
  1182. //
  1183. // @config pointerEvents
  1184. // @type string
  1185. //
  1186. pointerEvents: {
  1187. valueFn: function()
  1188. {
  1189. var val = "visiblePainted",
  1190. node = this.node;
  1191. if(node)
  1192. {
  1193. node.setAttribute("pointer-events", val);
  1194. }
  1195. return val;
  1196. },
  1197.  
  1198. setter: function(val)
  1199. {
  1200. var node = this.node;
  1201. if(node)
  1202. {
  1203. node.setAttribute("pointer-events", val);
  1204. }
  1205. return val;
  1206. }
  1207. },
  1208.  
  1209. /**
  1210. * Dom node for the shape.
  1211. *
  1212. * @config node
  1213. * @type HTMLElement
  1214. * @readOnly
  1215. */
  1216. node: {
  1217. readOnly: true,
  1218.  
  1219. getter: function()
  1220. {
  1221. return this.node;
  1222. }
  1223. },
  1224.  
  1225. /**
  1226. * Represents an SVG Path string. This will be parsed and added to shape's API to represent the SVG data across all
  1227. * implementations. Note that when using VML or SVG implementations, part of this content will be added to the DOM using
  1228. * respective VML/SVG attributes. If your content comes from an untrusted source, you will need to ensure that no
  1229. * malicious code is included in that content.
  1230. *
  1231. * @config data
  1232. * @type String
  1233. */
  1234. data: {
  1235. setter: function(val)
  1236. {
  1237. if(this.get("node"))
  1238. {
  1239. this._parsePathData(val);
  1240. }
  1241. return val;
  1242. }
  1243. },
  1244.  
  1245. /**
  1246. * Reference to the parent graphic instance
  1247. *
  1248. * @config graphic
  1249. * @type SVGGraphic
  1250. * @readOnly
  1251. */
  1252. graphic: {
  1253. readOnly: true,
  1254.  
  1255. getter: function()
  1256. {
  1257. return this._graphic;
  1258. }
  1259. }
  1260. };
  1261. Y.SVGShape = SVGShape;
  1262.  
  1263.