debian-mirror-gitlab/app/assets/javascripts/pages/dashboard/todos/index/todos.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

234 lines
7.2 KiB
JavaScript
Raw Normal View History

2021-12-11 22:18:48 +05:30
/* eslint-disable class-methods-use-this */
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2021-04-29 21:17:54 +05:30
import { getGroups } from '~/api/groups_api';
import { getProjects } from '~/api/projects_api';
2020-11-24 15:15:51 +05:30
import initDeprecatedJQueryDropdown from '~/deprecated_jquery_dropdown';
2022-11-25 23:54:43 +05:30
import { createAlert } from '~/flash';
2021-03-11 19:13:27 +05:30
import axios from '~/lib/utils/axios_utils';
2018-03-17 18:26:18 +05:30
import { isMetaClick } from '~/lib/utils/common_utils';
2019-12-21 20:55:43 +05:30
import { addDelimiter } from '~/lib/utils/text_utility';
2021-03-11 19:13:27 +05:30
import { visitUrl } from '~/lib/utils/url_utility';
2018-03-27 19:54:05 +05:30
import { __ } from '~/locale';
2021-03-11 19:13:27 +05:30
import UsersSelect from '~/users_select';
2017-09-10 17:25:29 +05:30
export default class Todos {
2017-08-17 22:00:37 +05:30
constructor() {
this.initFilters();
this.bindEvents();
this.todo_ids = [];
this.cleanupWrapper = this.cleanup.bind(this);
document.addEventListener('beforeunload', this.cleanupWrapper);
}
cleanup() {
this.unbindEvents();
document.removeEventListener('beforeunload', this.cleanupWrapper);
}
unbindEvents() {
2022-08-13 15:12:31 +05:30
document.querySelectorAll('.js-done-todo, .js-undo-todo, .js-add-todo').forEach((el) => {
el.removeEventListener('click', this.updateRowStateClickedWrapper);
});
document.querySelectorAll('.js-todos-mark-all, .js-todos-undo-all').forEach((el) => {
el.removeEventListener('click', this.updateallStateClickedWrapper);
});
document.querySelectorAll('.todo').forEach((el) => {
el.removeEventListener('click', this.goToTodoUrl);
el.removeEventListener('auxclick', this.goToTodoUrl);
});
2017-08-17 22:00:37 +05:30
}
bindEvents() {
this.updateRowStateClickedWrapper = this.updateRowStateClicked.bind(this);
this.updateAllStateClickedWrapper = this.updateAllStateClicked.bind(this);
2022-08-13 15:12:31 +05:30
document.querySelectorAll('.js-done-todo, .js-undo-todo, .js-add-todo').forEach((el) => {
el.addEventListener('click', this.updateRowStateClickedWrapper);
});
document.querySelectorAll('.js-todos-mark-all, .js-todos-undo-all').forEach((el) => {
el.addEventListener('click', this.updateAllStateClickedWrapper);
});
document.querySelectorAll('.todo').forEach((el) => {
el.addEventListener('click', this.goToTodoUrl);
el.addEventListener('auxclick', this.goToTodoUrl);
});
2017-08-17 22:00:37 +05:30
}
initFilters() {
2021-04-29 21:17:54 +05:30
this.initAjaxFilterDropdown(getGroups, $('.js-group-search'), 'group_id');
this.initAjaxFilterDropdown(getProjects, $('.js-project-search'), 'project_id');
2017-08-17 22:00:37 +05:30
this.initFilterDropdown($('.js-type-search'), 'type');
this.initFilterDropdown($('.js-action-search'), 'action_id');
return new UsersSelect();
}
2021-04-29 21:17:54 +05:30
initAjaxFilterDropdown(apiMethod, $dropdown, fieldName) {
initDeprecatedJQueryDropdown($dropdown, {
fieldName,
selectable: true,
filterable: true,
filterRemote: true,
data(search, callback) {
return apiMethod(search, {}, (data) => {
callback(
data.map((d) => ({
id: d.id,
text: d.full_name || d.name_with_namespace,
})),
);
});
},
clicked: () => {
const $formEl = $dropdown.closest('form.filter-form');
$formEl.submit();
},
});
}
2017-08-17 22:00:37 +05:30
initFilterDropdown($dropdown, fieldName, searchFields) {
2020-11-24 15:15:51 +05:30
initDeprecatedJQueryDropdown($dropdown, {
2017-08-17 22:00:37 +05:30
fieldName,
selectable: true,
2021-12-11 22:18:48 +05:30
filterable: Boolean(searchFields),
2017-08-17 22:00:37 +05:30
search: { fields: searchFields },
data: $dropdown.data('data'),
2018-11-18 11:00:15 +05:30
clicked: () => {
const $formEl = $dropdown.closest('form.filter-form');
$formEl.submit();
},
2017-08-17 22:00:37 +05:30
});
}
updateRowStateClicked(e) {
2017-09-10 17:25:29 +05:30
e.stopPropagation();
2017-08-17 22:00:37 +05:30
e.preventDefault();
2018-11-08 19:23:39 +05:30
const { target } = e;
2017-08-17 22:00:37 +05:30
target.setAttribute('disabled', true);
target.classList.add('disabled');
2018-03-17 18:26:18 +05:30
2022-05-07 20:08:51 +05:30
target.querySelector('.gl-spinner-container').classList.add('gl-mr-2');
2018-03-17 18:26:18 +05:30
axios[target.dataset.method](target.dataset.href)
.then(({ data }) => {
2017-08-17 22:00:37 +05:30
this.updateRowState(target);
2018-03-17 18:26:18 +05:30
this.updateBadges(data);
2018-12-13 13:39:08 +05:30
})
.catch(() => {
2018-12-05 23:21:45 +05:30
this.updateRowState(target, true);
2022-11-25 23:54:43 +05:30
return createAlert({
2021-09-30 23:02:18 +05:30
message: __('Error updating status of to-do item.'),
});
2018-12-05 23:21:45 +05:30
});
2017-08-17 22:00:37 +05:30
}
2018-12-05 23:21:45 +05:30
updateRowState(target, isInactive = false) {
2017-08-17 22:00:37 +05:30
const row = target.closest('li');
const restoreBtn = row.querySelector('.js-undo-todo');
const doneBtn = row.querySelector('.js-done-todo');
target.classList.add('hidden');
target.removeAttribute('disabled');
target.classList.remove('disabled');
2022-05-07 20:08:51 +05:30
target.querySelector('.gl-spinner-container').classList.remove('gl-mr-2');
2018-12-05 23:21:45 +05:30
if (isInactive === true) {
restoreBtn.classList.add('hidden');
doneBtn.classList.remove('hidden');
} else if (target === doneBtn) {
2022-10-11 01:57:18 +05:30
row.classList.add('done-reversible', 'gl-bg-gray-50', 'gl-border-gray-100');
2017-08-17 22:00:37 +05:30
restoreBtn.classList.remove('hidden');
} else if (target === restoreBtn) {
2022-10-11 01:57:18 +05:30
row.classList.remove('done-reversible', 'gl-bg-gray-50', 'gl-border-gray-100');
2017-08-17 22:00:37 +05:30
doneBtn.classList.remove('hidden');
} else {
row.parentNode.removeChild(row);
}
}
updateAllStateClicked(e) {
2017-09-10 17:25:29 +05:30
e.stopPropagation();
2017-08-17 22:00:37 +05:30
e.preventDefault();
const target = e.currentTarget;
target.setAttribute('disabled', true);
target.classList.add('disabled');
2018-03-17 18:26:18 +05:30
2022-05-07 20:08:51 +05:30
target.querySelector('.gl-spinner-container').classList.add('gl-mr-2');
2018-03-17 18:26:18 +05:30
axios[target.dataset.method](target.dataset.href, {
ids: this.todo_ids,
2018-12-13 13:39:08 +05:30
})
.then(({ data }) => {
this.updateAllState(target, data);
this.updateBadges(data);
})
2021-09-30 23:02:18 +05:30
.catch(() =>
2022-11-25 23:54:43 +05:30
createAlert({
2021-09-30 23:02:18 +05:30
message: __('Error updating status for all to-do items.'),
}),
);
2017-08-17 22:00:37 +05:30
}
updateAllState(target, data) {
const markAllDoneBtn = document.querySelector('.js-todos-mark-all');
const undoAllBtn = document.querySelector('.js-todos-undo-all');
const todoListContainer = document.querySelector('.js-todos-list-container');
const nothingHereContainer = document.querySelector('.js-nothing-here-container');
target.removeAttribute('disabled');
target.classList.remove('disabled');
2022-05-07 20:08:51 +05:30
target.querySelector('.gl-spinner-container').classList.remove('gl-mr-2');
2018-12-13 13:39:08 +05:30
this.todo_ids = target === markAllDoneBtn ? data.updated_ids : [];
2017-08-17 22:00:37 +05:30
undoAllBtn.classList.toggle('hidden');
markAllDoneBtn.classList.toggle('hidden');
todoListContainer.classList.toggle('hidden');
nothingHereContainer.classList.toggle('hidden');
}
updateBadges(data) {
2022-08-13 15:12:31 +05:30
const event = new CustomEvent('todo:toggle', {
detail: {
count: data.count,
},
});
document.dispatchEvent(event);
2022-10-11 01:57:18 +05:30
// eslint-disable-next-line no-unsanitized/property
2022-03-02 08:16:31 +05:30
document.querySelector('.js-todos-pending .js-todos-badge').innerHTML = addDelimiter(
data.count,
);
2022-10-11 01:57:18 +05:30
// eslint-disable-next-line no-unsanitized/property
2022-03-02 08:16:31 +05:30
document.querySelector('.js-todos-done .js-todos-badge').innerHTML = addDelimiter(
data.done_count,
);
2017-08-17 22:00:37 +05:30
}
goToTodoUrl(e) {
const todoLink = this.dataset.url;
2021-03-08 18:12:59 +05:30
if (!todoLink || e.target.closest('a')) {
2017-08-17 22:00:37 +05:30
return;
}
2018-03-17 18:26:18 +05:30
e.stopPropagation();
e.preventDefault();
2022-07-23 23:45:48 +05:30
const isPrimaryClick = e.button === 0;
2018-03-17 18:26:18 +05:30
if (isMetaClick(e)) {
2017-08-17 22:00:37 +05:30
const windowTarget = '_blank';
2018-03-17 18:26:18 +05:30
window.open(todoLink, windowTarget);
2022-07-23 23:45:48 +05:30
} else if (isPrimaryClick) {
2018-03-17 18:26:18 +05:30
visitUrl(todoLink);
2017-08-17 22:00:37 +05:30
}
}
}