1 line
5.4 KiB
JavaScript
1 line
5.4 KiB
JavaScript
wp.customHtmlWidgets=function(l){"use strict";var c={idBases:["custom_html"],codeEditorSettings:{},l10n:{errorNotice:{singular:"",plural:""}}};return c.CustomHtmlWidgetControl=Backbone.View.extend({events:{},initialize:function(e){var n=this;if(!e.el)throw new Error("Missing options.el");if(!e.syncContainer)throw new Error("Missing options.syncContainer");Backbone.View.prototype.initialize.call(n,e),n.syncContainer=e.syncContainer,n.widgetIdBase=n.syncContainer.parent().find(".id_base").val(),n.widgetNumber=n.syncContainer.parent().find(".widget_number").val(),n.customizeSettingId="widget_"+n.widgetIdBase+"["+String(n.widgetNumber)+"]",n.$el.addClass("custom-html-widget-fields"),n.$el.html(wp.template("widget-custom-html-control-fields")({codeEditorDisabled:c.codeEditorSettings.disabled})),n.errorNoticeContainer=n.$el.find(".code-editor-error-container"),n.currentErrorAnnotations=[],n.saveButton=n.syncContainer.add(n.syncContainer.parent().find(".widget-control-actions")).find(".widget-control-save, #savewidget"),n.saveButton.addClass("custom-html-widget-save-button"),n.fields={title:n.$el.find(".title"),content:n.$el.find(".content")},_.each(n.fields,function(t,i){t.on("input change",function(){var e=n.syncContainer.find(".sync-input."+i);e.val()!==t.val()&&(e.val(t.val()),e.trigger("change"))}),t.val(n.syncContainer.find(".sync-input."+i).val())})},updateFields:function(){var e,t=this;t.fields.title.is(document.activeElement)||(e=t.syncContainer.find(".sync-input.title"),t.fields.title.val(e.val())),t.contentUpdateBypassed=t.fields.content.is(document.activeElement)||t.editor&&t.editor.codemirror.state.focused||0!==t.currentErrorAnnotations.length,t.contentUpdateBypassed||(e=t.syncContainer.find(".sync-input.content"),t.fields.content.val(e.val()))},updateErrorNotice:function(e){var t,i,n=this,o="";1===e.length?o=c.l10n.errorNotice.singular.replace("%d","1"):1<e.length&&(o=c.l10n.errorNotice.plural.replace("%d",String(e.length))),n.fields.content[0].setCustomValidity&&n.fields.content[0].setCustomValidity(o),wp.customize&&wp.customize.has(n.customizeSettingId)?((i=wp.customize(n.customizeSettingId)).notifications.remove("htmlhint_error"),0!==e.length&&i.notifications.add("htmlhint_error",new wp.customize.Notification("htmlhint_error",{message:o,type:"error"}))):0!==e.length?((t=l('<div class="inline notice notice-error notice-alt"></div>')).append(l("<p></p>",{text:o})),n.errorNoticeContainer.empty(),n.errorNoticeContainer.append(t),n.errorNoticeContainer.slideDown("fast"),wp.a11y.speak(o)):n.errorNoticeContainer.slideUp("fast")},initializeEditor:function(){var e,t=this;c.codeEditorSettings.disabled||(e=_.extend({},c.codeEditorSettings,{onTabPrevious:function(){t.fields.title.focus()},onTabNext:function(){t.syncContainer.add(t.syncContainer.parent().find(".widget-position, .widget-control-actions")).find(":tabbable").first().focus()},onChangeLintingErrors:function(e){t.currentErrorAnnotations=e},onUpdateErrorNotice:function(e){t.saveButton.toggleClass("validation-blocked disabled",0<e.length),t.updateErrorNotice(e)}}),t.editor=wp.codeEditor.initialize(t.fields.content,e),l(t.editor.codemirror.display.lineDiv).attr({role:"textbox","aria-multiline":"true","aria-labelledby":t.fields.content[0].id+"-label","aria-describedby":"editor-keyboard-trap-help-1 editor-keyboard-trap-help-2 editor-keyboard-trap-help-3 editor-keyboard-trap-help-4"}),l("#"+t.fields.content[0].id+"-label").on("click",function(){t.editor.codemirror.focus()}),t.fields.content.on("change",function(){this.value!==t.editor.codemirror.getValue()&&t.editor.codemirror.setValue(this.value)}),t.editor.codemirror.on("change",function(){var e=t.editor.codemirror.getValue();e!==t.fields.content.val()&&t.fields.content.val(e).trigger("change")}),t.editor.codemirror.on("blur",function(){t.contentUpdateBypassed&&t.syncContainer.find(".sync-input.content").trigger("change")}),wp.customize&&t.editor.codemirror.on("keydown",function(e,t){27===t.keyCode&&t.stopPropagation()}))}}),c.widgetControls={},c.handleWidgetAdded=function(e,t){var i,n,o,d,r,a,s;n=(i=t.find("> .widget-inside > .form, > .widget-inside > form")).find("> .id_base").val(),-1!==c.idBases.indexOf(n)&&(d=i.find(".widget-id").val(),c.widgetControls[d]||(a=l("<div></div>"),(s=t.find(".widget-content:first")).before(a),o=new c.CustomHtmlWidgetControl({el:a,syncContainer:s}),c.widgetControls[d]=o,(r=function(){(wp.customize?t.parent().hasClass("expanded"):t.hasClass("open"))?o.initializeEditor():setTimeout(r,50)})()))},c.setupAccessibleMode=function(){var e,t,i,n;0!==(e=l(".editwidget > form")).length&&(t=e.find("> .widget-control-actions > .id_base").val(),-1!==c.idBases.indexOf(t)&&(i=l("<div></div>"),(n=e.find("> .widget-inside")).before(i),new c.CustomHtmlWidgetControl({el:i,syncContainer:n}).initializeEditor()))},c.handleWidgetUpdated=function(e,t){var i,n,o,d;d=(i=t.find("> .widget-inside > .form, > .widget-inside > form")).find("> .id_base").val(),-1!==c.idBases.indexOf(d)&&(n=i.find("> .widget-id").val(),(o=c.widgetControls[n])&&o.updateFields())},c.init=function(e){var t=l(document);_.extend(c.codeEditorSettings,e),t.on("widget-added",c.handleWidgetAdded),t.on("widget-synced widget-updated",c.handleWidgetUpdated),l(function(){"widgets"===window.pagenow&&(l(".widgets-holder-wrap:not(#available-widgets)").find("div.widget").one("click.toggle-widget-expanded",function(){var e=l(this);c.handleWidgetAdded(new jQuery.Event("widget-added"),e)}),l(window).on("load",function(){c.setupAccessibleMode()}))})},c}(jQuery); |