2017-04-07 07:13:23 -04:00
|
|
|
/* eslint-disable func-names, wrap-iife, no-use-before-define,
|
|
|
|
consistent-return, prefer-rest-params */
|
2016-12-14 00:26:26 -05:00
|
|
|
/* global Breakpoints */
|
|
|
|
|
2017-05-30 04:43:13 -04:00
|
|
|
import _ from 'underscore';
|
2017-04-18 08:27:10 -04:00
|
|
|
import { bytesToKiB } from './lib/utils/number_utils';
|
|
|
|
|
2017-04-07 07:13:23 -04:00
|
|
|
window.Build = (function () {
|
2017-03-11 02:30:44 -05:00
|
|
|
Build.timeout = null;
|
|
|
|
Build.state = null;
|
|
|
|
|
|
|
|
function Build(options) {
|
2017-04-07 07:13:23 -04:00
|
|
|
this.options = options || $('.js-build-options').data();
|
|
|
|
|
|
|
|
this.pageUrl = this.options.pageUrl;
|
|
|
|
this.buildUrl = this.options.buildUrl;
|
|
|
|
this.buildStatus = this.options.buildStatus;
|
|
|
|
this.state = this.options.logState;
|
|
|
|
this.buildStage = this.options.buildStage;
|
2017-03-11 02:30:44 -05:00
|
|
|
this.$document = $(document);
|
2017-04-18 08:27:10 -04:00
|
|
|
this.logBytes = 0;
|
2017-05-30 04:43:13 -04:00
|
|
|
this.scrollOffsetPadding = 30;
|
2017-06-09 07:58:52 -04:00
|
|
|
this.hasBeenScrolled = false;
|
2017-04-07 07:13:23 -04:00
|
|
|
|
2017-05-30 04:43:13 -04:00
|
|
|
this.updateDropdown = this.updateDropdown.bind(this);
|
|
|
|
this.getBuildTrace = this.getBuildTrace.bind(this);
|
|
|
|
this.scrollToBottom = this.scrollToBottom.bind(this);
|
2017-04-07 07:13:23 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
this.$body = $('body');
|
|
|
|
this.$buildTrace = $('#build-trace');
|
|
|
|
this.$buildRefreshAnimation = $('.js-build-refresh');
|
2017-04-07 07:13:23 -04:00
|
|
|
this.$truncatedInfo = $('.js-truncated-info');
|
2017-05-30 04:43:13 -04:00
|
|
|
this.$buildTraceOutput = $('.js-build-output');
|
|
|
|
this.$scrollContainer = $('.js-scroll-container');
|
|
|
|
|
|
|
|
// Scroll controllers
|
|
|
|
this.$scrollTopBtn = $('.js-scroll-up');
|
|
|
|
this.$scrollBottomBtn = $('.js-scroll-down');
|
2017-03-11 02:30:44 -05:00
|
|
|
|
|
|
|
clearTimeout(Build.timeout);
|
|
|
|
// Init breakpoint checker
|
|
|
|
this.bp = Breakpoints.get();
|
|
|
|
|
|
|
|
this.initSidebar();
|
|
|
|
this.populateJobs(this.buildStage);
|
|
|
|
this.updateStageDropdownText(this.buildStage);
|
|
|
|
this.sidebarOnResize();
|
|
|
|
|
2017-04-07 07:13:23 -04:00
|
|
|
this.$document
|
|
|
|
.off('click', '.js-sidebar-build-toggle')
|
|
|
|
.on('click', '.js-sidebar-build-toggle', this.sidebarOnClick.bind(this));
|
|
|
|
|
|
|
|
this.$document
|
|
|
|
.off('click', '.stage-item')
|
|
|
|
.on('click', '.stage-item', this.updateDropdown);
|
|
|
|
|
2017-05-30 04:43:13 -04:00
|
|
|
// add event listeners to the scroll buttons
|
|
|
|
this.$scrollTopBtn
|
|
|
|
.off('click')
|
|
|
|
.on('click', this.scrollToTop.bind(this));
|
|
|
|
|
|
|
|
this.$scrollBottomBtn
|
|
|
|
.off('click')
|
|
|
|
.on('click', this.scrollToBottom.bind(this));
|
2017-04-07 07:13:23 -04:00
|
|
|
|
2017-06-09 07:58:52 -04:00
|
|
|
const scrollThrottled = _.throttle(this.toggleScroll.bind(this), 100);
|
|
|
|
|
|
|
|
this.$scrollContainer
|
|
|
|
.off('scroll')
|
|
|
|
.on('scroll', () => {
|
|
|
|
this.hasBeenScrolled = true;
|
|
|
|
scrollThrottled();
|
|
|
|
});
|
|
|
|
|
2017-04-07 07:13:23 -04:00
|
|
|
$(window)
|
|
|
|
.off('resize.build')
|
2017-06-02 12:16:54 -04:00
|
|
|
.on('resize.build', _.throttle(this.sidebarOnResize.bind(this), 100));
|
2017-04-07 07:13:23 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
this.updateArtifactRemoveDate();
|
2017-05-30 04:43:13 -04:00
|
|
|
|
|
|
|
// eslint-disable-next-line
|
|
|
|
this.getBuildTrace()
|
2017-06-09 07:58:52 -04:00
|
|
|
.then(() => this.toggleScroll())
|
|
|
|
.then(() => {
|
|
|
|
if (!this.hasBeenScrolled) {
|
|
|
|
this.scrollToBottom();
|
|
|
|
}
|
|
|
|
});
|
2017-05-30 04:43:13 -04:00
|
|
|
|
|
|
|
this.verifyTopPosition();
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
|
2017-05-30 04:43:13 -04:00
|
|
|
Build.prototype.canScroll = function () {
|
|
|
|
return (this.$scrollContainer.prop('scrollHeight') - this.scrollOffsetPadding) > this.$scrollContainer.height();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* | | Up | Down |
|
|
|
|
* |--------------------------|----------|----------|
|
|
|
|
* | on scroll bottom | active | disabled |
|
|
|
|
* | on scroll top | disabled | active |
|
|
|
|
* | no scroll | disabled | disabled |
|
|
|
|
* | on.('scroll') is on top | disabled | active |
|
|
|
|
* | on('scroll) is on bottom | active | disabled |
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
Build.prototype.toggleScroll = function () {
|
2017-06-09 07:58:52 -04:00
|
|
|
const currentPosition = this.$scrollContainer.scrollTop();
|
|
|
|
const bottomScroll = currentPosition + this.$scrollContainer.innerHeight();
|
2017-05-30 04:43:13 -04:00
|
|
|
|
|
|
|
if (this.canScroll()) {
|
2017-06-09 07:58:52 -04:00
|
|
|
if (currentPosition === 0) {
|
2017-05-30 04:43:13 -04:00
|
|
|
this.toggleDisableButton(this.$scrollTopBtn, true);
|
|
|
|
this.toggleDisableButton(this.$scrollBottomBtn, false);
|
|
|
|
} else if (bottomScroll === this.$scrollContainer.prop('scrollHeight')) {
|
|
|
|
this.toggleDisableButton(this.$scrollTopBtn, false);
|
|
|
|
this.toggleDisableButton(this.$scrollBottomBtn, true);
|
|
|
|
} else {
|
|
|
|
this.toggleDisableButton(this.$scrollTopBtn, false);
|
|
|
|
this.toggleDisableButton(this.$scrollBottomBtn, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Build.prototype.scrollToTop = function () {
|
2017-06-09 07:58:52 -04:00
|
|
|
this.hasBeenScrolled = true;
|
|
|
|
this.$scrollContainer.scrollTop(0);
|
2017-05-30 04:43:13 -04:00
|
|
|
this.toggleScroll();
|
|
|
|
};
|
|
|
|
|
|
|
|
Build.prototype.scrollToBottom = function () {
|
2017-06-09 07:58:52 -04:00
|
|
|
this.hasBeenScrolled = true;
|
|
|
|
this.$scrollContainer.scrollTop(this.$scrollContainer.prop('scrollHeight'));
|
2017-05-30 04:43:13 -04:00
|
|
|
this.toggleScroll();
|
|
|
|
};
|
|
|
|
|
|
|
|
Build.prototype.toggleDisableButton = function ($button, disable) {
|
|
|
|
if (disable && $button.prop('disabled')) return;
|
|
|
|
$button.prop('disabled', disable);
|
|
|
|
};
|
|
|
|
|
|
|
|
Build.prototype.toggleScrollAnimation = function (toggle) {
|
|
|
|
this.$scrollBottomBtn.toggleClass('animate', toggle);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build trace top position depends on the space ocupied by the elments rendered before
|
|
|
|
*/
|
|
|
|
Build.prototype.verifyTopPosition = function () {
|
|
|
|
const $buildPage = $('.build-page');
|
|
|
|
|
2017-06-12 05:20:19 -04:00
|
|
|
const $flashError = $('.alert-wrapper');
|
2017-05-30 04:43:13 -04:00
|
|
|
const $header = $('.build-header', $buildPage);
|
|
|
|
const $runnersStuck = $('.js-build-stuck', $buildPage);
|
|
|
|
const $startsEnvironment = $('.js-environment-container', $buildPage);
|
|
|
|
const $erased = $('.js-build-erased', $buildPage);
|
2017-06-12 05:20:19 -04:00
|
|
|
const prependTopDefault = 20;
|
2017-05-30 04:43:13 -04:00
|
|
|
|
2017-06-12 05:20:19 -04:00
|
|
|
// header + navigation + margin
|
2017-05-30 04:43:13 -04:00
|
|
|
let topPostion = 168;
|
|
|
|
|
2017-06-12 05:20:19 -04:00
|
|
|
if ($header.length) {
|
2017-05-30 04:43:13 -04:00
|
|
|
topPostion += $header.outerHeight();
|
|
|
|
}
|
|
|
|
|
2017-06-12 05:20:19 -04:00
|
|
|
if ($runnersStuck.length) {
|
2017-05-30 04:43:13 -04:00
|
|
|
topPostion += $runnersStuck.outerHeight();
|
|
|
|
}
|
|
|
|
|
2017-06-12 05:20:19 -04:00
|
|
|
if ($startsEnvironment.length) {
|
|
|
|
topPostion += $startsEnvironment.outerHeight() + prependTopDefault;
|
2017-05-30 04:43:13 -04:00
|
|
|
}
|
|
|
|
|
2017-06-12 05:20:19 -04:00
|
|
|
if ($erased.length) {
|
|
|
|
topPostion += $erased.outerHeight() + prependTopDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($flashError.length) {
|
|
|
|
topPostion += $flashError.outerHeight();
|
2017-05-30 04:43:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.$buildTrace.css({
|
|
|
|
top: topPostion,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-04-07 07:13:23 -04:00
|
|
|
Build.prototype.initSidebar = function () {
|
2017-03-11 02:30:44 -05:00
|
|
|
this.$sidebar = $('.js-build-sidebar');
|
|
|
|
this.$sidebar.niceScroll();
|
|
|
|
};
|
|
|
|
|
2017-04-07 07:13:23 -04:00
|
|
|
Build.prototype.getBuildTrace = function () {
|
2017-03-11 02:30:44 -05:00
|
|
|
return $.ajax({
|
2017-04-07 07:13:23 -04:00
|
|
|
url: `${this.pageUrl}/trace.json`,
|
2017-05-30 04:43:13 -04:00
|
|
|
data: this.state,
|
|
|
|
})
|
|
|
|
.done((log) => {
|
2017-04-07 09:22:24 -04:00
|
|
|
gl.utils.setCiStatusFavicon(`${this.pageUrl}/status.json`);
|
2017-04-07 07:13:23 -04:00
|
|
|
if (log.state) {
|
|
|
|
this.state = log.state;
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2017-04-07 07:13:23 -04:00
|
|
|
|
|
|
|
if (log.append) {
|
2017-05-30 04:43:13 -04:00
|
|
|
this.$buildTraceOutput.append(log.html);
|
2017-04-18 08:27:10 -04:00
|
|
|
this.logBytes += log.size;
|
2017-04-07 07:13:23 -04:00
|
|
|
} else {
|
2017-05-30 04:43:13 -04:00
|
|
|
this.$buildTraceOutput.html(log.html);
|
2017-04-18 08:27:10 -04:00
|
|
|
this.logBytes = log.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the incremental sum of logBytes we received is less than the total
|
|
|
|
// we need to show a message warning the user about that.
|
|
|
|
if (this.logBytes < log.total) {
|
|
|
|
// size is in bytes, we need to calculate KiB
|
|
|
|
const size = bytesToKiB(this.logBytes);
|
|
|
|
$('.js-truncated-info-size').html(`${size}`);
|
|
|
|
this.$truncatedInfo.removeClass('hidden');
|
|
|
|
} else {
|
|
|
|
this.$truncatedInfo.addClass('hidden');
|
2017-04-07 07:13:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!log.complete) {
|
2017-05-30 04:43:13 -04:00
|
|
|
this.toggleScrollAnimation(true);
|
|
|
|
|
2017-04-07 07:13:23 -04:00
|
|
|
Build.timeout = setTimeout(() => {
|
2017-05-30 04:43:13 -04:00
|
|
|
//eslint-disable-next-line
|
|
|
|
this.getBuildTrace()
|
2017-06-09 07:58:52 -04:00
|
|
|
.then(() => {
|
|
|
|
if (!this.hasBeenScrolled) {
|
|
|
|
this.scrollToBottom();
|
|
|
|
}
|
|
|
|
});
|
2017-04-07 07:13:23 -04:00
|
|
|
}, 4000);
|
|
|
|
} else {
|
2017-03-11 02:30:44 -05:00
|
|
|
this.$buildRefreshAnimation.remove();
|
2017-05-30 04:43:13 -04:00
|
|
|
this.toggleScrollAnimation(false);
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
|
2017-04-07 07:13:23 -04:00
|
|
|
if (log.status !== this.buildStatus) {
|
2017-05-30 04:43:13 -04:00
|
|
|
gl.utils.visitUrl(this.pageUrl);
|
2017-04-07 07:13:23 -04:00
|
|
|
}
|
2017-05-30 04:43:13 -04:00
|
|
|
})
|
|
|
|
.fail(() => {
|
2017-04-07 07:13:23 -04:00
|
|
|
this.$buildRefreshAnimation.remove();
|
2017-05-30 04:43:13 -04:00
|
|
|
});
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
|
|
|
|
2017-04-07 07:13:23 -04:00
|
|
|
Build.prototype.shouldHideSidebarForViewport = function () {
|
|
|
|
const bootstrapBreakpoint = this.bp.getBreakpointSize();
|
2017-03-11 02:30:44 -05:00
|
|
|
return bootstrapBreakpoint === 'xs' || bootstrapBreakpoint === 'sm';
|
|
|
|
};
|
|
|
|
|
2017-04-07 07:13:23 -04:00
|
|
|
Build.prototype.toggleSidebar = function (shouldHide) {
|
2017-06-08 08:21:03 -04:00
|
|
|
const shouldShow = typeof shouldHide === 'boolean' ? !shouldHide : undefined;
|
2017-06-12 05:20:19 -04:00
|
|
|
const $toggleButton = $('.js-sidebar-build-toggle-header');
|
2017-04-07 07:13:23 -04:00
|
|
|
|
2017-05-30 04:43:13 -04:00
|
|
|
this.$buildTrace
|
|
|
|
.toggleClass('sidebar-expanded', shouldShow)
|
2017-03-11 02:30:44 -05:00
|
|
|
.toggleClass('sidebar-collapsed', shouldHide);
|
2017-05-30 04:43:13 -04:00
|
|
|
this.$sidebar
|
|
|
|
.toggleClass('right-sidebar-expanded', shouldShow)
|
2017-03-11 02:30:44 -05:00
|
|
|
.toggleClass('right-sidebar-collapsed', shouldHide);
|
2017-06-12 05:20:19 -04:00
|
|
|
|
|
|
|
$('.js-build-page')
|
|
|
|
.toggleClass('sidebar-expanded', shouldShow)
|
|
|
|
.toggleClass('sidebar-collapsed', shouldHide);
|
|
|
|
|
|
|
|
if (this.$sidebar.hasClass('right-sidebar-expanded')) {
|
|
|
|
$toggleButton.addClass('hidden');
|
|
|
|
} else {
|
|
|
|
$toggleButton.removeClass('hidden');
|
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
|
|
|
|
2017-04-07 07:13:23 -04:00
|
|
|
Build.prototype.sidebarOnResize = function () {
|
2017-03-11 02:30:44 -05:00
|
|
|
this.toggleSidebar(this.shouldHideSidebarForViewport());
|
2017-06-02 12:16:54 -04:00
|
|
|
|
2017-05-30 04:43:13 -04:00
|
|
|
this.verifyTopPosition();
|
|
|
|
|
2017-06-09 07:58:52 -04:00
|
|
|
if (this.canScroll()) {
|
2017-05-30 04:43:13 -04:00
|
|
|
this.toggleScroll();
|
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
|
|
|
|
2017-04-07 07:13:23 -04:00
|
|
|
Build.prototype.sidebarOnClick = function () {
|
2017-03-11 02:30:44 -05:00
|
|
|
if (this.shouldHideSidebarForViewport()) this.toggleSidebar();
|
2017-06-12 05:20:19 -04:00
|
|
|
this.verifyTopPosition();
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
|
|
|
|
2017-04-07 07:13:23 -04:00
|
|
|
Build.prototype.updateArtifactRemoveDate = function () {
|
|
|
|
const $date = $('.js-artifacts-remove');
|
2017-03-11 02:30:44 -05:00
|
|
|
if ($date.length) {
|
2017-04-07 07:13:23 -04:00
|
|
|
const date = $date.text();
|
|
|
|
return $date.text(
|
|
|
|
gl.utils.timeFor(new Date(date.replace(/([0-9]+)-([0-9]+)-([0-9]+)/g, '$1/$2/$3')), ' '),
|
|
|
|
);
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-07 07:13:23 -04:00
|
|
|
Build.prototype.populateJobs = function (stage) {
|
2017-03-11 02:30:44 -05:00
|
|
|
$('.build-job').hide();
|
2017-04-07 07:13:23 -04:00
|
|
|
$(`.build-job[data-stage="${stage}"]`).show();
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
|
|
|
|
2017-04-07 07:13:23 -04:00
|
|
|
Build.prototype.updateStageDropdownText = function (stage) {
|
2017-03-11 02:30:44 -05:00
|
|
|
$('.stage-selection').text(stage);
|
|
|
|
};
|
|
|
|
|
2017-04-07 07:13:23 -04:00
|
|
|
Build.prototype.updateDropdown = function (e) {
|
2017-03-11 02:30:44 -05:00
|
|
|
e.preventDefault();
|
2017-04-07 07:13:23 -04:00
|
|
|
const stage = e.currentTarget.text;
|
2017-03-11 02:30:44 -05:00
|
|
|
this.updateStageDropdownText(stage);
|
|
|
|
this.populateJobs(stage);
|
|
|
|
};
|
|
|
|
|
|
|
|
return Build;
|
|
|
|
})();
|