var WidgetCustomizer=function(a){"use strict";function b(a){var b,c={number:null,id_base:null};return b=a.match(/^(.+)-(\d+)$/),b?(c.id_base=b[1],c.number=parseInt(b[2],10)):c.id_base=a,c}function c(a){var c,d=b(a);return c="widget_"+d.id_base,d.number&&(c+="["+d.number+"]"),c}var d,e,f,g,h,i=wp.customize,j={nonce:null,i18n:{save_btn_label:"",save_btn_tooltip:"",remove_btn_label:"",remove_btn_tooltip:"",error:""},available_widgets:[],registered_widgets:[],active_sidebar_control:null,previewer:null,saved_widget_ids:{},registered_sidebars:[],tpl:{move_widget_area:"",widget_reorder_nav:""}};return a.extend(j,WidgetCustomizer_exports),"undefined"==typeof window.ajaxurl&&(window.ajaxurl=wp.ajax.settings.url),a("#customize-theme-controls").closest("div:not([id])").attr("id","widgets-right"),d=j.Widget=Backbone.Model.extend({id:null,temp_id:null,classname:null,control_tpl:null,description:null,is_disabled:null,is_multi:null,multi_number:null,name:null,id_base:null,transport:"refresh",params:[],width:null,height:null}),e=j.WidgetCollection=Backbone.Collection.extend({model:d,doSearch:function(a){this.terms!==a&&(this.terms=a,this.terms.length>0&&this.search(this.terms),""===this.terms&&this.reset(WidgetCustomizer_exports.available_widgets),this.trigger("update"))},search:function(a){var b,c,d;this.reset(WidgetCustomizer_exports.available_widgets,{silent:!0}),a=a.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&"),a=a.replace(/ /g,")(?=.*"),b=new RegExp("^(?=.*"+a+").+","i"),c=this.filter(function(a){return d=_.union(a.get("name"),a.get("id"),a.get("description")),b.test(d)}),this.reset(c)}}),j.available_widgets=new e(j.available_widgets),f=j.Sidebar=Backbone.Model.extend({after_title:null,after_widget:null,before_title:null,before_widget:null,"class":null,description:null,id:null,name:null,is_rendered:!1}),g=j.SidebarCollection=Backbone.Collection.extend({model:f}),j.registered_sidebars=new g(j.registered_sidebars),j.init=function(){this.availableWidgetsPanel.setup(),this.previewer.bind("highlight-widget-control",j.highlightWidgetFormControl),this.previewer.bind("focus-widget-control",j.focusWidgetFormControl)},wp.customize.bind("ready",function(){j.init()}),i.controlConstructor.sidebar_widgets=i.Control.extend({ready:function(){var a=this;a.control_section=a.container.closest(".control-section"),a.section_content=a.container.closest(".accordion-section-content"),a._setupModel(),a._setupSortable(),a._setupAddition(),a._applyCardinalOrderClassNames()},_setupModel:function(){var c=this,d=j.registered_sidebars.get(c.params.sidebar_id);c.setting.bind(function(d,e){var f,g,h,i=_(e).difference(d);d=_(d).filter(function(a){var c=b(a);return!!j.available_widgets.findWhere({id_base:c.id_base})}),f=_(d).map(function(a){var b=j.getWidgetFormControlForWidget(a);return b||(b=c.addWidget(a)),b}),f.sort(function(a,b){var c=_.indexOf(d,a.params.widget_id),e=_.indexOf(d,b.params.widget_id);return c===e?0:e>c?-1:1}),g=c.section_content.find(".customize-control-sidebar_widgets"),h=_(f).map(function(a){return a.container[0]}),g.before(h),c._applyCardinalOrderClassNames(),_(f).each(function(a){a.params.sidebar_id=c.params.sidebar_id}),_(i).each(function(d){setTimeout(function(){var e,f,g,h,i,k=!1;wp.customize.each(function(a){if(a.id!==c.setting.id&&0===a.id.indexOf("sidebars_widgets[")&&"sidebars_widgets[wp_inactive_widgets]"!==a.id){var b,e=a();b=_.indexOf(e,d),-1!==b&&(k=!0)}}),k||(e=j.getWidgetFormControlForWidget(d),f=e&&a.contains(document,e.container[0])&&!a.contains(c.section_content[0],e.container[0]),e&&!f&&(wp.customize.control.remove(e.id),e.container.remove()),j.saved_widget_ids[d]&&(g=wp.customize.value("sidebars_widgets[wp_inactive_widgets]")().slice(),g.push(d),wp.customize.value("sidebars_widgets[wp_inactive_widgets]")(_(g).unique())),h=b(d).id_base,i=j.available_widgets.findWhere({id_base:h}),i&&!i.get("is_multi")&&i.set("is_disabled",!1))})})}),j.previewer.bind("rendered-sidebars",function(a){var b=!!a[c.params.sidebar_id];d.set("is_rendered",b)}),d.on("change:is_rendered",function(){var b,c="#accordion-section-sidebar-widgets-"+this.get("id");b=a(c),this.get("is_rendered")?b.stop().slideDown(function(){a(this).css("height","auto")}):(b.hasClass("open")&&b.find(".accordion-section-title").trigger("click"),b.stop().slideUp())})},_setupSortable:function(){var b=this;b.is_reordering=!1,b.section_content.sortable({items:"> .customize-control-widget_form",handle:".widget-top",axis:"y",connectWith:".accordion-section-content:has(.customize-control-sidebar_widgets)",update:function(){var c,d=b.section_content.sortable("toArray");c=a.map(d,function(b){return a("#"+b).find(":input[name=widget-id]").val()}),b.setting(c)}}),b.control_section.find(".accordion-section-title").droppable({accept:".customize-control-widget_form",over:function(){b.control_section.hasClass("open")||(b.control_section.addClass("open"),b.section_content.toggle(!1).slideToggle(150,function(){b.section_content.sortable("refreshPositions")}))}}),b.container.find(".reorder-toggle").on("click keydown",function(a){("keydown"!==a.type||13===a.which||32===a.which)&&b.toggleReordering(!b.is_reordering)})},_setupAddition:function(){var b=this;b.container.find(".add-new-widget").on("click keydown",function(c){("keydown"!==c.type||13===c.which||32===c.which)&&(b.section_content.hasClass("reordering")||(a("body").hasClass("adding-widget")?j.availableWidgetsPanel.close():j.availableWidgetsPanel.open(b)))})},_applyCardinalOrderClassNames:function(){var a=this;a.section_content.find(".customize-control-widget_form").removeClass("first-widget").removeClass("last-widget").find(".move-widget-down, .move-widget-up").prop("tabIndex",0),a.section_content.find(".customize-control-widget_form:first").addClass("first-widget").find(".move-widget-up").prop("tabIndex",-1),a.section_content.find(".customize-control-widget_form:last").addClass("last-widget").find(".move-widget-down").prop("tabIndex",-1)},toggleReordering:function(a){var b=this;a=Boolean(a),a!==b.section_content.hasClass("reordering")&&(b.is_reordering=a,b.section_content.toggleClass("reordering",a),a&&_(b.getWidgetFormControls()).each(function(a){a.collapseForm()}))},getWidgetFormControls:function(){var a,b=this;return a=_(b.setting()).map(function(a){var b=c(a),d=i.control(b);if(!d)throw new Error("Unable to find widget_form control for "+a);return d})},addWidget:function(c){var d,e,f,g,h,i,k,l,m=this,n="widget_form",o=b(c),p=o.number,q=o.id_base,r=j.available_widgets.findWhere({id_base:q});if(!r)throw new Error("Widget unexpectedly not found.");if(p&&!r.get("is_multi"))throw new Error("Did not expect a widget number to be supplied for a non-multi widget");return r.get("is_multi")&&!p&&(r.set("multi_number",r.get("multi_number")+1),p=r.get("multi_number")),d=a("#widget-tpl-"+r.get("id")).html(),r.get("is_multi")?d=d.replace(/<[^<>]+>/g,function(a){return a.replace(/__i__|%i%/g,p)}):r.set("is_disabled",!0),e=a("
"),e.addClass("customize-control"),e.addClass("customize-control-"+n),e.append(a(d)),e.find("> .widget-icon").remove(),r.get("is_multi")&&(e.find('input[name="widget_number"]').val(p),e.find('input[name="multi_number"]').val(p)),c=e.find('[name="widget-id"]').val(),e.hide(),f="widget_"+r.get("id_base"),r.get("is_multi")&&(f+="["+p+"]"),e.attr("id","customize-control-"+f.replace(/\]/g,"").replace(/\[/g,"-")),m.container.after(e),g=wp.customize.has(f),g||(l={transport:"refresh",previewer:m.setting.previewer},wp.customize.create(f,f,{},l)),h=wp.customize.controlConstructor[n],i=new h(f,{params:{settings:{"default":f},sidebar_id:m.params.sidebar_id,widget_id:c,widget_id_base:r.get("id_base"),type:n,is_new:!g,width:r.get("width"),height:r.get("height"),is_wide:r.get("is_wide")},previewer:m.setting.previewer}),wp.customize.control.add(f,i),wp.customize.each(function(a){if(a.id!==m.setting.id&&0===a.id.indexOf("sidebars_widgets[")){var b,d=a().slice();b=_.indexOf(d,c),-1!==b&&(d.splice(b),a(d))}}),k=m.setting().slice(),-1===_.indexOf(k,c)&&(k.push(c),m.setting(k)),e.slideDown(function(){g?(i.expandForm(),i.updateWidget({instance:i.setting(),complete:function(a){if(a)throw a;i.focus()}})):i.focus()}),i}}),i.controlConstructor.widget_form=i.Control.extend({ready:function(){var a=this;a._setupModel(),a._setupWideWidget(),a._setupControlToggle(),a._setupWidgetTitle(),a._setupReorderUI(),a._setupHighlightEffects(),a._setupUpdateUI(),a._setupRemoveUI(),a.hook("init")},hooks:{_default:{},rss:{formUpdated:function(a){var b=this,c=b.container.find(".widget-error:first"),d=a.find(".widget-error:first");c.length&&d.length?c.replaceWith(d):c.length?c.remove():d.length&&b.container.find(".widget-content").prepend(d)}}},hook:function(a){var b,c=Array.prototype.slice.call(arguments,1);this.hooks[this.params.widget_id_base]&&this.hooks[this.params.widget_id_base][a]?b=this.hooks[this.params.widget_id_base][a]:this.hooks._default[a]&&(b=this.hooks._default[a]),b&&b.apply(this,c)},_setupModel:function(){var a,b=this;a=function(){j.saved_widget_ids[b.params.widget_id]=!0},wp.customize.bind("ready",a),wp.customize.bind("saved",a),b._update_count=0,b.is_widget_updating=!1,b.setting.bind(function(a,c){_(c).isEqual(a)||b.is_widget_updating||b.updateWidget({instance:a})})},_setupWideWidget:function(){var b,c,d,e,f,g=this;g.params.is_wide&&(b=g.container.find(".widget-inside"),c=b.find("> .form"),d=a(".wp-full-overlay-sidebar-content:first"),g.container.addClass("wide-widget-control"),g.container.find(".widget-content:first").css({"max-width":g.params.width,"min-height":g.params.height}),e=function(){var d,e=g.container.offset().top,f=a(window).height(),h=c.outerHeight();b.css("max-height",f),d=Math.max(0,Math.min(Math.max(e,0),f-h)),b.css("top",d)},f=a("#customize-theme-controls"),g.container.on("expand",function(){e(),d.on("scroll",e),a(window).on("resize",e),f.on("expanded collapsed",e)}),g.container.on("collapsed",function(){d.off("scroll",e),a(window).off("resize",e),f.off("expanded collapsed",e)}),wp.customize.each(function(a){0===a.id.indexOf("sidebars_widgets[")&&a.bind(function(){g.container.hasClass("expanded")&&e()})}))},_setupControlToggle:function(){var a,b=this;b.container.find(".widget-top").on("click",function(a){a.preventDefault();var c=b.getSidebarWidgetsControl();c.is_reordering||b.toggleForm()}),a=b.container.find(".widget-control-close"),a.on("click",function(a){a.preventDefault(),b.collapseForm(),b.container.find(".widget-top .widget-action:first").focus()})},_setupWidgetTitle:function(){var a,b=this;a=function(){var a=b.setting().title,c=b.container.find(".in-widget-title");c.text(a?": "+a:"")},b.setting.bind(a),a()},_setupReorderUI:function(){var b,c,d,e,f=this;b=function(a){a.siblings(".selected").removeClass("selected"),a.addClass("selected");var b=a.data("id")===f.params.sidebar_id;f.container.find(".move-widget-btn").prop("disabled",b)},f.container.find(".widget-title-action").after(a(j.tpl.widget_reorder_nav)),c=a(_.template(j.tpl.move_widget_area,{sidebars:_(j.registered_sidebars.toArray()).pluck("attributes")})),f.container.find(".widget-top").after(c),e=function(){var d,e=c.find("li");d=e.filter(function(){return a(this).data("id")===f.params.sidebar_id}),e.each(function(){var c,e,f=a(this);c=f.data("id"),e=j.registered_sidebars.get(c),f.toggle(e.get("is_rendered")),f.hasClass("selected")&&!e.get("is_rendered")&&b(d)})},e(),j.registered_sidebars.on("change:is_rendered",e),d=f.container.find(".widget-reorder-nav"),d.find(".move-widget, .move-widget-down, .move-widget-up").on("click keypress",function(b){if("keypress"!==b.type||13===b.which||32===b.which)if(a(this).focus(),a(this).is(".move-widget"))f.toggleWidgetMoveArea();else{var c=a(this).is(".move-widget-down"),d=a(this).is(".move-widget-up"),e=f.getWidgetSidebarPosition();if(d&&0===e||c&&e===f.getSidebarWidgetsControl().setting().length-1)return;d?f.moveUp():f.moveDown(),a(this).focus()}}),f.container.find(".widget-area-select").on("click keypress","li",function(c){("keypress"!==event.type||13===event.which||32===event.which)&&(c.preventDefault(),b(a(this)))}),f.container.find(".move-widget-btn").click(function(){f.getSidebarWidgetsControl().toggleReordering(!1);var a,b,c,d,e,g=f.params.sidebar_id,h=f.container.find(".widget-area-select li.selected").data("id");a=i("sidebars_widgets["+g+"]"),b=i("sidebars_widgets["+h+"]"),c=Array.prototype.slice.call(a()),d=Array.prototype.slice.call(b()),e=f.getWidgetSidebarPosition(),c.splice(e,1),d.push(f.params.widget_id),a(c),b(d),f.focus()})},_setupHighlightEffects:function(){var a=this;a.container.on("mouseenter click",function(){a.setting.previewer.send("highlight-widget",a.params.widget_id)}),a.setting.bind(function(){a.setting.previewer.send("highlight-widget",a.params.widget_id)}),a.container.on("expand",function(){a.scrollPreviewWidgetIntoView()})},_setupUpdateUI:function(){var a,b,c,d=this;a=d.container.find(".widget-content"),b=d.container.find(".widget-control-save"),b.val(j.i18n.save_btn_label),b.attr("title",j.i18n.save_btn_tooltip),b.removeClass("button-primary").addClass("button-secondary"),b.on("click",function(a){a.preventDefault(),d.updateWidget()}),c=_.debounce(function(){d.updateWidget()},250),d.container.find(".widget-content").on("keydown","input",function(a){13===a.which&&(a.preventDefault(),d.updateWidget({ignore_active_element:!0}))}),a.on("change input propertychange",":input",function(a){"change"===a.type?d.updateWidget():this.checkValidity&&this.checkValidity()&&c()}),d.setting.previewer.channel.bind("synced",function(){d.container.removeClass("previewer-loading")}),j.previewer.bind("widget-updated",function(a){a===d.params.widget_id&&d.container.removeClass("previewer-loading")}),j.previewer.bind("rendered-widgets",function(a){var b=!!a[d.params.widget_id];d.container.toggleClass("widget-rendered",b)})},_setupRemoveUI:function(){var a,b,c=this;a=c.container.find("a.widget-control-remove"),a.on("click",function(a){a.preventDefault();var b;b=c.container.next().is(".customize-control-widget_form")?c.container.next().find(".widget-action:first"):c.container.prev().is(".customize-control-widget_form")?c.container.prev().find(".widget-action:first"):c.container.next(".customize-control-sidebar_widgets").find(".add-new-widget:first"),c.container.slideUp(function(){var a,d,e=j.getSidebarWidgetControlContainingWidget(c.params.widget_id);if(!e)throw new Error("Unable to find sidebars_widgets_control");if(a=e.setting().slice(),d=_.indexOf(a,c.params.widget_id),-1===d)throw new Error("Widget is not in sidebar");a.splice(d,1),e.setting(a),b.focus()})}),b=function(){a.text(j.i18n.remove_btn_label),a.attr("title",j.i18n.remove_btn_tooltip)},c.params.is_new?wp.customize.bind("saved",b):b()},_getInputsSignature:function(b){var c=_(b).map(function(b){b=a(b);var c;return c=b.is("option")?[b.prop("nodeName"),b.prop("value")]:b.is(":checkbox, :radio")?[b.prop("type"),b.attr("id"),b.attr("name"),b.prop("value")]:[b.prop("nodeName"),b.attr("id"),b.attr("name"),b.attr("type")],c.join(",")});return c.join(";")},_getInputStatePropertyName:function(b){return b=a(b),b.is(":radio, :checkbox")?"checked":b.is("option")?"selected":"value"},getSidebarWidgetsControl:function(){var a,b,c=this;if(a="sidebars_widgets["+c.params.sidebar_id+"]",b=i.control(a),!b)throw new Error("Unable to locate sidebar_widgets control for "+c.params.sidebar_id);return b},updateWidget:function(b){var c,d,e,f,g,h,i,k,l,m=this,n=null,o=null,p=null;if(b=a.extend({instance:null,complete:null,ignore_active_element:!1},b),c=b.instance,d=b.complete,m._update_count+=1,e=m._update_count,f=m.container.find(".widget-content"),f.find(".widget-error").remove(),a.contains(m.container[0],document.activeElement)&&a(document.activeElement).is("[id]")){n=a(document.activeElement).prop("id");try{o=document.activeElement.selectionStart,p=document.activeElement.selectionEnd}catch(q){}}m.container.addClass("widget-form-loading"),m.container.addClass("previewer-loading"),k=wp.customize.state("processing"),k(k()+1),g={},g.action="update-widget",g.wp_customize="on",g.nonce=j.nonce,h=a.param(g),i=f.find(":input, option"),i.each(function(){var b=a(this),c=m._getInputStatePropertyName(this);b.data("state"+e,b.prop(c))}),h+=c?"&"+a.param({sanitized_widget_setting:JSON.stringify(c)}):"&"+i.serialize(),h+="&"+f.find("~ :input").serialize(),l=a.post(wp.ajax.settings.url,h,function(c){var g,h,k,l,q;return"0"===c?(j.previewer.preview.iframe.hide(),void j.previewer.login().done(function(){m.updateWidget(b),j.previewer.preview.iframe.show()})):"-1"===c?void j.previewer.cheatin():void(c.success?(h=a("