github.com/hernad/nomad@v1.6.112/ui/app/components/tooltip.js (about)

     1  /**
     2   * Copyright (c) HashiCorp, Inc.
     3   * SPDX-License-Identifier: MPL-2.0
     4   */
     5  
     6  // @ts-check
     7  import { assert } from '@ember/debug';
     8  import Component from '@glimmer/component';
     9  
    10  /**
    11   * Tooltip component that conditionally displays a truncated text.
    12   *
    13   * @class Tooltip
    14   * @extends Component
    15   */
    16  export default class Tooltip extends Component {
    17    /**
    18     * Determines if the tooltip should be displayed.
    19     * Defaults to `true` if the `condition` argument is not provided.
    20     *
    21     * @property condition
    22     * @type {boolean}
    23     * @readonly
    24     */
    25    get condition() {
    26      if (this.args.condition === undefined) return true;
    27  
    28      assert('Must pass a boolean.', typeof this.args.condition === 'boolean');
    29  
    30      return this.args.condition;
    31    }
    32  
    33    /**
    34     * Returns the truncated text to be displayed in the tooltip.
    35     * If the input text length is less than 30 characters, the input text is returned as-is.
    36     * Otherwise, the text is truncated to include the first 15 characters, followed by an ellipsis,
    37     * and then the last 10 characters.
    38     *
    39     * @property text
    40     * @type {string}
    41     * @readonly
    42     */
    43    get text() {
    44      const inputText = this.args.text?.toString();
    45      if (!inputText || inputText.length < 30) {
    46        return inputText;
    47      }
    48  
    49      const prefix = inputText.substr(0, 15).trim();
    50      const suffix = inputText
    51        .substr(inputText.length - 10, inputText.length)
    52        .trim();
    53      return `${prefix}...${suffix}`;
    54    }
    55  }