Commit 2597d8d2 authored by Filipa Lacerda's avatar Filipa Lacerda Committed by Phil Hughes

Removes CommitsList from global namespace

parent 8af29c21
/* eslint-disable func-names, space-before-function-paren, wrap-iife, quotes, consistent-return, no-return-assign, no-param-reassign, one-var, no-var, one-var-declaration-per-line, no-unused-vars, prefer-template, object-shorthand, comma-dangle, max-len, prefer-arrow-callback */ /* 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 */
/* global Pager */ /* global Pager */
window.CommitsList = (function() { export default (function () {
var CommitsList = {}; const CommitsList = {};
CommitsList.timer = null; CommitsList.timer = null;
CommitsList.init = function(limit) { CommitsList.init = function (limit) {
this.$contentList = $('.content_list'); this.$contentList = $('.content_list');
$("body").on("click", ".day-commits-table li.commit", function(e) { $('body').on('click', '.day-commits-table li.commit', function (e) {
if (e.target.nodeName !== "A") { if (e.target.nodeName !== 'A') {
location.href = $(this).attr("url"); location.href = $(this).attr('url');
e.stopPropagation(); e.stopPropagation();
return false; return false;
} }
...@@ -19,48 +21,47 @@ window.CommitsList = (function() { ...@@ -19,48 +21,47 @@ window.CommitsList = (function() {
Pager.init(parseInt(limit, 10), false, false, this.processCommits); Pager.init(parseInt(limit, 10), false, false, this.processCommits);
this.content = $("#commits-list"); this.content = $('#commits-list');
this.searchField = $("#commits-search"); this.searchField = $('#commits-search');
this.lastSearch = this.searchField.val(); this.lastSearch = this.searchField.val();
return this.initSearch(); return this.initSearch();
}; };
CommitsList.initSearch = function() { CommitsList.initSearch = function () {
this.timer = null; this.timer = null;
return this.searchField.keyup((function(_this) { return this.searchField.keyup((function (_this) {
return function() { return function () {
clearTimeout(_this.timer); clearTimeout(_this.timer);
return _this.timer = setTimeout(_this.filterResults, 500); return _this.timer = setTimeout(_this.filterResults, 500);
}; };
})(this)); })(this));
}; };
CommitsList.filterResults = function() { CommitsList.filterResults = function () {
var commitsUrl, form, search; const form = $('.commits-search-form');
form = $(".commits-search-form"); const search = CommitsList.searchField.val();
search = CommitsList.searchField.val();
if (search === CommitsList.lastSearch) return; if (search === CommitsList.lastSearch) return;
commitsUrl = form.attr("action") + '?' + form.serialize(); const commitsUrl = form.attr('action') + '?' + form.serialize();
CommitsList.content.fadeTo('fast', 0.5); CommitsList.content.fadeTo('fast', 0.5);
return $.ajax({ return $.ajax({
type: "GET", type: 'GET',
url: form.attr("action"), url: form.attr('action'),
data: form.serialize(), data: form.serialize(),
complete: function() { complete: function () {
return CommitsList.content.fadeTo('fast', 1.0); return CommitsList.content.fadeTo('fast', 1.0);
}, },
success: function(data) { success: function (data) {
CommitsList.lastSearch = search; CommitsList.lastSearch = search;
CommitsList.content.html(data.html); CommitsList.content.html(data.html);
return history.replaceState({ return history.replaceState({
page: commitsUrl page: commitsUrl,
// Change url so if user reload a page - search results are saved // Change url so if user reload a page - search results are saved
}, document.title, commitsUrl); }, document.title, commitsUrl);
}, },
error: function() { error: function () {
CommitsList.lastSearch = null; CommitsList.lastSearch = null;
}, },
dataType: "json" dataType: 'json',
}); });
}; };
...@@ -81,7 +82,7 @@ window.CommitsList = (function() { ...@@ -81,7 +82,7 @@ window.CommitsList = (function() {
commitsCount = $commitsHeadersLast.nextUntil('li.js-commit-header').find('li.commit').length; commitsCount = $commitsHeadersLast.nextUntil('li.js-commit-header').find('li.commit').length;
// Remove duplicate of commits header. // Remove duplicate of commits header.
processedData = $processedData.not(`li.js-commit-header[data-day="${loadedShownDayFirst}"]`); processedData = $processedData.not(`li.js-commit-header[data-day='${loadedShownDayFirst}']`);
// Update commits count in the previous commits header. // Update commits count in the previous commits header.
commitsCount += Number($(processedData).nextUntil('li.js-commit-header').first().find('li.commit').length); commitsCount += Number($(processedData).nextUntil('li.js-commit-header').first().find('li.commit').length);
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
/* global IssuableForm */ /* global IssuableForm */
/* global LabelsSelect */ /* global LabelsSelect */
/* global MilestoneSelect */ /* global MilestoneSelect */
/* global CommitsList */
/* global NewBranchForm */ /* global NewBranchForm */
/* global NotificationsForm */ /* global NotificationsForm */
/* global NotificationsDropdown */ /* global NotificationsDropdown */
...@@ -35,6 +34,7 @@ ...@@ -35,6 +34,7 @@
/* global Sidebar */ /* global Sidebar */
/* global ShortcutsWiki */ /* global ShortcutsWiki */
import CommitsList from './commits';
import Issue from './issue'; import Issue from './issue';
import BindInOut from './behaviors/bind_in_out'; import BindInOut from './behaviors/bind_in_out';
import DeleteModal from './branches/branches_delete_modal'; import DeleteModal from './branches/branches_delete_modal';
......
/* global CommitsList */
import 'vendor/jquery.endless-scroll'; import 'vendor/jquery.endless-scroll';
import '~/pager'; import '~/pager';
import '~/commits'; import CommitsList from '~/commits';
(() => {
describe('Commits List', () => {
beforeEach(() => {
setFixtures(`
<form class="commits-search-form" action="/h5bp/html5-boilerplate/commits/master">
<input id="commits-search">
</form>
<ol id="commits-list"></ol>
`);
});
it('should be defined', () => { describe('Commits List', () => {
expect(CommitsList).toBeDefined(); beforeEach(() => {
}); setFixtures(`
<form class="commits-search-form" action="/h5bp/html5-boilerplate/commits/master">
<input id="commits-search">
</form>
<ol id="commits-list"></ol>
`);
});
describe('processCommits', () => { it('should be defined', () => {
it('should join commit headers', () => { expect(CommitsList).toBeDefined();
CommitsList.$contentList = $(` });
<div>
<li class="commit-header" data-day="2016-09-20">
<span class="day">20 Sep, 2016</span>
<span class="commits-count">1 commit</span>
</li>
<li class="commit"></li>
</div>
`);
const data = ` describe('processCommits', () => {
it('should join commit headers', () => {
CommitsList.$contentList = $(`
<div>
<li class="commit-header" data-day="2016-09-20"> <li class="commit-header" data-day="2016-09-20">
<span class="day">20 Sep, 2016</span> <span class="day">20 Sep, 2016</span>
<span class="commits-count">1 commit</span> <span class="commits-count">1 commit</span>
</li> </li>
<li class="commit"></li> <li class="commit"></li>
`; </div>
`);
// The last commit header should be removed const data = `
// since the previous one has the same data-day value. <li class="commit-header" data-day="2016-09-20">
expect(CommitsList.processCommits(data).find('li.commit-header').length).toBe(0); <span class="day">20 Sep, 2016</span>
}); <span class="commits-count">1 commit</span>
</li>
<li class="commit"></li>
`;
// The last commit header should be removed
// since the previous one has the same data-day value.
expect(CommitsList.processCommits(data).find('li.commit-header').length).toBe(0);
}); });
});
describe('on entering input', () => { describe('on entering input', () => {
let ajaxSpy; let ajaxSpy;
beforeEach(() => { beforeEach(() => {
CommitsList.init(25); CommitsList.init(25);
CommitsList.searchField.val(''); CommitsList.searchField.val('');
spyOn(history, 'replaceState').and.stub(); spyOn(history, 'replaceState').and.stub();
ajaxSpy = spyOn(jQuery, 'ajax').and.callFake((req) => { ajaxSpy = spyOn(jQuery, 'ajax').and.callFake((req) => {
req.success({ req.success({
data: '<li>Result</li>', data: '<li>Result</li>',
});
}); });
}); });
});
it('should save the last search string', () => { it('should save the last search string', () => {
CommitsList.searchField.val('GitLab'); CommitsList.searchField.val('GitLab');
CommitsList.filterResults(); CommitsList.filterResults();
expect(ajaxSpy).toHaveBeenCalled(); expect(ajaxSpy).toHaveBeenCalled();
expect(CommitsList.lastSearch).toEqual('GitLab'); expect(CommitsList.lastSearch).toEqual('GitLab');
}); });
it('should not make ajax call if the input does not change', () => { it('should not make ajax call if the input does not change', () => {
CommitsList.filterResults(); CommitsList.filterResults();
expect(ajaxSpy).not.toHaveBeenCalled(); expect(ajaxSpy).not.toHaveBeenCalled();
expect(CommitsList.lastSearch).toEqual(''); expect(CommitsList.lastSearch).toEqual('');
});
}); });
}); });
})(); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment