1
/* YUI 3.9.1 (build 5852) Copyright 2013 Yahoo! Inc. http://yuilibrary.com/license/ */
2
YUI.add('graphics-svg', function (Y, NAME) {
4
var IMPLEMENTATION = "svg",
6
SPLITPATHPATTERN = /[a-z][^a-z]*/ig,
7
SPLITARGSPATTERN = /[\-]?[0-9]*[0-9|\.][0-9]*/g,
9
AttributeLite = Y.AttributeLite,
17
DOCUMENT = Y.config.doc,
18
_getClassName = Y.ClassNameManager.getClassName;
20
function SVGDrawing(){}
23
* <a href="http://www.w3.org/TR/SVG/">SVG</a> implementation of the <a href="Drawing.html">`Drawing`</a> class.
24
* `SVGDrawing` is not intended to be used directly. Instead, use the <a href="Drawing.html">`Drawing`</a> class.
25
* If the browser has <a href="http://www.w3.org/TR/SVG/">SVG</a> capabilities, the <a href="Drawing.html">`Drawing`</a>
26
* class will point to the `SVGDrawing` class.
32
SVGDrawing.prototype = {
34
* Maps path to methods
36
* @property _pathSymbolToMethod
40
_pathSymbolToMethod: {
47
Q: "quadraticCurveTo",
48
q: "relativeQuadraticCurveTo",
54
* Current x position of the drawing.
63
* Current y position of the drqwing.
72
* Indicates the type of shape
82
* Draws a bezier curve.
85
* @param {Number} cp1x x-coordinate for the first control point.
86
* @param {Number} cp1y y-coordinate for the first control point.
87
* @param {Number} cp2x x-coordinate for the second control point.
88
* @param {Number} cp2y y-coordinate for the second control point.
89
* @param {Number} x x-coordinate for the end point.
90
* @param {Number} y y-coordinate for the end point.
94
this._curveTo.apply(this, [Y.Array(arguments), false]);
99
* Draws a bezier curve relative to the current coordinates.
101
* @method relativeCurveTo
102
* @param {Number} cp1x x-coordinate for the first control point.
103
* @param {Number} cp1y y-coordinate for the first control point.
104
* @param {Number} cp2x x-coordinate for the second control point.
105
* @param {Number} cp2y y-coordinate for the second control point.
106
* @param {Number} x x-coordinate for the end point.
107
* @param {Number} y y-coordinate for the end point.
110
relativeCurveTo: function() {
111
this._curveTo.apply(this, [Y.Array(arguments), true]);
116
* Implements curveTo methods.
119
* @param {Array} args The arguments to be used.
120
* @param {Boolean} relative Indicates whether or not to use relative coordinates.
123
_curveTo: function(args, relative) {
141
command = relative ? "c" : "C",
142
relativeX = relative ? parseFloat(this._currentX) : 0,
143
relativeY = relative ? parseFloat(this._currentY) : 0;
144
this._pathArray = this._pathArray || [];
145
if(this._pathType !== command)
147
this._pathType = command;
148
currentArray = [command];
149
this._pathArray.push(currentArray);
153
currentArray = this._pathArray[Math.max(0, this._pathArray.length - 1)];
157
this._pathArray.push(currentArray);
160
pathArrayLen = this._pathArray.length - 1;
161
this._pathArray[pathArrayLen] = this._pathArray[pathArrayLen].concat(args);
162
len = args.length - 5;
163
for(i = 0; i < len; i = i + 6)
165
cp1x = parseFloat(args[i]) + relativeX;
166
cp1y = parseFloat(args[i + 1]) + relativeY;
167
cp2x = parseFloat(args[i + 2]) + relativeX;
168
cp2y = parseFloat(args[i + 3]) + relativeY;
169
x = parseFloat(args[i + 4]) + relativeX;
170
y = parseFloat(args[i + 5]) + relativeY;
171
right = Math.max(x, Math.max(cp1x, cp2x));
172
bottom = Math.max(y, Math.max(cp1y, cp2y));
173
left = Math.min(x, Math.min(cp1x, cp2x));
174
top = Math.min(y, Math.min(cp1y, cp2y));
175
w = Math.abs(right - left);
176
h = Math.abs(bottom - top);
177
pts = [[this._currentX, this._currentY] , [cp1x, cp1y], [cp2x, cp2y], [x, y]];
178
this._setCurveBoundingBox(pts, w, h);
185
* Draws a quadratic bezier curve.
187
* @method quadraticCurveTo
188
* @param {Number} cpx x-coordinate for the control point.
189
* @param {Number} cpy y-coordinate for the control point.
190
* @param {Number} x x-coordinate for the end point.
191
* @param {Number} y y-coordinate for the end point.
194
quadraticCurveTo: function() {
195
this._quadraticCurveTo.apply(this, [Y.Array(arguments), false]);
200
* Draws a quadratic bezier curve relative to the current position.
202
* @method quadraticCurveTo
203
* @param {Number} cpx x-coordinate for the control point.
204
* @param {Number} cpy y-coordinate for the control point.
205
* @param {Number} x x-coordinate for the end point.
206
* @param {Number} y y-coordinate for the end point.
209
relativeQuadraticCurveTo: function() {
210
this._quadraticCurveTo.apply(this, [Y.Array(arguments), true]);
215
* Implements quadraticCurveTo methods.
217
* @method _quadraticCurveTo
218
* @param {Array} args The arguments to be used.
219
* @param {Boolean} relative Indicates whether or not to use relative coordinates.
222
_quadraticCurveTo: function(args, relative) {
238
command = relative ? "q" : "Q",
239
relativeX = relative ? parseFloat(this._currentX) : 0,
240
relativeY = relative ? parseFloat(this._currentY) : 0;
241
this._pathArray = this._pathArray || [];
242
if(this._pathType !== command)
244
this._pathType = command;
245
currentArray = [command];
246
this._pathArray.push(currentArray);
250
currentArray = this._pathArray[Math.max(0, this._pathArray.length - 1)];
254
this._pathArray.push(currentArray);
257
pathArrayLen = this._pathArray.length - 1;
258
this._pathArray[pathArrayLen] = this._pathArray[pathArrayLen].concat(args);
259
len = args.length - 3;
260
for(i = 0; i < len; i = i + 4)
262
cpx = parseFloat(args[i]) + relativeX;
263
cpy = parseFloat(args[i + 1]) + relativeY;
264
x = parseFloat(args[i + 2]) + relativeX;
265
y = parseFloat(args[i + 3]) + relativeY;
266
right = Math.max(x, cpx);
267
bottom = Math.max(y, cpy);
268
left = Math.min(x, cpx);
269
top = Math.min(y, cpy);
270
w = Math.abs(right - left);
271
h = Math.abs(bottom - top);
272
pts = [[this._currentX, this._currentY] , [cpx, cpy], [x, y]];
273
this._setCurveBoundingBox(pts, w, h);
283
* @param {Number} x x-coordinate
284
* @param {Number} y y-coordinate
285
* @param {Number} w width
286
* @param {Number} h height
289
drawRect: function(x, y, w, h) {
291
this.lineTo(x + w, y);
292
this.lineTo(x + w, y + h);
293
this.lineTo(x, y + h);
299
* Draws a rectangle with rounded corners.
301
* @method drawRoundRect
302
* @param {Number} x x-coordinate
303
* @param {Number} y y-coordinate
304
* @param {Number} w width
305
* @param {Number} h height
306
* @param {Number} ew width of the ellipse used to draw the rounded corners
307
* @param {Number} eh height of the ellipse used to draw the rounded corners
310
drawRoundRect: function(x, y, w, h, ew, eh) {
311
this.moveTo(x, y + eh);
312
this.lineTo(x, y + h - eh);
313
this.quadraticCurveTo(x, y + h, x + ew, y + h);
314
this.lineTo(x + w - ew, y + h);
315
this.quadraticCurveTo(x + w, y + h, x + w, y + h - eh);
316
this.lineTo(x + w, y + eh);
317
this.quadraticCurveTo(x + w, y, x + w - ew, y);
318
this.lineTo(x + ew, y);
319
this.quadraticCurveTo(x, y, x, y + eh);
327
* @param {Number} x y-coordinate
328
* @param {Number} y x-coordinate
329
* @param {Number} r radius
333
drawCircle: function(x, y, radius) {
334
var circum = radius * 2;
335
this._drawingComplete = false;
336
this._trackSize(x, y);
337
this._trackSize(x + circum, y + circum);
338
this._pathArray = this._pathArray || [];
339
this._pathArray.push(["M", x + radius, y]);
340
this._pathArray.push(["A", radius, radius, 0, 1, 0, x + radius, y + circum]);
341
this._pathArray.push(["A", radius, radius, 0, 1, 0, x + radius, y]);
350
* @method drawEllipse
351
* @param {Number} x x-coordinate
352
* @param {Number} y y-coordinate
353
* @param {Number} w width
354
* @param {Number} h height
358
drawEllipse: function(x, y, w, h) {
359
var radius = w * 0.5,
361
this._drawingComplete = false;
362
this._trackSize(x, y);
363
this._trackSize(x + w, y + h);
364
this._pathArray = this._pathArray || [];
365
this._pathArray.push(["M", x + radius, y]);
366
this._pathArray.push(["A", radius, yRadius, 0, 1, 0, x + radius, y + h]);
367
this._pathArray.push(["A", radius, yRadius, 0, 1, 0, x + radius, y]);
376
* @method drawDiamond
377
* @param {Number} x y-coordinate
378
* @param {Number} y x-coordinate
379
* @param {Number} width width
380
* @param {Number} height height
384
drawDiamond: function(x, y, width, height)
386
var midWidth = width * 0.5,
387
midHeight = height * 0.5;
388
this.moveTo(x + midWidth, y);
389
this.lineTo(x + width, y + midHeight);
390
this.lineTo(x + midWidth, y + height);
391
this.lineTo(x, y + midHeight);
392
this.lineTo(x + midWidth, y);
400
* @param {Number} x x-coordinate of the wedge's center point
401
* @param {Number} y y-coordinate of the wedge's center point
402
* @param {Number} startAngle starting angle in degrees
403
* @param {Number} arc sweep of the wedge. Negative values draw clockwise.
404
* @param {Number} radius radius of wedge. If [optional] yRadius is defined, then radius is the x radius.
405
* @param {Number} yRadius [optional] y radius for wedge.
409
drawWedge: function(x, y, startAngle, arc, radius, yRadius)
423
diameter = radius * 2,
426
this._pathArray = this._pathArray || [];
427
yRadius = yRadius || radius;
428
if(this._pathType !== "M")
430
this._pathType = "M";
431
currentArray = ["M"];
432
this._pathArray.push(currentArray);
436
currentArray = this._getCurrentArray();
438
pathArrayLen = this._pathArray.length - 1;
439
this._pathArray[pathArrayLen].push(x);
440
this._pathArray[pathArrayLen].push(x);
442
// limit sweep to reasonable numbers
443
if(Math.abs(arc) > 360)
448
// First we calculate how many segments are needed
450
segs = Math.ceil(Math.abs(arc) / 45);
452
// Now calculate the sweep of each segment.
453
segAngle = arc / segs;
455
// The math requires radians rather than degrees. To convert from degrees
456
// use the formula (degrees/180)*Math.PI to get radians.
457
theta = -(segAngle / 180) * Math.PI;
459
// convert angle startAngle to radians
460
angle = (startAngle / 180) * Math.PI;
463
// draw a line from the center to the start of the curve
464
ax = x + Math.cos(startAngle / 180 * Math.PI) * radius;
465
ay = y + Math.sin(startAngle / 180 * Math.PI) * yRadius;
466
this._pathType = "L";
468
this._pathArray[pathArrayLen] = ["L"];
469
this._pathArray[pathArrayLen].push(Math.round(ax));
470
this._pathArray[pathArrayLen].push(Math.round(ay));
472
this._pathType = "Q";
473
this._pathArray[pathArrayLen] = ["Q"];
474
for(i = 0; i < segs; ++i)
477
angleMid = angle - (theta / 2);
478
bx = x + Math.cos(angle) * radius;
479
by = y + Math.sin(angle) * yRadius;
480
cx = x + Math.cos(angleMid) * (radius / Math.cos(theta / 2));
481
cy = y + Math.sin(angleMid) * (yRadius / Math.cos(theta / 2));
482
this._pathArray[pathArrayLen].push(Math.round(cx));
483
this._pathArray[pathArrayLen].push(Math.round(cy));
484
this._pathArray[pathArrayLen].push(Math.round(bx));
485
this._pathArray[pathArrayLen].push(Math.round(by));
490
this._trackSize(diameter, diameter);
495
* Draws a line segment using the current line style from the current drawing position to the specified x and y coordinates.
498
* @param {Number} point1 x-coordinate for the end point.
499
* @param {Number} point2 y-coordinate for the end point.
504
this._lineTo.apply(this, [Y.Array(arguments), false]);
509
* Draws a line segment using the current line style from the current drawing position to the relative x and y coordinates.
511
* @method relativeLineTo
512
* @param {Number} point1 x-coordinate for the end point.
513
* @param {Number} point2 y-coordinate for the end point.
516
relativeLineTo: function()
518
this._lineTo.apply(this, [Y.Array(arguments), true]);
523
* Implements lineTo methods.
526
* @param {Array} args The arguments to be used.
527
* @param {Boolean} relative Indicates whether or not to use relative coordinates.
530
_lineTo: function(args, relative) {
531
var point1 = args[0],
538
command = relative ? "l" : "L",
539
relativeX = relative ? parseFloat(this._currentX) : 0,
540
relativeY = relative ? parseFloat(this._currentY) : 0;
541
this._pathArray = this._pathArray || [];
542
this._shapeType = "path";
544
if(this._pathType !== command)
546
this._pathType = command;
547
currentArray = [command];
548
this._pathArray.push(currentArray);
552
currentArray = this._getCurrentArray();
554
pathArrayLen = this._pathArray.length - 1;
555
if (typeof point1 === 'string' || typeof point1 === 'number') {
556
for (i = 0; i < len; i = i + 2) {
557
x = parseFloat(args[i]);
558
y = parseFloat(args[i + 1]);
559
this._pathArray[pathArrayLen].push(x);
560
this._pathArray[pathArrayLen].push(y);
565
this._trackSize.apply(this, [x, y]);
570
for (i = 0; i < len; ++i) {
571
x = parseFloat(args[i][0]);
572
y = parseFloat(args[i][1]);
573
this._pathArray[pathArrayLen].push(x);
574
this._pathArray[pathArrayLen].push(y);
579
this._trackSize.apply(this, [x, y]);
585
* Moves the current drawing position to specified x and y coordinates.
588
* @param {Number} x x-coordinate for the end point.
589
* @param {Number} y y-coordinate for the end point.
594
this._moveTo.apply(this, [Y.Array(arguments), false]);
599
* Moves the current drawing position relative to specified x and y coordinates.
601
* @method relativeMoveTo
602
* @param {Number} x x-coordinate for the end point.
603
* @param {Number} y y-coordinate for the end point.
606
relativeMoveTo: function()
608
this._moveTo.apply(this, [Y.Array(arguments), true]);
613
* Implements moveTo methods.
616
* @param {Array} args The arguments to be used.
617
* @param {Boolean} relative Indicates whether or not to use relative coordinates.
620
_moveTo: function(args, relative) {
623
x = parseFloat(args[0]),
624
y = parseFloat(args[1]),
625
command = relative ? "m" : "M",
626
relativeX = relative ? parseFloat(this._currentX) : 0,
627
relativeY = relative ? parseFloat(this._currentY) : 0;
628
this._pathArray = this._pathArray || [];
629
this._pathType = command;
630
currentArray = [command];
631
this._pathArray.push(currentArray);
632
pathArrayLen = this._pathArray.length - 1;
633
this._pathArray[pathArrayLen] = this._pathArray[pathArrayLen].concat([x, y]);
638
this._trackSize(x, y);
642
* Completes a drawing operation.
669
this._pathArray = [];
681
_closePath: function()
691
left = parseFloat(this._left),
692
top = parseFloat(this._top),
693
fill = this.get("fill");
696
pathArray = this._pathArray.concat();
697
while(pathArray && pathArray.length > 0)
699
segmentArray = pathArray.shift();
700
len = segmentArray.length;
701
pathType = segmentArray[0];
704
path += pathType + segmentArray[1] + "," + segmentArray[2];
706
else if(pathType === "z" || pathType === "Z")
710
else if(pathType === "C" || pathType === "c")
712
path += pathType + (segmentArray[1] - left)+ "," + (segmentArray[2] - top);
716
path += " " + pathType + parseFloat(segmentArray[1] - left);
726
for(i = 2; i < len; ++i)
728
val = (i % 2 === 0) ? top : left;
729
val = segmentArray[i] - val;
730
path += ", " + parseFloat(val);
734
val = " " + parseFloat(segmentArray[3]) + " " + parseFloat(segmentArray[4]);
735
val += "," + parseFloat(segmentArray[5]) + " " + parseFloat(segmentArray[6] - left);
736
val += "," + parseFloat(segmentArray[7] - top);
741
for(i = 3; i < len - 1; i = i + 2)
743
val = parseFloat(segmentArray[i] - left);
745
val = val + parseFloat(segmentArray[i + 1] - top);
751
if(fill && fill.color)
758
node.setAttribute("d", path);
762
this._fillChangeHandler();
763
this._strokeChangeHandler();
764
this._updateTransform();
769
* Ends a fill and stroke
774
closePath: function()
776
this._pathArray.push(["z"]);
781
* Returns the current array of drawing commands.
783
* @method _getCurrentArray
787
_getCurrentArray: function()
789
var currentArray = this._pathArray[Math.max(0, this._pathArray.length - 1)];
793
this._pathArray.push(currentArray);
799
* Returns the points on a curve
801
* @method getBezierData
802
* @param Array points Array containing the begin, end and control points of a curve.
803
* @param Number t The value for incrementing the next set of points.
807
getBezierData: function(points, t) {
808
var n = points.length,
813
for (i = 0; i < n; ++i){
814
tmp[i] = [points[i][0], points[i][1]]; // save input
817
for (j = 1; j < n; ++j) {
818
for (i = 0; i < n - j; ++i) {
819
tmp[i][0] = (1 - t) * tmp[i][0] + t * tmp[parseInt(i + 1, 10)][0];
820
tmp[i][1] = (1 - t) * tmp[i][1] + t * tmp[parseInt(i + 1, 10)][1];
823
return [ tmp[0][0], tmp[0][1] ];
827
* Calculates the bounding box for a curve
829
* @method _setCurveBoundingBox
830
* @param Array pts Array containing points for start, end and control points of a curve.
831
* @param Number w Width used to calculate the number of points to describe the curve.
832
* @param Number h Height used to calculate the number of points to describe the curve.
835
_setCurveBoundingBox: function(pts, w, h)
838
left = this._currentX,
840
top = this._currentY,
842
len = Math.round(Math.sqrt((w * w) + (h * h))),
845
for(i = 0; i < len; ++i)
847
xy = this.getBezierData(pts, t * i);
848
left = isNaN(left) ? xy[0] : Math.min(xy[0], left);
849
right = isNaN(right) ? xy[0] : Math.max(xy[0], right);
850
top = isNaN(top) ? xy[1] : Math.min(xy[1], top);
851
bottom = isNaN(bottom) ? xy[1] : Math.max(xy[1], bottom);
853
left = Math.round(left * 10)/10;
854
right = Math.round(right * 10)/10;
855
top = Math.round(top * 10)/10;
856
bottom = Math.round(bottom * 10)/10;
857
this._trackSize(right, bottom);
858
this._trackSize(left, top);
862
* Updates the size of the graphics object
865
* @param {Number} w width
866
* @param {Number} h height
869
_trackSize: function(w, h) {
870
if (w > this._right) {
881
if (h > this._bottom)
885
this._width = this._right - this._left;
886
this._height = this._bottom - this._top;
889
Y.SVGDrawing = SVGDrawing;
891
* <a href="http://www.w3.org/TR/SVG/">SVG</a> implementation of the <a href="Shape.html">`Shape`</a> class.
892
* `SVGShape` is not intended to be used directly. Instead, use the <a href="Shape.html">`Shape`</a> class.
893
* If the browser has <a href="http://www.w3.org/TR/SVG/">SVG</a> capabilities, the <a href="Shape.html">`Shape`</a>
894
* class will point to the `SVGShape` class.
899
* @param {Object} cfg (optional) Attribute configs
901
SVGShape = function()
903
this._transforms = [];
904
this.matrix = new Y.Matrix();
905
this._normalizedMatrix = new Y.Matrix();
906
SVGShape.superclass.constructor.apply(this, arguments);
909
SVGShape.NAME = "shape";
911
Y.extend(SVGShape, Y.GraphicBase, Y.mix({
913
* Storage for x attribute.
921
* Storage for y attribute.
929
* Init method, invoked during construction.
930
* Calls `initializer` method.
937
this.initializer.apply(this, arguments);
941
* Initializes the shape
944
* @method initializer
946
initializer: function(cfg)
949
graphic = cfg.graphic,
950
data = this.get("data");
954
host._setGraphic(graphic);
958
host._parsePathData(data);
960
host._updateHandler();
964
* Set the Graphic instance for the shape.
966
* @method _setGraphic
967
* @param {Graphic | Node | HTMLElement | String} render This param is used to determine the graphic instance. If it is a
968
* `Graphic` instance, it will be assigned to the `graphic` attribute. Otherwise, a new Graphic instance will be created
969
* and rendered into the dom element that the render represents.
972
_setGraphic: function(render)
975
if(render instanceof Y.SVGGraphic)
977
this._graphic = render;
981
render = Y.one(render);
982
graphic = new Y.SVGGraphic({
985
graphic._appendShape(this);
986
this._graphic = graphic;
991
* Add a class name to each node.
994
* @param {String} className the class name to add to the node's class attribute
996
addClass: function(className)
998
var node = this.node;
999
node.className.baseVal = Y_LANG.trim([node.className.baseVal, className].join(' '));
1003
* Removes a class name from each node.
1005
* @method removeClass
1006
* @param {String} className the class name to remove from the node's class attribute
1008
removeClass: function(className)
1010
var node = this.node,
1011
classString = node.className.baseVal;
1012
classString = classString.replace(new RegExp(className + ' '), className).replace(new RegExp(className), '');
1013
node.className.baseVal = classString;
1017
* Gets the current position of the node in page coordinates.
1020
* @return Array The XY position of the shape.
1024
var graphic = this._graphic,
1025
parentXY = graphic.getXY(),
1028
return [parentXY[0] + x, parentXY[1] + y];
1032
* Set the position of the shape in page coordinates, regardless of how the node is positioned.
1035
* @param {Array} Contains x & y values for new position (coordinates are page-based)
1039
var graphic = this._graphic,
1040
parentXY = graphic.getXY();
1041
this._x = xy[0] - parentXY[0];
1042
this._y = xy[1] - parentXY[1];
1043
this.set("transform", this.get("transform"));
1047
* Determines whether the node is an ancestor of another HTML element in the DOM hierarchy.
1050
* @param {SVGShape | HTMLElement} needle The possible node or descendent
1051
* @return Boolean Whether or not this shape is the needle or its ancestor.
1053
contains: function(needle)
1055
return needle === Y.one(this.node);
1059
* Compares nodes to determine if they match.
1060
* Node instances can be compared to each other and/or HTMLElements.
1062
* @param {HTMLElement | Node} refNode The reference node to compare to the node.
1063
* @return {Boolean} True if the nodes match, false if they do not.
1065
compareTo: function(refNode) {
1066
var node = this.node;
1068
return node === refNode;
1072
* Test if the supplied node matches the supplied selector.
1075
* @param {String} selector The CSS selector to test against.
1076
* @return Boolean Wheter or not the shape matches the selector.
1078
test: function(selector)
1080
return Y.Selector.test(this.node, selector);
1084
* Value function for fill attribute
1087
* @method _getDefaultFill
1090
_getDefaultFill: function() {
1103
* Value function for stroke attribute
1106
* @method _getDefaultStroke
1109
_getDefaultStroke: function()
1120
* Creates the dom node for the shape.
1122
* @method createNode
1123
* @return HTMLElement
1126
createNode: function()
1129
node = DOCUMENT.createElementNS("http://www.w3.org/2000/svg", "svg:" + this._type),
1130
id = host.get("id"),
1132
concat = host._camelCaseConcat,
1133
pointerEvents = host.get("pointerEvents");
1136
_getClassName(SHAPE) +
1138
_getClassName(concat(IMPLEMENTATION, SHAPE)) +
1140
_getClassName(name) +
1142
_getClassName(concat(IMPLEMENTATION, name))
1146
node.setAttribute("id", id);
1150
node.setAttribute("pointer-events", pointerEvents);
1152
if(!host.get("visible"))
1154
Y.one(node).setStyle("visibility", "hidden");
1160
* Overrides default `on` method. Checks to see if its a dom interaction event. If so,
1161
* return an event attached to the `node` element. If not, return the normal functionality.
1164
* @param {String} type event type
1165
* @param {Object} callback function
1168
on: function(type, fn)
1170
if(Y.Node.DOM_EVENTS[type])
1172
return Y.one("#" + this.get("id")).on(type, fn);
1174
return Y.on.apply(this, arguments);
1178
* Adds a stroke to the shape node.
1180
* @method _strokeChangeHandler
1183
_strokeChangeHandler: function()
1185
var node = this.node,
1186
stroke = this.get("stroke"),
1191
if(stroke && stroke.weight && stroke.weight > 0)
1193
linejoin = stroke.linejoin || "round";
1194
strokeOpacity = parseFloat(stroke.opacity);
1195
dashstyle = stroke.dashstyle || "none";
1196
dash = Y_LANG.isArray(dashstyle) ? dashstyle.toString() : dashstyle;
1197
stroke.color = stroke.color || "#000000";
1198
stroke.weight = stroke.weight || 1;
1199
stroke.opacity = Y_LANG.isNumber(strokeOpacity) ? strokeOpacity : 1;
1200
stroke.linecap = stroke.linecap || "butt";
1201
node.setAttribute("stroke-dasharray", dash);
1202
node.setAttribute("stroke", stroke.color);
1203
node.setAttribute("stroke-linecap", stroke.linecap);
1204
node.setAttribute("stroke-width", stroke.weight);
1205
node.setAttribute("stroke-opacity", stroke.opacity);
1206
if(linejoin === "round" || linejoin === "bevel")
1208
node.setAttribute("stroke-linejoin", linejoin);
1212
linejoin = parseInt(linejoin, 10);
1213
if(Y_LANG.isNumber(linejoin))
1215
node.setAttribute("stroke-miterlimit", Math.max(linejoin, 1));
1216
node.setAttribute("stroke-linejoin", "miter");
1222
node.setAttribute("stroke", "none");
1227
* Adds a fill to the shape node.
1229
* @method _fillChangeHandler
1232
_fillChangeHandler: function()
1234
var node = this.node,
1235
fill = this.get("fill"),
1241
if(type === "linear" || type === "radial")
1243
this._setGradientFill(fill);
1244
node.setAttribute("fill", "url(#grad" + this.get("id") + ")");
1246
else if(!fill.color)
1248
node.setAttribute("fill", "none");
1252
fillOpacity = parseFloat(fill.opacity);
1253
fillOpacity = Y_LANG.isNumber(fillOpacity) ? fillOpacity : 1;
1254
node.setAttribute("fill", fill.color);
1255
node.setAttribute("fill-opacity", fillOpacity);
1260
node.setAttribute("fill", "none");
1265
* Creates a gradient fill
1267
* @method _setGradientFill
1268
* @param {String} type gradient type
1271
_setGradientFill: function(fill) {
1277
isNumber = Y_LANG.isNumber,
1278
graphic = this._graphic,
1280
gradientNode = graphic.getGradientNode("grad" + this.get("id"), type),
1282
w = this.get("width"),
1283
h = this.get("height"),
1284
rotation = fill.rotation || 0,
1285
radCon = Math.PI/180,
1286
tanRadians = parseFloat(parseFloat(Math.tan(rotation * radCon)).toFixed(8)),
1301
if(type === "linear")
1305
if(Math.abs(tanRadians) * w/2 >= h/2)
1317
x1 = cx - ((cy - y1)/tanRadians);
1318
x2 = cx - ((cy - y2)/tanRadians);
1322
if(rotation > 90 && rotation < 270)
1332
y1 = ((tanRadians * (cx - x1)) - cy) * -1;
1333
y2 = ((tanRadians * (cx - x2)) - cy) * -1;
1336
x1 = Math.round(100 * x1/w);
1337
x2 = Math.round(100 * x2/w);
1338
y1 = Math.round(100 * y1/h);
1339
y2 = Math.round(100 * y2/h);
1341
//Set default value if not valid
1342
x1 = isNumber(x1) ? x1 : 0;
1343
x2 = isNumber(x2) ? x2 : 100;
1344
y1 = isNumber(y1) ? y1 : 0;
1345
y2 = isNumber(y2) ? y2 : 0;
1347
gradientNode.setAttribute("spreadMethod", "pad");
1348
gradientNode.setAttribute("width", w);
1349
gradientNode.setAttribute("height", h);
1350
gradientNode.setAttribute("x1", x1 + "%");
1351
gradientNode.setAttribute("x2", x2 + "%");
1352
gradientNode.setAttribute("y1", y1 + "%");
1353
gradientNode.setAttribute("y2", y2 + "%");
1357
gradientNode.setAttribute("cx", (cx * 100) + "%");
1358
gradientNode.setAttribute("cy", (cy * 100) + "%");
1359
gradientNode.setAttribute("fx", (fx * 100) + "%");
1360
gradientNode.setAttribute("fy", (fy * 100) + "%");
1361
gradientNode.setAttribute("r", (r * 100) + "%");
1366
for(i = 0; i < len; ++i)
1368
if(this._stops && this._stops.length > 0)
1370
stopNode = this._stops.shift();
1375
stopNode = graphic._createGraphicNode("stop");
1379
opacity = stop.opacity;
1381
offset = stop.offset || i/(len - 1);
1382
offset = Math.round(offset * 100) + "%";
1383
opacity = isNumber(opacity) ? opacity : 1;
1384
opacity = Math.max(0, Math.min(1, opacity));
1385
def = (i + 1) / len;
1386
stopNode.setAttribute("offset", offset);
1387
stopNode.setAttribute("stop-color", color);
1388
stopNode.setAttribute("stop-opacity", opacity);
1391
gradientNode.appendChild(stopNode);
1393
stopNodes.push(stopNode);
1395
while(this._stops && this._stops.length > 0)
1397
gradientNode.removeChild(this._stops.shift());
1399
this._stops = stopNodes;
1405
* Sets the value of an attribute.
1408
* @param {String|Object} name The name of the attribute. Alternatively, an object of key value pairs can
1409
* be passed in to set multiple attributes at once.
1410
* @param {Any} value The value to set the attribute to. This value is ignored if an object is received as
1416
AttributeLite.prototype.set.apply(host, arguments);
1417
if(host.initialized)
1419
host._updateHandler();
1424
* Specifies a 2d translation.
1427
* @param {Number} x The value to transate on the x-axis.
1428
* @param {Number} y The value to translate on the y-axis.
1430
translate: function()
1432
this._addTransform("translate", arguments);
1436
* Translates the shape along the x-axis. When translating x and y coordinates,
1437
* use the `translate` method.
1439
* @method translateX
1440
* @param {Number} x The value to translate.
1442
translateX: function()
1444
this._addTransform("translateX", arguments);
1448
* Translates the shape along the y-axis. When translating x and y coordinates,
1449
* use the `translate` method.
1451
* @method translateY
1452
* @param {Number} y The value to translate.
1454
translateY: function()
1456
this._addTransform("translateY", arguments);
1460
* Skews the shape around the x-axis and y-axis.
1463
* @param {Number} x The value to skew on the x-axis.
1464
* @param {Number} y The value to skew on the y-axis.
1468
this._addTransform("skew", arguments);
1472
* Skews the shape around the x-axis.
1475
* @param {Number} x x-coordinate
1479
this._addTransform("skewX", arguments);
1483
* Skews the shape around the y-axis.
1486
* @param {Number} y y-coordinate
1490
this._addTransform("skewY", arguments);
1494
* Rotates the shape clockwise around it transformOrigin.
1497
* @param {Number} deg The degree of the rotation.
1501
this._addTransform("rotate", arguments);
1505
* Specifies a 2d scaling operation.
1508
* @param {Number} val
1512
this._addTransform("scale", arguments);
1516
* Adds a transform to the shape.
1518
* @method _addTransform
1519
* @param {String} type The transform being applied.
1520
* @param {Array} args The arguments for the transform.
1523
_addTransform: function(type, args)
1525
args = Y.Array(args);
1526
this._transform = Y_LANG.trim(this._transform + " " + type + "(" + args.join(", ") + ")");
1528
this._transforms.push(args);
1529
if(this.initialized)
1531
this._updateTransform();
1536
* Applies all transforms.
1538
* @method _updateTransform
1541
_updateTransform: function()
1543
var isPath = this._type === "path",
1552
matrix = this.matrix,
1553
normalizedMatrix = this._normalizedMatrix,
1555
len = this._transforms.length;
1557
if(isPath || (this._transforms && this._transforms.length > 0))
1561
transformOrigin = this.get("transformOrigin");
1562
tx = x + (transformOrigin[0] * this.get("width"));
1563
ty = y + (transformOrigin[1] * this.get("height"));
1564
//need to use translate for x/y coords
1567
//adjust origin for custom shapes
1568
if(!(this instanceof Y.SVGPath))
1570
tx = this._left + (transformOrigin[0] * this.get("width"));
1571
ty = this._top + (transformOrigin[1] * this.get("height"));
1573
normalizedMatrix.init({dx: x + this._left, dy: y + this._top});
1575
normalizedMatrix.translate(tx, ty);
1576
for(i = 0; i < len; ++i)
1578
key = this._transforms[i].shift();
1581
normalizedMatrix[key].apply(normalizedMatrix, this._transforms[i]);
1582
matrix[key].apply(matrix, this._transforms[i]);
1586
this._transforms[i].unshift(key);
1589
normalizedMatrix.translate(-tx, -ty);
1590
transform = "matrix(" + normalizedMatrix.a + "," +
1591
normalizedMatrix.b + "," +
1592
normalizedMatrix.c + "," +
1593
normalizedMatrix.d + "," +
1594
normalizedMatrix.dx + "," +
1595
normalizedMatrix.dy + ")";
1597
this._graphic.addToRedrawQueue(this);
1600
node.setAttribute("transform", transform);
1604
this._transforms = [];
1616
var node = this.node;
1617
node.setAttribute("width", this.get("width"));
1618
node.setAttribute("height", this.get("height"));
1619
node.setAttribute("x", this._x);
1620
node.setAttribute("y", this._y);
1621
node.style.left = this._x + "px";
1622
node.style.top = this._y + "px";
1623
this._fillChangeHandler();
1624
this._strokeChangeHandler();
1625
this._updateTransform();
1629
* Updates `Shape` based on attribute changes.
1631
* @method _updateHandler
1634
_updateHandler: function()
1640
* Storage for the transform attribute.
1642
* @property _transform
1649
* Returns the bounds for a shape.
1651
* Calculates the a new bounding box from the original corner coordinates (base on size and position) and the transform matrix.
1652
* The calculated bounding box is used by the graphic instance to calculate its viewBox.
1657
getBounds: function()
1659
var type = this._type,
1660
stroke = this.get("stroke"),
1661
w = this.get("width"),
1662
h = this.get("height"),
1663
x = type === "path" ? 0 : this._x,
1664
y = type === "path" ? 0 : this._y,
1668
if(stroke && stroke.weight)
1672
w = (x + w + wt) - (x - wt);
1673
h = (y + h + wt) - (y - wt);
1677
return this._normalizedMatrix.getContentRect(w, h, x, y);
1681
* Places the shape above all other shapes.
1687
var graphic = this.get("graphic");
1690
graphic._toFront(this);
1695
* Places the shape underneath all other shapes.
1701
var graphic = this.get("graphic");
1704
graphic._toBack(this);
1709
* Parses path data string and call mapped methods.
1711
* @method _parsePathData
1712
* @param {String} val The path data
1715
_parsePathData: function(val)
1720
commandArray = Y.Lang.trim(val.match(SPLITPATHPATTERN)),
1724
symbolToMethod = this._pathSymbolToMethod;
1728
len = commandArray.length || 0;
1729
for(i = 0; i < len; i = i + 1)
1731
str = commandArray[i];
1732
methodSymbol = str.substr(0, 1);
1733
args = str.substr(1).match(SPLITARGSPATTERN);
1734
method = symbolToMethod[methodSymbol];
1739
this[method].apply(this, args);
1743
this[method].apply(this);
1752
* Destroys the shape instance.
1758
var graphic = this.get("graphic");
1761
graphic.removeShape(this);
1770
* Implementation for shape destruction
1775
_destroy: function()
1779
Y.Event.purgeElement(this.node, true);
1780
if(this.node.parentNode)
1782
this.node.parentNode.removeChild(this.node);
1787
}, Y.SVGDrawing.prototype));
1791
* 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
1792
* fraction of the shape's corresponding bounding box dimension. The default value is [0.5, 0.5].
1794
* @config transformOrigin
1805
* <p>A string containing, in order, transform operations applied to the shape instance. The `transform` string can contain the following values:
1808
* <dt>rotate</dt><dd>Rotates the shape clockwise around it transformOrigin.</dd>
1809
* <dt>translate</dt><dd>Specifies a 2d translation.</dd>
1810
* <dt>skew</dt><dd>Skews the shape around the x-axis and y-axis.</dd>
1811
* <dt>scale</dt><dd>Specifies a 2d scaling operation.</dd>
1812
* <dt>translateX</dt><dd>Translates the shape along the x-axis.</dd>
1813
* <dt>translateY</dt><dd>Translates the shape along the y-axis.</dd>
1814
* <dt>skewX</dt><dd>Skews the shape around the x-axis.</dd>
1815
* <dt>skewY</dt><dd>Skews the shape around the y-axis.</dd>
1816
* <dt>matrix</dt><dd>Specifies a 2D transformation matrix comprised of the specified six values.</dd>
1819
* <p>Applying transforms through the transform attribute will reset the transform matrix and apply a new transform. The shape class also contains
1820
* corresponding methods for each transform that will apply the transform to the current matrix. The below code illustrates how you might use the
1821
* `transform` attribute to instantiate a recangle with a rotation of 45 degrees.</p>
1822
var myRect = new Y.Rect({
1826
transform: "rotate(45)"
1828
* <p>The code below would apply `translate` and `rotate` to an existing shape.</p>
1830
myRect.set("transform", "translate(40, 50) rotate(45)");
1835
setter: function(val)
1838
this._normalizedMatrix.init();
1839
this._transforms = this.matrix.getTransformArray(val);
1840
this._transform = val;
1846
return this._transform;
1851
* Unique id for class instance.
1862
setter: function(val)
1864
var node = this.node;
1867
node.setAttribute("id", val);
1874
* Indicates the x position of shape.
1885
setter: function(val)
1887
var transform = this.get("transform");
1891
this.set("transform", transform);
1897
* Indicates the y position of shape.
1908
setter: function(val)
1910
var transform = this.get("transform");
1914
this.set("transform", transform);
1920
* Indicates the width of the shape
1930
* Indicates the height of the shape
1940
* Indicates whether the shape is visible.
1948
setter: function(val){
1949
var visibility = val ? "visible" : "hidden";
1952
this.node.style.visibility = visibility;
1959
* Contains information about the fill of the shape.
1961
* <dt>color</dt><dd>The color of the fill.</dd>
1962
* <dt>opacity</dt><dd>Number between 0 and 1 that indicates the opacity of the fill. The default value is 1.</dd>
1963
* <dt>type</dt><dd>Type of fill.
1965
* <dt>solid</dt><dd>Solid single color fill. (default)</dd>
1966
* <dt>linear</dt><dd>Linear gradient fill.</dd>
1967
* <dt>radial</dt><dd>Radial gradient fill.</dd>
1971
* <p>If a `linear` or `radial` is specified as the fill type. The following additional property is used:
1973
* <dt>stops</dt><dd>An array of objects containing the following properties:
1975
* <dt>color</dt><dd>The color of the stop.</dd>
1976
* <dt>opacity</dt><dd>Number between 0 and 1 that indicates the opacity of the stop. The default value is 1.
1977
* Note: No effect for IE 6 - 8</dd>
1978
* <dt>offset</dt><dd>Number between 0 and 1 indicating where the color stop is positioned.</dd>
1981
* <p>Linear gradients also have the following property:</p>
1982
* <dt>rotation</dt><dd>Linear gradients flow left to right by default. The rotation property allows you to change the
1983
* flow by rotation. (e.g. A rotation of 180 would make the gradient pain from right to left.)</dd>
1984
* <p>Radial gradients have the following additional properties:</p>
1985
* <dt>r</dt><dd>Radius of the gradient circle.</dd>
1986
* <dt>fx</dt><dd>Focal point x-coordinate of the gradient.</dd>
1987
* <dt>fy</dt><dd>Focal point y-coordinate of the gradient.</dd>
1989
* <p>The x-coordinate of the center of the gradient circle. Determines where the color stop begins. The default value 0.5.</p>
1990
* <p><strong>Note: </strong>Currently, this property is not implemented for corresponding `CanvasShape` and
1991
* `VMLShape` classes which are used on Android or IE 6 - 8.</p>
1994
* <p>The y-coordinate of the center of the gradient circle. Determines where the color stop begins. The default value 0.5.</p>
1995
* <p><strong>Note: </strong>Currently, this property is not implemented for corresponding `CanvasShape` and `VMLShape`
1996
* classes which are used on Android or IE 6 - 8.</p>
2004
valueFn: "_getDefaultFill",
2006
setter: function(val)
2009
tmpl = this.get("fill") || this._getDefaultFill();
2010
fill = (val) ? Y.merge(tmpl, val) : null;
2011
if(fill && fill.color)
2013
if(fill.color === undefined || fill.color === "none")
2023
* Contains information about the stroke of the shape.
2025
* <dt>color</dt><dd>The color of the stroke.</dd>
2026
* <dt>weight</dt><dd>Number that indicates the width of the stroke.</dd>
2027
* <dt>opacity</dt><dd>Number between 0 and 1 that indicates the opacity of the stroke. The default value is 1.</dd>
2028
* <dt>dashstyle</dt>Indicates whether to draw a dashed stroke. When set to "none", a solid stroke is drawn. When set
2029
* to an array, the first index indicates the length of the dash. The second index indicates the length of gap.
2030
* <dt>linecap</dt><dd>Specifies the linecap for the stroke. The following values can be specified:
2032
* <dt>butt (default)</dt><dd>Specifies a butt linecap.</dd>
2033
* <dt>square</dt><dd>Specifies a sqare linecap.</dd>
2034
* <dt>round</dt><dd>Specifies a round linecap.</dd>
2037
* <dt>linejoin</dt><dd>Specifies a linejoin for the stroke. The following values can be specified:
2039
* <dt>round (default)</dt><dd>Specifies that the linejoin will be round.</dd>
2040
* <dt>bevel</dt><dd>Specifies a bevel for the linejoin.</dd>
2041
* <dt>miter limit</dt><dd>An integer specifying the miter limit of a miter linejoin. If you want to specify a linejoin
2042
* of miter, you simply specify the limit as opposed to having separate miter and miter limit values.</dd>
2051
valueFn: "_getDefaultStroke",
2053
setter: function(val)
2055
var tmpl = this.get("stroke") || this._getDefaultStroke(),
2057
if(val && val.hasOwnProperty("weight"))
2059
wt = parseInt(val.weight, 10);
2065
return (val) ? Y.merge(tmpl, val) : null;
2069
// Only implemented in SVG
2070
// Determines whether the instance will receive mouse events.
2072
// @config pointerEvents
2078
var val = "visiblePainted",
2082
node.setAttribute("pointer-events", val);
2087
setter: function(val)
2089
var node = this.node;
2092
node.setAttribute("pointer-events", val);
2099
* Dom node for the shape.
2115
* Represents an SVG Path string. This will be parsed and added to shape's API to represent the SVG data across all
2116
* implementations. Note that when using VML or SVG implementations, part of this content will be added to the DOM using
2117
* respective VML/SVG attributes. If your content comes from an untrusted source, you will need to ensure that no
2118
* malicious code is included in that content.
2124
setter: function(val)
2126
if(this.get("node"))
2128
this._parsePathData(val);
2135
* Reference to the parent graphic instance
2146
return this._graphic;
2150
Y.SVGShape = SVGShape;
2153
* <a href="http://www.w3.org/TR/SVG/">SVG</a> implementation of the <a href="Path.html">`Path`</a> class.
2154
* `SVGPath` is not intended to be used directly. Instead, use the <a href="Path.html">`Path`</a> class.
2155
* If the browser has <a href="http://www.w3.org/TR/SVG/">SVG</a> capabilities, the <a href="Path.html">`Path`</a>
2156
* class will point to the `SVGPath` class.
2163
SVGPath = function()
2165
SVGPath.superclass.constructor.apply(this, arguments);
2167
SVGPath.NAME = "path";
2168
Y.extend(SVGPath, Y.SVGShape, {
2170
* Left edge of the path
2179
* Right edge of the path
2188
* Top edge of the path
2197
* Bottom edge of the path
2206
* Indicates the type of shape
2225
SVGPath.ATTRS = Y.merge(Y.SVGShape.ATTRS, {
2227
* Indicates the path used for the node.
2243
* Indicates the width of the shape
2251
var val = Math.max(this._right - this._left, 0);
2257
* Indicates the height of the shape
2265
return Math.max(this._bottom - this._top, 0);
2269
Y.SVGPath = SVGPath;
2271
* <a href="http://www.w3.org/TR/SVG/">SVG</a> implementation of the <a href="Rect.html">`Rect`</a> class.
2272
* `SVGRect` is not intended to be used directly. Instead, use the <a href="Rect.html">`Rect`</a> class.
2273
* If the browser has <a href="http://www.w3.org/TR/SVG/">SVG</a> capabilities, the <a href="Rect.html">`Rect`</a>
2274
* class will point to the `SVGRect` class.
2280
SVGRect = function()
2282
SVGRect.superclass.constructor.apply(this, arguments);
2284
SVGRect.NAME = "rect";
2285
Y.extend(SVGRect, Y.SVGShape, {
2287
* Indicates the type of shape
2295
SVGRect.ATTRS = Y.SVGShape.ATTRS;
2296
Y.SVGRect = SVGRect;
2298
* <a href="http://www.w3.org/TR/SVG/">SVG</a> implementation of the <a href="Ellipse.html">`Ellipse`</a> class.
2299
* `SVGEllipse` is not intended to be used directly. Instead, use the <a href="Ellipse.html">`Ellipse`</a> class.
2300
* If the browser has <a href="http://www.w3.org/TR/SVG/">SVG</a> capabilities, the <a href="Ellipse.html">`Ellipse`</a>
2301
* class will point to the `SVGEllipse` class.
2307
SVGEllipse = function()
2309
SVGEllipse.superclass.constructor.apply(this, arguments);
2312
SVGEllipse.NAME = "ellipse";
2314
Y.extend(SVGEllipse, SVGShape, {
2316
* Indicates the type of shape
2325
* Updates the shape.
2332
var node = this.node,
2333
w = this.get("width"),
2334
h = this.get("height"),
2341
node.setAttribute("rx", xRadius);
2342
node.setAttribute("ry", yRadius);
2343
node.setAttribute("cx", cx);
2344
node.setAttribute("cy", cy);
2345
this._fillChangeHandler();
2346
this._strokeChangeHandler();
2347
this._updateTransform();
2351
SVGEllipse.ATTRS = Y.merge(SVGShape.ATTRS, {
2353
* Horizontal radius for the ellipse.
2359
setter: function(val)
2361
this.set("width", val * 2);
2366
var val = this.get("width");
2376
* Vertical radius for the ellipse.
2383
setter: function(val)
2385
this.set("height", val * 2);
2390
var val = this.get("height");
2399
Y.SVGEllipse = SVGEllipse;
2401
* <a href="http://www.w3.org/TR/SVG/">SVG</a> implementation of the <a href="Circle.html">`Circle`</a> class.
2402
* `SVGCircle` is not intended to be used directly. Instead, use the <a href="Circle.html">`Circle`</a> class.
2403
* If the browser has <a href="http://www.w3.org/TR/SVG/">SVG</a> capabilities, the <a href="Circle.html">`Circle`</a>
2404
* class will point to the `SVGCircle` class.
2410
SVGCircle = function()
2412
SVGCircle.superclass.constructor.apply(this, arguments);
2415
SVGCircle.NAME = "circle";
2417
Y.extend(SVGCircle, Y.SVGShape, {
2420
* Indicates the type of shape
2429
* Updates the shape.
2436
var node = this.node,
2439
radius = this.get("radius"),
2442
node.setAttribute("r", radius);
2443
node.setAttribute("cx", cx);
2444
node.setAttribute("cy", cy);
2445
this._fillChangeHandler();
2446
this._strokeChangeHandler();
2447
this._updateTransform();
2451
SVGCircle.ATTRS = Y.merge(Y.SVGShape.ATTRS, {
2453
* Indicates the width of the shape
2459
setter: function(val)
2461
this.set("radius", val/2);
2467
return this.get("radius") * 2;
2472
* Indicates the height of the shape
2478
setter: function(val)
2480
this.set("radius", val/2);
2486
return this.get("radius") * 2;
2491
* Radius of the circle
2500
Y.SVGCircle = SVGCircle;
2505
* @class SVGPieSlice
2508
SVGPieSlice = function()
2510
SVGPieSlice.superclass.constructor.apply(this, arguments);
2512
SVGPieSlice.NAME = "svgPieSlice";
2513
Y.extend(SVGPieSlice, Y.SVGShape, Y.mix({
2515
* Indicates the type of shape
2524
* Change event listener
2527
* @method _updateHandler
2531
var x = this.get("cx"),
2533
startAngle = this.get("startAngle"),
2534
arc = this.get("arc"),
2535
radius = this.get("radius");
2537
this.drawWedge(x, y, startAngle, arc, radius);
2540
}, Y.SVGDrawing.prototype));
2541
SVGPieSlice.ATTRS = Y.mix({
2550
* Starting angle in relation to a circle in which to begin the pie slice drawing.
2552
* @config startAngle
2570
* Radius of the circle in which the pie slice is drawn
2578
}, Y.SVGShape.ATTRS);
2579
Y.SVGPieSlice = SVGPieSlice;
2581
* <a href="http://www.w3.org/TR/SVG/">SVG</a> implementation of the <a href="Graphic.html">`Graphic`</a> class.
2582
* `SVGGraphic` is not intended to be used directly. Instead, use the <a href="Graphic.html">`Graphic`</a> class.
2583
* If the browser has <a href="http://www.w3.org/TR/SVG/">SVG</a> capabilities, the <a href="Graphic.html">`Graphic`</a>
2584
* class will point to the `SVGGraphic` class.
2590
SVGGraphic = function() {
2591
SVGGraphic.superclass.constructor.apply(this, arguments);
2594
SVGGraphic.NAME = "svgGraphic";
2596
SVGGraphic.ATTRS = {
2598
* Whether or not to render the `Graphic` automatically after to a specified parent node after init. This can be a Node
2599
* instance or a CSS selector string.
2602
* @type Node | String
2607
* Unique id for class instance.
2618
setter: function(val)
2620
var node = this._node;
2623
node.setAttribute("id", val);
2630
* Key value pairs in which a shape instance is associated with its id.
2641
return this._shapes;
2646
* Object containing size and coordinate data for the content of a Graphic in relation to the coordSpace node.
2648
* @config contentBounds
2657
return this._contentBounds;
2662
* The html element that represents to coordinate system of the Graphic instance.
2678
* Indicates the width of the `Graphic`.
2684
setter: function(val)
2688
this._node.style.width = val + "px";
2695
* Indicates the height of the `Graphic`.
2701
setter: function(val)
2705
this._node.style.height = val + "px";
2712
* Determines the sizing of the Graphic.
2715
* <dt>sizeContentToGraphic</dt><dd>The Graphic's width and height attributes are, either explicitly set through the
2716
* <code>width</code> and <code>height</code> attributes or are determined by the dimensions of the parent element. The
2717
* content contained in the Graphic will be sized to fit with in the Graphic instance's dimensions. When using this
2718
* setting, the <code>preserveAspectRatio</code> attribute will determine how the contents are sized.</dd>
2719
* <dt>sizeGraphicToContent</dt><dd>(Also accepts a value of true) The Graphic's width and height are determined by the
2720
* size and positioning of the content.</dd>
2721
* <dt>false</dt><dd>The Graphic's width and height attributes are, either explicitly set through the <code>width</code>
2722
* and <code>height</code> attributes or are determined by the dimensions of the parent element. The contents of the
2723
* Graphic instance are not affected by this setting.</dd>
2728
* @type Boolean | String
2736
* Determines how content is sized when <code>autoSize</code> is set to <code>sizeContentToGraphic</code>.
2739
* <dt>none<dt><dd>Do not force uniform scaling. Scale the graphic content of the given element non-uniformly if necessary
2740
* such that the element's bounding box exactly matches the viewport rectangle.</dd>
2741
* <dt>xMinYMin</dt><dd>Force uniform scaling position along the top left of the Graphic's node.</dd>
2742
* <dt>xMidYMin</dt><dd>Force uniform scaling horizontally centered and positioned at the top of the Graphic's node.<dd>
2743
* <dt>xMaxYMin</dt><dd>Force uniform scaling positioned horizontally from the right and vertically from the top.</dd>
2744
* <dt>xMinYMid</dt>Force uniform scaling positioned horizontally from the left and vertically centered.</dd>
2745
* <dt>xMidYMid (the default)</dt><dd>Force uniform scaling with the content centered.</dd>
2746
* <dt>xMaxYMid</dt><dd>Force uniform scaling positioned horizontally from the right and vertically centered.</dd>
2747
* <dt>xMinYMax</dt><dd>Force uniform scaling positioned horizontally from the left and vertically from the bottom.</dd>
2748
* <dt>xMidYMax</dt><dd>Force uniform scaling horizontally centered and position vertically from the bottom.</dd>
2749
* <dt>xMaxYMax</dt><dd>Force uniform scaling positioned horizontally from the right and vertically from the bottom.</dd>
2752
* @config preserveAspectRatio
2756
preserveAspectRatio: {
2761
* The contentBounds will resize to greater values but not to smaller values. (for performance)
2762
* When resizing the contentBounds down is desirable, set the resizeDown value to true.
2764
* @config resizeDown
2772
* Indicates the x-coordinate for the instance.
2783
setter: function(val)
2788
this._node.style.left = val + "px";
2795
* Indicates the y-coordinate for the instance.
2806
setter: function(val)
2811
this._node.style.top = val + "px";
2818
* Indicates whether or not the instance will automatically redraw after a change is made to a shape.
2819
* This property will get set to false when batching operations.
2833
setter: function(val)
2835
this._toggleVisible(val);
2841
// Indicates the pointer-events setting for the svg:svg element.
2843
// @config pointerEvents
2851
Y.extend(SVGGraphic, Y.GraphicBase, {
2853
* Sets the value of an attribute.
2856
* @param {String|Object} name The name of the attribute. Alternatively, an object of key value pairs can
2857
* be passed in to set multiple attributes at once.
2858
* @param {Any} value The value to set the attribute to. This value is ignored if an object is received as
2864
attr = arguments[0],
2868
preserveAspectRatio: true,
2872
forceRedraw = false;
2873
AttributeLite.prototype.set.apply(host, arguments);
2874
if(host._state.autoDraw === true && Y.Object.size(this._shapes) > 0)
2876
if(Y_LANG.isString && redrawAttrs[attr])
2880
else if(Y_LANG.isObject(attr))
2882
for(key in redrawAttrs)
2884
if(redrawAttrs.hasOwnProperty(key) && attr[key])
2899
* Storage for `x` attribute.
2908
* Storage for `y` attribute.
2917
* Gets the current position of the graphic instance in page coordinates.
2920
* @return Array The XY position of the shape.
2924
var node = Y.one(this._node),
2934
* Initializes the class.
2936
* @method initializer
2939
initializer: function() {
2940
var render = this.get("render"),
2941
visibility = this.get("visible") ? "visible" : "hidden";
2943
this._contentBounds = {
2949
this._gradients = {};
2950
this._node = DOCUMENT.createElement('div');
2951
this._node.style.position = "absolute";
2952
this._node.style.left = this.get("x") + "px";
2953
this._node.style.top = this.get("y") + "px";
2954
this._node.style.visibility = visibility;
2955
this._contentNode = this._createGraphics();
2956
this._contentNode.style.visibility = visibility;
2957
this._contentNode.setAttribute("id", this.get("id"));
2958
this._node.appendChild(this._contentNode);
2961
this.render(render);
2966
* Adds the graphics node to the dom.
2969
* @param {HTMLElement} parentNode node in which to render the graphics node into.
2971
render: function(render) {
2972
var parentNode = Y.one(render),
2973
w = this.get("width") || parseInt(parentNode.getComputedStyle("width"), 10),
2974
h = this.get("height") || parseInt(parentNode.getComputedStyle("height"), 10);
2975
parentNode = parentNode || Y.one(DOCUMENT.body);
2976
parentNode.append(this._node);
2977
this.parentNode = parentNode;
2978
this.set("width", w);
2979
this.set("height", h);
2984
* Removes all nodes.
2990
this.removeAllShapes();
2991
if(this._contentNode)
2993
this._removeChildren(this._contentNode);
2994
if(this._contentNode.parentNode)
2996
this._contentNode.parentNode.removeChild(this._contentNode);
2998
this._contentNode = null;
3002
this._removeChildren(this._node);
3003
Y.one(this._node).remove(true);
3009
* Generates a shape instance by type.
3012
* @param {Object} cfg attributes for the shape
3015
addShape: function(cfg)
3018
if(!this.get("visible"))
3020
cfg.visible = false;
3022
var ShapeClass = this._getShapeClass(cfg.type),
3023
shape = new ShapeClass(cfg);
3024
this._appendShape(shape);
3029
* Adds a shape instance to the graphic instance.
3031
* @method _appendShape
3032
* @param {Shape} shape The shape instance to be added to the graphic.
3035
_appendShape: function(shape)
3037
var node = shape.node,
3038
parentNode = this._frag || this._contentNode;
3039
if(this.get("autoDraw"))
3041
parentNode.appendChild(node);
3045
this._getDocFrag().appendChild(node);
3050
* Removes a shape instance from from the graphic instance.
3052
* @method removeShape
3053
* @param {Shape|String} shape The instance or id of the shape to be removed.
3055
removeShape: function(shape)
3057
if(!(shape instanceof SVGShape))
3059
if(Y_LANG.isString(shape))
3061
shape = this._shapes[shape];
3064
if(shape && shape instanceof SVGShape)
3067
delete this._shapes[shape.get("id")];
3069
if(this.get("autoDraw"))
3077
* Removes all shape instances from the dom.
3079
* @method removeAllShapes
3081
removeAllShapes: function()
3083
var shapes = this._shapes,
3087
if(shapes.hasOwnProperty(i))
3089
shapes[i]._destroy();
3096
* Removes all child nodes.
3098
* @method _removeChildren
3099
* @param {HTMLElement} node
3102
_removeChildren: function(node)
3104
if(node.hasChildNodes())
3107
while(node.firstChild)
3109
child = node.firstChild;
3110
this._removeChildren(child);
3111
node.removeChild(child);
3117
* Clears the graphics object.
3122
this.removeAllShapes();
3126
* Toggles visibility
3128
* @method _toggleVisible
3129
* @param {Boolean} val indicates visibilitye
3132
_toggleVisible: function(val)
3135
shapes = this._shapes,
3136
visibility = val ? "visible" : "hidden";
3141
if(shapes.hasOwnProperty(i))
3143
shapes[i].set("visible", val);
3147
if(this._contentNode)
3149
this._contentNode.style.visibility = visibility;
3153
this._node.style.visibility = visibility;
3158
* Returns a shape class. Used by `addShape`.
3160
* @method _getShapeClass
3161
* @param {Shape | String} val Indicates which shape class.
3165
_getShapeClass: function(val)
3167
var shape = this._shapeClass[val];
3176
* Look up for shape classes. Used by `addShape` to retrieve a class for instantiation.
3178
* @property _shapeClass
3183
circle: Y.SVGCircle,
3186
ellipse: Y.SVGEllipse,
3187
pieslice: Y.SVGPieSlice
3191
* Returns a shape based on the id of its dom node.
3193
* @method getShapeById
3194
* @param {String} id Dom id of the shape's node attribute.
3197
getShapeById: function(id)
3199
var shape = this._shapes[id];
3204
* Allows for creating multiple shapes in order to batch appending and redraw operations.
3207
* @param {Function} method Method to execute.
3209
batch: function(method)
3211
var autoDraw = this.get("autoDraw");
3212
this.set("autoDraw", false);
3214
this.set("autoDraw", autoDraw);
3218
* Returns a document fragment to for attaching shapes.
3220
* @method _getDocFrag
3221
* @return DocumentFragment
3224
_getDocFrag: function()
3228
this._frag = DOCUMENT.createDocumentFragment();
3234
* Redraws all shapes.
3241
var autoSize = this.get("autoSize"),
3242
preserveAspectRatio = this.get("preserveAspectRatio"),
3243
box = this.get("resizeDown") ? this._getUpdatedContentBounds() : this._contentBounds,
3247
bottom = box.bottom,
3248
width = right - left,
3249
height = bottom - top,
3257
if(autoSize === "sizeContentToGraphic")
3259
node = Y.one(this._node);
3260
computedWidth = parseFloat(node.getComputedStyle("width"));
3261
computedHeight = parseFloat(node.getComputedStyle("height"));
3262
computedLeft = computedTop = 0;
3263
this._contentNode.setAttribute("preserveAspectRatio", preserveAspectRatio);
3267
computedWidth = width;
3268
computedHeight = height;
3269
computedLeft = left;
3271
this._state.width = width;
3272
this._state.height = height;
3275
this._node.style.width = width + "px";
3276
this._node.style.height = height + "px";
3282
computedWidth = width;
3283
computedHeight = height;
3284
computedLeft = left;
3287
if(this._contentNode)
3289
this._contentNode.style.left = computedLeft + "px";
3290
this._contentNode.style.top = computedTop + "px";
3291
this._contentNode.setAttribute("width", computedWidth);
3292
this._contentNode.setAttribute("height", computedHeight);
3293
this._contentNode.style.width = computedWidth + "px";
3294
this._contentNode.style.height = computedHeight + "px";
3295
this._contentNode.setAttribute("viewBox", "" + left + " " + top + " " + width + " " + height + "");
3299
if(this._contentNode)
3301
this._contentNode.appendChild(this._frag);
3308
* Adds a shape to the redraw queue and calculates the contentBounds. Used internally
3309
* by `Shape` instances.
3311
* @method addToRedrawQueue
3312
* @param shape {SVGShape}
3315
addToRedrawQueue: function(shape)
3319
this._shapes[shape.get("id")] = shape;
3320
if(!this.get("resizeDown"))
3322
shapeBox = shape.getBounds();
3323
box = this._contentBounds;
3324
box.left = box.left < shapeBox.left ? box.left : shapeBox.left;
3325
box.top = box.top < shapeBox.top ? box.top : shapeBox.top;
3326
box.right = box.right > shapeBox.right ? box.right : shapeBox.right;
3327
box.bottom = box.bottom > shapeBox.bottom ? box.bottom : shapeBox.bottom;
3328
box.width = box.right - box.left;
3329
box.height = box.bottom - box.top;
3330
this._contentBounds = box;
3332
if(this.get("autoDraw"))
3339
* Recalculates and returns the `contentBounds` for the `Graphic` instance.
3341
* @method _getUpdatedContentBounds
3345
_getUpdatedContentBounds: function()
3350
queue = this._shapes,
3354
if(queue.hasOwnProperty(i))
3357
bounds = shape.getBounds();
3358
box.left = Y_LANG.isNumber(box.left) ? Math.min(box.left, bounds.left) : bounds.left;
3359
box.top = Y_LANG.isNumber(box.top) ? Math.min(box.top, bounds.top) : bounds.top;
3360
box.right = Y_LANG.isNumber(box.right) ? Math.max(box.right, bounds.right) : bounds.right;
3361
box.bottom = Y_LANG.isNumber(box.bottom) ? Math.max(box.bottom, bounds.bottom) : bounds.bottom;
3364
box.left = Y_LANG.isNumber(box.left) ? box.left : 0;
3365
box.top = Y_LANG.isNumber(box.top) ? box.top : 0;
3366
box.right = Y_LANG.isNumber(box.right) ? box.right : 0;
3367
box.bottom = Y_LANG.isNumber(box.bottom) ? box.bottom : 0;
3368
this._contentBounds = box;
3373
* Creates a contentNode element
3375
* @method _createGraphics
3378
_createGraphics: function() {
3379
var contentNode = this._createGraphicNode("svg"),
3380
pointerEvents = this.get("pointerEvents");
3381
contentNode.style.position = "absolute";
3382
contentNode.style.top = "0px";
3383
contentNode.style.left = "0px";
3384
contentNode.style.overflow = "auto";
3385
contentNode.setAttribute("overflow", "auto");
3386
contentNode.setAttribute("pointer-events", pointerEvents);
3391
* Creates a graphic node
3393
* @method _createGraphicNode
3394
* @param {String} type node type to create
3395
* @param {String} pe specified pointer-events value
3396
* @return HTMLElement
3399
_createGraphicNode: function(type, pe)
3401
var node = DOCUMENT.createElementNS("http://www.w3.org/2000/svg", "svg:" + type),
3403
if(type !== "defs" && type !== "stop" && type !== "linearGradient" && type !== "radialGradient")
3405
node.setAttribute("pointer-events", v);
3411
* Returns a reference to a gradient definition based on an id and type.
3413
* @method getGradientNode
3414
* @param {String} key id that references the gradient definition
3415
* @param {String} type description of the gradient type
3416
* @return HTMLElement
3419
getGradientNode: function(key, type)
3421
var gradients = this._gradients,
3423
nodeType = type + "Gradient";
3424
if(gradients.hasOwnProperty(key) && gradients[key].tagName.indexOf(type) > -1)
3426
gradient = this._gradients[key];
3430
gradient = this._createGraphicNode(nodeType);
3433
this._defs = this._createGraphicNode("defs");
3434
this._contentNode.appendChild(this._defs);
3436
this._defs.appendChild(gradient);
3437
key = key || "gradient" + Math.round(100000 * Math.random());
3438
gradient.setAttribute("id", key);
3439
if(gradients.hasOwnProperty(key))
3441
this._defs.removeChild(gradients[key]);
3443
gradients[key] = gradient;
3449
* Inserts shape on the top of the tree.
3452
* @param {SVGShape} Shape to add.
3455
_toFront: function(shape)
3457
var contentNode = this._contentNode;
3458
if(shape instanceof Y.SVGShape)
3460
shape = shape.get("node");
3462
if(contentNode && shape)
3464
contentNode.appendChild(shape);
3469
* Inserts shape as the first child of the content node.
3472
* @param {SVGShape} Shape to add.
3475
_toBack: function(shape)
3477
var contentNode = this._contentNode,
3479
if(shape instanceof Y.SVGShape)
3481
shape = shape.get("node");
3483
if(contentNode && shape)
3485
targetNode = contentNode.firstChild;
3488
contentNode.insertBefore(shape, targetNode);
3492
contentNode.appendChild(shape);
3498
Y.SVGGraphic = SVGGraphic;
3502
}, '3.9.1', {"requires": ["graphics"]});