2017-10-09 06:46:40 -04:00
|
|
|
/* 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-12-14 00:26:26 -05:00
|
|
|
/* global Pager */
|
|
|
|
|
2017-10-09 06:46:40 -04:00
|
|
|
export default (function () {
|
|
|
|
const CommitsList = {};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
CommitsList.timer = null;
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-10-09 06:46:40 -04:00
|
|
|
CommitsList.init = function (limit) {
|
2017-06-02 21:40:20 -04:00
|
|
|
this.$contentList = $('.content_list');
|
|
|
|
|
2017-10-09 06:46:40 -04:00
|
|
|
$('body').on('click', '.day-commits-table li.commit', function (e) {
|
|
|
|
if (e.target.nodeName !== 'A') {
|
|
|
|
location.href = $(this).attr('url');
|
2017-03-11 02:30:44 -05:00
|
|
|
e.stopPropagation();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2017-06-02 21:40:20 -04:00
|
|
|
|
2017-08-16 10:56:12 -04:00
|
|
|
Pager.init(parseInt(limit, 10), false, false, this.processCommits);
|
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();
|
|
|
|
return this.initSearch();
|
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-10-09 06:46:40 -04:00
|
|
|
CommitsList.initSearch = function () {
|
2017-03-11 02:30:44 -05:00
|
|
|
this.timer = null;
|
2017-10-09 06:46:40 -04:00
|
|
|
return this.searchField.keyup((function (_this) {
|
|
|
|
return function () {
|
2017-03-11 02:30:44 -05:00
|
|
|
clearTimeout(_this.timer);
|
|
|
|
return _this.timer = setTimeout(_this.filterResults, 500);
|
|
|
|
};
|
|
|
|
})(this));
|
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-10-09 06:46:40 -04:00
|
|
|
CommitsList.filterResults = function () {
|
|
|
|
const form = $('.commits-search-form');
|
|
|
|
const search = CommitsList.searchField.val();
|
2017-03-11 02:30:44 -05:00
|
|
|
if (search === CommitsList.lastSearch) return;
|
2017-10-09 06:46:40 -04:00
|
|
|
const commitsUrl = form.attr('action') + '?' + form.serialize();
|
2017-03-11 02:30:44 -05:00
|
|
|
CommitsList.content.fadeTo('fast', 0.5);
|
|
|
|
return $.ajax({
|
2017-10-09 06:46:40 -04:00
|
|
|
type: 'GET',
|
|
|
|
url: form.attr('action'),
|
2017-03-11 02:30:44 -05:00
|
|
|
data: form.serialize(),
|
2017-10-09 06:46:40 -04:00
|
|
|
complete: function () {
|
2017-03-11 02:30:44 -05:00
|
|
|
return CommitsList.content.fadeTo('fast', 1.0);
|
|
|
|
},
|
2017-10-09 06:46:40 -04:00
|
|
|
success: function (data) {
|
2017-03-11 02:30:44 -05:00
|
|
|
CommitsList.lastSearch = search;
|
|
|
|
CommitsList.content.html(data.html);
|
|
|
|
return history.replaceState({
|
2017-10-09 06:46:40 -04:00
|
|
|
page: commitsUrl,
|
2017-03-11 02:30:44 -05:00
|
|
|
// Change url so if user reload a page - search results are saved
|
|
|
|
}, document.title, commitsUrl);
|
|
|
|
},
|
2017-10-09 06:46:40 -04:00
|
|
|
error: function () {
|
2017-03-11 02:30:44 -05:00
|
|
|
CommitsList.lastSearch = null;
|
|
|
|
},
|
2017-10-09 06:46:40 -04:00
|
|
|
dataType: 'json',
|
2017-03-11 02:30:44 -05:00
|
|
|
});
|
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-06-02 21:40:20 -04:00
|
|
|
// 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.
|
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.
|
|
|
|
commitsCount += Number($(processedData).nextUntil('li.js-commit-header').first().find('li.commit').length);
|
|
|
|
$commitsHeadersLast.find('span.commits-count').text(`${commitsCount} ${gl.text.pluralize('commit', commitsCount)}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
gl.utils.localTimeAgo($processedData.find('.js-timeago'));
|
|
|
|
|
|
|
|
return processedData;
|
|
|
|
};
|
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
return CommitsList;
|
|
|
|
})();
|