2017-05-30 04:43:13 -04:00
|
|
|
import _ from 'underscore';
|
2017-12-11 07:28:11 -05:00
|
|
|
import { visitUrl } from './lib/utils/url_utility';
|
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-09-06 12:14:34 -04:00
|
|
|
import { setCiStatusFavicon } from './lib/utils/common_utils';
|
2017-12-07 06:09:17 -05:00
|
|
|
import { timeFor } from './lib/utils/datetime_utility';
|
2017-04-18 08:27:10 -04:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
export default class Job {
|
|
|
|
constructor(options) {
|
|
|
|
this.timeout = null;
|
|
|
|
this.state = null;
|
2017-04-07 07:13:23 -04:00
|
|
|
this.options = options || $('.js-build-options').data();
|
|
|
|
|
2017-12-04 21:18:45 -05:00
|
|
|
this.pagePath = this.options.pagePath;
|
2017-04-07 07:13:23 -04:00
|
|
|
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-11-09 09:47:05 -05:00
|
|
|
this.$window = $(window);
|
2017-04-18 08:27:10 -04:00
|
|
|
this.logBytes = 0;
|
2017-05-30 04:43:13 -04:00
|
|
|
this.updateDropdown = this.updateDropdown.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
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
clearTimeout(this.timeout);
|
2017-03-11 02:30:44 -05:00
|
|
|
|
|
|
|
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-11-09 09:47:05 -05:00
|
|
|
this.$window
|
2017-06-09 07:58:52 -04:00
|
|
|
.off('scroll')
|
|
|
|
.on('scroll', () => {
|
2017-11-09 09:47:05 -05:00
|
|
|
if (!this.isScrolledToBottom()) {
|
2017-07-05 07:32:22 -04:00
|
|
|
this.toggleScrollAnimation(false);
|
2017-11-09 09:47:05 -05:00
|
|
|
} else if (this.isScrolledToBottom() && !this.isLogComplete) {
|
|
|
|
this.toggleScrollAnimation(true);
|
2017-07-05 07:32:22 -04:00
|
|
|
}
|
|
|
|
this.scrollThrottled();
|
2017-06-09 07:58:52 -04:00
|
|
|
});
|
|
|
|
|
2017-11-09 09:47:05 -05:00
|
|
|
this.$window
|
2017-04-07 07:13:23 -04:00
|
|
|
.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-10-10 07:06:42 -04:00
|
|
|
initAffixTopArea() {
|
2017-07-05 07:32:22 -04:00
|
|
|
/**
|
|
|
|
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-10-10 07:06:42 -04:00
|
|
|
}
|
2017-07-05 07:32:22 -04:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
canScroll() {
|
2017-11-09 09:47:05 -05:00
|
|
|
return this.$document.height() > this.$window.height();
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
2017-05-30 04:43:13 -04:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
toggleScroll() {
|
2017-11-09 09:47:05 -05:00
|
|
|
const currentPosition = this.$document.scrollTop();
|
|
|
|
const scrollHeight = this.$document.height();
|
2017-05-30 04:43:13 -04:00
|
|
|
|
2017-11-09 09:47:05 -05:00
|
|
|
const windowHeight = this.$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) {
|
2017-10-10 07:06:42 -04:00
|
|
|
// User is at Top of Log
|
2017-07-05 07:32:22 -04:00
|
|
|
|
2017-05-30 04:43:13 -04:00
|
|
|
this.toggleDisableButton(this.$scrollTopBtn, true);
|
|
|
|
this.toggleDisableButton(this.$scrollBottomBtn, false);
|
2017-11-09 09:47:05 -05:00
|
|
|
} else if (this.isScrolledToBottom()) {
|
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-10-10 07:06:42 -04:00
|
|
|
}
|
2017-05-30 04:43:13 -04:00
|
|
|
|
2017-11-09 09:47:05 -05:00
|
|
|
isScrolledToBottom() {
|
|
|
|
const currentPosition = this.$document.scrollTop();
|
|
|
|
const scrollHeight = this.$document.height();
|
|
|
|
|
|
|
|
const windowHeight = this.$window.height();
|
|
|
|
return scrollHeight - currentPosition === windowHeight;
|
|
|
|
}
|
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
scrollDown() {
|
2017-11-09 09:47:05 -05:00
|
|
|
this.$document.scrollTop(this.$document.height());
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
2017-07-05 07:32:22 -04:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
scrollToBottom() {
|
2017-07-05 07:32:22 -04:00
|
|
|
this.scrollDown();
|
2017-06-09 07:58:52 -04:00
|
|
|
this.hasBeenScrolled = true;
|
2017-05-30 04:43:13 -04:00
|
|
|
this.toggleScroll();
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
2017-05-30 04:43:13 -04:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
scrollToTop() {
|
2017-11-09 09:47:05 -05:00
|
|
|
this.$document.scrollTop(0);
|
2017-06-09 07:58:52 -04:00
|
|
|
this.hasBeenScrolled = true;
|
2017-05-30 04:43:13 -04:00
|
|
|
this.toggleScroll();
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
2017-05-30 04:43:13 -04:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
toggleDisableButton($button, disable) {
|
2017-05-30 04:43:13 -04:00
|
|
|
if (disable && $button.prop('disabled')) return;
|
|
|
|
$button.prop('disabled', disable);
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
2017-05-30 04:43:13 -04:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
toggleScrollAnimation(toggle) {
|
2017-05-30 04:43:13 -04:00
|
|
|
this.$scrollBottomBtn.toggleClass('animate', toggle);
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
2017-05-30 04:43:13 -04:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
initSidebar() {
|
2017-03-11 02:30:44 -05:00
|
|
|
this.$sidebar = $('.js-build-sidebar');
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
getBuildTrace() {
|
2017-03-11 02:30:44 -05:00
|
|
|
return $.ajax({
|
2017-12-04 21:18:45 -05:00
|
|
|
url: `${this.pagePath}/trace.json`,
|
2017-09-28 12:24:24 -04:00
|
|
|
data: { state: this.state },
|
2017-05-30 04:43:13 -04:00
|
|
|
})
|
|
|
|
.done((log) => {
|
2017-12-04 21:18:45 -05:00
|
|
|
setCiStatusFavicon(`${this.pagePath}/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-11-09 09:47:05 -05:00
|
|
|
this.isScrollInBottom = this.isScrolledToBottom();
|
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
|
|
|
}
|
2017-11-09 09:47:05 -05:00
|
|
|
this.isLogComplete = log.complete;
|
2017-04-07 07:13:23 -04:00
|
|
|
|
|
|
|
if (!log.complete) {
|
2017-10-10 07:06:42 -04:00
|
|
|
this.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-12-11 07:28:11 -05:00
|
|
|
visitUrl(this.pagePath);
|
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(() => {
|
2017-11-09 09:47:05 -05:00
|
|
|
if (this.isScrollInBottom) {
|
2017-07-05 07:32:22 -04:00
|
|
|
this.scrollDown();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(() => this.toggleScroll());
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
shouldHideSidebarForViewport() {
|
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-10-10 07:06:42 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
toggleSidebar(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-10-10 07:06:42 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
sidebarOnResize() {
|
2017-03-11 02:30:44 -05:00
|
|
|
this.toggleSidebar(this.shouldHideSidebarForViewport());
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
sidebarOnClick() {
|
2017-03-11 02:30:44 -05:00
|
|
|
if (this.shouldHideSidebarForViewport()) this.toggleSidebar();
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
|
|
|
// eslint-disable-next-line class-methods-use-this, consistent-return
|
|
|
|
updateArtifactRemoveDate() {
|
2017-04-07 07:13:23 -04:00
|
|
|
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(
|
2017-12-07 06:09:17 -05:00
|
|
|
timeFor(new Date(date.replace(/([0-9]+)-([0-9]+)-([0-9]+)/g, '$1/$2/$3'))),
|
2017-04-07 07:13:23 -04:00
|
|
|
);
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
populateJobs(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-10-10 07:06:42 -04:00
|
|
|
}
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
updateStageDropdownText(stage) {
|
2017-03-11 02:30:44 -05:00
|
|
|
$('.stage-selection').text(stage);
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
updateDropdown(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);
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
|
|
|
}
|