2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2019-07-24 13:39:18 -04:00
|
|
|
import { n__ } from '~/locale';
|
2021-02-14 13:09:20 -05:00
|
|
|
import axios from './lib/utils/axios_utils';
|
2017-12-07 06:09:17 -05:00
|
|
|
import { localTimeAgo } from './lib/utils/datetime_utility';
|
2017-12-15 04:31:58 -05:00
|
|
|
import Pager from './pager';
|
2017-11-13 04:11:54 -05:00
|
|
|
|
2018-02-15 12:17:29 -05:00
|
|
|
export default class CommitsList {
|
|
|
|
constructor(limit = 0) {
|
|
|
|
this.timer = null;
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-06-02 21:40:20 -04:00
|
|
|
this.$contentList = $('.content_list');
|
|
|
|
|
2021-04-06 14:09:02 -04:00
|
|
|
Pager.init({ limit: parseInt(limit, 10), prepareData: this.processCommits.bind(this) });
|
2017-06-02 21:40:20 -04:00
|
|
|
|
2017-10-09 06:46:40 -04:00
|
|
|
this.content = $('#commits-list');
|
|
|
|
this.searchField = $('#commits-search');
|
2017-03-11 02:30:44 -05:00
|
|
|
this.lastSearch = this.searchField.val();
|
2018-02-15 12:17:29 -05:00
|
|
|
this.initSearch();
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2018-02-15 12:17:29 -05:00
|
|
|
initSearch() {
|
2017-03-11 02:30:44 -05:00
|
|
|
this.timer = null;
|
2018-02-15 12:17:29 -05:00
|
|
|
this.searchField.on('keyup', () => {
|
|
|
|
clearTimeout(this.timer);
|
|
|
|
this.timer = setTimeout(this.filterResults.bind(this), 500);
|
|
|
|
});
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2018-02-15 12:17:29 -05:00
|
|
|
filterResults() {
|
2017-10-09 06:46:40 -04:00
|
|
|
const form = $('.commits-search-form');
|
2018-02-15 12:17:29 -05:00
|
|
|
const search = this.searchField.val();
|
|
|
|
if (search === this.lastSearch) return Promise.resolve();
|
|
|
|
const commitsUrl = `${form.attr('action')}?${form.serialize()}`;
|
2020-12-03 07:09:39 -05:00
|
|
|
this.content.addClass('gl-opacity-5');
|
2018-10-24 15:17:03 -04:00
|
|
|
const params = form.serializeArray().reduce(
|
|
|
|
(acc, obj) =>
|
|
|
|
Object.assign(acc, {
|
|
|
|
[obj.name]: obj.value,
|
|
|
|
}),
|
|
|
|
{},
|
|
|
|
);
|
2018-01-26 04:41:13 -05:00
|
|
|
|
2018-10-24 15:17:03 -04:00
|
|
|
return axios
|
|
|
|
.get(form.attr('action'), {
|
|
|
|
params,
|
|
|
|
})
|
2018-01-26 04:41:13 -05:00
|
|
|
.then(({ data }) => {
|
2018-02-15 12:17:29 -05:00
|
|
|
this.lastSearch = search;
|
|
|
|
this.content.html(data.html);
|
2020-12-03 07:09:39 -05:00
|
|
|
this.content.removeClass('gl-opacity-5');
|
2018-01-26 04:41:13 -05:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
// Change url so if user reload a page - search results are saved
|
2018-10-24 15:17:03 -04:00
|
|
|
window.history.replaceState(
|
|
|
|
{
|
|
|
|
page: commitsUrl,
|
|
|
|
},
|
|
|
|
document.title,
|
|
|
|
commitsUrl,
|
|
|
|
);
|
2018-01-26 04:41:13 -05:00
|
|
|
})
|
|
|
|
.catch(() => {
|
2020-12-03 07:09:39 -05:00
|
|
|
this.content.removeClass('gl-opacity-5');
|
2018-02-15 12:17:29 -05:00
|
|
|
this.lastSearch = null;
|
2018-01-26 04:41:13 -05:00
|
|
|
});
|
2018-02-15 12:17:29 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-06-02 21:40:20 -04:00
|
|
|
// Prepare loaded data.
|
2018-02-15 12:17:29 -05:00
|
|
|
processCommits(data) {
|
2017-06-02 21:40:20 -04:00
|
|
|
let processedData = data;
|
|
|
|
const $processedData = $(processedData);
|
2018-02-15 12:17:29 -05:00
|
|
|
const $commitsHeadersLast = this.$contentList.find('li.js-commit-header').last();
|
2017-06-02 21:40:20 -04:00
|
|
|
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.
|
2017-10-09 06:46:40 -04:00
|
|
|
processedData = $processedData.not(`li.js-commit-header[data-day='${loadedShownDayFirst}']`);
|
2017-06-02 21:40:20 -04:00
|
|
|
|
|
|
|
// Update commits count in the previous commits header.
|
2018-10-24 15:17:03 -04:00
|
|
|
commitsCount += Number(
|
2020-12-23 07:10:26 -05:00
|
|
|
$(processedData).nextUntil('li.js-commit-header').first().find('li.commit').length,
|
2018-10-24 15:17:03 -04:00
|
|
|
);
|
2019-07-24 13:39:18 -04:00
|
|
|
|
2018-10-24 15:17:03 -04:00
|
|
|
$commitsHeadersLast
|
|
|
|
.find('span.commits-count')
|
2019-07-24 13:39:18 -04:00
|
|
|
.text(n__('%d commit', '%d commits', commitsCount));
|
2017-06-02 21:40:20 -04:00
|
|
|
}
|
|
|
|
|
2017-12-07 06:09:17 -05:00
|
|
|
localTimeAgo($processedData.find('.js-timeago'));
|
2017-06-02 21:40:20 -04:00
|
|
|
|
|
|
|
return processedData;
|
2018-02-15 12:17:29 -05:00
|
|
|
}
|
|
|
|
}
|