Commit f6624b5c authored by Mike Greiling's avatar Mike Greiling

fix eslint failures on Diff and MergeRequestTabs

parent 825fea63
/* eslint-disable */
/* eslint-disable class-methods-use-this, no-param-reassign */
((global) => {
const UNFOLD_COUNT = 20;
......@@ -29,7 +29,8 @@
const newLineNumber = ref[1];
const offset = newLineNumber - oldLineNumber;
const bottom = $target.hasClass('js-unfold-bottom');
let since, to;
let since;
let to;
let unfold = true;
if (bottom) {
......@@ -54,19 +55,19 @@
const view = file.data('view');
const params = { since, to, bottom, offset, unfold, view };
$.get(link, params, (response) => $target.parent().replaceWith(response));
$.get(link, params, response => $target.parent().replaceWith(response));
}
handleClickLineNum(event) {
const hash = $(event.currentTarget).attr('href');
event.preventDefault();
if (history.pushState) {
history.pushState(null, null, hash);
if (window.history.pushState) {
window.history.pushState(null, null, hash);
} else {
window.location.hash = hash;
}
this.highlighSelectedLine();
};
}
diffViewType() {
return $('.inline-parallel-buttons a.active').data('view-type');
......@@ -76,10 +77,7 @@
if (!line.children().length) {
return [0, 0];
}
return line.find('.diff-line-num').map(function() {
return parseInt($(this).data('linenumber'));
});
return line.find('.diff-line-num').map((i, elm) => parseInt($(elm).data('linenumber'), 10));
}
highlighSelectedLine() {
......@@ -96,5 +94,4 @@
}
global.Diff = Diff;
})(window.gl || (window.gl = {}));
/* eslint-disable max-len, func-names, space-before-function-paren, no-var, space-before-blocks, prefer-rest-params, wrap-iife, no-use-before-define, no-underscore-dangle, no-undef, one-var, one-var-declaration-per-line, quotes, comma-dangle, consistent-return, prefer-template, no-param-reassign, camelcase, vars-on-top, space-in-parens, curly, prefer-arrow-callback, no-unused-vars, no-return-assign, semi, object-shorthand, operator-assignment, padded-blocks, max-len */
/* eslint-disable no-new, no-param-reassign, class-methods-use-this */
/* global Breakpoints, Cookies, DiffNotesApp */
/*= require js.cookie */
/*= require breakpoints */
/* eslint-disable max-len */
// MergeRequestTabs
//
// Handles persisting and restoring the current tab selection and lazily-loading
// content on the MergeRequests#show page.
//
/*= require js.cookie */
//
// ### Example Markup
//
......@@ -45,11 +48,15 @@
// </div>
// </div>
//
/* eslint-enable max-len */
((global) => {
// Store the `location` object, allowing for easier stubbing in tests
let location = window.location;
class MergeRequestTabs {
constructor({ action, setUrl, buildsLoaded } = {}) {
constructor({ action, setUrl, buildsLoaded, stubLocation } = {}) {
this.diffsLoaded = false;
this.buildsLoaded = false;
this.pipelinesLoaded = false;
......@@ -63,8 +70,10 @@
this.tabShown = this.tabShown.bind(this);
this.showTab = this.showTab.bind(this);
// Store the `location` object, allowing for easier stubbing in tests
this._location = window.location;
if (stubLocation) {
location = stubLocation;
}
this.bindEvents();
this.activateTab(action);
this.initAffix();
......@@ -97,15 +106,15 @@
this.resetViewContainer();
} else if (this.isDiffAction(action)) {
this.loadDiff($target.attr('href'));
if ((typeof bp !== "undefined" && bp !== null) && bp.getBreakpointSize() !== 'lg') {
if (Breakpoints.get().getBreakpointSize() !== 'lg') {
this.shrinkView();
}
if (this.diffViewType() === 'parallel') {
this.expandViewContainer();
}
const navBarHeight = $('.navbar-gitlab').outerHeight();
$.scrollTo(".merge-request-details .merge-request-tabs", {
offset: -navBarHeight
$.scrollTo('.merge-request-details .merge-request-tabs', {
offset: -navBarHeight,
});
} else if (action === 'builds') {
this.loadBuilds($target.attr('href'));
......@@ -125,13 +134,12 @@
}
scrollToElement(container) {
if (window.location.hash) {
if (location.hash) {
const navBarHeight = $('.navbar-gitlab').outerHeight() + $('.layout-nav').outerHeight() + document.querySelector('.js-tabs-affix').offsetHeight;
const navBarHeight = $('.navbar-gitlab').outerHeight() + $('.layout-nav').outerHeight();
const $el = $(`${container} ${window.location.hash}:not(.match)`);
const $el = $(`${container} ${location.hash}:not(.match)`);
if ($el.length) {
$.scrollTo($el[0], {
offset: -navBarHeight
offset: -navBarHeight,
});
}
}
......@@ -139,11 +147,9 @@
// Activate a tab based on the current action
activateTab(action) {
if (action === 'show') {
action = 'notes';
}
const activate = action === 'show' ? 'notes' : action;
// important note: the .tab('show') method triggers 'shown.bs.tab' event itself
$(`.merge-request-tabs a[data-action='${action}']`).tab('show');
$(`.merge-request-tabs a[data-action='${activate}']`).tab('show');
}
// Replaces the current Merge Request-specific action in the URL with a new one
......@@ -167,33 +173,29 @@
//
// Returns the new URL String
setCurrentAction(action) {
// Normalize action, just to be safe
if (action === 'show') {
action = 'notes';
}
this.currentAction = action;
this.currentAction = action === 'show' ? 'notes' : action;
// Remove a trailing '/commits' '/diffs' '/builds' '/pipelines' '/new' '/new/diffs'
let new_state = this._location.pathname.replace(/\/(commits|diffs|builds|pipelines|new|new\/diffs)(\.html)?\/?$/, '');
let newState = location.pathname.replace(/\/(commits|diffs|builds|pipelines|new|new\/diffs)(\.html)?\/?$/, '');
// Append the new action if we're on a tab other than 'notes'
if (action !== 'notes') {
new_state += `/${action}`;
if (this.currentAction !== 'notes') {
newState += `/${this.currentAction}`;
}
// Ensure parameters and hash come along for the ride
new_state += this._location.search + this._location.hash;
newState += location.search + location.hash;
// Replace the current history state with the new one without breaking
// Turbolinks' history.
//
// See https://github.com/rails/turbolinks/issues/363
history.replaceState({
window.history.replaceState({
turbolinks: true,
url: new_state
}, document.title, new_state);
url: newState,
}, document.title, newState);
return new_state;
return newState;
}
loadCommits(source) {
......@@ -203,11 +205,11 @@
this.ajaxGet({
url: `${source}.json`,
success: (data) => {
document.querySelector("div#commits").innerHTML = data.html;
document.querySelector('div#commits').innerHTML = data.html;
gl.utils.localTimeAgo($('.js-timeago', 'div#commits'));
this.commitsLoaded = true;
this.scrollToElement("#commits");
}
this.scrollToElement('#commits');
},
});
}
......@@ -222,7 +224,7 @@
url.href = source;
this.ajaxGet({
url: `${url.pathname}.json${this._location.search}`,
url: `${url.pathname}.json${location.search}`,
success: (data) => {
$('#diffs').html(data.html);
......@@ -233,14 +235,14 @@
gl.utils.localTimeAgo($('.js-timeago', 'div#diffs'));
$('#diffs .js-syntax-highlight').syntaxHighlight();
if (this.diffViewType() === 'parallel' && this.isDiffAction(this.currentAction) ) {
if (this.diffViewType() === 'parallel' && this.isDiffAction(this.currentAction)) {
this.expandViewContainer();
}
this.diffsLoaded = true;
this.scrollToElement("#diffs");
this.scrollToElement('#diffs');
new gl.Diff();
}
},
});
}
......@@ -251,12 +253,12 @@
this.ajaxGet({
url: `${source}.json`,
success: (data) => {
document.querySelector("div#builds").innerHTML = data.html;
document.querySelector('div#builds').innerHTML = data.html;
gl.utils.localTimeAgo($('.js-timeago', 'div#builds'));
this.buildsLoaded = true;
new gl.Pipelines();
this.scrollToElement("#builds");
}
this.scrollToElement('#builds');
},
});
}
......@@ -270,8 +272,8 @@
$('#pipelines').html(data.html);
gl.utils.localTimeAgo($('.js-timeago', '#pipelines'));
this.pipelinesLoaded = true;
this.scrollToElement("#pipelines");
}
this.scrollToElement('#pipelines');
},
});
}
......@@ -287,10 +289,9 @@
beforeSend: () => this.toggleLoading(true),
complete: () => this.toggleLoading(false),
dataType: 'json',
type: 'GET'
type: 'GET',
};
options = $.extend({}, defaults, options);
$.ajax(options);
$.ajax($.extend({}, defaults, options));
}
diffViewType() {
......@@ -298,7 +299,7 @@
}
isDiffAction(action) {
return action === 'diffs' || action === 'new/diffs'
return action === 'diffs' || action === 'new/diffs';
}
expandViewContainer() {
......@@ -356,11 +357,13 @@
const $layoutNav = $('.layout-nav');
$tabs.off('affix.bs.affix affix-top.bs.affix')
.affix({ offset: {
top: () => (
$diffTabs.offset().top - $tabs.height() - $fixedNav.height() - $layoutNav.height()
)
}})
.affix({
offset: {
top: () => (
$diffTabs.offset().top - $tabs.height() - $fixedNav.height() - $layoutNav.height()
),
},
})
.on('affix.bs.affix', () => $diffTabs.css({ marginTop: $tabs.height() }))
.on('affix-top.bs.affix', () => $diffTabs.css({ marginTop: '' }));
......@@ -372,5 +375,4 @@
}
global.MergeRequestTabs = MergeRequestTabs;
})(window.gl || (window.gl = {}));
/* eslint-disable space-before-function-paren, no-var, comma-dangle, dot-notation, quotes, no-undef, no-return-assign, no-underscore-dangle, camelcase, padded-blocks, max-len */
/* eslint-disable no-var, comma-dangle, object-shorthand */
/*= require merge_request_tabs */
//= require breakpoints
(function() {
describe('MergeRequestTabs', function() {
var stubLocation;
stubLocation = function(stubs) {
var defaults;
defaults = {
(function () {
describe('MergeRequestTabs', function () {
var stubLocation = {};
var setLocation = function (stubs) {
var defaults = {
pathname: '',
search: '',
hash: ''
};
return $.extend(defaults, stubs);
$.extend(stubLocation, defaults, stubs || {});
};
fixture.preload('merge_request_tabs.html');
beforeEach(function() {
this["class"] = new gl.MergeRequestTabs();
return this.spies = {
ajax: spyOn($, 'ajax').and.callFake(function() {}),
history: spyOn(history, 'replaceState').and.callFake(function() {})
beforeEach(function () {
this.class = new gl.MergeRequestTabs({ stubLocation: stubLocation });
setLocation();
this.spies = {
ajax: spyOn($, 'ajax').and.callFake(function () {}),
history: spyOn(window.history, 'replaceState').and.callFake(function () {})
};
});
describe('#activateTab', function() {
beforeEach(function() {
describe('#activateTab', function () {
beforeEach(function () {
fixture.load('merge_request_tabs.html');
return this.subject = this["class"].activateTab;
this.subject = this.class.activateTab;
});
it('shows the first tab when action is show', function() {
it('shows the first tab when action is show', function () {
this.subject('show');
return expect($('#notes')).toHaveClass('active');
expect($('#notes')).toHaveClass('active');
});
it('shows the notes tab when action is notes', function() {
it('shows the notes tab when action is notes', function () {
this.subject('notes');
return expect($('#notes')).toHaveClass('active');
expect($('#notes')).toHaveClass('active');
});
it('shows the commits tab when action is commits', function() {
it('shows the commits tab when action is commits', function () {
this.subject('commits');
return expect($('#commits')).toHaveClass('active');
expect($('#commits')).toHaveClass('active');
});
return it('shows the diffs tab when action is diffs', function() {
it('shows the diffs tab when action is diffs', function () {
this.subject('diffs');
return expect($('#diffs')).toHaveClass('active');
expect($('#diffs')).toHaveClass('active');
});
});
return describe('#setCurrentAction', function() {
beforeEach(function() {
return this.subject = this["class"].setCurrentAction;
describe('#setCurrentAction', function () {
beforeEach(function () {
this.subject = this.class.setCurrentAction;
});
it('changes from commits', function() {
this["class"]._location = stubLocation({
it('changes from commits', function () {
setLocation({
pathname: '/foo/bar/merge_requests/1/commits'
});
expect(this.subject('notes')).toBe('/foo/bar/merge_requests/1');
return expect(this.subject('diffs')).toBe('/foo/bar/merge_requests/1/diffs');
expect(this.subject('diffs')).toBe('/foo/bar/merge_requests/1/diffs');
});
it('changes from diffs', function() {
this["class"]._location = stubLocation({
it('changes from diffs', function () {
setLocation({
pathname: '/foo/bar/merge_requests/1/diffs'
});
expect(this.subject('notes')).toBe('/foo/bar/merge_requests/1');
return expect(this.subject('commits')).toBe('/foo/bar/merge_requests/1/commits');
expect(this.subject('commits')).toBe('/foo/bar/merge_requests/1/commits');
});
it('changes from diffs.html', function() {
this["class"]._location = stubLocation({
it('changes from diffs.html', function () {
setLocation({
pathname: '/foo/bar/merge_requests/1/diffs.html'
});
expect(this.subject('notes')).toBe('/foo/bar/merge_requests/1');
return expect(this.subject('commits')).toBe('/foo/bar/merge_requests/1/commits');
expect(this.subject('commits')).toBe('/foo/bar/merge_requests/1/commits');
});
it('changes from notes', function() {
this["class"]._location = stubLocation({
it('changes from notes', function () {
setLocation({
pathname: '/foo/bar/merge_requests/1'
});
expect(this.subject('diffs')).toBe('/foo/bar/merge_requests/1/diffs');
return expect(this.subject('commits')).toBe('/foo/bar/merge_requests/1/commits');
expect(this.subject('commits')).toBe('/foo/bar/merge_requests/1/commits');
});
it('includes search parameters and hash string', function() {
this["class"]._location = stubLocation({
it('includes search parameters and hash string', function () {
setLocation({
pathname: '/foo/bar/merge_requests/1/diffs',
search: '?view=parallel',
hash: '#L15-35'
});
return expect(this.subject('show')).toBe('/foo/bar/merge_requests/1?view=parallel#L15-35');
expect(this.subject('show')).toBe('/foo/bar/merge_requests/1?view=parallel#L15-35');
});
it('replaces the current history state', function() {
var new_state;
this["class"]._location = stubLocation({
it('replaces the current history state', function () {
var newState;
setLocation({
pathname: '/foo/bar/merge_requests/1'
});
new_state = this.subject('commits');
return expect(this.spies.history).toHaveBeenCalledWith({
newState = this.subject('commits');
expect(this.spies.history).toHaveBeenCalledWith({
turbolinks: true,
url: new_state
}, document.title, new_state);
url: newState
}, document.title, newState);
});
return it('treats "show" like "notes"', function() {
this["class"]._location = stubLocation({
it('treats "show" like "notes"', function () {
setLocation({
pathname: '/foo/bar/merge_requests/1/commits'
});
return expect(this.subject('show')).toBe('/foo/bar/merge_requests/1');
expect(this.subject('show')).toBe('/foo/bar/merge_requests/1');
});
});
});
}).call(this);
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