source: Dev/trunk/src/client/dojox/drawing/stencil/Image.js @ 532

Last change on this file since 532 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

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