index.js 7.7 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
      boardsEndpoint: $boardApp.dataset.boardsEndpoint,
61
      recentBoardsEndpoint: $boardApp.dataset.recentBoardsEndpoint,
62
      listsEndpoint: $boardApp.dataset.listsEndpoint,
63
      boardId: $boardApp.dataset.boardId,
64
      disabled: parseBoolean($boardApp.dataset.disabled),
65
      issueLinkBase: $boardApp.dataset.issueLinkBase,
66
      rootPath: $boardApp.dataset.rootPath,
67
      bulkUpdatePath: $boardApp.dataset.bulkUpdatePath,
68
      detailIssue: boardsStore.detail,
69
      defaultAvatar: $boardApp.dataset.defaultAvatar,
P
Phil Hughes 已提交
70
    },
71
    computed: {
P
Phil Hughes 已提交
72
      detailIssueVisible() {
73
        return Object.keys(this.detailIssue.issue).length;
74
      },
75
    },
P
Phil Hughes 已提交
76
    created() {
77 78
      gl.boardService = new BoardService({
        boardsEndpoint: this.boardsEndpoint,
79
        recentBoardsEndpoint: this.recentBoardsEndpoint,
80 81 82 83
        listsEndpoint: this.listsEndpoint,
        bulkUpdatePath: this.bulkUpdatePath,
        boardId: this.boardId,
      });
84
      boardsStore.rootPath = this.boardsEndpoint;
P
Phil Hughes 已提交
85

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

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

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

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

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

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

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

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

  if (issueBoardsModal) {
186 187
    // eslint-disable-next-line no-new
    new Vue({
P
Phil Hughes 已提交
188 189 190 191 192
      el: issueBoardsModal,
      mixins: [modalMixin],
      data() {
        return {
          modal: ModalStore.store,
193
          store: boardsStore.state,
P
Phil Hughes 已提交
194 195
          canAdminList: this.$options.el.hasAttribute('data-can-admin-list'),
        };
196
      },
P
Phil Hughes 已提交
197 198 199 200 201 202 203 204 205 206 207
      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';
          }
208

P
Phil Hughes 已提交
209 210
          return '';
        },
211
      },
P
Phil Hughes 已提交
212 213 214 215 216 217
      watch: {
        disabled() {
          this.updateTooltip();
        },
      },
      mounted() {
218 219
        this.updateTooltip();
      },
P
Phil Hughes 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
      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);
235
          }
P
Phil Hughes 已提交
236
        },
237
      },
P
Phil Hughes 已提交
238 239 240
      template: `
        <div class="board-extra-actions">
          <button
241
            class="btn btn-success prepend-left-10"
P
Phil Hughes 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255
            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 已提交
256
};