2 * WordPress Administration Navigation Menu
3 * Interface JS functions
8 * @subpackage Administration
15 var api = wpNavMenu = {
18 menuItemDepthPerLevel : 30, // Do not use directly. Use depthToPx and pxToDepth instead.
22 menuList : undefined, // Set in init.
23 targetList : undefined, // Set in init.
25 isRTL: !! ( 'undefined' != typeof isRtl && isRtl ),
26 negateIfRTL: ( 'undefined' != typeof isRtl && isRtl ) ? -1 : 1,
28 // Functions that run on init.
30 api.menuList = $('#menu-to-edit');
31 api.targetList = api.menuList;
33 this.jQueryExtensions();
35 this.attachMenuEditListeners();
37 this.setupInputWithDefaultTitle();
38 this.attachQuickSearchListeners();
39 this.attachThemeLocationsListeners();
41 this.attachTabsPanelListeners();
43 this.attachUnsavedChangesListener();
45 if ( api.menuList.length )
48 if ( menus.oneThemeLocationNoMenus )
49 $( '#posttype-page' ).addSelectedToMenu( api.addMenuItemToBottom );
51 this.initManageLocations();
53 this.initAccessibility();
58 jQueryExtensions : function() {
61 menuItemDepth : function() {
62 var margin = api.isRTL ? this.eq(0).css('margin-right') : this.eq(0).css('margin-left');
63 return api.pxToDepth( margin && -1 != margin.indexOf('px') ? margin.slice(0, -2) : 0 );
65 updateDepthClass : function(current, prev) {
66 return this.each(function(){
68 prev = prev || t.menuItemDepth();
69 $(this).removeClass('menu-item-depth-'+ prev )
70 .addClass('menu-item-depth-'+ current );
73 shiftDepthClass : function(change) {
74 return this.each(function(){
76 depth = t.menuItemDepth();
77 $(this).removeClass('menu-item-depth-'+ depth )
78 .addClass('menu-item-depth-'+ (depth + change) );
81 childMenuItems : function() {
84 var t = $(this), depth = t.menuItemDepth(), next = t.next();
85 while( next.length && next.menuItemDepth() > depth ) {
86 result = result.add( next );
92 shiftHorizontally : function( dir ) {
93 return this.each(function(){
95 depth = t.menuItemDepth(),
96 newDepth = depth + dir;
98 // Change .menu-item-depth-n class
99 t.moveHorizontally( newDepth, depth );
102 moveHorizontally : function( newDepth, depth ) {
103 return this.each(function(){
105 children = t.childMenuItems(),
106 diff = newDepth - depth,
107 subItemText = t.find('.is-submenu');
109 // Change .menu-item-depth-n class
110 t.updateDepthClass( newDepth, depth ).updateParentMenuItemDBId();
112 // If it has children, move those too
114 children.each(function( index ) {
116 thisDepth = t.menuItemDepth(),
117 newDepth = thisDepth + diff;
118 t.updateDepthClass(newDepth, thisDepth).updateParentMenuItemDBId();
122 // Show "Sub item" helper text
129 updateParentMenuItemDBId : function() {
130 return this.each(function(){
132 input = item.find( '.menu-item-data-parent-id' ),
133 depth = parseInt( item.menuItemDepth() ),
134 parentDepth = depth - 1,
135 parent = item.prevAll( '.menu-item-depth-' + parentDepth ).first();
137 if ( 0 == depth ) { // Item is on the top level, has no parent
139 } else { // Find the parent item, and retrieve its object id.
140 input.val( parent.find( '.menu-item-data-db-id' ).val() );
144 hideAdvancedMenuItemFields : function() {
145 return this.each(function(){
147 $('.hide-column-tog').not(':checked').each(function(){
148 that.find('.field-' + $(this).val() ).addClass('hidden-field');
153 * Adds selected menu items to the menu.
155 * @param jQuery metabox The metabox jQuery object.
157 addSelectedToMenu : function(processMethod) {
158 if ( 0 == $('#menu-to-edit').length ) {
162 return this.each(function() {
163 var t = $(this), menuItems = {},
164 checkboxes = ( menus.oneThemeLocationNoMenus && 0 == t.find('.tabs-panel-active .categorychecklist li input:checked').length ) ? t.find('#page-all li input[type="checkbox"]') : t.find('.tabs-panel-active .categorychecklist li input:checked'),
165 re = new RegExp('menu-item\\[(\[^\\]\]*)');
167 processMethod = processMethod || api.addMenuItemToBottom;
169 // If no items are checked, bail.
170 if ( !checkboxes.length )
173 // Show the ajax spinner
174 t.find('.spinner').show();
176 // Retrieve menu item data
177 $(checkboxes).each(function(){
179 listItemDBIDMatch = re.exec( t.attr('name') ),
180 listItemDBID = 'undefined' == typeof listItemDBIDMatch[1] ? 0 : parseInt(listItemDBIDMatch[1], 10);
182 if ( this.className && -1 != this.className.indexOf('add-to-top') )
183 processMethod = api.addMenuItemToTop;
184 menuItems[listItemDBID] = t.closest('li').getItemData( 'add-menu-item', listItemDBID );
188 api.addItemToMenu(menuItems, processMethod, function(){
189 // Deselect the items and hide the ajax spinner
190 checkboxes.removeAttr('checked');
191 t.find('.spinner').hide();
195 getItemData : function( itemType, id ) {
196 itemType = itemType || 'menu-item';
198 var itemData = {}, i,
201 'menu-item-object-id',
203 'menu-item-parent-id',
204 'menu-item-position',
208 'menu-item-description',
209 'menu-item-attr-title',
215 if( !id && itemType == 'menu-item' ) {
216 id = this.find('.menu-item-data-db-id').val();
219 if( !id ) return itemData;
221 this.find('input').each(function() {
225 if( itemType == 'menu-item' )
226 field = fields[i] + '[' + id + ']';
227 else if( itemType == 'add-menu-item' )
228 field = 'menu-item[' + id + '][' + fields[i] + ']';
234 itemData[fields[i]] = this.value;
241 setItemData : function( itemData, itemType, id ) { // Can take a type, such as 'menu-item', or an id.
242 itemType = itemType || 'menu-item';
244 if( !id && itemType == 'menu-item' ) {
245 id = $('.menu-item-data-db-id', this).val();
248 if( !id ) return this;
250 this.find('input').each(function() {
251 var t = $(this), field;
252 $.each( itemData, function( attr, val ) {
253 if( itemType == 'menu-item' )
254 field = attr + '[' + id + ']';
255 else if( itemType == 'add-menu-item' )
256 field = 'menu-item[' + id + '][' + attr + ']';
258 if ( field == t.attr('name') ) {
268 countMenuItems : function( depth ) {
269 return $( '.menu-item-depth-' + depth ).length;
272 moveMenuItem : function( $this, dir ) {
274 var menuItems = $('#menu-to-edit li');
275 menuItemsCount = menuItems.length,
276 thisItem = $this.parents( 'li.menu-item' ),
277 thisItemChildren = thisItem.childMenuItems(),
278 thisItemData = thisItem.getItemData(),
279 thisItemDepth = parseInt( thisItem.menuItemDepth() ),
280 thisItemPosition = parseInt( thisItem.index() ),
281 nextItem = thisItem.next(),
282 nextItemChildren = nextItem.childMenuItems(),
283 nextItemDepth = parseInt( nextItem.menuItemDepth() ) + 1,
284 prevItem = thisItem.prev(),
285 prevItemDepth = parseInt( prevItem.menuItemDepth() ),
286 prevItemId = prevItem.getItemData()['menu-item-db-id'];
290 var newItemPosition = thisItemPosition - 1;
293 if ( 0 === thisItemPosition )
296 // If a sub item is moved to top, shift it to 0 depth
297 if ( 0 === newItemPosition && 0 !== thisItemDepth )
298 thisItem.moveHorizontally( 0, thisItemDepth );
300 // If prev item is sub item, shift to match depth
301 if ( 0 !== prevItemDepth )
302 thisItem.moveHorizontally( prevItemDepth, thisItemDepth );
304 // Does this item have sub items?
305 if ( thisItemChildren ) {
306 var items = thisItem.add( thisItemChildren );
307 // Move the entire block
308 items.detach().insertBefore( menuItems.eq( newItemPosition ) ).updateParentMenuItemDBId();
310 thisItem.detach().insertBefore( menuItems.eq( newItemPosition ) ).updateParentMenuItemDBId();
314 // Does this item have sub items?
315 if ( thisItemChildren ) {
316 var items = thisItem.add( thisItemChildren ),
317 nextItem = menuItems.eq( items.length + thisItemPosition ),
318 nextItemChildren = 0 !== nextItem.childMenuItems().length;
320 if ( nextItemChildren ) {
321 var newDepth = parseInt( nextItem.menuItemDepth() ) + 1;
322 thisItem.moveHorizontally( newDepth, thisItemDepth );
325 // Have we reached the bottom?
326 if ( menuItemsCount === thisItemPosition + items.length )
329 items.detach().insertAfter( menuItems.eq( thisItemPosition + items.length ) ).updateParentMenuItemDBId();
331 // If next item has sub items, shift depth
332 if ( 0 !== nextItemChildren.length )
333 thisItem.moveHorizontally( nextItemDepth, thisItemDepth );
335 // Have we reached the bottom
336 if ( menuItemsCount === thisItemPosition + 1 )
338 thisItem.detach().insertAfter( menuItems.eq( thisItemPosition + 1 ) ).updateParentMenuItemDBId();
343 if ( 0 === thisItemPosition )
345 // Does this item have sub items?
346 if ( thisItemChildren ) {
347 var items = thisItem.add( thisItemChildren );
348 // Move the entire block
349 items.detach().insertBefore( menuItems.eq( 0 ) ).updateParentMenuItemDBId();
351 thisItem.detach().insertBefore( menuItems.eq( 0 ) ).updateParentMenuItemDBId();
355 // As far left as possible
356 if ( 0 === thisItemDepth )
358 thisItem.shiftHorizontally( -1 );
361 // Can't be sub item at top
362 if ( 0 === thisItemPosition )
364 // Already sub item of prevItem
365 if ( thisItemData['menu-item-parent-id'] === prevItemId )
367 thisItem.shiftHorizontally( 1 );
371 api.registerChange();
372 api.refreshKeyboardAccessibility();
373 api.refreshAdvancedAccessibility();
376 initAccessibility : function() {
377 api.refreshKeyboardAccessibility();
378 api.refreshAdvancedAccessibility();
381 $( '.menus-move-up' ).on( 'click', function ( e ) {
382 api.moveMenuItem( $( this ).parents( 'li.menu-item' ).find( 'a.item-edit' ), 'up' );
385 $( '.menus-move-down' ).on( 'click', function ( e ) {
386 api.moveMenuItem( $( this ).parents( 'li.menu-item' ).find( 'a.item-edit' ), 'down' );
389 $( '.menus-move-top' ).on( 'click', function ( e ) {
390 api.moveMenuItem( $( this ).parents( 'li.menu-item' ).find( 'a.item-edit' ), 'top' );
393 $( '.menus-move-left' ).on( 'click', function ( e ) {
394 api.moveMenuItem( $( this ).parents( 'li.menu-item' ).find( 'a.item-edit' ), 'left' );
397 $( '.menus-move-right' ).on( 'click', function ( e ) {
398 api.moveMenuItem( $( this ).parents( 'li.menu-item' ).find( 'a.item-edit' ), 'right' );
403 refreshAdvancedAccessibility : function() {
405 // Hide all links by default
406 $( '.menu-item-settings .field-move a' ).css( 'display', 'none' );
408 $( '.item-edit' ).each( function() {
411 availableMovement = '',
412 menuItem = $this.closest( 'li.menu-item' ).first(),
413 depth = menuItem.menuItemDepth(),
414 isPrimaryMenuItem = ( 0 === depth ),
415 itemName = $this.closest( '.menu-item-handle' ).find( '.menu-item-title' ).text(),
416 position = parseInt( menuItem.index() ),
417 prevItemDepth = ( isPrimaryMenuItem ) ? depth : parseInt( depth - 1 ),
418 prevItemNameLeft = menuItem.prevAll('.menu-item-depth-' + prevItemDepth).first().find( '.menu-item-title' ).text(),
419 prevItemNameRight = menuItem.prevAll('.menu-item-depth-' + depth).first().find( '.menu-item-title' ).text(),
420 totalMenuItems = $('#menu-to-edit li').length,
421 hasSameDepthSibling = menuItem.nextAll( '.menu-item-depth-' + depth ).length;
423 // Where can they move this menu item?
424 if ( 0 !== position ) {
425 var thisLink = menuItem.find( '.menus-move-up' );
426 thisLink.prop( 'title', menus.moveUp ).css( 'display', 'inline' );
429 if ( 0 !== position && isPrimaryMenuItem ) {
430 var thisLink = menuItem.find( '.menus-move-top' );
431 thisLink.prop( 'title', menus.moveToTop ).css( 'display', 'inline' );
434 if ( position + 1 !== totalMenuItems && 0 !== position ) {
435 var thisLink = menuItem.find( '.menus-move-down' );
436 thisLink.prop( 'title', menus.moveDown ).css( 'display', 'inline' );
439 if ( 0 === position && 0 !== hasSameDepthSibling ) {
440 var thisLink = menuItem.find( '.menus-move-down' );
441 thisLink.prop( 'title', menus.moveDown ).css( 'display', 'inline' );
444 if ( ! isPrimaryMenuItem ) {
445 var thisLink = menuItem.find( '.menus-move-left' ),
446 thisLinkText = menus.outFrom.replace( '%s', prevItemNameLeft );
447 thisLink.prop( 'title', menus.moveOutFrom.replace( '%s', prevItemNameLeft ) ).html( thisLinkText ).css( 'display', 'inline' );
450 if ( 0 !== position ) {
451 if ( menuItem.find( '.menu-item-data-parent-id' ).val() !== menuItem.prev().find( '.menu-item-data-db-id' ).val() ) {
452 var thisLink = menuItem.find( '.menus-move-right' ),
453 thisLinkText = menus.under.replace( '%s', prevItemNameRight );
454 thisLink.prop( 'title', menus.moveUnder.replace( '%s', prevItemNameRight ) ).html( thisLinkText ).css( 'display', 'inline' );
458 if ( isPrimaryMenuItem ) {
459 var primaryItems = $( '.menu-item-depth-0' ),
460 itemPosition = primaryItems.index( menuItem ) + 1,
461 totalMenuItems = primaryItems.length,
463 // String together help text for primary menu items
464 title = menus.menuFocus.replace( '%1$s', itemName ).replace( '%2$d', itemPosition ).replace( '%3$d', totalMenuItems );
466 var parentItem = menuItem.prevAll( '.menu-item-depth-' + parseInt( depth - 1 ) ).first(),
467 parentItemId = parentItem.find( '.menu-item-data-db-id' ).val(),
468 parentItemName = parentItem.find( '.menu-item-title' ).text(),
469 subItems = $( '.menu-item .menu-item-data-parent-id[value="' + parentItemId + '"]' ),
470 itemPosition = $( subItems.parents('.menu-item').get().reverse() ).index( menuItem ) + 1;
472 // String together help text for sub menu items
473 title = menus.subMenuFocus.replace( '%1$s', itemName ).replace( '%2$d', itemPosition ).replace( '%3$s', parentItemName );
476 $this.prop('title', title).html( title );
480 refreshKeyboardAccessibility : function() {
481 $( '.item-edit' ).off( 'focus' ).on( 'focus', function(){
482 $(this).off( 'keydown' ).on( 'keydown', function(e){
486 // Bail if it's not an arrow key
487 if ( 37 != e.which && 38 != e.which && 39 != e.which && 40 != e.which )
490 // Avoid multiple keydown events
491 $this.off('keydown');
493 // Bail if there is only one menu item
494 if ( 1 === $('#menu-to-edit li').length )
497 // If RTL, swap left/right arrows
498 var arrows = { '38' : 'up', '40' : 'down', '37' : 'left', '39' : 'right' };
499 if ( $('body').hasClass('rtl') )
500 arrows = { '38' : 'up', '40' : 'down', '39' : 'left', '37' : 'right' };
502 switch ( arrows[e.which] ) {
504 api.moveMenuItem( $this, 'up' );
507 api.moveMenuItem( $this, 'down' );
510 api.moveMenuItem( $this, 'left' );
513 api.moveMenuItem( $this, 'right' );
516 // Put focus back on same menu item
517 $( '#edit-' + thisItemData['menu-item-db-id'] ).focus();
523 initToggles : function() {
525 postboxes.add_postbox_toggles('nav-menus');
527 // adjust columns functions for menus UI
528 columns.useCheckboxesForHidden();
529 columns.checked = function(field) {
530 $('.field-' + field).removeClass('hidden-field');
532 columns.unchecked = function(field) {
533 $('.field-' + field).addClass('hidden-field');
536 api.menuList.hideAdvancedMenuItemFields();
538 $('.hide-postbox-tog').click(function () {
539 var hidden = $( '.accordion-container li.accordion-section' ).filter(':hidden').map(function() { return this.id; }).get().join(',');
541 action: 'closed-postboxes',
543 closedpostboxesnonce: jQuery('#closedpostboxesnonce').val(),
549 initSortables : function() {
550 var currentDepth = 0, originalDepth, minDepth, maxDepth,
551 prev, next, prevBottom, nextThreshold, helperHeight, transport,
552 menuEdge = api.menuList.offset().left,
553 body = $('body'), maxChildDepth,
554 menuMaxDepth = initialMenuMaxDepth();
556 if( 0 != $( '#menu-to-edit li' ).length )
557 $( '.drag-instructions' ).show();
559 // Use the right edge if RTL.
560 menuEdge += api.isRTL ? api.menuList.width() : 0;
562 api.menuList.sortable({
563 handle: '.menu-item-handle',
564 placeholder: 'sortable-placeholder',
565 start: function(e, ui) {
566 var height, width, parent, children, tempHolder;
568 // handle placement for rtl orientation
570 ui.item[0].style.right = 'auto';
572 transport = ui.item.children('.menu-item-transport');
574 // Set depths. currentDepth must be set before children are located.
575 originalDepth = ui.item.menuItemDepth();
576 updateCurrentDepth(ui, originalDepth);
578 // Attach child elements to parent
579 // Skip the placeholder
580 parent = ( ui.item.next()[0] == ui.placeholder[0] ) ? ui.item.next() : ui.item;
581 children = parent.childMenuItems();
582 transport.append( children );
584 // Update the height of the placeholder to match the moving item.
585 height = transport.outerHeight();
586 // If there are children, account for distance between top of children and parent
587 height += ( height > 0 ) ? (ui.placeholder.css('margin-top').slice(0, -2) * 1) : 0;
588 height += ui.helper.outerHeight();
589 helperHeight = height;
590 height -= 2; // Subtract 2 for borders
591 ui.placeholder.height(height);
593 // Update the width of the placeholder to match the moving item.
594 maxChildDepth = originalDepth;
595 children.each(function(){
596 var depth = $(this).menuItemDepth();
597 maxChildDepth = (depth > maxChildDepth) ? depth : maxChildDepth;
599 width = ui.helper.find('.menu-item-handle').outerWidth(); // Get original width
600 width += api.depthToPx(maxChildDepth - originalDepth); // Account for children
601 width -= 2; // Subtract 2 for borders
602 ui.placeholder.width(width);
604 // Update the list of menu items.
605 tempHolder = ui.placeholder.next();
606 tempHolder.css( 'margin-top', helperHeight + 'px' ); // Set the margin to absorb the placeholder
607 ui.placeholder.detach(); // detach or jQuery UI will think the placeholder is a menu item
608 $(this).sortable( "refresh" ); // The children aren't sortable. We should let jQ UI know.
609 ui.item.after( ui.placeholder ); // reattach the placeholder.
610 tempHolder.css('margin-top', 0); // reset the margin
612 // Now that the element is complete, we can update...
613 updateSharedVars(ui);
615 stop: function(e, ui) {
616 var children, depthChange = currentDepth - originalDepth;
618 // Return child elements to the list
619 children = transport.children().insertAfter(ui.item);
621 // Add "sub menu" description
622 var subMenuTitle = ui.item.find( '.item-title .is-submenu' );
623 if ( 0 < currentDepth )
628 // Update depth classes
629 if( depthChange != 0 ) {
630 ui.item.updateDepthClass( currentDepth );
631 children.shiftDepthClass( depthChange );
632 updateMenuMaxDepth( depthChange );
635 api.registerChange();
636 // Update the item data.
637 ui.item.updateParentMenuItemDBId();
639 // address sortable's incorrectly-calculated top in opera
640 ui.item[0].style.top = 0;
642 // handle drop placement for rtl orientation
644 ui.item[0].style.left = 'auto';
645 ui.item[0].style.right = 0;
648 api.refreshKeyboardAccessibility();
649 api.refreshAdvancedAccessibility();
651 change: function(e, ui) {
652 // Make sure the placeholder is inside the menu.
653 // Otherwise fix it, or we're in trouble.
654 if( ! ui.placeholder.parent().hasClass('menu') )
655 (prev.length) ? prev.after( ui.placeholder ) : api.menuList.prepend( ui.placeholder );
657 updateSharedVars(ui);
659 sort: function(e, ui) {
660 var offset = ui.helper.offset(),
661 edge = api.isRTL ? offset.left + ui.helper.width() : offset.left,
662 depth = api.negateIfRTL * api.pxToDepth( edge - menuEdge );
663 // Check and correct if depth is not within range.
664 // Also, if the dragged element is dragged upwards over
665 // an item, shift the placeholder to a child position.
666 if ( depth > maxDepth || offset.top < prevBottom ) depth = maxDepth;
667 else if ( depth < minDepth ) depth = minDepth;
669 if( depth != currentDepth )
670 updateCurrentDepth(ui, depth);
672 // If we overlap the next element, manually shift downwards
673 if( nextThreshold && offset.top + helperHeight > nextThreshold ) {
674 next.after( ui.placeholder );
675 updateSharedVars( ui );
676 $(this).sortable( "refreshPositions" );
681 function updateSharedVars(ui) {
684 prev = ui.placeholder.prev();
685 next = ui.placeholder.next();
687 // Make sure we don't select the moving item.
688 if( prev[0] == ui.item[0] ) prev = prev.prev();
689 if( next[0] == ui.item[0] ) next = next.next();
691 prevBottom = (prev.length) ? prev.offset().top + prev.height() : 0;
692 nextThreshold = (next.length) ? next.offset().top + next.height() / 3 : 0;
693 minDepth = (next.length) ? next.menuItemDepth() : 0;
696 maxDepth = ( (depth = prev.menuItemDepth() + 1) > api.options.globalMaxDepth ) ? api.options.globalMaxDepth : depth;
701 function updateCurrentDepth(ui, depth) {
702 ui.placeholder.updateDepthClass( depth, currentDepth );
703 currentDepth = depth;
706 function initialMenuMaxDepth() {
707 if( ! body[0].className ) return 0;
708 var match = body[0].className.match(/menu-max-depth-(\d+)/);
709 return match && match[1] ? parseInt(match[1]) : 0;
712 function updateMenuMaxDepth( depthChange ) {
713 var depth, newDepth = menuMaxDepth;
714 if ( depthChange === 0 ) {
716 } else if ( depthChange > 0 ) {
717 depth = maxChildDepth + depthChange;
718 if( depth > menuMaxDepth )
720 } else if ( depthChange < 0 && maxChildDepth == menuMaxDepth ) {
721 while( ! $('.menu-item-depth-' + newDepth, api.menuList).length && newDepth > 0 )
724 // Update the depth class.
725 body.removeClass( 'menu-max-depth-' + menuMaxDepth ).addClass( 'menu-max-depth-' + newDepth );
726 menuMaxDepth = newDepth;
730 initManageLocations : function () {
731 $('#menu-locations-wrap form').submit(function(){
732 window.onbeforeunload = null;
734 $('.menu-location-menus select').on('change', function () {
735 var editLink = $(this).closest('tr').find('.locations-edit-menu-link');
736 if ($(this).find('option:selected').data('orig'))
743 attachMenuEditListeners : function() {
745 $('#update-nav-menu').bind('click', function(e) {
746 if ( e.target && e.target.className ) {
747 if ( -1 != e.target.className.indexOf('item-edit') ) {
748 return that.eventOnClickEditLink(e.target);
749 } else if ( -1 != e.target.className.indexOf('menu-save') ) {
750 return that.eventOnClickMenuSave(e.target);
751 } else if ( -1 != e.target.className.indexOf('menu-delete') ) {
752 return that.eventOnClickMenuDelete(e.target);
753 } else if ( -1 != e.target.className.indexOf('item-delete') ) {
754 return that.eventOnClickMenuItemDelete(e.target);
755 } else if ( -1 != e.target.className.indexOf('item-cancel') ) {
756 return that.eventOnClickCancelLink(e.target);
760 $('#add-custom-links input[type="text"]').keypress(function(e){
761 if ( e.keyCode === 13 ) {
763 $("#submit-customlinkdiv").click();
769 * An interface for managing default values for input elements
770 * that is both JS and accessibility-friendly.
772 * Input elements that add the class 'input-with-default-title'
773 * will have their values set to the provided HTML title when empty.
775 setupInputWithDefaultTitle : function() {
776 var name = 'input-with-default-title';
778 $('.' + name).each( function(){
779 var $t = $(this), title = $t.attr('title'), val = $t.val();
780 $t.data( name, title );
782 if( '' == val ) $t.val( title );
783 else if ( title == val ) return;
784 else $t.removeClass( name );
785 }).focus( function(){
787 if( $t.val() == $t.data(name) )
788 $t.val('').removeClass( name );
792 $t.addClass( name ).val( $t.data(name) );
795 $( '.blank-slate .input-with-default-title' ).focus();
798 attachThemeLocationsListeners : function() {
799 var loc = $('#nav-menu-theme-locations'), params = {};
800 params['action'] = 'menu-locations-save';
801 params['menu-settings-column-nonce'] = $('#menu-settings-column-nonce').val();
802 loc.find('input[type="submit"]').click(function() {
803 loc.find('select').each(function() {
804 params[this.name] = $(this).val();
806 loc.find('.spinner').show();
807 $.post( ajaxurl, params, function(r) {
808 loc.find('.spinner').hide();
814 attachQuickSearchListeners : function() {
817 $('.quick-search').keypress(function(e){
820 if( 13 == e.which ) {
821 api.updateQuickSearchResults( t );
825 if( searchTimer ) clearTimeout(searchTimer);
827 searchTimer = setTimeout(function(){
828 api.updateQuickSearchResults( t );
830 }).attr('autocomplete','off');
833 updateQuickSearchResults : function(input) {
838 if( q.length < minSearchLength ) return;
840 panel = input.parents('.tabs-panel');
842 'action': 'menu-quick-search',
843 'response-format': 'markup',
844 'menu': $('#menu').val(),
845 'menu-settings-column-nonce': $('#menu-settings-column-nonce').val(),
847 'type': input.attr('name')
850 $('.spinner', panel).show();
852 $.post( ajaxurl, params, function(menuMarkup) {
853 api.processQuickSearchQueryResponse(menuMarkup, params, panel);
857 addCustomLink : function( processMethod ) {
858 var url = $('#custom-menu-item-url').val(),
859 label = $('#custom-menu-item-name').val();
861 processMethod = processMethod || api.addMenuItemToBottom;
863 if ( '' == url || 'http://' == url )
866 // Show the ajax spinner
867 $('.customlinkdiv .spinner').show();
868 this.addLinkToMenu( url, label, processMethod, function() {
869 // Remove the ajax spinner
870 $('.customlinkdiv .spinner').hide();
871 // Set custom link form back to defaults
872 $('#custom-menu-item-name').val('').blur();
873 $('#custom-menu-item-url').val('http://');
877 addLinkToMenu : function(url, label, processMethod, callback) {
878 processMethod = processMethod || api.addMenuItemToBottom;
879 callback = callback || function(){};
883 'menu-item-type': 'custom',
884 'menu-item-url': url,
885 'menu-item-title': label
887 }, processMethod, callback);
890 addItemToMenu : function(menuItem, processMethod, callback) {
891 var menu = $('#menu').val(),
892 nonce = $('#menu-settings-column-nonce').val();
894 processMethod = processMethod || function(){};
895 callback = callback || function(){};
898 'action': 'add-menu-item',
900 'menu-settings-column-nonce': nonce,
901 'menu-item': menuItem
904 $.post( ajaxurl, params, function(menuMarkup) {
905 var ins = $('#menu-instructions');
907 menuMarkup = $.trim( menuMarkup ); // Trim leading whitespaces
908 processMethod(menuMarkup, params);
910 // Make it stand out a bit more visually, by adding a fadeIn
911 $( 'li.pending' ).hide().fadeIn('slow');
912 $( '.drag-instructions' ).show();
913 if( ! ins.hasClass( 'menu-instructions-inactive' ) && ins.siblings().length )
914 ins.addClass( 'menu-instructions-inactive' );
921 * Process the add menu item request response into menu list item.
923 * @param string menuMarkup The text server response of menu item markup.
924 * @param object req The request arguments.
926 addMenuItemToBottom : function( menuMarkup, req ) {
927 $(menuMarkup).hideAdvancedMenuItemFields().appendTo( api.targetList );
928 api.refreshKeyboardAccessibility();
929 api.refreshAdvancedAccessibility();
932 addMenuItemToTop : function( menuMarkup, req ) {
933 $(menuMarkup).hideAdvancedMenuItemFields().prependTo( api.targetList );
934 api.refreshKeyboardAccessibility();
935 api.refreshAdvancedAccessibility();
938 attachUnsavedChangesListener : function() {
939 $('#menu-management input, #menu-management select, #menu-management, #menu-management textarea, .menu-location-menus select').change(function(){
940 api.registerChange();
943 if ( 0 != $('#menu-to-edit').length || 0 != $('.menu-location-menus select').length ) {
944 window.onbeforeunload = function(){
945 if ( api.menusChanged )
946 return navMenuL10n.saveAlert;
949 // Make the post boxes read-only, as they can't be used yet
950 $( '#menu-settings-column' ).find( 'input,select' ).end().find( 'a' ).attr( 'href', '#' ).unbind( 'click' );
954 registerChange : function() {
955 api.menusChanged = true;
958 attachTabsPanelListeners : function() {
959 $('#menu-settings-column').bind('click', function(e) {
960 var selectAreaMatch, panelId, wrapper, items,
961 target = $(e.target);
963 if ( target.hasClass('nav-tab-link') ) {
965 panelId = target.data( 'type' );
967 wrapper = target.parents('.accordion-section-content').first();
969 // upon changing tabs, we want to uncheck all checkboxes
970 $('input', wrapper).removeAttr('checked');
972 $('.tabs-panel-active', wrapper).removeClass('tabs-panel-active').addClass('tabs-panel-inactive');
973 $('#' + panelId, wrapper).removeClass('tabs-panel-inactive').addClass('tabs-panel-active');
975 $('.tabs', wrapper).removeClass('tabs');
976 target.parent().addClass('tabs');
978 // select the search bar
979 $('.quick-search', wrapper).focus();
982 } else if ( target.hasClass('select-all') ) {
983 selectAreaMatch = /#(.*)$/.exec(e.target.href);
984 if ( selectAreaMatch && selectAreaMatch[1] ) {
985 items = $('#' + selectAreaMatch[1] + ' .tabs-panel-active .menu-item-title input');
986 if( items.length === items.filter(':checked').length )
987 items.removeAttr('checked');
989 items.prop('checked', true);
992 } else if ( target.hasClass('submit-add-to-menu') ) {
993 api.registerChange();
995 if ( e.target.id && 'submit-customlinkdiv' == e.target.id )
996 api.addCustomLink( api.addMenuItemToBottom );
997 else if ( e.target.id && -1 != e.target.id.indexOf('submit-') )
998 $('#' + e.target.id.replace(/submit-/, '')).addSelectedToMenu( api.addMenuItemToBottom );
1000 } else if ( target.hasClass('page-numbers') ) {
1001 $.post( ajaxurl, e.target.href.replace(/.*\?/, '').replace(/action=([^&]*)/, '') + '&action=menu-get-metabox',
1003 if ( -1 == resp.indexOf('replace-id') )
1006 var metaBoxData = $.parseJSON(resp),
1007 toReplace = document.getElementById(metaBoxData['replace-id']),
1008 placeholder = document.createElement('div'),
1009 wrap = document.createElement('div');
1011 if ( ! metaBoxData['markup'] || ! toReplace )
1014 wrap.innerHTML = metaBoxData['markup'] ? metaBoxData['markup'] : '';
1016 toReplace.parentNode.insertBefore( placeholder, toReplace );
1017 placeholder.parentNode.removeChild( toReplace );
1019 placeholder.parentNode.insertBefore( wrap, placeholder );
1021 placeholder.parentNode.removeChild( placeholder );
1031 eventOnClickEditLink : function(clickedEl) {
1033 matchedSection = /#(.*)$/.exec(clickedEl.href);
1034 if ( matchedSection && matchedSection[1] ) {
1035 settings = $('#'+matchedSection[1]);
1036 item = settings.parent();
1037 if( 0 != item.length ) {
1038 if( item.hasClass('menu-item-edit-inactive') ) {
1039 if( ! settings.data('menu-item-data') ) {
1040 settings.data( 'menu-item-data', settings.getItemData() );
1042 settings.slideDown('fast');
1043 item.removeClass('menu-item-edit-inactive')
1044 .addClass('menu-item-edit-active');
1046 settings.slideUp('fast');
1047 item.removeClass('menu-item-edit-active')
1048 .addClass('menu-item-edit-inactive');
1055 eventOnClickCancelLink : function(clickedEl) {
1056 var settings = $( clickedEl ).closest( '.menu-item-settings' ),
1057 thisMenuItem = $( clickedEl ).closest( '.menu-item' );
1058 thisMenuItem.removeClass('menu-item-edit-active').addClass('menu-item-edit-inactive');
1059 settings.setItemData( settings.data('menu-item-data') ).hide();
1063 eventOnClickMenuSave : function(clickedEl) {
1065 menuName = $('#menu-name'),
1066 menuNameVal = menuName.val();
1067 // Cancel and warn if invalid menu name
1068 if( !menuNameVal || menuNameVal == menuName.attr('title') || !menuNameVal.replace(/\s+/, '') ) {
1069 menuName.parent().addClass('form-invalid');
1072 // Copy menu theme locations
1073 $('#nav-menu-theme-locations select').each(function() {
1074 locs += '<input type="hidden" name="' + this.name + '" value="' + $(this).val() + '" />';
1076 $('#update-nav-menu').append( locs );
1077 // Update menu item position data
1078 api.menuList.find('.menu-item-data-position').val( function(index) { return index + 1; } );
1079 window.onbeforeunload = null;
1084 eventOnClickMenuDelete : function(clickedEl) {
1085 // Delete warning AYS
1086 if ( confirm( navMenuL10n.warnDeleteMenu ) ) {
1087 window.onbeforeunload = null;
1093 eventOnClickMenuItemDelete : function(clickedEl) {
1094 var itemID = parseInt(clickedEl.id.replace('delete-', ''), 10);
1095 api.removeMenuItem( $('#menu-item-' + itemID) );
1096 api.registerChange();
1101 * Process the quick search response into a search result
1103 * @param string resp The server response to the query.
1104 * @param object req The request arguments.
1105 * @param jQuery panel The tabs panel we're searching in.
1107 processQuickSearchQueryResponse : function(resp, req, panel) {
1110 form = document.getElementById('nav-menu-meta'),
1111 pattern = new RegExp('menu-item\\[(\[^\\]\]*)', 'g'),
1112 $items = $('<div>').html(resp).find('li'),
1115 if( ! $items.length ) {
1116 $('.categorychecklist', panel).html( '<li><p>' + navMenuL10n.noResultsFound + '</p></li>' );
1117 $('.spinner', panel).hide();
1121 $items.each(function(){
1124 // make a unique DB ID number
1125 matched = pattern.exec($item.html());
1127 if ( matched && matched[1] ) {
1129 while( form.elements['menu-item[' + newID + '][menu-item-type]'] || takenIDs[ newID ] ) {
1133 takenIDs[newID] = true;
1134 if ( newID != matched[1] ) {
1135 $item.html( $item.html().replace(new RegExp(
1136 'menu-item\\[' + matched[1] + '\\]', 'g'),
1137 'menu-item[' + newID + ']'
1143 $('.categorychecklist', panel).html( $items );
1144 $('.spinner', panel).hide();
1147 removeMenuItem : function(el) {
1148 var children = el.childMenuItems();
1150 el.addClass('deleting').animate({
1153 }, 350, function() {
1154 var ins = $('#menu-instructions');
1156 children.shiftDepthClass( -1 ).updateParentMenuItemDBId();
1157 if( 0 == $( '#menu-to-edit li' ).length ) {
1158 $( '.drag-instructions' ).hide();
1159 ins.removeClass( 'menu-instructions-inactive' );
1164 depthToPx : function(depth) {
1165 return depth * api.options.menuItemDepthPerLevel;
1168 pxToDepth : function(px) {
1169 return Math.floor(px / api.options.menuItemDepthPerLevel);
1174 $(document).ready(function(){ wpNavMenu.init(); });