diff --git a/wp-admin/js/nav-menu.dev.js b/wp-admin/js/nav-menu.dev.js index c21d29d1f1..5d7af1e47a 100644 --- a/wp-admin/js/nav-menu.dev.js +++ b/wp-admin/js/nav-menu.dev.js @@ -87,7 +87,7 @@ var WPNavMenuHandler = function ($) { return Math.floor(px / menuItemDepthPerLevel); }, - menuList, targetList; + menuList, targetList, api; // jQuery extensions $.fn.extend({ @@ -166,10 +166,50 @@ var WPNavMenuHandler = function ($) { else 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(); + }); + }); + + // Remove the ajax spinner + t.find('img.waiting').hide(); + }); + }, }); - return { + return api = { // Functions that run on init. init : function() { @@ -177,8 +217,10 @@ var WPNavMenuHandler = function ($) { targetList = menuList; this.attachMenuEditListeners(); - - this.attachMenuMetaListeners(document.getElementById('nav-menu-meta')); + + this.setupInputWithDefaultTitle(); + this.attachAddMenuItemListeners(); + this.attachQuickSearchListeners(); this.attachTabsPanelListeners(); @@ -422,38 +464,39 @@ var WPNavMenuHandler = function ($) { }).blur( function(){ var $t = $(this); if( '' == $t.val() ) - $t.val( $t.data(name) ).addClass( name ); + $t.addClass( name ).val( $t.data(name) ); }); }, - attachMenuMetaListeners : function(formEL) { - if ( ! formEL ) - return; - - var that = this; - this.setupInputWithDefaultTitle(); - + attachAddMenuItemListeners : function() { + var form = $('#nav-menu-meta'); + + form.find('.add-to-menu input').click(function(){ + $(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 $('input.quick-search').each(function(i, el) { that.setupQuickSearchEventListeners(el); }); - - // If a "Add to Menu" button was clicked, submit that metabox ajax style. - $(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; - } else if ( $(e.target).is('input') && $(e.target).hasClass('quick-search-submit') ) { - that.quickSearch( $(divcontainer).attr('id') ); - return false; - }; + form.find('.quick-search-submit').click(function() { + $(this).trigger('wp-quick-search'); + return false; + }); + form.find('.inside').children().bind('wp-quick-search', function() { + that.quickSearch( $(this).attr('id') ); }); }, @@ -482,57 +525,76 @@ var WPNavMenuHandler = function ($) { }); }, - addCustomLink : function(url, label, addToTop) { - var url = url || $('#custom-menu-item-url').val(), - label = label || $('#custom-menu-item-name').val(), - addToTop = addToTop || false, - menu = $('#menu').val(), - nonce = $('#menu-settings-column-nonce').val(), - params = {}, - that = this, - processMethod = function(){}; + addCustomLink : function( processMethod ) { + var url = $('#custom-menu-item-url').val(), + label = $('#custom-menu-item-name').val(); + + processMethod = processMethod || api.addMenuItemToBottom; if ( '' == url || 'http://' == url ) return false; // Show the ajax spinner $('.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://'); + }); + }, + + addLinkToMenu : function(url, label, processMethod, callback) { + processMethod = processMethod || api.addMenuItemToBottom; + callback = callback || function(){}; + + api.addItemToMenu({ + '-1': { + 'menu-item-type': 'custom', + 'menu-item-url': url, + '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': { - '-1': { - 'menu-item-type': 'custom', - 'menu-item-url': url, - 'menu-item-title': label - } - } + 'menu-item': menuItem, }; - processMethod = addToTop ? that.addMenuItemToTop : that.addMenuItemToBottom; - $.post( ajaxurl, params, function(menuMarkup) { - processMethod.call(that, menuMarkup, params); - - // Remove the ajax spinner - $('.customlinkdiv img.waiting').hide(); - - // Reset the form - wpNavMenu.resetCustomLinkForm(); + processMethod(menuMarkup, params); + callback(); }); }, + + /** + * 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 ); + }, - resetCustomLinkForm : function() { - // set custom link form back to defaults - $('#custom-menu-item-name').val('').blur(); - $('#custom-menu-item-url').val('http://'); + addMenuItemToTop : function( menuMarkup, req ) { + $(menuMarkup).hideAdvancedMenuItemFields().prependTo( targetList ); }, attachHomeLinkListener : function() { $('.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; }); }, @@ -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 * diff --git a/wp-admin/js/nav-menu.js b/wp-admin/js/nav-menu.js index df721c07d4..3f09825af1 100644 --- a/wp-admin/js/nav-menu.js +++ b/wp-admin/js/nav-menu.js @@ -1 +1 @@ -var WPNavMenuHandler=function(d){var h={},f=30,c=11,k=function(m,q,n,p){if(m&&m[0]){var o=d.parseJSON(m[0]);if(o.post_title){if(o.ID&&o.post_type){h[o.post_title]={ID:o.ID,object_type:o.post_type}}return o.post_title}}},l=function(m,q,n,p){if(m&&m[0]){var o=d.parseJSON(m[0]);if(o.post_title){return o.post_title}}},b=function(s,r){if(!s){return false}r=r||document;var n=["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"],m={},o=r.getElementsByTagName("input"),q=o.length,p,t=document.getElementById("nav-menu-meta-object-id").value;while(q--){p=n.length;while(p--){if(o[q]&&o[q].name&&"menu-item["+s+"]["+n[p]+"]"==o[q].name){m[n[p]]=o[q].value}}}return m},a=function(){g.find(".menu-item-data-position").val(function(m){return m+1})},e=function(m){return m*f},i=function(m){return Math.floor(m/f)},g,j;d.fn.extend({menuItemDepth:function(){return i(this.eq(0).css("margin-left").slice(0,-2))},updateDepthClass:function(n,m){return this.each(function(){var o=d(this);m=m||o.menuItemDepth();d(this).removeClass("menu-item-depth-"+m).addClass("menu-item-depth-"+n)})},shiftDepthClass:function(m){return this.each(function(){var n=d(this),o=n.menuItemDepth();d(this).removeClass("menu-item-depth-"+o).addClass("menu-item-depth-"+(o+m))})},childMenuItems:function(){var m=d();this.each(function(){var n=d(this),p=n.menuItemDepth(),o=n.next();while(o.length&&o.menuItemDepth()>p){m=m.add(o);o=o.next()}});return m},updateParentMenuItemDBId:function(){return this.each(function(){var o=d(this),m=o.find(".menu-item-data-parent-id"),p=o.menuItemDepth(),n=o.prev();if(p==0){m.val(0)}else{while(n.menuItemDepth()!=p-1){n=n.prev()}m.val(n.find(".menu-item-data-db-id").val())}})},hideAdvancedMenuItemFields:function(){return this.each(function(){var m=d(this);d(".hide-column-tog").not(":checked").each(function(){m.find(".field-"+d(this).val()).addClass("hidden-field")})})},selectItem:function(){return this.each(function(){d(this).addClass("selected-menu-item").next().children("input").attr("checked","checked")})},deselectItem:function(){return this.each(function(){d(this).removeClass("selected-menu-item").next().children("input").removeAttr("checked")})},toggleItem:function(){return this.each(function(){var m=d(this);if(m.hasClass("selected-menu-item")){m.deselectItem()}else{m.selectItem()}})}});return{init:function(){g=d("#menu-to-edit");j=g;this.attachMenuEditListeners();this.attachMenuMetaListeners(document.getElementById("nav-menu-meta"));this.attachTabsPanelListeners();this.attachHomeLinkListener();if(g.length){this.initSortables()}this.initToggles();this.initTabManager();this.initAddMenuItemDraggables();this.checkForEmptyMenu()},initToggles:function(){postboxes.add_postbox_toggles("nav-menus");columns.useCheckboxesForHidden();columns.checked=function(m){d(".field-"+m).removeClass("hidden-field")};columns.unchecked=function(m){d(".field-"+m).addClass("hidden-field")};g.hideAdvancedMenuItemFields()},initSortables:function(){var s=0,r,q,m,p=g.offset().left,t,o;g.sortable({handle:".menu-item-handle",placeholder:"sortable-placeholder",start:function(C,B){var A,v,z,y,w,x;o=B.item.children(".menu-item-transport");t=(B.helper.hasClass("new-menu-item"));r=(t)?0:B.item.menuItemDepth();n(B,r);if(!t){y=(B.item.next()[0]==B.placeholder[0])?B.item.next():B.item;w=y.childMenuItems();o.append(w)}u(B);v=o.outerHeight();v+=(v>0)?(B.placeholder.css("margin-top").slice(0,-2)*1):0;v+=B.helper.outerHeight();v-=2;B.placeholder.height(v);x=r;if(!t){w.each(function(){var D=d(this).menuItemDepth();x=(D>x)?D:x})}z=B.helper.find(".menu-item-handle").outerWidth();z+=e(x-r);z-=2;B.placeholder.width(z)},stop:function(y,x){var w,v=s-r;w=o.children().insertAfter(x.item);if(t){x.item.remove();if(v!=0){w.shiftDepthClass(v)}w.updateParentMenuItemDBId()}else{if(v!=0){x.item.updateDepthClass(s);w.shiftDepthClass(v)}x.item.updateParentMenuItemDBId()}a()},change:function(w,v){if(!v.placeholder.parent().hasClass("menu")){v.placeholder.appendTo(g)}u(v)},sort:function(w,v){var x=i(v.helper.offset().left-p);if(xm){x=m}}if(x!=s){n(v,x)}},receive:function(w,v){o=v.sender.children(".menu-item-transport")}});function u(x){var w=x.placeholder.prev(),v=x.placeholder.next(),y;if(w[0]==x.item[0]){w=w.prev()}if(v[0]==x.item[0]){v=v.next()}q=(v.length)?v.menuItemDepth():0;if(w.length){m=((y=w.menuItemDepth()+1)>c)?c:y}else{m=0}}function n(v,w){v.placeholder.updateDepthClass(w,s);s=w}},initAddMenuItemDraggables:function(){var m=d(".potential-menu-item");m.click(function(n){d(this).toggleItem()}).children().draggable({helper:"clone",connectToSortable:"ul#menu-to-edit",distance:5,zIndex:100,start:function(s,q){var r=d(s.target),p=r.parent(),n=p.parent(),o;p.selectItem();j=r.children(".menu-item-transport");o=n.parents(".tabs-panel").find(".selected-menu-item").children().not(q.helper).clone();q.helper.children(".additional-menu-items").append(o);q.helper.addClass("new-menu-item");q.helper.children("div").hide();o.first().css("margin-top",0);o.children("div").addClass("menu-item-handle");q.helper.children("div").addClass("hidden-handle");n.parents(".inside").find(".add-to-menu input").click();q.helper.width(q.helper.width());q.helper.height(q.helper.height())},stop:function(o,n){j=g;d(o.target).parents(".tabs-panel").find(".selected-menu-item").deselectItem()}})},attachMenuEditListeners:function(){var m=this;d("#update-nav-menu").bind("click",function(n){if(n.target&&n.target.className){if(-1!=n.target.className.indexOf("item-edit")){return m.eventOnClickEditLink(n.target)}else{if(-1!=n.target.className.indexOf("menu-delete")){return m.eventOnClickMenuDelete(n.target)}else{if(-1!=n.target.className.indexOf("item-delete")){return m.eventOnClickMenuItemDelete(n.target)}else{if(-1!=n.target.className.indexOf("item-close")){return m.eventOnClickCloseLink(n.target)}}}}}})},setupInputWithDefaultTitle:function(){var m="input-with-default-title";d("."+m).each(function(){var p=d(this),o=p.attr("title"),n=p.val();p.data(m,o);if(""==n){p.val(o)}else{if(o==n){return}else{p.removeClass(m)}}}).focus(function(){var n=d(this);if(n.val()==n.data(m)){n.val("").removeClass(m)}}).blur(function(){var n=d(this);if(""==n.val()){n.val(n.data(m)).addClass(m)}})},attachMenuMetaListeners:function(m){if(!m){return}var n=this;this.setupInputWithDefaultTitle();d("input.quick-search").each(function(o,p){n.setupQuickSearchEventListeners(p)});d(m).click(function(p){var o=d(p.target).parent().parent().parent();if(d(p.target).is("input")&&d(p.target).hasClass("button-secondary")&&!d(p.target).hasClass("quick-search-submit")){if(d(o).hasClass("customlinkdiv")){n.addCustomLink()}else{if(d(o).hasClass("posttypediv")||d(o).hasClass("taxonomydiv")){n.addItemsToMenu(d(o).attr("id"))}}return false}else{if(d(p.target).is("input")&&d(p.target).hasClass("quick-search-submit")){n.quickSearch(d(o).attr("id"));return false}}})},quickSearch:function(u){var o=d("#"+u+" .quick-search").attr("name"),r=d("#"+u+" .quick-search").val(),t=d("#menu").val(),n=d("#menu-settings-column-nonce").val(),s={},p=this,m=function(){};m=p.processQuickSearchQueryResponse;s={action:"menu-quick-search","response-format":"markup",menu:t,"menu-settings-column-nonce":n,q:r,type:o};d.post(ajaxurl,s,function(q){m.call(p,q,s)})},addCustomLink:function(o,n,p){var o=o||d("#custom-menu-item-url").val(),n=n||d("#custom-menu-item-name").val(),p=p||false,t=d("#menu").val(),q=d("#menu-settings-column-nonce").val(),s={},r=this,m=function(){};if(""==o||"http://"==o){return false}d(".customlinkdiv img.waiting").show();s={action:"add-menu-item",menu:t,"menu-settings-column-nonce":q,"menu-item":{"-1":{"menu-item-type":"custom","menu-item-url":o,"menu-item-title":n}}};m=p?r.addMenuItemToTop:r.addMenuItemToBottom;d.post(ajaxurl,s,function(u){m.call(r,u,s);d(".customlinkdiv img.waiting").hide();wpNavMenu.resetCustomLinkForm()})},resetCustomLinkForm:function(){d("#custom-menu-item-name").val("").blur();d("#custom-menu-item-url").val("http://")},attachHomeLinkListener:function(){d(".add-home-link",".customlinkdiv").click(function(m){wpNavMenu.addCustomLink(navMenuL10n.homeurl,navMenuL10n.home,true);return false})},attachTabsPanelListeners:function(){d("#menu-settings-column").bind("click",function(r){if(r.target&&r.target.className&&-1!=r.target.className.indexOf("nav-tab-link")){var o,u=/#(.*)$/.exec(r.target.href),t,m=d(r.target).parents(".inside").first()[0],q=m?m.getElementsByTagName("input"):[],p=q.length;while(p--){q[p].checked=false}d(".tabs-panel",m).each(function(){if(this.className){this.className=this.className.replace("tabs-panel-active","tabs-panel-inactive")}});d(".tabs",m).each(function(){this.className=this.className.replace("tabs","")});r.target.parentNode.className+=" tabs";if(u&&u[1]){o=document.getElementById(u[1]);if(o){o.className=o.className.replace("tabs-panel-inactive","tabs-panel-active")}}return false}else{if(r.target&&r.target.className&&-1!=r.target.className.indexOf("select-all")){var n=/#(.*)$/.exec(r.target.href),s;if(n&&n[1]){s=d("#"+n[1]+" .tabs-panel-active .potential-menu-item");if(s.length===s.filter(".selected-menu-item").length){s.deselectItem()}else{s.selectItem()}return false}}}})},initTabManager:function(){var r=d(".nav-tabs-wrapper"),s=r.children(".nav-tabs"),q=s.children(".nav-tab-active"),u=s.children(".nav-tab"),o=0,v,p,t,n;resizing=false;function m(){p=r.offset().left;v=p+r.width();q.makeTabVisible()}d.fn.extend({makeTabVisible:function(){var x=this.eq(0),y,w;if(!x.length){return}y=x.offset().left;w=y+x.outerWidth();if(w>v){s.animate({"margin-left":"+="+(v-w)+"px",},"fast")}else{if(y=p)?true:false}});u.each(function(){o+=d(this).outerWidth(true)});if(o<=r.width()-s.css("padding-left").slice(0,-2)-s.css("padding-right").slice(0,-2)){return}s.css({"margin-right":(-1*o)+"px",padding:0,});t=d('');n=d('');r.wrap('