board_sidebar.js 3.47 KB
Newer Older
1 2 3 4 5
/* eslint-disable comma-dangle, space-before-function-paren, no-new */
/* global IssuableContext */
/* global MilestoneSelect */
/* global LabelsSelect */
/* global Sidebar */
6
/* global Flash */
7

8
import Vue from 'vue';
9 10 11
import eventHub from '../../sidebar/event_hub';
import AssigneeTitle from '../../sidebar/components/assignees/assignee_title';
import Assignees from '../../sidebar/components/assignees/assignees';
12
import './sidebar/remove_issue';
13

14
const Store = gl.issueBoards.BoardsStore;
Phil Hughes's avatar
Phil Hughes committed
15

16 17
window.gl = window.gl || {};
window.gl.issueBoards = window.gl.issueBoards || {};
Phil Hughes's avatar
Phil Hughes committed
18

19 20 21 22 23 24 25 26 27
gl.issueBoards.BoardSidebar = Vue.extend({
  props: {
    currentUser: Object
  },
  data() {
    return {
      detail: Store.detail,
      issue: {},
      list: {},
28
      loadingAssignees: false,
29 30 31 32 33
    };
  },
  computed: {
    showSidebar () {
      return Object.keys(this.issue).length;
34
    },
35 36
    milestoneTitle() {
      return this.issue.milestone ? this.issue.milestone.title : 'No Milestone';
37 38 39 40 41 42
    }
  },
  watch: {
    detail: {
      handler () {
        if (this.issue.id !== this.detail.issue.id) {
Clement Ho's avatar
Clement Ho committed
43 44 45 46 47 48
          $('.block.assignee')
            .find('input:not(.js-vue)[name="issue[assignee_ids][]"]')
            .each((i, el) => {
              $(el).remove();
            });

49 50
          $('.js-issue-board-sidebar', this.$el).each((i, el) => {
            $(el).data('glDropdown').clearMenu();
51 52
          });
        }
53 54 55

        this.issue = this.detail.issue;
        this.list = this.detail.list;
56 57 58 59

        this.$nextTick(() => {
          this.endpoint = this.$refs.assigneeDropdown.dataset.issueUpdate;
        });
60 61
      },
      deep: true
62
    },
63 64 65 66
  },
  methods: {
    closeSidebar () {
      this.detail.issue = {};
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    },
    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;

      gl.issueBoards.BoardsStore.detail.issue.update(this.endpoint)
        .then(() => {
          this.loadingAssignees = false;
        })
        .catch(() => {
          this.loadingAssignees = false;
          return new Flash('An error occurred while saving assignees');
        });
    },
  },
  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);
109 110 111 112 113 114 115 116 117 118 119
  },
  mounted () {
    new IssuableContext(this.currentUser);
    new MilestoneSelect();
    new gl.DueDateSelectors();
    new LabelsSelect();
    new Sidebar();
    gl.Subscription.bindAll('.subscription');
  },
  components: {
    removeBtn: gl.issueBoards.RemoveIssueBtn,
120 121
    'assignee-title': AssigneeTitle,
    assignees: Assignees,
122 123
  },
});