board_sidebar.js 3.69 KB
Newer Older
1 2
/* eslint-disable comma-dangle, space-before-function-paren, no-new */

3
import $ from 'jquery';
4
import Vue from 'vue';
Phil Hughes's avatar
Phil Hughes committed
5
import Flash from '../../flash';
6
import { __ } from '../../locale';
7
import Sidebar from '../../right_sidebar';
8
import eventHub from '../../sidebar/event_hub';
9
import assigneeTitle from '../../sidebar/components/assignees/assignee_title.vue';
10
import assignees from '../../sidebar/components/assignees/assignees.vue';
Filipa Lacerda's avatar
Filipa Lacerda committed
11
import DueDateSelectors from '../../due_date_select';
12
import './sidebar/remove_issue';
13
import IssuableContext from '../../issuable_context';
14
import LabelsSelect from '../../labels_select';
15
import subscriptions from '../../sidebar/components/subscriptions/subscriptions.vue';
16
import MilestoneSelect from '../../milestone_select';
17

18
const Store = gl.issueBoards.BoardsStore;
Phil Hughes's avatar
Phil Hughes committed
19

20 21
window.gl = window.gl || {};
window.gl.issueBoards = window.gl.issueBoards || {};
Phil Hughes's avatar
Phil Hughes committed
22

23
gl.issueBoards.BoardSidebar = Vue.extend({
24 25 26 27 28 29
  components: {
    assigneeTitle,
    assignees,
    removeBtn: gl.issueBoards.RemoveIssueBtn,
    subscriptions,
  },
30
  props: {
31 32 33 34
    currentUser: {
      type: Object,
      default: () => ({}),
    },
35 36 37 38 39 40
  },
  data() {
    return {
      detail: Store.detail,
      issue: {},
      list: {},
41
      loadingAssignees: false,
42 43 44 45 46
    };
  },
  computed: {
    showSidebar () {
      return Object.keys(this.issue).length;
47
    },
48 49
    milestoneTitle() {
      return this.issue.milestone ? this.issue.milestone.title : 'No Milestone';
50 51 52 53
    },
    canRemove() {
      return !this.list.preset;
    },
54 55 56 57 58
  },
  watch: {
    detail: {
      handler () {
        if (this.issue.id !== this.detail.issue.id) {
Clement Ho's avatar
Clement Ho committed
59 60 61 62 63 64
          $('.block.assignee')
            .find('input:not(.js-vue)[name="issue[assignee_ids][]"]')
            .each((i, el) => {
              $(el).remove();
            });

65 66
          $('.js-issue-board-sidebar', this.$el).each((i, el) => {
            $(el).data('glDropdown').clearMenu();
67 68
          });
        }
69 70 71 72 73

        this.issue = this.detail.issue;
        this.list = this.detail.list;
      },
      deep: true
74
    },
75
  },
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  created () {
    // 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);
  },
  mounted () {
    new IssuableContext(this.currentUser);
    new MilestoneSelect();
    new DueDateSelectors();
    new LabelsSelect();
    new Sidebar();
  },
96 97 98
  methods: {
    closeSidebar () {
      this.detail.issue = {};
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    },
    assignSelf () {
      // Notify gl dropdown that we are now assigning to current user
      this.$refs.assigneeBlock.dispatchEvent(new Event('assignYourself'));

      this.addAssignee(this.currentUser);
      this.saveAssignees();
    },
    removeAssignee (a) {
      gl.issueBoards.BoardsStore.detail.issue.removeAssignee(a);
    },
    addAssignee (a) {
      gl.issueBoards.BoardsStore.detail.issue.addAssignee(a);
    },
    removeAllAssignees () {
      gl.issueBoards.BoardsStore.detail.issue.removeAllAssignees();
    },
    saveAssignees () {
      this.loadingAssignees = true;

119
      gl.issueBoards.BoardsStore.detail.issue.update()
120 121 122 123 124
        .then(() => {
          this.loadingAssignees = false;
        })
        .catch(() => {
          this.loadingAssignees = false;
125
          Flash(__('An error occurred while saving assignees'));
126 127 128
        });
    },
  },
129
});