WordPress/wp-includes/js/customize-selective-refresh.min.js
Weston Ruter 1c2324f834 Customize: Update server-sent setting validation notifications as changes are entered.
Send back setting validities with full refreshes and selective refreshes so that invalid settings can have notifications displayed immediately before attempting save, and so that these notifications can be cleared as soon as the input is corrected.

* Splits out JS logic for listing controls into separate methods  `wp.customize.Setting.prototype.findControls()` and `wp.customize.findControlsForSettings()`.
* Adds a `setting` property to the `data` on notifications added to controls that are synced from their settings.
* Adds `selective-refresh-setting-validities` message sent from preview to pane.
* Changes `WP_Customize_Manager::validate_setting_values()` to return when settings are valid as well as invalid.
* Adds `WP_Customize_Manager::prepare_setting_validity_for_js()`.
* Add setting validities to data exported to JS in Customizer Preview and in selective refresh responses.

Fixes #36944.

Built from https://develop.svn.wordpress.org/trunk@37700


git-svn-id: http://core.svn.wordpress.org/trunk@37666 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-06-14 19:17:28 +00:00

1 line
7.6 KiB
JavaScript

wp.customize.selectiveRefresh=function(a,b){"use strict";var c,d,e;return c={ready:a.Deferred(),data:{partials:{},renderQueryVar:"",l10n:{shiftClickToEdit:""},refreshBuffer:250},currentRequest:null},_.extend(c,b.Events),d=c.Partial=b.Class.extend({id:null,initialize:function(b,c){var d=this;c=c||{},d.id=b,d.params=_.extend({selector:null,settings:[],primarySetting:null,containerInclusive:!1,fallbackRefresh:!0},c.params||{}),d.deferred={},d.deferred.ready=a.Deferred(),d.deferred.ready.done(function(){d.ready()})},ready:function(){var b=this;_.each(_.pluck(b.placements(),"container"),function(b){a(b).attr("title",c.data.l10n.shiftClickToEdit)}),a(document).on("click",b.params.selector,function(c){c.shiftKey&&(c.preventDefault(),_.each(b.placements(),function(d){a(d.container).is(c.currentTarget)&&b.showControl()}))})},placements:function(){var b,c=this;return b=c.params.selector||"",b&&(b+=", "),b+='[data-customize-partial-id="'+c.id+'"]',a(b).map(function(){var b,d=a(this);if(b=d.data("customize-partial-placement-context"),_.isString(b)&&"{"===b.substr(0,1))throw new Error("context JSON parse error");return new e({partial:c,container:d,context:b})}).get()},settings:function(){var a=this;return a.params.settings&&0!==a.params.settings.length?a.params.settings:a.params.primarySetting?[a.params.primarySetting]:[a.id]},isRelatedSetting:function(a){var c=this;return _.isString(a)&&(a=b(a)),a?-1!==_.indexOf(c.settings(),a.id):!1},showControl:function(){var a=this,c=a.params.primarySetting;c||(c=_.first(a.settings())),b.preview.send("focus-control-for-setting",c)},preparePlacement:function(b){a(b.container).addClass("customize-partial-refreshing")},_pendingRefreshPromise:null,refresh:function(){var a,b=this;return a=c.requestPartial(b),b._pendingRefreshPromise||(_.each(b.placements(),function(a){b.preparePlacement(a)}),a.done(function(a){_.each(a,function(a){b.renderContent(a)})}),a.fail(function(a,c){b.fallback(a,c)}),b._pendingRefreshPromise=a,a.always(function(){b._pendingRefreshPromise=null})),a},renderContent:function(b){var d,e,f=this;if(!b.container)return f.fallback(new Error("no_container"),[b]),!1;if(b.container=a(b.container),!1===b.addedContent)return f.fallback(new Error("missing_render"),[b]),!1;if(!_.isString(b.addedContent))return f.fallback(new Error("non_string_content"),[b]),!1;c.orginalDocumentWrite=document.write,document.write=function(){throw new Error(c.data.l10n.badDocumentWrite)};try{if(d=b.addedContent,wp.emoji&&wp.emoji.parse&&!a.contains(document.head,b.container[0])&&(d=wp.emoji.parse(d)),f.params.containerInclusive)e=a(d),b.context=_.extend(b.context,e.data("customize-partial-placement-context")||{}),e.data("customize-partial-placement-context",b.context),b.removedNodes=b.container,b.container=e,b.removedNodes.replaceWith(b.container),b.container.attr("title",c.data.l10n.shiftClickToEdit);else{for(b.removedNodes=document.createDocumentFragment();b.container[0].firstChild;)b.removedNodes.appendChild(b.container[0].firstChild);b.container.html(d)}b.container.removeClass("customize-render-content-error")}catch(g){"undefined"!=typeof console&&console.error&&console.error(f.id,g)}return document.write=c.orginalDocumentWrite,c.orginalDocumentWrite=null,b.container.removeClass("customize-partial-refreshing"),b.container.data("customize-partial-content-rendered",!0),c.trigger("partial-content-rendered",b),!0},fallback:function(){var a=this;a.params.fallbackRefresh&&c.requestFullRefresh()}}),c.Placement=e=b.Class.extend({partial:null,container:null,startNode:null,endNode:null,context:null,addedContent:null,removedNodes:null,initialize:function(b){var c=this;if(b=_.extend({},b||{}),!b.partial||!b.partial.extended(d))throw new Error("Missing partial");b.context=b.context||{},b.container&&(b.container=a(b.container)),_.extend(c,b)}}),c.partialConstructor={},c.partial=new b.Values({defaultConstructor:d}),c.getCustomizeQuery=function(){var a={};return b.each(function(b,c){b._dirty&&(a[c]=b())}),{wp_customize:"on",nonce:b.settings.nonce.preview,theme:b.settings.theme.stylesheet,customized:JSON.stringify(a)}},c._pendingPartialRequests={},c._debouncedTimeoutId=null,c._currentRequest=null,c.requestFullRefresh=function(){b.preview.send("refresh")},c.requestPartial=function(d){var f;return c._debouncedTimeoutId&&(clearTimeout(c._debouncedTimeoutId),c._debouncedTimeoutId=null),c._currentRequest&&(c._currentRequest.abort(),c._currentRequest=null),f=c._pendingPartialRequests[d.id],f&&"pending"===f.deferred.state()||(f={deferred:a.Deferred(),partial:d},c._pendingPartialRequests[d.id]=f),d=null,c._debouncedTimeoutId=setTimeout(function(){var a,d,f,g;c._debouncedTimeoutId=null,a=c.getCustomizeQuery(),f={},d={},_.each(c._pendingPartialRequests,function(a,b){f[b]=a.partial.placements(),c.partial.has(b)?d[b]=_.map(f[b],function(a){return a.context||{}}):a.deferred.rejectWith(a.partial,[new Error("partial_removed"),f[b]])}),a.partials=JSON.stringify(d),a[c.data.renderQueryVar]="1",g=c._currentRequest=wp.ajax.send(null,{data:a,url:b.settings.url.self}),g.done(function(a){c.trigger("render-partials-response",a),a.errors&&"undefined"!=typeof console&&console.warn&&_.each(a.errors,function(a){console.warn(a)}),_.each(c._pendingPartialRequests,function(b,c){var d;_.isArray(a.contents[c])?(d=_.map(a.contents[c],function(a,d){var g=f[c][d];return g?g.addedContent=a:g=new e({partial:b.partial,addedContent:a}),g}),b.deferred.resolveWith(b.partial,[d])):b.deferred.rejectWith(b.partial,[new Error("unrecognized_partial"),f[c]])}),c._pendingPartialRequests={}}),g.fail(function(a,b){"abort"!==b&&(_.each(c._pendingPartialRequests,function(b,c){b.deferred.rejectWith(b.partial,[a,f[c]])}),c._pendingPartialRequests={})})},c.data.refreshBuffer),f.deferred.promise()},c.addPartials=function(b,d){var f;b||(b=document.documentElement),b=a(b),d=_.extend({triggerRendered:!0},d||{}),f=b.find("[data-customize-partial-id]"),b.is("[data-customize-partial-id]")&&(f=f.add(b)),f.each(function(){var b,f,g,h,i,j=a(this);f=j.data("customize-partial-id"),f&&(i=j.data("customize-partial-placement-context")||{},b=c.partial(f),b||(h=j.data("customize-partial-options")||{},h.constructingContainerContext=j.data("customize-partial-placement-context")||{},g=c.partialConstructor[j.data("customize-partial-type")]||c.Partial,b=new g(f,h),c.partial.add(b.id,b)),d.triggerRendered&&!j.data("customize-partial-content-rendered")&&c.trigger("partial-content-rendered",new e({partial:b,context:i,container:j})),j.data("customize-partial-content-rendered",!0))})},b.bind("preview-ready",function(){var d,e,f;document.head||(document.head=a("head:first")[0]),_.extend(c.data,_customizePartialRefreshExports),_.each(c.data.partials,function(a,b){var d,e=c.partial(b);e?_.extend(e.params,a):(d=c.partialConstructor[a.type]||c.Partial,e=new d(b,{params:a}),c.partial.add(b,e))}),d=function(a,b){var d=this;c.partial.each(function(c){c.isRelatedSetting(d,a,b)&&c.refresh()})},e=function(a){d.call(a,a(),null),a.bind(d)},f=function(a){d.call(a,null,a()),a.unbind(d)},b.bind("add",e),b.bind("remove",f),b.each(function(a){a.bind(d)}),c.addPartials(document.documentElement,{triggerRendered:!1}),"undefined"!=typeof MutationObserver&&(c.mutationObserver=new MutationObserver(function(b){_.each(b,function(b){c.addPartials(a(b.target))})}),c.mutationObserver.observe(document.documentElement,{childList:!0,subtree:!0})),b.selectiveRefresh.bind("partial-content-rendered",function(a){a.container&&c.addPartials(a.container)}),b.selectiveRefresh.bind("render-partials-response",function(a){a.setting_validities&&b.preview.send("selective-refresh-setting-validities",a.setting_validities)}),b.preview.bind("active",function(){c.partial.each(function(a){a.deferred.ready.resolve()}),c.partial.bind("add",function(a){a.deferred.ready.resolve()})})}),c}(jQuery,wp.customize);