aboutsummaryrefslogtreecommitdiffstats
path: root/web_src/js/components/ContextPopup.vue
blob: efaa7be89e126985ea34fe77fafbc8385494d4e8 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<template>
  <div>
    <div v-if="loading" class="ui active centered inline loader"/>
    <div v-if="!loading && issue !== null">
      <p><small>{{ issue.repository.full_name }} on {{ createdAt }}</small></p>
      <p><svg-icon :name="icon" :class="[color]" /> <strong>{{ issue.title }}</strong> #{{ issue.number }}</p>
      <p>{{ body }}</p>
      <div>
        <div
          v-for="label in labels"
          :key="label.name"
          class="ui label"
          :style="{ color: label.textColor, backgroundColor: label.color }"
        >
          {{ label.name }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {SvgIcon} from '../svg.js';

const {appSubUrl} = window.config;

// NOTE: see models/issue_label.go for similar implementation
const srgbToLinear = (color) => {
  color /= 255;
  if (color <= 0.04045) {
    return color / 12.92;
  }
  return ((color + 0.055) / 1.055) ** 2.4;
};
const luminance = (colorString) => {
  const r = srgbToLinear(parseInt(colorString.substring(0, 2), 16));
  const g = srgbToLinear(parseInt(colorString.substring(2, 4), 16));
  const b = srgbToLinear(parseInt(colorString.substring(4, 6), 16));
  return 0.2126 * r + 0.7152 * g + 0.0722 * b;
};
const luminanceThreshold = 0.179;

export default {
  name: 'ContextPopup',

  components: {
    SvgIcon,
  },

  data: () => ({
    loading: false,
    issue: null
  }),

  computed: {
    createdAt() {
      return new Date(this.issue.created_at).toLocaleDateString(undefined, {year: 'numeric', month: 'short', day: 'numeric'});
    },

    body() {
      const body = this.issue.body.replace(/\n+/g, ' ');
      if (body.length > 85) {
        return `${body.substring(0, 85)}…`;
      }
      return body;
    },

    icon() {
      if (this.issue.pull_request !== null) {
        if (this.issue.state === 'open') {
          return 'octicon-git-pull-request'; // Open PR
        } else if (this.issue.pull_request.merged === true) {
          return 'octicon-git-merge'; // Merged PR
        }
        return 'octicon-git-pull-request'; // Closed PR
      } else if (this.issue.state === 'open') {
        return 'octicon-issue-opened'; // Open Issue
      }
      return 'octicon-issue-closed'; // Closed Issue
    },

    color() {
      if (this.issue.state === 'open') {
        return 'green';
      } else if (this.issue.pull_request !== null && this.issue.pull_request.merged === true) {
        return 'purple';
      }
      return 'red';
    },

    labels() {
      return this.issue.labels.map((label) => {
        let textColor;
        if (luminance(label.color) < luminanceThreshold) {
          textColor = '#ffffff';
        } else {
          textColor = '#000000';
        }
        return {name: label.name, color: `#${label.color}`, textColor};
      });
    }
  },

  mounted() {
    this.$root.$on('load-context-popup', (data, callback) => {
      if (!this.loading && this.issue === null) {
        this.load(data, callback);
      }
    });
  },

  methods: {
    load(data, callback) {
      this.loading = true;
      $.get(`${appSubUrl}/api/v1/repos/${data.owner}/${data.repo}/issues/${data.index}`, (issue) => {
        this.issue = issue;
        this.loading = false;
        this.$nextTick(() => {
          if (callback) {
            callback();
          }
        });
      });
    }
  }
};
</script>