source: Dev/trunk/src/client/dojox/mobile/app/AlertDialog.js

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

Added Dojo 1.9.3 release.

File size: 4.2 KB
Line 
1dojo.provide("dojox.mobile.app.AlertDialog");
2dojo.experimental("dojox.mobile.app.AlertDialog");
3dojo.require("dijit._WidgetBase");
4
5dojo.declare("dojox.mobile.app.AlertDialog", dijit._WidgetBase, {
6
7        // title: String
8        //              The title of the AlertDialog
9        title: "",
10
11        // text: String
12        //              The text message displayed in the AlertDialog
13        text: "",
14
15        // controller: Object
16        //              The SceneController for the currently active scene
17        controller: null,
18
19        // buttons: Array
20        buttons: null,
21
22        defaultButtonLabel: "OK",
23
24        // onChoose: Function
25        //              The callback function that is invoked when a button is tapped.
26        //              If the dialog is cancelled, no parameter is passed to this function.
27        onChoose: null,
28
29        constructor: function(){
30                this.onClick = dojo.hitch(this, this.onClick);
31                this._handleSelect = dojo.hitch(this, this._handleSelect);
32        },
33
34        buildRendering: function(){
35                this.domNode = dojo.create("div",{
36                        "class": "alertDialog"
37                });
38
39                // Create the outer dialog body
40                var dlgBody = dojo.create("div", {"class": "alertDialogBody"}, this.domNode);
41
42                // Create the title
43                dojo.create("div", {"class": "alertTitle", innerHTML: this.title || ""}, dlgBody);
44
45                // Create the text
46                dojo.create("div", {"class": "alertText", innerHTML: this.text || ""}, dlgBody);
47
48                // Create the node that encapsulates all the buttons
49                var btnContainer = dojo.create("div", {"class": "alertBtns"}, dlgBody);
50
51                // If no buttons have been defined, default to a single button saying OK
52                if(!this.buttons || this.buttons.length == 0){
53                        this.buttons = [{
54                                label: this.defaultButtonLabel,
55                                value: "ok",
56                                "class": "affirmative"
57                        }];
58                }
59
60                var _this = this;
61
62                // Create each of the buttons
63                dojo.forEach(this.buttons, function(btnInfo){
64                        var btn = new dojox.mobile.Button({
65                                btnClass: btnInfo["class"] || "",
66                                label: btnInfo.label
67                        });
68                        btn._dialogValue = btnInfo.value;
69                        dojo.place(btn.domNode, btnContainer);
70                        _this.connect(btn, "onClick", _this._handleSelect);
71                });
72
73                var viewportSize = this.controller.getWindowSize();
74
75                // Create the mask that blocks out the rest of the screen
76                this.mask = dojo.create("div", {"class": "dialogUnderlayWrapper",
77                        innerHTML: "<div class=\"dialogUnderlay\"></div>",
78                        style: {
79                                width: viewportSize.w + "px",
80                                height: viewportSize.h + "px"
81                        }
82                }, this.controller.assistant.domNode);
83
84                this.connect(this.mask, "onclick", function(){
85                        _this.onChoose && _this.onChoose();
86                        _this.hide();
87                });
88        },
89
90        postCreate: function(){
91                this.subscribe("/dojox/mobile/app/goback", this._handleSelect);
92        },
93
94        _handleSelect: function(event){
95                // summary:
96                //              Handle the selection of a value
97                var node;
98                console.log("handleSelect");
99                if(event && event.target){
100                        node = event.target;
101
102                        // Find the widget that was tapped.
103                        while(!dijit.byNode(node)){
104                                node = node.parentNode;
105                        }
106                }
107
108                // If an onChoose function was provided, tell it what button
109                // value was chosen
110                if(this.onChoose){
111                        this.onChoose(node ? dijit.byNode(node)._dialogValue: undefined);
112                }
113                // Hide the dialog
114                this.hide();
115        },
116
117        show: function(){
118                // summary:
119                //              Show the dialog
120                this._doTransition(1);
121        },
122
123        hide: function(){
124                // summary:
125                //              Hide the dialog
126                this._doTransition(-1);
127        },
128
129        _doTransition: function(dir){
130                // summary:
131                //              Either shows or hides the dialog.
132                // dir:
133                //              An integer.  If positive, the dialog is shown. If negative,
134                //              the dialog is hidden.
135
136                // TODO: replace this with CSS transitions
137
138                var anim;
139                var h = dojo.marginBox(this.domNode.firstChild).h;
140
141
142                var bodyHeight = this.controller.getWindowSize().h;
143                console.log("dialog height = " + h, " body height = " + bodyHeight);
144
145                var high = bodyHeight - h;
146                var low = bodyHeight;
147
148                var anim1 = dojo.fx.slideTo({
149                        node: this.domNode,
150                        duration: 400,
151                        top: {start: dir < 0 ? high : low, end: dir < 0 ? low: high}
152                });
153
154                var anim2 = dojo[dir < 0 ? "fadeOut" : "fadeIn"]({
155                        node: this.mask,
156                        duration: 400
157                });
158
159                var anim = dojo.fx.combine([anim1, anim2]);
160
161                var _this = this;
162
163                dojo.connect(anim, "onEnd", this, function(){
164                        if(dir < 0){
165                                _this.domNode.style.display = "none";
166                                dojo.destroy(_this.domNode);
167                                dojo.destroy(_this.mask);
168                        }
169                });
170                anim.play();
171        },
172
173        destroy: function(){
174                this.inherited(arguments);
175                dojo.destroy(this.mask);
176        },
177
178
179        onClick: function(){
180
181        }
182});
Note: See TracBrowser for help on using the repository browser.