gitlab-org--gitlab-foss/app/assets/javascripts/boards/index.js

255 lines
7.6 KiB
JavaScript
Raw Normal View History

import $ from 'jquery';
import _ from 'underscore';
2017-03-17 17:21:25 +00:00
import Vue from 'vue';
import Flash from '~/flash';
import { __ } from '~/locale';
2018-03-05 12:35:58 +00:00
import '~/vue_shared/models/label';
import '~/vue_shared/models/assignee';
import FilteredSearchBoards from './filtered_search_boards';
import eventHub from './eventhub';
import sidebarEventHub from '~/sidebar/event_hub';
import './models/issue';
import './models/list';
import './models/milestone';
2018-02-19 19:06:16 +00:00
import './models/project';
import boardsStore from './stores/boards_store';
import ModalStore from './stores/modal_store';
import BoardService from './services/board_service';
import modalMixin from './mixins/modal_mixins';
import './filters/due_date_filters';
import Board from './components/board';
import BoardSidebar from './components/board_sidebar';
import initNewListDropdown from './components/new_list_dropdown';
import BoardAddIssuesModal from './components/modal/index.vue';
import '~/vue_shared/vue_resource_interceptor';
import { NavigationType, parseBoolean } from '~/lib/utils/common_utils';
2016-07-28 11:33:04 +00:00
let issueBoardsApp;
2018-02-21 22:28:08 +00:00
export default () => {
2017-01-12 03:49:57 +00:00
const $boardApp = document.getElementById('board-app');
2016-07-28 11:33:04 +00:00
// check for browser back and trigger a hard reload to circumvent browser caching.
window.addEventListener('pageshow', event => {
const isNavTypeBackForward =
window.performance && window.performance.navigation.type === NavigationType.TYPE_BACK_FORWARD;
if (event.persisted || isNavTypeBackForward) {
window.location.reload();
}
});
if (issueBoardsApp) {
issueBoardsApp.$destroy(true);
}
boardsStore.create();
issueBoardsApp = new Vue({
el: $boardApp,
components: {
Board,
BoardSidebar,
BoardAddIssuesModal,
},
2016-07-28 11:33:04 +00:00
data: {
state: boardsStore.state,
loading: true,
boardsEndpoint: $boardApp.dataset.boardsEndpoint,
listsEndpoint: $boardApp.dataset.listsEndpoint,
boardId: $boardApp.dataset.boardId,
disabled: parseBoolean($boardApp.dataset.disabled),
issueLinkBase: $boardApp.dataset.issueLinkBase,
rootPath: $boardApp.dataset.rootPath,
bulkUpdatePath: $boardApp.dataset.bulkUpdatePath,
detailIssue: boardsStore.detail,
2017-05-04 15:33:25 +00:00
defaultAvatar: $boardApp.dataset.defaultAvatar,
2016-07-28 11:33:04 +00:00
},
computed: {
2018-07-25 15:38:43 +00:00
detailIssueVisible() {
return Object.keys(this.detailIssue.issue).length;
},
},
2018-07-25 15:38:43 +00:00
created() {
gl.boardService = new BoardService({
boardsEndpoint: this.boardsEndpoint,
listsEndpoint: this.listsEndpoint,
bulkUpdatePath: this.bulkUpdatePath,
boardId: this.boardId,
});
boardsStore.rootPath = this.boardsEndpoint;
eventHub.$on('updateTokens', this.updateTokens);
eventHub.$on('newDetailIssue', this.updateDetailIssue);
eventHub.$on('clearDetailIssue', this.clearDetailIssue);
sidebarEventHub.$on('toggleSubscription', this.toggleSubscription);
},
beforeDestroy() {
eventHub.$off('updateTokens', this.updateTokens);
eventHub.$off('newDetailIssue', this.updateDetailIssue);
eventHub.$off('clearDetailIssue', this.clearDetailIssue);
sidebarEventHub.$off('toggleSubscription', this.toggleSubscription);
2016-08-08 15:15:05 +00:00
},
2018-07-25 15:38:43 +00:00
mounted() {
this.filterManager = new FilteredSearchBoards(boardsStore.filter, true, boardsStore.cantEdit);
2017-09-26 21:19:28 +00:00
this.filterManager.setup();
boardsStore.disabled = this.disabled;
2018-07-25 15:38:43 +00:00
gl.boardService
.all()
2017-12-15 08:31:14 +00:00
.then(res => res.data)
2018-07-25 15:38:43 +00:00
.then(data => {
data.forEach(board => {
const list = boardsStore.addList(board, this.defaultAvatar);
if (list.type === 'closed') {
list.position = Infinity;
} else if (list.type === 'backlog') {
list.position = -1;
}
2016-08-17 12:57:36 +00:00
});
2016-11-09 09:23:48 +00:00
this.state.lists = _.sortBy(this.state.lists, 'position');
boardsStore.addBlankState();
2016-08-08 15:15:05 +00:00
this.loading = false;
2017-07-12 14:47:09 +00:00
})
2017-12-15 08:31:14 +00:00
.catch(() => {
Flash('An error occurred while fetching the board lists. Please try again.');
});
},
methods: {
updateTokens() {
this.filterManager.updateTokens();
},
updateDetailIssue(newIssue) {
const { sidebarInfoEndpoint } = newIssue;
if (sidebarInfoEndpoint && newIssue.subscribed === undefined) {
newIssue.setFetchingState('subscriptions', true);
BoardService.getIssueInfo(sidebarInfoEndpoint)
2017-12-15 08:31:14 +00:00
.then(res => res.data)
2018-07-25 15:38:43 +00:00
.then(data => {
newIssue.setFetchingState('subscriptions', false);
newIssue.updateData({
subscribed: data.subscribed,
});
})
.catch(() => {
newIssue.setFetchingState('subscriptions', false);
Flash(__('An error occurred while fetching sidebar data'));
});
}
boardsStore.detail.issue = newIssue;
},
clearDetailIssue() {
boardsStore.detail.issue = {};
},
toggleSubscription(id) {
const { issue } = boardsStore.detail;
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'));
});
}
2018-07-25 15:38:43 +00:00
},
},
2016-07-28 11:33:04 +00:00
});
// eslint-disable-next-line no-new
new Vue({
2017-03-08 12:17:01 +00:00
el: document.getElementById('js-add-list'),
data: {
filters: boardsStore.state.filters,
},
2018-07-25 15:38:43 +00:00
mounted() {
initNewListDropdown();
},
});
2018-07-25 15:38:43 +00:00
const issueBoardsModal = document.getElementById('js-add-issues-btn');
if (issueBoardsModal) {
// eslint-disable-next-line no-new
new Vue({
2018-07-25 15:38:43 +00:00
el: issueBoardsModal,
mixins: [modalMixin],
data() {
return {
modal: ModalStore.store,
store: boardsStore.state,
2018-07-25 15:38:43 +00:00
canAdminList: this.$options.el.hasAttribute('data-can-admin-list'),
};
},
2018-07-25 15:38:43 +00:00
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';
}
2018-07-25 15:38:43 +00:00
return '';
},
},
2018-07-25 15:38:43 +00:00
watch: {
disabled() {
this.updateTooltip();
},
},
mounted() {
this.updateTooltip();
},
2018-07-25 15:38:43 +00:00
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);
}
2018-07-25 15:38:43 +00:00
},
},
2018-07-25 15:38:43 +00:00
template: `
<div class="board-extra-actions">
<button
class="btn btn-success prepend-left-10"
2018-07-25 15:38:43 +00:00
type="button"
data-placement="bottom"
ref="addIssuesButton"
:class="{ 'disabled': disabled }"
:title="tooltipTitle"
:aria-disabled="disabled"
v-if="canAdminList"
@click="openModal">
Add issues
</button>
</div>
`,
});
}
2018-02-21 22:28:08 +00:00
};