index.js 7.2 KB
Newer Older
P
Phil Hughes 已提交
1
/* eslint-disable one-var, quote-props, comma-dangle, space-before-function-paren */
2

3
import _ from 'underscore';
M
Mike Greiling 已提交
4
import Vue from 'vue';
5 6 7 8

import Flash from '~/flash';
import { __ } from '~/locale';

9
import FilteredSearchBoards from './filtered_search_boards';
10
import eventHub from './eventhub';
11
import sidebarEventHub from '~/sidebar/event_hub'; // eslint-disable-line import/first
12 13 14 15 16 17 18
import './models/issue';
import './models/label';
import './models/list';
import './models/milestone';
import './models/assignee';
import './stores/boards_store';
import './stores/modal_store';
19
import BoardService from './services/board_service';
20 21 22 23 24 25 26
import './mixins/modal_mixins';
import './mixins/sortable_default_options';
import './filters/due_date_filters';
import './components/board';
import './components/board_sidebar';
import './components/new_list_dropdown';
import './components/modal/index';
27
import '~/vue_shared/vue_resource_interceptor'; // eslint-disable-line import/first
P
Phil Hughes 已提交
28

C
Constance Okoghenun 已提交
29
export default () => {
30 31
  const $boardApp = document.getElementById('board-app');
  const Store = gl.issueBoards.BoardsStore;
P
Phil Hughes 已提交
32
  const ModalStore = gl.issueBoards.ModalStore;
P
Phil Hughes 已提交
33

34
  window.gl = window.gl || {};
P
Phil Hughes 已提交
35

36 37 38 39
  if (gl.IssueBoardsApp) {
    gl.IssueBoardsApp.$destroy(true);
  }

40 41
  Store.create();

42 43 44 45
  // hack to allow sidebar scripts like milestone_select manipulate the BoardsStore
  gl.issueBoards.boardStoreIssueSet = (...args) => Vue.set(Store.detail.issue, ...args);
  gl.issueBoards.boardStoreIssueDelete = (...args) => Vue.delete(Store.detail.issue, ...args);

46
  gl.IssueBoardsApp = new Vue({
47
    el: $boardApp,
48
    components: {
P
Phil Hughes 已提交
49
      'board': gl.issueBoards.Board,
P
Phil Hughes 已提交
50 51
      'board-sidebar': gl.issueBoards.BoardSidebar,
      'board-add-issues-modal': gl.issueBoards.IssuesModal,
52
    },
P
Phil Hughes 已提交
53
    data: {
54
      state: Store.state,
P
Phil Hughes 已提交
55
      loading: true,
56 57
      boardsEndpoint: $boardApp.dataset.boardsEndpoint,
      listsEndpoint: $boardApp.dataset.listsEndpoint,
58
      boardId: $boardApp.dataset.boardId,
59
      disabled: $boardApp.dataset.disabled === 'true',
60
      issueLinkBase: $boardApp.dataset.issueLinkBase,
61
      rootPath: $boardApp.dataset.rootPath,
62
      bulkUpdatePath: $boardApp.dataset.bulkUpdatePath,
63 64
      detailIssue: Store.detail,
      defaultAvatar: $boardApp.dataset.defaultAvatar,
P
Phil Hughes 已提交
65
    },
66 67 68
    computed: {
      detailIssueVisible () {
        return Object.keys(this.detailIssue.issue).length;
69
      },
70
    },
P
Phil Hughes 已提交
71
    created () {
72 73 74 75 76 77 78
      gl.boardService = new BoardService({
        boardsEndpoint: this.boardsEndpoint,
        listsEndpoint: this.listsEndpoint,
        bulkUpdatePath: this.bulkUpdatePath,
        boardId: this.boardId,
      });
      Store.rootPath = this.boardsEndpoint;
P
Phil Hughes 已提交
79

80
      eventHub.$on('updateTokens', this.updateTokens);
81 82 83
      eventHub.$on('newDetailIssue', this.updateDetailIssue);
      eventHub.$on('clearDetailIssue', this.clearDetailIssue);
      sidebarEventHub.$on('toggleSubscription', this.toggleSubscription);
P
Phil Hughes 已提交
84 85
    },
    beforeDestroy() {
86
      eventHub.$off('updateTokens', this.updateTokens);
87 88 89
      eventHub.$off('newDetailIssue', this.updateDetailIssue);
      eventHub.$off('clearDetailIssue', this.clearDetailIssue);
      sidebarEventHub.$off('toggleSubscription', this.toggleSubscription);
90
    },
F
Fatih Acet 已提交
91
    mounted () {
92 93 94
      this.filterManager = new FilteredSearchBoards(Store.filter, true);
      this.filterManager.setup();

95
      Store.disabled = this.disabled;
96
      gl.boardService.all()
E
Eric Eastwood 已提交
97 98 99
        .then(res => res.data)
        .then((data) => {
          data.forEach((board) => {
100
            const list = Store.addList(board, this.defaultAvatar);
101

102
            if (list.type === 'closed') {
P
Phil Hughes 已提交
103
              list.position = Infinity;
104 105
            } else if (list.type === 'backlog') {
              list.position = -1;
106
            }
P
Phil Hughes 已提交
107
          });
108

P
Phil Hughes 已提交
109 110
          this.state.lists = _.sortBy(this.state.lists, 'position');

111
          Store.addBlankState();
112
          this.loading = false;
F
Filipa Lacerda 已提交
113
        })
E
Eric Eastwood 已提交
114 115 116
        .catch(() => {
          Flash('An error occurred while fetching the board lists. Please try again.');
        });
P
Phil Hughes 已提交
117 118 119 120
    },
    methods: {
      updateTokens() {
        this.filterManager.updateTokens();
121 122 123 124 125 126
      },
      updateDetailIssue(newIssue) {
        const sidebarInfoEndpoint = newIssue.sidebarInfoEndpoint;
        if (sidebarInfoEndpoint && newIssue.subscribed === undefined) {
          newIssue.setFetchingState('subscriptions', true);
          BoardService.getIssueInfo(sidebarInfoEndpoint)
E
Eric Eastwood 已提交
127
            .then(res => res.data)
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
            .then((data) => {
              newIssue.setFetchingState('subscriptions', false);
              newIssue.updateData({
                subscribed: data.subscribed,
              });
            })
            .catch(() => {
              newIssue.setFetchingState('subscriptions', false);
              Flash(__('An error occurred while fetching sidebar data'));
            });
        }

        Store.detail.issue = newIssue;
      },
      clearDetailIssue() {
        Store.detail.issue = {};
      },
      toggleSubscription(id) {
        const issue = Store.detail.issue;
        if (issue.id === id && issue.toggleSubscriptionEndpoint) {
          issue.setFetchingState('subscriptions', true);
          BoardService.toggleIssueSubscription(issue.toggleSubscriptionEndpoint)
            .then(() => {
              issue.setFetchingState('subscriptions', false);
              issue.updateData({
                subscribed: !issue.subscribed,
              });
            })
            .catch(() => {
              issue.setFetchingState('subscriptions', false);
              Flash(__('An error occurred when toggling the notification subscription'));
            });
        }
P
Phil Hughes 已提交
161 162
      }
    },
P
Phil Hughes 已提交
163
  });
164 165

  gl.IssueBoardsSearch = new Vue({
P
Phil Hughes 已提交
166
    el: document.getElementById('js-add-list'),
167
    data: {
168
      filters: Store.state.filters,
169 170 171
    },
    mounted () {
      gl.issueBoards.newListDropdownInit();
172
    },
173
  });
P
Phil Hughes 已提交
174

175
  gl.IssueBoardsModalAddBtn = new Vue({
176
    el: document.getElementById('js-add-issues-btn'),
177
    mixins: [gl.issueBoards.ModalMixins],
178 179 180 181 182
    data() {
      return {
        modal: ModalStore.store,
        store: Store.state,
      };
183 184 185
    },
    computed: {
      disabled() {
186 187 188
        if (!this.store) {
          return true;
        }
P
Phil Hughes 已提交
189
        return !this.store.lists.filter(list => !list.preset).length;
190
      },
191 192 193 194 195 196 197 198
      tooltipTitle() {
        if (this.disabled) {
          return 'Please add a list to your board first';
        }

        return '';
      },
    },
199 200 201 202 203 204 205 206
    watch: {
      disabled() {
        this.updateTooltip();
      },
    },
    mounted() {
      this.updateTooltip();
    },
207 208
    methods: {
      updateTooltip() {
209
        const $tooltip = $(this.$refs.addIssuesButton);
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

        this.$nextTick(() => {
          if (this.disabled) {
            $tooltip.tooltip();
          } else {
            $tooltip.tooltip('destroy');
          }
        });
      },
      openModal() {
        if (!this.disabled) {
          this.toggleModal(true);
        }
      },
    },
225
    template: `
226 227 228 229 230 231 232 233 234 235 236 237 238
      <div class="board-extra-actions">
        <button
          class="btn btn-create prepend-left-10"
          type="button"
          data-placement="bottom"
          ref="addIssuesButton"
          :class="{ 'disabled': disabled }"
          :title="tooltipTitle"
          :aria-disabled="disabled"
          @click="openModal">
          Add issues
        </button>
      </div>
239 240
    `,
  });
C
Constance Okoghenun 已提交
241
};