board_sidebar.js 4.27 KB
Newer Older
1
/* eslint-disable no-new */
2

3
import $ from 'jquery';
4
import Vue from 'vue';
5 6 7 8 9 10 11 12 13 14 15 16
import Flash from '~/flash';
import { sprintf, __ } from '~/locale';
import Sidebar from '~/right_sidebar';
import eventHub from '~/sidebar/event_hub';
import DueDateSelectors from '~/due_date_select';
import IssuableContext from '~/issuable_context';
import LabelsSelect from '~/labels_select';
import AssigneeTitle from '~/sidebar/components/assignees/assignee_title.vue';
import Assignees from '~/sidebar/components/assignees/assignees.vue';
import Subscriptions from '~/sidebar/components/subscriptions/subscriptions.vue';
import TimeTracker from '~/sidebar/components/time_tracking/time_tracker.vue';
import MilestoneSelect from '~/milestone_select';
17
import RemoveBtn from './sidebar/remove_issue.vue';
18
import boardsStore from '../stores/boards_store';
19
import { isScopedLabel } from '~/lib/utils/common_utils';
20

21
export default Vue.extend({
22
  components: {
23 24 25 26
    AssigneeTitle,
    Assignees,
    RemoveBtn,
    Subscriptions,
27
    TimeTracker,
28
  },
29
  props: {
30 31 32 33
    currentUser: {
      type: Object,
      default: () => ({}),
    },
34 35 36
  },
  data() {
    return {
37
      detail: boardsStore.detail,
38 39
      issue: {},
      list: {},
40
      loadingAssignees: false,
41 42 43
    };
  },
  computed: {
44
    showSidebar() {
45
      return Object.keys(this.issue).length;
46
    },
47
    milestoneTitle() {
48
      return this.issue.milestone ? this.issue.milestone.title : __('No Milestone');
49 50 51 52
    },
    canRemove() {
      return !this.list.preset;
    },
53 54 55 56
    hasLabels() {
      return this.issue.labels && this.issue.labels.length;
    },
    labelDropdownTitle() {
57 58 59 60 61 62
      return this.hasLabels
        ? sprintf(__('%{firstLabel} +%{labelCount} more'), {
            firstLabel: this.issue.labels[0].title,
            labelCount: this.issue.labels.length - 1,
          })
        : __('Label');
63 64 65
    },
    selectedLabels() {
      return this.hasLabels ? this.issue.labels.map(l => l.title).join(',') : '';
66
    },
67 68 69
  },
  watch: {
    detail: {
70
      handler() {
71
        if (this.issue.id !== this.detail.issue.id) {
Clement Ho's avatar
Clement Ho committed
72 73 74 75 76 77
          $('.block.assignee')
            .find('input:not(.js-vue)[name="issue[assignee_ids][]"]')
            .each((i, el) => {
              $(el).remove();
            });

78
          $('.js-issue-board-sidebar', this.$el).each((i, el) => {
79 80 81
            $(el)
              .data('glDropdown')
              .clearMenu();
82 83
          });
        }
84 85 86 87

        this.issue = this.detail.issue;
        this.list = this.detail.list;
      },
88
      deep: true,
89
    },
90
  },
91
  created() {
92 93 94 95 96 97 98 99 100 101 102 103
    // Get events from glDropdown
    eventHub.$on('sidebar.removeAssignee', this.removeAssignee);
    eventHub.$on('sidebar.addAssignee', this.addAssignee);
    eventHub.$on('sidebar.removeAllAssignees', this.removeAllAssignees);
    eventHub.$on('sidebar.saveAssignees', this.saveAssignees);
  },
  beforeDestroy() {
    eventHub.$off('sidebar.removeAssignee', this.removeAssignee);
    eventHub.$off('sidebar.addAssignee', this.addAssignee);
    eventHub.$off('sidebar.removeAllAssignees', this.removeAllAssignees);
    eventHub.$off('sidebar.saveAssignees', this.saveAssignees);
  },
104
  mounted() {
105 106 107 108 109 110
    new IssuableContext(this.currentUser);
    new MilestoneSelect();
    new DueDateSelectors();
    new LabelsSelect();
    new Sidebar();
  },
111
  methods: {
112
    closeSidebar() {
113
      this.detail.issue = {};
114
    },
115
    assignSelf() {
116 117 118 119 120 121
      // Notify gl dropdown that we are now assigning to current user
      this.$refs.assigneeBlock.dispatchEvent(new Event('assignYourself'));

      this.addAssignee(this.currentUser);
      this.saveAssignees();
    },
122
    removeAssignee(a) {
123
      boardsStore.detail.issue.removeAssignee(a);
124
    },
125
    addAssignee(a) {
126
      boardsStore.detail.issue.addAssignee(a);
127
    },
128
    removeAllAssignees() {
129
      boardsStore.detail.issue.removeAllAssignees();
130
    },
131
    saveAssignees() {
132 133
      this.loadingAssignees = true;

134 135
      boardsStore.detail.issue
        .update()
136 137 138 139 140
        .then(() => {
          this.loadingAssignees = false;
        })
        .catch(() => {
          this.loadingAssignees = false;
141
          Flash(__('An error occurred while saving assignees'));
142 143
        });
    },
144 145 146 147 148 149
    showScopedLabels(label) {
      return boardsStore.scopedLabels.enabled && isScopedLabel(label);
    },
    helpLink() {
      return boardsStore.scopedLabels.helpLink;
    },
150
  },
151
});