wp/wp-admin/js/nav-menu.js
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
--- a/wp/wp-admin/js/nav-menu.js	Mon Oct 14 18:06:33 2019 +0200
+++ b/wp/wp-admin/js/nav-menu.js	Mon Oct 14 18:28:13 2019 +0200
@@ -6,17 +6,21 @@
  *
  * @package WordPress
  * @subpackage Administration
+ * @output wp-admin/js/nav-menu.js
  */
 
-/* global menus, postboxes, columns, isRtl, navMenuL10n, ajaxurl */
-
-var wpNavMenu;
+/* global menus, postboxes, columns, isRtl, navMenuL10n, ajaxurl, wpNavMenu */
 
 (function($) {
 
 	var api;
 
-	api = wpNavMenu = {
+	/**
+	 * Contains all the functions to handle WordPress navigation menus administration.
+	 *
+	 * @namespace wpNavMenu
+	 */
+	api = window.wpNavMenu = {
 
 		options : {
 			menuItemDepthPerLevel : 30, // Do not use directly. Use depthToPx and pxToDepth instead.
@@ -167,6 +171,8 @@
 				/**
 				 * Adds selected menu items to the menu.
 				 *
+				 * @ignore
+				 *
 				 * @param jQuery metabox The metabox jQuery object.
 				 */
 				addSelectedToMenu : function(processMethod) {
@@ -829,6 +835,20 @@
 					}
 				}
 			});
+
+			$( '#menu-name' ).on( 'input', _.debounce( function () {
+				var menuName = $( document.getElementById( 'menu-name' ) ),
+					menuNameVal = menuName.val();
+
+				if ( ! menuNameVal || ! menuNameVal.replace( /\s+/, '' ) ) {
+					// Add warning for invalid menu name.
+					menuName.parent().addClass( 'form-invalid' );
+				} else {
+					// Remove warning for valid menu name.
+					menuName.parent().removeClass( 'form-invalid' );
+				}
+			}, 500 ) );
+
 			$('#add-custom-links input[type="text"]').keypress(function(e){
 				$('#customlinkdiv').removeClass('form-invalid');
 
@@ -867,26 +887,14 @@
 		},
 
 		attachQuickSearchListeners : function() {
-			var searchTimer,
-				inputEvent;
+			var searchTimer;
 
 			// Prevent form submission.
 			$( '#nav-menu-meta' ).on( 'submit', function( event ) {
 				event.preventDefault();
 			});
 
-			/*
-			 * 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() {
+			$( '#nav-menu-meta' ).on( 'input', '.quick-search', function() {
 				var $this = $( this );
 
 				$this.attr( 'autocomplete', 'off' );
@@ -936,7 +944,7 @@
 		},
 
 		addCustomLink : function( processMethod ) {
-			var url = $('#custom-menu-item-url').val(),
+			var url = $('#custom-menu-item-url').val().trim(),
 				label = $('#custom-menu-item-name').val();
 
 			processMethod = processMethod || api.addMenuItemToBottom;
@@ -1172,8 +1180,8 @@
 			menuName = $('#menu-name'),
 			menuNameVal = menuName.val();
 			// Cancel and warn if invalid menu name
-			if( !menuNameVal || menuNameVal == menuName.attr('title') || !menuNameVal.replace(/\s+/, '') ) {
-				menuName.parent().addClass('form-invalid');
+			if ( ! menuNameVal || ! menuNameVal.replace( /\s+/, '' ) ) {
+				menuName.parent().addClass( 'form-invalid' );
 				return false;
 			}
 			// Copy menu theme locations