debian-mirror-gitlab/app/assets/javascripts/commits.js

101 lines
3 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2019-10-12 21:52:04 +05:30
import { n__ } from '~/locale';
2021-03-11 19:13:27 +05:30
import axios from './lib/utils/axios_utils';
2018-03-17 18:26:18 +05:30
import { localTimeAgo } from './lib/utils/datetime_utility';
import Pager from './pager';
2018-03-27 19:54:05 +05:30
export default class CommitsList {
constructor(limit = 0) {
this.timer = null;
2016-09-13 17:45:13 +05:30
2017-09-10 17:25:29 +05:30
this.$contentList = $('.content_list');
2018-03-27 19:54:05 +05:30
Pager.init(parseInt(limit, 10), false, false, this.processCommits.bind(this));
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
this.content = $('#commits-list');
this.searchField = $('#commits-search');
2017-08-17 22:00:37 +05:30
this.lastSearch = this.searchField.val();
2018-03-27 19:54:05 +05:30
this.initSearch();
}
2016-09-13 17:45:13 +05:30
2018-03-27 19:54:05 +05:30
initSearch() {
2017-08-17 22:00:37 +05:30
this.timer = null;
2018-03-27 19:54:05 +05:30
this.searchField.on('keyup', () => {
clearTimeout(this.timer);
this.timer = setTimeout(this.filterResults.bind(this), 500);
});
}
2016-09-13 17:45:13 +05:30
2018-03-27 19:54:05 +05:30
filterResults() {
2018-03-17 18:26:18 +05:30
const form = $('.commits-search-form');
2018-03-27 19:54:05 +05:30
const search = this.searchField.val();
if (search === this.lastSearch) return Promise.resolve();
const commitsUrl = `${form.attr('action')}?${form.serialize()}`;
2021-02-22 17:27:13 +05:30
this.content.addClass('gl-opacity-5');
2018-12-13 13:39:08 +05:30
const params = form.serializeArray().reduce(
(acc, obj) =>
Object.assign(acc, {
[obj.name]: obj.value,
}),
{},
);
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
return axios
.get(form.attr('action'), {
params,
})
2018-03-17 18:26:18 +05:30
.then(({ data }) => {
2018-03-27 19:54:05 +05:30
this.lastSearch = search;
this.content.html(data.html);
2021-02-22 17:27:13 +05:30
this.content.removeClass('gl-opacity-5');
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
// Change url so if user reload a page - search results are saved
2018-12-13 13:39:08 +05:30
window.history.replaceState(
{
page: commitsUrl,
},
document.title,
commitsUrl,
);
2018-03-17 18:26:18 +05:30
})
.catch(() => {
2021-02-22 17:27:13 +05:30
this.content.removeClass('gl-opacity-5');
2018-03-27 19:54:05 +05:30
this.lastSearch = null;
2018-03-17 18:26:18 +05:30
});
2018-03-27 19:54:05 +05:30
}
2016-09-13 17:45:13 +05:30
2017-09-10 17:25:29 +05:30
// Prepare loaded data.
2018-03-27 19:54:05 +05:30
processCommits(data) {
2017-09-10 17:25:29 +05:30
let processedData = data;
const $processedData = $(processedData);
2018-03-27 19:54:05 +05:30
const $commitsHeadersLast = this.$contentList.find('li.js-commit-header').last();
2017-09-10 17:25:29 +05:30
const lastShownDay = $commitsHeadersLast.data('day');
const $loadedCommitsHeadersFirst = $processedData.filter('li.js-commit-header').first();
const loadedShownDayFirst = $loadedCommitsHeadersFirst.data('day');
let commitsCount;
// If commits headers show the same date,
// remove the last header and change the previous one.
if (lastShownDay === loadedShownDayFirst) {
// Last shown commits count under the last commits header.
commitsCount = $commitsHeadersLast.nextUntil('li.js-commit-header').find('li.commit').length;
// Remove duplicate of commits header.
2018-03-17 18:26:18 +05:30
processedData = $processedData.not(`li.js-commit-header[data-day='${loadedShownDayFirst}']`);
2017-09-10 17:25:29 +05:30
// Update commits count in the previous commits header.
2018-12-13 13:39:08 +05:30
commitsCount += Number(
2021-03-08 18:12:59 +05:30
$(processedData).nextUntil('li.js-commit-header').first().find('li.commit').length,
2018-12-13 13:39:08 +05:30
);
2019-10-12 21:52:04 +05:30
2018-12-13 13:39:08 +05:30
$commitsHeadersLast
.find('span.commits-count')
2019-10-12 21:52:04 +05:30
.text(n__('%d commit', '%d commits', commitsCount));
2017-09-10 17:25:29 +05:30
}
2018-03-17 18:26:18 +05:30
localTimeAgo($processedData.find('.js-timeago'));
2017-09-10 17:25:29 +05:30
return processedData;
2018-03-27 19:54:05 +05:30
}
}