wp.customize.selectiveRefresh=(function($,api){'use strict';var self,Partial,Placement;self={ready:$.Deferred(),editShortcutVisibility:new api.Value(),data:{partials:{},renderQueryVar:'',l10n:{shiftClickToEdit:''}},currentRequest:null};_.extend(self,api.Events);Partial=self.Partial=api.Class.extend({id:null,defaults:{selector:null,primarySetting:null,containerInclusive:false,fallbackRefresh:true},initialize:function(id,options){var partial=this;options=options||{};partial.id=id;partial.params=_.extend({settings:[]},partial.defaults,options.params||options);partial.deferred={};partial.deferred.ready=$.Deferred();partial.deferred.ready.done(function(){partial.ready();});},ready:function(){var partial=this;_.each(partial.placements(),function(placement){$(placement.container).attr('title',self.data.l10n.shiftClickToEdit);partial.createEditShortcutForPlacement(placement);});$(document).on('click',partial.params.selector,function(e){if(!e.shiftKey){return;}
e.preventDefault();_.each(partial.placements(),function(placement){if($(placement.container).is(e.currentTarget)){partial.showControl();}});});},createEditShortcutForPlacement:function(placement){var partial=this,$shortcut,$placementContainer,illegalAncestorSelector,illegalContainerSelector;if(!placement.container){return;}
$placementContainer=$(placement.container);illegalAncestorSelector='head';illegalContainerSelector='area, audio, base, bdi, bdo, br, button, canvas, col, colgroup, command, datalist, embed, head, hr, html, iframe, img, input, keygen, label, link, map, math, menu, meta, noscript, object, optgroup, option, param, progress, rp, rt, ruby, script, select, source, style, svg, table, tbody, textarea, tfoot, thead, title, tr, track, video, wbr';if(!$placementContainer.length||$placementContainer.is(illegalContainerSelector)||$placementContainer.closest(illegalAncestorSelector).length){return;}
$shortcut=partial.createEditShortcut();$shortcut.on('click',function(event){event.preventDefault();event.stopPropagation();partial.showControl();});partial.addEditShortcutToPlacement(placement,$shortcut);},addEditShortcutToPlacement:function(placement,$editShortcut){var $placementContainer=$(placement.container);$placementContainer.prepend($editShortcut);if(!$placementContainer.is(':visible')||'none'===$placementContainer.css('display')){$editShortcut.addClass('customize-partial-edit-shortcut-hidden');}},getEditShortcutClassName:function(){var partial=this,cleanId;cleanId=partial.id.replace(/]/g,'').replace(/\[/g,'-');return'customize-partial-edit-shortcut-'+cleanId;},getEditShortcutTitle:function(){var partial=this,l10n=self.data.l10n;switch(partial.getType()){case'widget':return l10n.clickEditWidget;case'blogname':return l10n.clickEditTitle;case'blogdescription':return l10n.clickEditTitle;case'nav_menu':return l10n.clickEditMenu;default:return l10n.clickEditMisc;}},getType:function(){var partial=this,settingId;settingId=partial.params.primarySetting||_.first(partial.settings())||'unknown';if(partial.params.type){return partial.params.type;}
if(settingId.match(/^nav_menu_instance\[/)){return'nav_menu';}
if(settingId.match(/^widget_.+\[\d+]$/)){return'widget';}
return settingId;},createEditShortcut:function(){var partial=this,shortcutTitle,$buttonContainer,$button,$image;shortcutTitle=partial.getEditShortcutTitle();$buttonContainer=$('<span>',{'class':'customize-partial-edit-shortcut '+partial.getEditShortcutClassName()});$button=$('<button>',{'aria-label':shortcutTitle,'title':shortcutTitle,'class':'customize-partial-edit-shortcut-button'});$image=$('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M13.89 3.39l2.71 2.72c.46.46.42 1.24.03 1.64l-8.01 8.02-5.56 1.16 1.16-5.58s7.6-7.63 7.99-8.03c.39-.39 1.22-.39 1.68.07zm-2.73 2.79l-5.59 5.61 1.11 1.11 5.54-5.65zm-2.97 8.23l5.58-5.6-1.07-1.08-5.59 5.6z"/></svg>');$button.append($image);$buttonContainer.append($button);return $buttonContainer;},placements:function(){var partial=this,selector;selector=partial.params.selector||'';if(selector){selector+=', ';}
selector+='[data-customize-partial-id="'+partial.id+'"]';return $(selector).map(function(){var container=$(this),context;context=container.data('customize-partial-placement-context');if(_.isString(context)&&'{'===context.substr(0,1)){throw new Error('context JSON parse error');}
return new Placement({partial:partial,container:container,context:context});}).get();},settings:function(){var partial=this;if(partial.params.settings&&0!==partial.params.settings.length){return partial.params.settings;}else if(partial.params.primarySetting){return[partial.params.primarySetting];}else{return[partial.id];}},isRelatedSetting:function(setting){var partial=this;if(_.isString(setting)){setting=api(setting);}
if(!setting){return false;}
return-1!==_.indexOf(partial.settings(),setting.id);},showControl:function(){var partial=this,settingId=partial.params.primarySetting;if(!settingId){settingId=_.first(partial.settings());}
if(partial.getType()==='nav_menu'){if(partial.params.navMenuArgs.theme_location){settingId='nav_menu_locations['+partial.params.navMenuArgs.theme_location+']';}else if(partial.params.navMenuArgs.menu){settingId='nav_menu['+String(partial.params.navMenuArgs.menu)+']';}}
api.preview.send('focus-control-for-setting',settingId);},preparePlacement:function(placement){$(placement.container).addClass('customize-partial-refreshing');},_pendingRefreshPromise:null,refresh:function(){var partial=this,refreshPromise;refreshPromise=self.requestPartial(partial);if(!partial._pendingRefreshPromise){_.each(partial.placements(),function(placement){partial.preparePlacement(placement);});refreshPromise.done(function(placements){_.each(placements,function(placement){partial.renderContent(placement);});});refreshPromise.fail(function(data,placements){partial.fallback(data,placements);});partial._pendingRefreshPromise=refreshPromise;refreshPromise.always(function(){partial._pendingRefreshPromise=null;});}
return refreshPromise;},renderContent:function(placement){var partial=this,content,newContainerElement;if(!placement.container){partial.fallback(new Error('no_container'),[placement]);return false;}
placement.container=$(placement.container);if(false===placement.addedContent){partial.fallback(new Error('missing_render'),[placement]);return false;}
if(!_.isString(placement.addedContent)){partial.fallback(new Error('non_string_content'),[placement]);return false;}
self.originalDocumentWrite=document.write;document.write=function(){throw new Error(self.data.l10n.badDocumentWrite);};try{content=placement.addedContent;if(wp.emoji&&wp.emoji.parse&&!$.contains(document.head,placement.container[0])){content=wp.emoji.parse(content);}
if(partial.params.containerInclusive){newContainerElement=$(content);placement.context=_.extend(placement.context,newContainerElement.data('customize-partial-placement-context')||{});newContainerElement.data('customize-partial-placement-context',placement.context);placement.removedNodes=placement.container;placement.container=newContainerElement;placement.removedNodes.replaceWith(placement.container);placement.container.attr('title',self.data.l10n.shiftClickToEdit);}else{placement.removedNodes=document.createDocumentFragment();while(placement.container[0].firstChild){placement.removedNodes.appendChild(placement.container[0].firstChild);}
placement.container.html(content);}
placement.container.removeClass('customize-render-content-error');}catch(error){if('undefined'!==typeof console&&console.error){console.error(partial.id,error);}
partial.fallback(error,[placement]);}
document.write=self.originalDocumentWrite;self.originalDocumentWrite=null;partial.createEditShortcutForPlacement(placement);placement.container.removeClass('customize-partial-refreshing');placement.container.data('customize-partial-content-rendered',true);if(wp.mediaelement){wp.mediaelement.initialize();}
if(wp.playlist){wp.playlist.initialize();}
self.trigger('partial-content-rendered',placement);return true;},fallback:function(){var partial=this;if(partial.params.fallbackRefresh){self.requestFullRefresh();}}});self.Placement=Placement=api.Class.extend({partial:null,container:null,startNode:null,endNode:null,context:null,addedContent:null,removedNodes:null,initialize:function(args){var placement=this;args=_.extend({},args||{});if(!args.partial||!args.partial.extended(Partial)){throw new Error('Missing partial');}
args.context=args.context||{};if(args.container){args.container=$(args.container);}
_.extend(placement,args);}});self.partialConstructor={};self.partial=new api.Values({defaultConstructor:Partial});self.getCustomizeQuery=function(){var dirtyCustomized={};api.each(function(value,key){if(value._dirty){dirtyCustomized[key]=value();}});return{wp_customize:'on',nonce:api.settings.nonce.preview,customize_theme:api.settings.theme.stylesheet,customized:JSON.stringify(dirtyCustomized),customize_changeset_uuid:api.settings.changeset.uuid};};self._pendingPartialRequests={};self._debouncedTimeoutId=null;self._currentRequest=null;self.requestFullRefresh=function(){api.preview.send('refresh');};self.requestPartial=function(partial){var partialRequest;if(self._debouncedTimeoutId){clearTimeout(self._debouncedTimeoutId);self._debouncedTimeoutId=null;}
if(self._currentRequest){self._currentRequest.abort();self._currentRequest=null;}
partialRequest=self._pendingPartialRequests[partial.id];if(!partialRequest||'pending'!==partialRequest.deferred.state()){partialRequest={deferred:$.Deferred(),partial:partial};self._pendingPartialRequests[partial.id]=partialRequest;}
partial=null;self._debouncedTimeoutId=setTimeout(function(){var data,partialPlacementContexts,partialsPlacements,request;self._debouncedTimeoutId=null;data=self.getCustomizeQuery();partialsPlacements={};partialPlacementContexts={};_.each(self._pendingPartialRequests,function(pending,partialId){partialsPlacements[partialId]=pending.partial.placements();if(!self.partial.has(partialId)){pending.deferred.rejectWith(pending.partial,[new Error('partial_removed'),partialsPlacements[partialId]]);}else{partialPlacementContexts[partialId]=_.map(partialsPlacements[partialId],function(placement){return placement.context||{};});}});data.partials=JSON.stringify(partialPlacementContexts);data[self.data.renderQueryVar]='1';request=self._currentRequest=wp.ajax.send(null,{data:data,url:api.settings.url.self});request.done(function(data){self.trigger('render-partials-response',data);if(data.errors&&'undefined'!==typeof console&&console.warn){_.each(data.errors,function(error){console.warn(error);});}
_.each(self._pendingPartialRequests,function(pending,partialId){var placementsContents;if(!_.isArray(data.contents[partialId])){pending.deferred.rejectWith(pending.partial,[new Error('unrecognized_partial'),partialsPlacements[partialId]]);}else{placementsContents=_.map(data.contents[partialId],function(content,i){var partialPlacement=partialsPlacements[partialId][i];if(partialPlacement){partialPlacement.addedContent=content;}else{partialPlacement=new Placement({partial:pending.partial,addedContent:content});}
return partialPlacement;});pending.deferred.resolveWith(pending.partial,[placementsContents]);}});self._pendingPartialRequests={};});request.fail(function(data,statusText){if('abort'===statusText){return;}
_.each(self._pendingPartialRequests,function(pending,partialId){pending.deferred.rejectWith(pending.partial,[data,partialsPlacements[partialId]]);});self._pendingPartialRequests={};});},api.settings.timeouts.selectiveRefresh);return partialRequest.deferred.promise();};self.addPartials=function(rootElement,options){var containerElements;if(!rootElement){rootElement=document.documentElement;}
rootElement=$(rootElement);options=_.extend({triggerRendered:true},options||{});containerElements=rootElement.find('[data-customize-partial-id]');if(rootElement.is('[data-customize-partial-id]')){containerElements=containerElements.add(rootElement);}
containerElements.each(function(){var containerElement=$(this),partial,placement,id,Constructor,partialOptions,containerContext;id=containerElement.data('customize-partial-id');if(!id){return;}
containerContext=containerElement.data('customize-partial-placement-context')||{};partial=self.partial(id);if(!partial){partialOptions=containerElement.data('customize-partial-options')||{};partialOptions.constructingContainerContext=containerElement.data('customize-partial-placement-context')||{};Constructor=self.partialConstructor[containerElement.data('customize-partial-type')]||self.Partial;partial=new Constructor(id,partialOptions);self.partial.add(partial);}
if(options.triggerRendered&&!containerElement.data('customize-partial-content-rendered')){placement=new Placement({partial:partial,context:containerContext,container:containerElement});$(placement.container).attr('title',self.data.l10n.shiftClickToEdit);partial.createEditShortcutForPlacement(placement);self.trigger('partial-content-rendered',placement);}
containerElement.data('customize-partial-content-rendered',true);});};api.bind('preview-ready',function(){var handleSettingChange,watchSettingChange,unwatchSettingChange;_.extend(self.data,_customizePartialRefreshExports);_.each(self.data.partials,function(data,id){var Constructor,partial=self.partial(id);if(!partial){Constructor=self.partialConstructor[data.type]||self.Partial;partial=new Constructor(id,_.extend({params:data},data));self.partial.add(partial);}else{_.extend(partial.params,data);}});handleSettingChange=function(newValue,oldValue){var setting=this;self.partial.each(function(partial){if(partial.isRelatedSetting(setting,newValue,oldValue)){partial.refresh();}});};watchSettingChange=function(setting){handleSettingChange.call(setting,setting(),null);setting.bind(handleSettingChange);};unwatchSettingChange=function(setting){handleSettingChange.call(setting,null,setting());setting.unbind(handleSettingChange);};api.bind('add',watchSettingChange);api.bind('remove',unwatchSettingChange);api.each(function(setting){setting.bind(handleSettingChange);});self.addPartials(document.documentElement,{triggerRendered:false});if('undefined'!==typeof MutationObserver){self.mutationObserver=new MutationObserver(function(mutations){_.each(mutations,function(mutation){self.addPartials($(mutation.target));});});self.mutationObserver.observe(document.documentElement,{childList:true,subtree:true});}
api.selectiveRefresh.bind('partial-content-rendered',function(placement){if(placement.container){self.addPartials(placement.container);}});api.selectiveRefresh.bind('render-partials-response',function handleSettingValiditiesResponse(data){if(data.setting_validities){api.preview.send('selective-refresh-setting-validities',data.setting_validities);}});api.preview.bind('edit-shortcut-visibility',function(visibility){api.selectiveRefresh.editShortcutVisibility.set(visibility);});api.selectiveRefresh.editShortcutVisibility.bind(function(visibility){var body=$(document.body),shouldAnimateHide;shouldAnimateHide=('hidden'===visibility&&body.hasClass('customize-partial-edit-shortcuts-shown')&&!body.hasClass('customize-partial-edit-shortcuts-hidden'));body.toggleClass('customize-partial-edit-shortcuts-hidden',shouldAnimateHide);body.toggleClass('customize-partial-edit-shortcuts-shown','visible'===visibility);});api.preview.bind('active',function(){self.partial.each(function(partial){partial.deferred.ready.resolve();});self.partial.bind('add',function(partial){partial.deferred.ready.resolve();});});});return self;}(jQuery,wp.customize));