WordPress/wp-admin/js/customize-widgets.min.js

1 line
28 KiB
JavaScript
Raw Normal View History

!function(p,m){if(p&&p.customize){var f,v=p.customize;v.Widgets=v.Widgets||{},v.Widgets.savedWidgetIds={},v.Widgets.data=_wpCustomizeWidgetsSettings||{},f=v.Widgets.data.l10n,v.Widgets.WidgetModel=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:null,params:[],width:null,height:null,search_matched:!0}),v.Widgets.WidgetCollection=Backbone.Collection.extend({model:v.Widgets.WidgetModel,doSearch:function(e){this.terms!==e&&(this.terms=e,0<this.terms.length&&this.search(this.terms),""===this.terms&&this.each(function(e){e.set("search_matched",!0)}))},search:function(e){var t,i;e=(e=e.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&")).replace(/ /g,")(?=.*"),t=new RegExp("^(?=.*"+e+").+","i"),this.each(function(e){i=[e.get("name"),e.get("id"),e.get("description")].join(" "),e.set("search_matched",t.test(i))})}}),v.Widgets.availableWidgets=new v.Widgets.WidgetCollection(v.Widgets.data.availableWidgets),v.Widgets.SidebarModel=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}),v.Widgets.SidebarCollection=Backbone.Collection.extend({model:v.Widgets.SidebarModel}),v.Widgets.registeredSidebars=new v.Widgets.SidebarCollection(v.Widgets.data.registeredSidebars),v.Widgets.AvailableWidgetsPanelView=p.Backbone.View.extend({el:"#available-widgets",events:{"input #widgets-search":"search","focus .widget-tpl":"focus","click .widget-tpl":"_submit","keypress .widget-tpl":"_submit",keydown:"keyboardAccessible"},selected:null,currentSidebarControl:null,$search:null,$clearResults:null,searchMatchesCount:null,initialize:function(){var i=this;this.$search=m("#widgets-search"),this.$clearResults=this.$el.find(".clear-results"),_.bindAll(this,"close"),this.listenTo(this.collection,"change",this.updateList),this.updateList(),this.searchMatchesCount=this.collection.length,m("#customize-controls, #available-widgets .customize-section-title").on("click keydown",function(e){var t=m(e.target).is(".add-new-widget, .add-new-widget *");m("body").hasClass("adding-widget")&&!t&&i.close()}),this.$clearResults.on("click",function(){i.$search.val("").focus(),i.collection.doSearch("")}),v.previewer.bind("url",this.close)},search:_.debounce(function(e){var t;this.collection.doSearch(e.target.value),this.updateSearchMatchesCount(),this.announceSearchMatches(),this.selected&&!this.selected.is(":visible")&&(this.selected.removeClass("selected"),this.selected=null),this.selected&&!e.target.value&&(this.selected.removeClass("selected"),this.selected=null),!this.selected&&e.target.value&&(t=this.$el.find("> .widget-tpl:visible:first")).length&&this.select(t),""!==e.target.value?this.$clearResults.addClass("is-visible"):""===e.target.value&&this.$clearResults.removeClass("is-visible"),this.searchMatchesCount?this.$el.removeClass("no-widgets-found"):this.$el.addClass("no-widgets-found")},500),updateSearchMatchesCount:function(){this.searchMatchesCount=this.collection.where({search_matched:!0}).length},announceSearchMatches:function(){var e=f.widgetsFound.replace("%d",this.searchMatchesCount);this.searchMatchesCount||(e=f.noWidgetsFound),p.a11y.speak(e)},updateList:function(){this.collection.each(function(e){var t=m("#widget-tpl-"+e.id);t.toggle(e.get("search_matched")&&!e.get("is_disabled")),e.get("is_disabled")&&t.is(this.selected)&&(this.selected=null)})},select:function(e){this.selected=m(e),this.selected.siblings(".widget-tpl").removeClass("selected"),this.selected.addClass("selected")},focus:function(e){this.select(m(e.currentTarget))},_submit:function(e){"keypress"===e.type&&13!==e.which&&32!==e.which||this.submit(m(e.currentTarget))},submit:function(e){var t,i,n;(e=e||this.selected)&&this.currentSidebarControl&&(this.select(e),t=m(this.selected).data("widget-id"),(i=this.collection.findWhere({id:t}))&&((n=this.currentSidebarControl.addWidget(i.get("id_base")))&&n.focus(),this.close()))},open:function(e){this.currentSidebarControl=e,_(this.curren