mirror of
https://github.com/WordPress/WordPress.git
synced 2024-11-18 00:25:37 +01:00
01c0b48822
Fixes #27298. Built from https://develop.svn.wordpress.org/trunk@27436 git-svn-id: http://core.svn.wordpress.org/trunk@27283 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:""},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}),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=d.indexOf(a.params.widget_id),e=d.indexOf(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=e.indexOf(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===a.id.indexOf("sidebars_widgets[")){var b,d=a().slice();b=d.indexOf(c),-1!==b&&(d.splice(b),a(d))}}),k=m.setting().slice(),-1===k.indexOf(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===a.id.indexOf("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||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=a.indexOf(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=this,l=null,m=null,n=null,o={};if(b=a.extend({instance:null,complete:null,ignore_active_element:!1},b),c=b.instance,d=b.complete,k._update_count+=1,e=k._update_count,f=k.container.find(".widget-content"),a.contains(k.container[0],document.activeElement)&&a(document.activeElement).is("[id]")){l=a(document.activeElement).prop("id");try{m=document.activeElement.selectionStart,n=document.activeElement.selectionEnd}catch(p){}}k.container.addClass("widget-form-loading"),k.container.addClass("previewer-loading"),o={},o.action=j.update_widget_ajax_action,o[j.update_widget_nonce_post_key]=j.update_widget_nonce_value,g=a.param(o),h=f.find(":input, option"),h.each(function(){var b=a(this),c=k._getInputStatePropertyName(this);b.data("state"+e,b.prop(c))}),g+=c?"&"+a.param({sanitized_widget_setting:JSON.stringify(c)}):"&"+h.serialize(),g+="&"+f.find("~ :input").serialize(),window.console&&window.console.log(wp.ajax.settings.url,g),i=a.post(wp.ajax.settings.url,g,function(c){var g,i,j,o,p;if(c.success)i=a("<div>"+c.data.form+"</div>"),k.hook("formUpdate",i),j=i.find(":input, option"),o=k._getInputsSignature(h)===k._getInputsSignature(j),o?(h.each(function(c){var d,f,g=a(this),h=a(j[c]),i=k._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),k.hook("unsanitaryField",g,f,d)):k.hook("sanitaryField",g,d)}),k.hook("formUpdated",i)):(f.html(i.html()),l&&a(document.getElementById(l)).prop({selectionStart:m,selectionEnd:n}).focus(),k.hook("formRefreshed")),p=_(k.setting()).isEqual(c.data.instance),p?k.container.removeClass("previewer-loading"):(k.is_widget_updating=!0,k.setting(c.data.instance),k.is_widget_updating=!1),d&&d.call(k,null,{no_change:p,ajax_finished:!0});else{if(window.console&&window.console.log(c),g="FAIL",c.data&&c.data.message&&(g=c.data.message),!d)throw new Error(g);d.call(k,g)}}),i.fail(function(a,b){if(!d)throw new Error(b);d.call(k,b)}),i.always(function(){k.container.removeClass("widget-form-loading"),h.each(function(){a(this).removeData("state"+e)})})},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=a.indexOf(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().WidgetCustomizerPreview;return b.getSidebarWidgetElement(a.params.sidebar_id,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!==c.setting().indexOf(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,c=this;c.container=a("#available-widgets"),c.filter_input=a("#available-widgets-filter").find("input"),b=function(){j.available_widgets.each(function(b){var d=a("#widget-tpl-"+b.id);d.toggle(!b.get("is_disabled")),b.get("is_disabled")&&d.is(c.selected_widget_tpl)&&(c.selected_widget_tpl=null)})},j.available_widgets.on("change",b),b(),a("#customize-controls").on("click keydown",function(b){var d=a(b.target).is(".add-new-widget, .add-new-widget *");a("body").hasClass("adding-widget")&&!d&&c.close()}),j.previewer.bind("url",function(){c.close()}),c.container.find(".widget-tpl").on("click keypress",function(a){("keypress"!==a.type||13===a.which||32===a.which)&&c.submit(this)}),c.container.liveFilter("#available-widgets-filter input",".widget-tpl",{filterChildSelector:".widget-title h4",after:function(){var a,b=c.filter_input.val();c.selected_widget_tpl&&!c.selected_widget_tpl.is(":visible")&&(c.selected_widget_tpl.removeClass("selected"),c.selected_widget_tpl=null),c.selected_widget_tpl&&!b&&(c.selected_widget_tpl.removeClass("selected"),c.selected_widget_tpl=null),!c.selected_widget_tpl&&b&&(a=c.container.find("> .widget-tpl:visible:first"),a.length&&c.select(a))}}),c.container.find(" > .widget-tpl").on("focus",function(){c.select(this)}),c.container.on("keydown",function(b){var d=13===b.which,e=27===b.which,f=40===b.which,g=38===b.which,h=null,i=c.container.find("> .widget-tpl:visible:first"),j=c.container.find("> .widget-tpl:visible:last"),k=a(b.target).is(c.filter_input);return f||g?(f?k?h=i:c.selected_widget_tpl&&0!==c.selected_widget_tpl.nextAll(".widget-tpl:visible").length&&(h=c.selected_widget_tpl.nextAll(".widget-tpl:visible:first")):g&&(k?h=j:c.selected_widget_tpl&&0!==c.selected_widget_tpl.prevAll(".widget-tpl:visible").length&&(h=c.selected_widget_tpl.prevAll(".widget-tpl:visible:first"))),c.select(h),void(h?h.focus():c.filter_input.focus())):void((!d||c.filter_input.val())&&(d?c.submit():e&&c.close({return_focus:!0})))})},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"),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);!function(a){a.fn.liveFilter=function(b,c,d){var e,f,g={filterChildSelector:null,filter:function(b,c){return a(b).text().toUpperCase().indexOf(c.toUpperCase())>=0},before:function(){},after:function(){}};d=a.extend(g,d),e=a(this).find(c),d.filterChildSelector&&(e=e.find(d.filterChildSelector)),f=d.filter,a(b).keyup(function(){var b,g,h=a(this).val();b=e.filter(function(){return f(this,h)}),g=e.not(b),d.filterChildSelector&&(b=b.parents(c),g=g.parents(c).hide()),d.before.call(this,b,g),b.show(),g.hide(),""===h&&(b.show(),g.show()),d.after.call(this,b,g)})}}(jQuery); |