menusChanged : false,
isRTL: !! ( 'undefined' != typeof isRtl && isRtl ),
negateIfRTL: ( 'undefined' != typeof isRtl && isRtl ) ? -1 : 1,
+ lastSearch: '',
// Functions that run on init.
init : function() {
this.attachMenuEditListeners();
- this.setupInputWithDefaultTitle();
this.attachQuickSearchListeners();
this.attachThemeLocationsListeners();
+ this.attachMenuSaveSubmitListeners();
this.attachTabsPanelListeners();
shiftDepthClass : function(change) {
return this.each(function(){
var t = $(this),
- depth = t.menuItemDepth();
- $(this).removeClass('menu-item-depth-'+ depth )
- .addClass('menu-item-depth-'+ (depth + change) );
+ depth = t.menuItemDepth(),
+ newDepth = depth + change;
+
+ t.removeClass( 'menu-item-depth-'+ depth )
+ .addClass( 'menu-item-depth-'+ ( newDepth ) );
+
+ if ( 0 === newDepth ) {
+ t.find( '.is-submenu' ).hide();
+ }
});
},
childMenuItems : function() {
return false;
// Show the ajax spinner
- t.find( '.spinner' ).addClass( 'is-active' );
+ t.find( '.button-controls .spinner' ).addClass( 'is-active' );
// Retrieve menu item data
$(checkboxes).each(function(){
api.addItemToMenu(menuItems, processMethod, function(){
// Deselect the items and hide the ajax spinner
checkboxes.removeAttr('checked');
- t.find( '.spinner' ).removeClass( 'is-active' );
+ t.find( '.button-controls .spinner' ).removeClass( 'is-active' );
});
});
},
} );
// Links for moving items
- menu.on( 'click', '.menus-move', function ( e ) {
+ menu.on( 'click', '.menus-move', function () {
var $this = $( this ),
dir = $this.data( 'dir' );
if ( 'undefined' !== typeof dir ) {
api.moveMenuItem( $( this ).parents( 'li.menu-item' ).find( 'a.item-edit' ), dir );
}
- e.preventDefault();
});
},
// Where can they move this menu item?
if ( 0 !== position ) {
thisLink = menuItem.find( '.menus-move-up' );
- thisLink.prop( 'title', menus.moveUp ).css( 'display', 'inline' );
+ thisLink.attr( 'aria-label', menus.moveUp ).css( 'display', 'inline' );
}
if ( 0 !== position && isPrimaryMenuItem ) {
thisLink = menuItem.find( '.menus-move-top' );
- thisLink.prop( 'title', menus.moveToTop ).css( 'display', 'inline' );
+ thisLink.attr( 'aria-label', menus.moveToTop ).css( 'display', 'inline' );
}
if ( position + 1 !== totalMenuItems && 0 !== position ) {
thisLink = menuItem.find( '.menus-move-down' );
- thisLink.prop( 'title', menus.moveDown ).css( 'display', 'inline' );
+ thisLink.attr( 'aria-label', menus.moveDown ).css( 'display', 'inline' );
}
if ( 0 === position && 0 !== hasSameDepthSibling ) {
thisLink = menuItem.find( '.menus-move-down' );
- thisLink.prop( 'title', menus.moveDown ).css( 'display', 'inline' );
+ thisLink.attr( 'aria-label', menus.moveDown ).css( 'display', 'inline' );
}
if ( ! isPrimaryMenuItem ) {
thisLink = menuItem.find( '.menus-move-left' ),
thisLinkText = menus.outFrom.replace( '%s', prevItemNameLeft );
- thisLink.prop( 'title', menus.moveOutFrom.replace( '%s', prevItemNameLeft ) ).text( thisLinkText ).css( 'display', 'inline' );
+ thisLink.attr( 'aria-label', menus.moveOutFrom.replace( '%s', prevItemNameLeft ) ).text( thisLinkText ).css( 'display', 'inline' );
}
if ( 0 !== position ) {
if ( menuItem.find( '.menu-item-data-parent-id' ).val() !== menuItem.prev().find( '.menu-item-data-db-id' ).val() ) {
thisLink = menuItem.find( '.menus-move-right' ),
thisLinkText = menus.under.replace( '%s', prevItemNameRight );
- thisLink.prop( 'title', menus.moveUnder.replace( '%s', prevItemNameRight ) ).text( thisLinkText ).css( 'display', 'inline' );
+ thisLink.attr( 'aria-label', menus.moveUnder.replace( '%s', prevItemNameRight ) ).text( thisLinkText ).css( 'display', 'inline' );
}
}
title = menus.subMenuFocus.replace( '%1$s', itemName ).replace( '%2$d', itemPosition ).replace( '%3$s', parentItemName );
}
- $this.prop('title', title).text( title );
+ // @todo Consider to update just the `aria-label` attribute.
+ $this.attr( 'aria-label', title ).text( title );
// Mark this item's accessibility as refreshed
$this.data( 'needs_accessibility_refresh', false );
*/
refreshAdvancedAccessibility : function() {
- // Hide all links by default
- $( '.menu-item-settings .field-move a' ).hide();
+ // Hide all the move buttons by default.
+ $( '.menu-item-settings .field-move .menus-move' ).hide();
// Mark all menu items as unprocessed
$( 'a.item-edit' ).data( 'needs_accessibility_refresh', true );
});
},
- /**
- * An interface for managing default values for input elements
- * that is both JS and accessibility-friendly.
- *
- * Input elements that add the class 'input-with-default-title'
- * will have their values set to the provided HTML title when empty.
- */
- setupInputWithDefaultTitle : function() {
- var name = 'input-with-default-title';
-
- $('.' + name).each( function(){
- var $t = $(this), title = $t.attr('title'), val = $t.val();
- $t.data( name, title );
-
- if( '' === val ) $t.val( title );
- else if ( title == val ) return;
- else $t.removeClass( name );
- }).focus( function(){
- var $t = $(this);
- if( $t.val() == $t.data(name) )
- $t.val('').removeClass( name );
- }).blur( function(){
- var $t = $(this);
- if( '' === $t.val() )
- $t.addClass( name ).val( $t.data(name) );
+ attachMenuSaveSubmitListeners : function() {
+ /*
+ * When a navigation menu is saved, store a JSON representation of all form data
+ * in a single input to avoid PHP `max_input_vars` limitations. See #14134.
+ */
+ $( '#update-nav-menu' ).submit( function() {
+ var navMenuData = $( '#update-nav-menu' ).serializeArray();
+ $( '[name="nav-menu-data"]' ).val( JSON.stringify( navMenuData ) );
});
-
- $( '.blank-slate .input-with-default-title' ).focus();
},
attachThemeLocationsListeners : function() {
},
attachQuickSearchListeners : function() {
- var searchTimer;
+ var searchTimer,
+ inputEvent;
- $('.quick-search').keypress(function(e){
- var t = $(this);
+ // Prevent form submission.
+ $( '#nav-menu-meta' ).on( 'submit', function( event ) {
+ event.preventDefault();
+ });
- if( 13 == e.which ) {
- api.updateQuickSearchResults( t );
- return false;
- }
+ /*
+ * Use feature detection to determine whether inputs should use
+ * the `keyup` or `input` event. Input is preferred but lacks support
+ * in legacy browsers. See changeset 34078, see also ticket #26600#comment:59
+ */
+ if ( 'oninput' in document.createElement( 'input' ) ) {
+ inputEvent = 'input';
+ } else {
+ inputEvent = 'keyup';
+ }
+
+ $( '#nav-menu-meta' ).on( inputEvent, '.quick-search', function() {
+ var $this = $( this );
- if( searchTimer ) clearTimeout(searchTimer);
+ $this.attr( 'autocomplete', 'off' );
+
+ if ( searchTimer ) {
+ clearTimeout( searchTimer );
+ }
- searchTimer = setTimeout(function(){
- api.updateQuickSearchResults( t );
- }, 400);
- }).attr('autocomplete','off');
+ searchTimer = setTimeout( function() {
+ api.updateQuickSearchResults( $this );
+ }, 500 );
+ }).on( 'blur', '.quick-search', function() {
+ api.lastSearch = '';
+ });
},
updateQuickSearchResults : function(input) {
var panel, params,
- minSearchLength = 2,
- q = input.val();
+ minSearchLength = 2,
+ q = input.val();
+
+ /*
+ * Minimum characters for a search. Also avoid a new AJAX search when
+ * the pressed key (e.g. arrows) doesn't change the searched term.
+ */
+ if ( q.length < minSearchLength || api.lastSearch == q ) {
+ return;
+ }
- if( q.length < minSearchLength ) return;
+ api.lastSearch = q;
panel = input.parents('.tabs-panel');
params = {
// select the search bar
$('.quick-search', wrapper).focus();
+ // Hide controls in the search tab if no items found.
+ if ( ! wrapper.find( '.tabs-panel-active .menu-item-title' ).length ) {
+ wrapper.addClass( 'has-no-menu-item' );
+ } else {
+ wrapper.removeClass( 'has-no-menu-item' );
+ }
+
e.preventDefault();
} else if ( target.hasClass('select-all') ) {
selectAreaMatch = /#(.*)$/.exec(e.target.href);
else if ( e.target.id && -1 != e.target.id.indexOf('submit-') )
$('#' + e.target.id.replace(/submit-/, '')).addSelectedToMenu( api.addMenuItemToBottom );
return false;
- } else if ( target.hasClass('page-numbers') ) {
- $.post( ajaxurl, e.target.href.replace(/.*\?/, '').replace(/action=([^&]*)/, '') + '&action=menu-get-metabox',
- function( resp ) {
- if ( -1 == resp.indexOf('replace-id') )
- return;
-
- var metaBoxData = $.parseJSON(resp),
- toReplace = document.getElementById(metaBoxData['replace-id']),
- placeholder = document.createElement('div'),
- wrap = document.createElement('div');
-
- if ( ! metaBoxData.markup || ! toReplace )
- return;
+ }
+ });
- wrap.innerHTML = metaBoxData.markup ? metaBoxData.markup : '';
+ /*
+ * Delegate the `click` event and attach it just to the pagination
+ * links thus excluding the current page `<span>`. See ticket #35577.
+ */
+ $( '#nav-menu-meta' ).on( 'click', 'a.page-numbers', function() {
+ var $container = $( this ).closest( '.inside' );
- toReplace.parentNode.insertBefore( placeholder, toReplace );
- placeholder.parentNode.removeChild( toReplace );
+ $.post( ajaxurl, this.href.replace( /.*\?/, '' ).replace( /action=([^&]*)/, '' ) + '&action=menu-get-metabox',
+ function( resp ) {
+ var metaBoxData = $.parseJSON( resp ),
+ toReplace;
- placeholder.parentNode.insertBefore( wrap, placeholder );
+ if ( -1 === resp.indexOf( 'replace-id' ) ) {
+ return;
+ }
- placeholder.parentNode.removeChild( placeholder );
+ // Get the post type menu meta box to update.
+ toReplace = document.getElementById( metaBoxData['replace-id'] );
+ if ( ! metaBoxData.markup || ! toReplace ) {
+ return;
}
- );
- return false;
- }
+ // Update the post type menu meta box with new content from the response.
+ $container.html( metaBoxData.markup );
+ }
+ );
+
+ return false;
});
},
form = document.getElementById('nav-menu-meta'),
pattern = /menu-item[(\[^]\]*/,
$items = $('<div>').html(resp).find('li'),
+ wrapper = panel.closest( '.accordion-section-content' ),
$item;
if( ! $items.length ) {
$('.categorychecklist', panel).html( '<li><p>' + navMenuL10n.noResultsFound + '</p></li>' );
$( '.spinner', panel ).removeClass( 'is-active' );
+ wrapper.addClass( 'has-no-menu-item' );
return;
}
$('.categorychecklist', panel).html( $items );
$( '.spinner', panel ).removeClass( 'is-active' );
+ wrapper.removeClass( 'has-no-menu-item' );
},
removeMenuItem : function(el) {