mirror of
https://github.com/WordPress/WordPress.git
synced 2024-12-28 03:57:46 +01:00
158597a5e3
props westonruter. fixes #27635. Built from https://develop.svn.wordpress.org/trunk@27913 git-svn-id: http://core.svn.wordpress.org/trunk@27744 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,j=wp.customize,k={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(k,WidgetCustomizer_exports),"undefined"==typeof window.ajaxurl&&(window.ajaxurl=wp.ajax.settings.url),d=k.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=k.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)}}),k.available_widgets=new e(k.available_widgets),f=k.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=k.SidebarCollection=Backbone.Collection.extend({model:f}),k.registered_sidebars=new g(k.registered_sidebars),i={rss:function(b,c,d){var e=c.find(".widget-error:first"),f=a("<div>"+d+"</div>").find(".widget-error:first");e.length&&f.length?e.replaceWith(f):e.length?e.remove():f.length&&c.find(".widget-content:first").prepend(f)}},k.init=function(){this.availableWidgetsPanel.setup(),this.previewer.bind("highlight-widget-control",k.highlightWidgetFormControl),this.previewer.bind("focus-widget-control",k.focusWidgetFormControl)},wp.customize.bind("ready",function(){k.init()}),j.controlConstructor.sidebar_widgets=j.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=k.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!!k.available_widgets.findWhere({id_base:c.id_base})}),f=_(d).map(function(a){var b=k.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,j=!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&&(j=!0)}}),j||(e=k.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()),k.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=k.available_widgets.findWhere({id_base:h}),i&&!i.get("is_multi")&&i.set("is_disabled",!1))})})}),k.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")?k.availableWidgetsPanel.close():k.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=j.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,j,l,m,n=this,o="widget_form",p=b(c),q=p.number,r=p.id_base,s=k.available_widgets.findWhere({id_base:r});if(!s)throw new Error("Widget unexpectedly not found.");if(q&&!s.get("is_multi"))throw new Error("Did not expect a widget number to be supplied for a non-multi widget");return s.get("is_multi")&&!q&&(s.set("multi_number",s.get("multi_number")+1),q=s.get("multi_number")),d=a("#widget-tpl-"+s.get("id")).html(),s.get("is_multi")?d=d.replace(/<[^<>]+>/g,function(a){return a.replace(/__i__|%i%/g,q)}):s.set("is_disabled",!0),e=a(d),f=a("<li></li>"),f.addClass("customize-control"),f.addClass("customize-control-"+o),f.append(e),f.find("> .widget-icon").remove(),s.get("is_multi")&&(f.find('input[name="widget_number"]').val(q),f.find('input[name="multi_number"]').val(q)),c=f.find('[name="widget-id"]').val(),f.hide(),g="widget_"+s.get("id_base"),s.get("is_multi")&&(g+="["+q+"]"),f.attr("id","customize-control-"+g.replace(/\]/g,"").replace(/\[/g,"-")),n.container.after(f),h=wp.customize.has(g),h||(m={transport:"refresh",previewer:n.setting.previewer},wp.customize.create(g,g,{},m)),i=wp.customize.controlConstructor[o],j=new i(g,{params:{settings:{"default":g},sidebar_id:n.params.sidebar_id,widget_id:c,widget_id_base:s.get("id_base"),type:o,is_new:!h,width:s.get("width"),height:s.get("height"),is_wide:s.get("is_wide")},previewer:n.setting.previewer}),wp.customize.control.add(g,j),wp.customize.each(function(a){if(a.id!==n.setting.id&&0===a.id.indexOf("sidebars_widgets[")){var b,d=a().slice();b=_.indexOf(d,c),-1!==b&&(d.splice(b),a(d))}}),l=n.setting().slice(),-1===_.indexOf(l,c)&&(l.push(c),n.setting(l)),f.slideDown(function(){h?(j.expandForm(),j.updateWidget({instance:j.setting(),complete:function(a){if(a)throw a;j.focus()}})):j.focus()}),a(document).trigger("widget-added",[e]),j}}),j.controlConstructor.widget_form=j.Control.extend({ready:function(){var a=this;a._setupModel(),a._setupWideWidget(),a._setupControlToggle(),a._setupWidgetTitle(),a._setupReorderUI(),a._setupHighlightEffects(),a._setupUpdateUI(),a._setupRemoveUI()},_setupModel:function(){var a,b=this;a=function(){k.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.live_update_mode=!0,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(k.tpl.widget_reorder_nav)),c=a(_.template(k.tpl.move_widget_area,{sidebars:_(k.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=k.registered_sidebars.get(c),f.toggle(e.get("is_rendered")),f.hasClass("selected")&&!e.get("is_rendered")&&b(d)})},e(),k.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=j("sidebars_widgets["+g+"]"),b=j("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 b,c,d,e,f,g=this;b=g.container.find(".widget:first"),c=b.find(".widget-content:first"),d=g.container.find(".widget-control-save"),d.val(k.i18n.save_btn_label),d.attr("title",k.i18n.save_btn_tooltip),d.removeClass("button-primary").addClass("button-secondary"),d.on("click",function(a){a.preventDefault(),g.updateWidget({disable_form:!0})}),e=_.debounce(function(){g.updateWidget()},250),g.container.find(".widget-content").on("keydown","input",function(a){13===a.which&&(a.preventDefault(),g.updateWidget({ignore_active_element:!0}))}),c.on("change input propertychange",":input",function(a){g.live_update_mode&&("change"===a.type?g.updateWidget():this.checkValidity&&this.checkValidity()&&e())}),g.setting.previewer.channel.bind("synced",function(){g.container.removeClass("previewer-loading")}),k.previewer.bind("widget-updated",function(a){a===g.params.widget_id&&g.container.removeClass("previewer-loading")}),k.previewer.bind("rendered-widgets",function(a){var b=!!a[g.params.widget_id];g.container.toggleClass("widget-rendered",b)}),f=i[g.params.widget_id_base],f&&a(document).on("widget-synced",function(a,c){b.is(c)&&f.apply(document,arguments)})},_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=k.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(k.i18n.remove_btn_label),a.attr("title",k.i18n.remove_btn_tooltip)},c.params.is_new?wp.customize.bind("saved",b):b()},_getInputs:function(b){return a(b).find(":input[name]")},_getInputsSignature:function(b){var c=_(b).map(function(b){b=a(b);var c;return c=b.is(":checkbox, :radio")?[b.attr("id"),b.attr("name"),b.prop("value")]:[b.attr("id"),b.attr("name")],c.join(",")});return c.join(";")},_getInputStatePropertyName:function(b){return b=a(b),b.is(":radio, :checkbox")?"checked":"value"},getSidebarWidgetsControl:function(){var a,b,c=this;if(a="sidebars_widgets["+c.params.sidebar_id+"]",b=j.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,j,l,m,n,o=this;b=a.extend({instance:null,complete:null,ignore_active_element:!1},b),c=b.instance,d=b.complete,o._update_count+=1,f=o._update_count,e=o.container.find(".widget:first"),g=e.find(".widget-content:first"),g.find(".widget-error").remove(),o.container.addClass("widget-form-loading"),o.container.addClass("previewer-loading"),l=wp.customize.state("processing"),l(l()+1),o.live_update_mode||o.container.addClass("widget-form-disabled"),h={},h.action="update-widget",h.wp_customize="on",h.nonce=k.nonce,i=a.param(h),j=o._getInputs(g),j.each(function(){var b=a(this),c=o._getInputStatePropertyName(this);b.data("state"+f,b.prop(c))}),i+=c?"&"+a.param({sanitized_widget_setting:JSON.stringify(c)}):"&"+j.serialize(),i+="&"+g.find("~ :input").serialize(),m=a.post(wp.ajax.settings.url,i,function(c){var h,i,l,m,p=!1;return"0"===c?(k.previewer.preview.iframe.hide(),void k.previewer.login().done(function(){o.updateWidget(b),k.previewer.preview.iframe.show()})):"-1"===c?void k.previewer.cheatin():void(c.success?(i=a("<div>"+c.data.form+"</div>"),l=o._getInputs(i),m=o._getInputsSignature(j)===o._getInputsSignature(l),m&&!o.live_update_mode&&(o.live_update_mode=!0,o.container.removeClass("widget-form-disabled"),o.container.find('input[name="savewidget"]').hide()),m&&o.live_update_mode?(j.each(function(c){var d,e,g,h=a(this),i=a(l[c]),j=o._getInputStatePropertyName(this);d=h.data("state"+f),e=i.prop(j),h.data("sanitized",e),g=d!==e&&(b.ignore_active_element||!h.is(document.activeElement)),g&&h.prop(j,e)}),a(document).trigger("widget-synced",[e,c.data.form])):o.live_update_mode?(o.live_update_mode=!1,o.container.find('input[name="savewidget"]').show(),p=!0):(g.html(c.data.form),o.container.removeClass("widget-form-disabled"),a(document).trigger("widget-updated",[e])),n=!p&&!_(o.setting()).isEqual(c.data.instance),n?(o.is_widget_updating=!0,o.setting(c.data.instance),o.is_widget_updating=!1):o.container.removeClass("previewer-loading"),d&&d.call(o,null,{no_change:!n,ajax_finished:!0})):(h=k.i18n.error,c.data&&c.data.message&&(h=c.data.message),d?d.call(o,h):g.prepend('<p class="widget-error"><strong>'+h+"</strong></p>")))}),m.fail(function(a,b){d&&d.call(o,b)}),m.always(function(){o.container.removeClass("widget-form-loading"),j.each(function(){a(this).removeData("state"+f)}),l(l()-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()}),d=function(){e.container.removeClass("expanding"),e.container.addClass("expanded"),e.container.trigger("expanded")},e.params.is_wide?c.fadeIn("fast",d):c.slideDown("fast",d),e.container.trigger("expand"),e.container.addClass("expanding")):(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.fadeOut("fast",d):c.slideUp("fast",function(){b.css({width:"",margin:""}),d()})))},focus:function(){var a=this;a.expandControlSection(),a.expandForm(),a.container.find(".widget-content :focusable:first").focus()},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)},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)}}),h=wp.customize.Previewer,wp.customize.Previewer=h.extend({initialize:function(a,b){k.previewer=this,h.prototype.initialize.call(this,a,b),this.bind("refresh",this.refresh)}}),k.highlightWidgetFormControl=function(a){var b=k.getWidgetFormControlForWidget(a);b&&b.highlightSectionAndControl()},k.focusWidgetFormControl=function(a){var b=k.getWidgetFormControlForWidget(a);b&&b.focus()},k.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},k.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},k.getPreviewWindow=function(){return a("#customize-preview").find("iframe").prop("contentWindow")},k.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"),k.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()}),k.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;k.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=k.availableWidgetsPanel;b.container.find(".widget-tpl").hide(),k.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=k.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"),k.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("")}},k}(jQuery); |