1 | define([ |
---|
2 | "dojo/_base/array", |
---|
3 | "dojo/_base/connect", |
---|
4 | "dojo/_base/declare", |
---|
5 | "dojo/dom", |
---|
6 | "dojo/dom-class", |
---|
7 | "dijit/registry", // registry.byNode |
---|
8 | "./View", |
---|
9 | "./_ScrollableMixin" |
---|
10 | ], function(array, connect, declare, dom, domClass, registry, View, ScrollableMixin){ |
---|
11 | |
---|
12 | /*===== |
---|
13 | var View = dojox.mobile.View; |
---|
14 | var ScrollableMixin = dojox.mobile._ScrollableMixin; |
---|
15 | =====*/ |
---|
16 | |
---|
17 | // module: |
---|
18 | // dojox/mobile/SwapView |
---|
19 | // summary: |
---|
20 | // A container that can be flipped horizontally. |
---|
21 | |
---|
22 | return declare("dojox.mobile.SwapView", [View, ScrollableMixin], { |
---|
23 | // summary: |
---|
24 | // A container that can be flipped horizontally. |
---|
25 | // description: |
---|
26 | // SwapView is a container widget that represents entire mobile |
---|
27 | // device screen, and can be swiped horizontally. (In dojo-1.6, it |
---|
28 | // was called 'FlippableView'.) SwapView is a subclass of |
---|
29 | // dojox.mobile.View. SwapView allows the user to swipe the screen |
---|
30 | // left or right to move between the views. When SwapView is |
---|
31 | // swiped, it finds an adjacent SwapView to open it. |
---|
32 | |
---|
33 | /* internal properties */ |
---|
34 | scrollDir: "f", |
---|
35 | weight: 1.2, |
---|
36 | |
---|
37 | buildRendering: function(){ |
---|
38 | this.inherited(arguments); |
---|
39 | domClass.add(this.domNode, "mblSwapView"); |
---|
40 | this.setSelectable(this.domNode, false); |
---|
41 | this.containerNode = this.domNode; |
---|
42 | connect.subscribe("/dojox/mobile/nextPage", this, "handleNextPage"); |
---|
43 | connect.subscribe("/dojox/mobile/prevPage", this, "handlePrevPage"); |
---|
44 | this.findAppBars(); |
---|
45 | }, |
---|
46 | |
---|
47 | resize: function(){ |
---|
48 | // summary: |
---|
49 | // Calls resize() of each child widget. |
---|
50 | this.inherited(arguments); // scrollable#resize() will be called |
---|
51 | array.forEach(this.getChildren(), function(child){ |
---|
52 | if(child.resize){ child.resize(); } |
---|
53 | }); |
---|
54 | }, |
---|
55 | |
---|
56 | onTouchStart: function(e){ |
---|
57 | // summary: |
---|
58 | // Internal function to handle touchStart events. |
---|
59 | var fromTop = this.domNode.offsetTop; |
---|
60 | var nextView = this.nextView(this.domNode); |
---|
61 | if(nextView){ |
---|
62 | nextView.stopAnimation(); |
---|
63 | domClass.add(nextView.domNode, "mblIn"); |
---|
64 | // Temporarily add padding to align with the fromNode while transition |
---|
65 | nextView.containerNode.style.paddingTop = fromTop + "px"; |
---|
66 | } |
---|
67 | var prevView = this.previousView(this.domNode); |
---|
68 | if(prevView){ |
---|
69 | prevView.stopAnimation(); |
---|
70 | domClass.add(prevView.domNode, "mblIn"); |
---|
71 | // Temporarily add padding to align with the fromNode while transition |
---|
72 | prevView.containerNode.style.paddingTop = fromTop + "px"; |
---|
73 | } |
---|
74 | this.inherited(arguments); |
---|
75 | }, |
---|
76 | |
---|
77 | handleNextPage: function(/*Widget*/w){ |
---|
78 | // summary: |
---|
79 | // Called when the "/dojox/mobile/nextPage" topic is published. |
---|
80 | var refNode = w.refId && dom.byId(w.refId) || w.domNode; |
---|
81 | if(this.domNode.parentNode !== refNode.parentNode){ return; } |
---|
82 | if(this.getShowingView() !== this){ return; } |
---|
83 | this.goTo(1); |
---|
84 | }, |
---|
85 | |
---|
86 | handlePrevPage: function(/*Widget*/w){ |
---|
87 | // summary: |
---|
88 | // Called when the "/dojox/mobile/prevPage" topic is published. |
---|
89 | var refNode = w.refId && dom.byId(w.refId) || w.domNode; |
---|
90 | if(this.domNode.parentNode !== refNode.parentNode){ return; } |
---|
91 | if(this.getShowingView() !== this){ return; } |
---|
92 | this.goTo(-1); |
---|
93 | }, |
---|
94 | |
---|
95 | goTo: function(/*Number*/dir){ |
---|
96 | // summary: |
---|
97 | // Moves to the next or previous view. |
---|
98 | var w = this.domNode.offsetWidth; |
---|
99 | var view = (dir == 1) ? this.nextView(this.domNode) : this.previousView(this.domNode); |
---|
100 | if(!view){ return; } |
---|
101 | view._beingFlipped = true; |
---|
102 | view.scrollTo({x:w*dir}); |
---|
103 | view._beingFlipped = false; |
---|
104 | view.domNode.style.display = ""; |
---|
105 | domClass.add(view.domNode, "mblIn"); |
---|
106 | this.slideTo({x:0}, 0.5, "ease-out", {x:-w*dir}); |
---|
107 | }, |
---|
108 | |
---|
109 | isSwapView: function(node){ |
---|
110 | // summary: |
---|
111 | // Returns true if the given node is a SwapView widget. |
---|
112 | return (node && node.nodeType === 1 && domClass.contains(node, "mblSwapView")); |
---|
113 | }, |
---|
114 | |
---|
115 | nextView: function(node){ |
---|
116 | // summary: |
---|
117 | // Returns the next view. |
---|
118 | for(var n = node.nextSibling; n; n = n.nextSibling){ |
---|
119 | if(this.isSwapView(n)){ return registry.byNode(n); } |
---|
120 | } |
---|
121 | return null; |
---|
122 | }, |
---|
123 | |
---|
124 | previousView: function(node){ |
---|
125 | // summary: |
---|
126 | // Returns the previous view. |
---|
127 | for(var n = node.previousSibling; n; n = n.previousSibling){ |
---|
128 | if(this.isSwapView(n)){ return registry.byNode(n); } |
---|
129 | } |
---|
130 | return null; |
---|
131 | }, |
---|
132 | |
---|
133 | scrollTo: function(/*Object*/to){ |
---|
134 | // summary: |
---|
135 | // Overrides dojox.mobile.scrollable.scrollTo(). |
---|
136 | if(!this._beingFlipped){ |
---|
137 | var newView, x; |
---|
138 | if(to.x < 0){ |
---|
139 | newView = this.nextView(this.domNode); |
---|
140 | x = to.x + this.domNode.offsetWidth; |
---|
141 | }else{ |
---|
142 | newView = this.previousView(this.domNode); |
---|
143 | x = to.x - this.domNode.offsetWidth; |
---|
144 | } |
---|
145 | if(newView){ |
---|
146 | newView.domNode.style.display = ""; |
---|
147 | newView._beingFlipped = true; |
---|
148 | newView.scrollTo({x:x}); |
---|
149 | newView._beingFlipped = false; |
---|
150 | } |
---|
151 | } |
---|
152 | this.inherited(arguments); |
---|
153 | }, |
---|
154 | |
---|
155 | slideTo: function(/*Object*/to, /*Number*/duration, /*String*/easing, fake_pos){ |
---|
156 | // summary: |
---|
157 | // Overrides dojox.mobile.scrollable.slideTo(). |
---|
158 | if(!this._beingFlipped){ |
---|
159 | var w = this.domNode.offsetWidth; |
---|
160 | var pos = fake_pos || this.getPos(); |
---|
161 | var newView, newX; |
---|
162 | if(pos.x < 0){ // moving to left |
---|
163 | newView = this.nextView(this.domNode); |
---|
164 | if(pos.x < -w/4){ // slide to next |
---|
165 | if(newView){ |
---|
166 | to.x = -w; |
---|
167 | newX = 0; |
---|
168 | } |
---|
169 | }else{ // go back |
---|
170 | if(newView){ |
---|
171 | newX = w; |
---|
172 | } |
---|
173 | } |
---|
174 | }else{ // moving to right |
---|
175 | newView = this.previousView(this.domNode); |
---|
176 | if(pos.x > w/4){ // slide to previous |
---|
177 | if(newView){ |
---|
178 | to.x = w; |
---|
179 | newX = 0; |
---|
180 | } |
---|
181 | }else{ // go back |
---|
182 | if(newView){ |
---|
183 | newX = -w; |
---|
184 | } |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | if(newView){ |
---|
189 | newView._beingFlipped = true; |
---|
190 | newView.slideTo({x:newX}, duration, easing); |
---|
191 | newView._beingFlipped = false; |
---|
192 | |
---|
193 | if(newX === 0){ // moving to another view |
---|
194 | dojox.mobile.currentView = newView; |
---|
195 | } |
---|
196 | newView.domNode._isShowing = (newView && newX === 0); |
---|
197 | } |
---|
198 | this.domNode._isShowing = !(newView && newX === 0); |
---|
199 | } |
---|
200 | this.inherited(arguments); |
---|
201 | }, |
---|
202 | |
---|
203 | onFlickAnimationEnd: function(e){ |
---|
204 | // summary: |
---|
205 | // Overrides dojox.mobile.scrollable.onFlickAnimationEnd(). |
---|
206 | if(e && e.animationName && e.animationName !== "scrollableViewScroll2"){ return; } |
---|
207 | // Hide all the views other than the currently showing one. |
---|
208 | // Otherwise, when the orientation is changed, other views |
---|
209 | // may appear unexpectedly. |
---|
210 | var children = this.domNode.parentNode.childNodes; |
---|
211 | for(var i = 0; i < children.length; i++){ |
---|
212 | var c = children[i]; |
---|
213 | if(this.isSwapView(c)){ |
---|
214 | domClass.remove(c, "mblIn"); |
---|
215 | if(!c._isShowing){ |
---|
216 | c.style.display = "none"; |
---|
217 | } |
---|
218 | } |
---|
219 | } |
---|
220 | this.inherited(arguments); |
---|
221 | if(this.getShowingView() === this){ |
---|
222 | connect.publish("/dojox/mobile/viewChanged", [this]); |
---|
223 | // Reset the temporary padding |
---|
224 | this.containerNode.style.paddingTop = ""; |
---|
225 | } |
---|
226 | } |
---|
227 | }); |
---|
228 | }); |
---|