X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/61343b82c4f0da4c68e4c6373daafff4a81efdd1..5d244c8fd9a27c9f89dd08da2af6fbc67d4fce63:/wp-admin/js/nav-menu.js diff --git a/wp-admin/js/nav-menu.js b/wp-admin/js/nav-menu.js index 4c6efec3..33324b14 100644 --- a/wp-admin/js/nav-menu.js +++ b/wp-admin/js/nav-menu.js @@ -8,11 +8,15 @@ * @subpackage Administration */ +/* global menus, postboxes, columns, isRtl, navMenuL10n, ajaxurl */ + var wpNavMenu; (function($) { - var api = wpNavMenu = { + var api; + + api = wpNavMenu = { options : { menuItemDepthPerLevel : 30, // Do not use directly. Use depthToPx and pxToDepth instead. @@ -53,6 +57,8 @@ var wpNavMenu; this.initAccessibility(); this.initToggles(); + + this.initPreviewing(); }, jQueryExtensions : function() { @@ -111,7 +117,7 @@ var wpNavMenu; // If it has children, move those too if ( children ) { - children.each(function( index ) { + children.each(function() { var t = $(this), thisDepth = t.menuItemDepth(), newDepth = thisDepth + diff; @@ -130,11 +136,11 @@ var wpNavMenu; return this.each(function(){ var item = $(this), input = item.find( '.menu-item-data-parent-id' ), - depth = parseInt( item.menuItemDepth() ), + 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 + 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() ); @@ -155,14 +161,14 @@ var wpNavMenu; * @param jQuery metabox The metabox jQuery object. */ addSelectedToMenu : function(processMethod) { - if ( 0 == $('#menu-to-edit').length ) { + 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 = new RegExp('menu-item\\[(\[^\\]\]*)'); + 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; @@ -178,6 +184,7 @@ var wpNavMenu; 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 ); @@ -270,23 +277,24 @@ var wpNavMenu; moveMenuItem : function( $this, dir ) { - var menuItems = $('#menu-to-edit li'); + 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() ), - thisItemPosition = parseInt( thisItem.index() ), + thisItemDepth = parseInt( thisItem.menuItemDepth(), 10 ), + thisItemPosition = parseInt( thisItem.index(), 10 ), nextItem = thisItem.next(), nextItemChildren = nextItem.childMenuItems(), - nextItemDepth = parseInt( nextItem.menuItemDepth() ) + 1, + nextItemDepth = parseInt( nextItem.menuItemDepth(), 10 ) + 1, prevItem = thisItem.prev(), - prevItemDepth = parseInt( prevItem.menuItemDepth() ), + prevItemDepth = parseInt( prevItem.menuItemDepth(), 10 ), prevItemId = prevItem.getItemData()['menu-item-db-id']; switch ( dir ) { case 'up': - var newItemPosition = thisItemPosition - 1; + newItemPosition = thisItemPosition - 1; // Already at top if ( 0 === thisItemPosition ) @@ -302,7 +310,7 @@ var wpNavMenu; // Does this item have sub items? if ( thisItemChildren ) { - var items = thisItem.add( thisItemChildren ); + items = thisItem.add( thisItemChildren ); // Move the entire block items.detach().insertBefore( menuItems.eq( newItemPosition ) ).updateParentMenuItemDBId(); } else { @@ -312,12 +320,12 @@ var wpNavMenu; case 'down': // Does this item have sub items? if ( thisItemChildren ) { - var items = thisItem.add( thisItemChildren ), + items = thisItem.add( thisItemChildren ), nextItem = menuItems.eq( items.length + thisItemPosition ), nextItemChildren = 0 !== nextItem.childMenuItems().length; if ( nextItemChildren ) { - var newDepth = parseInt( nextItem.menuItemDepth() ) + 1; + newDepth = parseInt( nextItem.menuItemDepth(), 10 ) + 1; thisItem.moveHorizontally( newDepth, thisItemDepth ); } @@ -343,7 +351,7 @@ var wpNavMenu; break; // Does this item have sub items? if ( thisItemChildren ) { - var items = thisItem.add( thisItemChildren ); + items = thisItem.add( thisItemChildren ); // Move the entire block items.detach().insertBefore( menuItems.eq( 0 ) ).updateParentMenuItemDBId(); } else { @@ -373,27 +381,29 @@ var wpNavMenu; }, initAccessibility : function() { + var menu = $( '#menu-to-edit' ); + api.refreshKeyboardAccessibility(); api.refreshAdvancedAccessibility(); // Events - $( '.menus-move-up' ).on( 'click', function ( e ) { + menu.on( 'click', '.menus-move-up', function ( e ) { api.moveMenuItem( $( this ).parents( 'li.menu-item' ).find( 'a.item-edit' ), 'up' ); e.preventDefault(); }); - $( '.menus-move-down' ).on( 'click', function ( e ) { + menu.on( 'click', '.menus-move-down', function ( e ) { api.moveMenuItem( $( this ).parents( 'li.menu-item' ).find( 'a.item-edit' ), 'down' ); e.preventDefault(); }); - $( '.menus-move-top' ).on( 'click', function ( e ) { + menu.on( 'click', '.menus-move-top', function ( e ) { api.moveMenuItem( $( this ).parents( 'li.menu-item' ).find( 'a.item-edit' ), 'top' ); e.preventDefault(); }); - $( '.menus-move-left' ).on( 'click', function ( e ) { + menu.on( 'click', '.menus-move-left', function ( e ) { api.moveMenuItem( $( this ).parents( 'li.menu-item' ).find( 'a.item-edit' ), 'left' ); e.preventDefault(); }); - $( '.menus-move-right' ).on( 'click', function ( e ) { + menu.on( 'click', '.menus-move-right', function ( e ) { api.moveMenuItem( $( this ).parents( 'li.menu-item' ).find( 'a.item-edit' ), 'right' ); e.preventDefault(); }); @@ -402,18 +412,18 @@ var wpNavMenu; refreshAdvancedAccessibility : function() { // Hide all links by default - $( '.menu-item-settings .field-move a' ).hide(); + $( '.menu-item-settings .field-move a' ).css( 'display', 'none' ); $( '.item-edit' ).each( function() { - var $this = $(this), - movement = [], - availableMovement = '', - menuItem = $this.parents( 'li.menu-item' ).first(), + var thisLink, thisLinkText, primaryItems, itemPosition, title, + parentItem, parentItemId, parentItemName, subItems, + $this = $(this), + menuItem = $this.closest( 'li.menu-item' ).first(), depth = menuItem.menuItemDepth(), isPrimaryMenuItem = ( 0 === depth ), - itemName = $this.parents( '.menu-item-handle' ).find( '.menu-item-title' ).text(), - position = parseInt( menuItem.index() ), - prevItemDepth = ( isPrimaryMenuItem ) ? depth : parseInt( depth - 1 ), + itemName = $this.closest( '.menu-item-handle' ).find( '.menu-item-title' ).text(), + position = parseInt( menuItem.index(), 10 ), + prevItemDepth = ( isPrimaryMenuItem ) ? depth : parseInt( depth - 1, 10 ), prevItemNameLeft = menuItem.prevAll('.menu-item-depth-' + prevItemDepth).first().find( '.menu-item-title' ).text(), prevItemNameRight = menuItem.prevAll('.menu-item-depth-' + depth).first().find( '.menu-item-title' ).text(), totalMenuItems = $('#menu-to-edit li').length, @@ -421,58 +431,58 @@ var wpNavMenu; // Where can they move this menu item? if ( 0 !== position ) { - var thisLink = menuItem.find( '.menus-move-up' ); - thisLink.prop( 'title', menus.moveUp ).show(); + thisLink = menuItem.find( '.menus-move-up' ); + thisLink.prop( 'title', menus.moveUp ).css( 'display', 'inline' ); } if ( 0 !== position && isPrimaryMenuItem ) { - var thisLink = menuItem.find( '.menus-move-top' ); - thisLink.prop( 'title', menus.moveToTop ).show(); + thisLink = menuItem.find( '.menus-move-top' ); + thisLink.prop( 'title', menus.moveToTop ).css( 'display', 'inline' ); } if ( position + 1 !== totalMenuItems && 0 !== position ) { - var thisLink = menuItem.find( '.menus-move-down' ); - thisLink.prop( 'title', menus.moveDown ).show(); + thisLink = menuItem.find( '.menus-move-down' ); + thisLink.prop( 'title', menus.moveDown ).css( 'display', 'inline' ); } if ( 0 === position && 0 !== hasSameDepthSibling ) { - var thisLink = menuItem.find( '.menus-move-down' ); - thisLink.prop( 'title', menus.moveDown ).show(); + thisLink = menuItem.find( '.menus-move-down' ); + thisLink.prop( 'title', menus.moveDown ).css( 'display', 'inline' ); } if ( ! isPrimaryMenuItem ) { - var thisLink = menuItem.find( '.menus-move-left' ), - thisLinkText = menus.outFrom.replace( '%s', prevItemNameLeft ); - thisLink.prop( 'title', menus.moveOutFrom.replace( '%s', prevItemNameLeft ) ).html( thisLinkText ).show(); + 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' ); } if ( 0 !== position ) { if ( menuItem.find( '.menu-item-data-parent-id' ).val() !== menuItem.prev().find( '.menu-item-data-db-id' ).val() ) { - var thisLink = menuItem.find( '.menus-move-right' ), - thisLinkText = menus.under.replace( '%s', prevItemNameRight ); - thisLink.prop( 'title', menus.moveUnder.replace( '%s', prevItemNameRight ) ).html( thisLinkText ).show(); + 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' ); } } if ( isPrimaryMenuItem ) { - var primaryItems = $( '.menu-item-depth-0' ), - itemPosition = primaryItems.index( menuItem ) + 1, - totalMenuItems = primaryItems.length, + primaryItems = $( '.menu-item-depth-0' ), + itemPosition = primaryItems.index( menuItem ) + 1, + totalMenuItems = primaryItems.length, - // String together help text for primary menu items - title = menus.menuFocus.replace( '%1$s', itemName ).replace( '%2$d', itemPosition ).replace( '%3$d', totalMenuItems ); + // String together help text for primary menu items + title = menus.menuFocus.replace( '%1$s', itemName ).replace( '%2$d', itemPosition ).replace( '%3$d', totalMenuItems ); } else { - var parentItem = menuItem.prevAll( '.menu-item-depth-' + parseInt( depth - 1 ) ).first(), - parentItemId = parentItem.find( '.menu-item-data-db-id' ).val(), - parentItemName = parentItem.find( '.menu-item-title' ).text(), - subItems = $( '.menu-item .menu-item-data-parent-id[value="' + parentItemId + '"]' ), - itemPosition = $( subItems.parents('.menu-item').get().reverse() ).index( menuItem ) + 1; - - // String together help text for sub menu items - title = menus.subMenuFocus.replace( '%1$s', itemName ).replace( '%2$d', itemPosition ).replace( '%3$s', parentItemName ); + parentItem = menuItem.prevAll( '.menu-item-depth-' + parseInt( depth - 1, 10 ) ).first(), + parentItemId = parentItem.find( '.menu-item-data-db-id' ).val(), + parentItemName = parentItem.find( '.menu-item-title' ).text(), + subItems = $( '.menu-item .menu-item-data-parent-id[value="' + parentItemId + '"]' ), + itemPosition = $( subItems.parents('.menu-item').get().reverse() ).index( menuItem ) + 1; + + // String together help text for sub menu items + title = menus.subMenuFocus.replace( '%1$s', itemName ).replace( '%2$d', itemPosition ).replace( '%3$s', parentItemName ); } - $this.prop('title', title).html( title ); + $this.prop('title', title).text( title ); }); }, @@ -480,7 +490,10 @@ var wpNavMenu; $( '.item-edit' ).off( 'focus' ).on( 'focus', function(){ $(this).off( 'keydown' ).on( 'keydown', function(e){ - var $this = $(this); + var arrows, + $this = $( this ), + thisItem = $this.parents( 'li.menu-item' ), + thisItemData = thisItem.getItemData(); // Bail if it's not an arrow key if ( 37 != e.which && 38 != e.which && 39 != e.which && 40 != e.which ) @@ -494,7 +507,7 @@ var wpNavMenu; return; // If RTL, swap left/right arrows - var arrows = { '38' : 'up', '40' : 'down', '37' : 'left', '39' : 'right' }; + arrows = { '38': 'up', '40': 'down', '37': 'left', '39': 'right' }; if ( $('body').hasClass('rtl') ) arrows = { '38' : 'up', '40' : 'down', '39' : 'left', '37' : 'right' }; @@ -519,6 +532,21 @@ var wpNavMenu; }); }, + initPreviewing : function() { + // Update the item handle title when the navigation label is changed. + $( '#menu-to-edit' ).on( 'change input', '.edit-menu-item-title', function(e) { + var input = $( e.currentTarget ), title, titleEl; + title = input.val(); + titleEl = input.closest( '.menu-item' ).find( '.menu-item-title' ); + // Don't update to empty title. + if ( title ) { + titleEl.text( title ).removeClass( 'no-title' ); + } else { + titleEl.text( navMenuL10n.untitled ).addClass( 'no-title' ); + } + } ); + }, + initToggles : function() { // init postboxes postboxes.add_postbox_toggles('nav-menus'); @@ -527,10 +555,10 @@ var wpNavMenu; columns.useCheckboxesForHidden(); columns.checked = function(field) { $('.field-' + field).removeClass('hidden-field'); - } + }; columns.unchecked = function(field) { $('.field-' + field).addClass('hidden-field'); - } + }; // hide fields api.menuList.hideAdvancedMenuItemFields(); @@ -552,7 +580,7 @@ var wpNavMenu; body = $('body'), maxChildDepth, menuMaxDepth = initialMenuMaxDepth(); - if( 0 != $( '#menu-to-edit li' ).length ) + if( 0 !== $( '#menu-to-edit li' ).length ) $( '.drag-instructions' ).show(); // Use the right edge if RTL. @@ -604,7 +632,7 @@ var wpNavMenu; tempHolder = ui.placeholder.next(); tempHolder.css( 'margin-top', helperHeight + 'px' ); // Set the margin to absorb the placeholder ui.placeholder.detach(); // detach or jQuery UI will think the placeholder is a menu item - $(this).sortable( "refresh" ); // The children aren't sortable. We should let jQ UI know. + $(this).sortable( 'refresh' ); // The children aren't sortable. We should let jQ UI know. ui.item.after( ui.placeholder ); // reattach the placeholder. tempHolder.css('margin-top', 0); // reset the margin @@ -612,20 +640,21 @@ var wpNavMenu; updateSharedVars(ui); }, stop: function(e, ui) { - var children, depthChange = currentDepth - originalDepth; + var children, subMenuTitle, + depthChange = currentDepth - originalDepth; // Return child elements to the list children = transport.children().insertAfter(ui.item); // Add "sub menu" description - var subMenuTitle = ui.item.find( '.item-title .is-submenu' ); + subMenuTitle = ui.item.find( '.item-title .is-submenu' ); if ( 0 < currentDepth ) subMenuTitle.show(); else subMenuTitle.hide(); // Update depth classes - if( depthChange != 0 ) { + if ( 0 !== depthChange ) { ui.item.updateDepthClass( currentDepth ); children.shiftDepthClass( depthChange ); updateMenuMaxDepth( depthChange ); @@ -672,7 +701,7 @@ var wpNavMenu; if( nextThreshold && offset.top + helperHeight > nextThreshold ) { next.after( ui.placeholder ); updateSharedVars( ui ); - $(this).sortable( "refreshPositions" ); + $( this ).sortable( 'refreshPositions' ); } } }); @@ -705,7 +734,7 @@ var wpNavMenu; function initialMenuMaxDepth() { if( ! body[0].className ) return 0; var match = body[0].className.match(/menu-max-depth-(\d+)/); - return match && match[1] ? parseInt(match[1]) : 0; + return match && match[1] ? parseInt( match[1], 10 ) : 0; } function updateMenuMaxDepth( depthChange ) { @@ -759,7 +788,7 @@ var wpNavMenu; $('#add-custom-links input[type="text"]').keypress(function(e){ if ( e.keyCode === 13 ) { e.preventDefault(); - $("#submit-customlinkdiv").click(); + $( '#submit-customlinkdiv' ).click(); } }); }, @@ -778,7 +807,7 @@ var wpNavMenu; var $t = $(this), title = $t.attr('title'), val = $t.val(); $t.data( name, title ); - if( '' == val ) $t.val( title ); + if( '' === val ) $t.val( title ); else if ( title == val ) return; else $t.removeClass( name ); }).focus( function(){ @@ -787,7 +816,7 @@ var wpNavMenu; $t.val('').removeClass( name ); }).blur( function(){ var $t = $(this); - if( '' == $t.val() ) + if( '' === $t.val() ) $t.addClass( name ).val( $t.data(name) ); }); @@ -796,14 +825,14 @@ var wpNavMenu; attachThemeLocationsListeners : function() { var loc = $('#nav-menu-theme-locations'), params = {}; - params['action'] = 'menu-locations-save'; + params.action = 'menu-locations-save'; params['menu-settings-column-nonce'] = $('#menu-settings-column-nonce').val(); loc.find('input[type="submit"]').click(function() { loc.find('select').each(function() { params[this.name] = $(this).val(); }); loc.find('.spinner').show(); - $.post( ajaxurl, params, function(r) { + $.post( ajaxurl, params, function() { loc.find('.spinner').hide(); }); return false; @@ -859,7 +888,7 @@ var wpNavMenu; processMethod = processMethod || api.addMenuItemToBottom; - if ( '' == url || 'http://' == url ) + if ( '' === url || 'http://' == url ) return false; // Show the ajax spinner @@ -888,7 +917,8 @@ var wpNavMenu; addItemToMenu : function(menuItem, processMethod, callback) { var menu = $('#menu').val(), - nonce = $('#menu-settings-column-nonce').val(); + nonce = $('#menu-settings-column-nonce').val(), + params; processMethod = processMethod || function(){}; callback = callback || function(){}; @@ -902,12 +932,16 @@ var wpNavMenu; $.post( ajaxurl, params, function(menuMarkup) { var ins = $('#menu-instructions'); + + menuMarkup = $.trim( menuMarkup ); // Trim leading whitespaces processMethod(menuMarkup, params); + // Make it stand out a bit more visually, by adding a fadeIn $( 'li.pending' ).hide().fadeIn('slow'); $( '.drag-instructions' ).show(); if( ! ins.hasClass( 'menu-instructions-inactive' ) && ins.siblings().length ) ins.addClass( 'menu-instructions-inactive' ); + callback(); }); }, @@ -918,13 +952,13 @@ var wpNavMenu; * @param string menuMarkup The text server response of menu item markup. * @param object req The request arguments. */ - addMenuItemToBottom : function( menuMarkup, req ) { + addMenuItemToBottom : function( menuMarkup ) { $(menuMarkup).hideAdvancedMenuItemFields().appendTo( api.targetList ); api.refreshKeyboardAccessibility(); api.refreshAdvancedAccessibility(); }, - addMenuItemToTop : function( menuMarkup, req ) { + addMenuItemToTop : function( menuMarkup ) { $(menuMarkup).hideAdvancedMenuItemFields().prependTo( api.targetList ); api.refreshKeyboardAccessibility(); api.refreshAdvancedAccessibility(); @@ -935,7 +969,7 @@ var wpNavMenu; api.registerChange(); }); - if ( 0 != $('#menu-to-edit').length || 0 != $('.menu-location-menus select').length ) { + if ( 0 !== $('#menu-to-edit').length || 0 !== $('.menu-location-menus select').length ) { window.onbeforeunload = function(){ if ( api.menusChanged ) return navMenuL10n.saveAlert; @@ -1003,10 +1037,10 @@ var wpNavMenu; placeholder = document.createElement('div'), wrap = document.createElement('div'); - if ( ! metaBoxData['markup'] || ! toReplace ) + if ( ! metaBoxData.markup || ! toReplace ) return; - wrap.innerHTML = metaBoxData['markup'] ? metaBoxData['markup'] : ''; + wrap.innerHTML = metaBoxData.markup ? metaBoxData.markup : ''; toReplace.parentNode.insertBefore( placeholder, toReplace ); placeholder.parentNode.removeChild( toReplace ); @@ -1029,7 +1063,7 @@ var wpNavMenu; if ( matchedSection && matchedSection[1] ) { settings = $('#'+matchedSection[1]); item = settings.parent(); - if( 0 != item.length ) { + if( 0 !== item.length ) { if( item.hasClass('menu-item-edit-inactive') ) { if( ! settings.data('menu-item-data') ) { settings.data( 'menu-item-data', settings.getItemData() ); @@ -1055,7 +1089,7 @@ var wpNavMenu; return false; }, - eventOnClickMenuSave : function(clickedEl) { + eventOnClickMenuSave : function() { var locs = '', menuName = $('#menu-name'), menuNameVal = menuName.val(); @@ -1076,9 +1110,9 @@ var wpNavMenu; return true; }, - eventOnClickMenuDelete : function(clickedEl) { + eventOnClickMenuDelete : function() { // Delete warning AYS - if ( confirm( navMenuL10n.warnDeleteMenu ) ) { + if ( window.confirm( navMenuL10n.warnDeleteMenu ) ) { window.onbeforeunload = null; return true; } @@ -1103,7 +1137,7 @@ var wpNavMenu; var matched, newID, takenIDs = {}, form = document.getElementById('nav-menu-meta'), - pattern = new RegExp('menu-item\\[(\[^\\]\]*)', 'g'), + pattern = /menu-item[(\[^]\]*/, $items = $('
').html(resp).find('li'), $item; @@ -1149,7 +1183,7 @@ var wpNavMenu; var ins = $('#menu-instructions'); el.remove(); children.shiftDepthClass( -1 ).updateParentMenuItemDBId(); - if( 0 == $( '#menu-to-edit li' ).length ) { + if ( 0 === $( '#menu-to-edit li' ).length ) { $( '.drag-instructions' ).hide(); ins.removeClass( 'menu-instructions-inactive' ); }