| 1 |
define([ |
| 2 |
"dojo/_base/array", |
| 3 |
"dojo/_base/declare", |
| 4 |
"dojo/dom", |
| 5 |
"dojo/dom-attr", |
| 6 |
"dojo/dom-class", |
| 7 |
"dojo/_base/lang", |
| 8 |
"dojo/mouse", |
| 9 |
"dojo/on", |
| 10 |
"dojo/window", |
| 11 |
"./a11yclick", |
| 12 |
"./registry", |
| 13 |
"./_Widget", |
| 14 |
"./_CssStateMixin", |
| 15 |
"./_KeyNavContainer", |
| 16 |
"./_TemplatedMixin" |
| 17 |
], function(array, declare, dom, domAttr, domClass, lang, mouse, on, winUtils, a11yclick, |
| 18 |
registry, _Widget, _CssStateMixin, _KeyNavContainer, _TemplatedMixin){ |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
return declare("dijit._MenuBase", [_Widget, _TemplatedMixin, _KeyNavContainer, _CssStateMixin], { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
selected: null, |
| 34 |
_setSelectedAttr: function(item){ |
| 35 |
if(this.selected != item){ |
| 36 |
if(this.selected){ |
| 37 |
this.selected._setSelected(false); |
| 38 |
this._onChildDeselect(this.selected); |
| 39 |
} |
| 40 |
if(item){ |
| 41 |
item._setSelected(true); |
| 42 |
} |
| 43 |
this._set("selected", item); |
| 44 |
} |
| 45 |
}, |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
activated: false, |
| 54 |
_setActivatedAttr: function(val){ |
| 55 |
domClass.toggle(this.domNode, "dijitMenuActive", val); |
| 56 |
domClass.toggle(this.domNode, "dijitMenuPassive", !val); |
| 57 |
this._set("activated", val); |
| 58 |
}, |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
parentMenu: null, |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
popupDelay: 500, |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
passivePopupDelay: Infinity, |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
autoFocus: false, |
| 78 |
|
| 79 |
childSelector: function( node){ |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
var widget = registry.byNode(node); |
| 87 |
return node.parentNode == this.containerNode && widget && widget.focus; |
| 88 |
}, |
| 89 |
|
| 90 |
postCreate: function(){ |
| 91 |
var self = this, |
| 92 |
matches = typeof this.childSelector == "string" ? this.childSelector : lang.hitch(this, "childSelector"); |
| 93 |
this.own( |
| 94 |
on(this.containerNode, on.selector(matches, mouse.enter), function(){ |
| 95 |
self.onItemHover(registry.byNode(this)); |
| 96 |
}), |
| 97 |
on(this.containerNode, on.selector(matches, mouse.leave), function(){ |
| 98 |
self.onItemUnhover(registry.byNode(this)); |
| 99 |
}), |
| 100 |
on(this.containerNode, on.selector(matches, a11yclick), function(evt){ |
| 101 |
self.onItemClick(registry.byNode(this), evt); |
| 102 |
evt.stopPropagation(); |
| 103 |
}), |
| 104 |
on(this.containerNode, on.selector(matches, "focusin"), function(){ |
| 105 |
self._onItemFocus(registry.byNode(this)); |
| 106 |
}) |
| 107 |
); |
| 108 |
this.inherited(arguments); |
| 109 |
}, |
| 110 |
|
| 111 |
onKeyboardSearch: function( item, evt, searchString, numMatches){ |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
this.inherited(arguments); |
| 118 |
if(!!item && (numMatches == -1 || (!!item.popup && numMatches == 1))){ |
| 119 |
this.onItemClick(item, evt); |
| 120 |
} |
| 121 |
}, |
| 122 |
|
| 123 |
_keyboardSearchCompare: function( item, searchString){ |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
if(!!item.shortcutKey){ |
| 132 |
|
| 133 |
return searchString == item.shortcutKey.toLowerCase() ? -1 : 0; |
| 134 |
} |
| 135 |
return this.inherited(arguments) ? 1 : 0; |
| 136 |
}, |
| 137 |
|
| 138 |
onExecute: function(){ |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
}, |
| 147 |
|
| 148 |
onCancel: function( ){ |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
}, |
| 156 |
|
| 157 |
_moveToPopup: function( evt){ |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
if(this.focusedChild && this.focusedChild.popup && !this.focusedChild.disabled){ |
| 166 |
this.onItemClick(this.focusedChild, evt); |
| 167 |
}else{ |
| 168 |
var topMenu = this._getTopMenu(); |
| 169 |
if(topMenu && topMenu._isMenuBar){ |
| 170 |
topMenu.focusNext(); |
| 171 |
} |
| 172 |
} |
| 173 |
}, |
| 174 |
|
| 175 |
_onPopupHover: function( ){ |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
this.set("selected", this.currentPopupItem); |
| 187 |
|
| 188 |
|
| 189 |
this._stopPendingCloseTimer(); |
| 190 |
}, |
| 191 |
|
| 192 |
onItemHover: function( item){ |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
if(this.activated){ |
| 203 |
this.set("selected", item); |
| 204 |
if(item.popup && !item.disabled && !this.hover_timer){ |
| 205 |
this.hover_timer = this.defer(function(){ |
| 206 |
this._openItemPopup(item); |
| 207 |
}, this.popupDelay); |
| 208 |
} |
| 209 |
}else if(this.passivePopupDelay < Infinity){ |
| 210 |
if(this.passive_hover_timer){ |
| 211 |
this.passive_hover_timer.remove(); |
| 212 |
} |
| 213 |
this.passive_hover_timer = this.defer(function(){ |
| 214 |
this.onItemClick(item, {type: "click"}); |
| 215 |
}, this.passivePopupDelay); |
| 216 |
} |
| 217 |
|
| 218 |
this._hoveredChild = item; |
| 219 |
|
| 220 |
item._set("hovering", true); |
| 221 |
}, |
| 222 |
|
| 223 |
_onChildDeselect: function(item){ |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
this._stopPopupTimer(); |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
if(this.currentPopupItem == item){ |
| 232 |
this._stopPendingCloseTimer(); |
| 233 |
this._pendingClose_timer = this.defer(function(){ |
| 234 |
this._pendingClose_timer = null; |
| 235 |
this.currentPopupItem = null; |
| 236 |
item._closePopup(); |
| 237 |
}, this.popupDelay); |
| 238 |
} |
| 239 |
}, |
| 240 |
|
| 241 |
onItemUnhover: function( item){ |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
if(this._hoveredChild == item){ |
| 248 |
this._hoveredChild = null; |
| 249 |
} |
| 250 |
|
| 251 |
if(this.passive_hover_timer){ |
| 252 |
this.passive_hover_timer.remove(); |
| 253 |
this.passive_hover_timer = null; |
| 254 |
} |
| 255 |
|
| 256 |
item._set("hovering", false); |
| 257 |
}, |
| 258 |
|
| 259 |
_stopPopupTimer: function(){ |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
if(this.hover_timer){ |
| 267 |
this.hover_timer = this.hover_timer.remove(); |
| 268 |
} |
| 269 |
}, |
| 270 |
|
| 271 |
_stopPendingCloseTimer: function(){ |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
if(this._pendingClose_timer){ |
| 277 |
this._pendingClose_timer = this._pendingClose_timer.remove(); |
| 278 |
} |
| 279 |
}, |
| 280 |
|
| 281 |
_getTopMenu: function(){ |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
for(var top = this; top.parentMenu; top = top.parentMenu){} |
| 287 |
return top; |
| 288 |
}, |
| 289 |
|
| 290 |
onItemClick: function( item, evt){ |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
if(this.passive_hover_timer){ |
| 297 |
this.passive_hover_timer.remove(); |
| 298 |
} |
| 299 |
|
| 300 |
this.focusChild(item); |
| 301 |
|
| 302 |
if(item.disabled){ |
| 303 |
return false; |
| 304 |
} |
| 305 |
|
| 306 |
if(item.popup){ |
| 307 |
this.set("selected", item); |
| 308 |
this.set("activated", true); |
| 309 |
var byKeyboard = /^key/.test(evt._origType || evt.type) || |
| 310 |
(evt.clientX == 0 && evt.clientY == 0); |
| 311 |
this._openItemPopup(item, byKeyboard); |
| 312 |
}else{ |
| 313 |
|
| 314 |
|
| 315 |
this.onExecute(); |
| 316 |
|
| 317 |
|
| 318 |
item._onClick ? item._onClick(evt) : item.onClick(evt); |
| 319 |
} |
| 320 |
}, |
| 321 |
|
| 322 |
_openItemPopup: function( from_item, focus){ |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
if(from_item == this.currentPopupItem){ |
| 329 |
|
| 330 |
return; |
| 331 |
} |
| 332 |
if(this.currentPopupItem){ |
| 333 |
|
| 334 |
this._stopPendingCloseTimer(); |
| 335 |
this.currentPopupItem._closePopup(); |
| 336 |
} |
| 337 |
this._stopPopupTimer(); |
| 338 |
|
| 339 |
var popup = from_item.popup; |
| 340 |
popup.parentMenu = this; |
| 341 |
|
| 342 |
|
| 343 |
this.own(this._mouseoverHandle = on.once(popup.domNode, "mouseover", lang.hitch(this, "_onPopupHover"))); |
| 344 |
|
| 345 |
var self = this; |
| 346 |
from_item._openPopup({ |
| 347 |
parent: this, |
| 348 |
orient: this._orient || ["after", "before"], |
| 349 |
onCancel: function(){ |
| 350 |
if(focus){ |
| 351 |
|
| 352 |
self.focusChild(from_item); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
self._cleanUp(); |
| 357 |
}, |
| 358 |
onExecute: lang.hitch(this, "_cleanUp", true), |
| 359 |
onClose: function(){ |
| 360 |
|
| 361 |
if(self._mouseoverHandle){ |
| 362 |
self._mouseoverHandle.remove(); |
| 363 |
delete self._mouseoverHandle; |
| 364 |
} |
| 365 |
} |
| 366 |
}, focus); |
| 367 |
|
| 368 |
this.currentPopupItem = from_item; |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
}, |
| 373 |
|
| 374 |
onOpen: function( ){ |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
this.isShowingNow = true; |
| 383 |
this.set("activated", true); |
| 384 |
}, |
| 385 |
|
| 386 |
onClose: function(){ |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
this.set("activated", false); |
| 395 |
this.set("selected", null); |
| 396 |
this.isShowingNow = false; |
| 397 |
this.parentMenu = null; |
| 398 |
}, |
| 399 |
|
| 400 |
_closeChild: function(){ |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
this._stopPopupTimer(); |
| 406 |
|
| 407 |
if(this.currentPopupItem){ |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
if(this.focused){ |
| 414 |
domAttr.set(this.selected.focusNode, "tabIndex", this.tabIndex); |
| 415 |
this.selected.focusNode.focus(); |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
this.currentPopupItem._closePopup(); |
| 420 |
this.currentPopupItem = null; |
| 421 |
} |
| 422 |
}, |
| 423 |
|
| 424 |
_onItemFocus: function( item){ |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
if(this._hoveredChild && this._hoveredChild != item){ |
| 435 |
this.onItemUnhover(this._hoveredChild); |
| 436 |
} |
| 437 |
this.set("selected", item); |
| 438 |
}, |
| 439 |
|
| 440 |
_onBlur: function(){ |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
this._cleanUp(true); |
| 447 |
this.inherited(arguments); |
| 448 |
}, |
| 449 |
|
| 450 |
_cleanUp: function( clearSelectedItem){ |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
this._closeChild(); |
| 457 |
if(typeof this.isShowingNow == 'undefined'){ |
| 458 |
this.set("activated", false); |
| 459 |
} |
| 460 |
|
| 461 |
if(clearSelectedItem){ |
| 462 |
this.set("selected", null); |
| 463 |
} |
| 464 |
} |
| 465 |
}); |
| 466 |
}); |