2017-04-07 07:13:23 -04:00
|
|
|
/* eslint-disable func-names, wrap-iife, no-use-before-define,
|
|
|
|
consistent-return, prefer-rest-params */
|
2017-05-30 04:43:13 -04:00
|
|
|
import _ from 'underscore';
|
2017-08-10 09:02:30 -04:00
|
|
|
import bp from './breakpoints';
|
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.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-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);
|
2017-04-07 07:13:23 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
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');
|
2017-07-05 07:32:22 -04:00
|
|
|
this.$topBar = $('.js-top-bar');
|
2017-05-30 04:43:13 -04:00
|
|
|
|
|
|
|
// Scroll controllers
|
|
|
|
this.$scrollTopBtn = $('.js-scroll-up');
|
|
|
|
this.$scrollBottomBtn = $('.js-scroll-down');
|
2017-03-11 02:30:44 -05:00
|
|
|
|
|
|
|
clearTimeout(Build.timeout);
|
|
|
|
|
|
|
|
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-07-05 07:32:22 -04:00
|
|
|
this.scrollThrottled = _.throttle(this.toggleScroll.bind(this), 100);
|
2017-06-09 07:58:52 -04:00
|
|
|
|
2017-07-05 07:32:22 -04:00
|
|
|
$(window)
|
2017-06-09 07:58:52 -04:00
|
|
|
.off('scroll')
|
|
|
|
.on('scroll', () => {
|
2017-07-31 14:57:37 -04:00
|
|
|
const contentHeight = this.$buildTraceOutput.height();
|
2017-07-05 07:32:22 -04:00
|
|
|
if (contentHeight > this.windowSize) {
|
|
|
|
// means the user did not scroll, the content was updated.
|
|
|
|
this.windowSize = contentHeight;
|
|
|
|
} else {
|
|
|
|
// User scrolled
|
|
|
|
this.hasBeenScrolled = true;
|
|
|
|
this.toggleScrollAnimation(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.scrollThrottled();
|
2017-06-09 07:58:52 -04:00
|
|
|
});
|
|
|
|
|
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-07-05 07:32:22 -04:00
|
|
|
this.initAffixTopArea();
|
2017-05-30 04:43:13 -04:00
|
|
|
|
2017-07-05 07:32:22 -04:00
|
|
|
this.getBuildTrace();
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 07:32:22 -04:00
|
|
|
Build.prototype.initAffixTopArea = function () {
|
|
|
|
/**
|
|
|
|
If the browser does not support position sticky, it returns the position as static.
|
|
|
|
If the browser does support sticky, then we allow the browser to handle it, if not
|
|
|
|
then we default back to Bootstraps affix
|
|
|
|
**/
|
|
|
|
if (this.$topBar.css('position') !== 'static') return;
|
|
|
|
|
|
|
|
const offsetTop = this.$buildTrace.offset().top;
|
|
|
|
|
|
|
|
this.$topBar.affix({
|
|
|
|
offset: {
|
|
|
|
top: offsetTop,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-05-30 04:43:13 -04:00
|
|
|
Build.prototype.canScroll = function () {
|
2017-07-31 14:57:37 -04:00
|
|
|
return $(document).height() > $(window).height();
|
2017-05-30 04:43:13 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Build.prototype.toggleScroll = function () {
|
2017-07-31 14:57:37 -04:00
|
|
|
const currentPosition = $(document).scrollTop();
|
|
|
|
const scrollHeight = $(document).height();
|
2017-05-30 04:43:13 -04:00
|
|
|
|
2017-07-31 14:57:37 -04:00
|
|
|
const windowHeight = $(window).height();
|
2017-05-30 04:43:13 -04:00
|
|
|
if (this.canScroll()) {
|
2017-07-05 07:32:22 -04:00
|
|
|
if (currentPosition > 0 &&
|
2017-07-31 14:57:37 -04:00
|
|
|
(scrollHeight - currentPosition !== windowHeight)) {
|
2017-07-05 07:32:22 -04:00
|
|
|
// User is in the middle of the log
|
|
|
|
|
|
|
|
this.toggleDisableButton(this.$scrollTopBtn, false);
|
|
|
|
this.toggleDisableButton(this.$scrollBottomBtn, false);
|
|
|
|
} else if (currentPosition === 0) {
|
|
|
|
// User is at Top of Build Log
|
|
|
|
|
2017-05-30 04:43:13 -04:00
|
|
|
this.toggleDisableButton(this.$scrollTopBtn, true);
|
|
|
|
this.toggleDisableButton(this.$scrollBottomBtn, false);
|
2017-07-31 14:57:37 -04:00
|
|
|
} else if (scrollHeight - currentPosition === windowHeight) {
|
2017-07-05 07:32:22 -04:00
|
|
|
// User is at the bottom of the build log.
|
|
|
|
|
2017-05-30 04:43:13 -04:00
|
|
|
this.toggleDisableButton(this.$scrollTopBtn, false);
|
|
|
|
this.toggleDisableButton(this.$scrollBottomBtn, true);
|
|
|
|
}
|
2017-07-05 07:32:22 -04:00
|
|
|
} else {
|
|
|
|
this.toggleDisableButton(this.$scrollTopBtn, true);
|
|
|
|
this.toggleDisableButton(this.$scrollBottomBtn, true);
|
2017-05-30 04:43:13 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-05 07:32:22 -04:00
|
|
|
Build.prototype.scrollDown = function () {
|
2017-07-31 14:57:37 -04:00
|
|
|
$(document).scrollTop($(document).height());
|
2017-07-05 07:32:22 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Build.prototype.scrollToBottom = function () {
|
|
|
|
this.scrollDown();
|
2017-06-09 07:58:52 -04:00
|
|
|
this.hasBeenScrolled = true;
|
2017-05-30 04:43:13 -04:00
|
|
|
this.toggleScroll();
|
|
|
|
};
|
|
|
|
|
2017-07-05 07:32:22 -04:00
|
|
|
Build.prototype.scrollToTop = function () {
|
2017-07-31 14:57:37 -04:00
|
|
|
$(document).scrollTop(0);
|
2017-06-09 07:58:52 -04:00
|
|
|
this.hasBeenScrolled = true;
|
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);
|
|
|
|
};
|
|
|
|
|
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');
|
|
|
|
};
|
|
|
|
|
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-06-29 07:08:54 -04:00
|
|
|
|
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
|
|
|
|
2017-07-31 14:57:37 -04:00
|
|
|
this.windowSize = this.$buildTraceOutput.height();
|
2017-07-05 07:32:22 -04: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-06-29 07:08:54 -04:00
|
|
|
if (!this.hasBeenScrolled) {
|
|
|
|
this.toggleScrollAnimation(true);
|
|
|
|
} else {
|
|
|
|
this.toggleScrollAnimation(false);
|
|
|
|
}
|
2017-05-30 04:43:13 -04:00
|
|
|
|
2017-04-07 07:13:23 -04:00
|
|
|
Build.timeout = setTimeout(() => {
|
2017-07-05 07:32:22 -04:00
|
|
|
this.getBuildTrace();
|
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-07-05 07:32:22 -04:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
if (!this.hasBeenScrolled) {
|
|
|
|
this.scrollDown();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(() => this.toggleScroll());
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
|
|
|
|
2017-04-07 07:13:23 -04:00
|
|
|
Build.prototype.shouldHideSidebarForViewport = function () {
|
2017-08-10 09:02:30 -04:00
|
|
|
const bootstrapBreakpoint = 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.$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
|
|
|
|
2017-07-05 07:32:22 -04:00
|
|
|
this.$topBar
|
2017-06-12 05:20:19 -04:00
|
|
|
.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-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-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;
|
|
|
|
})();
|