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';
P
Phil Hughes 已提交
5
import Flash from '../flash';
6
import { __ } from '../locale';
7
import FilteredSearchBoards from './filtered_search_boards';
8
import eventHub from './eventhub';
9
import sidebarEventHub from '../sidebar/event_hub';
10 11 12 13 14 15 16
import './models/issue';
import './models/label';
import './models/list';
import './models/milestone';
import './models/assignee';
import './stores/boards_store';
import './stores/modal_store';
17
import BoardService from './services/board_service';
18 19 20 21 22 23 24 25
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';
import '../vue_shared/vue_resource_interceptor';
P
Phil Hughes 已提交
26

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

32
  window.gl = window.gl || {};
P
Phil Hughes 已提交
33

34 35 36 37
  if (gl.IssueBoardsApp) {
    gl.IssueBoardsApp.$destroy(true);
  }

38 39
  Store.create();

40 41 42 43
  // 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);

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

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

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

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

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

109
          Store.addBlankState();
110
          this.loading = false;
F
Filipa Lacerda 已提交
111
        })
E
Eric Eastwood 已提交
112 113 114
        .catch(() => {
          Flash('An error occurred while fetching the board lists. Please try again.');
        });
P
Phil Hughes 已提交
115 116 117 118
    },
    methods: {
      updateTokens() {
        this.filterManager.updateTokens();
119 120 121 122 123 124
      },
      updateDetailIssue(newIssue) {
        const sidebarInfoEndpoint = newIssue.sidebarInfoEndpoint;
        if (sidebarInfoEndpoint && newIssue.subscribed === undefined) {
          newIssue.setFetchingState('subscriptions', true);
          BoardService.getIssueInfo(sidebarInfoEndpoint)
E
Eric Eastwood 已提交
125
            .then(res => res.data)
126 127 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
            .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 已提交
159 160
      }
    },
P
Phil Hughes 已提交
161
  });
162 163

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

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

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

        this.$nextTick(() => {
          if (this.disabled) {
            $tooltip.tooltip();
          } else {
            $tooltip.tooltip('destroy');
          }
        });
      },
      openModal() {
        if (!this.disabled) {
          this.toggleModal(true);
        }
      },
    },
223
    template: `
224 225 226 227 228 229 230 231 232 233 234 235 236
      <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>
237 238
    `,
  });
C
Constance Okoghenun 已提交
239
};