"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)("Velikost pisave; za izbiro pritisnite navzdol."));
    this.applyLabelToSelector("formatBtn", (0, _escapeHtml.default)("Oblikovanje; za izbiro pritisnite navzdol."));
    this.applyLabelToSelector("fgColorBtn", (0, _escapeHtml.default)("Barva besedila; za izbiro pritisnite navzdol."));
    this.applyLabelToSelector("bgColorBtn", (0, _escapeHtml.default)("Barva ozadja; za izbiro pritisnite navzdol."));
    this.find("editorIframe").setAttribute("title", (0, _escapeHtml.default)("Obmo\u010Dje besedila Za pomo\u010D pritisnite ALT+F8."));
    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);
    }
  };
}