WordPress/wp-admin/js/customize-nav-menus.min.js

2 lines
35 KiB
JavaScript
Raw Normal View History

!function(a,b,c){"use strict";function d(a){return"nav_menu_item["+a+"]"}wpNavMenu.originalInit=wpNavMenu.init,wpNavMenu.options.menuItemDepthPerLevel=20,wpNavMenu.options.sortableItems="> .customize-control-nav_menu_item",wpNavMenu.options.targetTolerance=10,wpNavMenu.init=function(){this.jQueryExtensions()},a.Menus=a.Menus||{},a.Menus.data={nonce:"",itemTypes:{taxonomies:{},postTypes:{}},l10n:{},menuItemTransport:"postMessage",phpIntMax:0,defaultSettingValues:{nav_menu:{},nav_menu_item:{}}},"undefined"!=typeof _wpCustomizeNavMenusSettings&&c.extend(a.Menus.data,_wpCustomizeNavMenusSettings),a.Menus.generatePlaceholderAutoIncrementId=function(){return-Math.ceil(a.Menus.data.phpIntMax*Math.random())},a.Menus.AvailableItemModel=Backbone.Model.extend(c.extend({id:null},a.Menus.data.defaultSettingValues.nav_menu_item)),a.Menus.AvailableItemCollection=Backbone.Collection.extend({model:a.Menus.AvailableItemModel,sort_key:"order",comparator:function(a){return-a.get(this.sort_key)},sortByField:function(a){this.sort_key=a,this.sort()}}),a.Menus.availableMenuItems=new a.Menus.AvailableItemCollection(a.Menus.data.availableMenuItems),a.Menus.AvailableMenuItemsPanelView=b.Backbone.View.extend({el:"#available-menu-items",events:{"input #menu-items-search":"debounceSearch","keyup #menu-items-search":"debounceSearch","click #menu-items-search":"debounceSearch","focus .menu-item-tpl":"focus","click .menu-item-tpl":"_submit","click #custom-menu-item-submit":"_submitLink","keypress #custom-menu-item-name":"_submitLink",keydown:"keyboardAccessible"},selected:null,currentMenuControl:null,debounceSearch:null,$search:null,searchTerm:"",rendered:!1,pages:{},sectionContent:"",loading:!1,initialize:function(){var b=this;this.$search=c("#menu-items-search"),this.sectionContent=this.$el.find(".accordion-section-content"),this.debounceSearch=_.debounce(b.search,500),_.bindAll(this,"close"),c("#customize-controls, .customize-section-back").on("click keydown",function(a){var d=c(a.target).is(".item-delete, .item-delete *"),e=c(a.target).is(".add-new-menu-item, .add-new-menu-item *");!c("body").hasClass("adding-menu-items")||d||e||b.close()}),this.$el.on("input","#custom-menu-item-name.invalid, #custom-menu-item-url.invalid",function(){c(this).removeClass("invalid")}),a.panel("nav_menus").container.bind("expanded",function(){b.rendered||(b.initList(),b.rendered=!0)}),this.sectionContent.scroll(function(){var a=b.$el.find(".accordion-section.open .accordion-section-content").prop("scrollHeight"),d=b.$el.find(".accordion-section.open").height();if(!b.loading&&c(this).scrollTop()>.75*a-d){var e=c(this).data("type"),f=c(this).data("obj_type");"search"===e?b.searchTerm&&b.doSearch(b.pages.search):b.loadItems(e,f)}}),a.previewer.bind("url",this.close)},search:function(a){a&&(this.searchTerm&&!c("#available-menu-items-search").hasClass("open")&&(c("#available-menu-items .accordion-section-content").slideUp("fast"),c("#available-menu-items-search .accordion-section-content").slideDown("fast"),c("#available-menu-items .accordion-section.open").removeClass("open"),c("#available-menu-items-search").addClass("open")),""===a.target.value&&c("#available-menu-items-search").removeClass("open"),this.searchTerm!==a.target.value&&(this.searchTerm=a.target.value,this.pages.search=1,this.doSearch(1)))},doSearch:function(d){var e,f=this,g=c("#available-menu-items-search"),h=g.find(".accordion-section-content"),i=b.template("available-menu-item");if(f.currentRequest&&f.currentRequest.abort(),!(0>d)){if(d>1)g.addClass("loading-more"),h.attr("aria-busy","true"),b.a11y.speak(a.Menus.data.l10n.itemsLoadingMore);else if(""===f.searchTerm)return h.html(""),void b.a11y.speak("");g.addClass("loading"),f.loading=!0,e={"customize-menus-nonce":a.Menus.data.nonce,wp_customize:"on",search:f.searchTerm,page:d},f.currentRequest=b.ajax.post("search-available-menu-items-customizer",e),f.currentRequest.done(function(c){var e;1===d&&h.empty(),g.removeClass("loading loading-more"),h.attr("aria-busy","false"),g.addClass("open"),f.loading=!1,e=new a.Menus.AvailableItemCollection(c.items),f.col
menu_id:j}}),a.section.add(e,d),h.val(""),b.a11y.speak(a.Menus.data.l10n.menuAdded),a.section(e).focus()}}),c.extend(a.controlConstructor,{nav_menu_location:a.Menus.MenuLocationControl,nav_menu_item:a.Menus.MenuItemControl,nav_menu:a.Menus.MenuControl,nav_menu_name:a.Menus.MenuNameControl,new_menu:a.Menus.NewMenuControl}),c.extend(a.panelConstructor,{nav_menus:a.Menus.MenusPanel}),c.extend(a.sectionConstructor,{nav_menu:a.Menus.MenuSection,new_menu:a.Menus.NewMenuSection}),a.bind("ready",function(){a.Menus.availableMenuItemsPanel=new a.Menus.AvailableMenuItemsPanelView({collection:a.Menus.availableMenuItems}),a.bind("saved",function(b){(b.nav_menu_updates||b.nav_menu_item_updates)&&a.Menus.applySavedData(b)}),a.previewer.bind("refresh",function(){a.previewer.refresh()})}),a.Menus.applySavedData=function(b){var c={};_(b.nav_menu_updates).each(function(b){var d,e,f,g,h,i,j;if("inserted"===b.status){if(!b.previous_term_id)throw new Error("Expected previous_term_id");if(!b.term_id)throw new Error("Expected term_id");if(d="nav_menu["+String(b.previous_term_id)+"]",!a.has(d))throw new Error("Expected setting to exist: "+d);if(f=a(d),!a.section.has(d))throw new Error("Expected control to exist: "+d);if(i=a.section(d),h=f.get(),!h)throw new Error("Did not expect setting to be empty (deleted).");h=_.clone(h),c[b.previous_term_id]=b.term_id,e="nav_menu["+String(b.term_id)+"]",g=a.create(e,e,h,{type:"nav_menu",transport:"postMessage",previewer:a.previewer}),i.expanded()&&i.collapse(),j=new a.Menus.MenuSection(e,{params:{id:e,panel:"nav_menus",title:h.name,customizeAction:a.Menus.data.l10n.customizingMenus,type:"nav_menu",priority:i.priority.get(),active:!0,menu_id:b.term_id}}),i.container.remove(),a.section.remove(d),a.section.add(e,j),f.callbacks.disable(),f.set(!1),f.preview(),g.preview(),a.each(function(c){var d=a.state("saved").get();/^nav_menu_locations\[/.test(c.id)&&c.get()===b.previous_term_id&&(c.set(b.term_id),c._dirty=!1,a.state("saved").set(d),c.preview())}),i.expanded.get()&&j.expand()}}),_(b.nav_menu_item_updates).each(function(b){var d,e,f,g,h,i,j;if("inserted"===b.status){if(!b.previous_post_id)throw new Error("Expected previous_post_id");if(!b.post_id)throw new Error("Expected post_id");if(d="nav_menu_item["+String(b.previous_post_id)+"]",!a.has(d))throw new Error("Expected setting to exist: "+d);if(f=a(d),!a.control.has(d))throw new Error("Expected control to exist: "+d);if(i=a.control(d),h=f.get(),!h)throw new Error("Did not expect setting to be empty (deleted).");h=_.clone(h),c[h.nav_menu_term_id]&&(h.nav_menu_term_id=c[h.nav_menu_term_id]),e="nav_menu_item["+String(b.post_id)+"]",g=a.create(e,e,h,{type:"nav_menu_item",transport:"postMessage",previewer:a.previewer}),j=new a.controlConstructor.nav_menu_item(e,{params:{type:"nav_menu_item",content:'<li id="customize-control-nav_menu_item-'+String(b.post_id)+'" class="customize-control customize-control-nav_menu_item"></li>',menu_id:b.post_id,section:"nav_menu["+String(h.nav_menu_term_id)+"]",priority:i.priority.get(),active:!0,settings:{"default":e},menu_item_id:b.post_id},previewer:a.previewer}),i.container.remove(),a.control.remove(d),a.control.add(e,j),f.callbacks.disable(),f.set(!1),f.preview(),g.preview(),j.container.toggleClass("menu-item-edit-inactive",i.container.hasClass("menu-item-edit-inactive"))}})},a.Menus.focusMenuItemControl=function(b){var c=a.Menus.getMenuItemControl(b);c&&c.focus()},a.Menus.getMenuControl=function(b){return a.control("nav_menu["+b+"]")},a.Menus.getTypeLabel=function(b,c){var d,e=a.Menus.data;return d="post_type"===b?e.itemTypes.postTypes[c]?e.itemTypes.postTypes[c].label:e.l10n.postTypeLabel:"taxonomy"===b?e.itemTypes.taxonomies[c]?e.itemTypes.taxonomies[c].label:e.l10n.taxonomyTermLabel:e.l10n.custom_label},a.Menus.getMenuItemControl=function(b){return a.control(d(b))}}(wp.customize,wp,jQuery);