summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/editor/components/source_editor_toolbar_button.vue
blob: 2595d67af3419d5c416b7b98923b746937321d3a (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
<script>
import { GlButton, GlTooltipDirective } from '@gitlab/ui';
import updateToolbarItemMutation from '~/editor/graphql/update_item.mutation.graphql';
import getToolbarItemQuery from '~/editor/graphql/get_item.query.graphql';

export default {
  name: 'SourceEditorToolbarButton',
  components: {
    GlButton,
  },
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  props: {
    button: {
      type: Object,
      required: false,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      buttonItem: this.button,
    };
  },
  apollo: {
    buttonItem: {
      query: getToolbarItemQuery,
      variables() {
        return {
          id: this.button.id,
        };
      },
      update({ item }) {
        return item;
      },
      skip() {
        return !this.button.id;
      },
    },
  },
  computed: {
    icon() {
      return this.buttonItem.selected
        ? this.buttonItem.selectedIcon || this.buttonItem.icon
        : this.buttonItem.icon;
    },
    label() {
      return this.buttonItem.selected
        ? this.buttonItem.selectedLabel || this.buttonItem.label
        : this.buttonItem.label;
    },
  },
  methods: {
    clickHandler() {
      if (this.buttonItem.onClick) {
        this.buttonItem.onClick();
      }
      this.$apollo.mutate({
        mutation: updateToolbarItemMutation,
        variables: {
          id: this.buttonItem.id,
          propsToUpdate: {
            selected: !this.buttonItem.selected,
          },
        },
      });
      this.$emit('click');
    },
  },
};
</script>
<template>
  <div>
    <gl-button
      v-gl-tooltip.hover
      :category="buttonItem.category"
      :variant="buttonItem.variant"
      type="button"
      :selected="buttonItem.selected"
      :icon="icon"
      :title="label"
      :aria-label="label"
      @click="clickHandler"
    />
  </div>
</template>