WordPress/wp-includes/js/customize-preview-widgets.m...

1 line
7.3 KiB
JavaScript

wp.customize.widgetsPreview=wp.customize.WidgetCustomizerPreview=function(d,o,c){var s={renderedSidebars:{},renderedWidgets:{},registeredSidebars:[],registeredWidgets:{},widgetSelectors:[],preview:null,l10n:{widgetTooltip:""},selectiveRefreshableWidgets:{},init:function(){var e=this;e.preview=c.preview,o.isEmpty(e.selectiveRefreshableWidgets)||e.addPartials(),e.buildWidgetSelectors(),e.highlightControls(),e.preview.bind("highlight-widget",e.highlightWidget),c.preview.bind("active",function(){e.highlightControls()})}};return s.WidgetPartial=c.selectiveRefresh.Partial.extend({initialize:function(e,t){var i=this,r=e.match(/^widget\[(.+)]$/);if(!r)throw new Error("Illegal id for widget partial.");i.widgetId=r[1],i.widgetIdParts=s.parseWidgetId(i.widgetId),(t=t||{}).params=o.extend({settings:[s.getWidgetSettingId(i.widgetId)],containerInclusive:!0},t.params||{}),c.selectiveRefresh.Partial.prototype.initialize.call(i,e,t)},refresh:function(){var e,t=this;return s.selectiveRefreshableWidgets[t.widgetIdParts.idBase]?c.selectiveRefresh.Partial.prototype.refresh.call(t):((e=d.Deferred()).reject(),t.fallback(),e.promise())},renderContent:function(e){var t=this;c.selectiveRefresh.Partial.prototype.renderContent.call(t,e)&&(c.preview.send("widget-updated",t.widgetId),c.selectiveRefresh.trigger("widget-updated",t))}}),s.SidebarPartial=c.selectiveRefresh.Partial.extend({initialize:function(e,t){var i=this,r=e.match(/^sidebar\[(.+)]$/);if(!r)throw new Error("Illegal id for sidebar partial.");if(i.sidebarId=r[1],(t=t||{}).params=o.extend({settings:["sidebars_widgets["+i.sidebarId+"]"]},t.params||{}),c.selectiveRefresh.Partial.prototype.initialize.call(i,e,t),!i.params.sidebarArgs)throw new Error("The sidebarArgs param was not provided.");if(1<i.params.settings.length)throw new Error("Expected SidebarPartial to only have one associated setting")},ready:function(){var i=this;o.each(i.settings(),function(e){c(e).bind(o.bind(i.handleSettingChange,i))}),c.selectiveRefresh.bind("partial-content-rendered",function(e){e.partial.extended(s.WidgetPartial)&&-1!==o.indexOf(i.getWidgetIds(),e.partial.widgetId)&&c.selectiveRefresh.trigger("sidebar-updated",i)}),c.bind("change",function(e){var t=s.parseWidgetSettingId(e.id);t&&(e=t.idBase,t.number&&(e+="-"+String(t.number)),-1!==o.indexOf(i.getWidgetIds(),e)&&i.ensureWidgetPlacementContainers(e))})},findDynamicSidebarBoundaryNodes:function(){var i=this,r={},n=/^(dynamic_sidebar_before|dynamic_sidebar_after):(.+):(\d+)$/,a=function(e){o.each(e,function(e){var t;8===e.nodeType?(t=e.nodeValue.match(n))&&t[2]===i.sidebarId&&(o.isUndefined(r[t[3]])&&(r[t[3]]={before:null,after:null,instanceNumber:parseInt(t[3],10)}),"dynamic_sidebar_before"===t[1]?r[t[3]].before=e:r[t[3]].after=e):1===e.nodeType&&a(e.childNodes)})};return a(document.body.childNodes),o.values(r)},placements:function(){var t=this;return o.map(t.findDynamicSidebarBoundaryNodes(),function(e){return new c.selectiveRefresh.Placement({partial:t,container:null,startNode:e.before,endNode:e.after,context:{instanceNumber:e.instanceNumber}})})},getWidgetIds:function(){var e=this.settings()[0];if(!e)throw new Error("Missing associated setting.");if(!c.has(e))throw new Error("Setting does not exist.");if(e=c(e).get(),!o.isArray(e))throw new Error("Expected setting to be array of widget IDs");return e.slice(0)},reflowWidgets:function(){var e=this,t=[],i=e.getWidgetIds(),r=e.placements(),s={};return o.each(i,function(e){var t=c.selectiveRefresh.partial("widget["+e+"]");t&&(s[e]=t)}),o.each(r,function(i){var r,n=[],a=!1,d=-1;o.each(s,function(t){o.each(t.placements(),function(e){i.context.instanceNumber===e.context.sidebar_instance_number&&(r=e.container.index(),n.push({partial:t,placement:e,position:r}),r<d&&(a=!0),d=r)})}),a&&(o.each(n,function(e){i.endNode.parentNode.insertBefore(e.placement.container[0],i.endNode),c.selectiveRefresh.trigger("partial-content-moved",e.placement)}),t.push(i))}),0<t.length&&c.selectiveRefresh.trigger("sidebar-updated",e),t},ensureWidgetPlacementContainers:function(i){var r=this,n=!1,e="widget["+i+"]",a=c.selectiveRefresh.partial(e);return a||(a=new s.WidgetPartial(e,{params:{}}),c.selectiveRefresh.partial.add(a.id,a)),o.each(r.placements(),function(t){var e;o.find(a.placements(),function(e){return e.context.sidebar_instance_number===t.context.instanceNumber})||(e=d(r.params.sidebarArgs.before_widget.replace(/%1\$s/g,i).replace(/%2\$s/g,"widget")+r.params.sidebarArgs.after_widget))[0]&&(e.attr("data-customize-partial-id",a.id),e.attr("data-customize-partial-type","widget"),e.attr("data-customize-widget-id",i),e.data("customize-partial-placement-context",{sidebar_id:r.sidebarId,sidebar_instance_number:t.context.instanceNumber}),t.endNode.parentNode.insertBefore(e[0],t.endNode),n=!0)}),n&&r.reflowWidgets(),a},handleSettingChange:function(e,t){var i,r=this,n=[];0<t.length&&0===e.length||0<e.length&&0===t.length?r.fallback():(i=o.difference(t,e),o.each(i,function(e){e=c.selectiveRefresh.partial("widget["+e+"]");e&&o.each(e.placements(),function(e){(e.context.sidebar_id===r.sidebarId||e.context.sidebar_args&&e.context.sidebar_args.id===r.sidebarId)&&e.container.remove()})}),t=o.difference(e,t),o.each(t,function(e){e=r.ensureWidgetPlacementContainers(e);n.push(e)}),o.each(n,function(e){e.refresh()}),c.selectiveRefresh.trigger("sidebar-updated",r))},refresh:function(){var e=this,t=d.Deferred();return t.fail(function(){e.fallback()}),0===e.placements().length?t.reject():(o.each(e.reflowWidgets(),function(e){c.selectiveRefresh.trigger("partial-content-rendered",e)}),t.resolve()),t.promise()}}),c.selectiveRefresh.partialConstructor.sidebar=s.SidebarPartial,c.selectiveRefresh.partialConstructor.widget=s.WidgetPartial,s.addPartials=function(){o.each(s.registeredSidebars,function(e){var t="sidebar["+e.id+"]",i=c.selectiveRefresh.partial(t);i||(i=new s.SidebarPartial(t,{params:{sidebarArgs:e}}),c.selectiveRefresh.partial.add(i.id,i))})},s.buildWidgetSelectors=function(){var r=this;d.each(r.registeredSidebars,function(e,t){var i=[t.before_widget,t.before_title,t.after_title,t.after_widget].join(""),t=d(i),i=t.prop("tagName")||"",t=t.prop("className")||"";t&&(i+="."+(t=(t=t.replace(/\S*%[12]\$s\S*/g,"")).replace(/^\s+|\s+$/g,"")).split(/\s+/).join("."),r.widgetSelectors.push(i))})},s.highlightWidget=function(e){var t=d(document.body),i=d("#"+e);t.find(".widget-customizer-highlighted-widget").removeClass("widget-customizer-highlighted-widget"),i.addClass("widget-customizer-highlighted-widget"),setTimeout(function(){i.removeClass("widget-customizer-highlighted-widget")},500)},s.highlightControls=function(){var t=this,e=this.widgetSelectors.join(",");d(e).attr("title",this.l10n.widgetTooltip),d(document).on("mouseenter",e,function(){t.preview.send("highlight-widget-control",d(this).prop("id"))}),d(document).on("click",e,function(e){e.shiftKey&&(e.preventDefault(),t.preview.send("focus-widget-control",d(this).prop("id")))})},s.parseWidgetId=function(e){var t={idBase:"",number:null},i=e.match(/^(.+)-(\d+)$/);return i?(t.idBase=i[1],t.number=parseInt(i[2],10)):t.idBase=e,t},s.parseWidgetSettingId=function(e){var t={idBase:"",number:null},e=e.match(/^widget_([^\[]+?)(?:\[(\d+)])?$/);return e?(t.idBase=e[1],e[2]&&(t.number=parseInt(e[2],10)),t):null},s.getWidgetSettingId=function(e){var t=this.parseWidgetId(e),e="widget_"+t.idBase;return t.number&&(e+="["+String(t.number)+"]"),e},c.bind("preview-ready",function(){d.extend(s,_wpWidgetCustomizerPreviewSettings),s.init()}),s}(jQuery,_,(wp,wp.customize));