1 | define([ |
---|
2 | "dojo/_base/array", |
---|
3 | "dojo/_base/declare", |
---|
4 | "dojo/dom-class", |
---|
5 | "dojo/dom-construct", |
---|
6 | "dijit/registry", // registry.byNode |
---|
7 | "./View", |
---|
8 | "./_ScrollableMixin" |
---|
9 | ], function(array, declare, domClass, domConstruct, registry, View, ScrollableMixin){ |
---|
10 | |
---|
11 | // module: |
---|
12 | // dojox/mobile/ScrollableView |
---|
13 | |
---|
14 | return declare("dojox.mobile.ScrollableView", [View, ScrollableMixin], { |
---|
15 | // summary: |
---|
16 | // A container that has a touch scrolling capability. |
---|
17 | // description: |
---|
18 | // ScrollableView is a subclass of View (dojox/mobile/View). |
---|
19 | // Unlike the base View class, ScrollableView's domNode always stays |
---|
20 | // at the top of the screen and its height is "100%" of the screen. |
---|
21 | // Inside this fixed domNode, the containerNode scrolls. The browser's |
---|
22 | // default scrolling behavior is disabled, and the scrolling mechanism is |
---|
23 | // reimplemented in JavaScript. Thus the user does not need to use the |
---|
24 | // two-finger operation to scroll the inner DIV (containerNode). |
---|
25 | // The main purpose of this widget is to realize fixed-positioned header |
---|
26 | // and/or footer bars. |
---|
27 | |
---|
28 | // scrollableParams: Object |
---|
29 | // Parameters for dojox/mobile/scrollable.init(). |
---|
30 | scrollableParams: null, |
---|
31 | |
---|
32 | // keepScrollPos: Boolean |
---|
33 | // Overrides dojox/mobile/View/keepScrollPos. |
---|
34 | keepScrollPos: false, |
---|
35 | |
---|
36 | constructor: function(){ |
---|
37 | // summary: |
---|
38 | // Creates a new instance of the class. |
---|
39 | this.scrollableParams = {noResize: true}; |
---|
40 | }, |
---|
41 | |
---|
42 | buildRendering: function(){ |
---|
43 | this.inherited(arguments); |
---|
44 | domClass.add(this.domNode, "mblScrollableView"); |
---|
45 | this.domNode.style.overflow = "hidden"; |
---|
46 | this.domNode.style.top = "0px"; |
---|
47 | this.containerNode = domConstruct.create("div", |
---|
48 | {className:"mblScrollableViewContainer"}, this.domNode); |
---|
49 | this.containerNode.style.position = "absolute"; |
---|
50 | this.containerNode.style.top = "0px"; // view bar is relative |
---|
51 | if(this.scrollDir === "v"){ |
---|
52 | this.containerNode.style.width = "100%"; |
---|
53 | } |
---|
54 | }, |
---|
55 | |
---|
56 | startup: function(){ |
---|
57 | if(this._started){ return; } |
---|
58 | // user can initialize the app footers using a value for fixedFooter (we keep this value for non regression of existing apps) |
---|
59 | if(this.fixedFooter && !this.isLocalFooter){ |
---|
60 | this._fixedAppFooter = this.fixedFooter; |
---|
61 | this.fixedFooter = ""; |
---|
62 | } |
---|
63 | this.reparent(); |
---|
64 | this.inherited(arguments); |
---|
65 | }, |
---|
66 | |
---|
67 | resize: function(){ |
---|
68 | // summary: |
---|
69 | // Calls resize() of each child widget. |
---|
70 | this.inherited(arguments); // scrollable#resize() will be called |
---|
71 | array.forEach(this.getChildren(), function(child){ |
---|
72 | if(child.resize){ child.resize(); } |
---|
73 | }); |
---|
74 | this._dim = this.getDim(); // update dimension cache |
---|
75 | if(this._conn){ |
---|
76 | // if a resize happens during a scroll, update the scrollbar |
---|
77 | this.resetScrollBar(); |
---|
78 | } |
---|
79 | }, |
---|
80 | |
---|
81 | isTopLevel: function(/*Event*/e){ |
---|
82 | // summary: |
---|
83 | // Returns true if this is a top-level widget. |
---|
84 | // Overrides dojox/mobile/scrollable.isTopLevel. |
---|
85 | var parent = this.getParent && this.getParent(); |
---|
86 | return (!parent || !parent.resize); // top level widget |
---|
87 | }, |
---|
88 | |
---|
89 | addFixedBar: function(/*Widget*/widget){ |
---|
90 | // summary: |
---|
91 | // Adds a view local fixed bar to this widget. |
---|
92 | // description: |
---|
93 | // This method can be used to programmatically add a view local |
---|
94 | // fixed bar to ScrollableView. The bar is appended to this |
---|
95 | // widget's domNode. The addChild API cannot be used for this |
---|
96 | // purpose, because it adds the given widget to containerNode. |
---|
97 | var c = widget.domNode; |
---|
98 | var fixed = this.checkFixedBar(c, true); |
---|
99 | if(!fixed){ return; } |
---|
100 | // Fixed bar has to be added to domNode, not containerNode. |
---|
101 | this.domNode.appendChild(c); |
---|
102 | if(fixed === "top"){ |
---|
103 | this.fixedHeaderHeight = c.offsetHeight; |
---|
104 | this.isLocalHeader = true; |
---|
105 | }else if(fixed === "bottom"){ |
---|
106 | this.fixedFooterHeight = c.offsetHeight; |
---|
107 | this.isLocalFooter = true; |
---|
108 | c.style.bottom = "0px"; |
---|
109 | } |
---|
110 | this.resize(); |
---|
111 | }, |
---|
112 | |
---|
113 | reparent: function(){ |
---|
114 | // summary: |
---|
115 | // Moves all the children, except header and footer, to |
---|
116 | // containerNode. |
---|
117 | var i, idx, len, c; |
---|
118 | for(i = 0, idx = 0, len = this.domNode.childNodes.length; i < len; i++){ |
---|
119 | c = this.domNode.childNodes[idx]; |
---|
120 | // search for view-specific header or footer |
---|
121 | if(c === this.containerNode || this.checkFixedBar(c, true)){ |
---|
122 | idx++; |
---|
123 | continue; |
---|
124 | } |
---|
125 | this.containerNode.appendChild(this.domNode.removeChild(c)); |
---|
126 | } |
---|
127 | }, |
---|
128 | |
---|
129 | onAfterTransitionIn: function(moveTo, dir, transition, context, method){ |
---|
130 | // summary: |
---|
131 | // Overrides View.onAfterTransitionIn to flash the scroll bar |
---|
132 | // after performing a view transition. |
---|
133 | this.flashScrollBar(); |
---|
134 | }, |
---|
135 | |
---|
136 | getChildren: function(){ |
---|
137 | // summary: |
---|
138 | // Overrides _WidgetBase.getChildren to add local fixed bars, |
---|
139 | // which are not under containerNode, to the children array. |
---|
140 | var children = this.inherited(arguments); |
---|
141 | var fixedWidget; |
---|
142 | if(this.fixedHeader && this.fixedHeader.parentNode === this.domNode){ |
---|
143 | fixedWidget = registry.byNode(this.fixedHeader); |
---|
144 | if(fixedWidget){ |
---|
145 | children.push(fixedWidget); |
---|
146 | } |
---|
147 | } |
---|
148 | if(this.fixedFooter && this.fixedFooter.parentNode === this.domNode){ |
---|
149 | fixedWidget = registry.byNode(this.fixedFooter); |
---|
150 | if(fixedWidget){ |
---|
151 | children.push(fixedWidget); |
---|
152 | } |
---|
153 | } |
---|
154 | return children; |
---|
155 | }, |
---|
156 | |
---|
157 | _addTransitionPaddingTop: function(/*String|Integer*/ value){ |
---|
158 | // add padding top to the view in order to get alignment during the transition |
---|
159 | this.domNode.style.paddingTop = value + "px"; |
---|
160 | this.containerNode.style.paddingTop = value + "px"; |
---|
161 | }, |
---|
162 | |
---|
163 | _removeTransitionPaddingTop: function(){ |
---|
164 | // remove padding top from the view after the transition |
---|
165 | this.domNode.style.paddingTop = ""; |
---|
166 | this.containerNode.style.paddingTop = ""; |
---|
167 | } |
---|
168 | |
---|
169 | }); |
---|
170 | }); |
---|