"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = accessibleEditor;

var _formatMessage = _interopRequireDefault(require("../format-message"));

var _escapeHtml = _interopRequireDefault(require("escape-html"));

/*
 * Copyright (C) 2018 - present Instructure, Inc.
 *
 * This file is part of Canvas.
 *
 * Canvas is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, version 3 of the License.
 *
 * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */
function accessibleEditor(editor, docContext) {
  this.selectors = {
    fontBtn: "div[aria-label='Font Sizes']",
    formatBtn: "div.mce-listbox.mce-last:not([aria-label])",
    fgColorBtn: "div[aria-label='Text color']",
    bgColorBtn: "div[aria-label='Background color']",
    recordBtn: "div[aria-label='Record/Upload Media']",
    menubar: ".mce-menubar",
    statusbar: ".mce-statusbar > .mce-container-body",
    editorIframe: ".mce-edit-area iframe"
  };

  this.editorElement = function () {
    return docContext.getElementById(editor.editorContainer.id);
  };

  this.find = function (selectorName) {
    return this.editorElement().querySelector(this.selectors[selectorName]);
  };

  this.applyLabelToSelector = function (selectorName, label) {
    var subElement = this.find(selectorName);

    if (subElement !== null) {
      subElement.setAttribute("aria-label", label);
    }
  };

  this.addLabels = function () {
    this.applyLabelToSelector("fontBtn", (0, _escapeHtml.default)("\u062D\u062C\u0645 \u0627\u0644\u062E\u0637\u060C \u0627\u0636\u063A\u0637 \u0644\u0623\u0633\u0641\u0644 \u0644\u0644\u062A\u062D\u062F\u064A\u062F"));
    this.applyLabelToSelector("formatBtn", (0, _escapeHtml.default)("\u0627\u0644\u062A\u0646\u0633\u064A\u0642\u060C \u0627\u0636\u063A\u0637 \u0644\u0623\u0633\u0641\u0644 \u0644\u0644\u062A\u062D\u062F\u064A\u062F"));
    this.applyLabelToSelector("fgColorBtn", (0, _escapeHtml.default)("\u0644\u0648\u0646 \u0627\u0644\u0646\u0635\u060C \u0627\u0636\u063A\u0637 \u0644\u0623\u0633\u0641\u0644 \u0644\u0644\u062A\u062D\u062F\u064A\u062F"));
    this.applyLabelToSelector("bgColorBtn", (0, _escapeHtml.default)("\u0644\u0648\u0646 \u0627\u0644\u062E\u0644\u0641\u064A\u0629\u060C \u0627\u0636\u063A\u0637 \u0644\u0623\u0633\u0641\u0644 \u0644\u0644\u062A\u062D\u062F\u064A\u062F"));
    this.find("editorIframe").setAttribute("title", (0, _escapeHtml.default)("\u0645\u0646\u0637\u0642\u0629 \u0646\u0635 \u0645\u0646\u0633\u0642. \u0627\u0636\u063A\u0637 \u0639\u0644\u0649 ALT+F8 \u0644\u0644\u0645\u0633\u0627\u0639\u062F\u0629"));
    var label = docContext.querySelector("label[for=\"".concat(editor.id, "\"]"));
    if (label) this.find("editorIframe").contentWindow.document.body.setAttribute("aria-label", label.textContent);
  }; // Hide the menubar until ALT+F9 is pressed.


  this.accessibilizeMenubar = function () {
    var menubar = this.find("menubar");

    if (menubar !== null) {
      var firstMenu = menubar.querySelector(".mce-menubtn");
      menubar.style.display = "none";
      editor.addShortcut("Alt+F9", "", function () {
        menubar.style.display = "";
        firstMenu.focus();
      });
    }
  }; // keyboard only nav gets permastuck in the statusbar in FF. If you can't
  // click with a mouse, the only way out is to refresh the page.


  this.removeStatusbarFromTabindex = function () {
    var statusbar = this.find("statusbar");

    if (statusbar !== null) {
      statusbar.setAttribute("tabindex", -1);
    }
  };
}