WordPress/wp-includes/js/customize-preview-nav-menus...

1 line
3.4 KiB
JavaScript
Raw Normal View History

wp.customize.menusPreview=function(a,b){"use strict";var c={},d=200,e={},f={renderQueryVar:null,renderNonceValue:null,renderNoncePostKey:null,previewCustomizeNonce:null,requestUri:"/",theme:{active:!1,stylesheet:""},navMenuInstanceArgs:{}},g={};return b.bind("preview-ready",function(){b.preview.bind("active",function(){g.init()})}),g.init=function(){var a=this,c={};e=_.extend({},f),"undefined"!=typeof _wpCustomizePreviewNavMenusExports&&_.extend(e,_wpCustomizePreviewNavMenusExports),b.each(function(b,d){b.id=d,c[b.id]=!0,a.bindListener(b)}),b.preview.bind("setting",function(d){var e,f,g;d=d.slice(),e=d.shift(),f=d.shift(),g=b(e),g||(g=b.create(e,f)),g.id||(g.id=e),c[g.id]||(c[g.id]=!0,a.bindListener(g)&&g.callbacks.fireWith(g,[g(),null]))})},g.bindListener=function(a){var b,c;return(b=a.id.match(/^nav_menu\[(-?\d+)]$/))?(a.navMenuId=parseInt(b[1],10),a.bind(g.onChangeNavMenuSetting),!0):(b=a.id.match(/^nav_menu_item\[(-?\d+)]$/))?(a.navMenuItemId=parseInt(b[1],10),a.bind(g.onChangeNavMenuItemSetting),!0):(b=a.id.match(/^nav_menu_locations\[(.+?)]/),b?(c=b[1],a.bind(function(){g.refreshMenuLocation(c)}),!0):!1)},g.onChangeNavMenuSetting=function(){var a=this;if(!a.navMenuId)throw new Error("Expected navMenuId property to be set.");g.refreshMenu(a.navMenuId)},g.onChangeNavMenuItemSetting=function(a,b){!b||!b.nav_menu_term_id||a&&b.nav_menu_term_id===a.nav_menu_term_id||g.refreshMenu(b.nav_menu_term_id),a&&a.nav_menu_term_id&&g.refreshMenu(a.nav_menu_term_id)},g.refreshMenu=function(a){var c=[];b.each(function(b,d){var e=d.match(/^nav_menu_locations\[(.+?)]/);e&&a===b()&&c.push(e[1])}),_.each(e.navMenuInstanceArgs,function(b,d){(a===b.menu||-1!==_.indexOf(c,b.theme_location))&&this.refreshMenuInstanceDebounced(d)},this)},g.refreshMenuLocation=function(a){var c=!1;_.each(e.navMenuInstanceArgs,function(b,d){a===b.theme_location&&(this.refreshMenuInstanceDebounced(d),c=!0)},this),c||b.preview.send("refresh")},g.refreshMenuInstance=function(c){var d,f,g,h,i,j,k,l;if(!e.navMenuInstanceArgs[c])throw new Error("unknown_instance_number");return k=e.navMenuInstanceArgs[c],l="partial-refreshable-nav-menu-"+String(c),h=a("."+l),_.isNumber(k.menu)?f=k.menu:k.theme_location&&b.has("nav_menu_locations["+k.theme_location+"]")&&(f=b("nav_menu_locations["+k.theme_location+"]").get()),f&&k.can_partial_refresh&&0!==h.length?(f=parseInt(f,10),d={nonce:e.previewCustomizeNonce,wp_customize:"on"},e.theme.active||(d.theme=e.theme.stylesheet),d[e.renderQueryVar]="1",g={},b.each(function(a,b){var c=a.get(),d=!1;d=d||/^nav_menu_locations\[/.test(b),d=d||b==="nav_menu["+String(f)+"]",d=d||/^nav_menu_item\[/.test(b)&&(!1===c||f===c.nav_menu_term_id),d&&(g[b]=c)}),d.customized=JSON.stringify(g),d[e.renderNoncePostKey]=e.renderNonceValue,j=a.extend({},k),d.wp_nav_menu_args_hash=j.args_hash,delete j.args_hash,d.wp_nav_menu_args=JSON.stringify(j),h.addClass("customize-partial-refreshing"),i=wp.ajax.send(null,{data:d,url:e.requestUri}),void i.done(function(d){if(!1===d)return void b.preview.send("refresh");var e,f=h;h=a(d),h.addClass(l),h.addClass("partial-refreshable-nav-menu customize-partial-refreshing"),f.replaceWith(h),e={instanceNumber:c,wpNavArgs:j,oldContainer:f,newContainer:h},h.removeClass("customize-partial-refreshing"),a(document).trigger("customize-preview-menu-refreshed",[e])})):void b.preview.send("refresh")},g.refreshMenuInstanceDebounced=function(a){c[a]&&clearTimeout(c[a]),c[a]=setTimeout(function(){g.refreshMenuInstance(a)},d)},g}(jQuery,wp.customize);