mirror of
https://github.com/WordPress/WordPress.git
synced 2024-09-05 12:21:15 +02:00
props adamsilverstein. see #27485. Built from https://develop.svn.wordpress.org/trunk@27654 git-svn-id: http://core.svn.wordpress.org/trunk@27497 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 line
24 KiB
JavaScript
1 line
24 KiB
JavaScript
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={update_widget_ajax_action:null,update_widget_nonce_value:null,update_widget_nonce_post_key: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.showFirstSidebarIfRequested(),this.availableWidgetsPanel.setup()},wp.customize.bind("ready",function(){j.init()}),j.showFirstSidebarIfRequested=function(){if(/widget-customizer=open/.test(location.search)){var b=function(){j.registered_sidebars.off("change:is_rendered",b);var c,d=j.registered_sidebars.find(function(a){return a.get("is_rendered")});d&&(c=a("#accordion-section-sidebar-widgets-"+d.get("id")),c.hasClass("open")||c.find(".accordion-section-title").trigger("click"),c[0].scrollIntoView())};b=_.debounce(b,100),j.registered_sidebars.on("change:is_rendered",b)}},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===_.indexOf(a.id,"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("<li></li>"),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===_.indexOf(a.id,"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=this;f.params.is_wide&&(b=f.container.find(".widget-inside"),c=a(".wp-full-overlay-sidebar-content:first"),f.container.addClass("wide-widget-control"),f.container.find(".widget-content:first").css({"min-width":f.params.width,"min-height":f.params.height}),d=function(){var c,d,e,g=f.container.offset().top;c=b.outerHeight(),d=Math.max(g,0),e=a(window).height()-c,d=Math.min(d,e),b.css("top",d)},e=a("#customize-theme-controls"),f.container.on("expand",function(){c.on("scroll",d),a(window).on("resize",d),e.on("expanded collapsed",d),d()}),f.container.on("collapsed",function(){c.off("scroll",d),e.off("expanded collapsed",d),a(window).off("resize",d)}),wp.customize.each(function(a){0===_.indexOf(a.id,"sidebars_widgets[")&&a.bind(function(){f.container.hasClass("expanded")&&d()})}))},_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.highlightPreviewWidget()}),a.setting.bind(function(){a.scrollPreviewWidgetIntoView(),a.highlightPreviewWidget()}),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=j.update_widget_ajax_action,g.wp_customize="on",g[j.update_widget_nonce_post_key]=j.update_widget_nonce_value,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("<div>"+c.data.form+"</div>"),m.hook("formUpdate",h),k=h.find(":input, option"),l=m._getInputsSignature(i)===m._getInputsSignature(k),l?(i.each(function(c){var d,f,g=a(this),h=a(k[c]),i=m._getInputStatePropertyName(this);d=g.data("state"+e),f=h.prop(i),g.data("sanitized",f),d!==f?((b.ignore_active_element||!g.is(document.activeElement))&&g.prop(i,f),m.hook("unsanitaryField",g,f,d)):m.hook("sanitaryField",g,d)}),m.hook("formUpdated",h)):(f.html(h.html()),n&&a(document.getElementById(n)).prop({selectionStart:o,selectionEnd:p}).focus(),m.hook("formRefreshed")),q=_(m.setting()).isEqual(c.data.instance),q||(m.is_widget_updating=!0,m.setting(c.data.instance),m.is_widget_updating=!1),d&&d.call(m,null,{no_change:q,ajax_finished:!0})):(g=j.i18n.error,c.data&&c.data.message&&(g=c.data.message),d?d.call(m,g):f.prepend('<p class="widget-error"><strong>'+g+"</strong></p>")))}),l.fail(function(a,b){d&&d.call(m,b)}),l.always(function(){m.container.removeClass("previewer-loading"),m.container.removeClass("widget-form-loading"),i.each(function(){a(this).removeData("state"+e)}),k(k()-1)})},expandControlSection:function(){var a=this.container.closest(".accordion-section");a.hasClass("open")||a.find(".accordion-section-title:first").trigger("click")},expandForm:function(){this.toggleForm(!0)},collapseForm:function(){this.toggleForm(!1)},toggleForm:function(a){var b,c,d,e=this;b=e.container.find("div.widget:first"),c=b.find(".widget-inside:first"),"undefined"==typeof a&&(a=!c.is(":visible")),c.is(":visible")!==a&&(a?(wp.customize.control.each(function(a){e.params.type===a.params.type&&e!==a&&a.collapseForm()}),e.container.trigger("expand"),e.container.addClass("expanding"),d=function(){e.container.removeClass("expanding"),e.container.addClass("expanded"),e.container.trigger("expanded")},e.params.is_wide?c.animate({width:"show"},"fast",d):c.slideDown("fast",d)):(e.container.trigger("collapse"),e.container.addClass("collapsing"),d=function(){e.container.removeClass("collapsing"),e.container.removeClass("expanded"),e.container.trigger("collapsed")},e.params.is_wide?c.animate({width:"hide"},"fast",d):c.slideUp("fast",function(){b.css({width:"",margin:""}),d()})))},focus:function(){var a=this;a.expandControlSection(),a.expandForm(),a.container.find(":focusable:first").focus().trigger("click")},getWidgetSidebarPosition:function(){var a,b,c=this;if(a=c.getSidebarWidgetsControl().setting(),b=_.indexOf(a,c.params.widget_id),-1===b)throw new Error("Widget was unexpectedly not present in the sidebar.");return b},moveUp:function(){this._moveWidgetByOne(-1)},moveDown:function(){this._moveWidgetByOne(1)},_moveWidgetByOne:function(a){var b,c,d,e,f=this;b=f.getWidgetSidebarPosition(),c=f.getSidebarWidgetsControl().setting,d=Array.prototype.slice.call(c()),e=d[b+a],d[b+a]=f.params.widget_id,d[b]=e,c(d)},toggleWidgetMoveArea:function(b){var c,d=this;c=d.container.find(".move-widget-area"),"undefined"==typeof b&&(b=!c.hasClass("active")),b&&(c.find(".selected").removeClass("selected"),c.find("li").filter(function(){return a(this).data("id")===d.params.sidebar_id}).addClass("selected"),d.container.find(".move-widget-btn").prop("disabled",!0)),c.toggleClass("active",b)},getPreviewWidgetElement:function(){var a=this,b=j.getPreviewWindow().wp.customize.WidgetCustomizerPreview;return b.getWidgetElement(a.params.widget_id)},scrollPreviewWidgetIntoView:function(){},highlightSectionAndControl:function(){var b,c=this;b=c.container.is(":hidden")?c.container.closest(".control-section"):c.container,a(".widget-customizer-highlighted").removeClass("widget-customizer-highlighted"),b.addClass("widget-customizer-highlighted"),setTimeout(function(){b.removeClass("widget-customizer-highlighted")},500)},highlightPreviewWidget:function(){var a,b,c=this;a=c.getPreviewWidgetElement(),b=a.closest("html"),b.find(".widget-customizer-highlighted-widget").removeClass("widget-customizer-highlighted-widget"),a.addClass("widget-customizer-highlighted-widget"),setTimeout(function(){a.removeClass("widget-customizer-highlighted-widget")},500)}}),h=wp.customize.Previewer,wp.customize.Previewer=h.extend({initialize:function(a,b){j.previewer=this,h.prototype.initialize.call(this,a,b),this.bind("refresh",this.refresh)}}),j.getSidebarWidgetControlContainingWidget=function(a){var b=null;return wp.customize.control.each(function(c){"sidebar_widgets"===c.params.type&&-1!==_.indexOf(c.setting(),a)&&(b=c)}),b},j.getWidgetFormControlForWidget=function(a){var b=null;return wp.customize.control.each(function(c){"widget_form"===c.params.type&&c.params.widget_id===a&&(b=c)}),b},j.getPreviewWindow=function(){return a("#customize-preview").find("iframe").prop("contentWindow")},j.availableWidgetsPanel={active_sidebar_widgets_control:null,selected_widget_tpl:null,container:null,filter_input:null,setup:function(){var b=this;b.container=a("#available-widgets"),b.filter_input=a("#available-widgets-filter").find("input"),j.available_widgets.on("change update",b.update_available_widgets_list),b.update_available_widgets_list(),a("#customize-controls").on("click keydown",function(c){var d=a(c.target).is(".add-new-widget, .add-new-widget *");a("body").hasClass("adding-widget")&&!d&&b.close()}),j.previewer.bind("url",function(){b.close()}),b.container.find(".widget-tpl").on("click keypress",function(a){("keypress"!==a.type||13===a.which||32===a.which)&&b.submit(this)}),b.filter_input.on("input keyup change",function(a){var c;j.available_widgets.doSearch(a.target.value),b.selected_widget_tpl&&!b.selected_widget_tpl.is(":visible")&&(b.selected_widget_tpl.removeClass("selected"),b.selected_widget_tpl=null),b.selected_widget_tpl&&!a.target.value&&(b.selected_widget_tpl.removeClass("selected"),b.selected_widget_tpl=null),!b.selected_widget_tpl&&a.target.value&&(c=b.container.find("> .widget-tpl:visible:first"),c.length&&b.select(c))}),b.container.find(" > .widget-tpl").on("focus",function(){b.select(this)}),b.container.on("keydown",function(c){var d=13===c.which,e=27===c.which,f=40===c.which,g=38===c.which,h=null,i=b.container.find("> .widget-tpl:visible:first"),j=b.container.find("> .widget-tpl:visible:last"),k=a(c.target).is(b.filter_input);return f||g?(f?k?h=i:b.selected_widget_tpl&&0!==b.selected_widget_tpl.nextAll(".widget-tpl:visible").length&&(h=b.selected_widget_tpl.nextAll(".widget-tpl:visible:first")):g&&(k?h=j:b.selected_widget_tpl&&0!==b.selected_widget_tpl.prevAll(".widget-tpl:visible").length&&(h=b.selected_widget_tpl.prevAll(".widget-tpl:visible:first"))),b.select(h),void(h?h.focus():b.filter_input.focus())):void((!d||b.filter_input.val())&&(d?b.submit():e&&b.close({return_focus:!0})))})},update_available_widgets_list:function(){var b=j.availableWidgetsPanel;b.container.find(".widget-tpl").hide(),j.available_widgets.each(function(c){var d=a("#widget-tpl-"+c.id);d.toggle(!c.get("is_disabled")),c.get("is_disabled")&&d.is(b.selected_widget_tpl)&&(b.selected_widget_tpl=null)})},select:function(b){var c=this;c.selected_widget_tpl=a(b),c.selected_widget_tpl.siblings(".widget-tpl").removeClass("selected"),c.selected_widget_tpl.addClass("selected")},submit:function(b){var c,d,e=this;if(b||(b=e.selected_widget_tpl),b&&e.active_sidebar_widgets_control){if(e.select(b),c=a(e.selected_widget_tpl).data("widget-id"),d=j.available_widgets.findWhere({id:c}),!d)throw new Error("Widget unexpectedly not found.");e.active_sidebar_widgets_control.addWidget(d.get("id_base")),e.close()}},open:function(b){var c=this;c.active_sidebar_widgets_control=b,_(b.getWidgetFormControls()).each(function(a){a.params.is_wide&&a.collapseForm()}),a("body").addClass("adding-widget"),c.container.find(".widget-tpl").removeClass("selected"),j.available_widgets.doSearch(""),c.filter_input.focus()},close:function(b){var c=this;b=b||{},b.return_focus&&c.active_sidebar_widgets_control&&c.active_sidebar_widgets_control.container.find(".add-new-widget").focus(),c.active_sidebar_widgets_control=null,c.selected_widget_tpl=null,a("body").removeClass("adding-widget"),c.filter_input.val("")}},j}(jQuery); |