mirror of
https://github.com/WordPress/WordPress.git
synced 2025-02-08 16:41:44 +01:00
Adopt WP's general JS structure. Remove last of trailing commas. Remove dead code. props koopersmith, see #13220.
git-svn-id: http://svn.automattic.com/wordpress/trunk@14475 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
494290312a
commit
0a71b5dc8a
@ -8,92 +8,26 @@
|
|||||||
* @subpackage Administration
|
* @subpackage Administration
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var wpNavMenu, WPNavMenuHandler = function ($) {
|
var wpNavMenu;
|
||||||
var autoCompleteData = {},
|
|
||||||
|
|
||||||
menuItemDepthPerLevel = 30, // Do not use directly. Use depthToPx and pxToDepth instead.
|
(function($) {
|
||||||
globalMaxDepth = 11,
|
|
||||||
|
|
||||||
formatAutocompleteResponse = function( resultRow, pos, total, queryTerm ) {
|
var api = wpNavMenu = {
|
||||||
if ( resultRow && resultRow[0] ) {
|
|
||||||
var data = $.parseJSON(resultRow[0]);
|
|
||||||
if ( data.post_title ) {
|
|
||||||
if ( data.ID && data.post_type )
|
|
||||||
autoCompleteData[data.post_title] = {ID: data.ID, object_type: data.post_type};
|
|
||||||
return data.post_title;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
formatAutocompleteResult = function( resultRow, pos, total, queryTerm ) {
|
options : {
|
||||||
if ( resultRow && resultRow[0] ) {
|
menuItemDepthPerLevel : 30, // Do not use directly. Use depthToPx and pxToDepth instead.
|
||||||
var data = $.parseJSON(resultRow[0]);
|
globalMaxDepth : 11
|
||||||
if ( data.post_title )
|
},
|
||||||
return data.post_title;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
getListDataFromID = function(menuItemID, parentEl) {
|
menuList : undefined, // Set in init.
|
||||||
if ( ! menuItemID )
|
targetList : undefined, // Set in init.
|
||||||
return false;
|
|
||||||
parentEl = parentEl || document;
|
|
||||||
var fields = [
|
|
||||||
'menu-item-db-id',
|
|
||||||
'menu-item-object-id',
|
|
||||||
'menu-item-object',
|
|
||||||
'menu-item-parent-id',
|
|
||||||
'menu-item-position',
|
|
||||||
'menu-item-type',
|
|
||||||
'menu-item-append',
|
|
||||||
'menu-item-title',
|
|
||||||
'menu-item-url',
|
|
||||||
'menu-item-description',
|
|
||||||
'menu-item-attr-title',
|
|
||||||
'menu-item-target',
|
|
||||||
'menu-item-classes',
|
|
||||||
'menu-item-xfn'
|
|
||||||
],
|
|
||||||
itemData = {},
|
|
||||||
inputs = parentEl.getElementsByTagName('input'),
|
|
||||||
i = inputs.length,
|
|
||||||
j;
|
|
||||||
|
|
||||||
while ( i-- ) {
|
autoCompleteData : {},
|
||||||
j = fields.length;
|
|
||||||
while ( j-- ) {
|
|
||||||
if (
|
|
||||||
inputs[i] &&
|
|
||||||
inputs[i].name &&
|
|
||||||
'menu-item[' + menuItemID + '][' + fields[j] + ']' == inputs[i].name
|
|
||||||
) {
|
|
||||||
itemData[fields[j]] = inputs[i].value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return itemData;
|
|
||||||
},
|
|
||||||
|
|
||||||
recalculateMenuItemPositions = function() {
|
|
||||||
menuList.find('.menu-item-data-position').val( function(index) { return index + 1; } );
|
|
||||||
},
|
|
||||||
|
|
||||||
depthToPx = function(depth) {
|
|
||||||
return depth * menuItemDepthPerLevel;
|
|
||||||
},
|
|
||||||
|
|
||||||
pxToDepth = function(px) {
|
|
||||||
return Math.floor(px / menuItemDepthPerLevel);
|
|
||||||
},
|
|
||||||
|
|
||||||
menuList, targetList, api;
|
|
||||||
|
|
||||||
return api = {
|
|
||||||
|
|
||||||
// Functions that run on init.
|
// Functions that run on init.
|
||||||
init : function() {
|
init : function() {
|
||||||
menuList = $('#menu-to-edit');
|
api.menuList = $('#menu-to-edit');
|
||||||
targetList = menuList;
|
api.targetList = api.menuList;
|
||||||
|
|
||||||
this.jQueryExtensions();
|
this.jQueryExtensions();
|
||||||
|
|
||||||
@ -107,7 +41,7 @@ var wpNavMenu, WPNavMenuHandler = function ($) {
|
|||||||
|
|
||||||
this.attachHomeLinkListener();
|
this.attachHomeLinkListener();
|
||||||
|
|
||||||
if( menuList.length ) // If no menu, we're in the + tab.
|
if( api.menuList.length ) // If no menu, we're in the + tab.
|
||||||
this.initSortables();
|
this.initSortables();
|
||||||
|
|
||||||
this.initToggles();
|
this.initToggles();
|
||||||
@ -121,7 +55,7 @@ var wpNavMenu, WPNavMenuHandler = function ($) {
|
|||||||
// jQuery extensions
|
// jQuery extensions
|
||||||
$.fn.extend({
|
$.fn.extend({
|
||||||
menuItemDepth : function() {
|
menuItemDepth : function() {
|
||||||
return pxToDepth( this.eq(0).css('margin-left').slice(0, -2) );
|
return api.pxToDepth( this.eq(0).css('margin-left').slice(0, -2) );
|
||||||
},
|
},
|
||||||
updateDepthClass : function(current, prev) {
|
updateDepthClass : function(current, prev) {
|
||||||
return this.each(function(){
|
return this.each(function(){
|
||||||
@ -199,7 +133,7 @@ var wpNavMenu, WPNavMenuHandler = function ($) {
|
|||||||
$(checkboxes).each(function(){
|
$(checkboxes).each(function(){
|
||||||
var listItemDBIDMatch = re.exec( $(this).attr('name') ),
|
var listItemDBIDMatch = re.exec( $(this).attr('name') ),
|
||||||
listItemDBID = 'undefined' == typeof listItemDBIDMatch[1] ? 0 : parseInt(listItemDBIDMatch[1], 10);
|
listItemDBID = 'undefined' == typeof listItemDBIDMatch[1] ? 0 : parseInt(listItemDBIDMatch[1], 10);
|
||||||
menuItems[listItemDBID] = getListDataFromID(listItemDBID);
|
menuItems[listItemDBID] = api.getListDataFromID(listItemDBID);
|
||||||
});
|
});
|
||||||
// Add the items
|
// Add the items
|
||||||
api.addItemToMenu(menuItems, processMethod, function(){
|
api.addItemToMenu(menuItems, processMethod, function(){
|
||||||
@ -225,15 +159,15 @@ var wpNavMenu, WPNavMenuHandler = function ($) {
|
|||||||
$('.field-' + field).addClass('hidden-field');
|
$('.field-' + field).addClass('hidden-field');
|
||||||
}
|
}
|
||||||
// hide fields
|
// hide fields
|
||||||
menuList.hideAdvancedMenuItemFields();
|
api.menuList.hideAdvancedMenuItemFields();
|
||||||
},
|
},
|
||||||
|
|
||||||
initSortables : function() {
|
initSortables : function() {
|
||||||
var currentDepth = 0, originalDepth, minDepth, maxDepth,
|
var currentDepth = 0, originalDepth, minDepth, maxDepth,
|
||||||
menuLeft = menuList.offset().left,
|
menuLeft = api.menuList.offset().left,
|
||||||
newItem, transport;
|
newItem, transport;
|
||||||
|
|
||||||
menuList.sortable({
|
api.menuList.sortable({
|
||||||
handle: '.menu-item-handle',
|
handle: '.menu-item-handle',
|
||||||
placeholder: 'sortable-placeholder',
|
placeholder: 'sortable-placeholder',
|
||||||
start: function(e, ui) {
|
start: function(e, ui) {
|
||||||
@ -269,7 +203,7 @@ var wpNavMenu, WPNavMenuHandler = function ($) {
|
|||||||
maxChildDepth = (depth > maxChildDepth) ? depth : maxChildDepth;
|
maxChildDepth = (depth > maxChildDepth) ? depth : maxChildDepth;
|
||||||
});
|
});
|
||||||
width = ui.helper.find('.menu-item-handle').outerWidth(); // Get original width
|
width = ui.helper.find('.menu-item-handle').outerWidth(); // Get original width
|
||||||
width += depthToPx(maxChildDepth - originalDepth); // Account for children
|
width += api.depthToPx(maxChildDepth - originalDepth); // Account for children
|
||||||
width -= 2; // Subtract 2 for borders
|
width -= 2; // Subtract 2 for borders
|
||||||
ui.placeholder.width(width);
|
ui.placeholder.width(width);
|
||||||
},
|
},
|
||||||
@ -287,25 +221,25 @@ var wpNavMenu, WPNavMenuHandler = function ($) {
|
|||||||
// Update the item data.
|
// Update the item data.
|
||||||
ui.item.updateParentMenuItemDBId();
|
ui.item.updateParentMenuItemDBId();
|
||||||
// Update positions
|
// Update positions
|
||||||
recalculateMenuItemPositions();
|
api.recalculateMenuItemPositions();
|
||||||
},
|
},
|
||||||
change: function(e, ui) {
|
change: function(e, ui) {
|
||||||
// Make sure the placeholder is inside the menu.
|
// Make sure the placeholder is inside the menu.
|
||||||
// Otherwise fix it, or we're in trouble.
|
// Otherwise fix it, or we're in trouble.
|
||||||
if( ! ui.placeholder.parent().hasClass('menu') )
|
if( ! ui.placeholder.parent().hasClass('menu') )
|
||||||
ui.placeholder.appendTo(menuList);
|
ui.placeholder.appendTo(api.menuList);
|
||||||
|
|
||||||
updateDepthRange(ui);
|
updateDepthRange(ui);
|
||||||
},
|
},
|
||||||
sort: function(e, ui) {
|
sort: function(e, ui) {
|
||||||
var depth = pxToDepth(ui.helper.offset().left - menuLeft);
|
var depth = api.pxToDepth(ui.helper.offset().left - menuLeft);
|
||||||
// Check and correct if depth is not within range.
|
// Check and correct if depth is not within range.
|
||||||
if ( depth < minDepth ) depth = minDepth;
|
if ( depth < minDepth ) depth = minDepth;
|
||||||
else if ( depth > maxDepth ) depth = maxDepth;
|
else if ( depth > maxDepth ) depth = maxDepth;
|
||||||
|
|
||||||
if( depth != currentDepth )
|
if( depth != currentDepth )
|
||||||
updateCurrentDepth(ui, depth);
|
updateCurrentDepth(ui, depth);
|
||||||
},
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function updateDepthRange(ui) {
|
function updateDepthRange(ui) {
|
||||||
@ -319,7 +253,7 @@ var wpNavMenu, WPNavMenuHandler = function ($) {
|
|||||||
minDepth = (next.length) ? next.menuItemDepth() : 0;
|
minDepth = (next.length) ? next.menuItemDepth() : 0;
|
||||||
|
|
||||||
if( prev.length )
|
if( prev.length )
|
||||||
maxDepth = ( (depth = prev.menuItemDepth() + 1) > globalMaxDepth ) ? globalMaxDepth : depth;
|
maxDepth = ( (depth = prev.menuItemDepth() + 1) > api.options.globalMaxDepth ) ? api.options.globalMaxDepth : depth;
|
||||||
else
|
else
|
||||||
maxDepth = 0;
|
maxDepth = 0;
|
||||||
}
|
}
|
||||||
@ -492,16 +426,16 @@ var wpNavMenu, WPNavMenuHandler = function ($) {
|
|||||||
* @param object req The request arguments.
|
* @param object req The request arguments.
|
||||||
*/
|
*/
|
||||||
addMenuItemToBottom : function( menuMarkup, req ) {
|
addMenuItemToBottom : function( menuMarkup, req ) {
|
||||||
$(menuMarkup).hideAdvancedMenuItemFields().appendTo( targetList );
|
$(menuMarkup).hideAdvancedMenuItemFields().appendTo( api.targetList );
|
||||||
},
|
},
|
||||||
|
|
||||||
addMenuItemToTop : function( menuMarkup, req ) {
|
addMenuItemToTop : function( menuMarkup, req ) {
|
||||||
$(menuMarkup).hideAdvancedMenuItemFields().prependTo( targetList );
|
$(menuMarkup).hideAdvancedMenuItemFields().prependTo( api.targetList );
|
||||||
},
|
},
|
||||||
|
|
||||||
attachHomeLinkListener : function() {
|
attachHomeLinkListener : function() {
|
||||||
$('.add-home-link', '.customlinkdiv').click(function(e) {
|
$('.add-home-link', '.customlinkdiv').click(function(e) {
|
||||||
api.addLinkToMenu( navMenuL10n.homeurl, navMenuL10n.home, api.addMenuItemToTop, recalculateMenuItemPositions );
|
api.addLinkToMenu( navMenuL10n.homeurl, navMenuL10n.home, api.addMenuItemToTop, api.recalculateMenuItemPositions );
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@ -666,13 +600,13 @@ var wpNavMenu, WPNavMenuHandler = function ($) {
|
|||||||
$(el).autocomplete( ajaxurl + '?action=menu-quick-search&type=' + el.name,
|
$(el).autocomplete( ajaxurl + '?action=menu-quick-search&type=' + el.name,
|
||||||
{
|
{
|
||||||
delay: 500,
|
delay: 500,
|
||||||
formatItem: formatAutocompleteResponse,
|
formatItem: api.formatAutocompleteResponse,
|
||||||
formatResult: formatAutocompleteResult,
|
formatResult: api.formatAutocompleteResult,
|
||||||
minchars: 2,
|
minchars: 2,
|
||||||
multiple: false
|
multiple: false
|
||||||
}
|
}
|
||||||
).bind('blur', function(e) {
|
).bind('blur', function(e) {
|
||||||
var changedData = autoCompleteData[this.value],
|
var changedData = api.autoCompleteData[this.value],
|
||||||
inputEl = this;
|
inputEl = this;
|
||||||
if ( changedData ) {
|
if ( changedData ) {
|
||||||
$.post(
|
$.post(
|
||||||
@ -680,7 +614,7 @@ var wpNavMenu, WPNavMenuHandler = function ($) {
|
|||||||
changedData,
|
changedData,
|
||||||
function(r) {
|
function(r) {
|
||||||
that.processQuickSearchQueryResponse.call(that, r, changedData);
|
that.processQuickSearchQueryResponse.call(that, r, changedData);
|
||||||
autoCompleteData[inputEl.value] = false;
|
api.autoCompleteData[inputEl.value] = false;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -820,22 +754,92 @@ var wpNavMenu, WPNavMenuHandler = function ($) {
|
|||||||
el.addClass('deleting').fadeOut( 350 , function() {
|
el.addClass('deleting').fadeOut( 350 , function() {
|
||||||
el.remove();
|
el.remove();
|
||||||
children.shiftDepthClass(-1).updateParentMenuItemDBId();
|
children.shiftDepthClass(-1).updateParentMenuItemDBId();
|
||||||
recalculateMenuItemPositions();
|
api.recalculateMenuItemPositions();
|
||||||
that.checkForEmptyMenu();
|
that.checkForEmptyMenu();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
checkForEmptyMenu : function() {
|
checkForEmptyMenu : function() {
|
||||||
if( menuList.children().length ) return;
|
if( api.menuList.children().length ) return;
|
||||||
menuList.height(80).one('sortstop', function(){
|
api.menuList.height(80).one('sortstop', function(){
|
||||||
$(this).height('auto');
|
$(this).height('auto');
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
formatAutocompleteResponse : function( resultRow, pos, total, queryTerm ) {
|
||||||
|
if ( resultRow && resultRow[0] ) {
|
||||||
|
var data = $.parseJSON(resultRow[0]);
|
||||||
|
if ( data.post_title ) {
|
||||||
|
if ( data.ID && data.post_type )
|
||||||
|
api.autoCompleteData[data.post_title] = {ID: data.ID, object_type: data.post_type};
|
||||||
|
return data.post_title;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
formatAutocompleteResult : function( resultRow, pos, total, queryTerm ) {
|
||||||
|
if ( resultRow && resultRow[0] ) {
|
||||||
|
var data = $.parseJSON(resultRow[0]);
|
||||||
|
if ( data.post_title )
|
||||||
|
return data.post_title;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
getListDataFromID : function(menuItemID, parentEl) {
|
||||||
|
if ( ! menuItemID )
|
||||||
|
return false;
|
||||||
|
parentEl = parentEl || document;
|
||||||
|
var fields = [
|
||||||
|
'menu-item-db-id',
|
||||||
|
'menu-item-object-id',
|
||||||
|
'menu-item-object',
|
||||||
|
'menu-item-parent-id',
|
||||||
|
'menu-item-position',
|
||||||
|
'menu-item-type',
|
||||||
|
'menu-item-append',
|
||||||
|
'menu-item-title',
|
||||||
|
'menu-item-url',
|
||||||
|
'menu-item-description',
|
||||||
|
'menu-item-attr-title',
|
||||||
|
'menu-item-target',
|
||||||
|
'menu-item-classes',
|
||||||
|
'menu-item-xfn'
|
||||||
|
],
|
||||||
|
itemData = {},
|
||||||
|
inputs = parentEl.getElementsByTagName('input'),
|
||||||
|
i = inputs.length,
|
||||||
|
j;
|
||||||
|
|
||||||
|
while ( i-- ) {
|
||||||
|
j = fields.length;
|
||||||
|
while ( j-- ) {
|
||||||
|
if (
|
||||||
|
inputs[i] &&
|
||||||
|
inputs[i].name &&
|
||||||
|
'menu-item[' + menuItemID + '][' + fields[j] + ']' == inputs[i].name
|
||||||
|
) {
|
||||||
|
itemData[fields[j]] = inputs[i].value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return itemData;
|
||||||
|
},
|
||||||
|
|
||||||
|
recalculateMenuItemPositions : function() {
|
||||||
|
api.menuList.find('.menu-item-data-position').val( function(index) { return index + 1; } );
|
||||||
|
},
|
||||||
|
|
||||||
|
depthToPx : function(depth) {
|
||||||
|
return depth * api.options.menuItemDepthPerLevel;
|
||||||
|
},
|
||||||
|
|
||||||
|
pxToDepth : function(px) {
|
||||||
|
return Math.floor(px / api.options.menuItemDepthPerLevel);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
wpNavMenu = new WPNavMenuHandler(jQuery);
|
};
|
||||||
|
|
||||||
jQuery(function() {
|
$(document).ready(function(){ wpNavMenu.init(); });
|
||||||
wpNavMenu.init();
|
|
||||||
});
|
})(jQuery);
|
||||||
|
File diff suppressed because one or more lines are too long
@ -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, '20100506' );
|
$scripts->add( 'nav-menu', "/wp-admin/js/nav-menu$suffix.js", false, '20100506b' );
|
||||||
$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('/'),
|
||||||
|
Loading…
Reference in New Issue
Block a user