WordPress/wp-includes/js/customize-selective-refresh...

1 line
7.6 KiB
JavaScript
Raw Permalink Normal View History

wp.customize.selectiveRefresh=function(o,r){"use strict";var t,d,s={ready:o.Deferred(),data:{partials:{},renderQueryVar:"",l10n:{shiftClickToEdit:""},refreshBuffer:250},currentRequest:null};return _.extend(s,r.Events),t=s.Partial=r.Class.extend({id:null,initialize:function(e,t){var n=this;t=t||{},n.id=e,n.params=_.extend({selector:null,settings:[],primarySetting:null,containerInclusive:!1,fallbackRefresh:!0},t.params||{}),n.deferred={},n.deferred.ready=o.Deferred(),n.deferred.ready.done(function(){n.ready()})},ready:function(){var n=this;_.each(_.pluck(n.placements(),"container"),function(e){o(e).attr("title",s.data.l10n.shiftClickToEdit)}),o(document).on("click",n.params.selector,function(t){t.shiftKey&&(t.preventDefault(),_.each(n.placements(),function(e){o(e.container).is(t.currentTarget)&&n.showControl()}))})},placements:function(){var n=this,e=n.params.selector||"";return e&&(e+=", "),e+='[data-customize-partial-id="'+n.id+'"]',o(e).map(function(){var e=o(this),t=e.data("customize-partial-placement-context");if(_.isString(t)&&"{"===t.substr(0,1))throw new Error("context JSON parse error");return new d({partial:n,container:e,context:t})}).get()},settings:function(){var e=this;return e.params.settings&&0!==e.params.settings.length?e.params.settings:e.params.primarySetting?[e.params.primarySetting]:[e.id]},isRelatedSetting:function(e){return!!(e=_.isString(e)?r(e):e)&&-1!==_.indexOf(this.settings(),e.id)},showControl:function(){var e=(e=this.params.primarySetting)||_.first(this.settings());r.preview.send("focus-control-for-setting",e)},preparePlacement:function(e){o(e.container).addClass("customize-partial-refreshing")},_pendingRefreshPromise:null,refresh:function(){var n=this,e=s.requestPartial(n);return n._pendingRefreshPromise||(_.each(n.placements(),function(e){n.preparePlacement(e)}),e.done(function(e){_.each(e,function(e){n.renderContent(e)})}),e.fail(function(e,t){n.fallback(e,t)}),(n._pendingRefreshPromise=e).always(function(){n._pendingRefreshPromise=null})),e},renderContent:function(e){var t,n,r=this;if(!e.container)return r.fallback(new Error("no_container"),[e]),!1;if(e.container=o(e.container),!1===e.addedContent)return r.fallback(new Error("missing_render"),[e]),!1;if(!_.isString(e.addedContent))return r.fallback(new Error("non_string_content"),[e]),!1;s.orginalDocumentWrite=document.write,document.write=function(){throw new Error(s.data.l10n.badDocumentWrite)};try{if(t=e.addedContent,wp.emoji&&wp.emoji.parse&&!o.contains(document.head,e.container[0])&&(t=wp.emoji.parse(t)),r.params.containerInclusive)n=o(t),e.context=_.extend(e.context,n.data("customize-partial-placement-context")||{}),n.data("customize-partial-placement-context",e.context),e.removedNodes=e.container,e.container=n,e.removedNodes.replaceWith(e.container),e.container.attr("title",s.data.l10n.shiftClickToEdit);else{for(e.removedNodes=document.createDocumentFragment();e.container[0].firstChild;)e.removedNodes.appendChild(e.container[0].firstChild);e.container.html(t)}e.container.removeClass("customize-render-content-error")}catch(e){"undefined"!=typeof console&&console.error&&console.error(r.id,e)}return document.write=s.orginalDocumentWrite,s.orginalDocumentWrite=null,e.container.removeClass("customize-partial-refreshing"),e.container.data("customize-partial-content-rendered",!0),s.trigger("partial-content-rendered",e),!0},fallback:function(){this.params.fallbackRefresh&&s.requestFullRefresh()}}),s.Placement=d=r.Class.extend({partial:null,container:null,startNode:null,endNode:null,context:null,addedContent:null,removedNodes:null,initialize:function(e){if(!(e=_.extend({},e||{})).partial||!e.partial.extended(t))throw new Error("Missing partial");e.context=e.context||{},e.container&&(e.container=o(e.container)),_.extend(this,e)}}),s.partialConstructor={},s.partial=new r.Values({defaultConstructor:t}),s.getCustomizeQuery=function(){var n={};return r.each(function(e,t){e._dirty&&(n[t]=e())}),{wp_customize:"on",nonce:r.settings.nonce.preview,theme:r.settings.theme.stylesheet,customized:JSON.stringify(n)}},s._pendingPartialRequests={},s._debounce