import Component from '@ember/component'; import { inject as service } from '@ember/service'; import { next } from '@ember/runloop'; import { set } from '@ember/object'; import ThrottledResize from 'shared/mixins/throttled-resize'; import { alternateLabel } from 'shared/utils/platform'; export default Component.extend(ThrottledResize, { instance: null, timeOutAnchor: null, alternateLabel: alternateLabel, showProtip: true, status: 'connecting', socket: null, pipeline: service(), logHeight: 300, globalStore: service(), onlyCombinedLog: true, which: 'combined', actions: { cancel: function() { this.disconnect(); this.sendAction('dismiss'); }, scrollToTop: function() { this.$('.log-body').animate({ scrollTop: '0px' }); }, scrollToBottom: function() { var body = this.$('.log-body'); body.stop().animate({ scrollTop: (body[0].scrollHeight + 1000) + 'px' }); }, }, showLogs: function() { var inst = this.get('instance'); if(!inst){ return } var key = inst.stageIndex + '-' + inst.stepIndex; next(() => { this.send('scrollToBottom'); }); if(!inst.activityLogs[key]){ this.observeInstance(); return } return inst.activityLogs[key]; }.property('instance.{stageIndex,stepIndex,activityLogs}', 'showLogsTrigger'), showLogsTrigger: '', observeInstance: function() { this.disconnect(); next(this, 'exec'); }.observes('instance.{stageIndex,stepIndex}'), didInsertElement: function() { this._super(); next(this, 'exec'); }, exec: function() { this.connect(); }, connect: function() { this.set('status', 'initializing'); var body = this.$('.log-body')[0]; var $body = $(body); // inst should be get from outside of the onmessage, cause inst may have changed when onmessage callback var inst = this.get('instance'); var onmessage = (message) => { inst = this.get('instance'); var isFollow = ($body.scrollTop() + $body.outerHeight() + 10) >= body.scrollHeight; // var logs = ''; // message.trim().split(/\n/).forEach((line) => { // var match = line.match(/^\[?([^ \]]+)\]?\s?/); // var dateStr, msg; // if (match) { // msg = line.substr(match[0].length); // var date = new Date(match[1] * 1); // dateStr = '' + Util.escapeHtml(date.toLocaleDateString()) + ' ' + Util.escapeHtml(date.toLocaleTimeString()) + ' '; // } else { // msg = line; // dateStr = 'Unknown Date'; // } // // body.insertAdjacentHTML('beforeend', // // '