index.js 7.6 KB
Newer Older
1
import $ from 'jquery';
2
import _ from 'underscore';
M
Mike Greiling 已提交
3
import Vue from 'vue';
4 5 6

import Flash from '~/flash';
import { __ } from '~/locale';
K
Kushal Pandya 已提交
7
import '~/vue_shared/models/label';
8
import '~/vue_shared/models/assignee';
9

10
import FilteredSearchBoards from './filtered_search_boards';
11
import eventHub from './eventhub';
M
Mike Greiling 已提交
12
import sidebarEventHub from '~/sidebar/event_hub';
13 14 15
import './models/issue';
import './models/list';
import './models/milestone';
F
Felipe Artur 已提交
16
import './models/project';
17
import boardsStore from './stores/boards_store';
18
import ModalStore from './stores/modal_store';
19
import BoardService from './services/board_service';
20
import modalMixin from './mixins/modal_mixins';
21
import './filters/due_date_filters';
22 23 24
import Board from './components/board';
import BoardSidebar from './components/board_sidebar';
import initNewListDropdown from './components/new_list_dropdown';
25
import BoardAddIssuesModal from './components/modal/index.vue';
M
Mike Greiling 已提交
26
import '~/vue_shared/vue_resource_interceptor';
27
import { NavigationType, parseBoolean } from '~/lib/utils/common_utils';
P
Phil Hughes 已提交
28

29 30
let issueBoardsApp;

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

34
  // check for browser back and trigger a hard reload to circumvent browser caching.
35 36 37
  window.addEventListener('pageshow', event => {
    const isNavTypeBackForward =
      window.performance && window.performance.navigation.type === NavigationType.TYPE_BACK_FORWARD;
38 39 40 41 42 43

    if (event.persisted || isNavTypeBackForward) {
      window.location.reload();
    }
  });

44 45
  if (issueBoardsApp) {
    issueBoardsApp.$destroy(true);
46 47
  }

48
  boardsStore.create();
49

50
  issueBoardsApp = new Vue({
51
    el: $boardApp,
52
    components: {
53 54
      Board,
      BoardSidebar,
55
      BoardAddIssuesModal,
56
    },
P
Phil Hughes 已提交
57
    data: {
58
      state: boardsStore.state,
P
Phil Hughes 已提交
59
      loading: true,
60 61
      boardsEndpoint: $boardApp.dataset.boardsEndpoint,
      listsEndpoint: $boardApp.dataset.listsEndpoint,
62
      boardId: $boardApp.dataset.boardId,
63
      disabled: parseBoolean($boardApp.dataset.disabled),
64
      issueLinkBase: $boardApp.dataset.issueLinkBase,
65
      rootPath: $boardApp.dataset.rootPath,
66
      bulkUpdatePath: $boardApp.dataset.bulkUpdatePath,
67
      detailIssue: boardsStore.detail,
68
      defaultAvatar: $boardApp.dataset.defaultAvatar,
P
Phil Hughes 已提交
69
    },
70
    computed: {
P
Phil Hughes 已提交
71
      detailIssueVisible() {
72
        return Object.keys(this.detailIssue.issue).length;
73
      },
74
    },
P
Phil Hughes 已提交
75
    created() {
76 77 78 79 80 81
      gl.boardService = new BoardService({
        boardsEndpoint: this.boardsEndpoint,
        listsEndpoint: this.listsEndpoint,
        bulkUpdatePath: this.bulkUpdatePath,
        boardId: this.boardId,
      });
82
      boardsStore.rootPath = this.boardsEndpoint;
P
Phil Hughes 已提交
83

84
      eventHub.$on('updateTokens', this.updateTokens);
85 86 87
      eventHub.$on('newDetailIssue', this.updateDetailIssue);
      eventHub.$on('clearDetailIssue', this.clearDetailIssue);
      sidebarEventHub.$on('toggleSubscription', this.toggleSubscription);
P
Phil Hughes 已提交
88 89
    },
    beforeDestroy() {
90
      eventHub.$off('updateTokens', this.updateTokens);
91 92 93
      eventHub.$off('newDetailIssue', this.updateDetailIssue);
      eventHub.$off('clearDetailIssue', this.clearDetailIssue);
      sidebarEventHub.$off('toggleSubscription', this.toggleSubscription);
94
    },
P
Phil Hughes 已提交
95
    mounted() {
96
      this.filterManager = new FilteredSearchBoards(boardsStore.filter, true, boardsStore.cantEdit);
97 98
      this.filterManager.setup();

99
      boardsStore.disabled = this.disabled;
P
Phil Hughes 已提交
100 101
      gl.boardService
        .all()
E
Eric Eastwood 已提交
102
        .then(res => res.data)
P
Phil Hughes 已提交
103 104
        .then(data => {
          data.forEach(board => {
105
            const list = boardsStore.addList(board, this.defaultAvatar);
106

107
            if (list.type === 'closed') {
P
Phil Hughes 已提交
108
              list.position = Infinity;
109 110
            } else if (list.type === 'backlog') {
              list.position = -1;
111
            }
P
Phil Hughes 已提交
112
          });
113

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

116
          boardsStore.addBlankState();
117
          this.loading = false;
F
Filipa Lacerda 已提交
118
        })
E
Eric Eastwood 已提交
119 120 121
        .catch(() => {
          Flash('An error occurred while fetching the board lists. Please try again.');
        });
P
Phil Hughes 已提交
122 123 124 125
    },
    methods: {
      updateTokens() {
        this.filterManager.updateTokens();
126 127
      },
      updateDetailIssue(newIssue) {
128
        const { sidebarInfoEndpoint } = newIssue;
129 130 131
        if (sidebarInfoEndpoint && newIssue.subscribed === undefined) {
          newIssue.setFetchingState('subscriptions', true);
          BoardService.getIssueInfo(sidebarInfoEndpoint)
E
Eric Eastwood 已提交
132
            .then(res => res.data)
P
Phil Hughes 已提交
133
            .then(data => {
134 135 136 137 138 139 140 141 142 143 144
              newIssue.setFetchingState('subscriptions', false);
              newIssue.updateData({
                subscribed: data.subscribed,
              });
            })
            .catch(() => {
              newIssue.setFetchingState('subscriptions', false);
              Flash(__('An error occurred while fetching sidebar data'));
            });
        }

145
        boardsStore.detail.issue = newIssue;
146 147
      },
      clearDetailIssue() {
148
        boardsStore.detail.issue = {};
149 150
      },
      toggleSubscription(id) {
151
        const { issue } = boardsStore.detail;
152 153 154 155 156 157 158 159 160 161 162 163 164 165
        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 已提交
166
      },
P
Phil Hughes 已提交
167
    },
P
Phil Hughes 已提交
168
  });
169

170 171
  // eslint-disable-next-line no-new
  new Vue({
P
Phil Hughes 已提交
172
    el: document.getElementById('js-add-list'),
173
    data: {
174
      filters: boardsStore.state.filters,
175
    },
P
Phil Hughes 已提交
176
    mounted() {
177
      initNewListDropdown();
178
    },
179
  });
P
Phil Hughes 已提交
180

P
Phil Hughes 已提交
181 182 183
  const issueBoardsModal = document.getElementById('js-add-issues-btn');

  if (issueBoardsModal) {
184 185
    // eslint-disable-next-line no-new
    new Vue({
P
Phil Hughes 已提交
186 187 188 189 190
      el: issueBoardsModal,
      mixins: [modalMixin],
      data() {
        return {
          modal: ModalStore.store,
191
          store: boardsStore.state,
P
Phil Hughes 已提交
192 193
          canAdminList: this.$options.el.hasAttribute('data-can-admin-list'),
        };
194
      },
P
Phil Hughes 已提交
195 196 197 198 199 200 201 202 203 204 205
      computed: {
        disabled() {
          if (!this.store) {
            return true;
          }
          return !this.store.lists.filter(list => !list.preset).length;
        },
        tooltipTitle() {
          if (this.disabled) {
            return 'Please add a list to your board first';
          }
206

P
Phil Hughes 已提交
207 208
          return '';
        },
209
      },
P
Phil Hughes 已提交
210 211 212 213 214 215
      watch: {
        disabled() {
          this.updateTooltip();
        },
      },
      mounted() {
216 217
        this.updateTooltip();
      },
P
Phil Hughes 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
      methods: {
        updateTooltip() {
          const $tooltip = $(this.$refs.addIssuesButton);

          this.$nextTick(() => {
            if (this.disabled) {
              $tooltip.tooltip();
            } else {
              $tooltip.tooltip('dispose');
            }
          });
        },
        openModal() {
          if (!this.disabled) {
            this.toggleModal(true);
233
          }
P
Phil Hughes 已提交
234
        },
235
      },
P
Phil Hughes 已提交
236 237 238
      template: `
        <div class="board-extra-actions">
          <button
239
            class="btn btn-success prepend-left-10"
P
Phil Hughes 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253
            type="button"
            data-placement="bottom"
            ref="addIssuesButton"
            :class="{ 'disabled': disabled }"
            :title="tooltipTitle"
            :aria-disabled="disabled"
            v-if="canAdminList"
            @click="openModal">
            Add issues
          </button>
        </div>
      `,
    });
  }
C
Constance Okoghenun 已提交
254
};