Commit f14c5ae8 authored by Mike Greiling's avatar Mike Greiling

refactor MergeRequestTabs to es6 class syntax

parent ac0d9b34
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
if (window.mrTabs) { if (window.mrTabs) {
window.mrTabs.unbindEvents(); window.mrTabs.unbindEvents();
} }
window.mrTabs = new MergeRequestTabs(this.opts); window.mrTabs = new gl.MergeRequestTabs(this.opts);
}; };
MergeRequest.prototype.showAllCommits = function() { MergeRequest.prototype.showAllCommits = function() {
......
...@@ -45,54 +45,49 @@ ...@@ -45,54 +45,49 @@
// </div> // </div>
// </div> // </div>
// //
(function() { ((global) => {
var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
this.MergeRequestTabs = (function() { class MergeRequestTabs {
MergeRequestTabs.prototype.diffsLoaded = false;
MergeRequestTabs.prototype.buildsLoaded = false; constructor({ action, setUrl, buildsLoaded } = {}) {
this.diffsLoaded = false;
this.buildsLoaded = false;
this.pipelinesLoaded = false;
this.commitsLoaded = false;
this.fixedLayoutPref = null;
MergeRequestTabs.prototype.pipelinesLoaded = false; this.setUrl = setUrl !== undefined ? setUrl : true;
this.buildsLoaded = buildsLoaded || false;
MergeRequestTabs.prototype.commitsLoaded = false; this.setCurrentAction = this.setCurrentAction.bind(this);
this.tabShown = this.tabShown.bind(this);
this.showTab = this.showTab.bind(this);
MergeRequestTabs.prototype.fixedLayoutPref = null;
function MergeRequestTabs(opts) {
this.opts = opts != null ? opts : {};
this.opts.setUrl = this.opts.setUrl !== undefined ? this.opts.setUrl : true;
this.buildsLoaded = this.opts.buildsLoaded || false;
this.setCurrentAction = bind(this.setCurrentAction, this);
this.tabShown = bind(this.tabShown, this);
this.showTab = bind(this.showTab, this);
// Store the `location` object, allowing for easier stubbing in tests // Store the `location` object, allowing for easier stubbing in tests
this._location = location; this._location = window.location;
this.bindEvents(); this.bindEvents();
this.activateTab(this.opts.action); this.activateTab(action);
this.initAffix(); this.initAffix();
} }
MergeRequestTabs.prototype.bindEvents = function() { bindEvents() {
$(document) $(document)
.on('shown.bs.tab', '.merge-request-tabs a[data-toggle="tab"]', this.tabShown) .on('shown.bs.tab', '.merge-request-tabs a[data-toggle="tab"]', this.tabShown)
.on('click', '.js-show-tab', this.showTab); .on('click', '.js-show-tab', this.showTab);
} }
MergeRequestTabs.prototype.unbindEvents = function() { unbindEvents() {
$(document) $(document)
.off('shown.bs.tab', '.merge-request-tabs a[data-toggle="tab"]', this.tabShown) .off('shown.bs.tab', '.merge-request-tabs a[data-toggle="tab"]', this.tabShown)
.off('click', '.js-show-tab', this.showTab); .off('click', '.js-show-tab', this.showTab);
} }
MergeRequestTabs.prototype.showTab = function(event) { showTab(event) {
event.preventDefault(); event.preventDefault();
return this.activateTab($(event.target).data('action')); this.activateTab($(event.target).data('action'));
}; }
MergeRequestTabs.prototype.tabShown = function(event) { tabShown(event) {
var $target, action, navBarHeight; var $target, action, navBarHeight;
$target = $(event.target); $target = $(event.target);
action = $target.data('action'); action = $target.data('action');
...@@ -124,12 +119,12 @@ ...@@ -124,12 +119,12 @@
this.expandView(); this.expandView();
this.resetViewContainer(); this.resetViewContainer();
} }
if (this.opts.setUrl) { if (this.setUrl) {
this.setCurrentAction(action); this.setCurrentAction(action);
} }
}; }
MergeRequestTabs.prototype.scrollToElement = function(container) { scrollToElement(container) {
var $el, navBarHeight; var $el, navBarHeight;
if (window.location.hash) { if (window.location.hash) {
navBarHeight = $('.navbar-gitlab').outerHeight() + $('.layout-nav').outerHeight() + document.querySelector('.js-tabs-affix').offsetHeight; navBarHeight = $('.navbar-gitlab').outerHeight() + $('.layout-nav').outerHeight() + document.querySelector('.js-tabs-affix').offsetHeight;
...@@ -140,16 +135,16 @@ ...@@ -140,16 +135,16 @@
}); });
} }
} }
}; }
// Activate a tab based on the current action // Activate a tab based on the current action
MergeRequestTabs.prototype.activateTab = function(action) { activateTab(action) {
if (action === 'show') { if (action === 'show') {
action = 'notes'; action = 'notes';
} }
// important note: the .tab('show') method triggers 'shown.bs.tab' event itself // 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='" + action + "']").tab('show');
}; }
// Replaces the current Merge Request-specific action in the URL with a new one // Replaces the current Merge Request-specific action in the URL with a new one
// //
...@@ -171,7 +166,7 @@ ...@@ -171,7 +166,7 @@
// location.pathname # => "/namespace/project/merge_requests/1/commits" // location.pathname # => "/namespace/project/merge_requests/1/commits"
// //
// Returns the new URL String // Returns the new URL String
MergeRequestTabs.prototype.setCurrentAction = function(action) { setCurrentAction(action) {
var new_state; var new_state;
// Normalize action, just to be safe // Normalize action, just to be safe
if (action === 'show') { if (action === 'show') {
...@@ -187,35 +182,37 @@ ...@@ -187,35 +182,37 @@
} }
// Ensure parameters and hash come along for the ride // Ensure parameters and hash come along for the ride
new_state += this._location.search + this._location.hash; new_state += this._location.search + this._location.hash;
history.replaceState({
turbolinks: true,
url: new_state
// Replace the current history state with the new one without breaking // Replace the current history state with the new one without breaking
// Turbolinks' history. // Turbolinks' history.
// //
// See https://github.com/rails/turbolinks/issues/363 // See https://github.com/rails/turbolinks/issues/363
history.replaceState({
turbolinks: true,
url: new_state
}, document.title, new_state); }, document.title, new_state);
return new_state; return new_state;
}; }
MergeRequestTabs.prototype.loadCommits = function(source) { loadCommits(source) {
if (this.commitsLoaded) { if (this.commitsLoaded) {
return; return;
} }
return this._get({ this.ajaxGet({
url: source + ".json", url: source + ".json",
success: (function(_this) { success: (function(_this) {
return function(data) { return function(data) {
document.querySelector("div#commits").innerHTML = data.html; document.querySelector("div#commits").innerHTML = data.html;
gl.utils.localTimeAgo($('.js-timeago', 'div#commits')); gl.utils.localTimeAgo($('.js-timeago', 'div#commits'));
_this.commitsLoaded = true; _this.commitsLoaded = true;
return _this.scrollToElement("#commits"); _this.scrollToElement("#commits");
}; };
})(this) })(this)
}); });
}; }
MergeRequestTabs.prototype.loadDiff = function(source) { loadDiff(source) {
if (this.diffsLoaded) { if (this.diffsLoaded) {
return; return;
} }
...@@ -225,7 +222,7 @@ ...@@ -225,7 +222,7 @@
var url = document.createElement('a'); var url = document.createElement('a');
url.href = source; url.href = source;
return this._get({ this.ajaxGet({
url: (url.pathname + ".json") + this._location.search, url: (url.pathname + ".json") + this._location.search,
success: (function(_this) { success: (function(_this) {
return function(data) { return function(data) {
...@@ -248,13 +245,13 @@ ...@@ -248,13 +245,13 @@
}; };
})(this) })(this)
}); });
}; }
MergeRequestTabs.prototype.loadBuilds = function(source) { loadBuilds(source) {
if (this.buildsLoaded) { if (this.buildsLoaded) {
return; return;
} }
return this._get({ this.ajaxGet({
url: source + ".json", url: source + ".json",
success: (function(_this) { success: (function(_this) {
return function(data) { return function(data) {
...@@ -262,106 +259,107 @@ ...@@ -262,106 +259,107 @@
gl.utils.localTimeAgo($('.js-timeago', 'div#builds')); gl.utils.localTimeAgo($('.js-timeago', 'div#builds'));
_this.buildsLoaded = true; _this.buildsLoaded = true;
if (!this.pipelines) this.pipelines = new gl.Pipelines(); if (!this.pipelines) this.pipelines = new gl.Pipelines();
return _this.scrollToElement("#builds"); _this.scrollToElement("#builds");
}; };
})(this) })(this)
}); });
}; }
MergeRequestTabs.prototype.loadPipelines = function(source) { loadPipelines(source) {
if (this.pipelinesLoaded) { if (this.pipelinesLoaded) {
return; return;
} }
return this._get({ this.ajaxGet({
url: source + ".json", url: source + ".json",
success: function(data) { success: function(data) {
$('#pipelines').html(data.html); $('#pipelines').html(data.html);
gl.utils.localTimeAgo($('.js-timeago', '#pipelines')); gl.utils.localTimeAgo($('.js-timeago', '#pipelines'));
this.pipelinesLoaded = true; this.pipelinesLoaded = true;
return this.scrollToElement("#pipelines"); this.scrollToElement("#pipelines");
}.bind(this) }.bind(this)
}); });
}; }
// Show or hide the loading spinner // Show or hide the loading spinner
// //
// status - Boolean, true to show, false to hide // status - Boolean, true to show, false to hide
MergeRequestTabs.prototype.toggleLoading = function(status) { toggleLoading(status) {
return $('.mr-loading-status .loading').toggle(status); $('.mr-loading-status .loading').toggle(status);
}; }
MergeRequestTabs.prototype._get = function(options) { ajaxGet(options) {
var defaults; var defaults = {
defaults = {
beforeSend: (function(_this) { beforeSend: (function(_this) {
return function() { return function() {
return _this.toggleLoading(true); _this.toggleLoading(true);
}; };
})(this), })(this),
complete: (function(_this) { complete: (function(_this) {
return function() { return function() {
return _this.toggleLoading(false); _this.toggleLoading(false);
}; };
})(this), })(this),
dataType: 'json', dataType: 'json',
type: 'GET' type: 'GET'
}; };
options = $.extend({}, defaults, options); options = $.extend({}, defaults, options);
return $.ajax(options); $.ajax(options);
}; }
MergeRequestTabs.prototype.diffViewType = function() { diffViewType() {
return $('.inline-parallel-buttons a.active').data('view-type'); return $('.inline-parallel-buttons a.active').data('view-type');
}; }
MergeRequestTabs.prototype.isDiffAction = function(action) { isDiffAction(action) {
return action === 'diffs' || action === 'new/diffs' return action === 'diffs' || action === 'new/diffs'
}; }
MergeRequestTabs.prototype.expandViewContainer = function() { expandViewContainer() {
var $wrapper = $('.content-wrapper .container-fluid'); var $wrapper = $('.content-wrapper .container-fluid');
if (this.fixedLayoutPref === null) { if (this.fixedLayoutPref === null) {
this.fixedLayoutPref = $wrapper.hasClass('container-limited'); this.fixedLayoutPref = $wrapper.hasClass('container-limited');
} }
$wrapper.removeClass('container-limited'); $wrapper.removeClass('container-limited');
}; }
MergeRequestTabs.prototype.resetViewContainer = function() { resetViewContainer() {
if (this.fixedLayoutPref !== null) { if (this.fixedLayoutPref !== null) {
$('.content-wrapper .container-fluid') $('.content-wrapper .container-fluid')
.toggleClass('container-limited', this.fixedLayoutPref); .toggleClass('container-limited', this.fixedLayoutPref);
} }
}; }
MergeRequestTabs.prototype.shrinkView = function() { shrinkView() {
var $gutterIcon; var $gutterIcon;
$gutterIcon = $('.js-sidebar-toggle i:visible'); $gutterIcon = $('.js-sidebar-toggle i:visible');
return setTimeout(function() {
if ($gutterIcon.is('.fa-angle-double-right')) {
return $gutterIcon.closest('a').trigger('click', [true]);
}
// Wait until listeners are set // Wait until listeners are set
setTimeout(function() {
// Only when sidebar is expanded // Only when sidebar is expanded
if ($gutterIcon.is('.fa-angle-double-right')) {
$gutterIcon.closest('a').trigger('click', [true]);
}
}, 0); }, 0);
}; }
MergeRequestTabs.prototype.expandView = function() { // Expand the issuable sidebar unless the user explicitly collapsed it
expandView() {
var $gutterIcon; var $gutterIcon;
if (Cookies.get('collapsed_gutter') === 'true') { if (Cookies.get('collapsed_gutter') === 'true') {
return; return;
} }
$gutterIcon = $('.js-sidebar-toggle i:visible'); $gutterIcon = $('.js-sidebar-toggle i:visible');
return setTimeout(function() {
// Wait until listeners are set
setTimeout(function() {
// Only when sidebar is collapsed
if ($gutterIcon.is('.fa-angle-double-left')) { if ($gutterIcon.is('.fa-angle-double-left')) {
return $gutterIcon.closest('a').trigger('click', [true]); $gutterIcon.closest('a').trigger('click', [true]);
} }
}, 0); }, 0);
// Expand the issuable sidebar unless the user explicitly collapsed it }
// Wait until listeners are set
// Only when sidebar is collapsed
};
MergeRequestTabs.prototype.initAffix = function () { initAffix() {
var $tabs = $('.js-tabs-affix'); var $tabs = $('.js-tabs-affix');
// Screen space on small screens is usually very sparse // Screen space on small screens is usually very sparse
...@@ -396,10 +394,9 @@ ...@@ -396,10 +394,9 @@
if ($tabs.hasClass('affix')) { if ($tabs.hasClass('affix')) {
$tabs.trigger('affix.bs.affix'); $tabs.trigger('affix.bs.affix');
} }
}; }
}
return MergeRequestTabs;
})(); global.MergeRequestTabs = MergeRequestTabs;
}).call(this); })(window.gl || (window.gl = {}));
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
}; };
fixture.preload('merge_request_tabs.html'); fixture.preload('merge_request_tabs.html');
beforeEach(function() { beforeEach(function() {
this["class"] = new MergeRequestTabs(); this["class"] = new gl.MergeRequestTabs();
return this.spies = { return this.spies = {
ajax: spyOn($, 'ajax').and.callFake(function() {}), ajax: spyOn($, 'ajax').and.callFake(function() {}),
history: spyOn(history, 'replaceState').and.callFake(function() {}) history: spyOn(history, 'replaceState').and.callFake(function() {})
......
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