Welcome to mirror list, hosted at ThFree Co, Russian Federation.

tooltip_on_truncate.vue « tooltip_on_truncate « components « vue_shared « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9ba5e8724f963d477b8cf1f0f21592c1968b296f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<script>
import { GlTooltipDirective, GlResizeObserverDirective } from '@gitlab/ui';
import { isFunction, debounce } from 'lodash';
import { hasHorizontalOverflow } from '~/lib/utils/dom_utils';

const UPDATE_TOOLTIP_DEBOUNCED_WAIT_MS = 300;

export default {
  directives: {
    GlTooltip: GlTooltipDirective,
    GlResizeObserver: GlResizeObserverDirective,
  },
  props: {
    title: {
      type: String,
      required: false,
      default: '',
    },
    placement: {
      type: String,
      required: false,
      default: 'top',
    },
    boundary: {
      type: String,
      required: false,
      default: '',
    },
    truncateTarget: {
      type: [String, Function],
      required: false,
      default: '',
    },
  },
  data() {
    return {
      tooltipDisabled: true,
    };
  },
  computed: {
    classes() {
      if (this.tooltipDisabled) {
        return '';
      }
      return 'js-show-tooltip';
    },
    tooltip() {
      return {
        title: this.title,
        placement: this.placement,
        disabled: this.tooltipDisabled,
        // Only set the tooltip boundary if it's truthy
        ...(this.boundary && { boundary: this.boundary }),
      };
    },
  },
  watch: {
    title() {
      // Wait on $nextTick in case the slot width changes
      this.$nextTick(this.updateTooltip);
    },
  },
  created() {
    this.updateTooltipDebounced = debounce(this.updateTooltip, UPDATE_TOOLTIP_DEBOUNCED_WAIT_MS);
  },
  mounted() {
    this.updateTooltip();
  },
  methods: {
    selectTarget() {
      if (isFunction(this.truncateTarget)) {
        return this.truncateTarget(this.$el);
      }
      if (this.truncateTarget === 'child') {
        return this.$el.childNodes[0];
      }
      return this.$el;
    },
    updateTooltip() {
      this.tooltipDisabled = !hasHorizontalOverflow(this.selectTarget());
    },
    onResize() {
      this.updateTooltipDebounced();
    },
  },
};
</script>

<template>
  <span v-gl-tooltip="tooltip" v-gl-resize-observer="onResize" :class="classes" class="gl-min-w-0">
    <slot></slot>
  </span>
</template>