summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_pipelines_index/time_ago.js.es6
blob: 3598da115739f31c0e533dd91ba7ebac6b95ada4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* global Vue, gl */
/* eslint-disable no-param-reassign */

window.Vue = require('vue');
require('../lib/utils/datetime_utility');

((gl) => {
  gl.VueTimeAgo = Vue.extend({
    data() {
      return {
        currentTime: new Date(),
      };
    },
    props: ['pipeline', 'svgs'],
    computed: {
      timeAgo() {
        return gl.utils.getTimeago();
      },
      localTimeFinished() {
        return gl.utils.formatDate(this.pipeline.details.finished_at);
      },
      timeStopped() {
        const changeTime = this.currentTime;
        const options = {
          weekday: 'long',
          year: 'numeric',
          month: 'short',
          day: 'numeric',
        };
        options.timeZoneName = 'short';
        const finished = this.pipeline.details.finished_at;
        if (!finished && changeTime) return false;
        return ({ words: this.timeAgo.format(finished) });
      },
      duration() {
        const { duration } = this.pipeline.details;
        const date = new Date(duration * 1000);

        let hh = date.getUTCHours();
        let mm = date.getUTCMinutes();
        let ss = date.getSeconds();

        if (hh < 10) hh = `0${hh}`;
        if (mm < 10) mm = `0${mm}`;
        if (ss < 10) ss = `0${ss}`;

        if (duration !== null) return `${hh}:${mm}:${ss}`;
        return false;
      },
    },
    methods: {
      changeTime() {
        this.currentTime = new Date();
      },
    },
    template: `
      <td>
        <p class="duration" v-if='duration'>
          <span v-html='svgs.iconTimer'></span>
          {{duration}}
        </p>
        <p class="finished-at" v-if='timeStopped'>
          <i class="fa fa-calendar"></i>
          <time
            data-toggle="tooltip"
            data-placement="top"
            data-container="body"
            :data-original-title='localTimeFinished'
          >
            {{timeStopped.words}}
          </time>
        </p>
      </td>
    `,
  });
})(window.gl || (window.gl = {}));