source: Dev/trunk/src/client/dojox/image/Badge.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: 5.7 KB
Line 
1define(["dojo", "dijit", "dojox/main", "dijit/_Widget", "dijit/_TemplatedMixin", "dojo/fx/easing"],
2function(dojo, dijit, dojox, _Widget, _TemplatedMixin){
3
4        dojo.experimental("dojox.image.Badge");
5        dojo.getObject("image", true, dojox);
6       
7        return dojo.declare("dojox.image.Badge", [_Widget, _TemplatedMixin], {
8                // summary:
9                //              A simple grid of Images that loops through thumbnails
10
11                baseClass: "dojoxBadge",
12
13                templateString:'<div class="dojoxBadge" dojoAttachPoint="containerNode"></div>',
14
15                // children: String
16                //              A CSS3 Selector that determines the node to become a child
17                children: "div.dojoxBadgeImage",
18
19                // rows: Integer
20                //              Number of Rows to display
21                rows: 4,
22
23                // cols: Integer
24                //              Number of Columns to display
25                cols: 5,
26
27                // cellSize: Integer
28                //              Size in PX of each thumbnail
29                cellSize: 50,
30
31                // cellMargin: Integer
32                //              Size in PX to adjust for cell margins
33                cellMargin: 1,
34
35                // delay: Integer
36                //              Time (in ms) to show the image before sizing down again
37                delay: 2000,
38
39                // threads: Integer
40                //              how many cycles will be going "simultaneously" (>2 not reccommended)
41                threads: 1,
42
43                // easing: Function|String
44                //              An easing function to use when showing the node (does not apply to shrinking)
45                easing: "dojo.fx.easing.backOut",
46
47                startup: function(){
48                        if(this._started){ return; }
49                        if(dojo.isString(this.easing)){
50                                this.easing = dojo.getObject(this.easing);
51                        }
52                        this.inherited(arguments);
53                        this._init();
54                },
55
56                _init: function(){
57                        // summary:
58                        //              Setup and layout the images
59
60                        var _row = 0,
61                                _w = this.cellSize;
62
63                        dojo.style(this.domNode, {
64                                width: _w * this.cols + "px",
65                                height: _w * this.rows + "px"
66                        });
67
68                        this._nl = dojo.query(this.children, this.containerNode)
69                                .forEach(function(n, _idx){
70
71                                        var _col = _idx % this.cols,
72                                                t = _row * _w,
73                                                l = _col * _w,
74                                                m = this.cellMargin * 2;
75
76                                        dojo.style(n, {
77                                                top: t + "px",
78                                                left: l + "px",
79                                                width: _w - m + "px",
80                                                height: _w - m + "px"
81                                        });
82
83                                        if(_col == this.cols - 1){ _row++; }
84                                        dojo.addClass(n, this.baseClass + "Image");
85
86                                }, this)
87                        ;
88
89                        var l = this._nl.length;
90                        while(this.threads--){
91                                var s = Math.floor(Math.random() * l);
92                                setTimeout(dojo.hitch(this, "_enbiggen", {
93                                        target: this._nl[s]
94                                }), this.delay * this.threads);
95                        }
96
97                },
98
99                _getCell: function(/* DomNode */ n){
100                        // summary:
101                        //              Return information about the position for a given node
102                        var _pos = this._nl.indexOf(n);
103                        if(_pos >= 0){
104                                var _col = _pos % this.cols;
105                                var _row = Math.floor(_pos / this.cols);
106                                return { x: _col, y: _row, n: this._nl[_pos], io: _pos };
107                        }else{
108                                return undefined;
109                        }
110                },
111
112                _getImage: function(){
113                        // summary:
114                        //              Returns the next image in the list, or the first one if not available
115                        return "url('')";
116                },
117
118                _enbiggen: function(/* Event|DomNode */ e){
119                        // summary:
120                        //              Show the passed node in the picker
121                        var _pos = this._getCell(e.target || e);
122
123                        if (_pos){
124                                // we have a node, and know where it is
125
126                                var m = this.cellMargin,
127                                        _cc = (this.cellSize * 2) - (m * 2),
128                                        props = {
129                                                height: _cc,
130                                                width: _cc
131                                        }
132                                ;
133
134                                var _tehDecider = function(){
135                                        // if we have room, we'll want to decide which direction to go
136                                        // let "teh decider" decide.
137                                        return Math.round(Math.random());
138                                };
139
140                                if(_pos.x == this.cols - 1 || (_pos.x > 0 && _tehDecider() )){
141                                        // we have to go left, at right edge (or we want to and not on left edge)
142                                        props.left = this.cellSize * (_pos.x - m);
143                                }
144
145                                if(_pos.y == this.rows - 1 || (_pos.y > 0 && _tehDecider() )){
146                                        // we have to go up, at bottom edge (or we want to and not at top)
147                                        props.top = this.cellSize * (_pos.y - m);
148                                }
149
150                                var bc = this.baseClass;
151                                dojo.addClass(_pos.n, bc + "Top");
152                                dojo.addClass(_pos.n, bc + "Seen");
153
154                                dojo.animateProperty({ node: _pos.n, properties: props,
155                                        onEnd: dojo.hitch(this, "_loadUnder", _pos, props),
156                                        easing: this.easing
157                                }).play();
158
159                        }
160                },
161
162                _loadUnder: function(info, props){
163                        // summary:
164                        //              figure out which three images are being covered, and
165                        //              determine if they need loaded or not
166
167                        var idx = info.io;
168                        var nodes = [];
169
170                        var isLeft = (props.left >= 0);
171                        var isUp = (props.top >= 0);
172
173                        var c = this.cols,
174                                // the three node index's we're allegedly over:
175                                e = idx + (isLeft ? -1 : 1),
176                                f = idx + (isUp ? -c : c),
177                                // don't ask:
178                                g = (isUp ? (isLeft ? e - c : f + 1) : (isLeft ? f - 1 : e + c)),
179
180                                bc = this.baseClass;
181
182                        dojo.forEach([e, f, g], function(x){
183                                var n = this._nl[x];
184                                if(n){
185                                        if(dojo.hasClass(n, bc + "Seen")){
186                                                // change the background image out?
187                                                dojo.removeClass(n, bc + "Seen");
188                                        }
189                                }
190                        },this);
191
192                        setTimeout(dojo.hitch(this, "_disenbiggen", info, props), this.delay * 1.25);
193
194                },
195
196                _disenbiggen: function(info, props){
197                        // summary:
198                        //              Hide the passed node (info.n), passing along properties
199                        //              received.
200
201                        if(props.top >= 0){
202                                props.top += this.cellSize;
203                        }
204                        if(props.left >= 0){
205                                props.left += this.cellSize;
206                        }
207                        var _cc = this.cellSize - (this.cellMargin * 2);
208                        dojo.animateProperty({
209                                node: info.n,
210                                properties: dojo.mixin(props, {
211                                        width:_cc,
212                                        height:_cc
213                                }),
214                                onEnd: dojo.hitch(this, "_cycle", info, props)
215                        }).play(5);
216                },
217
218                _cycle: function(info, props){
219                        // summary:
220                        //              Select an un-viewed image from the list, and show it
221
222                        var bc = this.baseClass;
223                        dojo.removeClass(info.n, bc + "Top");
224                        var ns = this._nl.filter(function(n){
225                                return !dojo.hasClass(n, bc + "Seen")
226                        });
227                        var c = ns[Math.floor(Math.random() * ns.length)];
228                        setTimeout(dojo.hitch(this,"_enbiggen", { target: c }), this.delay / 2)
229
230                }
231
232        });
233});
234
Note: See TracBrowser for help on using the repository browser.