Wordpress/wp-admin/js/nav-menu.js
2010-05-19 09:56:02 +00:00

1 line
16 KiB
JavaScript

var wpNavMenu;(function($){var api=wpNavMenu={options:{menuItemDepthPerLevel:30,globalMaxDepth:11},menuList:undefined,targetList:undefined,menusChanged:false,init:function(){api.menuList=$("#menu-to-edit");api.targetList=api.menuList;this.jQueryExtensions();this.attachMenuEditListeners();this.setupInputWithDefaultTitle();this.attachAddMenuItemListeners();this.attachQuickSearchListeners();this.attachThemeLocationsListeners();this.attachTabsPanelListeners();this.attachHomeLinkListener();this.attachUnsavedChangesListener();if(api.menuList.length){this.initSortables()}this.initToggles();this.initTabManager()},jQueryExtensions:function(){$.fn.extend({menuItemDepth:function(){return api.pxToDepth(this.eq(0).css("margin-left").slice(0,-2))},updateDepthClass:function(current,prev){return this.each(function(){var t=$(this);prev=prev||t.menuItemDepth();$(this).removeClass("menu-item-depth-"+prev).addClass("menu-item-depth-"+current)})},shiftDepthClass:function(change){return this.each(function(){var t=$(this),depth=t.menuItemDepth();$(this).removeClass("menu-item-depth-"+depth).addClass("menu-item-depth-"+(depth+change))})},childMenuItems:function(){var result=$();this.each(function(){var t=$(this),depth=t.menuItemDepth(),next=t.next();while(next.length&&next.menuItemDepth()>depth){result=result.add(next);next=next.next()}});return result},updateParentMenuItemDBId:function(){return this.each(function(){var item=$(this),input=item.find(".menu-item-data-parent-id"),depth=item.menuItemDepth(),parent=item.prev();if(depth==0){input.val(0)}else{while(parent.menuItemDepth()!=depth-1){parent=parent.prev()}input.val(parent.find(".menu-item-data-db-id").val())}})},hideAdvancedMenuItemFields:function(){return this.each(function(){var that=$(this);$(".hide-column-tog").not(":checked").each(function(){that.find(".field-"+$(this).val()).addClass("hidden-field")})})},addSelectedToMenu:function(processMethod){return this.each(function(){var t=$(this),menuItems={},checkboxes=t.find(".tabs-panel-active .categorychecklist li input:checked"),re=new RegExp("menu-item\\[([^\\]]*)");processMethod=processMethod||api.addMenuItemToBottom;if(!checkboxes.length){return false}t.find("img.waiting").show();$(checkboxes).each(function(){var listItemDBIDMatch=re.exec($(this).attr("name")),listItemDBID="undefined"==typeof listItemDBIDMatch[1]?0:parseInt(listItemDBIDMatch[1],10);menuItems[listItemDBID]=api.getListDataFromID(listItemDBID)});api.addItemToMenu(menuItems,processMethod,function(){checkboxes.removeAttr("checked");t.find("img.waiting").hide()})})}})},initToggles:function(){postboxes.add_postbox_toggles("nav-menus");columns.useCheckboxesForHidden();columns.checked=function(field){$(".field-"+field).removeClass("hidden-field")};columns.unchecked=function(field){$(".field-"+field).addClass("hidden-field")};api.menuList.hideAdvancedMenuItemFields()},initSortables:function(){var currentDepth=0,originalDepth,minDepth,maxDepth,prev,next,prevBottom,nextThreshold,helperHeight,transport,menuLeft=api.menuList.offset().left;api.menuList.sortable({handle:".menu-item-handle",placeholder:"sortable-placeholder",start:function(e,ui){var height,width,parent,children,maxChildDepth,tempHolder;transport=ui.item.children(".menu-item-transport");originalDepth=ui.item.menuItemDepth();updateCurrentDepth(ui,originalDepth);parent=(ui.item.next()[0]==ui.placeholder[0])?ui.item.next():ui.item;children=parent.childMenuItems();transport.append(children);height=transport.outerHeight();height+=(height>0)?(ui.placeholder.css("margin-top").slice(0,-2)*1):0;height+=ui.helper.outerHeight();helperHeight=height;height-=2;ui.placeholder.height(height);maxChildDepth=originalDepth;children.each(function(){var depth=$(this).menuItemDepth();maxChildDepth=(depth>maxChildDepth)?depth:maxChildDepth});width=ui.helper.find(".menu-item-handle").outerWidth();width+=api.depthToPx(maxChildDepth-originalDepth);width-=2;ui.placeholder.width(width);tempHolder=ui.placeholder.next();tempHolder.css("margin-top",helperHeight+"px");ui.placeholder.detach();$(this).sortable("refresh");ui.item.after(ui.placeholder);tempHolder.css("margin-top",0);updateSharedVars(ui)},stop:function(e,ui){var children,depthChange=currentDepth-originalDepth;children=transport.children().insertAfter(ui.item);if(depthChange!=0){ui.item.updateDepthClass(currentDepth);children.shiftDepthClass(depthChange);api.registerChange()}ui.item.updateParentMenuItemDBId()},change:function(e,ui){if(!ui.placeholder.parent().hasClass("menu")){(prev.length)?prev.after(ui.placeholder):api.menuList.prepend(ui.placeholder)}updateSharedVars(ui)},sort:function(e,ui){var offset=ui.helper.offset(),depth=api.pxToDepth(offset.left-menuLeft);if(depth>maxDepth||offset.top<prevBottom){depth=maxDepth}else{if(depth<minDepth){depth=minDepth}}if(depth!=currentDepth){updateCurrentDepth(ui,depth)}if(nextThreshold&&offset.top+helperHeight>nextThreshold){next.after(ui.placeholder);updateSharedVars(ui);$(this).sortable("refreshPositions")}},update:function(e,ui){api.registerChange()}});function updateSharedVars(ui){var depth;prev=ui.placeholder.prev();next=ui.placeholder.next();if(prev[0]==ui.item[0]){prev=prev.prev()}if(next[0]==ui.item[0]){next=next.next()}prevBottom=(prev.length)?prev.offset().top+prev.height():0;nextThreshold=(next.length)?next.offset().top+next.height()/3:0;minDepth=(next.length)?next.menuItemDepth():0;if(prev.length){maxDepth=((depth=prev.menuItemDepth()+1)>api.options.globalMaxDepth)?api.options.globalMaxDepth:depth}else{maxDepth=0}}function updateCurrentDepth(ui,depth){ui.placeholder.updateDepthClass(depth,currentDepth);currentDepth=depth}},attachMenuEditListeners:function(){var that=this;$("#update-nav-menu").bind("click",function(e){if(e.target&&e.target.className){if(-1!=e.target.className.indexOf("item-edit")){return that.eventOnClickEditLink(e.target)}else{if(-1!=e.target.className.indexOf("menu-save")){return that.eventOnClickMenuSave(e.target)}else{if(-1!=e.target.className.indexOf("menu-delete")){return that.eventOnClickMenuDelete(e.target)}else{if(-1!=e.target.className.indexOf("item-delete")){return that.eventOnClickMenuItemDelete(e.target)}else{if(-1!=e.target.className.indexOf("item-close")){return that.eventOnClickCloseLink(e.target)}}}}}}})},setupInputWithDefaultTitle:function(){var name="input-with-default-title";$("."+name).each(function(){var $t=$(this),title=$t.attr("title"),val=$t.val();$t.data(name,title);if(""==val){$t.val(title)}else{if(title==val){return}else{$t.removeClass(name)}}}).focus(function(){var $t=$(this);if($t.val()==$t.data(name)){$t.val("").removeClass(name)}}).blur(function(){var $t=$(this);if(""==$t.val()){$t.addClass(name).val($t.data(name))}})},attachAddMenuItemListeners:function(){var form=$("#nav-menu-meta");form.find(".add-to-menu input").click(function(){api.registerChange();$(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)})},attachThemeLocationsListeners:function(){var loc=$("#nav-menu-theme-locations"),params={action:"menu-locations-save","menu-locations":loc.find("select").serialize(),"menu-settings-column-nonce":$("#menu-settings-column-nonce").val()};loc.find("input[type=submit]").click(function(){loc.find(".waiting").show();$.post(ajaxurl,params,function(r){loc.find(".waiting").hide()});return false})},attachQuickSearchListeners:function(){var searchTimer;$(".quick-search").keypress(function(e){var t=$(this);if(13==e.which){api.updateQuickSearchResults(t);return false}if(searchTimer){clearTimeout(searchTimer)}searchTimer=setTimeout(function(){api.updateQuickSearchResults(t)},400)}).attr("autocomplete","off")},updateQuickSearchResults:function(input){var panel,params,minSearchLength=2,q=input.val();if(q.length<minSearchLength){return}panel=input.parents(".tabs-panel");params={action:"menu-quick-search","response-format":"markup",menu:$("#menu").val(),"menu-settings-column-nonce":$("#menu-settings-column-nonce").val(),q:q,type:input.attr("name")};$("img.waiting",panel).show();$.post(ajaxurl,params,function(menuMarkup){api.processQuickSearchQueryResponse(menuMarkup,params,panel)})},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}$(".customlinkdiv img.waiting").show();this.addLinkToMenu(url,label,processMethod,function(){$(".customlinkdiv img.waiting").hide();$("#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":menuItem};$.post(ajaxurl,params,function(menuMarkup){processMethod(menuMarkup,params);callback()})},addMenuItemToBottom:function(menuMarkup,req){$(menuMarkup).hideAdvancedMenuItemFields().appendTo(api.targetList)},addMenuItemToTop:function(menuMarkup,req){$(menuMarkup).hideAdvancedMenuItemFields().prependTo(api.targetList)},attachHomeLinkListener:function(){$(".add-home-link",".customlinkdiv").click(function(e){api.addLinkToMenu(navMenuL10n.homeurl,navMenuL10n.home,api.addMenuItemToTop);return false})},attachUnsavedChangesListener:function(){$("#menu-management input, #menu-management select, #menu-management, #menu-management textarea").change(function(){api.registerChange()});window.onbeforeunload=function(){if(api.menusChanged){return navMenuL10n.saveAlert}};$("input.menu-save, input.save-menu-item").click(function(){window.onbeforeunload=null})},registerChange:function(){api.menusChanged=true},attachTabsPanelListeners:function(){$("#menu-settings-column").bind("click",function(e){var selectAreaMatch,activePanel,panelId,wrapper,items,target=$(e.target);if(target.hasClass("nav-tab-link")){panelId=/#(.*)$/.exec(e.target.href);if(panelId&&panelId[1]){panelId=panelId[1]}else{return false}wrapper=target.parents(".inside").first();$("input",wrapper).removeAttr("checked");$(".tabs-panel-active",wrapper).removeClass("tabs-panel-active").addClass("tabs-panel-inactive");$("#"+panelId,wrapper).removeClass("tabs-panel-inactive").addClass("tabs-panel-active");$(".tabs",wrapper).removeClass("tabs");target.parent().addClass("tabs");$(".quick-search",wrapper).focus();return false}else{if(target.hasClass("select-all")){selectAreaMatch=/#(.*)$/.exec(e.target.href);if(selectAreaMatch&&selectAreaMatch[1]){items=$("#"+selectAreaMatch[1]+" .tabs-panel-active .menu-item-title input");if(items.length===items.filter(":checked").length){items.removeAttr("checked")}else{items.attr("checked","checked")}return false}}}})},initTabManager:function(){var fixed=$(".nav-tabs-wrapper"),fluid=fixed.children(".nav-tabs"),active=fluid.children(".nav-tab-active"),tabs=fluid.children(".nav-tab"),tabsWidth=0,fixedRight,fixedLeft,arrowLeft,arrowRight,resizing=false;function resetMenuTabs(){fixedLeft=fixed.offset().left;fixedRight=fixedLeft+fixed.width();active.makeTabVisible()}$.fn.extend({makeTabVisible:function(){var t=this.eq(0),left,right;if(!t.length){return}left=t.offset().left;right=left+t.outerWidth();if(right>fixedRight){fluid.animate({"margin-left":"+="+(fixedRight-right)+"px"},"fast")}else{if(left<fixedLeft){fluid.animate({"margin-left":"-="+(left-fixedLeft)+"px"},"fast")}}return t},isTabVisible:function(){var t=this.eq(0),left=t.offset().left,right=left+t.outerWidth();return(right<=fixedRight&&left>=fixedLeft)?true:false}});tabs.each(function(){tabsWidth+=$(this).outerWidth(true)});if(tabsWidth<=fixed.width()-fluid.css("padding-left").slice(0,-2)-fluid.css("padding-right").slice(0,-2)){return}fluid.css({"margin-right":(-1*tabsWidth)+"px",padding:0});arrowLeft=$('<div class="nav-tabs-arrow nav-tabs-arrow-left"><a>&laquo;</a></div>');arrowRight=$('<div class="nav-tabs-arrow nav-tabs-arrow-right"><a>&raquo;</a></div>');fixed.wrap('<div class="nav-tabs-nav"/>').parent().prepend(arrowLeft).append(arrowRight);resetMenuTabs();$(window).resize(function(){if(resizing){return}resizing=true;setTimeout(function(){resetMenuTabs();resizing=false},1000)});$.each([{arrow:arrowLeft,next:"next",last:"first",operator:"+="},{arrow:arrowRight,next:"prev",last:"last",operator:"-="}],function(){var that=this;this.arrow.mousedown(function(){var last=tabs[that.last](),fn=function(){if(!last.isTabVisible()){fluid.animate({"margin-left":that.operator+"90px"},300,"linear",fn)}};fn()}).mouseup(function(){var tab,next;fluid.stop(true);tab=tabs[that.last]();while((next=tab[that.next]())&&next.length&&!next.isTabVisible()){tab=next}tab.makeTabVisible()})})},eventOnClickEditLink:function(clickedEl){var activeEdit,matchedSection=/#(.*)$/.exec(clickedEl.href);if(matchedSection&&matchedSection[1]){activeEdit=$("#"+matchedSection[1]);if(0!=activeEdit.length){if(activeEdit.hasClass("menu-item-edit-inactive")){activeEdit.slideDown("fast").siblings("dl").andSelf().removeClass("menu-item-edit-inactive").addClass("menu-item-edit-active")}else{activeEdit.slideUp("fast").siblings("dl").andSelf().removeClass("menu-item-edit-active").addClass("menu-item-edit-inactive")}return false}}},eventOnClickCloseLink:function(clickedEl){$(clickedEl).closest(".menu-item-settings").siblings("dl").find(".item-edit").click();return false},eventOnClickMenuSave:function(clickedEl){api.menuList.find(".menu-item-data-position").val(function(index){return index+1});return true},eventOnClickMenuDelete:function(clickedEl){if(confirm(navMenuL10n.warnDeleteMenu)){return true}else{return false}},eventOnClickMenuItemDelete:function(clickedEl){var itemID,matchedSection,that=this;if(confirm(navMenuL10n.warnDeleteMenuItem)){matchedSection=/_wpnonce=([a-zA-Z0-9]*)$/.exec(clickedEl.href);if(matchedSection&&matchedSection[1]){itemID=parseInt(clickedEl.id.replace("delete-",""),10);$.post(ajaxurl,{action:"delete-menu-item","menu-item":itemID,_wpnonce:matchedSection[1]},function(resp){if("1"==resp){that.removeMenuItem(document.getElementById("menu-item-"+itemID))}});return false}return true}else{return false}},processQuickSearchQueryResponse:function(resp,req,panel){var i,matched,newID,takenIDs={},form=document.getElementById("nav-menu-meta"),pattern=new RegExp("menu-item\\[([^\\]]*)","g"),items=resp.match(/<li>.*<\/li>/g);if(!items){$(".categorychecklist",panel).html("<li><p>"+navMenuL10n.noResultsFound+"</p></li>");$("img.waiting",panel).hide();return}i=items.length;while(i--){matched=pattern.exec(items[i]);if(matched&&matched[1]){newID=matched[1];while(form.elements["menu-item["+newID+"][menu-item-type]"]||takenIDs[newID]){newID--}takenIDs[newID]=true;if(newID!=matched[1]){items[i]=items[i].replace(new RegExp("menu-item\\["+matched[1]+"\\]","g"),"menu-item["+newID+"]")}}}$(".categorychecklist",panel).html(items.join(""));$("img.waiting",panel).hide()},removeMenuItem:function(el){el=$(el);var children=el.childMenuItems(),that=this;el.addClass("deleting").fadeOut(350,function(){el.remove();children.shiftDepthClass(-1).updateParentMenuItemDBId()})},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-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},depthToPx:function(depth){return depth*api.options.menuItemDepthPerLevel},pxToDepth:function(px){return Math.floor(px/api.options.menuItemDepthPerLevel)}};$(document).ready(function(){wpNavMenu.init()})})(jQuery);