Debian Codegraph @

dojox/drawing/stencil/Path.js

raw symbols SCIP
1 define(["dojo", "dojo/_base/array", "../util/oo", "./_Base", "../manager/_registry"],
2 function(lang, array, oo, Base, registry){
3 //console.log('base is', lang.isFunction(Base));
4 var Path = oo.declare(
5 Base,
6 function(options){
7 },
8 {
9 // summary:
10 // Creates a dojox.gfx Path based on points provided.
11
12 type:"dojox.drawing.stencil.Path",
13 closePath: true,
14 baseRender:true,
15 closeRadius:10,
16 closeColor:{r:255,g:255,b:0,a:.5},
17
18 /*=====
19 StencilData: {
20 // NOT SUPPORTED FOR PATH
21 },
22
23 StencilPoints: [
24 // summary:
25 // An Array of StencilPoint objects that describe the Stencil
26 ],
27 =====*/
28
29 _create: function(/*String*/shp, /*Object*/sty){
30 // summary:
31 // Creates a dojox.gfx.shape based on passed arguments.
32 // Can be called many times by implementation to create
33 // multiple shapes in one stencil.
34
35 this.remove(this[shp]);
36 if(!this.points.length){ return; }
37
38 if(dojox.gfx.renderer=="svg"){
39 // NOTE:
40 // In order to avoid the Safari d="" errors,
41 // we'll need to build a string and set that.
42 var strAr = [];
43 array.forEach(this.points, function(o, i){
44 if(!o.skip){
45 if(i==0){
46 strAr.push("M " + o.x +" "+ o.y);
47 }else{
48 var cmd = (o.t || "") + " ";
49 if(o.x===undefined){// Z + undefined works here, but checking anyway
50 strAr.push(cmd);
51 }else{
52 strAr.push(cmd + o.x +" "+ o.y);
53 }
54 }
55 }
56 }, this);
57 if(this.closePath){
58 strAr.push("Z");
59 }
60
61 this.stringPath = strAr.join(" ");
62
63 this[shp] = this.container.createPath(strAr.join(" ")).setStroke(sty);
64 this.closePath && this[shp].setFill(sty.fill);
65
66 }else{
67 // Leaving this code for VML. It seems slightly faster but times vary.
68 this[shp] = this.container.createPath({}).setStroke(sty);
69
70 this.closePath && this[shp].setFill(sty.fill);
71
72 array.forEach(this.points, function(o, i){
73 if(!o.skip){
74 if(i==0 || o.t=="M"){
75 this[shp].moveTo(o.x, o.y);
76 }else if(o.t=="Z"){
77 this.closePath && this[shp].closePath();
78 }else{
79 this[shp].lineTo(o.x, o.y);
80 }
81 }
82 }, this);
83
84 this.closePath && this[shp].closePath();
85 }
86
87 this._setNodeAtts(this[shp]);
88 },
89
90 render: function(){
91 // summary:
92 // Renders the 'hit' object (the shape used for an expanded
93 // hit area and for highlighting) and the'shape' (the actual
94 // display object).
95
96 this.onBeforeRender(this);
97 this.renderHit && this._create("hit", this.style.currentHit);
98 this._create("shape", this.style.current);
99 //console.log("path render")
100
101
102 //console.log("---------------------rend hit", this.renderHit, this.id)
103 },
104 getBounds: function(/* ? Boolean*/absolute){
105 // summary:
106 // Overwriting _Base.getBounds. Not sure how absolute should
107 // work for a path.
108 var minx = 10000, miny = 10000, maxx = 0, maxy = 0;
109 array.forEach(this.points, function(p){
110 if(p.x!==undefined && !isNaN(p.x)){
111 minx = Math.min(minx, p.x);
112 miny = Math.min(miny, p.y);
113 maxx = Math.max(maxx, p.x);
114 maxy = Math.max(maxy, p.y);
115 }
116 });
117
118 return {
119 x1:minx,
120 y1:miny,
121 x2:maxx,
122 y2:maxy,
123 x:minx,
124 y:miny,
125 w:maxx-minx,
126 h:maxy-miny
127 };
128 },
129
130 checkClosePoint: function(/*Object*/firstPt, /*Object*/currPt, /*Boolean*/remove){
131 // summary:
132 // Checks if points are close enough to indicate that
133 // path should be close. Provides a visual cue.
134 // description:
135 // Not actually used in stencil.path - this is used for
136 // drawable tools that extend it. Note that those tools
137 // need to remove the shape created: this.closeGuide, or
138 // add arg: remove
139
140 var dist = this.util.distance(firstPt.x, firstPt.y, currPt.x, currPt.y);
141 if(this.points.length>1){
142 if(dist<this.closeRadius && !this.closeGuide && !remove){
143 var c = {
144 cx:firstPt.x,
145 cy:firstPt.y,
146 rx:this.closeRadius,
147 ry:this.closeRadius
148 }
149 this.closeGuide = this.container.createEllipse(c)
150 .setFill(this.closeColor);
151
152 }else if(remove || dist > this.closeRadius && this.closeGuide){
153 this.remove(this.closeGuide);
154 this.closeGuide = null;
155 }
156 }
157 // return if we are within close distance
158 return dist < this.closeRadius; // Boolean
159 }
160 }
161 );
162
163 lang.setObject("dojox.drawing.stencil.Path", Path);
164 registry.register({
165 name:"dojox.drawing.stencil.Path"
166 }, "stencil");
167
168 return Path;
169 });