| 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"); |
| 18 |
|
| 19 |
return declare("dojox.mobile.ComboBox", [TextBox, AutoCompleterMixin], { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
dropDownClass: "dojox.mobile._ComboBoxMenu", |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
selectOnClick: false, |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
autoComplete: false, |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
dropDown: null, |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
maxHeight: -1, |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
dropDownPosition: ["below","above"], |
| 64 |
|
| 65 |
_throttleOpenClose: function(){ |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 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 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 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){ |
| 93 |
this._onKey(e); |
| 94 |
this.inherited(arguments); |
| 95 |
} |
| 96 |
}, |
| 97 |
|
| 98 |
_setListAttr: function(v){ |
| 99 |
|
| 100 |
|
| 101 |
this._set('list', v); |
| 102 |
}, |
| 103 |
|
| 104 |
closeDropDown: function(){ |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 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 |
|
| 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 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 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); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
if(!this._preparedNode){ |
| 162 |
this._preparedNode = true; |
| 163 |
|
| 164 |
if(ddNode.style.width){ |
| 165 |
this._explicitDDWidth = true; |
| 166 |
} |
| 167 |
if(ddNode.style.height){ |
| 168 |
this._explicitDDHeight = true; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 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 |
|
| 187 |
var maxHeight = this.maxHeight; |
| 188 |
if(maxHeight == -1){ |
| 189 |
|
| 190 |
|
| 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 |
|
| 197 |
|
| 198 |
popup.moveOffScreen(dropDown); |
| 199 |
|
| 200 |
if(dropDown.startup && !dropDown._started){ |
| 201 |
dropDown.startup(); |
| 202 |
} |
| 203 |
|
| 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 |
|
| 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 |
|
| 243 |
|
| 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; |
| 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; |
| 267 |
} |
| 268 |
); |
| 269 |
this.endHandler = this.connect(win.doc.documentElement, touch.release, |
| 270 |
function(){ |
| 271 |
this.defer(function(){ |
| 272 |
skipReposition = true; |
| 273 |
if(!isGesture && active){ |
| 274 |
this.closeDropDown(); |
| 275 |
} |
| 276 |
active = false; |
| 277 |
}); |
| 278 |
} |
| 279 |
); |
| 280 |
this.repositionTimer = setInterval(lang.hitch(this, function(){ |
| 281 |
if(skipReposition){ |
| 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 |
|
| 290 |
if(Math.abs(currentDeltaX - deltaX) >= 1 || Math.abs(currentDeltaY - deltaY) >= 1){ |
| 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); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 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( e){ |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
if(!this._throttleHandler){ |
| 324 |
if(this.opened){ |
| 325 |
this.closeDropDown(); |
| 326 |
}else{ |
| 327 |
this._startSearchAll(); |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
}); |
| 332 |
}); |