Consolidate nav menu item ajax. Other misc fixes. props koopersmith, see #13204.

git-svn-id: https://develop.svn.wordpress.org/trunk@14464 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Nacin 2010-05-05 20:29:50 +00:00
parent 71c9ad21de
commit 971d337bd3
3 changed files with 124 additions and 127 deletions

View File

@ -87,7 +87,7 @@ var WPNavMenuHandler = function ($) {
return Math.floor(px / menuItemDepthPerLevel); return Math.floor(px / menuItemDepthPerLevel);
}, },
menuList, targetList; menuList, targetList, api;
// jQuery extensions // jQuery extensions
$.fn.extend({ $.fn.extend({
@ -166,10 +166,50 @@ var WPNavMenuHandler = function ($) {
else else
t.selectItem(); t.selectItem();
}); });
} },
/**
* Adds selected menu items to the menu.
*
* @param jQuery metabox The metabox jQuery object.
*/
addSelectedToMenu : function(processMethod) {
return this.each(function() {
var t = $(this),
checked = t.find('.tabs-panel-active .categorychecklist li input:checked'),
re = new RegExp('menu-item\\[(\[^\\]\]*)');
processMethod = processMethod || api.addMenuItemToBottom;
// If no items are checked, bail.
if ( !checked.length )
return false;
// Show the ajax spinner
t.find('img.waiting').show();
// Retrieve menu item data
$(checked).each(function(){
var checkbox = $(this),
item = checkbox.parent().prev();
listItemDBIDMatch = re.exec( checkbox.attr('name') );
listItemDBID = 'undefined' == typeof listItemDBIDMatch[1] ? 0 : parseInt(listItemDBIDMatch[1], 10);
listItemData = getListDataFromID(listItemDBID);
menuItem = {};
menuItem[listItemDBID] = listItemData;
api.addItemToMenu(menuItem, processMethod, function(){
item.deselectItem();
});
}); });
return { // Remove the ajax spinner
t.find('img.waiting').hide();
});
},
});
return api = {
// Functions that run on init. // Functions that run on init.
init : function() { init : function() {
@ -178,7 +218,9 @@ var WPNavMenuHandler = function ($) {
this.attachMenuEditListeners(); this.attachMenuEditListeners();
this.attachMenuMetaListeners(document.getElementById('nav-menu-meta')); this.setupInputWithDefaultTitle();
this.attachAddMenuItemListeners();
this.attachQuickSearchListeners();
this.attachTabsPanelListeners(); this.attachTabsPanelListeners();
@ -422,38 +464,39 @@ var WPNavMenuHandler = function ($) {
}).blur( function(){ }).blur( function(){
var $t = $(this); var $t = $(this);
if( '' == $t.val() ) if( '' == $t.val() )
$t.val( $t.data(name) ).addClass( name ); $t.addClass( name ).val( $t.data(name) );
}); });
}, },
attachMenuMetaListeners : function(formEL) { attachAddMenuItemListeners : function() {
if ( ! formEL ) var form = $('#nav-menu-meta');
return;
var that = this; form.find('.add-to-menu input').click(function(){
this.setupInputWithDefaultTitle(); $(this).trigger('wp-add-menu-item', [api.addMenuItemToBottom]);
return false;
});
form.find('.customlinkdiv').bind('wp-add-menu-item', function(e, processMethod) {
api.addCustomLink( processMethod );
});
form.find('.posttypediv, .taxonomydiv').bind('wp-add-menu-item', function(e, processMethod) {
$(this).addSelectedToMenu( processMethod );
});
},
attachQuickSearchListeners : function() {
var that = this,
form = $('#nav-menu-meta');
// auto-suggest for the quick-search boxes // auto-suggest for the quick-search boxes
$('input.quick-search').each(function(i, el) { $('input.quick-search').each(function(i, el) {
that.setupQuickSearchEventListeners(el); that.setupQuickSearchEventListeners(el);
}); });
form.find('.quick-search-submit').click(function() {
// If a "Add to Menu" button was clicked, submit that metabox ajax style. $(this).trigger('wp-quick-search');
$(formEL).click(function(e) {
// based on the input, call that function
var divcontainer = $(e.target).parent().parent().parent();
if ( $(e.target).is('input') && $(e.target).hasClass('button-secondary') && !$(e.target).hasClass('quick-search-submit') ) {
if ( $(divcontainer).hasClass('customlinkdiv') ) {
that.addCustomLink();
} else if ( $(divcontainer).hasClass('posttypediv') || $(divcontainer).hasClass('taxonomydiv') ) {
that.addItemsToMenu( $(divcontainer).attr('id') );
};
return false; return false;
} else if ( $(e.target).is('input') && $(e.target).hasClass('quick-search-submit') ) { });
that.quickSearch( $(divcontainer).attr('id') ); form.find('.inside').children().bind('wp-quick-search', function() {
return false; that.quickSearch( $(this).attr('id') );
};
}); });
}, },
@ -482,57 +525,76 @@ var WPNavMenuHandler = function ($) {
}); });
}, },
addCustomLink : function(url, label, addToTop) { addCustomLink : function( processMethod ) {
var url = url || $('#custom-menu-item-url').val(), var url = $('#custom-menu-item-url').val(),
label = label || $('#custom-menu-item-name').val(), label = $('#custom-menu-item-name').val();
addToTop = addToTop || false,
menu = $('#menu').val(), processMethod = processMethod || api.addMenuItemToBottom;
nonce = $('#menu-settings-column-nonce').val(),
params = {},
that = this,
processMethod = function(){};
if ( '' == url || 'http://' == url ) if ( '' == url || 'http://' == url )
return false; return false;
// Show the ajax spinner // Show the ajax spinner
$('.customlinkdiv img.waiting').show(); $('.customlinkdiv img.waiting').show();
this.addLinkToMenu( url, label, processMethod, function() {
// Remove the ajax spinner
$('.customlinkdiv img.waiting').hide();
// Set custom link form back to defaults
$('#custom-menu-item-name').val('').blur();
$('#custom-menu-item-url').val('http://');
});
},
params = { addLinkToMenu : function(url, label, processMethod, callback) {
'action': 'add-menu-item', processMethod = processMethod || api.addMenuItemToBottom;
'menu': menu, callback = callback || function(){};
'menu-settings-column-nonce': nonce,
'menu-item': { api.addItemToMenu({
'-1': { '-1': {
'menu-item-type': 'custom', 'menu-item-type': 'custom',
'menu-item-url': url, 'menu-item-url': url,
'menu-item-title': label 'menu-item-title': label
} }
} }, processMethod, callback);
},
addItemToMenu : function(menuItem, processMethod, callback) {
var menu = $('#menu').val(),
nonce = $('#menu-settings-column-nonce').val();
processMethod = processMethod || function(){};
callback = callback || function(){};
params = {
'action': 'add-menu-item',
'menu': menu,
'menu-settings-column-nonce': nonce,
'menu-item': menuItem,
}; };
processMethod = addToTop ? that.addMenuItemToTop : that.addMenuItemToBottom;
$.post( ajaxurl, params, function(menuMarkup) { $.post( ajaxurl, params, function(menuMarkup) {
processMethod.call(that, menuMarkup, params); processMethod(menuMarkup, params);
callback();
// Remove the ajax spinner
$('.customlinkdiv img.waiting').hide();
// Reset the form
wpNavMenu.resetCustomLinkForm();
}); });
}, },
resetCustomLinkForm : function() { /**
// set custom link form back to defaults * Process the add menu item request response into menu list item.
$('#custom-menu-item-name').val('').blur(); *
$('#custom-menu-item-url').val('http://'); * @param string menuMarkup The text server response of menu item markup.
* @param object req The request arguments.
*/
addMenuItemToBottom : function( menuMarkup, req ) {
$(menuMarkup).hideAdvancedMenuItemFields().appendTo( targetList );
},
addMenuItemToTop : function( menuMarkup, req ) {
$(menuMarkup).hideAdvancedMenuItemFields().prependTo( targetList );
}, },
attachHomeLinkListener : function() { attachHomeLinkListener : function() {
$('.add-home-link', '.customlinkdiv').click(function(e) { $('.add-home-link', '.customlinkdiv').click(function(e) {
wpNavMenu.addCustomLink( navMenuL10n.homeurl, navMenuL10n.home, true); api.addLinkToMenu( navMenuL10n.homeurl, navMenuL10n.home, api.addMenuItemToTop, recalculateMenuItemPositions );
return false; return false;
}); });
}, },
@ -783,71 +845,6 @@ var WPNavMenuHandler = function ($) {
} }
}, },
/**
* Adds menu items to the menu.
*
* @param string id The id of the metabox
*/
addItemsToMenu : function(id, addToTop) {
var items = $( '.tabs-panel-active .categorychecklist li input:checked', '#' + id),
menu = $('#menu').val(),
nonce = $('#menu-settings-column-nonce').val(),
params = {},
that = this,
addToTop = addToTop || false,
processMethod = function(){},
re = new RegExp('menu-item\\[(\[^\\]\]*)');
processMethod = addToTop ? that.addMenuItemToTop : that.addMenuItemToBottom;
// If no items are checked, bail.
if ( !items.length )
return false;
// Show the ajax spinner
$('#' + id + ' img.waiting').show();
// do stuff
$(items).each(function(){
listItemDBIDMatch = re.exec( $(this).attr('name') );
listItemDBID = 'undefined' == typeof listItemDBIDMatch[1] ? 0 : parseInt(listItemDBIDMatch[1], 10);
listItemData = getListDataFromID(listItemDBID);
params = {
'action': 'add-menu-item',
'menu': menu,
'menu-settings-column-nonce': nonce,
'menu-item': {}
};
params['menu-item'][listItemDBID] = listItemData;
$.post( ajaxurl, params, function(menuMarkup) {
processMethod.call(that, menuMarkup, params);
});
// Uncheck the item
$(this).parent().prev().deselectItem();
});
// Remove the ajax spinner
$('#' + id + ' img.waiting').hide();
},
/**
* Process the add menu item request response into menu list item.
*
* @param string menuMarkup The text server response of menu item markup.
* @param object req The request arguments.
*/
addMenuItemToBottom : function( menuMarkup, req ) {
$(menuMarkup).hideAdvancedMenuItemFields().appendTo( targetList );
},
addMenuItemToTop : function( menuMarkup, req ) {
$(menuMarkup).hideAdvancedMenuItemFields().prependTo( targetList );
},
/** /**
* Process the quick search response into a search result * Process the quick search response into a search result
* *

File diff suppressed because one or more lines are too long

View File

@ -393,7 +393,7 @@ function wp_default_scripts( &$scripts ) {
) ); ) );
// Custom Navigation // Custom Navigation
$scripts->add( 'nav-menu', "/wp-admin/js/nav-menu$suffix.js", false, '20100504' ); $scripts->add( 'nav-menu', "/wp-admin/js/nav-menu$suffix.js", false, '20100505' );
$scripts->localize( 'nav-menu', 'navMenuL10n', array( $scripts->localize( 'nav-menu', 'navMenuL10n', array(
'home' => _x('Home', 'nav menu home label'), 'home' => _x('Home', 'nav menu home label'),
'homeurl' => home_url('/'), 'homeurl' => home_url('/'),