<commit_before>// Copyright 2018 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.

import * as Common from '../common/common.js';
import * as UI from '../ui/ui.js';

/**
 * @implements {UI.ListWidget.Delegate}
 * @unrestricted
 */
export class GeolocationsSettingsTab extends UI.Widget.VBox {
  constructor() {
    super(true);
    this.registerRequiredCSS('emulation/geolocationsSettingsTab.css');

    this.contentElement.createChild('div', 'header').textContent = Common.UIString.UIString('Custom Geolocations');

    const addButton = UI.UIUtils.createTextButton(
        Common.UIString.UIString('Add location...'), this._addButtonClicked.bind(this), 'add-geolocations-button');
    this.contentElement.appendChild(addButton);

    this._list = new UI.ListWidget.ListWidget(this);
    this._list.element.classList.add('geolocations-list');
    this._list.registerRequiredCSS('emulation/geolocationsSettingsTab.css');
    this._list.show(this.contentElement);

    this._customSetting = self.Common.settings.moduleSetting('emulation.geolocations');
    this._customSetting.addChangeListener(this._geolocationsUpdated, this);

    this.setDefaultFocusedElement(addButton);
  }

  /**
   * @override
   */
  wasShown() {
    super.wasShown();
    this._geolocationsUpdated();
  }

  _geolocationsUpdated() {
    this._list.clear();

    const conditions = this._customSetting.get();
    for (let i = 0; i < conditions.length; ++i) {
      this._list.appendItem(conditions[i], true);
    }

    this._list.appendSeparator();
  }

  _addButtonClicked() {
    this._list.addNewItem(this._customSetting.get().length, {title: '', lat: 0, long: 0, timezoneId: ''});
  }

  /**
   * @override
   * @param {*} item
   * @param {boolean} editable
   * @return {!Element}
   */
  renderItem(item, editable) {
    const geolocation = /** @type {!Emulation.GeolocationsSettingsTab.Item} */ (item);
    const element = createElementWithClass('div', 'geolocations-list-item');
    const title = element.createChild('div', 'geolocations-list-text geolocations-list-title');
    const titleText = title.createChild('div', 'geolocations-list-title-text');
    titleText.textContent = geolocation.title;
    titleText.title = geolocation.title;
    element.createChild('div', 'geolocations-list-separator');
    element.createChild('div', 'geolocations-list-text').textContent = geolocation.lat;
    element.createChild('div', 'geolocations-list-separator');
    element.createChild('div', 'geolocations-list-text').textContent = geolocation.long;
    element.createChild('div', 'geolocations-list-separator');
    element.createChild('div', 'geolocations-list-text').textContent = geolocation.timezoneId;
    return element;
  }

  /**
   * @override
   * @param {*} item
   * @param {number} index
   */
  removeItemRequested(item, index) {
    const list = this._customSetting.get();
    list.splice(index, 1);
    this._customSetting.set(list);
  }

  /**
   * @override
   * @param {*} item
   * @param {!UI.ListWidget.Editor} editor
   * @param {boolean} isNew
   */
  commitEdit(item, editor, isNew) {
    const geolocation = /** @type {?Emulation.GeolocationsSettingsTab.Item} */ (item);
    geolocation.title = editor.control('title').value.trim();
    const lat = editor.control('lat').value.trim();
    geolocation.lat = lat ? parseFloat(lat) : 0;
    const long = editor.control('long').value.trim();
    geolocation.long = long ? parseFloat(long) : 0;
    const timezoneId = editor.control('timezoneId').value.trim();
    geolocation.timezoneId = timezoneId;

    const list = this._customSetting.get();
    if (isNew) {
      list.push(geolocation);
    }
    this._customSetting.set(list);
  }

  /**
   * @override
   * @param {*} item
   * @return {!UI.ListWidget.Editor}
   */
  beginEdit(item) {
    const geolocation = /** @type {?Emulation.GeolocationsSettingsTab.Item} */ (item);
    const editor = this._createEditor();
    editor.control('title').value = geolocation.title;
    editor.control('lat').value = String(geolocation.lat);
    editor.control('long').value = String(geolocation.long);
    editor.control('timezoneId').value = String(geolocation.timezoneId);
    return editor;
  }

  /**
   * @return {!UI.ListWidget.Editor}
   */
  _createEditor() {
    if (this._editor) {
      return this._editor;
    }

    const editor = new UI.ListWidget.Editor();
    this._editor = editor;
    const content = editor.contentElement();

    const titles = content.createChild('div', 'geolocations-edit-row');
    titles.createChild('div', 'geolocations-list-text geolocations-list-title').textContent =
        Common.UIString.UIString('Location name');
    titles.createChild('div', 'geolocations-list-separator geolocations-list-separator-invisible');
    titles.createChild('div', 'geolocations-list-text').textContent = Common.UIString.UIString('Lat');
    titles.createChild('div', 'geolocations-list-separator geolocations-list-separator-invisible');
    titles.createChild('div', 'geolocations-list-text').textContent = Common.UIString.UIString('Long');
    titles.createChild('div', 'geolocations-list-separator geolocations-list-separator-invisible');
    titles.createChild('div', 'geolocations-list-text').textContent = Common.UIString.UIString('Timezone ID');

    const fields = content.createChild('div', 'geolocations-edit-row');
    fields.createChild('div', 'geolocations-list-text geolocations-list-title')
        .appendChild(editor.createInput('title', 'text', ls`Location name`, titleValidator));
    fields.createChild('div', 'geolocations-list-separator geolocations-list-separator-invisible');

    let cell = fields.createChild('div', 'geolocations-list-text');
    cell.appendChild(editor.createInput('lat', 'text', ls`Latitude`, latValidator));
    fields.createChild('div', 'geolocations-list-separator geolocations-list-separator-invisible');

    cell = fields.createChild('div', 'geolocations-list-text');
    cell.appendChild(editor.createInput('long', 'text', ls`Longitude`, longValidator));

    cell = fields.createChild('div', 'geolocations-list-text');
    cell.appendChild(editor.createInput('timezoneId', 'text', ls`Timezone ID`, timezoneIdValidator));

    return editor;

    /**
     * @param {*} item
     * @param {number} index
     * @param {!HTMLInputElement|!HTMLSelectElement} input
     * @return {!UI.ListWidget.ValidatorResult}
     */
    function titleValidator(item, index, input) {
      const maxLength = 50;
      const value = input.value.trim();

      let errorMessage;
      if (!value.length) {
        errorMessage = ls`Location name cannot be empty`;
      } else if (value.length > maxLength) {
        errorMessage = ls`Location name must be less than ${maxLength} characters`;<commit_msg>Fix layout issue in add-location-table<commit_after>// Copyright 2018 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.

import * as Common from '../common/common.js';
import * as UI from '../ui/ui.js';

/**
 * @implements {UI.ListWidget.Delegate}
 * @unrestricted
 */
export class GeolocationsSettingsTab extends UI.Widget.VBox {
  constructor() {
    super(true);
    this.registerRequiredCSS('emulation/geolocationsSettingsTab.css');

    this.contentElement.createChild('div', 'header').textContent = Common.UIString.UIString('Custom Geolocations');

    const addButton = UI.UIUtils.createTextButton(
        Common.UIString.UIString('Add location...'), this._addButtonClicked.bind(this), 'add-geolocations-button');
    this.contentElement.appendChild(addButton);

    this._list = new UI.ListWidget.ListWidget(this);
    this._list.element.classList.add('geolocations-list');
    this._list.registerRequiredCSS('emulation/geolocationsSettingsTab.css');
    this._list.show(this.contentElement);

    this._customSetting = self.Common.settings.moduleSetting('emulation.geolocations');
    this._customSetting.addChangeListener(this._geolocationsUpdated, this);

    this.setDefaultFocusedElement(addButton);
  }

  /**
   * @override
   */
  wasShown() {
    super.wasShown();
    this._geolocationsUpdated();
  }

  _geolocationsUpdated() {
    this._list.clear();

    const conditions = this._customSetting.get();
    for (let i = 0; i < conditions.length; ++i) {
      this._list.appendItem(conditions[i], true);
    }

    this._list.appendSeparator();
  }

  _addButtonClicked() {
    this._list.addNewItem(this._customSetting.get().length, {title: '', lat: 0, long: 0, timezoneId: ''});
  }

  /**
   * @override
   * @param {*} item
   * @param {boolean} editable
   * @return {!Element}
   */
  renderItem(item, editable) {
    const geolocation = /** @type {!Emulation.GeolocationsSettingsTab.Item} */ (item);
    const element = createElementWithClass('div', 'geolocations-list-item');
    const title = element.createChild('div', 'geolocations-list-text geolocations-list-title');
    const titleText = title.createChild('div', 'geolocations-list-title-text');
    titleText.textContent = geolocation.title;
    titleText.title = geolocation.title;
    element.createChild('div', 'geolocations-list-separator');
    element.createChild('div', 'geolocations-list-text').textContent = geolocation.lat;
    element.createChild('div', 'geolocations-list-separator');
    element.createChild('div', 'geolocations-list-text').textContent = geolocation.long;
    element.createChild('div', 'geolocations-list-separator');
    element.createChild('div', 'geolocations-list-text').textContent = geolocation.timezoneId;
    return element;
  }

  /**
   * @override
   * @param {*} item
   * @param {number} index
   */
  removeItemRequested(item, index) {
    const list = this._customSetting.get();
    list.splice(index, 1);
    this._customSetting.set(list);
  }

  /**
   * @override
   * @param {*} item
   * @param {!UI.ListWidget.Editor} editor
   * @param {boolean} isNew
   */
  commitEdit(item, editor, isNew) {
    const geolocation = /** @type {?Emulation.GeolocationsSettingsTab.Item} */ (item);
    geolocation.title = editor.control('title').value.trim();
    const lat = editor.control('lat').value.trim();
    geolocation.lat = lat ? parseFloat(lat) : 0;
    const long = editor.control('long').value.trim();
    geolocation.long = long ? parseFloat(long) : 0;
    const timezoneId = editor.control('timezoneId').value.trim();
    geolocation.timezoneId = timezoneId;

    const list = this._customSetting.get();
    if (isNew) {
      list.push(geolocation);
    }
    this._customSetting.set(list);
  }

  /**
   * @override
   * @param {*} item
   * @return {!UI.ListWidget.Editor}
   */
  beginEdit(item) {
    const geolocation = /** @type {?Emulation.GeolocationsSettingsTab.Item} */ (item);
    const editor = this._createEditor();
    editor.control('title').value = geolocation.title;
    editor.control('lat').value = String(geolocation.lat);
    editor.control('long').value = String(geolocation.long);
    editor.control('timezoneId').value = String(geolocation.timezoneId);
    return editor;
  }

  /**
   * @return {!UI.ListWidget.Editor}
   */
  _createEditor() {
    if (this._editor) {
      return this._editor;
    }

    const editor = new UI.ListWidget.Editor();
    this._editor = editor;
    const content = editor.contentElement();

    const titles = content.createChild('div', 'geolocations-edit-row');
    titles.createChild('div', 'geolocations-list-text geolocations-list-title').textContent =
        Common.UIString.UIString('Location name');
    titles.createChild('div', 'geolocations-list-separator geolocations-list-separator-invisible');
    titles.createChild('div', 'geolocations-list-text').textContent = Common.UIString.UIString('Lat');
    titles.createChild('div', 'geolocations-list-separator geolocations-list-separator-invisible');
    titles.createChild('div', 'geolocations-list-text').textContent = Common.UIString.UIString('Long');
    titles.createChild('div', 'geolocations-list-separator geolocations-list-separator-invisible');
    titles.createChild('div', 'geolocations-list-text').textContent = Common.UIString.UIString('Timezone ID');

    const fields = content.createChild('div', 'geolocations-edit-row');
    fields.createChild('div', 'geolocations-list-text geolocations-list-title')
        .appendChild(editor.createInput('title', 'text', ls`Location name`, titleValidator));
    fields.createChild('div', 'geolocations-list-separator geolocations-list-separator-invisible');

    let cell = fields.createChild('div', 'geolocations-list-text');
    cell.appendChild(editor.createInput('lat', 'text', ls`Latitude`, latValidator));
    fields.createChild('div', 'geolocations-list-separator geolocations-list-separator-invisible');

    cell = fields.createChild('div', 'geolocations-list-text');
    cell.appendChild(editor.createInput('long', 'text', ls`Longitude`, longValidator));
    fields.createChild('div', 'geolocations-list-separator geolocations-list-separator-invisible');

    cell = fields.createChild('div', 'geolocations-list-text');
    cell.appendChild(editor.createInput('timezoneId', 'text', ls`Timezone ID`, timezoneIdValidator));

    return editor;

    /**
     * @param {*} item
     * @param {number} index
     * @param {!HTMLInputElement|!HTMLSelectElement} input
     * @return {!UI.ListWidget.ValidatorResult}
     */
    function titleValidator(item, index, input) {
      const maxLength = 50;
      const value = input.value.trim();

      let errorMessage;
      if (!value.length) {
        errorMessage = ls`Location name cannot be empty`;
      } else if (value.length > maxLength) {
        errorMessage = ls`Location name must be less than ${maxLength} characters`;
{'fraction_non_alphanumeric': 0.15216920522630478, 'fraction_numerical': 0.001732476719844077, 'mean_word_length': 4.026850507982584, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5891936', 'n_tokens_mistral': 4387, 'n_tokens_neox': 4191, 'n_words': 730}