Ticket #13220: 13220.adopt.wp.js.standard.1.2.patch
File 13220.adopt.wp.js.standard.1.2.patch, 10.3 KB (added by , 14 years ago) |
---|
-
wp-admin/js/nav-menu.dev.js
8 8 * @subpackage Administration 9 9 */ 10 10 11 var wpNavMenu, WPNavMenuHandler = function ($) { 12 var autoCompleteData = {}, 11 var wpNavMenu; 13 12 14 menuItemDepthPerLevel = 30, // Do not use directly. Use depthToPx and pxToDepth instead. 15 globalMaxDepth = 11, 13 (function($) { 14 15 var api = wpNavMenu = { 16 17 options : { 18 menuItemDepthPerLevel : 30, // Do not use directly. Use depthToPx and pxToDepth instead. 19 globalMaxDepth : 11 20 }, 21 22 menuList : undefined, // Set in init. 23 targetList : undefined, // Set in init. 24 25 autoCompleteData : {}, 16 26 17 formatAutocompleteResponse = function( resultRow, pos, total, queryTerm ) {18 if ( resultRow && resultRow[0] ) {19 var data = $.parseJSON(resultRow[0]);20 if ( data.post_title ) {21 if ( data.ID && data.post_type )22 autoCompleteData[data.post_title] = {ID: data.ID, object_type: data.post_type};23 return data.post_title;24 }25 }26 },27 28 formatAutocompleteResult = function( resultRow, pos, total, queryTerm ) {29 if ( resultRow && resultRow[0] ) {30 var data = $.parseJSON(resultRow[0]);31 if ( data.post_title )32 return data.post_title;33 }34 },35 36 getListDataFromID = function(menuItemID, parentEl) {37 if ( ! menuItemID )38 return false;39 parentEl = parentEl || document;40 var fields = [41 'menu-item-db-id',42 'menu-item-object-id',43 'menu-item-object',44 'menu-item-parent-id',45 'menu-item-position',46 'menu-item-type',47 'menu-item-append',48 'menu-item-title',49 'menu-item-url',50 'menu-item-description',51 'menu-item-attr-title',52 'menu-item-target',53 'menu-item-classes',54 'menu-item-xfn'55 ],56 itemData = {},57 inputs = parentEl.getElementsByTagName('input'),58 i = inputs.length,59 j;60 61 while ( i-- ) {62 j = fields.length;63 while ( j-- ) {64 if (65 inputs[i] &&66 inputs[i].name &&67 'menu-item[' + menuItemID + '][' + fields[j] + ']' == inputs[i].name68 ) {69 itemData[fields[j]] = inputs[i].value;70 }71 }72 }73 74 return itemData;75 },76 77 recalculateMenuItemPositions = function() {78 menuList.find('.menu-item-data-position').val( function(index) { return index + 1; } );79 },80 81 depthToPx = function(depth) {82 return depth * menuItemDepthPerLevel;83 },84 85 pxToDepth = function(px) {86 return Math.floor(px / menuItemDepthPerLevel);87 },88 89 menuList, targetList, api;90 91 return api = {92 93 27 // Functions that run on init. 94 28 init : function() { 95 menuList = $('#menu-to-edit');96 targetList =menuList;29 api.menuList = $('#menu-to-edit'); 30 api.targetList = api.menuList; 97 31 98 32 this.jQueryExtensions(); 99 33 … … 107 41 108 42 this.attachHomeLinkListener(); 109 43 110 if( menuList.length ) // If no menu, we're in the + tab.44 if( api.menuList.length ) // If no menu, we're in the + tab. 111 45 this.initSortables(); 112 46 113 47 this.initToggles(); … … 121 55 // jQuery extensions 122 56 $.fn.extend({ 123 57 menuItemDepth : function() { 124 return pxToDepth( this.eq(0).css('margin-left').slice(0, -2) );58 return api.pxToDepth( this.eq(0).css('margin-left').slice(0, -2) ); 125 59 }, 126 60 updateDepthClass : function(current, prev) { 127 61 return this.each(function(){ … … 199 133 $(checkboxes).each(function(){ 200 134 var listItemDBIDMatch = re.exec( $(this).attr('name') ), 201 135 listItemDBID = 'undefined' == typeof listItemDBIDMatch[1] ? 0 : parseInt(listItemDBIDMatch[1], 10); 202 menuItems[listItemDBID] = getListDataFromID(listItemDBID);136 menuItems[listItemDBID] = api.getListDataFromID(listItemDBID); 203 137 }); 204 138 // Add the items 205 139 api.addItemToMenu(menuItems, processMethod, function(){ … … 225 159 $('.field-' + field).addClass('hidden-field'); 226 160 } 227 161 // hide fields 228 menuList.hideAdvancedMenuItemFields();162 api.menuList.hideAdvancedMenuItemFields(); 229 163 }, 230 164 231 165 initSortables : function() { 232 166 var currentDepth = 0, originalDepth, minDepth, maxDepth, 233 menuLeft = menuList.offset().left,167 menuLeft = api.menuList.offset().left, 234 168 newItem, transport; 235 169 236 menuList.sortable({170 api.menuList.sortable({ 237 171 handle: '.menu-item-handle', 238 172 placeholder: 'sortable-placeholder', 239 173 start: function(e, ui) { … … 269 203 maxChildDepth = (depth > maxChildDepth) ? depth : maxChildDepth; 270 204 }); 271 205 width = ui.helper.find('.menu-item-handle').outerWidth(); // Get original width 272 width += depthToPx(maxChildDepth - originalDepth); // Account for children206 width += api.depthToPx(maxChildDepth - originalDepth); // Account for children 273 207 width -= 2; // Subtract 2 for borders 274 208 ui.placeholder.width(width); 275 209 }, … … 287 221 // Update the item data. 288 222 ui.item.updateParentMenuItemDBId(); 289 223 // Update positions 290 recalculateMenuItemPositions();224 api.recalculateMenuItemPositions(); 291 225 }, 292 226 change: function(e, ui) { 293 227 // Make sure the placeholder is inside the menu. 294 228 // Otherwise fix it, or we're in trouble. 295 229 if( ! ui.placeholder.parent().hasClass('menu') ) 296 ui.placeholder.appendTo( menuList);230 ui.placeholder.appendTo(api.menuList); 297 231 298 232 updateDepthRange(ui); 299 233 }, 300 234 sort: function(e, ui) { 301 var depth = pxToDepth(ui.helper.offset().left - menuLeft);235 var depth = api.pxToDepth(ui.helper.offset().left - menuLeft); 302 236 // Check and correct if depth is not within range. 303 237 if ( depth < minDepth ) depth = minDepth; 304 238 else if ( depth > maxDepth ) depth = maxDepth; 305 239 306 240 if( depth != currentDepth ) 307 241 updateCurrentDepth(ui, depth); 308 } ,242 } 309 243 }); 310 244 311 245 function updateDepthRange(ui) { … … 319 253 minDepth = (next.length) ? next.menuItemDepth() : 0; 320 254 321 255 if( prev.length ) 322 maxDepth = ( (depth = prev.menuItemDepth() + 1) > globalMaxDepth ) ?globalMaxDepth : depth;256 maxDepth = ( (depth = prev.menuItemDepth() + 1) > api.options.globalMaxDepth ) ? api.options.globalMaxDepth : depth; 323 257 else 324 258 maxDepth = 0; 325 259 } … … 492 426 * @param object req The request arguments. 493 427 */ 494 428 addMenuItemToBottom : function( menuMarkup, req ) { 495 $(menuMarkup).hideAdvancedMenuItemFields().appendTo( targetList );429 $(menuMarkup).hideAdvancedMenuItemFields().appendTo( api.targetList ); 496 430 }, 497 431 498 432 addMenuItemToTop : function( menuMarkup, req ) { 499 $(menuMarkup).hideAdvancedMenuItemFields().prependTo( targetList );433 $(menuMarkup).hideAdvancedMenuItemFields().prependTo( api.targetList ); 500 434 }, 501 435 502 436 attachHomeLinkListener : function() { 503 437 $('.add-home-link', '.customlinkdiv').click(function(e) { 504 api.addLinkToMenu( navMenuL10n.homeurl, navMenuL10n.home, api.addMenuItemToTop, recalculateMenuItemPositions );438 api.addLinkToMenu( navMenuL10n.homeurl, navMenuL10n.home, api.addMenuItemToTop, api.recalculateMenuItemPositions ); 505 439 return false; 506 440 }); 507 441 }, … … 666 600 $(el).autocomplete( ajaxurl + '?action=menu-quick-search&type=' + el.name, 667 601 { 668 602 delay: 500, 669 formatItem: formatAutocompleteResponse,670 formatResult: formatAutocompleteResult,603 formatItem: api.formatAutocompleteResponse, 604 formatResult: api.formatAutocompleteResult, 671 605 minchars: 2, 672 606 multiple: false 673 607 } 674 608 ).bind('blur', function(e) { 675 var changedData = a utoCompleteData[this.value],609 var changedData = api.autoCompleteData[this.value], 676 610 inputEl = this; 677 611 if ( changedData ) { 678 612 $.post( … … 680 614 changedData, 681 615 function(r) { 682 616 that.processQuickSearchQueryResponse.call(that, r, changedData); 683 a utoCompleteData[inputEl.value] = false;617 api.autoCompleteData[inputEl.value] = false; 684 618 } 685 619 ); 686 620 } … … 820 754 el.addClass('deleting').fadeOut( 350 , function() { 821 755 el.remove(); 822 756 children.shiftDepthClass(-1).updateParentMenuItemDBId(); 823 recalculateMenuItemPositions();757 api.recalculateMenuItemPositions(); 824 758 that.checkForEmptyMenu(); 825 759 }); 826 760 }, 827 761 828 762 checkForEmptyMenu : function() { 829 if( menuList.children().length ) return;830 menuList.height(80).one('sortstop', function(){763 if( api.menuList.children().length ) return; 764 api.menuList.height(80).one('sortstop', function(){ 831 765 $(this).height('auto'); 832 766 }); 767 }, 768 769 formatAutocompleteResponse : function( resultRow, pos, total, queryTerm ) { 770 if ( resultRow && resultRow[0] ) { 771 var data = $.parseJSON(resultRow[0]); 772 if ( data.post_title ) { 773 if ( data.ID && data.post_type ) 774 api.autoCompleteData[data.post_title] = {ID: data.ID, object_type: data.post_type}; 775 return data.post_title; 776 } 777 } 778 }, 779 780 formatAutocompleteResult : function( resultRow, pos, total, queryTerm ) { 781 if ( resultRow && resultRow[0] ) { 782 var data = $.parseJSON(resultRow[0]); 783 if ( data.post_title ) 784 return data.post_title; 785 } 786 }, 787 788 getListDataFromID : function(menuItemID, parentEl) { 789 if ( ! menuItemID ) 790 return false; 791 parentEl = parentEl || document; 792 var fields = [ 793 'menu-item-db-id', 794 'menu-item-object-id', 795 'menu-item-object', 796 'menu-item-parent-id', 797 'menu-item-position', 798 'menu-item-type', 799 'menu-item-append', 800 'menu-item-title', 801 'menu-item-url', 802 'menu-item-description', 803 'menu-item-attr-title', 804 'menu-item-target', 805 'menu-item-classes', 806 'menu-item-xfn' 807 ], 808 itemData = {}, 809 inputs = parentEl.getElementsByTagName('input'), 810 i = inputs.length, 811 j; 812 813 while ( i-- ) { 814 j = fields.length; 815 while ( j-- ) { 816 if ( 817 inputs[i] && 818 inputs[i].name && 819 'menu-item[' + menuItemID + '][' + fields[j] + ']' == inputs[i].name 820 ) { 821 itemData[fields[j]] = inputs[i].value; 822 } 823 } 824 } 825 826 return itemData; 827 }, 828 829 recalculateMenuItemPositions : function() { 830 api.menuList.find('.menu-item-data-position').val( function(index) { return index + 1; } ); 831 }, 832 833 depthToPx : function(depth) { 834 return depth * api.options.menuItemDepthPerLevel; 835 }, 836 837 pxToDepth : function(px) { 838 return Math.floor(px / api.options.menuItemDepthPerLevel); 833 839 } 834 }835 }836 840 837 wpNavMenu = new WPNavMenuHandler(jQuery);841 }; 838 842 839 jQuery(function() { 840 wpNavMenu.init(); 841 }) ;843 $(document).ready(function(){ wpNavMenu.init(); }); 844 845 })(jQuery);