summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/issue_show/issue_title_description.vue
blob: dc3ba2550c5a5fe67875570c3fe16ee1e9147f4a (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<script>
import Visibility from 'visibilityjs';
import Poll from './../lib/utils/poll';
import Service from './services/index';
import tasks from './actions/tasks';

export default {
  props: {
    endpoint: {
      required: true,
      type: String,
    },
    canUpdateTasksClass: {
      required: true,
      type: String,
    },
  },
  data() {
    const resource = new Service(this.$http, this.endpoint);

    const poll = new Poll({
      resource,
      method: 'getTitle',
      successCallback: (res) => {
        this.renderResponse(res);
      },
      errorCallback: (err) => {
        throw new Error(err);
      },
    });

    return {
      poll,
      apiData: {},
      tasks: '0 of 0',
      title: null,
      titleText: '',
      titleFlag: {
        pre: true,
        pulse: false,
      },
      description: null,
      descriptionText: '',
      descriptionChange: false,
      descriptionFlag: {
        pre: true,
        pulse: false,
      },
      timeAgoEl: $('.issue_edited_ago'),
      titleEl: document.querySelector('title'),
    };
  },
  methods: {
    updateFlag(key, toggle) {
      this[key].pre = toggle;
      this[key].pulse = !toggle;
    },
    renderResponse(res) {
      this.apiData = res.json();
      this.triggerAnimation();
    },
    updateTaskHTML() {
      tasks(this.apiData, this.tasks);
    },
    elementsToVisualize(noTitleChange, noDescriptionChange) {
      if (!noTitleChange) {
        this.titleText = this.apiData.title_text;
        this.updateFlag('titleFlag', true);
      }

      if (!noDescriptionChange) {
        // only change to true when we need to bind TaskLists the html of description
        this.descriptionChange = true;
        this.updateTaskHTML();
        this.tasks = this.apiData.task_status;
        this.updateFlag('descriptionFlag', true);
      }
    },
    setTabTitle() {
      const currentTabTitleScope = this.titleEl.innerText.split('·');
      currentTabTitleScope[0] = `${this.titleText} (#${this.apiData.issue_number}) `;
      this.titleEl.innerText = currentTabTitleScope.join('·');
    },
    animate(title, description) {
      this.title = title;
      this.description = description;
      this.setTabTitle();

      this.$nextTick(() => {
        this.updateFlag('titleFlag', false);
        this.updateFlag('descriptionFlag', false);
      });
    },
    triggerAnimation() {
      // always reset to false before checking the change
      this.descriptionChange = false;

      const { title, description } = this.apiData;
      this.descriptionText = this.apiData.description_text;

      const noTitleChange = this.title === title;
      const noDescriptionChange = this.description === description;

      /**
      * since opacity is changed, even if there is no diff for Vue to update
      * we must check the title/description even on a 304 to ensure no visual change
      */
      if (noTitleChange && noDescriptionChange) return;

      this.elementsToVisualize(noTitleChange, noDescriptionChange);
      this.animate(title, description);
    },
    updateEditedTimeAgo() {
      const toolTipTime = gl.utils.formatDate(this.apiData.updated_at);
      this.timeAgoEl.attr('datetime', this.apiData.updated_at);
      this.timeAgoEl.attr('title', toolTipTime).tooltip('fixTitle');
    },
  },
  created() {
    if (!Visibility.hidden()) {
      this.poll.makeRequest();
    }

    Visibility.change(() => {
      if (!Visibility.hidden()) {
        this.poll.restart();
      } else {
        this.poll.stop();
      }
    });
  },
  updated() {
    // if new html is injected (description changed) - bind TaskList and call renderGFM
    if (this.descriptionChange) {
      this.updateEditedTimeAgo();

      $(this.$refs['issue-content-container-gfm-entry']).renderGFM();

      const tl = new gl.TaskList({
        dataType: 'issue',
        fieldName: 'description',
        selector: '.detail-page-description',
      });

      return tl && null;
    }

    return null;
  },
};
</script>

<template>
  <div>
    <h2
      class="title"
      :class="{ 'issue-realtime-pre-pulse': titleFlag.pre, 'issue-realtime-trigger-pulse': titleFlag.pulse }"
      ref="issue-title"
      v-html="title"
    >
    </h2>
    <div
      class="description is-task-list-enabled"
      :class="canUpdateTasksClass"
      v-if="description"
    >
      <div
        class="wiki"
        :class="{ 'issue-realtime-pre-pulse': descriptionFlag.pre, 'issue-realtime-trigger-pulse': descriptionFlag.pulse }"
        v-html="description"
        ref="issue-content-container-gfm-entry"
      >
      </div>
      <textarea
        class="hidden js-task-list-field"
        v-if="descriptionText"
      >{{descriptionText}}</textarea>
    </div>
  </div>
</template>