1 line
7.6 KiB
JavaScript
1 line
7.6 KiB
JavaScript
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._debouncedTimeoutId=null,s._currentRequest=null,s.requestFullRefresh=function(){r.preview.send("refresh")},s.requestPartial=function(e){var t;return s._debouncedTimeoutId&&(clearTimeout(s._debouncedTimeoutId),s._debouncedTimeoutId=null),s._currentRequest&&(s._currentRequest.abort(),s._currentRequest=null),(t=s._pendingPartialRequests[e.id])&&"pending"===t.deferred.state()||(t={deferred:o.Deferred(),partial:e},s._pendingPartialRequests[e.id]=t),e=null,s._debouncedTimeoutId=setTimeout(function(){var n,a,e;s._debouncedTimeoutId=null,e=s.getCustomizeQuery(),a={},n={},_.each(s._pendingPartialRequests,function(e,t){a[t]=e.partial.placements(),s.partial.has(t)?n[t]=_.map(a[t],function(e){return e.context||{}}):e.deferred.rejectWith(e.partial,[new Error("partial_removed"),a[t]])}),e.partials=JSON.stringify(n),e[s.data.renderQueryVar]="1",(e=s._currentRequest=wp.ajax.send(null,{data:e,url:r.settings.url.self})).done(function(t){s.trigger("render-partials-response",t),t.errors&&"undefined"!=typeof console&&console.warn&&_.each(t.errors,function(e){console.warn(e)}),_.each(s._pendingPartialRequests,function(n,r){var e;_.isArray(t.contents[r])?(e=_.map(t.contents[r],function(e,t){t=a[r][t];return t?t.addedContent=e:t=new d({partial:n.partial,addedContent:e}),t}),n.deferred.resolveWith(n.partial,[e])):n.deferred.rejectWith(n.partial,[new Error("unrecognized_partial"),a[r]])}),s._pendingPartialRequests={}}),e.fail(function(n,e){"abort"!==e&&(_.each(s._pendingPartialRequests,function(e,t){e.deferred.rejectWith(e.partial,[n,a[t]])}),s._pendingPartialRequests={})})},s.data.refreshBuffer),t.deferred.promise()},s.addPartials=function(e,i){var t;e=e||document.documentElement,e=o(e),i=_.extend({triggerRendered:!0},i||{}),t=e.find("[data-customize-partial-id]"),(t=e.is("[data-customize-partial-id]")?t.add(e):t).each(function(){var e,t,n,r=o(this),a=r.data("customize-partial-id");a&&(n=r.data("customize-partial-placement-context")||{},(e=s.partial(a))||((t=r.data("customize-partial-options")||{}).constructingContainerContext=r.data("customize-partial-placement-context")||{},e=new(s.partialConstructor[r.data("customize-partial-type")]||s.Partial)(a,t),s.partial.add(e.id,e)),i.triggerRendered&&!r.data("customize-partial-content-rendered")&&s.trigger("partial-content-rendered",new d({partial:e,context:n,container:r})),r.data("customize-partial-content-rendered",!0))})},r.bind("preview-ready",function(){var t,e,n;document.head||(document.head=o("head:first")[0]),_.extend(s.data,_customizePartialRefreshExports),_.each(s.data.partials,function(e,t){var n=s.partial(t);n?_.extend(n.params,e):(n=new(s.partialConstructor[e.type]||s.Partial)(t,{params:e}),s.partial.add(t,n))}),t=function(t,n){var r=this;s.partial.each(function(e){e.isRelatedSetting(r,t,n)&&e.refresh()})},e=function(e){t.call(e,e(),null),e.bind(t)},n=function(e){t.call(e,null,e()),e.unbind(t)},r.bind("add",e),r.bind("remove",n),r.each(function(e){e.bind(t)}),s.addPartials(document.documentElement,{triggerRendered:!1}),"undefined"!=typeof MutationObserver&&(s.mutationObserver=new MutationObserver(function(e){_.each(e,function(e){s.addPartials(o(e.target))})}),s.mutationObserver.observe(document.documentElement,{childList:!0,subtree:!0})),r.selectiveRefresh.bind("partial-content-rendered",function(e){e.container&&s.addPartials(e.container)}),r.selectiveRefresh.bind("render-partials-response",function(e){e.setting_validities&&r.preview.send("selective-refresh-setting-validities",e.setting_validities)}),r.preview.bind("active",function(){s.partial.each(function(e){e.deferred.ready.resolve()}),s.partial.bind("add",function(e){e.deferred.ready.resolve()})})}),s}(jQuery,wp.customize); |