1 line
24 KiB
JavaScript
1 line
24 KiB
JavaScript
!function(n,u){var h,p;function l(e){var t={number:null,id_base:null},i=e.match(/^(.+)-(\d+)$/);return i?(t.id_base=i[1],t.number=parseInt(i[2],10)):t.id_base=e,t}n&&n.customize&&((h=n.customize).Widgets=h.Widgets||{},h.Widgets.data=_wpCustomizeWidgetsSettings||{},p=h.Widgets.data.l10n,delete h.Widgets.data.l10n,h.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:"refresh",params:[],width:null,height:null,search_matched:!0}),h.Widgets.WidgetCollection=Backbone.Collection.extend({model:h.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))})}}),h.Widgets.availableWidgets=new h.Widgets.WidgetCollection(h.Widgets.data.availableWidgets),h.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}),h.Widgets.SidebarCollection=Backbone.Collection.extend({model:h.Widgets.SidebarModel}),h.Widgets.registeredSidebars=new h.Widgets.SidebarCollection(h.Widgets.data.registeredSidebars),h.Widgets.AvailableWidgetsPanelView=n.Backbone.View.extend({el:"#available-widgets",events:{"input #widgets-search":"search","keyup #widgets-search":"search","change #widgets-search":"search","search #widgets-search":"search","focus .widget-tpl":"focus","click .widget-tpl":"_submit","keypress .widget-tpl":"_submit",keydown:"keyboardAccessible"},selected:null,currentSidebarControl:null,$search:null,initialize:function(){var t=this;this.$search=u("#widgets-search"),_.bindAll(this,"close"),this.listenTo(this.collection,"change",this.updateList),this.updateList(),u("#customize-controls, .customize-overlay-close").on("click keydown",function(e){e=u(e.target).is(".add-new-widget, .add-new-widget *");u("body").hasClass("adding-widget")&&!e&&t.close()}),h.previewer.bind("url",this.close)},search:function(e){var t;this.collection.doSearch(e.target.value),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)},updateList:function(){this.collection.each(function(e){var t=u("#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=u(e),this.selected.siblings(".widget-tpl").removeClass("selected"),this.selected.addClass("selected")},focus:function(e){this.select(u(e.currentTarget))},_submit:function(e){"keypress"===e.type&&13!==e.which&&32!==e.which||this.submit(u(e.currentTarget))},submit:function(e){(e=e||this.selected)&&this.currentSidebarControl&&(this.select(e),e=u(this.selected).data("widget-id"),(e=this.collection.findWhere({id:e}))&&((e=this.currentSidebarControl.addWidget(e.get("id_base")))&&e.focus(),this.close()))},open:function(e){this.currentSidebarControl=e,_(this.currentSidebarControl.getWidgetFormControls()).each(function(e){e.params.is_wide&&e.collapseForm()}),u("body").addClass("adding-widget"),this.$el.find(".selected").removeClass("selected"),this.collection.doSearch(""),h.settings.browser.mobile||this.$search.focus()},close:function(e){(e=e||{}).returnFocus&&this.currentSidebarControl&&this.currentSidebarControl.container.find(".add-new-widget").focus(),this.currentSidebarControl=null,this.selected=null,u("body").removeClass("adding-widget"),this.$search.val("")},keyboardAccessible:function(e){var t=13===e.which,i=27===e.which,n=40===e.which,s=38===e.which,d=9===e.which,o=e.shiftKey,a=null,r=this.$el.find("> .widget-tpl:visible:first"),c=this.$el.find("> .widget-tpl:visible:last"),l=u(e.target).is(this.$search),g=u(e.target).is(".widget-tpl:visible:last");if(n||s)return n?l?a=r:this.selected&&0!==this.selected.nextAll(".widget-tpl:visible").length&&(a=this.selected.nextAll(".widget-tpl:visible:first")):s&&(l?a=c:this.selected&&0!==this.selected.prevAll(".widget-tpl:visible").length&&(a=this.selected.prevAll(".widget-tpl:visible:first"))),this.select(a),void(a||this.$search).focus();t&&!this.$search.val()||(t?this.submit():i&&this.close({returnFocus:!0}),d&&(o&&l||!o&&g)&&(this.currentSidebarControl.container.find(".add-new-widget").focus(),e.preventDefault()))}}),h.Widgets.formSyncHandlers={rss:function(e,t,i){var n=t.find(".widget-error:first"),i=u("<div>"+i+"</div>").find(".widget-error:first");n.length&&i.length?n.replaceWith(i):n.length?n.remove():i.length&&t.find(".widget-content:first").prepend(i)}},h.Widgets.WidgetControl=h.Control.extend({defaultExpandedArguments:{duration:"fast",completeCallback:u.noop},initialize:function(e,t){var i=this;h.Control.prototype.initialize.call(i,e,t),i.expanded=new h.Value,i.expandedArgumentsQueue=[],i.expanded.bind(function(e){var t=i.expandedArgumentsQueue.shift(),t=u.extend({},i.defaultExpandedArguments,t);i.onChangeExpanded(e,t)}),i.expanded.set(!1)},ready:function(){this._setupModel(),this._setupWideWidget(),this._setupControlToggle(),this._setupWidgetTitle(),this._setupReorderUI(),this._setupHighlightEffects(),this._setupUpdateUI(),this._setupRemoveUI(),u(document).trigger("widget-added",[this.container.find(".widget:first")])},_setupModel:function(){var e,i=this;h.Widgets.savedWidgetIds=h.Widgets.savedWidgetIds||[],e=function(){h.Widgets.savedWidgetIds[i.params.widget_id]=!0},h.bind("ready",e),h.bind("saved",e),this._updateCount=0,this.isWidgetUpdating=!1,this.liveUpdateMode=!0,this.setting.bind(function(e,t){_(t).isEqual(e)||i.isWidgetUpdating||i.updateWidget({instance:e})})},_setupWideWidget:function(){var n,s,e,t,i,d=this;this.params.is_wide&&(n=this.container.find(".widget-inside"),s=n.find("> .form"),e=u(".wp-full-overlay-sidebar-content:first"),this.container.addClass("wide-widget-control"),this.container.find(".widget-content:first").css({"max-width":this.params.width,"min-height":this.params.height}),i=function(){var e=d.container.offset().top,t=u(window).height(),i=s.outerHeight();n.css("max-height",t),i=Math.max(0,Math.min(Math.max(e,0),t-i)),n.css("top",i)},t=u("#customize-theme-controls"),this.container.on("expand",function(){i(),e.on("scroll",i),u(window).on("resize",i),t.on("expanded collapsed",i)}),this.container.on("collapsed",function(){e.off("scroll",i),u(window).off("resize",i),t.off("expanded collapsed",i)}),h.each(function(e){0===e.id.indexOf("sidebars_widgets[")&&e.bind(function(){d.container.hasClass("expanded")&&i()})}))},_setupControlToggle:function(){var t=this;this.container.find(".widget-top").on("click",function(e){e.preventDefault(),t.getSidebarWidgetsControl().isReordering||t.expanded(!t.expanded())}),this.container.find(".widget-control-close").on("click",function(e){e.preventDefault(),t.collapse(),t.container.find(".widget-top .widget-action:first").focus()})},_setupWidgetTitle:function(){var i=this,e=function(){var e=i.setting().title,t=i.container.find(".in-widget-title");e?t.text(": "+e):t.text("")};this.setting.bind(e),e()},_setupReorderUI:function(){var t,e,d=this,s=function(e){e.siblings(".selected").removeClass("selected"),e.addClass("selected");e=e.data("id")===d.params.sidebar_id;d.container.find(".move-widget-btn").prop("disabled",e)};this.container.find(".widget-title-action").after(u(h.Widgets.data.tpl.widgetReorderNav)),t=u(_.template(h.Widgets.data.tpl.moveWidgetArea,{sidebars:_(h.Widgets.registeredSidebars.toArray()).pluck("attributes")})),this.container.find(".widget-top").after(t),(e=function(){var e=t.find("li"),i=0,n=e.filter(function(){return u(this).data("id")===d.params.sidebar_id});e.each(function(){var e=u(this),t=e.data("id"),t=h.Widgets.registeredSidebars.get(t).get("is_rendered");e.toggle(t),t&&(i+=1),e.hasClass("selected")&&!t&&s(n)}),1<i?d.container.find(".move-widget").show():d.container.find(".move-widget").hide()})(),h.Widgets.registeredSidebars.on("change:is_rendered",e),this.container.find(".widget-reorder-nav").find(".move-widget, .move-widget-down, .move-widget-up").each(function(){u(this).prepend(d.container.find(".widget-title").text()+": ")}).on("click keypress",function(e){var t,i;"keypress"===e.type&&13!==e.which&&32!==e.which||(u(this).focus(),u(this).is(".move-widget")?d.toggleWidgetMoveArea():(t=u(this).is(".move-widget-down"),i=u(this).is(".move-widget-up"),e=d.getWidgetSidebarPosition(),i&&0===e||t&&e===d.getSidebarWidgetsControl().setting().length-1||(i?(d.moveUp(),n.a11y.speak(p.widgetMovedUp)):(d.moveDown(),n.a11y.speak(p.widgetMovedDown)),u(this).focus())))}),this.container.find(".widget-area-select").on("click keypress","li",function(e){"keypress"===e.type&&13!==e.which&&32!==e.which||(e.preventDefault(),s(u(this)))}),this.container.find(".move-widget-btn").click(function(){d.getSidebarWidgetsControl().toggleReordering(!1);var e=d.params.sidebar_id,t=d.container.find(".widget-area-select li.selected").data("id"),i=h("sidebars_widgets["+e+"]"),n=h("sidebars_widgets["+t+"]"),s=Array.prototype.slice.call(i()),e=Array.prototype.slice.call(n()),t=d.getWidgetSidebarPosition();s.splice(t,1),e.push(d.params.widget_id),i(s),n(e),d.focus()})},_setupHighlightEffects:function(){var e=this;this.container.on("mouseenter click",function(){e.setting.previewer.send("highlight-widget",e.params.widget_id)}),this.setting.bind(function(){e.setting.previewer.send("highlight-widget",e.params.widget_id)})},_setupUpdateUI:function(){var t,i,n=this,s=this.container.find(".widget:first"),e=s.find(".widget-content:first"),d=this.container.find(".widget-control-save");d.val(p.saveBtnLabel),d.attr("title",p.saveBtnTooltip),d.removeClass("button-primary").addClass("button-secondary"),d.on("click",function(e){e.preventDefault(),n.updateWidget({disable_form:!0})}),t=_.debounce(function(){n.updateWidget()},250),e.on("keydown","input",function(e){13===e.which&&(e.preventDefault(),n.updateWidget({ignoreActiveElement:!0}))}),e.on("change input propertychange",":input",function(e){n.liveUpdateMode&&("change"===e.type?n.updateWidget():this.checkValidity&&this.checkValidity()&&t())}),this.setting.previewer.channel.bind("synced",function(){n.container.removeClass("previewer-loading")}),h.previewer.bind("widget-updated",function(e){e===n.params.widget_id&&n.container.removeClass("previewer-loading")}),(i=h.Widgets.formSyncHandlers[this.params.widget_id_base])&&u(document).on("widget-synced",function(e,t){s.is(t)&&i.apply(document,arguments)})},onChangeActive:function(e,t){this.container.toggleClass("widget-rendered",e),t.completeCallback&&t.completeCallback()},_setupRemoveUI:function(){var e,s=this,t=this.container.find("a.widget-control-remove");t.on("click",function(e){var n;e.preventDefault(),n=s.container.next().is(".customize-control-widget_form")?s.container.next().find(".widget-action:first"):s.container.prev().is(".customize-control-widget_form")?s.container.prev().find(".widget-action:first"):s.container.next(".customize-control-sidebar_widgets").find(".add-new-widget:first"),s.container.slideUp(function(){var e,t,i=h.Widgets.getSidebarWidgetControlContainingWidget(s.params.widget_id);i&&(e=i.setting().slice(),-1!==(t=_.indexOf(e,s.params.widget_id))&&(e.splice(t,1),i.setting(e),n.focus()))})}),e=function(){t.text(p.removeBtnLabel),t.attr("title",p.removeBtnTooltip)},this.params.is_new?h.bind("saved",e):e()},_getInputs:function(e){return u(e).find(":input[name]")},_getInputsSignature:function(e){return _(e).map(function(e){e=u(e),e=e.is(":checkbox, :radio")?[e.attr("id"),e.attr("name"),e.prop("value")]:[e.attr("id"),e.attr("name")];return e.join(",")}).join(";")},_getInputState:function(e){return(e=u(e)).is(":radio, :checkbox")?e.prop("checked"):e.is("select[multiple]")?e.find("option:selected").map(function(){return u(this).val()}).get():e.val()},_setInputState:function(e,t){(e=u(e)).is(":radio, :checkbox")?e.prop("checked",t):e.is("select[multiple]")?(t=u.isArray(t)?_.map(t,function(e){return String(e)}):[],e.find("option").each(function(){u(this).prop("selected",-1!==_.indexOf(t,String(this.value)))})):e.val(t)},getSidebarWidgetsControl:function(){var e="sidebars_widgets["+this.params.sidebar_id+"]",e=h.control(e);if(e)return e},updateWidget:function(s){var d,o,a,r,e,t,c,l=this,i=(s=u.extend({instance:null,complete:null,ignoreActiveElement:!1},s)).instance,g=s.complete;this._updateCount+=1,a=this._updateCount,d=this.container.find(".widget:first"),(o=d.find(".widget-content:first")).find(".widget-error").remove(),this.container.addClass("widget-form-loading"),this.container.addClass("previewer-loading"),(e=h.state("processing"))(e()+1),this.liveUpdateMode||this.container.addClass("widget-form-disabled"),(t={action:"update-widget",wp_customize:"on"}).nonce=h.Widgets.data.nonce,t.theme=h.settings.theme.stylesheet,t=u.param(t),(r=this._getInputs(o)).each(function(){u(this).data("state"+a,l._getInputState(this))}),t+=i?"&"+u.param({sanitized_widget_setting:JSON.stringify(i)}):"&"+r.serialize(),t+="&"+o.find("~ :input").serialize(),this._previousUpdateRequest&&this._previousUpdateRequest.abort(),t=u.post(n.ajax.settings.url,t),(this._previousUpdateRequest=t).done(function(e){var n,t,i=!1;if("0"===e)return h.previewer.preview.iframe.hide(),void h.previewer.login().done(function(){l.updateWidget(s),h.previewer.preview.iframe.show()});"-1"!==e?e.success?(t=u("<div>"+e.data.form+"</div>"),n=l._getInputs(t),(t=l._getInputsSignature(r)===l._getInputsSignature(n))&&!l.liveUpdateMode&&(l.liveUpdateMode=!0,l.container.removeClass("widget-form-disabled"),l.container.find('input[name="savewidget"]').hide()),t&&l.liveUpdateMode?(r.each(function(e){var t=u(this),i=u(n[e]),e=t.data("state"+a),i=l._getInputState(i);t.data("sanitized",i),_.isEqual(e,i)||!s.ignoreActiveElement&&t.is(document.activeElement)||l._setInputState(t,i)}),u(document).trigger("widget-synced",[d,e.data.form])):l.liveUpdateMode?(l.liveUpdateMode=!1,l.container.find('input[name="savewidget"]').show(),i=!0):(o.html(e.data.form),l.container.removeClass("widget-form-disabled"),u(document).trigger("widget-updated",[d])),(c=!i&&!_(l.setting()).isEqual(e.data.instance))?(l.isWidgetUpdating=!0,l.setting(e.data.instance),l.isWidgetUpdating=!1):l.container.removeClass("previewer-loading"),g&&g.call(l,null,{noChange:!c,ajaxFinished:!0})):(i=p.error,e.data&&e.data.message&&(i=e.data.message),g?g.call(l,i):o.prepend('<p class="widget-error"><strong>'+i+"</strong></p>")):h.previewer.cheatin()}),t.fail(function(e,t){g&&g.call(l,t)}),t.always(function(){l.container.removeClass("widget-form-loading"),r.each(function(){u(this).removeData("state"+a)}),e(e()-1)})},expandControlSection:function(){h.Control.prototype.expand.call(this)},_toggleExpanded:h.Section.prototype._toggleExpanded,expand:h.Section.prototype.expand,expandForm:function(){this.expand()},collapse:h.Section.prototype.collapse,collapseForm:function(){this.collapse()},toggleForm:function(e){void 0===e&&(e=!this.expanded()),this.expanded(e)},onChangeExpanded:function(e,t){var i,n,s,d,o=this;t.unchanged?e&&h.Control.prototype.expand.call(o,{completeCallback:t.completeCallback}):(n=(i=this.container.find("div.widget:first")).find(".widget-inside:first"),e?(o.expandControlSection(),h.control.each(function(e){o.params.type===e.params.type&&o!==e&&e.collapse()}),s=function(){o.container.removeClass("expanding"),o.container.addClass("expanded"),o.container.trigger("expanded")},t.completeCallback&&(d=s,s=function(){d(),t.completeCallback()}),o.params.is_wide?n.fadeIn(t.duration,s):n.slideDown(t.duration,s),o.container.trigger("expand"),o.container.addClass("expanding")):(s=function(){o.container.removeClass("collapsing"),o.container.removeClass("expanded"),o.container.trigger("collapsed")},t.completeCallback&&(d=s,s=function(){d(),t.completeCallback()}),o.container.trigger("collapse"),o.container.addClass("collapsing"),o.params.is_wide?n.fadeOut(t.duration,s):n.slideUp(t.duration,function(){i.css({width:"",margin:""}),s()})))},getWidgetSidebarPosition:function(){var e=this.getSidebarWidgetsControl().setting(),e=_.indexOf(e,this.params.widget_id);if(-1!==e)return e},moveUp:function(){this._moveWidgetByOne(-1)},moveDown:function(){this._moveWidgetByOne(1)},_moveWidgetByOne:function(e){var t=this.getWidgetSidebarPosition(),i=this.getSidebarWidgetsControl().setting,n=Array.prototype.slice.call(i()),s=n[t+e];n[t+e]=this.params.widget_id,n[t]=s,i(n)},toggleWidgetMoveArea:function(e){var t=this,i=this.container.find(".move-widget-area");(e=void 0===e?!i.hasClass("active"):e)&&(i.find(".selected").removeClass("selected"),i.find("li").filter(function(){return u(this).data("id")===t.params.sidebar_id}).addClass("selected"),this.container.find(".move-widget-btn").prop("disabled",!0)),i.toggleClass("active",e)},highlightSectionAndControl:function(){var e=this.container.is(":hidden")?this.container.closest(".control-section"):this.container;u(".highlighted").removeClass("highlighted"),e.addClass("highlighted"),setTimeout(function(){e.removeClass("highlighted")},500)}}),h.Widgets.SidebarSection=h.Section.extend({ready:function(){var t;h.Section.prototype.ready.call(this),t=h.Widgets.registeredSidebars.get(this.params.sidebarId),this.active.bind(function(e){t.set("is_rendered",e)}),t.set("is_rendered",this.active())}}),h.Widgets.SidebarControl=h.Control.extend({ready:function(){this.$controlSection=this.container.closest(".control-section"),this.$sectionContent=this.container.closest(".accordion-section-content"),this._setupModel(),this._setupSortable(),this._setupAddition(),this._applyCardinalOrderClassNames()},_setupModel:function(){var d=this;this.setting.bind(function(i,e){var t,n=_(e).difference(i);i=_(i).filter(function(e){e=l(e);return!!h.Widgets.availableWidgets.findWhere({id_base:e.id_base})}),(e=_(i).map(function(e){return h.Widgets.getWidgetFormControlForWidget(e)||d.addWidget(e)})).sort(function(e,t){return _.indexOf(i,e.params.widget_id)-_.indexOf(i,t.params.widget_id)}),t=0,_(e).each(function(e){e.priority(t),e.section(d.section()),t+=1}),d.priority(t),d._applyCardinalOrderClassNames(),_(e).each(function(e){e.params.sidebar_id=d.params.sidebar_id}),_(n).each(function(s){setTimeout(function(){var e,t,i,n=!1;h.each(function(e){e.id!==d.setting.id&&0===e.id.indexOf("sidebars_widgets[")&&"sidebars_widgets[wp_inactive_widgets]"!==e.id&&(e=e(),-1!==_.indexOf(e,s)&&(n=!0))}),n||(t=(e=h.Widgets.getWidgetFormControlForWidget(s))&&u.contains(document,e.container[0])&&!u.contains(d.$sectionContent[0],e.container[0]),e&&!t&&(h.control.remove(e.id),e.container.remove()),h.Widgets.savedWidgetIds[s]&&((i=h.value("sidebars_widgets[wp_inactive_widgets]")().slice()).push(s),h.value("sidebars_widgets[wp_inactive_widgets]")(_(i).unique())),i=l(s).id_base,(i=h.Widgets.availableWidgets.findWhere({id_base:i}))&&!i.get("is_multi")&&i.set("is_disabled",!1))})})})},_setupSortable:function(){var t=this;this.isReordering=!1,this.$sectionContent.sortable({items:"> .customize-control-widget_form",handle:".widget-top",axis:"y",connectWith:".accordion-section-content:has(.customize-control-sidebar_widgets)",update:function(){var e=t.$sectionContent.sortable("toArray"),e=u.map(e,function(e){return u("#"+e).find(":input[name=widget-id]").val()});t.setting(e)}}),this.$controlSection.find(".accordion-section-title").droppable({accept:".customize-control-widget_form",over:function(){h.section(t.section.get()).expand({allowMultiple:!0,completeCallback:function(){h.section.each(function(e){e.container.find(".customize-control-sidebar_widgets").length&&e.container.find(".accordion-section-content:first").sortable("refreshPositions")})}})}}),this.container.find(".reorder-toggle").on("click keydown",function(e){"keydown"===e.type&&13!==e.which&&32!==e.which||t.toggleReordering(!t.isReordering)})},_setupAddition:function(){var t=this;this.container.find(".add-new-widget").on("click keydown",function(e){"keydown"===e.type&&13!==e.which&&32!==e.which||t.$sectionContent.hasClass("reordering")||(u("body").hasClass("adding-widget")?h.Widgets.availableWidgetsPanel.close():h.Widgets.availableWidgetsPanel.open(t))})},_applyCardinalOrderClassNames:function(){var t=[];_.each(this.setting(),function(e){e=h.Widgets.getWidgetFormControlForWidget(e);e&&t.push(e)}),t.length?(this.container.find(".reorder-toggle").show(),u(t).each(function(){u(this.container).removeClass("first-widget").removeClass("last-widget").find(".move-widget-down, .move-widget-up").prop("tabIndex",0)}),_.first(t).container.addClass("first-widget").find(".move-widget-up").prop("tabIndex",-1),_.last(t).container.addClass("last-widget").find(".move-widget-down").prop("tabIndex",-1)):this.container.find(".reorder-toggle").hide()},toggleReordering:function(e){(e=Boolean(e))!==this.$sectionContent.hasClass("reordering")&&(this.isReordering=e,this.$sectionContent.toggleClass("reordering",e),e?(_(this.getWidgetFormControls()).each(function(e){e.collapse()}),this.$sectionContent.find(".first-widget .move-widget").focus(),this.$sectionContent.find(".add-new-widget").prop("tabIndex",-1)):this.$sectionContent.find(".add-new-widget").prop("tabIndex",0))},getWidgetFormControls:function(){var t=[];return _(this.setting()).each(function(e){e=function(e){var t=l(e);e="widget_"+t.id_base,t.number&&(e+="["+t.number+"]");return e}(e),e=h.control(e);e&&t.push(e)}),t},addWidget:function(n){var e,t,i,s=this,d="widget_form",o=l(n),a=o.number,r=o.id_base,c=h.Widgets.availableWidgets.findWhere({id_base:r});return!!c&&(!(a&&!c.get("is_multi"))&&(c.get("is_multi")&&!a&&(c.set("multi_number",c.get("multi_number")+1),a=c.get("multi_number")),o=u.trim(u("#widget-tpl-"+c.get("id")).html()),c.get("is_multi")?o=o.replace(/<[^<>]+>/g,function(e){return e.replace(/__i__|%i%/g,a)}):c.set("is_disabled",!0),r=u(o),(o=u("<li/>").addClass("customize-control").addClass("customize-control-"+d).append(r)).find("> .widget-icon").remove(),c.get("is_multi")&&(o.find('input[name="widget_number"]').val(a),o.find('input[name="multi_number"]').val(a)),n=o.find('[name="widget-id"]').val(),o.hide(),r="widget_"+c.get("id_base"),c.get("is_multi")&&(r+="["+a+"]"),o.attr("id","customize-control-"+r.replace(/\]/g,"").replace(/\[/g,"-")),(e=h.has(r))||(i={transport:"refresh",previewer:this.setting.previewer},h.create(r,r,"",i).set({})),i=h.controlConstructor[d],t=new i(r,{params:{settings:{"default":r},content:o,sidebar_id:s.params.sidebar_id,widget_id:n,widget_id_base:c.get("id_base"),type:d,is_new:!e,width:c.get("width"),height:c.get("height"),is_wide:c.get("is_wide")},previewer:s.setting.previewer}),h.control.add(r,t),h.each(function(e){var t,i;e.id!==s.setting.id&&0===e.id.indexOf("sidebars_widgets[")&&(t=e().slice(),-1!==(i=_.indexOf(t,n))&&(t.splice(i),e(t)))}),r=this.setting().slice(),-1===_.indexOf(r,n)&&(r.push(n),this.setting(r)),o.slideDown(function(){e&&t.updateWidget({instance:t.setting()})}),t))}}),u.extend(h.sectionConstructor,{sidebar:h.Widgets.SidebarSection}),u.extend(h.controlConstructor,{widget_form:h.Widgets.WidgetControl,sidebar_widgets:h.Widgets.SidebarControl}),h.bind("nonce-refresh",function(e){h.Widgets.data.nonce=e["update-widget"]}),h.bind("ready",function(){h.Widgets.availableWidgetsPanel=new h.Widgets.AvailableWidgetsPanelView({collection:h.Widgets.availableWidgets}),h.previewer.bind("highlight-widget-control",h.Widgets.highlightWidgetFormControl),h.previewer.bind("focus-widget-control",h.Widgets.focusWidgetFormControl)}),h.Widgets.highlightWidgetFormControl=function(e){e=h.Widgets.getWidgetFormControlForWidget(e);e&&e.highlightSectionAndControl()},h.Widgets.focusWidgetFormControl=function(e){e=h.Widgets.getWidgetFormControlForWidget(e);e&&e.focus()},h.Widgets.getSidebarWidgetControlContainingWidget=function(t){var i=null;return h.control.each(function(e){"sidebar_widgets"===e.params.type&&-1!==_.indexOf(e.setting(),t)&&(i=e)}),i},h.Widgets.getWidgetFormControlForWidget=function(t){var i=null;return h.control.each(function(e){"widget_form"===e.params.type&&e.params.widget_id===t&&(i=e)}),i})}(window.wp,jQuery); |