| 265 | countMenuItems : function( depth ) { |
| 266 | return $( '.menu-item-depth-' + depth ).length; |
| 267 | }, |
| 268 | |
| 269 | moveMenuItem : function( $this, dir ) { |
| 270 | |
| 271 | var menuItems = $('#menu-to-edit li'); |
| 272 | menuItemsCount = menuItems.length, |
| 273 | thisItem = $this.parents( 'li.menu-item' ), |
| 274 | thisItemChildren = thisItem.childMenuItems(), |
| 275 | thisItemData = thisItem.getItemData(), |
| 276 | thisItemDepth = parseInt( thisItem.menuItemDepth() ), |
| 277 | thisItemPosition = parseInt( thisItem.index() ), |
| 278 | nextItem = thisItem.next(), |
| 279 | nextItemChildren = nextItem.childMenuItems(), |
| 280 | nextItemDepth = parseInt( nextItem.menuItemDepth() ) + 1, |
| 281 | prevItem = thisItem.prev(), |
| 282 | prevItemDepth = parseInt( prevItem.menuItemDepth() ), |
| 283 | prevItemId = prevItem.getItemData()['menu-item-db-id']; |
| 284 | |
| 285 | switch ( dir ) { |
| 286 | case 'up': |
| 287 | var newItemPosition = thisItemPosition - 1; |
| 288 | |
| 289 | // Already at top |
| 290 | if ( 0 === thisItemPosition ) |
| 291 | break; |
| 292 | |
| 293 | // If a sub item is moved to top, shift it to 0 depth |
| 294 | if ( 0 === newItemPosition && 0 !== thisItemDepth ) |
| 295 | thisItem.moveHorizontally( 0, thisItemDepth ); |
| 296 | |
| 297 | // If prev item is sub item, shift to match depth |
| 298 | if ( 0 !== prevItemDepth ) |
| 299 | thisItem.moveHorizontally( prevItemDepth, thisItemDepth ); |
| 300 | |
| 301 | // Does this item have sub items? |
| 302 | if ( thisItemChildren ) { |
| 303 | var items = thisItem.add( thisItemChildren ); |
| 304 | // Move the entire block |
| 305 | items.detach().insertBefore( menuItems.eq( newItemPosition ) ).updateParentMenuItemDBId(); |
| 306 | } else { |
| 307 | thisItem.detach().insertBefore( menuItems.eq( newItemPosition ) ).updateParentMenuItemDBId(); |
| 308 | } |
| 309 | break; |
| 310 | case 'down': |
| 311 | // Does this item have sub items? |
| 312 | if ( thisItemChildren ) { |
| 313 | var items = thisItem.add( thisItemChildren ), |
| 314 | nextItem = menuItems.eq( items.length + thisItemPosition ), |
| 315 | nextItemChildren = 0 !== nextItem.childMenuItems().length; |
| 316 | |
| 317 | if ( nextItemChildren ) { |
| 318 | var newDepth = parseInt( nextItem.menuItemDepth() ) + 1; |
| 319 | thisItem.moveHorizontally( newDepth, thisItemDepth ); |
| 320 | } |
| 321 | |
| 322 | // Have we reached the bottom? |
| 323 | if ( menuItemsCount === thisItemPosition + items.length ) |
| 324 | break; |
| 325 | |
| 326 | items.detach().insertAfter( menuItems.eq( thisItemPosition + items.length ) ).updateParentMenuItemDBId(); |
| 327 | } else { |
| 328 | // If next item has sub items, shift depth |
| 329 | if ( 0 !== nextItemChildren.length ) |
| 330 | thisItem.moveHorizontally( nextItemDepth, thisItemDepth ); |
| 331 | |
| 332 | // Have we reached the bottom |
| 333 | if ( menuItemsCount === thisItemPosition + 1 ) |
| 334 | break; |
| 335 | thisItem.detach().insertAfter( menuItems.eq( thisItemPosition + 1 ) ).updateParentMenuItemDBId(); |
| 336 | } |
| 337 | break; |
| 338 | case 'top': |
| 339 | // Already at top |
| 340 | if ( 0 === thisItemPosition ) |
| 341 | break; |
| 342 | // Does this item have sub items? |
| 343 | if ( thisItemChildren ) { |
| 344 | var items = thisItem.add( thisItemChildren ); |
| 345 | // Move the entire block |
| 346 | items.detach().insertBefore( menuItems.eq( 0 ) ).updateParentMenuItemDBId(); |
| 347 | } else { |
| 348 | thisItem.detach().insertBefore( menuItems.eq( 0 ) ).updateParentMenuItemDBId(); |
| 349 | } |
| 350 | break; |
| 351 | case 'left': |
| 352 | // As far left as possible |
| 353 | if ( 0 === thisItemDepth ) |
| 354 | break; |
| 355 | thisItem.shiftHorizontally( -1 ); |
| 356 | break; |
| 357 | case 'right': |
| 358 | // Can't be sub item at top |
| 359 | if ( 0 === thisItemPosition ) |
| 360 | break; |
| 361 | // Already sub item of prevItem |
| 362 | if ( thisItemData['menu-item-parent-id'] === prevItemId ) |
| 363 | break; |
| 364 | thisItem.shiftHorizontally( 1 ); |
| 365 | break; |
| 366 | } |
| 367 | $this.focus(); |
| 368 | api.registerChange(); |
| 369 | api.refreshKeyboardAccessibility(); |
| 370 | api.refreshAdvancedAccessibility(); |
| 371 | }, |
| 372 | |
| 374 | api.refreshKeyboardAccessibility(); |
| 375 | api.refreshAdvancedAccessibility(); |
| 376 | |
| 377 | // Events |
| 378 | $( '.menus-move-up' ).on( 'click', function ( e ) { |
| 379 | api.moveMenuItem( $( this ).parents( 'li.menu-item' ).find( 'a.item-edit' ), 'up' ); |
| 380 | e.preventDefault(); |
| 381 | }); |
| 382 | $( '.menus-move-down' ).on( 'click', function ( e ) { |
| 383 | api.moveMenuItem( $( this ).parents( 'li.menu-item' ).find( 'a.item-edit' ), 'down' ); |
| 384 | e.preventDefault(); |
| 385 | }); |
| 386 | $( '.menus-move-top' ).on( 'click', function ( e ) { |
| 387 | api.moveMenuItem( $( this ).parents( 'li.menu-item' ).find( 'a.item-edit' ), 'top' ); |
| 388 | e.preventDefault(); |
| 389 | }); |
| 390 | $( '.menus-move-left' ).on( 'click', function ( e ) { |
| 391 | api.moveMenuItem( $( this ).parents( 'li.menu-item' ).find( 'a.item-edit' ), 'left' ); |
| 392 | e.preventDefault(); |
| 393 | }); |
| 394 | $( '.menus-move-right' ).on( 'click', function ( e ) { |
| 395 | api.moveMenuItem( $( this ).parents( 'li.menu-item' ).find( 'a.item-edit' ), 'right' ); |
| 396 | e.preventDefault(); |
| 397 | }); |
| 398 | }, |
| 399 | |
| 400 | refreshAdvancedAccessibility : function() { |
| 401 | |
| 402 | // Hide all links by default |
| 403 | $( '.menu-item-settings .field-move a' ).hide(); |
| 404 | |
| 405 | $( '.item-edit' ).each( function() { |
| 406 | var $this = $(this), |
| 407 | movement = [], |
| 408 | availableMovement = '', |
| 409 | menuItem = $this.parents( 'li.menu-item' ).first(), |
| 410 | depth = menuItem.menuItemDepth(), |
| 411 | isPrimaryMenuItem = ( 0 === depth ), |
| 412 | itemName = $this.parents( '.menu-item-handle' ).find( '.menu-item-title' ).text(), |
| 413 | position = parseInt( menuItem.index() ), |
| 414 | prevItemDepth = ( isPrimaryMenuItem ) ? depth : parseInt( depth - 1 ), |
| 415 | prevItemNameLeft = menuItem.prevAll('.menu-item-depth-' + prevItemDepth).first().find( '.menu-item-title' ).text(), |
| 416 | prevItemNameRight = menuItem.prevAll('.menu-item-depth-' + depth).first().find( '.menu-item-title' ).text(), |
| 417 | totalMenuItems = $('#menu-to-edit li').length, |
| 418 | hasSameDepthSibling = menuItem.nextAll( '.menu-item-depth-' + depth ).length; |
| 419 | |
| 420 | // Where can they move this menu item? |
| 421 | if ( 0 !== position ) { |
| 422 | var thisLink = menuItem.find( '.menus-move-up' ), |
| 423 | thisLinkText = thisLink.text(); |
| 424 | thisLink.prop('title', menus.move + ' ' + thisLinkText).show(); |
| 425 | } |
| 426 | |
| 427 | if ( 0 !== position && isPrimaryMenuItem ) { |
| 428 | var thisLink = menuItem.find( '.menus-move-top' ), |
| 429 | thisLinkText = thisLink.text(); |
| 430 | thisLink.prop('title', menus.move + ' ' + thisLinkText).show(); |
| 431 | } |
| 432 | |
| 433 | if ( position + 1 !== totalMenuItems && 0 !== position ) { |
| 434 | var thisLink = menuItem.find( '.menus-move-down' ), |
| 435 | thisLinkText = thisLink.text(); |
| 436 | thisLink.prop('title', menus.move + ' ' + thisLinkText).show(); |
| 437 | } |
| 438 | |
| 439 | if ( 0 === position && 0 !== hasSameDepthSibling ) { |
| 440 | var thisLink = menuItem.find( '.menus-move-down' ), |
| 441 | thisLinkText = thisLink.text(); |
| 442 | thisLink.prop('title', menus.move + ' ' + thisLinkText).show(); |
| 443 | } |
| 444 | |
| 445 | if ( ! isPrimaryMenuItem ) { |
| 446 | var thisLink = menuItem.find( '.menus-move-left' ), |
| 447 | thisLinkText = menus.outFrom + ' ' + prevItemNameLeft; |
| 448 | thisLink.prop('title', menus.move + ' ' + thisLinkText).html(thisLinkText).show(); |
| 449 | } |
| 450 | |
| 451 | if ( 0 !== position ) { |
| 452 | if ( menuItem.find( '.menu-item-data-parent-id' ).val() !== menuItem.prev().find( '.menu-item-data-db-id' ).val() ) { |
| 453 | var thisLink = menuItem.find( '.menus-move-right' ), |
| 454 | thisLinkText = menus.under + ' ' + prevItemNameRight; |
| 455 | thisLink.prop('title', menus.move + ' ' + thisLinkText).html(thisLinkText).show(); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | if ( isPrimaryMenuItem ) { |
| 460 | var primaryItems = $( '.menu-item-depth-0' ), |
| 461 | itemPosition = primaryItems.index( menuItem ) + 1, |
| 462 | totalMenuItems = primaryItems.length, |
| 463 | |
| 464 | // String together help text for primary menu items |
| 465 | title = itemName + '. ' + menus.menuFocus.replace('%d', itemPosition).replace('%d', totalMenuItems) + '.'; |
| 466 | } else { |
| 467 | var parentItem = menuItem.prevAll( '.menu-item-depth-' + parseInt( depth - 1 ) ).first(), |
| 468 | parentItemId = parentItem.find( '.menu-item-data-db-id' ).val(), |
| 469 | parentItemName = parentItem.find( '.menu-item-title' ).text(), |
| 470 | subItems = $( '.menu-item .menu-item-data-parent-id[value="' + parentItemId + '"]' ), |
| 471 | itemPosition = $(subItems.parents('.menu-item').get().reverse()).index( menuItem ) + 1; |
| 472 | |
| 473 | // String together help text for sub menu items |
| 474 | |
| 475 | title = itemName + '. ' + menus.subMenuFocus.replace('%d', itemPosition) + parentItemName + '.'; |
| 476 | } |
| 477 | |
| 478 | $this.prop('title', title).html( title ); |
| 479 | }); |
| 480 | }, |
| 481 | |
| 482 | refreshKeyboardAccessibility : function() { |
278 | | var menuItems = $('#menu-to-edit li'); |
279 | | menuItemsCount = menuItems.length, |
280 | | thisItem = $this.parents( 'li.menu-item' ), |
281 | | thisItemChildren = thisItem.childMenuItems(), |
282 | | thisItemData = thisItem.getItemData(), |
283 | | thisItemDepth = parseInt( thisItem.menuItemDepth() ), |
284 | | thisItemPosition = parseInt( thisItem.index() ), |
285 | | nextItem = thisItem.next(), |
286 | | nextItemChildren = nextItem.childMenuItems(), |
287 | | nextItemDepth = parseInt( nextItem.menuItemDepth() ) + 1, |
288 | | prevItem = thisItem.prev(), |
289 | | prevItemDepth = parseInt( prevItem.menuItemDepth() ), |
290 | | prevItemId = prevItem.getItemData()['menu-item-db-id']; |
291 | | |
327 | | // Does this item have sub items? |
328 | | if ( thisItemChildren ) { |
329 | | var items = thisItem.add( thisItemChildren ), |
330 | | nextItem = menuItems.eq( items.length + thisItemPosition ), |
331 | | nextItemChildren = 0 !== nextItem.childMenuItems().length; |
332 | | |
333 | | if ( nextItemChildren ) { |
334 | | var newDepth = parseInt( nextItem.menuItemDepth() ) + 1; |
335 | | thisItem.moveHorizontally( newDepth, thisItemDepth ); |
336 | | } |
337 | | |
338 | | // Have we reached the bottom? |
339 | | if ( menuItemsCount === thisItemPosition + items.length ) |
340 | | break; |
341 | | |
342 | | items.detach().insertAfter( menuItems.eq( thisItemPosition + items.length ) ); |
343 | | } else { |
344 | | // If next item has sub items, shift depth |
345 | | if ( 0 !== nextItemChildren.length ) |
346 | | thisItem.moveHorizontally( nextItemDepth, thisItemDepth ); |
347 | | |
348 | | // Have we reached the bottom |
349 | | if ( menuItemsCount === thisItemPosition + 1 ) |
350 | | break; |
351 | | thisItem.detach().insertAfter( menuItems.eq( thisItemPosition + 1 ) ); |
352 | | } |
| 509 | api.moveMenuItem( $this, 'down' ); |