Debian Codegraph @

dojox/mobile/ComboBox.js

raw symbols SCIP
1 define([
2 "dojo/_base/kernel",
3 "dojo/_base/declare",
4 "dojo/_base/lang",
5 "dojo/_base/window",
6 "dojo/dom-geometry",
7 "dojo/dom-style",
8 "dojo/dom-attr",
9 "dojo/window",
10 "dojo/touch",
11 "dijit/form/_AutoCompleterMixin",
12 "dijit/popup",
13 "./_ComboBoxMenu",
14 "./TextBox",
15 "./sniff"
16 ], function(kernel, declare, lang, win, domGeometry, domStyle, domAttr, windowUtils, touch, AutoCompleterMixin, popup, ComboBoxMenu, TextBox, has){
17 kernel.experimental("dojox.mobile.ComboBox"); // should be using a more native search-type UI
18
19 return declare("dojox.mobile.ComboBox", [TextBox, AutoCompleterMixin], {
20 // summary:
21 // A non-templated auto-completing text box widget.
22
23 // dropDownClass: [protected extension] String
24 // Name of the drop-down widget class used to select a date/time.
25 // Should be specified by subclasses.
26 dropDownClass: "dojox.mobile._ComboBoxMenu",
27
28 // initially disable selection since iphone displays selection handles
29 // that makes it hard to pick from the list
30
31 // selectOnClick: Boolean
32 // Flag which enables the selection on click.
33 selectOnClick: false,
34
35 // autoComplete: Boolean
36 // Flag which enables the auto-completion.
37 autoComplete: false,
38
39 // dropDown: [protected] Widget
40 // The widget to display as a popup. This widget *must* be
41 // defined before the startup function is called.
42 dropDown: null,
43
44 // maxHeight: [protected] int
45 // The maximum height for the drop-down.
46 // Any drop-down taller than this value will have scrollbars.
47 // Set to -1 to limit the height to the available space in the viewport.
48 maxHeight: -1,
49
50 // dropDownPosition: [const] String[]
51 // This variable controls the position of the drop-down.
52 // It is an array of strings with the following values:
53 //
54 // - before: places drop down to the left of the target node/widget, or to the right in
55 // the case of RTL scripts like Hebrew and Arabic
56 // - after: places drop down to the right of the target node/widget, or to the left in
57 // the case of RTL scripts like Hebrew and Arabic
58 // - above: drop down goes above target node
59 // - below: drop down goes below target node
60 //
61 // The list is positions is tried, in order, until a position is found where the drop down fits
62 // within the viewport.
63 dropDownPosition: ["below","above"],
64
65 _throttleOpenClose: function(){
66 // summary:
67 // Prevents the open/close in rapid succession.
68 // tags:
69 // private
70 if(this._throttleHandler){
71 this._throttleHandler.remove();
72 }
73 this._throttleHandler = this.defer(function(){ this._throttleHandler = null; }, 500);
74 },
75
76 _onFocus: function(){
77 // summary:
78 // Shows drop-down if the user is selecting Next/Previous from the virtual keyboard.
79 // tags:
80 // private
81 this.inherited(arguments);
82 if(!this._opened && !this._throttleHandler){
83 this._startSearchAll();
84 }
85
86 if(has("windows-theme")) {
87 this.domNode.blur();
88 }
89 },
90
91 onInput: function(e){
92 if(!e || e.charCode !== 0){ // #18047
93 this._onKey(e);
94 this.inherited(arguments);
95 }
96 },
97
98 _setListAttr: function(v){
99 // tags:
100 // private
101 this._set('list', v); // needed for Firefox 4+ to prevent HTML5 mode
102 },
103
104 closeDropDown: function(){
105 // summary:
106 // Closes the drop down on this widget
107 // tags:
108 // protected
109
110 this._throttleOpenClose();
111 if(this.endHandler){
112 this.disconnect(this.startHandler);
113 this.disconnect(this.endHandler);
114 this.disconnect(this.moveHandler);
115 clearInterval(this.repositionTimer);
116 this.repositionTimer = this.endHandler = null;
117 }
118 this.inherited(arguments);
119 domAttr.remove(this.domNode, "aria-owns");
120 domAttr.set(this.domNode, "aria-expanded", "false");
121 popup.close(this.dropDown);
122 this._opened = false;
123
124 // Remove disable attribute to make input element clickable after context menu closed
125 if(has("windows-theme") && this.domNode.disabled){
126 this.defer(function(){
127 this.domNode.removeAttribute("disabled");
128 }, 300);
129 }
130
131 },
132
133 openDropDown: function(){
134 // summary:
135 // Opens the dropdown for this widget. To be called only when this.dropDown
136 // has been created and is ready to display (that is, its data is loaded).
137 // returns:
138 // Returns the value of popup.open().
139 // tags:
140 // protected
141
142 var wasClosed = !this._opened;
143 var dropDown = this.dropDown,
144 ddNode = dropDown.domNode,
145 aroundNode = this.domNode,
146 self = this;
147
148 domAttr.set(dropDown.domNode, "role", "listbox");
149 domAttr.set(this.domNode, "aria-expanded", "true");
150 if(dropDown.id){
151 domAttr.set(this.domNode, "aria-owns", dropDown.id);
152 }
153
154 if(has("touch") && (!has("ios") || has("ios") < 8)){
155 win.global.scrollBy(0, domGeometry.position(aroundNode, false).y); // don't call scrollIntoView since it messes up ScrollableView
156 }
157
158 // TODO: isn't maxHeight dependent on the return value from popup.open(),
159 // i.e., dependent on how much space is available (BK)
160
161 if(!this._preparedNode){
162 this._preparedNode = true;
163 // Check if we have explicitly set width and height on the dropdown widget dom node
164 if(ddNode.style.width){
165 this._explicitDDWidth = true;
166 }
167 if(ddNode.style.height){
168 this._explicitDDHeight = true;
169 }
170 }
171
172 // Code for resizing dropdown (height limitation, or increasing width to match my width)
173 var myStyle = {
174 display: "",
175 overflow: "hidden",
176 visibility: "hidden"
177 };
178 if(!this._explicitDDWidth){
179 myStyle.width = "";
180 }
181 if(!this._explicitDDHeight){
182 myStyle.height = "";
183 }
184 domStyle.set(ddNode, myStyle);
185
186 // Figure out maximum height allowed (if there is a height restriction)
187 var maxHeight = this.maxHeight;
188 if(maxHeight == -1){
189 // limit height to space available in viewport either above or below my domNode
190 // (whichever side has more room)
191 var viewport = windowUtils.getBox(),
192 position = domGeometry.position(aroundNode, false);
193 maxHeight = Math.floor(Math.max(position.y, viewport.h - (position.y + position.h)));
194 }
195
196 // Attach dropDown to DOM and make make visibility:hidden rather than display:none
197 // so we call startup() and also get the size
198 popup.moveOffScreen(dropDown);
199
200 if(dropDown.startup && !dropDown._started){
201 dropDown.startup(); // this has to be done after being added to the DOM
202 }
203 // Get size of drop down, and determine if vertical scroll bar needed
204 var mb = domGeometry.position(this.dropDown.containerNode, false);
205 var overHeight = (maxHeight && mb.h > maxHeight);
206 if(overHeight){
207 mb.h = maxHeight;
208 }
209
210 // Adjust dropdown width to match or be larger than my width
211 mb.w = Math.max(mb.w, aroundNode.offsetWidth);
212 domGeometry.setMarginBox(ddNode, mb);
213
214 var retVal = popup.open({
215 parent: this,
216 popup: dropDown,
217 around: aroundNode,
218 orient: has("windows-theme") ? ["above"] : this.dropDownPosition,
219 onExecute: function(){
220 self.closeDropDown();
221 },
222 onCancel: function(){
223 self.closeDropDown();
224 },
225 onClose: function(){
226 self._opened = false;
227 }
228 });
229 this._opened=true;
230
231 if(wasClosed){
232 var isGesture = false,
233 skipReposition = false,
234 active = false,
235 wrapper = dropDown.domNode.parentNode,
236 aroundNodePos = domGeometry.position(aroundNode, false),
237 popupPos = domGeometry.position(wrapper, false),
238 deltaX = popupPos.x - aroundNodePos.x,
239 deltaY = popupPos.y - aroundNodePos.y,
240 startX = -1, startY = -1;
241
242 // touchstart isn't really needed since touchmove implies touchstart, but
243 // mousedown is needed since mousemove doesn't know if the left button is down or not
244 this.startHandler = this.connect(win.doc.documentElement, touch.press,
245 function(e){
246 skipReposition = true;
247 active = true;
248 isGesture = false;
249 startX = e.clientX;
250 startY = e.clientY;
251 }
252 );
253 this.moveHandler = this.connect(win.doc.documentElement, touch.move,
254 function(e){
255 skipReposition = true;
256 if(e.touches){
257 active = isGesture = true; // touchmove implies touchstart
258 }else if(active && (e.clientX != startX || e.clientY != startY)){
259 isGesture = true;
260 }
261 }
262 );
263 this.clickHandler = this.connect(dropDown.domNode, "onclick",
264 function(){
265 skipReposition = true;
266 active = isGesture = false; // click implies no gesture movement
267 }
268 );
269 this.endHandler = this.connect(win.doc.documentElement, touch.release,
270 function(){
271 this.defer(function(){ // allow onclick to go first
272 skipReposition = true;
273 if(!isGesture && active){ // if click without move, then close dropdown
274 this.closeDropDown();
275 }
276 active = false;
277 });
278 }
279 );
280 this.repositionTimer = setInterval(lang.hitch(this, function(){
281 if(skipReposition){ // don't reposition if busy
282 skipReposition = false;
283 return;
284 }
285 var currentAroundNodePos = domGeometry.position(aroundNode, false),
286 currentPopupPos = domGeometry.position(wrapper, false),
287 currentDeltaX = currentPopupPos.x - currentAroundNodePos.x,
288 currentDeltaY = currentPopupPos.y - currentAroundNodePos.y;
289 // if the popup is no longer placed correctly, relocate it
290 if(Math.abs(currentDeltaX - deltaX) >= 1 || Math.abs(currentDeltaY - deltaY) >= 1){ // Firefox plays with partial pixels
291 domStyle.set(wrapper, { left: parseInt(domStyle.get(wrapper, "left")) + deltaX - currentDeltaX + 'px', top: parseInt(domStyle.get(wrapper, "top")) + deltaY - currentDeltaY + 'px' });
292 }
293 }), 50); // yield a short time to allow for consolidation for better CPU throughput
294 }
295
296 // We need to disable input control in order to prevent opening the soft keyboard in IE
297 if(has("windows-theme")){
298 this.domNode.setAttribute("disabled", true);
299 }
300
301 return retVal;
302 },
303
304 postCreate: function(){
305 this.inherited(arguments);
306 this.connect(this.domNode, "onclick", "_onClick");
307 domAttr.set(this.domNode, "role", "combobox");
308 domAttr.set(this.domNode, "aria-expanded", "false");
309 },
310
311 destroy: function(){
312 if(this.repositionTimer){
313 clearInterval(this.repositionTimer);
314 }
315 this.inherited(arguments);
316 },
317
318 _onClick: function(/*Event*/ e){
319 // tags:
320 // private
321
322 // throttle clicks to prevent double click from doing double actions
323 if(!this._throttleHandler){
324 if(this.opened){
325 this.closeDropDown();
326 }else{
327 this._startSearchAll();
328 }
329 }
330 }
331 });
332 });