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

245 lines
7.4 KiB
JavaScript
Raw Normal View History

2017-03-08 07:21:07 -05:00
/* eslint-disable one-var, quote-props, comma-dangle, space-before-function-paren */
import $ from 'jquery';
import _ from 'underscore';
2017-03-17 13:21:25 -04:00
import Vue from 'vue';
import Flash from '~/flash';
import { __ } from '~/locale';
2018-03-05 07:35:58 -05:00
import '~/vue_shared/models/label';
import FilteredSearchBoards from './filtered_search_boards';
import eventHub from './eventhub';
import sidebarEventHub from '~/sidebar/event_hub'; // eslint-disable-line import/first
import './models/issue';
import './models/list';
import './models/milestone';
2018-02-19 14:06:16 -05:00
import './models/project';
import './models/assignee';
import './stores/boards_store';
import ModalStore from './stores/modal_store';
import BoardService from './services/board_service';
import modalMixin from './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'; // eslint-disable-line import/first
2016-07-28 07:33:04 -04:00
2018-02-21 17:28:08 -05:00
export default () => {
2017-01-11 22:49:57 -05:00
const $boardApp = document.getElementById('board-app');
const Store = gl.issueBoards.BoardsStore;
window.gl = window.gl || {};
2016-07-28 07:33:04 -04:00
if (gl.IssueBoardsApp) {
gl.IssueBoardsApp.$destroy(true);
}
Store.create();
// 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);
gl.IssueBoardsApp = new Vue({
el: $boardApp,
components: {
2016-10-04 10:27:02 -04:00
'board': gl.issueBoards.Board,
'board-sidebar': gl.issueBoards.BoardSidebar,
'board-add-issues-modal': gl.issueBoards.IssuesModal,
},
2016-07-28 07:33:04 -04:00
data: {
state: Store.state,
loading: true,
boardsEndpoint: $boardApp.dataset.boardsEndpoint,
listsEndpoint: $boardApp.dataset.listsEndpoint,
boardId: $boardApp.dataset.boardId,
disabled: $boardApp.dataset.disabled === 'true',
issueLinkBase: $boardApp.dataset.issueLinkBase,
rootPath: $boardApp.dataset.rootPath,
bulkUpdatePath: $boardApp.dataset.bulkUpdatePath,
2017-05-04 11:33:25 -04:00
detailIssue: Store.detail,
defaultAvatar: $boardApp.dataset.defaultAvatar,
2016-07-28 07:33:04 -04:00
},
computed: {
detailIssueVisible () {
return Object.keys(this.detailIssue.issue).length;
},
},
created () {
gl.boardService = new BoardService({
boardsEndpoint: this.boardsEndpoint,
listsEndpoint: this.listsEndpoint,
bulkUpdatePath: this.bulkUpdatePath,
boardId: this.boardId,
});
Store.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 11:15:05 -04:00
},
2016-11-02 20:37:33 -04:00
mounted () {
2018-02-19 14:06:16 -05:00
this.filterManager = new FilteredSearchBoards(Store.filter, true, Store.cantEdit);
2017-09-26 17:19:28 -04:00
this.filterManager.setup();
Store.disabled = this.disabled;
gl.boardService.all()
2017-12-15 03:31:14 -05:00
.then(res => res.data)
.then((data) => {
data.forEach((board) => {
2017-05-04 11:33:25 -04:00
const list = Store.addList(board, this.defaultAvatar);
if (list.type === 'closed') {
list.position = Infinity;
} else if (list.type === 'backlog') {
list.position = -1;
}
2016-08-17 08:57:36 -04:00
});
2016-11-09 04:23:48 -05:00
this.state.lists = _.sortBy(this.state.lists, 'position');
Store.addBlankState();
2016-08-08 11:15:05 -04:00
this.loading = false;
2017-07-12 10:47:09 -04:00
})
2017-12-15 03:31:14 -05:00
.catch(() => {
Flash('An error occurred while fetching the board lists. Please try again.');
});
},
methods: {
updateTokens() {
this.filterManager.updateTokens();
},
updateDetailIssue(newIssue) {
const sidebarInfoEndpoint = newIssue.sidebarInfoEndpoint;
if (sidebarInfoEndpoint && newIssue.subscribed === undefined) {
newIssue.setFetchingState('subscriptions', true);
BoardService.getIssueInfo(sidebarInfoEndpoint)
2017-12-15 03:31:14 -05:00
.then(res => res.data)
.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'));
});
}
}
},
2016-07-28 07:33:04 -04:00
});
gl.IssueBoardsSearch = new Vue({
2017-03-08 07:17:01 -05:00
el: document.getElementById('js-add-list'),
data: {
filters: Store.state.filters,
},
mounted () {
gl.issueBoards.newListDropdownInit();
},
});
gl.IssueBoardsModalAddBtn = new Vue({
2017-02-03 15:42:30 -05:00
el: document.getElementById('js-add-issues-btn'),
mixins: [modalMixin],
data() {
return {
modal: ModalStore.store,
store: Store.state,
2018-02-19 14:06:16 -05:00
canAdminList: this.$options.el.hasAttribute('data-can-admin-list'),
};
},
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';
}
return '';
},
},
watch: {
disabled() {
this.updateTooltip();
},
},
mounted() {
this.updateTooltip();
},
methods: {
updateTooltip() {
const $tooltip = $(this.$refs.addIssuesButton);
this.$nextTick(() => {
if (this.disabled) {
$tooltip.tooltip();
} else {
$tooltip.tooltip('destroy');
}
});
},
openModal() {
if (!this.disabled) {
this.toggleModal(true);
}
},
},
template: `
<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"
2018-02-19 14:06:16 -05:00
v-if="canAdminList"
@click="openModal">
Add issues
</button>
</div>
`,
});
2018-02-21 17:28:08 -05:00
};