root/collected/trunk/ondemand-js/ondemand.js

Revision 84, 2.1 kB (checked in by simon, 20 months ago)

Another small comment fix.

Line 
1// Copyright (c) 2007, 2008 Simon Bates
2// Licensed under the MIT license.
3
4(function(){
5        var makeTrigger = function(elem, label, func){
6                // create a trigger button and place it after the element
7                var p = document.createElement("p");
8                var button = document.createElement("button");
9                button.innerHTML = label;
10                p.appendChild(button);
11                dojo.place(p, elem, "after");
12                dojo.connect(button, "onclick", func);
13        };
14
15        ondemand = function(elem, showLabel, hideLabel){
16                elem = dojo.byId(elem);
17                // hide the element
18                dojo.style(elem, "display", "none");
19                dojo.style(elem, "overflow", "hidden");
20
21                var show = function(event){
22                        dojo.style(elem, "height", "1px");
23                        dojo.style(elem, "display", "block");
24                        var anim = dojo.animateProperty({ node: elem, duration: 300,
25                                properties: {
26                                        height: { start: "1", end: elem.scrollHeight, unit: "px" }
27                                }
28                        });
29                        dojo.connect(anim, "onEnd", function(){
30                                // after we are finished showing the element
31                                // switch the functionality to hide
32                                // by changing the label on the trigger control
33                                // and directing our event handler to use the
34                                // hide function
35                                event.target.innerHTML = hideLabel;
36                                dispatchTo = hide;
37                        });
38                        anim.play();
39                };
40
41                var hide = function(event){
42                        dojo.style(elem, "display", "block");
43                        dojo.style(elem, "height", elem.scrollHeight + "px");
44                        var anim = dojo.animateProperty({ node: elem, duration: 300,
45                                properties: { height: { end: "1", unit: "px" } }
46                        });
47                        dojo.connect(anim, "onEnd", function(){
48                                // after we are finished hiding the element
49                                // switch the functionality to show
50                                // by changing the label on the trigger control
51                                // and directing our event handler to use the
52                                // show function
53                                dojo.style(elem, "display", "none");
54                                event.target.innerHTML = showLabel;
55                                dispatchTo = show;
56                        });
57                        anim.play();
58                };
59
60                // our event handler uses a variable called "dispatchTo"
61                // to determine whether it will show or hide
62                var dispatchTo = show;
63                var dispatcher = function(event){
64                        dispatchTo.apply(null, [event])
65                };
66
67                makeTrigger(elem, showLabel, dispatcher);
68        }
69})();
Note: See TracBrowser for help on using the browser.