source: Dev/trunk/src/client/dojox/fx/Shadow.js @ 529

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

Added Dojo 1.9.3 release.

File size: 4.6 KB
Line 
1define(["dojo/_base/kernel", "dojo/_base/query" ,"dojo/_base/lang", "dojo/_base/declare", "dojo/_base/sniff",
2                "dojo/dom-construct", "dojo/dom-class", "dojo/dom-geometry", "dojo/_base/fx", "dojo/fx", "dijit/_Widget",
3                "dojo/NodeList-fx"],
4  function(kernel, query, lang, declare, has, domConstruct, domClass, domGeom, baseFx, coreFx, Widget, NodeListFx){
5
6        kernel.experimental("dojox.fx.Shadow");
7
8        return declare("dojox.fx.Shadow", Widget,{
9                // summary:
10                //              Adds a drop-shadow to a node.
11                // example:
12                // |    // add drop shadows to all nodes with class="hasShadow"
13                // |    dojo.query(".hasShadow").forEach(function(n){
14                // |            var foo = new dojox.fx.Shadow({ node: n });
15                // |            foo.startup();
16                // |    });
17
18                // shadowPng: String
19                //              Base location for drop-shadow images
20                shadowPng: kernel.moduleUrl("dojox.fx", "resources/shadow"),
21       
22                // shadowThickness: Integer
23                //              How wide (in px) to make the shadow
24                shadowThickness: 7,
25       
26                // shadowOffset: Integer
27                //              How deep to make the shadow appear to be
28                shadowOffset: 3,
29       
30                // opacity: Float
31                //              Overall opacity of the shadow
32                opacity: 0.75,
33       
34                // animate: Boolean
35                //              A toggle to disable animated transitions
36                animate: false,
37       
38                // node: DomNode
39                //              The node we will be applying this shadow to
40                node: null,
41       
42                startup: function(){
43                        // summary:
44                        //              Initializes the shadow.
45       
46                        this.inherited(arguments);
47                        this.node.style.position = "relative";
48                        // make all the pieces of the shadow, and position/size them as much
49                        // as possible (but a lot of the coordinates are set in sizeShadow
50                        this.pieces={};
51                        var x1 = -1 * this.shadowThickness;
52                        var y0 = this.shadowOffset;
53                        var y1 = this.shadowOffset + this.shadowThickness;
54                        this._makePiece("tl", "top", y0, "left", x1);
55                        this._makePiece("l", "top", y1, "left", x1, "scale");
56                        this._makePiece("tr", "top", y0, "left", 0);
57                        this._makePiece("r", "top", y1, "left", 0, "scale");
58                        this._makePiece("bl", "top", 0, "left", x1);
59                        this._makePiece("b", "top", 0, "left", 0, "crop");
60                        this._makePiece("br", "top", 0, "left", 0);
61       
62                        this.nodeList = query(".shadowPiece",this.node);
63       
64                        this.setOpacity(this.opacity);
65                        this.resize();
66                },
67       
68                _makePiece: function(name, vertAttach, vertCoord, horzAttach, horzCoord, sizing){
69                        // summary:
70                        //              append a shadow pieces to the node, and position it
71                        var img;
72                        var url = this.shadowPng + name.toUpperCase() + ".png";
73                        if(has("ie") < 7){
74                                img = domConstruct.create("div");
75                                img.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+url+"'"+
76                                        (sizing?", sizingMethod='"+sizing+"'":"") + ")";
77                        }else{
78                                img = domConstruct.create("img", { src:url });
79                        }
80       
81                        img.style.position="absolute";
82                        img.style[vertAttach]=vertCoord+"px";
83                        img.style[horzAttach]=horzCoord+"px";
84                        img.style.width=this.shadowThickness+"px";
85                        img.style.height=this.shadowThickness+"px";
86                        domClass.add(img,"shadowPiece");
87                        this.pieces[name]=img;
88                        this.node.appendChild(img);
89       
90                },
91       
92                setOpacity: function(/* Float */n,/* Object? */animArgs){
93                        // summary:
94                        //              set the opacity of the underlay
95
96                        // note: does not work in IE? FIXME.
97
98                        if(has("ie")){ return; }
99                        if(!animArgs){ animArgs = {}; }
100                        if(this.animate){
101                                var _anims = [];
102                                this.nodeList.forEach(function(node){
103                                        _anims.push(baseFx._fade(lang.mixin(animArgs,{ node: node, end: n })));
104                                });
105                                coreFx.combine(_anims).play();
106                        }else{
107                                this.nodeList.style("opacity",n);
108                        }
109       
110                },
111       
112                setDisabled: function(/* Boolean */disabled){
113                        // summary:
114                        //              enable / disable the shadow
115                        if(disabled){
116                                if(this.disabled){ return; }
117                                if(this.animate){ this.nodeList.fadeOut().play();
118                                }else{ this.nodeList.style("visibility","hidden"); }
119                                this.disabled = true;
120                        }else{
121                                if(!this.disabled){ return; }
122                                if(this.animate){ this.nodeList.fadeIn().play();
123                                }else{ this.nodeList.style("visibility","visible"); }
124                                this.disabled = false;
125                        }
126                },
127       
128                resize: function(/* dojox.fx._arg.ShadowResizeArgs */args){
129                        // summary:
130                        //              Resizes the shadow based on width and height.
131                        var x; var y;
132                        if(args){ x = args.x; y = args.y;
133                        }else{
134                                var co = domGeom.position(this.node);
135                                x = co.w; y = co.h;
136                        }
137                        var sideHeight = y - (this.shadowOffset+this.shadowThickness);
138                        if (sideHeight < 0) { sideHeight = 0; }
139                        if (y < 1) { y = 1; }
140                        if (x < 1) { x = 1; }
141                        with(this.pieces){
142                                l.style.height = sideHeight+"px";
143                                r.style.height = sideHeight+"px";
144                                b.style.width = x+"px";
145                                bl.style.top = y+"px";
146                                b.style.top = y+"px";
147                                br.style.top = y+"px";
148                                tr.style.left = x+"px";
149                                r.style.left = x+"px";
150                                br.style.left = x+"px";
151                        }
152                }
153        });
154});
Note: See TracBrowser for help on using the repository browser.