File "nav-menu-20241222172113.js"

Full Path: /home/ycoalition/public_html/blog/wp-admin/css/nav-menu-20241222172113.js
File size: 20 KB
MIME-type: text/plain
Charset: utf-8

/**
 * WordPress Administration Navigation Menu
 * Interface JS functions
 *
 * @version 2.0.0
 *
 * @package WordPress
 * @subpackage Administration
 * @output wp-admin/js/nav-menu.js
 */

/* global menus, postboxes, columns, isRtl, ajaxurl, wpNavMenu */

(function($) {

	var api;

	/**
	 * 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.
			globalMaxDepth:  11,
			sortableItems:   '> *',
			targetTolerance: 0
		},

		menuList : undefined,	// Set in init.
		targetList : undefined, // Set in init.
		menusChanged : false,
		isRTL: !! ( 'undefined' != typeof isRtl && isRtl ),
		negateIfRTL: ( 'undefined' != typeof isRtl && isRtl ) ? -1 : 1,
		lastSearch: '',

		// Functions that run on init.
		init : function() {
			api.menuList = $('#menu-to-edit');
			api.targetList = api.menuList;

			this.jQueryExtensions();

			this.attachMenuEditListeners();

			this.attachBulkSelectButtonListeners();
			this.attachMenuCheckBoxListeners();
			this.attachMenuItemDeleteButton();
			this.attachPendingMenuItemsListForDeletion();

			this.attachQuickSearchListeners();
			this.attachThemeLocationsListeners();
			this.attachMenuSaveSubmitListeners();

			this.attachTabsPanelListeners();

			this.attachUnsavedChangesListener();

			if ( api.menuList.length )
				this.initSortables();

			if ( menus.oneThemeLocationNoMenus )
				$( '#posttype-page' ).addSelectedToMenu( api.addMenuItemToBottom );

			this.initManageLocations();

			this.initAccessibility();

			this.initToggles();

			this.initPreviewing();
		},

		jQueryExtensions : function() {
			// jQuery extensions.
			$.fn.extend({
				menuItemDepth : function() {
					var margin = api.isRTL ? this.eq(0).css('margin-right') : this.eq(0).css('margin-left');
					return api.pxToDepth( margin && -1 != margin.indexOf('px') ? margin.slice(0, -2) : 0 );
				},
				updateDepthClass : function(current, prev) {
					return this.each(function(){
						var t = $(this);
						prev = prev || t.menuItemDepth();
						$(this).removeClass('menu-item-depth-'+ prev )
							.addClass('menu-item-depth-'+ current );
					});
				},
				shiftDepthClass : function(change) {
					return this.each(function(){
						var t = $(this),
							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() {
					var result = $();
					this.each(function(){
						var t = $(this), depth = t.menuItemDepth(), next = t.next( '.menu-item' );
						while( next.length && next.menuItemDepth() > depth ) {
							result = result.add( next );
							next = next.next( '.menu-item' );
						}
					});
					return result;
				},
				shiftHorizontally : function( dir ) {
					return this.each(function(){
						var t = $(this),
							depth = t.menuItemDepth(),
							newDepth = depth + dir;

						// Change .menu-item-depth-n class.
						t.moveHorizontally( newDepth, depth );
					});
				},
				moveHorizontally : function( newDepth, depth ) {
					return this.each(function(){
						var t = $(this),
							children = t.childMenuItems(),
							diff = newDepth - depth,
							subItemText = t.find('.is-submenu');

						// Change .menu-item-depth-n class.
						t.updateDepthClass( newDepth, depth ).updateParentMenuItemDBId();

						// If it has children, move those too.
						if ( children ) {
							children.each(function() {
								var t = $(this),
									thisDepth = t.menuItemDepth(),
									newDepth = thisDepth + diff;
								t.updateDepthClass(newDepth, thisDepth).updateParentMenuItemDBId();
							});
						}

						// Show "Sub item" helper text.
						if (0 === newDepth)
							subItemText.hide();
						else
							subItemText.show();
					});
				},
				updateParentMenuItemDBId : function() {
					return this.each(function(){
						var item = $(this),
							input = item.find( '.menu-item-data-parent-id' ),
							depth = parseInt( item.menuItemDepth(), 10 ),
							parentDepth = depth - 1,
							parent = item.prevAll( '.menu-item-depth-' + parentDepth ).first();

						if ( 0 === depth ) { // Item is on the top level, has no parent.
							input.val(0);
						} else { // Find the parent item, and retrieve its object id.
							input.val( parent.find( '.menu-item-data-db-id' ).val() );
						}
					});
				},
				hideAdvancedMenuItemFields : function() {
					return this.each(function(){
						var that = $(this);
						$('.hide-column-tog').not(':checked').each(function(){
							that.find('.field-' + $(this).val() ).addClass('hidden-field');
						});
					});
				},
				/**
				 * Adds selected menu items to the menu.
				 *
				 * @ignore
				 *
				 * @param jQuery metabox The metabox jQuery object.
				 */
				addSelectedToMenu : function(processMethod) {
					if ( 0 === $('#menu-to-edit').length ) {
						return false;
					}

					return this.each(function() {
						var t = $(this), menuItems = {},
							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' ),
							re = /menu-item\[([^\]]*)/;

						processMethod = processMethod || api.addMenuItemToBottom;

						// If no items are checked, bail.
						if ( !checkboxes.length )
							return false;

						// Show the Ajax spinner.
						t.find( '.button-controls .spinner' ).addClass( 'is-active' );

						// Retrieve menu item data.
						$(checkboxes).each(function(){
							var t = $(this),
								listItemDBIDMatch = re.exec( t.attr('name') ),
								listItemDBID = 'undefined' == typeof listItemDBIDMatch[1] ? 0 : parseInt(listItemDBIDMatch[1], 10);

							if ( this.className && -1 != this.className.indexOf('add-to-top') )
								processMethod = api.addMenuItemToTop;
							menuItems[listItemDBID] = t.closest('li').getItemData( 'add-menu-item', listItemDBID );
						});

						// Add the items.
						api.addItemToMenu(menuItems, processMethod, function(){
							// Deselect the items and hide the Ajax spinner.
							checkboxes.prop( 'checked', false );
							t.find( '.button-controls .select-all' ).prop( 'checked', false );
							t.find( '.button-controls .spinner' ).removeClass( 'is-active' );
							t.updateParentDropdown();
							t.updateOrderDropdown();
						});
					});
				},
				getItemData : function( itemType, id ) {
					itemType = itemType || 'menu-item';

					var itemData = {}, i,
					fields = [
						'menu-item-db-id',
						'menu-item-object-id',
						'menu-item-object',
						'menu-item-parent-id',
						'menu-item-position',
						'menu-item-type',
						'menu-item-title',
						'menu-item-url',
						'menu-item-description',
						'menu-item-attr-title',
						'menu-item-target',
						'menu-item-classes',
						'menu-item-xfn'
					];

					if( !id && itemType == 'menu-item' ) {
						id = this.find('.menu-item-data-db-id').val();
					}

					if( !id ) return itemData;

					this.find('input').each(function() {
						var field;
						i = fields.length;
						while ( i-- ) {
							if( itemType == 'menu-item' )
								field = fields[i] + '[' + id + ']';
							else if( itemType == 'add-menu-item' )
								field = 'menu-item[' + id + '][' + fields[i] + ']';

							if (
								this.name &&
								field == this.name
							) {
								itemData[fields[i]] = this.value;
							}
						}
					});

					return itemData;
				},
				setItemData : function( itemData, itemType, id ) { // Can take a type, such as 'menu-item', or an id.
					itemType = itemType || 'menu-item';

					if( !id && itemType == 'menu-item' ) {
						id = $('.menu-item-data-db-id', this).val();
					}

					if( !id ) return this;

					this.find('input').each(function() {
						var t = $(this), field;
						$.each( itemData, function( attr, val ) {
							if( itemType == 'menu-item' )
								field = attr + '[' + id + ']';
							else if( itemType == 'add-menu-item' )
								field = 'menu-item[' + id + '][' + attr + ']';

							if ( field == t.attr('name') ) {
								t.val( val );
							}
						});
					});
					return this;
				},
				updateParentDropdown : function() {
					return this.each(function(){
						var menuItems = $( '#menu-to-edit li' ),
							parentDropdowns = $( '.edit-menu-item-parent' );

						$.each( parentDropdowns, function() {
							var parentDropdown = $( this ),
								$html = '',
								$selected = '',
								currentItemID = parentDropdown.closest( 'li.menu-item' ).find( '.menu-item-data-db-id' ).val(),
								currentparentID = parentDropdown.closest( 'li.menu-item' ).find( '.menu-item-data-parent-id' ).val(),
								currentItem = parentDropdown.closest( 'li.menu-item' ),
								currentMenuItemChild = currentItem.childMenuItems(),
								excludeMenuItem = [ currentItemID ];

							if ( currentMenuItemChild.length > 0 ) {
								$.each( currentMenuItemChild, function(){
									var childItem = $(this),
										childID = childItem.find( '.menu-item-data-db-id' ).val();

									excludeMenuItem.push( childID );
								});
							}

							if ( currentparentID == 0 ) {
								$selected = 'selected';
							}

							$html += '<option ' + $selected + ' value="0">' + wp.i18n._x( 'No Parent', 'menu item without a parent in navigation menu' ) + '</option>';

							$.each( menuItems, function() {
								var menuItem = $(this),
								$selected = '',
								menuID = menuItem.find( '.menu-item-data-db-id' ).val(),
								menuTitle = menuItem.find( '.edit-menu-item-title' ).val();

								if ( ! excludeMenuItem.includes( menuID ) ) {
									if ( currentparentID == menuID ) {
										$selected = 'selected';
									}
									$html += '<option ' + $selected + ' value="' + menuID + '">' + menuTitle + '</option>';
								}
							});

							parentDropdown.html( $html );
						});
						
					});
				},
				updateOrderDropdown : function() {
					return this.each( function() {
						var itemPosition,
							orderDropdowns = $( '.edit-menu-item-order' );

						$.each( orderDropdowns, function() {
							var orderDropdown = $( this ),
								menuItem = orderDropdown.closest( 'li.menu-item' ).first(),
								depth = menuItem.menuItemDepth(),
								isPrimaryMenuItem = ( 0 === depth ),
								$html = '',
								$selected = '';

							if ( isPrimaryMenuItem ) {
								var primaryItems = $( '.menu-item-depth-0' ),
									totalMenuItems = primaryItems.length;

								itemPosition = primaryItems.index( menuItem ) + 1;

								for ( let i = 1; i < totalMenuItems + 1; i++ ) {
									$selected = '';
									if ( i == itemPosition ) { 
										$selected = 'selected';
									}
									var itemString = wp.i18n.sprintf( 
										/* translators: 1: The current menu item number, 2: The total number of menu items. */
										wp.i18n._x( '%1$s of %2$s', 'part of a total number of menu items' ),
										i,
										totalMenuItems
									);
									$html += '<option ' + $selected + ' value="' + i + '">' + itemString + '</option>';
								}

							} else {
								var parentItem = menuItem.prevAll( '.menu-item-depth-' + parseInt( depth - 1, 10 ) ).first(),
									parentItemId = parentItem.find( '.menu-item-data-db-id' ).val(),
									subItems = $( '.menu-item .menu-item-data-parent-id[value="' + parentItemId + '"]' ),
									totalSubMenuItems = subItems.length;

								itemPosition = $( subItems.parents('.menu-item').get().reverse() ).index( menuItem ) + 1;

								for ( let i = 1; i < totalSubMenuItems + 1; i++ ) {
									$selected = '';
									if ( i == itemPosition ) {
										$selected = 'selected';
									}
									var submenuString = wp.i18n.sprintf( 
										/* translators: 1: The current submenu item number, 2: The total number of submenu items. */
										wp.i18n._x( '%1$s of %2$s', 'part of a total number of menu items' ),
										i,
										totalSubMenuItems
									);
									$html += '<option ' + $selected + ' value="' + i + '">' + submenuString + '</option>';
								}

							}

							orderDropdown.html( $html );
						});
						
					});
				}
			});
		},

		countMenuItems : function( depth ) {
			return $( '.menu-item-depth-' + depth ).length;
		},

		moveMenuItem : function( $this, dir ) {
			var items, newItemPosition, newDepth,
				menuItems = $( '#menu-to-edit li' ),
				menuItemsCount = menuItems.length,
				thisItem = $this.parents( 'li.menu-item' ),
				thisItemChildren = thisItem.childMenuItems(),
				thisItemData = thisItem.getItemData(),
				thisItemDepth = parseInt( thisItem.menuItemDepth(), 10 ),
				thisItemPosition = parseInt( thisItem.index(), 10 ),
				nextItem = thisItem.next(),
				nextItemChildren = nextItem.childMenuItems(),
				nextItemDepth = parseInt( nextItem.menuItemDepth(), 10 ) + 1,
				prevItem = thisItem.prev(),
				prevItemDepth = parseInt( prevItem.menuItemDepth(), 10 ),
				prevItemId = prevItem.getItemData()['menu-item-db-id'],
				a11ySpeech = menus[ 'moved' + dir.charAt(0).toUpperCase() + dir.slice(1) ];

			switch ( dir ) {
			case 'up':
				newItemPosition = thisItemPosition - 1;

				// Already at top.
				if ( 0 === thisItemPosition )
					break;

				// If a sub item is moved to top, shift it to 0 depth.
				if ( 0 === newItemPosition && 0 !== thisItemDepth )
					thisItem.moveHorizontally( 0, thisItemDepth );

				// If prev item is sub item, shift to match depth.
				if ( 0 !== prevItemDepth )
					thisItem.moveHorizontally( prevItemDepth, thisItemDepth );

				// Does this item have sub items?
				if ( thisItemChildren ) {
					items = thisItem.add( thisItemChildren );
					// Move the entire block.
					items.detach().insertBefore( menuItems.eq( newItemPosition ) ).updateParentMenuItemDBId();
				} else {
					thisItem.detach().insertBefore( menuItems.eq( newItemPosition ) ).updateParentMenuItemDBId();
				}
				break;
			case 'down':
				// Does this item have sub items?
				if ( thisItemChildren ) {
					items = thisItem.add( thisItemChildren ),
						nextItem = menuItems.eq( items.length + thisItemPosition ),
						nextItemChildren = 0 !== nextItem.childMenuItems().length;

					if ( nextItemChildren ) {
						newDepth = parseInt( nextItem.menuItemDepth(), 10 ) + 1;
						thisItem.moveHorizontally( newDepth, thisItemDepth );
					}

					// Have we reached the bottom?
					if ( menuItemsCount === thisItemPosition + items.length )
						break;

					items.detach().insertAfter( menuItems.eq( thisItemPosition + items.length ) ).updateParentMenuItemDBId();
				} else {
					// If next item has sub items, shift depth.
					if ( 0 !== nextItemChildren.length )
						thisItem.moveHorizontally( nextItemDepth, thisItemDepth );

					// Have we reached the bottom?
					if ( menuItemsCount === thisItemPosition + 1 )
						break;
					thisItem.detach().insertAfter( menuItems.eq( thisItemPosition + 1 ) ).updateParentMenuItemDBId();
				}
				break;
			case 'top':
				// Already at top.
				if ( 0 === thisItemPosition )
					break;
				// Does this item have sub items?
				if ( thisItemChildren ) {
					items = thisItem.add( thisItemChildren );
					// Move the entire block.
					items.detach().insertBefore( menuItems.eq( 0 ) ).updateParentMenuItemDBId();
				} else {
					thisItem.detach().insertBefore( menuItems.eq( 0 ) ).updateParentMenuItemDBId();
				}
				break;
			case 'left':
				// As far left as possible.
				if ( 0 === thisItemDepth )
					break;
				thisItem.shiftHorizontally( -1 );
				break;
			case 'right':
				// Can't be sub item at top.
				if ( 0 === thisItemPosition )
					break;
				// Already sub item of prevItem.
				if ( thisItemData['menu-item-parent-id'] === prevItemId )
					break;
				thisItem.shiftHorizontally( 1 );
				break;
			}
			$this.trigger( 'focus' );
			api.registerChange();
			api.refreshKeyboardAccessibility();
			api.refreshAdvancedAccessibility();
			thisItem.updateParentDropdown();
			thisItem.updateOrderDropdown();

			if ( a11ySpeech ) {
				wp.a11y.speak( a11ySpeech );
			}
		},

		initAccessibility : function() {
			var menu = $( '#menu-to-edit' );

			api.refreshKeyboardAccessibility();
			api.refreshAdvancedAccessibility();

			// Refresh the accessibility when the user comes close to the item in any way.
			menu.on( 'mouseenter.refreshAccessibility focus.refreshAccessibility touchstart.refreshAccessibility' , '.menu-item' , function(){
				api.refreshAdvancedAccessibilityOfItem( $( this ).find( 'a.item-edit' ) );
			} );

			// We have to update on click as well because we might hover first, change the item, and then click.
			menu.on( 'click', 'a.item-edit', function() {
				api.refreshAdvancedAccessibilityOfItem( $( this ) );
			} );

			// Links for moving items.
			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 );
				}
			});

			// Set menu parents data for all menu items.
			menu.updateParentDropdown();

			// Set menu order data for all menu items.
			menu.updateOrderDropdown();

			// Update menu item parent when value is changed.
			menu.on( 'change', '.edit-menu-item-parent', function() {
				api.changeMenuParent( $( this ) );
			});
			
			// Update menu item order when value is changed.
			menu.on( 'change', '.edit-menu-item-order', function() {
				api.changeMenuOrder( $( this ) );
			});
		},

		/**
		 * changeMenuParent( [parentDropdown] )
		 * 
		 * @since 6.7.0
		 *
		 * @param {object} parentDropdown select field
		 */
		changeMenuParent : function( parentDropdown ) {
			var menuItemNewPosition,
				menuItems = $( '#menu-to-edit li' ),
				$this = $( parentDropdown ),
				newParentID = $this.val(),
				menuItem = $this.closest( 'li.menu-item' ).first(),
				menuItemOldDepth = menuItem.menuItemDepth(),
				menuItemChildren = menuItem.childMenuItems(),
				menuItemNoChildren = parseInt( menuItem.childMenuItems().length, 10 ),
				parentItem = $( '#menu-item-' + newParentID ),
				parentItemDepth = parentItem.menuItemDepth(),
				menuItemNewDepth = parseInt( parentItemDepth ) + 1;

			if ( newParentID == 0 ) {
				menuItemNewDepth = 0;
			}

			menuItem.find( '.menu-item-data-parent-id' ).val( newParentID );
			menuItem.moveHorizontally( menuItemNewDepth, menuItemOldDepth );

			if ( menuItemNoChildren > 0 ) {
				menuItem = menuItem.add( menuItemChildren );
			}
			menuItem.detach();

			menuItems = $( '#menu-to-edit li' );

			var	parentItemPosition = parseInt( parentItem.index(), 10 ),
				parentItemNoChild = parseInt( parentItem.childMenuItems().length, 10 );

			if ( parentItemNoChild > 0 ){
				menuItemNewPosition = parentItemPosition + parentItemNoChild;
			} else {
				menuItemNewPosition = parentItemPosition;
			}

			if ( newParentID == 0 ) {
				menuItemNewPosition = menuItems.length - 1;
			}

			menuItem.insertAfter( menuItems.eq( menuItemNewPosition ) ).updateParentMenuItemDBId().updateParentDropdown().updateOrderDropdown();

			api.registerChange();
			api.refreshKeyboardAccessibility();
			api.refreshAdvancedAccessibility();
			$this.trigger( 'focus' );
			wp.a11y.speak( menus.parentUpdated, 'polite' );
		},

		/**
		 * changeMenuOrder( [OrderDropdown] )
		 * 
		 * @since 6.7.0
		 *
		 * @param {object} orderDropdown select field
		 */
		changeMenuOrder : function( orderDropdown ) {
			var menuItems = $( '#menu-to-edit li' ),
				$this = $( orderDropdown ),
				newOrderID = parseInt( $this.val(), 10),
				menuItem = $this.closest( 'li.menu-item' ).first(),
				menuItemChildren = menuItem.childMenuItems(),
				menuItemNoChildren = menuItemChildren.length,
				menuItemCurrentPosition = parseInt( menuItem.index(), 10 ),
				parentItemID = menuItem.find( '.menu-item-data-parent-id' ).val(),
				subItems = $( '.menu-item .menu-item-data-parent-id[value="' + parentItemID + '"]' ),
				currentItemAtPosition = $(subItems[newOrderID - 1]).closest( 'li.menu-item' );

			if ( menuItemNoChildren > 0 ) {
				menuItem = menuItem.add( menuItemChildren );
			}

			var currentItemNoChildren = currentItemAtPosition.childMenuItems().leng