aboutsummaryrefslogtreecommitdiff
path: root/srcs/wordpress/wp-admin/js/theme-plugin-editor.min.js
blob: 4f5fe69cf9e896cd8e70351b66e9d9da8ea04ecb (plain)
1
window.wp||(window.wp={}),wp.themePluginEditor=function(s){"use strict";var o,t;o={l10n:{lintError:{singular:"",plural:""},saveAlert:"",saveError:""},codeEditor:{},instance:null,noticeElements:{},dirty:!1,lintErrors:[],init:function(e,t){o.form=e,t&&s.extend(o,t),o.noticeTemplate=wp.template("wp-file-editor-notice"),o.noticesContainer=o.form.find(".editor-notices"),o.submitButton=o.form.find(":input[name=submit]"),o.spinner=o.form.find(".submit .spinner"),o.form.on("submit",o.submit),o.textarea=o.form.find("#newcontent"),o.textarea.on("change",o.onChange),o.warning=s(".file-editor-warning"),o.docsLookUpButton=o.form.find("#docs-lookup"),o.docsLookUpList=o.form.find("#docs-list"),0<o.warning.length&&o.showWarning(),!1!==o.codeEditor&&_.defer(function(){o.initCodeEditor()}),s(o.initFileBrowser),s(window).on("beforeunload",function(){if(o.dirty)return o.l10n.saveAlert}),o.docsLookUpList.on("change",function(){""===s(this).val()?o.docsLookUpButton.prop("disabled",!0):o.docsLookUpButton.prop("disabled",!1)})},showWarning:function(){var e=o.warning.find(".file-editor-warning-message").text();s("#wpwrap").attr("aria-hidden","true"),s(document.body).addClass("modal-open").append(o.warning.detach()),o.warning.removeClass("hidden").find(".file-editor-warning-go-back").focus(),o.warningTabbables=o.warning.find("a, button"),o.warningTabbables.on("keydown",o.constrainTabbing),o.warning.on("click",".file-editor-warning-dismiss",o.dismissWarning),setTimeout(function(){wp.a11y.speak(wp.sanitize.stripTags(e.replace(/\s+/g," ")),"assertive")},1e3)},constrainTabbing:function(e){var t,i;9===e.which&&(t=o.warningTabbables.first()[0],(i=o.warningTabbables.last()[0])!==e.target||e.shiftKey?t===e.target&&e.shiftKey&&(i.focus(),e.preventDefault()):(t.focus(),e.preventDefault()))},dismissWarning:function(){wp.ajax.post("dismiss-wp-pointer",{pointer:o.themeOrPlugin+"_editor_notice"}),o.warning.remove(),s("#wpwrap").removeAttr("aria-hidden"),s("body").removeClass("modal-open")},onChange:function(){o.dirty=!0,o.removeNotice("file_saved")},submit:function(e){var t,i={};e.preventDefault(),s.each(o.form.serializeArray(),function(){i[this.name]=this.value}),o.instance&&(i.newcontent=o.instance.codemirror.getValue()),o.isSaving||(o.lintErrors.length?o.instance.codemirror.setCursor(o.lintErrors[0].from.line):(o.isSaving=!0,o.textarea.prop("readonly",!0),o.instance&&o.instance.codemirror.setOption("readOnly",!0),o.spinner.addClass("is-active"),t=wp.ajax.post("edit-theme-plugin-file",i),o.lastSaveNoticeCode&&o.removeNotice(o.lastSaveNoticeCode),t.done(function(e){o.lastSaveNoticeCode="file_saved",o.addNotice({code:o.lastSaveNoticeCode,type:"success",message:e.message,dismissible:!0}),o.dirty=!1}),t.fail(function(e){var t=s.extend({code:"save_error",message:o.l10n.saveError},e,{type:"error",dismissible:!0});o.lastSaveNoticeCode=t.code,o.addNotice(t)}),t.always(function(){o.spinner.removeClass("is-active"),o.isSaving=!1,o.textarea.prop("readonly",!1),o.instance&&o.instance.codemirror.setOption("readOnly",!1)})))},addNotice:function(e){var t;if(!e.code)throw new Error("Missing code.");return o.removeNotice(e.code),(t=s(o.noticeTemplate(e))).hide(),t.find(".notice-dismiss").on("click",function(){o.removeNotice(e.code),e.onDismiss&&e.onDismiss(e)}),wp.a11y.speak(e.message),o.noticesContainer.append(t),t.slideDown("fast"),o.noticeElements[e.code]=t},removeNotice:function(e){return!!o.noticeElements[e]&&(o.noticeElements[e].slideUp("fast",function(){s(this).remove()}),delete o.noticeElements[e],!0)},initCodeEditor:function(){var i,e;(i=s.extend({},o.codeEditor)).onTabPrevious=function(){s("#templateside").find(":tabbable").last().focus()},i.onTabNext=function(){s("#template").find(":tabbable:not(.CodeMirror-code)").first().focus()},i.onChangeLintingErrors=function(e){0===(o.lintErrors=e).length&&o.submitButton.toggleClass("disabled",!1)},i.onUpdateErrorNotice=function(e){var t;o.submitButton.toggleClass("disabled",0<e.length),0!==e.length?(t=1===e.length?o.l10n.lintError.singular.replace("%d","1"):o.l10n.lintError.plural.replace("%d",String(e.length)),o.addNotice({code:"lint_errors",type:"error",message:t,dismissible:!1}).find("input[type=checkbox]").on("click",function(){i.onChangeLintingErrors([]),o.removeNotice("lint_errors")})):o.removeNotice("lint_errors")},(e=wp.codeEditor.initialize(s("#newcontent"),i)).codemirror.on("change",o.onChange),s(e.codemirror.display.lineDiv).attr({role:"textbox","aria-multiline":"true","aria-labelledby":"theme-plugin-editor-label","aria-describedby":"editor-keyboard-trap-help-1 editor-keyboard-trap-help-2 editor-keyboard-trap-help-3 editor-keyboard-trap-help-4"}),s("#theme-plugin-editor-label").on("click",function(){e.codemirror.focus()}),o.instance=e},initFileBrowser:function(){var e=s("#templateside");e.find('[role="group"]').parent().attr("aria-expanded",!1),e.find(".notice").parents("[aria-expanded]").attr("aria-expanded",!0),e.find('[role="tree"]').each(function(){new t(this).init()}),e.find(".current-file:first").each(function(){this.scrollIntoViewIfNeeded?this.scrollIntoViewIfNeeded():this.scrollIntoView(!1)})}};var n=(e.prototype.init=function(){this.domNode.tabIndex=-1,this.domNode.getAttribute("role")||this.domNode.setAttribute("role","treeitem"),this.domNode.addEventListener("keydown",this.handleKeydown.bind(this)),this.domNode.addEventListener("click",this.handleClick.bind(this)),this.domNode.addEventListener("focus",this.handleFocus.bind(this)),this.domNode.addEventListener("blur",this.handleBlur.bind(this)),this.isExpandable?(this.domNode.firstElementChild.addEventListener("mouseover",this.handleMouseOver.bind(this)),this.domNode.firstElementChild.addEventListener("mouseout",this.handleMouseOut.bind(this))):(this.domNode.addEventListener("mouseover",this.handleMouseOver.bind(this)),this.domNode.addEventListener("mouseout",this.handleMouseOut.bind(this)))},e.prototype.isExpanded=function(){return!!this.isExpandable&&"true"===this.domNode.getAttribute("aria-expanded")},e.prototype.handleKeydown=function(e){e.currentTarget;var t=!1,i=e.key;function s(e){return 1===e.length&&e.match(/\S/)}function o(e){"*"==i?(e.tree.expandAllSiblingItems(e),t=!0):s(i)&&(e.tree.setFocusByFirstCharacter(e,i),t=!0)}if(this.stopDefaultClick=!1,!(e.altKey||e.ctrlKey||e.metaKey)){if(e.shift)e.keyCode==this.keyCode.SPACE||e.keyCode==this.keyCode.RETURN?(e.stopPropagation(),this.stopDefaultClick=!0):s(i)&&o(this);else switch(e.keyCode){case this.keyCode.SPACE:case this.keyCode.RETURN:this.isExpandable?(this.isExpanded()?this.tree.collapseTreeitem(this):this.tree.expandTreeitem(this),t=!0):(e.stopPropagation(),this.stopDefaultClick=!0);break;case this.keyCode.UP:this.tree.setFocusToPreviousItem(this),t=!0;break;case this.keyCode.DOWN:this.tree.setFocusToNextItem(this),t=!0;break;case this.keyCode.RIGHT:this.isExpandable&&(this.isExpanded()?this.tree.setFocusToNextItem(this):this.tree.expandTreeitem(this)),t=!0;break;case this.keyCode.LEFT:this.isExpandable&&this.isExpanded()?(this.tree.collapseTreeitem(this),t=!0):this.inGroup&&(this.tree.setFocusToParentItem(this),t=!0);break;case this.keyCode.HOME:this.tree.setFocusToFirstItem(),t=!0;break;case this.keyCode.END:this</