summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/sidebar/labels_select_vue/store/getters.js
blob: d14f96720b737ac592d6eebdfc62fab429a84b04 (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
import { __, s__, sprintf } from '~/locale';
import { DropdownVariant } from '../constants';

/**
 * Returns string representing current labels
 * selection on dropdown button.
 *
 * @param {object} state
 */
export const dropdownButtonText = (state, getters) => {
  const selectedLabels = getters.isDropdownVariantSidebar
    ? state.labels.filter((label) => label.set)
    : state.selectedLabels;

  if (!selectedLabels.length) {
    return state.dropdownButtonText || __('Label');
  } else if (selectedLabels.length > 1) {
    return sprintf(s__('LabelSelect|%{firstLabelName} +%{remainingLabelCount} more'), {
      firstLabelName: selectedLabels[0].title,
      remainingLabelCount: selectedLabels.length - 1,
    });
  }
  return selectedLabels[0].title;
};

/**
 * Returns array containing only label IDs from
 * selectedLabels array.
 * @param {object} state
 */
export const selectedLabelsList = (state) => state.selectedLabels.map((label) => label.id);

/**
 * Returns boolean representing whether dropdown variant
 * is `sidebar`
 * @param {object} state
 */
export const isDropdownVariantSidebar = (state) => state.variant === DropdownVariant.Sidebar;

/**
 * Returns boolean representing whether dropdown variant
 * is `standalone`
 * @param {object} state
 */
export const isDropdownVariantStandalone = (state) => state.variant === DropdownVariant.Standalone;

/**
 * Returns boolean representing whether dropdown variant
 * is `embedded`
 * @param {object} state
 */
export const isDropdownVariantEmbedded = (state) => state.variant === DropdownVariant.Embedded;