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

104 lines
3.6 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
/* eslint-disable func-names, wrap-iife, consistent-return,
no-return-assign, no-param-reassign, one-var-declaration-per-line, no-unused-vars,
prefer-template, object-shorthand, prefer-arrow-callback */
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
import { pluralize } from './lib/utils/text_utility';
import { localTimeAgo } from './lib/utils/datetime_utility';
import Pager from './pager';
import axios from './lib/utils/axios_utils';
export default (function () {
const CommitsList = {};
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
CommitsList.timer = null;
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
CommitsList.init = function (limit) {
2017-09-10 17:25:29 +05:30
this.$contentList = $('.content_list');
2018-03-17 18:26:18 +05:30
$('body').on('click', '.day-commits-table li.commit', function (e) {
if (e.target.nodeName !== 'A') {
location.href = $(this).attr('url');
2017-08-17 22:00:37 +05:30
e.stopPropagation();
return false;
}
});
2017-09-10 17:25:29 +05:30
Pager.init(parseInt(limit, 10), false, false, this.processCommits);
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();
return this.initSearch();
};
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
CommitsList.initSearch = function () {
2017-08-17 22:00:37 +05:30
this.timer = null;
2018-03-17 18:26:18 +05:30
return this.searchField.keyup((function (_this) {
return function () {
2017-08-17 22:00:37 +05:30
clearTimeout(_this.timer);
return _this.timer = setTimeout(_this.filterResults, 500);
};
})(this));
};
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
CommitsList.filterResults = function () {
const form = $('.commits-search-form');
const search = CommitsList.searchField.val();
if (search === CommitsList.lastSearch) return Promise.resolve();
const commitsUrl = form.attr('action') + '?' + form.serialize();
2017-08-17 22:00:37 +05:30
CommitsList.content.fadeTo('fast', 0.5);
2018-03-17 18:26:18 +05:30
const params = form.serializeArray().reduce((acc, obj) => Object.assign(acc, {
[obj.name]: obj.value,
}), {});
return axios.get(form.attr('action'), {
params,
})
.then(({ data }) => {
2017-08-17 22:00:37 +05:30
CommitsList.lastSearch = search;
CommitsList.content.html(data.html);
2018-03-17 18:26:18 +05:30
CommitsList.content.fadeTo('fast', 1.0);
2017-08-17 22:00:37 +05:30
// Change url so if user reload a page - search results are saved
2018-03-17 18:26:18 +05:30
history.replaceState({
page: commitsUrl,
2017-08-17 22:00:37 +05:30
}, document.title, commitsUrl);
2018-03-17 18:26:18 +05:30
})
.catch(() => {
CommitsList.content.fadeTo('fast', 1.0);
2017-08-17 22:00:37 +05:30
CommitsList.lastSearch = null;
2018-03-17 18:26:18 +05:30
});
2017-08-17 22:00:37 +05:30
};
2016-09-13 17:45:13 +05:30
2017-09-10 17:25:29 +05:30
// Prepare loaded data.
CommitsList.processCommits = (data) => {
let processedData = data;
const $processedData = $(processedData);
const $commitsHeadersLast = CommitsList.$contentList.find('li.js-commit-header').last();
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.
commitsCount += Number($(processedData).nextUntil('li.js-commit-header').first().find('li.commit').length);
2018-03-17 18:26:18 +05:30
$commitsHeadersLast.find('span.commits-count').text(`${commitsCount} ${pluralize('commit', 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;
};
2017-08-17 22:00:37 +05:30
return CommitsList;
})();