1 line
39 KiB
JavaScript
1 line
39 KiB
JavaScript
!function(d,c,l){"use strict";function m(e){return e=e||"",e=l("<div>").text(e).html(),(e=l.trim(e))||d.Menus.data.l10n.unnamed}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()},d.Menus=d.Menus||{},d.Menus.data={itemTypes:[],l10n:{},settingTransport:"refresh",phpIntMax:0,defaultSettingValues:{nav_menu:{},nav_menu_item:{}},locationSlugMappedToName:{}},"undefined"!=typeof _wpCustomizeNavMenusSettings&&l.extend(d.Menus.data,_wpCustomizeNavMenusSettings),d.Menus.generatePlaceholderAutoIncrementId=function(){return-Math.ceil(d.Menus.data.phpIntMax*Math.random())},d.Menus.AvailableItemModel=Backbone.Model.extend(l.extend({id:null},d.Menus.data.defaultSettingValues.nav_menu_item)),d.Menus.AvailableItemCollection=Backbone.Collection.extend({model:d.Menus.AvailableItemModel,sort_key:"order",comparator:function(e){return-e.get(this.sort_key)},sortByField:function(e){this.sort_key=e,this.sort()}}),d.Menus.availableMenuItems=new d.Menus.AvailableItemCollection(d.Menus.data.availableMenuItems),d.Menus.AvailableMenuItemsPanelView=c.Backbone.View.extend({el:"#available-menu-items",events:{"input #menu-items-search":"debounceSearch","keyup #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 n=this;d.panel.has("nav_menus")&&(this.$search=l("#menu-items-search"),this.sectionContent=this.$el.find(".accordion-section-content"),this.debounceSearch=_.debounce(n.search,500),_.bindAll(this,"close"),l("#customize-controls, .customize-section-back").on("click keydown",function(e){var t=l(e.target).is(".item-delete, .item-delete *"),e=l(e.target).is(".add-new-menu-item, .add-new-menu-item *");!l("body").hasClass("adding-menu-items")||t||e||n.close()}),l(".clear-results").on("click",function(){n.$search.val("").focus().trigger("keyup")}),this.$el.on("input","#custom-menu-item-name.invalid, #custom-menu-item-url.invalid",function(){l(this).removeClass("invalid")}),d.panel("nav_menus").container.bind("expanded",function(){n.rendered||(n.initList(),n.rendered=!0)}),this.sectionContent.scroll(function(){var e=n.$el.find(".accordion-section.open .accordion-section-content").prop("scrollHeight"),t=n.$el.find(".accordion-section.open").height();!n.loading&&l(this).scrollTop()>.75*e-t&&(e=l(this).data("type"),t=l(this).data("object"),"search"===e?n.searchTerm&&n.doSearch(n.pages.search):n.loadItems(e,t))}),d.previewer.bind("url",this.close),n.delegateEvents())},search:function(e){var t=l("#available-menu-items-search"),n=l("#available-menu-items .accordion-section").not(t);e&&this.searchTerm!==e.target.value&&(""===e.target.value||t.hasClass("open")?""===e.target.value&&(t.removeClass("open"),n.show(),t.find(".clear-results").removeClass("is-visible")):(n.fadeOut(100),t.find(".accordion-section-content").slideDown("fast"),t.addClass("open"),t.find(".clear-results").addClass("is-visible")),this.searchTerm=e.target.value,this.pages.search=1,this.doSearch(1))},doSearch:function(t){var e,n=this,i=l("#available-menu-items-search"),a=i.find(".accordion-section-content"),o=c.template("available-menu-item");if(n.currentRequest&&n.currentRequest.abort(),!(t<0)){if(1<t)i.addClass("loading-more"),a.attr("aria-busy","true"),c.a11y.speak(d.Menus.data.l10n.itemsLoadingMore);else if(""===n.searchTerm)return a.html(""),void c.a11y.speak("");i.addClass("loading"),n.loading=!0,e={"customize-menus-nonce":d.settings.nonce["customize-menus"],wp_customize:"on",search:n.searchTerm,page:t},n.currentRequest=c.ajax.post("search-available-menu-items-customizer",e),n.currentRequest.done(function(e){1===t&&a.empty(),i.removeClass("loading loading-more"),a.attr("aria-busy","false"),i.addClass("open"),n.loading=!1,e=new d.Menus.AvailableItemCollection(e.items),n.collection.add(e.models),e.each(function(e){a.append(o(e.attributes))}),e.length<20?n.pages.search=-1:n.pages.search=n.pages.search+1,e&&1<t?c.a11y.speak(d.Menus.data.l10n.itemsFoundMore.replace("%d",e.length)):e&&1===t&&c.a11y.speak(d.Menus.data.l10n.itemsFound.replace("%d",e.length))}),n.currentRequest.fail(function(e){e.message&&(a.empty().append(l('<p class="nothing-found"></p>').text(e.message)),c.a11y.speak(e.message)),n.pages.search=-1}),n.currentRequest.always(function(){i.removeClass("loading loading-more"),a.attr("aria-busy","false"),n.loading=!1,n.currentRequest=null})}},initList:function(){var t=this;_.each(d.Menus.data.itemTypes,function(e){t.pages[e.type+":"+e.object]=0,t.loadItems(e.type,e.object)})},loadItems:function(n,i){var e,a,o=this,s=c.template("available-menu-item");-1!==o.pages[n+":"+i]&&((a=l("#available-menu-items-"+n+"-"+i)).find(".accordion-section-title").addClass("loading"),o.loading=!0,e={"customize-menus-nonce":d.settings.nonce["customize-menus"],wp_customize:"on",type:n,object:i,page:o.pages[n+":"+i]},(e=c.ajax.post("load-available-menu-items-customizer",e)).done(function(e){var t,e=e.items;if(0===e.length)return 0===o.pages[n+":"+i]&&a.addClass("cannot-expand").removeClass("loading").find(".accordion-section-title > button").prop("tabIndex",-1),void(o.pages[n+":"+i]=-1);e=new d.Menus.AvailableItemCollection(e),o.collection.add(e.models),t=a.find(".accordion-section-content"),e.each(function(e){t.append(s(e.attributes))}),o.pages[n+":"+i]+=1}),e.fail(function(e){"undefined"!=typeof console&&console.error&&console.error(e)}),e.always(function(){a.find(".accordion-section-title").removeClass("loading"),o.loading=!1}))},itemSectionHeight:function(){var e=window.innerHeight,t=this.$el.find(".accordion-section:not( #available-menu-items-search ) .accordion-section-content"),e=e-(46*(2+t.length)-13);120<e&&e<290&&t.css("max-height",e)},select:function(e){this.selected=l(e),this.selected.siblings(".menu-item-tpl").removeClass("selected"),this.selected.addClass("selected")},focus:function(e){this.select(l(e.currentTarget))},_submit:function(e){"keypress"===e.type&&13!==e.which&&32!==e.which||this.submit(l(e.currentTarget))},submit:function(e){var t;(e=e||this.selected)&&this.currentMenuControl&&(this.select(e),t=l(this.selected).data("menu-item-id"),(t=this.collection.findWhere({id:t}))&&(this.currentMenuControl.addItemToMenu(t.attributes),l(e).find(".menu-item-handle").addClass("item-added")))},_submitLink:function(e){"keypress"===e.type&&13!==e.which||this.submitLink()},submitLink:function(){var e,t=l("#custom-menu-item-name"),n=l("#custom-menu-item-url");this.currentMenuControl&&(""!==t.val()?""!==n.val()&&"http://"!==n.val()?(e={title:t.val(),url:n.val(),type:"custom",type_label:d.Menus.data.l10n.custom_label,object:""},this.currentMenuControl.addItemToMenu(e),n.val("http://"),t.val("")):n.addClass("invalid"):t.addClass("invalid"))},open:function(e){this.currentMenuControl=e,this.itemSectionHeight(),l("body").addClass("adding-menu-items"),_(this.currentMenuControl.getMenuItemControls()).each(function(e){e.collapseForm()}),this.$el.find(".selected").removeClass("selected"),this.$search.focus()},close:function(e){(e=e||{}).returnFocus&&this.currentMenuControl&&this.currentMenuControl.container.find(".add-new-menu-item").focus(),this.currentMenuControl=null,this.selected=null,l("body").removeClass("adding-menu-items"),l("#available-menu-items .menu-item-handle.item-added").removeClass("item-added"),this.$search.val("")},keyboardAccessible:function(e){var t=13===e.which,n=27===e.which,i=9===e.which&&e.shiftKey,a=l(e.target).is(this.$search);t&&!this.$search.val()||(a&&i?(this.currentMenuControl.container.find(".add-new-menu-item").focus(),e.preventDefault()):n&&this.close({returnFocus:!0}))}}),d.Menus.MenusPanel=d.Panel.extend({attachEvents:function(){d.Panel.prototype.attachEvents.call(this);var t=this.container.find(".panel-meta"),n=t.find(".customize-help-toggle"),i=t.find(".customize-panel-description"),a=l("#screen-options-wrap"),o=t.find(".customize-screen-options-toggle");o.on("click keydown",function(e){if(!d.utils.isKeydownButNotEnterEvent(e))return e.preventDefault(),i.not(":hidden")&&(i.slideUp("fast"),n.attr("aria-expanded","false")),"true"===o.attr("aria-expanded")?(o.attr("aria-expanded","false"),t.removeClass("open"),t.removeClass("active-menu-screen-options"),a.slideUp("fast")):(o.attr("aria-expanded","true"),t.addClass("open"),t.addClass("active-menu-screen-options"),a.slideDown("fast")),!1}),n.on("click keydown",function(e){d.utils.isKeydownButNotEnterEvent(e)||(e.preventDefault(),"true"===o.attr("aria-expanded")&&(o.attr("aria-expanded","false"),n.attr("aria-expanded","true"),t.addClass("open"),t.removeClass("active-menu-screen-options"),a.slideUp("fast"),i.slideDown("fast")))})},ready:function(){var n=this;this.container.find(".hide-column-tog").click(function(){var e=l(this),t=e.val();e.prop("checked")?n.checked(t):n.unchecked(t),n.saveManageColumnsState()}),this.container.find(".hide-column-tog").each(function(){var e=l(this),t=e.val();e.prop("checked")?n.checked(t):n.unchecked(t)})},saveManageColumnsState:_.debounce(function(){var e=this;e._updateHiddenColumnsRequest&&e._updateHiddenColumnsRequest.abort(),e._updateHiddenColumnsRequest=c.ajax.post("hidden-columns",{hidden:e.hidden(),screenoptionnonce:l("#screenoptionnonce").val(),page:"nav-menus"}),e._updateHiddenColumnsRequest.always(function(){e._updateHiddenColumnsRequest=null})},2e3),checked:function(e){this.container.addClass("field-"+e+"-active")},unchecked:function(e){this.container.removeClass("field-"+e+"-active")},hidden:function(){return l(".hide-column-tog").not(":checked").map(function(){var e=this.id;return e.substring(0,e.length-5)}).get().join(",")}}),d.Menus.MenuSection=d.Section.extend({initialize:function(e,t){d.Section.prototype.initialize.call(this,e,t),this.deferred.initSortables=l.Deferred()},ready:function(){var n=this;if(void 0===n.params.menu_id)throw new Error("params.menu_id was not defined");n.active.validate=function(){return!!d.has(n.id)&&!!d(n.id).get()},n.populateControls(),n.navMenuLocationSettings={},n.assignedLocations=new d.Value([]),d.each(function(e,t){t=t.match(/^nav_menu_locations\[(.+?)]/);t&&(n.navMenuLocationSettings[t[1]]=e).bind(function(){n.refreshAssignedLocations()})}),n.assignedLocations.bind(function(e){n.updateAssignedLocationsInSectionTitle(e)}),n.refreshAssignedLocations(),d.bind("pane-contents-reflowed",function(){n.container.parent().length&&(n.container.find(".menu-item .menu-item-reorder-nav button").attr({tabindex:"0","aria-hidden":"false"}),n.container.find(".menu-item.move-up-disabled .menus-move-up").attr({tabindex:"-1","aria-hidden":"true"}),n.container.find(".menu-item.move-down-disabled .menus-move-down").attr({tabindex:"-1","aria-hidden":"true"}),n.container.find(".menu-item.move-left-disabled .menus-move-left").attr({tabindex:"-1","aria-hidden":"true"}),n.container.find(".menu-item.move-right-disabled .menus-move-right").attr({tabindex:"-1","aria-hidden":"true"}))})},populateControls:function(){var e=this,t=e.id+"[name]",n=d.control(t);n||(n=new d.controlConstructor.nav_menu_name(t,{params:{type:"nav_menu_name",content:'<li id="customize-control-'+e.id.replace("[","-").replace("]","")+'-name" class="customize-control customize-control-nav_menu_name"></li>',label:d.Menus.data.l10n.menuNameLabel,active:!0,section:e.id,priority:0,settings:{"default":e.id}}}),d.control.add(n.id,n),n.active.set(!0)),(t=d.control(e.id))||(t=new d.controlConstructor.nav_menu(e.id,{params:{type:"nav_menu",content:'<li id="customize-control-'+e.id.replace("[","-").replace("]","")+'" class="customize-control customize-control-nav_menu"></li>',section:e.id,priority:998,active:!0,settings:{"default":e.id},menu_id:e.params.menu_id}}),d.control.add(t.id,t),t.active.set(!0)),n=e.id+"[auto_add]",(t=d.control(n))||(t=new d.controlConstructor.nav_menu_auto_add(n,{params:{type:"nav_menu_auto_add",content:'<li id="customize-control-'+e.id.replace("[","-").replace("]","")+'-auto-add" class="customize-control customize-control-nav_menu_auto_add"></li>',label:"",active:!0,section:e.id,priority:999,settings:{"default":e.id}}}),d.control.add(t.id,t),t.active.set(!0))},refreshAssignedLocations:function(){var n=this.params.menu_id,i=[];_.each(this.navMenuLocationSettings,function(e,t){e()===n&&i.push(t)}),this.assignedLocations.set(i)},updateAssignedLocationsInSectionTitle:function(e){var n=this.container.find(".accordion-section-title:first");n.find(".menu-in-location").remove(),_.each(e,function(e){var t=l('<span class="menu-in-location"></span>'),e=d.Menus.data.locationSlugMappedToName[e];t.text(d.Menus.data.l10n.menuLocation.replace("%s",e)),n.append(t)}),this.container.toggleClass("assigned-to-menu-location",0!==e.length)},onChangeExpanded:function(e,t){var n=this;e&&(wpNavMenu.menuList=n.container.find(".accordion-section-content:first"),wpNavMenu.targetList=wpNavMenu.menuList,l("#menu-to-edit").removeAttr("id"),wpNavMenu.menuList.attr("id","menu-to-edit").addClass("menu"),_.each(d.section(n.id).controls(),function(e){"nav_menu_item"===e.params.type&&e.actuallyEmbed()}),"resolved"!==n.deferred.initSortables.state()&&(wpNavMenu.initSortables(),n.deferred.initSortables.resolve(wpNavMenu.menuList),d.control("nav_menu["+String(n.params.menu_id)+"]").reflowMenuItems())),d.Section.prototype.onChangeExpanded.call(n,e,t)}}),d.Menus.NewMenuSection=d.Section.extend({attachEvents:function(){var e=this;this.container.on("click",".add-menu-toggle",function(){e.expanded()?e.collapse():e.expand()})},onChangeExpanded:function(e){var t=this.container.find(".add-menu-toggle"),n=this.container.find(".new-menu-section-content"),i=this.container.closest(".wp-full-overlay-sidebar-content");e?(t.addClass("open"),t.attr("aria-expanded","true"),n.slideDown("fast",function(){i.scrollTop(i.height())})):(t.removeClass("open"),t.attr("aria-expanded","false"),n.slideUp("fast"),n.find(".menu-name-field").removeClass("invalid"))}}),d.Menus.MenuLocationControl=d.Control.extend({initialize:function(e,t){var n=e.match(/^nav_menu_locations\[(.+?)]/);this.themeLocation=n[1],d.Control.prototype.initialize.call(this,e,t)},ready:function(){var n=this,i=/^nav_menu\[(-?\d+)]/;n.setting.validate=function(e){return parseInt(e,10)},n.container.find(".edit-menu").on("click",function(){var e=n.setting();d.section("nav_menu["+e+"]").focus()}),n.setting.bind("change",function(){0===n.setting()?n.container.find(".edit-menu").addClass("hidden"):n.container.find(".edit-menu").removeClass("hidden")}),d.bind("add",function(e){var t=e.id.match(i);t&&!1!==e()&&(t=t[1],t=new Option(m(e().name),t),n.container.find("select").append(t))}),d.bind("remove",function(e){var e=e.id.match(i);e&&(e=parseInt(e[1],10),n.setting()===e&&n.setting.set(""),n.container.find("option[value="+e+"]").remove())}),d.bind("change",function(e){var t=e.id.match(i);t&&(t=parseInt(t[1],10),!1===e()?(n.setting()===t&&n.setting.set(""),n.container.find("option[value="+t+"]").remove()):n.container.find("option[value="+t+"]").text(m(e().name)))})}}),d.Menus.MenuItemControl=d.Control.extend({initialize:function(e,t){var n=this;n.expanded=new d.Value(!1),n.expandedArgumentsQueue=[],n.expanded.bind(function(e){var t=n.expandedArgumentsQueue.shift(),t=l.extend({},n.defaultExpandedArguments,t);n.onChangeExpanded(e,t)}),d.Control.prototype.initialize.call(n,e,t),n.active.validate=function(){var e=d.section(n.section()),e=!!e&&e.active();return e}},embed:function(){var e=this.section();e&&((e=d.section(e))&&e.expanded()||d.settings.autofocus.control===this.id)&&this.actuallyEmbed()},actuallyEmbed:function(){"resolved"!==this.deferred.embedded.state()&&(this.renderContent(),this.deferred.embedded.resolve())},ready:function(){if(void 0===this.params.menu_item_id)throw new Error("params.menu_item_id was not defined");this._setupControlToggle(),this._setupReorderUI(),this._setupUpdateUI(),this._setupRemoveUI(),this._setupLinksUI(),this._setupTitleUI()},_setupControlToggle:function(){var t=this;this.container.find(".menu-item-handle").on("click",function(e){e.preventDefault(),e.stopPropagation();e=t.getMenuControl();e.isReordering||e.isSorting||t.toggleForm()})},_setupReorderUI:function(){var o=this,e=c.template("menu-item-reorder-nav");o.container.find(".item-controls").after(e),o.container.find(".menu-item-reorder-nav").find(".menus-move-up, .menus-move-down, .menus-move-left, .menus-move-right").on("click",function(){var e=l(this);e.focus();var t=e.is(".menus-move-up"),n=e.is(".menus-move-down"),i=e.is(".menus-move-left"),a=e.is(".menus-move-right");t?o.moveUp():n?o.moveDown():i?o.moveLeft():a&&o.moveRight(),e.focus()})},_setupUpdateUI:function(){var s=this,e=s.setting();s.elements={},s.elements.url=new d.Element(s.container.find(".edit-menu-item-url")),s.elements.title=new d.Element(s.container.find(".edit-menu-item-title")),s.elements.attr_title=new d.Element(s.container.find(".edit-menu-item-attr-title")),s.elements.target=new d.Element(s.container.find(".edit-menu-item-target")),s.elements.classes=new d.Element(s.container.find(".edit-menu-item-classes")),s.elements.xfn=new d.Element(s.container.find(".edit-menu-item-xfn")),s.elements.description=new d.Element(s.container.find(".edit-menu-item-description")),_.each(s.elements,function(n,i){n.bind(function(e){n.element.is("input[type=checkbox]")&&(e=e?n.element.val():"");var t=s.setting();t&&t[i]!==e&&((t=_.clone(t))[i]=e,s.setting.set(t))}),e&&("classes"!==i&&"xfn"!==i||!_.isArray(e[i])?n.set(e[i]):n.set(e[i].join(" ")))}),s.setting.bind(function(n,i){var e,t=s.params.menu_item_id,a=[],o=[];!1===n?(e=d.control("nav_menu["+String(i.nav_menu_term_id)+"]"),s.container.remove(),_.each(e.getMenuItemControls(),function(e){i.menu_item_parent===e.setting().menu_item_parent&&e.setting().position>i.position?a.push(e):e.setting().menu_item_parent===t&&o.push(e)}),_.each(a,function(e){var t=_.clone(e.setting());t.position+=o.length,e.setting.set(t)}),_.each(o,function(e,t){var n=_.clone(e.setting());n.position=i.position+t,n.menu_item_parent=i.menu_item_parent,e.setting.set(n)}),e.debouncedReflowMenuItems()):(_.each(n,function(e,t){s.elements[t]&&s.elements[t].set(n[t])}),s.container.find(".menu-item-data-parent-id").val(n.menu_item_parent),n.position===i.position&&n.menu_item_parent===i.menu_item_parent||s.getMenuControl().debouncedReflowMenuItems())})},_setupRemoveUI:function(){var a=this;a.container.find(".item-delete").on("click",function(){var e,t,n,i=!0;l("body").hasClass("adding-menu-items")||(i=!1),t=a.container.nextAll(".customize-control-nav_menu_item:visible").first(),n=a.container.prevAll(".customize-control-nav_menu_item:visible").first(),e=(t.length?t.find(!1===i?".item-edit":".item-delete"):n.length?n.find(!1===i?".item-edit":".item-delete"):a.container.nextAll(".customize-control-nav_menu").find(".add-new-menu-item")).first(),a.container.slideUp(function(){a.setting.set(!1),c.a11y.speak(d.Menus.data.l10n.itemDeleted),e.focus()})})},_setupLinksUI:function(){this.container.find("a.original-link").on("click",function(e){e.preventDefault(),d.previewer.previewUrl(e.target.toString())})},_setupTitleUI:function(){var i=this;i.setting.bind(function(e){var t,n;e&&(t=i.container.find(".menu-item-title"),n=e.title||d.Menus.data.l10n.untitled,e._invalid&&(n=d.Menus.data.l10n.invalidTitleTpl.replace("%s",n)),e.title?t.text(n).removeClass("no-title"):t.text(n).addClass("no-title"))})},getDepth:function(){var e=this,t=e.setting(),n=0;if(!t)return 0;for(;t&&t.menu_item_parent&&(n+=1,e=d.control("nav_menu_item["+t.menu_item_parent+"]"));)t=e.setting();return n},renderContent:function(){var e,t=this,n=t.setting();t.params.title=n.title||"",t.params.depth=t.getDepth(),t.container.data("item-depth",t.params.depth),e=["menu-item","menu-item-depth-"+String(t.params.depth),"menu-item-"+n.object,"menu-item-edit-inactive"],n._invalid?(e.push("menu-item-invalid"),t.params.title=d.Menus.data.l10n.invalidTitleTpl.replace("%s",t.params.title)):"draft"===n.status&&(e.push("pending"),t.params.title=d.Menus.data.pendingTitleTpl.replace("%s",t.params.title)),t.params.el_classes=e.join(" "),t.params.item_type_label=n.type_label,t.params.item_type=n.type,t.params.url=n.url,t.params.target=n.target,t.params.attr_title=n.attr_title,t.params.classes=_.isArray(n.classes)?n.classes.join(" "):n.classes,t.params.attr_title=n.attr_title,t.params.xfn=n.xfn,t.params.description=n.description,t.params.parent=n.menu_item_parent,t.params.original_title=n.original_title||"",t.container.addClass(t.params.el_classes),d.Control.prototype.renderContent.call(t)},getMenuControl:function(){var e=this.setting();return e&&e.nav_menu_term_id?d.control("nav_menu["+e.nav_menu_term_id+"]"):null},expandControlSection:function(){var e=this.container.closest(".accordion-section");e.hasClass("open")||e.find(".accordion-section-title:first").trigger("click")},_toggleExpanded:d.Section.prototype._toggleExpanded,expand:d.Section.prototype.expand,expandForm:function(e){this.expand(e)},collapse:d.Section.prototype.collapse,collapseForm:function(e){this.collapse(e)},toggleForm:function(e,t){(e=void 0===e?!this.expanded():e)?this.expand(t):this.collapse(t)},onChangeExpanded:function(e,t){var n,i=this,a=this.container,o=a.find(".menu-item-settings:first");void 0===e&&(e=!o.is(":visible")),o.is(":visible")!==e?e?(d.control.each(function(e){i.params.type===e.params.type&&i!==e&&e.collapseForm()}),n=function(){a.removeClass("menu-item-edit-inactive").addClass("menu-item-edit-active"),i.container.trigger("expanded"),t&&t.completeCallback&&t.completeCallback()},a.find(".item-edit").attr("aria-expanded","true"),o.slideDown("fast",n),i.container.trigger("expand")):(n=function(){a.addClass("menu-item-edit-inactive").removeClass("menu-item-edit-active"),i.container.trigger("collapsed"),t&&t.completeCallback&&t.completeCallback()},i.container.trigger("collapse"),a.find(".item-edit").attr("aria-expanded","false"),o.slideUp("fast",n)):t&&t.completeCallback&&t.completeCallback()},focus:function(e){var t=this,n=(e=e||{}).completeCallback;t.expandControlSection(),e.completeCallback=function(){t.container.find(".menu-item-settings").find("input, select, textarea, button, object, a[href], [tabindex]").filter(":visible").first().focus(),n&&n()},t.expandForm(e)},moveUp:function(){this._changePosition(-1),c.a11y.speak(d.Menus.data.l10n.movedUp)},moveDown:function(){this._changePosition(1),c.a11y.speak(d.Menus.data.l10n.movedDown)},moveLeft:function(){this._changeDepth(-1),c.a11y.speak(d.Menus.data.l10n.movedLeft)},moveRight:function(){this._changeDepth(1),c.a11y.speak(d.Menus.data.l10n.movedRight)},_changePosition:function(e){var t,n=this,i=_.clone(n.setting()),a=[];if(1!==e&&-1!==e)throw new Error("Offset changes by 1 are only supported.");if(n.setting()){if(_(n.getMenuControl().getMenuItemControls()).each(function(e){e.setting().menu_item_parent===i.menu_item_parent&&a.push(e.setting)}),a.sort(function(e,t){return e().position-t().position}),-1===(t=_.indexOf(a,n.setting)))throw new Error("Expected setting to be among siblings.");0===t&&e<0||t===a.length-1&&0<e||((t=a[t+e])&&t.set(l.extend(_.clone(t()),{position:i.position})),i.position+=e,n.setting.set(i))}},_changeDepth:function(e){if(1!==e&&-1!==e)throw new Error("Offset changes by 1 are only supported.");var t,n,i=this,a=_.clone(i.setting()),o=[];if(_(i.getMenuControl().getMenuItemControls()).each(function(e){e.setting().menu_item_parent===a.menu_item_parent&&o.push(e)}),o.sort(function(e,t){return e.setting().position-t.setting().position}),-1===(t=_.indexOf(o,i)))throw new Error("Expected control to be among siblings.");-1===e?a.menu_item_parent&&(n=d.control("nav_menu_item["+a.menu_item_parent+"]"),_(o).chain().slice(t).each(function(e,t){e.setting.set(l.extend({},e.setting(),{menu_item_parent:i.params.menu_item_id,position:t}))}),_(i.getMenuControl().getMenuItemControls()).each(function(e){var t;e.setting().menu_item_parent===n.setting().menu_item_parent&&e.setting().position>n.setting().position&&(t=_.clone(e.setting()),e.setting.set(l.extend(t,{position:t.position+1})))}),a.position=n.setting().position+1,a.menu_item_parent=n.setting().menu_item_parent,i.setting.set(a)):1===e&&0!==t&&(t=o[t-1],a.menu_item_parent=t.params.menu_item_id,a.position=0,_(i.getMenuControl().getMenuItemControls()).each(function(e){e.setting().menu_item_parent===a.menu_item_parent&&(a.position=Math.max(a.position,e.setting().position))}),a.position+=1,i.setting.set(a))}}),d.Menus.MenuNameControl=d.Control.extend({ready:function(){var n=this,e=n.setting();n.active.validate=function(){var e=d.section(n.section()),e=!!e&&e.active();return e},n.nameElement=new d.Element(n.container.find(".menu-name-field")),n.nameElement.bind(function(e){var t=n.setting();t&&t.name!==e&&((t=_.clone(t)).name=e,n.setting.set(t))}),e&&n.nameElement.set(e.name),n.setting.bind(function(e){e&&n.nameElement.set(e.name)})}}),d.Menus.MenuAutoAddControl=d.Control.extend({ready:function(){var n=this,e=n.setting();n.active.validate=function(){var e=d.section(n.section()),e=!!e&&e.active();return e},n.autoAddElement=new d.Element(n.container.find("input[type=checkbox].auto_add")),n.autoAddElement.bind(function(e){var t=n.setting();t&&t.name!==e&&((t=_.clone(t)).auto_add=e,n.setting.set(t))}),e&&n.autoAddElement.set(e.auto_add),n.setting.bind(function(e){e&&n.autoAddElement.set(e.auto_add)})}}),d.Menus.MenuControl=d.Control.extend({ready:function(){var t,n,i=this,a=i.params.menu_id,e=i.setting();if(void 0===this.params.menu_id)throw new Error("params.menu_id was not defined");i.active.validate=function(){var e=d.section(i.section()),e=!!e&&e.active();return e},i.$controlSection=i.container.closest(".control-section"),i.$sectionContent=i.container.closest(".accordion-section-content"),this._setupModel(),d.section(i.section(),function(e){e.deferred.initSortables.done(function(e){i._setupSortable(e)})}),this._setupAddition(),this._setupLocations(),this._setupTitle(),e&&(t=m(e.name),d.control.each(function(e){e.extended(d.controlConstructor.widget_form)&&"nav_menu"===e.params.widget_id_base&&(e.container.find(".nav-menu-widget-form-controls:first").show(),e.container.find(".nav-menu-widget-no-menus-message:first").hide(),0===(n=e.container.find("select")).find("option[value="+String(a)+"]").length&&n.append(new Option(t,a)))}),(e=l("#available-widgets-list .widget-tpl:has( input.id_base[ value=nav_menu ] )")).find(".nav-menu-widget-form-controls:first").show(),e.find(".nav-menu-widget-no-menus-message:first").hide(),0===(n=e.find(".widget-inside select:first")).find("option[value="+String(a)+"]").length&&n.append(new Option(t,a)))},_setupModel:function(){var n=this,i=n.params.menu_id;n.setting.bind(function(e){var t;!1===e?n._handleDeletion():(t=m(e.name),d.control.each(function(e){e.extended(d.controlConstructor.widget_form)&&"nav_menu"===e.params.widget_id_base&&e.container.find("select").find("option[value="+String(i)+"]").text(t)}))}),n.container.find(".menu-delete").on("click",function(e){e.stopPropagation(),e.preventDefault(),n.setting.set(!1)})},_setupSortable:function(e){var a=this;if(!e.is(a.$sectionContent))throw new Error("Unexpected menuList.");e.on("sortstart",function(){a.isSorting=!0}),e.on("sortstop",function(){setTimeout(function(){var e=a.$sectionContent.sortable("toArray"),t=[],n=0,i=10;a.isSorting=!1,a.$sectionContent.scrollLeft(0),_.each(e,function(e){var e=e.match(/^customize-control-nav_menu_item-(-?\d+)$/,"");e&&(e=parseInt(e[1],10),(e=d.control("nav_menu_item["+String(e)+"]"))&&t.push(e))}),_.each(t,function(e){var t;!1!==e.setting()&&(t=_.clone(e.setting()),n+=1,i+=1,t.position=n,e.priority(i),t.menu_item_parent=parseInt(e.container.find(".menu-item-data-parent-id").val(),10),t.menu_item_parent||(t.menu_item_parent=0),e.setting.set(t))})})}),a.isReordering=!1,this.container.find(".reorder-toggle").on("click",function(){a.toggleReordering(!a.isReordering)})},_setupAddition:function(){var t=this;this.container.find(".add-new-menu-item").on("click",function(e){t.$sectionContent.hasClass("reordering")||(l("body").hasClass("adding-menu-items")?(l(this).attr("aria-expanded","false"),d.Menus.availableMenuItemsPanel.close(),e.stopPropagation()):(l(this).attr("aria-expanded","true"),d.Menus.availableMenuItemsPanel.open(t)))})},_handleDeletion:function(){var e,n=this.params.menu_id,i=0,t=d.section(this.section()),a=function(){t.container.remove(),d.section.remove(t.id)};t&&t.expanded()?t.collapse({completeCallback:function(){a(),c.a11y.speak(d.Menus.data.l10n.menuDeleted),d.panel("nav_menus").focus()}}):a(),d.each(function(e){/^nav_menu\[/.test(e.id)&&!1!==e()&&(i+=1)}),d.control.each(function(e){var t;e.extended(d.controlConstructor.widget_form)&&"nav_menu"===e.params.widget_id_base&&((t=e.container.find("select")).val()===String(n)&&t.prop("selectedIndex",0).trigger("change"),e.container.find(".nav-menu-widget-form-controls:first").toggle(0!==i),e.container.find(".nav-menu-widget-no-menus-message:first").toggle(0===i),e.container.find("option[value="+String(n)+"]").remove())}),(e=l("#available-widgets-list .widget-tpl:has( input.id_base[ value=nav_menu ] )")).find(".nav-menu-widget-form-controls:first").toggle(0!==i),e.find(".nav-menu-widget-no-menus-message:first").toggle(0===i),e.find("option[value="+String(n)+"]").remove()},_setupLocations:function(){var o=this;o.container.find(".assigned-menu-location").each(function(){var n=l(this),e=n.find("input[type=checkbox]"),t=d("nav_menu_locations["+e.data("location-id")+"]"),i=function(e){var t=d("nav_menu["+String(e)+"]");e&&t&&t()?n.find(".theme-location-set").show().find("span").text(m(t().name)):n.find(".theme-location-set").hide()},a=new d.Element(e);a.set(t.get()===o.params.menu_id),e.on("change",function(){t.set(this.checked?o.params.menu_id:0)}),t.bind(function(e){a.set(e===o.params.menu_id),i(e)}),i(t.get())})},_setupTitle:function(){var u=this;u.setting.bind(function(e){var t,n,i,a,o,s,r;e&&(t=u.container.closest(".accordion-section"),n=u.params.menu_id,i=t.find(".accordion-section-title"),a=t.find(".customize-section-title h3"),o=t.find(".menu-in-location"),s=a.find(".customize-action"),r=m(e.name),i.text(r),o.length&&o.appendTo(i),a.text(r),s.length&&s.prependTo(a),d.control.each(function(e){/^nav_menu_locations\[/.test(e.id)&&e.container.find("option[value="+n+"]").text(r)}),t.find(".customize-control-checkbox input").each(function(){l(this).prop("checked")&&l(".current-menu-location-name-"+l(this).data("location-id")).text(r)}))})},toggleReordering:function(e){var t=this.container.find(".add-new-menu-item"),n=this.container.find(".reorder-toggle"),i=this.$sectionContent.find(".item-title");(e=Boolean(e))!==this.$sectionContent.hasClass("reordering")&&(this.isReordering=e,this.$sectionContent.toggleClass("reordering",e),this.$sectionContent.sortable(this.isReordering?"disable":"enable"),this.isReordering?(t.attr({tabindex:"-1","aria-hidden":"true"}),n.attr("aria-label",d.Menus.data.l10n.reorderLabelOff),c.a11y.speak(d.Menus.data.l10n.reorderModeOn),i.attr("aria-hidden","false")):(t.removeAttr("tabindex aria-hidden"),n.attr("aria-label",d.Menus.data.l10n.reorderLabelOn),c.a11y.speak(d.Menus.data.l10n.reorderModeOff),i.attr("aria-hidden","true")),e&&_(this.getMenuItemControls()).each(function(e){e.collapseForm()}))},getMenuItemControls:function(){var t=[],n=this.params.menu_id;return d.control.each(function(e){"nav_menu_item"===e.params.type&&e.setting()&&n===e.setting().nav_menu_term_id&&t.push(e)}),t},reflowMenuItems:function(){var e=this.getMenuItemControls(),a=function(n){var t=[],i=n.currentParent;_.each(n.menuItemControls,function(e){i===e.setting().menu_item_parent&&t.push(e)}),t.sort(function(e,t){return e.setting().position-t.setting().position}),_.each(t,function(t){n.currentAbsolutePosition+=1,t.priority.set(n.currentAbsolutePosition),t.container.hasClass("menu-item-depth-"+String(n.currentDepth))||(_.each(t.container.prop("className").match(/menu-item-depth-\d+/g),function(e){t.container.removeClass(e)}),t.container.addClass("menu-item-depth-"+String(n.currentDepth))),t.container.data("item-depth",n.currentDepth),n.currentDepth+=1,n.currentParent=t.params.menu_item_id,a(n),--n.currentDepth,n.currentParent=i}),t.length&&(_(t).each(function(e){e.container.removeClass("move-up-disabled move-down-disabled move-left-disabled move-right-disabled"),0===n.currentDepth?e.container.addClass("move-left-disabled"):10===n.currentDepth&&e.container.addClass("move-right-disabled")}),t[0].container.addClass("move-up-disabled").addClass("move-right-disabled").toggleClass("move-down-disabled",1===t.length),t[t.length-1].container.addClass("move-down-disabled").toggleClass("move-up-disabled",1===t.length))};a({menuItemControls:e,currentParent:0,currentDepth:0,currentAbsolutePosition:0}),this.container.find(".reorder-toggle").toggle(1<e.length)},debouncedReflowMenuItems:_.debounce(function(){this.reflowMenuItems.apply(this,arguments)},0),addItemToMenu:function(e){var t,n,i,a=0,o=10;return _.each(this.getMenuItemControls(),function(e){!1!==e.setting()&&(o=Math.max(o,e.priority()),0===e.setting().menu_item_parent&&(a=Math.max(a,e.setting().position)))}),a+=1,o+=1,delete(e=l.extend({},d.Menus.data.defaultSettingValues.nav_menu_item,e,{nav_menu_term_id:this.params.menu_id,original_title:e.title,position:a})).id,i=d.Menus.generatePlaceholderAutoIncrementId(),t="nav_menu_item["+String(i)+"]",n={type:"nav_menu_item",transport:d.Menus.data.settingTransport,previewer:d.previewer},(n=d.create(t,t,{},n)).set(e),i=new d.controlConstructor.nav_menu_item(t,{params:{type:"nav_menu_item",content:'<li id="customize-control-nav_menu_item-'+String(i)+'" class="customize-control customize-control-nav_menu_item"></li>',section:this.id,priority:o,active:!0,settings:{"default":t},menu_item_id:i},previewer:d.previewer}),d.control.add(t,i),n.preview(),this.debouncedReflowMenuItems(),c.a11y.speak(d.Menus.data.l10n.itemAdded),i}}),d.Menus.NewMenuControl=d.Control.extend({ready:function(){this._bindHandlers()},_bindHandlers:function(){var t=this,e=l("#customize-control-new_menu_name input"),n=l("#create-new-menu-submit");e.on("keydown",function(e){13===e.which&&t.submit()}),n.on("click",function(e){t.submit(),e.stopPropagation(),e.preventDefault()})},submit:function(){var e,t=this.container.closest(".accordion-section-new-menu").find(".menu-name-field").first(),n=t.val(),i=d.Menus.generatePlaceholderAutoIncrementId();if(!n)return t.addClass("invalid"),void t.focus();e="nav_menu["+String(i)+"]",d.create(e,e,{},{type:"nav_menu",transport:d.Menus.data.settingTransport,previewer:d.previewer}),d(e).set(l.extend({},d.Menus.data.defaultSettingValues.nav_menu,{name:n})),i=new d.Menus.MenuSection(e,{params:{id:e,panel:"nav_menus",title:m(n),customizeAction:d.Menus.data.l10n.customizingMenus,type:"nav_menu",priority:10,menu_id:i}}),d.section.add(e,i),t.val(""),t.removeClass("invalid"),c.a11y.speak(d.Menus.data.l10n.menuAdded),d.section(e).focus(),l("#menu-to-edit").css("margin-top",0)}}),l.extend(d.controlConstructor,{nav_menu_location:d.Menus.MenuLocationControl,nav_menu_item:d.Menus.MenuItemControl,nav_menu:d.Menus.MenuControl,nav_menu_name:d.Menus.MenuNameControl,nav_menu_auto_add:d.Menus.MenuAutoAddControl,new_menu:d.Menus.NewMenuControl}),l.extend(d.panelConstructor,{nav_menus:d.Menus.MenusPanel}),l.extend(d.sectionConstructor,{nav_menu:d.Menus.MenuSection,new_menu:d.Menus.NewMenuSection}),d.bind("ready",function(){d.Menus.availableMenuItemsPanel=new d.Menus.AvailableMenuItemsPanelView({collection:d.Menus.availableMenuItems}),d.bind("saved",function(e){(e.nav_menu_updates||e.nav_menu_item_updates)&&d.Menus.applySavedData(e)}),d.previewer.bind("focus-nav-menu-item-control",d.Menus.focusMenuItemControl)}),d.Menus.applySavedData=function(e){var u={},r={};_(e.nav_menu_updates).each(function(n){var e,t,i,a,o,s,r;if("inserted"===n.status){if(!n.previous_term_id)throw new Error("Expected previous_term_id");if(!n.term_id)throw new Error("Expected term_id");if(s="nav_menu["+String(n.previous_term_id)+"]",!d.has(s))throw new Error("Expected setting to exist: "+s);if(t=d(s),!d.section.has(s))throw new Error("Expected control to exist: "+s);if(o=d.section(s),!(a=t.get()))throw new Error("Did not expect setting to be empty (deleted).");a=l.extend(_.clone(a),n.saved_value),u[n.previous_term_id]=n.term_id,e="nav_menu["+String(n.term_id)+"]",i=d.create(e,e,a,{type:"nav_menu",transport:d.Menus.data.settingTransport,previewer:d.previewer}),o.expanded()&&o.collapse(),a=new d.Menus.MenuSection(e,{params:{id:e,panel:"nav_menus",title:a.name,customizeAction:d.Menus.data.l10n.customizingMenus,type:"nav_menu",priority:o.priority.get(),active:!0,menu_id:n.term_id}}),d.section.add(e,a),d.control.each(function(e){var t;e.extended(d.controlConstructor.widget_form)&&"nav_menu"===e.params.widget_id_base&&(e=(t=e.container.find("select")).find("option[value="+String(n.previous_term_id)+"]"),t.find("option[value="+String(n.term_id)+"]").prop("selected",e.prop("selected")),e.remove())}),t.callbacks.disable(),t.set(!1),t.preview(),i.preview(),t._dirty=!1,o.container.remove(),d.section.remove(s),r=0,d.each(function(e){/^nav_menu\[/.test(e.id)&&!1!==e()&&(r+=1)}),(s=l("#available-widgets-list .widget-tpl:has( input.id_base[ value=nav_menu ] )")).find(".nav-menu-widget-form-controls:first").toggle(0!==r),s.find(".nav-menu-widget-no-menus-message:first").toggle(0===r),s.find("option[value="+String(n.previous_term_id)+"]").remove(),c.customize.control.each(function(e){/^nav_menu_locations\[/.test(e.id)&&e.container.find("option[value="+String(n.previous_term_id)+"]").remove()}),d.each(function(e){var t=d.state("saved").get();/^nav_menu_locations\[/.test(e.id)&&e.get()===n.previous_term_id&&(e.set(n.term_id),e._dirty=!1,d.state("saved").set(t),e.preview())}),o.expanded.get()&&a.expand()}else if("updated"===n.status){if(o="nav_menu["+String(n.term_id)+"]",!d.has(o))throw new Error("Expected setting to exist: "+o);a=d(o),_.isEqual(n.saved_value,a.get())||(o=d.state("saved").get(),a.set(n.saved_value),a._dirty=!1,d.state("saved").set(o))}}),_(e.nav_menu_item_updates).each(function(e){e.previous_post_id&&(r[e.previous_post_id]=e.post_id)}),_(e.nav_menu_item_updates).each(function(e){var t,n,i,a,o,s;if("inserted"===e.status){if(!e.previous_post_id)throw new Error("Expected previous_post_id");if(!e.post_id)throw new Error("Expected post_id");if(t="nav_menu_item["+String(e.previous_post_id)+"]",!d.has(t))throw new Error("Expected setting to exist: "+t);if(i=d(t),!d.control.has(t))throw new Error("Expected control to exist: "+t);if(s=d.control(t),!(o=i.get()))throw new Error("Did not expect setting to be empty (deleted).");if((o=_.clone(o)).menu_item_parent<0){if(!r[o.menu_item_parent])throw new Error("inserted ID for menu_item_parent not available");o.menu_item_parent=r[o.menu_item_parent]}u[o.nav_menu_term_id]&&(o.nav_menu_term_id=u[o.nav_menu_term_id]),n="nav_menu_item["+String(e.post_id)+"]",a=d.create(n,n,o,{type:"nav_menu_item",transport:d.Menus.data.settingTransport,previewer:d.previewer}),e=new d.controlConstructor.nav_menu_item(n,{params:{type:"nav_menu_item",content:'<li id="customize-control-nav_menu_item-'+String(e.post_id)+'" class="customize-control customize-control-nav_menu_item"></li>',menu_id:e.post_id,section:"nav_menu["+String(o.nav_menu_term_id)+"]",priority:s.priority.get(),active:!0,settings:{"default":n},menu_item_id:e.post_id},previewer:d.previewer}),s.container.remove(),d.control.remove(t),d.control.add(n,e),i.callbacks.disable(),i.set(!1),i.preview(),a.preview(),i._dirty=!1,e.container.toggleClass("menu-item-edit-inactive",s.container.hasClass("menu-item-edit-inactive"))}}),_.each(e.widget_nav_menu_updates,function(e,t){t=d(t);t&&(t._value=e,t.preview())})},d.Menus.focusMenuItemControl=function(e){e=d.Menus.getMenuItemControl(e);e&&e.focus()},d.Menus.getMenuControl=function(e){return d.control("nav_menu["+e+"]")},d.Menus.getMenuItemControl=function(e){return d.control("nav_menu_item["+e+"]")}}(wp.customize,wp,jQuery); |