| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
(function( $, undefined ) { |
| 17 |
|
| 18 |
var mouseHandled = false; |
| 19 |
|
| 20 |
$.widget( "ui.menu", { |
| 21 |
version: "1.9.2", |
| 22 |
defaultElement: "<ul>", |
| 23 |
delay: 300, |
| 24 |
options: { |
| 25 |
icons: { |
| 26 |
submenu: "ui-icon-carat-1-e" |
| 27 |
}, |
| 28 |
menus: "ul", |
| 29 |
position: { |
| 30 |
my: "left top", |
| 31 |
at: "right top" |
| 32 |
}, |
| 33 |
role: "menu", |
| 34 |
|
| 35 |
|
| 36 |
blur: null, |
| 37 |
focus: null, |
| 38 |
select: null |
| 39 |
}, |
| 40 |
|
| 41 |
_create: function() { |
| 42 |
this.activeMenu = this.element; |
| 43 |
this.element |
| 44 |
.uniqueId() |
| 45 |
.addClass( "ui-menu ui-widget ui-widget-content ui-corner-all" ) |
| 46 |
.toggleClass( "ui-menu-icons", !!this.element.find( ".ui-icon" ).length ) |
| 47 |
.attr({ |
| 48 |
role: this.options.role, |
| 49 |
tabIndex: 0 |
| 50 |
}) |
| 51 |
.on( "click" + this.eventNamespace, $.proxy(function( event ) { |
| 52 |
if ( this.options.disabled ) { |
| 53 |
event.preventDefault(); |
| 54 |
} |
| 55 |
}, this )); |
| 56 |
|
| 57 |
if ( this.options.disabled ) { |
| 58 |
this.element |
| 59 |
.addClass( "ui-state-disabled" ) |
| 60 |
.attr( "aria-disabled", "true" ); |
| 61 |
} |
| 62 |
|
| 63 |
this._on({ |
| 64 |
|
| 65 |
|
| 66 |
"mousedown .ui-menu-item > a": function( event ) { |
| 67 |
event.preventDefault(); |
| 68 |
}, |
| 69 |
"click .ui-state-disabled > a": function( event ) { |
| 70 |
event.preventDefault(); |
| 71 |
}, |
| 72 |
"click .ui-menu-item:has(a)": function( event ) { |
| 73 |
var target = $( event.target ).closest( ".ui-menu-item" ); |
| 74 |
if ( !mouseHandled && target.not( ".ui-state-disabled" ).length ) { |
| 75 |
mouseHandled = true; |
| 76 |
|
| 77 |
this.select( event ); |
| 78 |
|
| 79 |
if ( target.has( ".ui-menu" ).length ) { |
| 80 |
this.expand( event ); |
| 81 |
} else if ( !this.element.is( ":focus" ) ) { |
| 82 |
|
| 83 |
this.element.trigger( "focus", [ true ] ); |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
if ( this.active && this.active.parents( ".ui-menu" ).length === 1 ) { |
| 88 |
clearTimeout( this.timer ); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
}, |
| 93 |
"mouseenter .ui-menu-item": function( event ) { |
| 94 |
var target = $( event.currentTarget ); |
| 95 |
|
| 96 |
|
| 97 |
target.siblings().children( ".ui-state-active" ).removeClass( "ui-state-active" ); |
| 98 |
this.focus( event, target ); |
| 99 |
}, |
| 100 |
mouseleave: "collapseAll", |
| 101 |
"mouseleave .ui-menu": "collapseAll", |
| 102 |
focus: function( event, keepActiveItem ) { |
| 103 |
|
| 104 |
|
| 105 |
var item = this.active || this.element.children( ".ui-menu-item" ).eq( 0 ); |
| 106 |
|
| 107 |
if ( !keepActiveItem ) { |
| 108 |
this.focus( event, item ); |
| 109 |
} |
| 110 |
}, |
| 111 |
blur: function( event ) { |
| 112 |
this._delay(function() { |
| 113 |
if ( !$.contains( this.element[0], this.document[0].activeElement ) ) { |
| 114 |
this.collapseAll( event ); |
| 115 |
} |
| 116 |
}); |
| 117 |
}, |
| 118 |
keydown: "_keydown" |
| 119 |
}); |
| 120 |
|
| 121 |
this.refresh(); |
| 122 |
|
| 123 |
|
| 124 |
this._on( this.document, { |
| 125 |
click: function( event ) { |
| 126 |
if ( !$( event.target ).closest( ".ui-menu" ).length ) { |
| 127 |
this.collapseAll( event ); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
mouseHandled = false; |
| 132 |
} |
| 133 |
}); |
| 134 |
}, |
| 135 |
|
| 136 |
_destroy: function() { |
| 137 |
|
| 138 |
this.element |
| 139 |
.removeAttr( "aria-activedescendant" ) |
| 140 |
.find( ".ui-menu" ).addBack() |
| 141 |
.removeClass( "ui-menu ui-widget ui-widget-content ui-corner-all ui-menu-icons" ) |
| 142 |
.removeAttr( "role" ) |
| 143 |
.removeAttr( "tabIndex" ) |
| 144 |
.removeAttr( "aria-labelledby" ) |
| 145 |
.removeAttr( "aria-expanded" ) |
| 146 |
.removeAttr( "aria-hidden" ) |
| 147 |
.removeAttr( "aria-disabled" ) |
| 148 |
.removeUniqueId() |
| 149 |
.show(); |
| 150 |
|
| 151 |
|
| 152 |
this.element.find( ".ui-menu-item" ) |
| 153 |
.removeClass( "ui-menu-item" ) |
| 154 |
.removeAttr( "role" ) |
| 155 |
.removeAttr( "aria-disabled" ) |
| 156 |
.children( "a" ) |
| 157 |
.removeUniqueId() |
| 158 |
.removeClass( "ui-corner-all ui-state-hover" ) |
| 159 |
.removeAttr( "tabIndex" ) |
| 160 |
.removeAttr( "role" ) |
| 161 |
.removeAttr( "aria-haspopup" ) |
| 162 |
.children().each( function() { |
| 163 |
var elem = $( this ); |
| 164 |
if ( elem.data( "ui-menu-submenu-carat" ) ) { |
| 165 |
elem.remove(); |
| 166 |
} |
| 167 |
}); |
| 168 |
|
| 169 |
|
| 170 |
this.element.find( ".ui-menu-divider" ).removeClass( "ui-menu-divider ui-widget-content" ); |
| 171 |
}, |
| 172 |
|
| 173 |
_keydown: function( event ) { |
| 174 |
var match, prev, character, skip, regex, |
| 175 |
preventDefault = true; |
| 176 |
|
| 177 |
function escape( value ) { |
| 178 |
return value.replace( /[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&" ); |
| 179 |
} |
| 180 |
|
| 181 |
switch ( event.keyCode ) { |
| 182 |
case $.ui.keyCode.PAGE_UP: |
| 183 |
this.previousPage( event ); |
| 184 |
break; |
| 185 |
case $.ui.keyCode.PAGE_DOWN: |
| 186 |
this.nextPage( event ); |
| 187 |
break; |
| 188 |
case $.ui.keyCode.HOME: |
| 189 |
this._move( "first", "first", event ); |
| 190 |
break; |
| 191 |
case $.ui.keyCode.END: |
| 192 |
this._move( "last", "last", event ); |
| 193 |
break; |
| 194 |
case $.ui.keyCode.UP: |
| 195 |
this.previous( event ); |
| 196 |
break; |
| 197 |
case $.ui.keyCode.DOWN: |
| 198 |
this.next( event ); |
| 199 |
break; |
| 200 |
case $.ui.keyCode.LEFT: |
| 201 |
this.collapse( event ); |
| 202 |
break; |
| 203 |
case $.ui.keyCode.RIGHT: |
| 204 |
if ( this.active && !this.active.is( ".ui-state-disabled" ) ) { |
| 205 |
this.expand( event ); |
| 206 |
} |
| 207 |
break; |
| 208 |
case $.ui.keyCode.ENTER: |
| 209 |
case $.ui.keyCode.SPACE: |
| 210 |
this._activate( event ); |
| 211 |
break; |
| 212 |
case $.ui.keyCode.ESCAPE: |
| 213 |
this.collapse( event ); |
| 214 |
break; |
| 215 |
default: |
| 216 |
preventDefault = false; |
| 217 |
prev = this.previousFilter || ""; |
| 218 |
character = String.fromCharCode( event.keyCode ); |
| 219 |
skip = false; |
| 220 |
|
| 221 |
clearTimeout( this.filterTimer ); |
| 222 |
|
| 223 |
if ( character === prev ) { |
| 224 |
skip = true; |
| 225 |
} else { |
| 226 |
character = prev + character; |
| 227 |
} |
| 228 |
|
| 229 |
regex = new RegExp( "^" + escape( character ), "i" ); |
| 230 |
match = this.activeMenu.children( ".ui-menu-item" ).filter(function() { |
| 231 |
return regex.test( $( this ).children( "a" ).text() ); |
| 232 |
}); |
| 233 |
match = skip && match.index( this.active.next() ) !== -1 ? |
| 234 |
this.active.nextAll( ".ui-menu-item" ) : |
| 235 |
match; |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
if ( !match.length ) { |
| 240 |
character = String.fromCharCode( event.keyCode ); |
| 241 |
regex = new RegExp( "^" + escape( character ), "i" ); |
| 242 |
match = this.activeMenu.children( ".ui-menu-item" ).filter(function() { |
| 243 |
return regex.test( $( this ).children( "a" ).text() ); |
| 244 |
}); |
| 245 |
} |
| 246 |
|
| 247 |
if ( match.length ) { |
| 248 |
this.focus( event, match ); |
| 249 |
if ( match.length > 1 ) { |
| 250 |
this.previousFilter = character; |
| 251 |
this.filterTimer = this._delay(function() { |
| 252 |
delete this.previousFilter; |
| 253 |
}, 1000 ); |
| 254 |
} else { |
| 255 |
delete this.previousFilter; |
| 256 |
} |
| 257 |
} else { |
| 258 |
delete this.previousFilter; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
if ( preventDefault ) { |
| 263 |
event.preventDefault(); |
| 264 |
} |
| 265 |
}, |
| 266 |
|
| 267 |
_activate: function( event ) { |
| 268 |
if ( !this.active.is( ".ui-state-disabled" ) ) { |
| 269 |
if ( this.active.children( "a[aria-haspopup='true']" ).length ) { |
| 270 |
this.expand( event ); |
| 271 |
} else { |
| 272 |
this.select( event ); |
| 273 |
} |
| 274 |
} |
| 275 |
}, |
| 276 |
|
| 277 |
refresh: function() { |
| 278 |
var menus, |
| 279 |
icon = this.options.icons.submenu, |
| 280 |
submenus = this.element.find( this.options.menus ); |
| 281 |
|
| 282 |
|
| 283 |
submenus.filter( ":not(.ui-menu)" ) |
| 284 |
.addClass( "ui-menu ui-widget ui-widget-content ui-corner-all" ) |
| 285 |
.hide() |
| 286 |
.attr({ |
| 287 |
role: this.options.role, |
| 288 |
"aria-hidden": "true", |
| 289 |
"aria-expanded": "false" |
| 290 |
}) |
| 291 |
.each(function() { |
| 292 |
var menu = $( this ), |
| 293 |
item = menu.prev( "a" ), |
| 294 |
submenuCarat = $( "<span>" ) |
| 295 |
.addClass( "ui-menu-icon ui-icon " + icon ) |
| 296 |
.data( "ui-menu-submenu-carat", true ); |
| 297 |
|
| 298 |
item |
| 299 |
.attr( "aria-haspopup", "true" ) |
| 300 |
.prepend( submenuCarat ); |
| 301 |
menu.attr( "aria-labelledby", item.attr( "id" ) ); |
| 302 |
}); |
| 303 |
|
| 304 |
menus = submenus.add( this.element ); |
| 305 |
|
| 306 |
|
| 307 |
menus.children( ":not(.ui-menu-item):has(a)" ) |
| 308 |
.addClass( "ui-menu-item" ) |
| 309 |
.attr( "role", "presentation" ) |
| 310 |
.children( "a" ) |
| 311 |
.uniqueId() |
| 312 |
.addClass( "ui-corner-all" ) |
| 313 |
.attr({ |
| 314 |
tabIndex: -1, |
| 315 |
role: this._itemRole() |
| 316 |
}); |
| 317 |
|
| 318 |
|
| 319 |
menus.children( ":not(.ui-menu-item)" ).each(function() { |
| 320 |
var item = $( this ); |
| 321 |
|
| 322 |
if ( !/[^\-—–\s]/.test( item.text() ) ) { |
| 323 |
item.addClass( "ui-widget-content ui-menu-divider" ); |
| 324 |
} |
| 325 |
}); |
| 326 |
|
| 327 |
|
| 328 |
menus.children( ".ui-state-disabled" ).attr( "aria-disabled", "true" ); |
| 329 |
|
| 330 |
|
| 331 |
if ( this.active && !$.contains( this.element[ 0 ], this.active[ 0 ] ) ) { |
| 332 |
this.blur(); |
| 333 |
} |
| 334 |
}, |
| 335 |
|
| 336 |
_itemRole: function() { |
| 337 |
return { |
| 338 |
menu: "menuitem", |
| 339 |
listbox: "option" |
| 340 |
}[ this.options.role ]; |
| 341 |
}, |
| 342 |
|
| 343 |
focus: function( event, item ) { |
| 344 |
var nested, focused; |
| 345 |
this.blur( event, event && event.type === "focus" ); |
| 346 |
|
| 347 |
this._scrollIntoView( item ); |
| 348 |
|
| 349 |
this.active = item.first(); |
| 350 |
focused = this.active.children( "a" ).addClass( "ui-state-focus" ); |
| 351 |
|
| 352 |
|
| 353 |
if ( this.options.role ) { |
| 354 |
this.element.attr( "aria-activedescendant", focused.attr( "id" ) ); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
this.active |
| 359 |
.parent() |
| 360 |
.closest( ".ui-menu-item" ) |
| 361 |
.children( "a:first" ) |
| 362 |
.addClass( "ui-state-active" ); |
| 363 |
|
| 364 |
if ( event && event.type === "keydown" ) { |
| 365 |
this._close(); |
| 366 |
} else { |
| 367 |
this.timer = this._delay(function() { |
| 368 |
this._close(); |
| 369 |
}, this.delay ); |
| 370 |
} |
| 371 |
|
| 372 |
nested = item.children( ".ui-menu" ); |
| 373 |
if ( nested.length && ( /^mouse/.test( event.type ) ) ) { |
| 374 |
this._startOpening(nested); |
| 375 |
} |
| 376 |
this.activeMenu = item.parent(); |
| 377 |
|
| 378 |
this._trigger( "focus", event, { item: item } ); |
| 379 |
}, |
| 380 |
|
| 381 |
_scrollIntoView: function( item ) { |
| 382 |
var borderTop, paddingTop, offset, scroll, elementHeight, itemHeight; |
| 383 |
if ( this._hasScroll() ) { |
| 384 |
borderTop = parseFloat( $.css( this.activeMenu[0], "borderTopWidth" ) ) || 0; |
| 385 |
paddingTop = parseFloat( $.css( this.activeMenu[0], "paddingTop" ) ) || 0; |
| 386 |
offset = item.offset().top - this.activeMenu.offset().top - borderTop - paddingTop; |
| 387 |
scroll = this.activeMenu.scrollTop(); |
| 388 |
elementHeight = this.activeMenu.height(); |
| 389 |
itemHeight = item.height(); |
| 390 |
|
| 391 |
if ( offset < 0 ) { |
| 392 |
this.activeMenu.scrollTop( scroll + offset ); |
| 393 |
} else if ( offset + itemHeight > elementHeight ) { |
| 394 |
this.activeMenu.scrollTop( scroll + offset - elementHeight + itemHeight ); |
| 395 |
} |
| 396 |
} |
| 397 |
}, |
| 398 |
|
| 399 |
blur: function( event, fromFocus ) { |
| 400 |
if ( !fromFocus ) { |
| 401 |
clearTimeout( this.timer ); |
| 402 |
} |
| 403 |
|
| 404 |
if ( !this.active ) { |
| 405 |
return; |
| 406 |
} |
| 407 |
|
| 408 |
this.active.children( "a" ).removeClass( "ui-state-focus" ); |
| 409 |
this.active = null; |
| 410 |
|
| 411 |
this._trigger( "blur", event, { item: this.active } ); |
| 412 |
}, |
| 413 |
|
| 414 |
_startOpening: function( submenu ) { |
| 415 |
clearTimeout( this.timer ); |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
if ( submenu.attr( "aria-hidden" ) !== "true" ) { |
| 420 |
return; |
| 421 |
} |
| 422 |
|
| 423 |
this.timer = this._delay(function() { |
| 424 |
this._close(); |
| 425 |
this._open( submenu ); |
| 426 |
}, this.delay ); |
| 427 |
}, |
| 428 |
|
| 429 |
_open: function( submenu ) { |
| 430 |
var position = $.extend({ |
| 431 |
of: this.active |
| 432 |
}, this.options.position ); |
| 433 |
|
| 434 |
clearTimeout( this.timer ); |
| 435 |
this.element.find( ".ui-menu" ).not( submenu.parents( ".ui-menu" ) ) |
| 436 |
.hide() |
| 437 |
.attr( "aria-hidden", "true" ); |
| 438 |
|
| 439 |
submenu |
| 440 |
.show() |
| 441 |
.removeAttr( "aria-hidden" ) |
| 442 |
.attr( "aria-expanded", "true" ) |
| 443 |
.position( position ); |
| 444 |
}, |
| 445 |
|
| 446 |
collapseAll: function( event, all ) { |
| 447 |
clearTimeout( this.timer ); |
| 448 |
this.timer = this._delay(function() { |
| 449 |
|
| 450 |
var currentMenu = all ? this.element : |
| 451 |
$( event && event.target ).closest( this.element.find( ".ui-menu" ) ); |
| 452 |
|
| 453 |
|
| 454 |
if ( !currentMenu.length ) { |
| 455 |
currentMenu = this.element; |
| 456 |
} |
| 457 |
|
| 458 |
this._close( currentMenu ); |
| 459 |
|
| 460 |
this.blur( event ); |
| 461 |
this.activeMenu = currentMenu; |
| 462 |
}, this.delay ); |
| 463 |
}, |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
_close: function( startMenu ) { |
| 468 |
if ( !startMenu ) { |
| 469 |
startMenu = this.active ? this.active.parent() : this.element; |
| 470 |
} |
| 471 |
|
| 472 |
startMenu |
| 473 |
.find( ".ui-menu" ) |
| 474 |
.hide() |
| 475 |
.attr( "aria-hidden", "true" ) |
| 476 |
.attr( "aria-expanded", "false" ) |
| 477 |
.end() |
| 478 |
.find( "a.ui-state-active" ) |
| 479 |
.removeClass( "ui-state-active" ); |
| 480 |
}, |
| 481 |
|
| 482 |
collapse: function( event ) { |
| 483 |
var newItem = this.active && |
| 484 |
this.active.parent().closest( ".ui-menu-item", this.element ); |
| 485 |
if ( newItem && newItem.length ) { |
| 486 |
this._close(); |
| 487 |
this.focus( event, newItem ); |
| 488 |
} |
| 489 |
}, |
| 490 |
|
| 491 |
expand: function( event ) { |
| 492 |
var newItem = this.active && |
| 493 |
this.active |
| 494 |
.children( ".ui-menu " ) |
| 495 |
.children( ".ui-menu-item" ) |
| 496 |
.first(); |
| 497 |
|
| 498 |
if ( newItem && newItem.length ) { |
| 499 |
this._open( newItem.parent() ); |
| 500 |
|
| 501 |
|
| 502 |
this._delay(function() { |
| 503 |
this.focus( event, newItem ); |
| 504 |
}); |
| 505 |
} |
| 506 |
}, |
| 507 |
|
| 508 |
next: function( event ) { |
| 509 |
this._move( "next", "first", event ); |
| 510 |
}, |
| 511 |
|
| 512 |
previous: function( event ) { |
| 513 |
this._move( "prev", "last", event ); |
| 514 |
}, |
| 515 |
|
| 516 |
isFirstItem: function() { |
| 517 |
return this.active && !this.active.prevAll( ".ui-menu-item" ).length; |
| 518 |
}, |
| 519 |
|
| 520 |
isLastItem: function() { |
| 521 |
return this.active && !this.active.nextAll( ".ui-menu-item" ).length; |
| 522 |
}, |
| 523 |
|
| 524 |
_move: function( direction, filter, event ) { |
| 525 |
var next; |
| 526 |
if ( this.active ) { |
| 527 |
if ( direction === "first" || direction === "last" ) { |
| 528 |
next = this.active |
| 529 |
[ direction === "first" ? "prevAll" : "nextAll" ]( ".ui-menu-item" ) |
| 530 |
.eq( -1 ); |
| 531 |
} else { |
| 532 |
next = this.active |
| 533 |
[ direction + "All" ]( ".ui-menu-item" ) |
| 534 |
.eq( 0 ); |
| 535 |
} |
| 536 |
} |
| 537 |
if ( !next || !next.length || !this.active ) { |
| 538 |
next = this.activeMenu.children( ".ui-menu-item" )[ filter ](); |
| 539 |
} |
| 540 |
|
| 541 |
this.focus( event, next ); |
| 542 |
}, |
| 543 |
|
| 544 |
nextPage: function( event ) { |
| 545 |
var item, base, height; |
| 546 |
|
| 547 |
if ( !this.active ) { |
| 548 |
this.next( event ); |
| 549 |
return; |
| 550 |
} |
| 551 |
if ( this.isLastItem() ) { |
| 552 |
return; |
| 553 |
} |
| 554 |
if ( this._hasScroll() ) { |
| 555 |
base = this.active.offset().top; |
| 556 |
height = this.element.height(); |
| 557 |
this.active.nextAll( ".ui-menu-item" ).each(function() { |
| 558 |
item = $( this ); |
| 559 |
return item.offset().top - base - height < 0; |
| 560 |
}); |
| 561 |
|
| 562 |
this.focus( event, item ); |
| 563 |
} else { |
| 564 |
this.focus( event, this.activeMenu.children( ".ui-menu-item" ) |
| 565 |
[ !this.active ? "first" : "last" ]() ); |
| 566 |
} |
| 567 |
}, |
| 568 |
|
| 569 |
previousPage: function( event ) { |
| 570 |
var item, base, height; |
| 571 |
if ( !this.active ) { |
| 572 |
this.next( event ); |
| 573 |
return; |
| 574 |
} |
| 575 |
if ( this.isFirstItem() ) { |
| 576 |
return; |
| 577 |
} |
| 578 |
if ( this._hasScroll() ) { |
| 579 |
base = this.active.offset().top; |
| 580 |
height = this.element.height(); |
| 581 |
this.active.prevAll( ".ui-menu-item" ).each(function() { |
| 582 |
item = $( this ); |
| 583 |
return item.offset().top - base + height > 0; |
| 584 |
}); |
| 585 |
|
| 586 |
this.focus( event, item ); |
| 587 |
} else { |
| 588 |
this.focus( event, this.activeMenu.children( ".ui-menu-item" ).first() ); |
| 589 |
} |
| 590 |
}, |
| 591 |
|
| 592 |
_hasScroll: function() { |
| 593 |
return this.element.outerHeight() < this.element.prop( "scrollHeight" ); |
| 594 |
}, |
| 595 |
|
| 596 |
select: function( event ) { |
| 597 |
|
| 598 |
|
| 599 |
this.active = this.active || $( event.target ).closest( ".ui-menu-item" ); |
| 600 |
var ui = { item: this.active }; |
| 601 |
if ( !this.active.has( ".ui-menu" ).length ) { |
| 602 |
this.collapseAll( event, true ); |
| 603 |
} |
| 604 |
this._trigger( "select", event, ui ); |
| 605 |
} |
| 606 |
}); |
| 607 |
|
| 608 |
}( jQuery )); |