<commit_before>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview A simple virtual keyboard implementation.
 */

/**
 * The keyboard layout name currently in use.
 * @type {string}
 */
var currentKeyboardLayout = 'us';

/**
 * The ratio of the row height to the font size.
 * @type {number}
 */
const kFontSizeRatio = 3.5;

/**
 * Return the id attribute of the keyboard element for the given layout.
 * @param {string} layout The keyboard layout.
 * @return {string} The id attribute of the keyboard element.
 */
function getKeyboardId(layout) {
  return 'keyboard_' + layout;
}

/**
 * Return the aspect ratio of the current keyboard.
 * @param {string} layout The keyboard layout.
 * @return {number} The aspect ratio of the current keyboard.
 */
function getKeyboardAspect() {
  return KEYBOARDS[currentKeyboardLayout]['aspect'];
}

/**
 * Calculate the height of the keyboard based on the size of the page.
 * @return {number} The height of the keyboard in pixels.
 */
function getKeyboardHeight() {
  var x = window.innerWidth;
  var y = window.innerHeight - ((imeui && imeui.visible) ? IME_HEIGHT : 0);
  return (x > getKeyboardAspect() * y) ?
      y : Math.floor(x / getKeyboardAspect());
}

/**
 * Set the keyboard mode.
 * @param {string} mode The new mode.
 * @return {void}
 */
function setMode(mode) {
  var rows = KEYBOARDS[currentKeyboardLayout]['rows'];
  for (var i = 0; i < rows.length; ++i) {
    rows[i].showMode(mode);
  }
}

/**
 * Create a DOM of the keyboard rows for the given keyboard layout.
 * Do nothing if the DOM is already created.
 * @param {string} layout The keyboard layout for which rows are created.
 * @param {Element} The DOM Element to which rows are appended.
 * @return {void}
 */
function initRows(layout, element) {
  var keyboard = KEYBOARDS[layout];
  if ('rows' in keyboard) {
    return;
  }
  var def = keyboard['definition'];
  var rows = [];
  for (var i = 0; i < def.length; ++i) {
    rows.push(new Row(i, def[i]));
  }
  keyboard['rows'] = rows;

  // A div element which holds rows for the layout.
  var rowsDiv = document.createElement('div');
  rowsDiv.className = 'rows';
  for (var i = 0; i < rows.length; ++i) {
    rowsDiv.appendChild(rows[i].makeDOM());
    rows[i].showMode(currentMode);
  }
  keyboard['rowsDiv'] = rowsDiv;
  element.appendChild(rowsDiv);
}

/**
 * Create a DOM of the handwriting canvas for the given keyboard layout.
 * Do nothing if the DOM is already created or the layout doesn't have canvas.
 * @param {string} layout The keyboard layout for which canvas is created.
 * @param {Element} The DOM Element to which canvas is appended.
 * @return {void}
 */
function initHandwritingCanvas(layout, element) {
  var keyboard = KEYBOARDS[layout];
  if (!('canvas' in keyboard) || keyboard['canvas']) {
    return;
  }
  var canvas = new HandwritingCanvas();
  canvas.className = 'handwritingcanvas';
  var border = 1;
  var marginTop = 5;
  var canvasHeight = getKeyboardHeight() - 2 * border - marginTop;
  canvas.resize(canvasHeight);
  keyboard['canvas'] = canvas;
  element.appendChild(canvas);
}

/**
 * Create a DOM of the keyboard for the given keyboard layout.
 * Do nothing if the DOM is already created.
 * @param {string} layout The keyboard layout for which keyboard is created.
 * @param {Element} The DOM Element to which keyboard is appended.
 * @return {void}
 */
function initKeyboard(layout, element) {
  var keyboard = KEYBOARDS[layout];
  if (!keyboard || keyboard['keyboardDiv']) {
    return;
  }
  var keyboardDiv = document.createElement('div');
  keyboardDiv.id = getKeyboardId(layout);
  keyboardDiv.className = 'keyboard';
  initRows(layout, keyboardDiv);
  initHandwritingCanvas(layout, keyboardDiv);
  keyboard['keyboardDiv'] = keyboardDiv;
  window.onresize();
  element.appendChild(keyboardDiv);
}

/**
 * Resize the keyboard according to the new window size.
 * @return {void}
 */
window.onresize = function() {
  var keyboardDiv = KEYBOARDS[currentKeyboardLayout]['keyboardDiv'];
  var height = getKeyboardHeight();
  keyboardDiv.style.height = height + 'px';
  var mainDiv = document.getElementById('main');
  mainDiv.style.width = Math.floor(getKeyboardAspect() * height) + 'px';
  var rowsLength = KEYBOARDS[currentKeyboardLayout]['rows'].length;
  keyboardDiv.style.fontSize = (height / kFontSizeRatio / rowsLength) + 'px';
  updateIme();
}

/**
 * Init the keyboard.
 * @return {void}
 */
window.onload = function() {
  var body = document.getElementById('b');

  var mainDiv = document.createElement('div');
  mainDiv.className = 'main';
  mainDiv.id = 'main';
  body.appendChild(mainDiv);

  initIme(mainDiv);
  initKeyboard(currentKeyboardLayout, mainDiv);

  window.onhashchange();

  chrome.experimental.input.onTextInputTypeChanged.addListener(function(type) {
    switch(type) {
      case "text":
        currentMode = SHIFT_MODE;
        break;
      case "password":
      case "search":
      case "url":
        currentMode = KEY_MODE;
        break;
      case "number":
      case "tel":
        currentMode = NUMBER_MODE;
        break;
      default:
        currentMode = KEY_MODE;
        break;
    }
    setMode(currentMode);
  });
}
// TODO(bryeung): would be nice to leave less gutter (without causing
// rendering issues with floated divs wrapping at some sizes).

/**
 * Switch the keyboard layout based on the current URL hash.
 * @return {void}
 */
window.onhashchange = function() {
  var old_layout = currentKeyboardLayout;
  var new_layout = location.hash.replace(/^#/, "");
  if (old_layout == new_layout) {
    return;
  }<commit_msg>Add "email" type to virtual keyboard.<commit_after>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview A simple virtual keyboard implementation.
 */

/**
 * The keyboard layout name currently in use.
 * @type {string}
 */
var currentKeyboardLayout = 'us';

/**
 * The ratio of the row height to the font size.
 * @type {number}
 */
const kFontSizeRatio = 3.5;

/**
 * Return the id attribute of the keyboard element for the given layout.
 * @param {string} layout The keyboard layout.
 * @return {string} The id attribute of the keyboard element.
 */
function getKeyboardId(layout) {
  return 'keyboard_' + layout;
}

/**
 * Return the aspect ratio of the current keyboard.
 * @param {string} layout The keyboard layout.
 * @return {number} The aspect ratio of the current keyboard.
 */
function getKeyboardAspect() {
  return KEYBOARDS[currentKeyboardLayout]['aspect'];
}

/**
 * Calculate the height of the keyboard based on the size of the page.
 * @return {number} The height of the keyboard in pixels.
 */
function getKeyboardHeight() {
  var x = window.innerWidth;
  var y = window.innerHeight - ((imeui && imeui.visible) ? IME_HEIGHT : 0);
  return (x > getKeyboardAspect() * y) ?
      y : Math.floor(x / getKeyboardAspect());
}

/**
 * Set the keyboard mode.
 * @param {string} mode The new mode.
 * @return {void}
 */
function setMode(mode) {
  var rows = KEYBOARDS[currentKeyboardLayout]['rows'];
  for (var i = 0; i < rows.length; ++i) {
    rows[i].showMode(mode);
  }
}

/**
 * Create a DOM of the keyboard rows for the given keyboard layout.
 * Do nothing if the DOM is already created.
 * @param {string} layout The keyboard layout for which rows are created.
 * @param {Element} The DOM Element to which rows are appended.
 * @return {void}
 */
function initRows(layout, element) {
  var keyboard = KEYBOARDS[layout];
  if ('rows' in keyboard) {
    return;
  }
  var def = keyboard['definition'];
  var rows = [];
  for (var i = 0; i < def.length; ++i) {
    rows.push(new Row(i, def[i]));
  }
  keyboard['rows'] = rows;

  // A div element which holds rows for the layout.
  var rowsDiv = document.createElement('div');
  rowsDiv.className = 'rows';
  for (var i = 0; i < rows.length; ++i) {
    rowsDiv.appendChild(rows[i].makeDOM());
    rows[i].showMode(currentMode);
  }
  keyboard['rowsDiv'] = rowsDiv;
  element.appendChild(rowsDiv);
}

/**
 * Create a DOM of the handwriting canvas for the given keyboard layout.
 * Do nothing if the DOM is already created or the layout doesn't have canvas.
 * @param {string} layout The keyboard layout for which canvas is created.
 * @param {Element} The DOM Element to which canvas is appended.
 * @return {void}
 */
function initHandwritingCanvas(layout, element) {
  var keyboard = KEYBOARDS[layout];
  if (!('canvas' in keyboard) || keyboard['canvas']) {
    return;
  }
  var canvas = new HandwritingCanvas();
  canvas.className = 'handwritingcanvas';
  var border = 1;
  var marginTop = 5;
  var canvasHeight = getKeyboardHeight() - 2 * border - marginTop;
  canvas.resize(canvasHeight);
  keyboard['canvas'] = canvas;
  element.appendChild(canvas);
}

/**
 * Create a DOM of the keyboard for the given keyboard layout.
 * Do nothing if the DOM is already created.
 * @param {string} layout The keyboard layout for which keyboard is created.
 * @param {Element} The DOM Element to which keyboard is appended.
 * @return {void}
 */
function initKeyboard(layout, element) {
  var keyboard = KEYBOARDS[layout];
  if (!keyboard || keyboard['keyboardDiv']) {
    return;
  }
  var keyboardDiv = document.createElement('div');
  keyboardDiv.id = getKeyboardId(layout);
  keyboardDiv.className = 'keyboard';
  initRows(layout, keyboardDiv);
  initHandwritingCanvas(layout, keyboardDiv);
  keyboard['keyboardDiv'] = keyboardDiv;
  window.onresize();
  element.appendChild(keyboardDiv);
}

/**
 * Resize the keyboard according to the new window size.
 * @return {void}
 */
window.onresize = function() {
  var keyboardDiv = KEYBOARDS[currentKeyboardLayout]['keyboardDiv'];
  var height = getKeyboardHeight();
  keyboardDiv.style.height = height + 'px';
  var mainDiv = document.getElementById('main');
  mainDiv.style.width = Math.floor(getKeyboardAspect() * height) + 'px';
  var rowsLength = KEYBOARDS[currentKeyboardLayout]['rows'].length;
  keyboardDiv.style.fontSize = (height / kFontSizeRatio / rowsLength) + 'px';
  updateIme();
}

/**
 * Init the keyboard.
 * @return {void}
 */
window.onload = function() {
  var body = document.getElementById('b');

  var mainDiv = document.createElement('div');
  mainDiv.className = 'main';
  mainDiv.id = 'main';
  body.appendChild(mainDiv);

  initIme(mainDiv);
  initKeyboard(currentKeyboardLayout, mainDiv);

  window.onhashchange();

  chrome.experimental.input.onTextInputTypeChanged.addListener(function(type) {
    switch(type) {
      case "text":
        currentMode = SHIFT_MODE;
        break;
      case "email":
      case "password":
      case "search":
      case "url":
        currentMode = KEY_MODE;
        break;
      case "number":
      case "tel":
        currentMode = NUMBER_MODE;
        break;
      default:
        currentMode = KEY_MODE;
        break;
    }
    setMode(currentMode);
  });
}
// TODO(bryeung): would be nice to leave less gutter (without causing
// rendering issues with floated divs wrapping at some sizes).

/**
 * Switch the keyboard layout based on the current URL hash.
 * @return {void}
 */
window.onhashchange = function() {
  var old_layout = currentKeyboardLayout;
  var new_layout = location.hash.replace(/^#/, "");
  if (old_layout == new_layout) {
    return;
  }
{'fraction_non_alphanumeric': 0.12597877153297374, 'fraction_numerical': 0.002262049765094832, 'mean_word_length': 3.6482005661140318, 'pattern_counts': {'":': 13, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2637481', 'n_tokens_mistral': 3448, 'n_tokens_neox': 3438, 'n_words': 1137}