subscriptions.vue 2.64 KB
Newer Older
Eric Eastwood's avatar
Eric Eastwood committed
1
<script>
2 3 4 5
  import { __ } from '~/locale';
  import icon from '~/vue_shared/components/icon.vue';
  import toggleButton from '~/vue_shared/components/toggle_button.vue';
  import tooltip from '~/vue_shared/directives/tooltip';
6
  import eventHub from '../../event_hub';
7 8 9 10 11

  const ICON_ON = 'notifications';
  const ICON_OFF = 'notifications-off';
  const LABEL_ON = __('Notifications on');
  const LABEL_OFF = __('Notifications off');
Eric Eastwood's avatar
Eric Eastwood committed
12

Filipa Lacerda's avatar
Filipa Lacerda committed
13
  export default {
14 15 16
    directives: {
      tooltip,
    },
Filipa Lacerda's avatar
Filipa Lacerda committed
17
    components: {
18
      icon,
19
      toggleButton,
Eric Eastwood's avatar
Eric Eastwood committed
20
    },
Filipa Lacerda's avatar
Filipa Lacerda committed
21 22 23 24 25 26 27 28 29
    props: {
      loading: {
        type: Boolean,
        required: false,
        default: false,
      },
      subscribed: {
        type: Boolean,
        required: false,
Mike Greiling's avatar
Mike Greiling committed
30
        default: null,
Filipa Lacerda's avatar
Filipa Lacerda committed
31 32 33 34
      },
      id: {
        type: Number,
        required: false,
Mike Greiling's avatar
Mike Greiling committed
35
        default: null,
Filipa Lacerda's avatar
Filipa Lacerda committed
36
      },
Eric Eastwood's avatar
Eric Eastwood committed
37
    },
Filipa Lacerda's avatar
Filipa Lacerda committed
38
    computed: {
39
      showLoadingState() {
40
        return this.subscribed === null;
Filipa Lacerda's avatar
Filipa Lacerda committed
41
      },
42
      notificationIcon() {
43
        return this.subscribed ? ICON_ON : ICON_OFF;
44
      },
45 46
      notificationTooltip() {
        return this.subscribed ? LABEL_ON : LABEL_OFF;
Mike Greiling's avatar
Mike Greiling committed
47
      },
Eric Eastwood's avatar
Eric Eastwood committed
48
    },
Filipa Lacerda's avatar
Filipa Lacerda committed
49
    methods: {
50 51 52 53 54 55 56 57 58 59
      /**
       * We need to emit this event on both component & eventHub
       * for 2 dependencies;
       *
       * 1. eventHub: This component is used in Issue Boards sidebar
       *              where component template is part of HAML
       *              and event listeners are tied to app's eventHub.
       * 2. Component: This compone is also used in Epics in EE
       *               where listeners are tied to component event.
       */
Filipa Lacerda's avatar
Filipa Lacerda committed
60
      toggleSubscription() {
61
        // App's eventHub event emission.
62
        eventHub.$emit('toggleSubscription', this.id);
63 64 65 66 67 68

        // Component event emission.
        this.$emit('toggleSubscription', this.id);
      },
      onClickCollapsedIcon() {
        this.$emit('toggleSidebar');
Filipa Lacerda's avatar
Filipa Lacerda committed
69
      },
Eric Eastwood's avatar
Eric Eastwood committed
70
    },
Filipa Lacerda's avatar
Filipa Lacerda committed
71
  };
Eric Eastwood's avatar
Eric Eastwood committed
72 73 74 75
</script>

<template>
  <div>
76 77 78 79
    <div
      class="sidebar-collapsed-icon"
      @click="onClickCollapsedIcon"
    >
80 81 82 83 84 85 86 87 88 89 90 91 92
      <span
        v-tooltip
        :title="notificationTooltip"
        data-container="body"
        data-placement="left"
      >
        <icon
          :name="notificationIcon"
          :size="16"
          aria-hidden="true"
          class="sidebar-item-icon is-active"
        />
      </span>
Eric Eastwood's avatar
Eric Eastwood committed
93 94 95 96
    </div>
    <span class="issuable-header-text hide-collapsed pull-left">
      {{ __('Notifications') }}
    </span>
97
    <toggle-button
Mike Greiling's avatar
Mike Greiling committed
98
      ref="toggleButton"
99 100 101 102
      class="pull-right hide-collapsed js-issuable-subscribe-button"
      :is-loading="showLoadingState"
      :value="subscribed"
      @change="toggleSubscription"
Eric Eastwood's avatar
Eric Eastwood committed
103 104 105
    />
  </div>
</template>