!function(t){"use strict";wp.textWidgets.TextWidgetControl=Backbone.View.extend({events:{},initialize:function(e){var i=this;if(!e.el)throw new Error("Missing options.el");if(!e.syncContainer)throw new Error("Missing options.syncContainer");Backbone.View.prototype.initialize.call(i,e),i.syncContainer=e.syncContainer,i.$el.addClass("text-widget-fields"),i.$el.html(wp.template("widget-text-control-fields")),i.customHtmlWidgetPointer=i.$el.find(".wp-pointer.custom-html-widget-pointer"),i.customHtmlWidgetPointer.length&&(i.customHtmlWidgetPointer.find(".close").on("click",(function(e){e.preventDefault(),i.customHtmlWidgetPointer.hide(),t("#"+i.fields.text.attr("id")+"-html").focus(),i.dismissPointers(["text_widget_custom_html"])})),i.customHtmlWidgetPointer.find(".add-widget").on("click",(function(t){t.preventDefault(),i.customHtmlWidgetPointer.hide(),i.openAvailableWidgetsPanel()}))),i.pasteHtmlPointer=i.$el.find(".wp-pointer.paste-html-pointer"),i.pasteHtmlPointer.length&&i.pasteHtmlPointer.find(".close").on("click",(function(t){t.preventDefault(),i.pasteHtmlPointer.hide(),i.editor.focus(),i.dismissPointers(["text_widget_custom_html","text_widget_paste_html"])})),i.fields={title:i.$el.find(".title"),text:i.$el.find(".text")},_.each(i.fields,(function(t,e){t.on("input change",(function(){var n=i.syncContainer.find(".sync-input."+e);n.val()!==t.val()&&(n.val(t.val()),n.trigger("change"))})),t.val(i.syncContainer.find(".sync-input."+e).val())}))},dismissPointers:function(t){_.each(t,(function(t){wp.ajax.post("dismiss-wp-pointer",{pointer:t}),wp.textWidgets.dismissedPointers.push(t)}))},openAvailableWidgetsPanel:function(){var t;wp.customize.section.each((function(e){e.extended(wp.customize.Widgets.SidebarSection)&&e.expanded()&&(t=wp.customize.control("sidebars_widgets["+e.params.sidebarId+"]"))})),t&&setTimeout((function(){wp.customize.Widgets.availableWidgetsPanel.open(t),wp.customize.Widgets.availableWidgetsPanel.$search.val("HTML").trigger("keyup")}))},updateFields:function(){var t;this.fields.title.is(document.activeElement)||(t=this.syncContainer.find(".sync-input.title"),this.fields.title.val(t.val())),t=this.syncContainer.find(".sync-input.text"),this.fields.text.is(":visible")?this.fields.text.is(document.activeElement)||this.fields.text.val(t.val()):this.editor&&!this.editorFocused&&t.val()!==this.fields.text.val()&&this.editor.setContent(wp.oldEditor.autop(t.val()))},initializeEditor:function(){var e,i,n,o,s=this,d=!1,c=!1;i=s.fields.text,e=i.attr("id"),o=i.val(),n=function(){s.editor.isDirty()&&(wp.customize&&wp.customize.state&&(wp.customize.state("processing").set(wp.customize.state("processing").get()+1),_.delay((function(){wp.customize.state("processing").set(wp.customize.state("processing").get()-1)}),300)),s.editor.isHidden()||s.editor.save()),c&&o!==i.val()&&(i.trigger("change"),c=!1,o=i.val())},s.syncContainer.closest(".widget").find("[name=savewidget]:first").on("click",(function(){n()})),function i(){var o,l,a;if(document.getElementById(e))if(void 0!==window.tinymce){if(tinymce.get(e)&&(d=tinymce.get(e).isHidden(),wp.oldEditor.remove(e)),t(document).one("wp-before-tinymce-init.text-widget-init",(function(t,e){e.plugins&&(/\bwpview\b/.test(e.plugins)||(e.plugins+=",wpview"))})),wp.oldEditor.initialize(e,{tinymce:{wpautop:!0},quicktags:!0,mediaButtons:!0}),a=function(e){e.show(),e.find(".close").focus(),wp.a11y.speak(e.find("h3, p").map((function(){return t(this).text()})).get().join("\n\n"))},!(o=window.tinymce.get(e)))throw new Error("Failed to initialize editor");l=function(){t(o.getWin()).on("unload",(function(){_.defer(i)})),d&&switchEditors.go(e,"html"),t("#"+e+"-html").on("click",(function(){s.pasteHtmlPointer.hide(),-1===wp.textWidgets.dismissedPointers.indexOf("text_widget_custom_html")&&a(s.customHtmlWidgetPointer)})),t("#"+e+"-tmce").on("click",(function(){s.customHtmlWidgetPointer.hide()})),o.on("pastepreprocess",(function(t){var e=t.content;-1===wp.textWidgets.dismissedPointers.indexOf("text_widget_paste_html")&&e&&/<\w+.*?>/.test(e)&&_.delay((function(){a(s.pasteHtmlPointer)}),250)}))},o.initialized?l():o.on("init",l),s.editorFocused=!1,o.on("focus",(function(){s.editorFocused=!0})),o.on("paste",(function(){o.setDirty(!0),n()})),o.on("NodeChange",(function(){c=!0})),o.on("NodeChange",_.debounce(n,1e3)),o.on("blur hide",(function(){s.editorFocused=!1,n()})),s.editor=o}else wp.oldEditor.initialize(e,{quicktags:!0,mediaButtons:!0})}()}})}(jQuery);