1 | dojo.provide("dojox.drawing.stencil.Image"); |
---|
2 | |
---|
3 | |
---|
4 | dojox.drawing.stencil.Image = dojox.drawing.util.oo.declare( |
---|
5 | // summary: |
---|
6 | // Creates an dojox.gfx Image based on the data |
---|
7 | // provided. |
---|
8 | // |
---|
9 | dojox.drawing.stencil._Base, |
---|
10 | function(options){ |
---|
11 | // summary: |
---|
12 | // constructor |
---|
13 | }, |
---|
14 | { |
---|
15 | type:"dojox.drawing.stencil.Image", |
---|
16 | anchorType: "group", |
---|
17 | baseRender:true, |
---|
18 | |
---|
19 | /*===== |
---|
20 | StencilData: { |
---|
21 | // summary: |
---|
22 | // The data used to create the dojox.gfx Shape |
---|
23 | // x: Number |
---|
24 | // Left point x |
---|
25 | // y: Number |
---|
26 | // Top point y |
---|
27 | // width: ? Number |
---|
28 | // Optional width of Image. If not provided, it is obtained |
---|
29 | // height: ? Number |
---|
30 | // Optional height of Image. If not provided, it is obtained |
---|
31 | // src: String |
---|
32 | // The location of the source image |
---|
33 | }, |
---|
34 | |
---|
35 | StencilPoints: [ |
---|
36 | // summary: |
---|
37 | // An Array of dojox.__StencilPoint objects that describe the Stencil |
---|
38 | // 0: Object |
---|
39 | // Top left point |
---|
40 | // 1: Object |
---|
41 | // Top right point |
---|
42 | // 2: Object |
---|
43 | // Bottom right point |
---|
44 | // 3: Object |
---|
45 | // Bottom left point |
---|
46 | ], |
---|
47 | =====*/ |
---|
48 | |
---|
49 | dataToPoints: function(/*Object*/o){ |
---|
50 | //summary: |
---|
51 | // Converts data to points. |
---|
52 | o = o || this.data; |
---|
53 | this.points = [ |
---|
54 | {x:o.x, y:o.y}, // TL |
---|
55 | {x:o.x + o.width, y:o.y}, // TR |
---|
56 | {x:o.x + o.width, y:o.y + o.height}, // BR |
---|
57 | {x:o.x, y:o.y + o.height} // BL |
---|
58 | ]; |
---|
59 | return this.points; |
---|
60 | }, |
---|
61 | |
---|
62 | pointsToData: function(/*Array*/p){ |
---|
63 | // summary: |
---|
64 | // Converts points to data |
---|
65 | p = p || this.points; |
---|
66 | var s = p[0]; |
---|
67 | var e = p[2]; |
---|
68 | this.data = { |
---|
69 | x: s.x, |
---|
70 | y: s.y, |
---|
71 | width: e.x-s.x, |
---|
72 | height: e.y-s.y, |
---|
73 | src: this.src || this.data.src |
---|
74 | }; |
---|
75 | return this.data; |
---|
76 | |
---|
77 | }, |
---|
78 | |
---|
79 | _createHilite: function(){ |
---|
80 | // summary: |
---|
81 | // Create the hit and highlight area |
---|
82 | // for the Image. |
---|
83 | this.remove(this.hit); |
---|
84 | this.hit = this.container.createRect(this.data) |
---|
85 | .setStroke(this.style.current) |
---|
86 | .setFill(this.style.current.fill); |
---|
87 | this._setNodeAtts(this.hit); |
---|
88 | }, |
---|
89 | _create: function(/*String*/shp, /*StencilData*/d, /*Object*/sty){ |
---|
90 | // summary: |
---|
91 | // Creates a dojox.gfx.shape based on passed arguments. |
---|
92 | // Can be called many times by implementation to create |
---|
93 | // multiple shapes in one stencil. |
---|
94 | // |
---|
95 | this.remove(this[shp]); |
---|
96 | var s = this.container.getParent(); |
---|
97 | this[shp] = s.createImage(d) |
---|
98 | this.container.add(this[shp]); |
---|
99 | this._setNodeAtts(this[shp]); |
---|
100 | }, |
---|
101 | |
---|
102 | render: function(dbg){ |
---|
103 | // summary: |
---|
104 | // Renders the 'hit' object (the shape used for an expanded |
---|
105 | // hit area and for highlighting) and the'shape' (the actual |
---|
106 | // display object). Image is slightly different than other |
---|
107 | // implementations. Instead of calling render twice, it calls |
---|
108 | // _createHilite for the 'hit' |
---|
109 | // |
---|
110 | if(this.data.width == "auto" || isNaN(this.data.width)){ |
---|
111 | this.getImageSize(true); |
---|
112 | console.warn("Image size not provided. Acquiring...") |
---|
113 | return; |
---|
114 | } |
---|
115 | this.onBeforeRender(this); |
---|
116 | this.renderHit && this._createHilite(); |
---|
117 | this._create("shape", this.data, this.style.current); |
---|
118 | }, |
---|
119 | getImageSize: function(render){ |
---|
120 | // summary: |
---|
121 | // Internal. If no image size is passed in with the data |
---|
122 | // create a dom node, insert and image, gets its dimensions |
---|
123 | // record them - then destroy everything. |
---|
124 | // |
---|
125 | if(this._gettingSize){ return; } // IE gets it twice (will need to mod if src changes) |
---|
126 | this._gettingSize = true; |
---|
127 | var img = dojo.create("img", {src:this.data.src}, dojo.body()); |
---|
128 | var err = dojo.connect(img, "error", this, function(){ |
---|
129 | dojo.disconnect(c); |
---|
130 | dojo.disconnect(err); |
---|
131 | console.error("Error loading image:", this.data.src) |
---|
132 | console.warn("Error image:", this.data) |
---|
133 | |
---|
134 | }); |
---|
135 | var c = dojo.connect(img, "load", this, function(){ |
---|
136 | var dim = dojo.marginBox(img); |
---|
137 | this.setData({ |
---|
138 | x:this.data.x, |
---|
139 | y:this.data.y, |
---|
140 | src:this.data.src, |
---|
141 | width:dim.w, |
---|
142 | height:dim.h |
---|
143 | }); |
---|
144 | dojo.disconnect(c); |
---|
145 | dojo.destroy(img); |
---|
146 | render && this.render(true); |
---|
147 | }); |
---|
148 | } |
---|
149 | } |
---|
150 | ); |
---|
151 | |
---|
152 | |
---|
153 | dojox.drawing.register({ |
---|
154 | name:"dojox.drawing.stencil.Image" |
---|
155 | }, "stencil"); |
---|