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
|
((w) => {
class ResolveServiceClass {
constructor() {
this.noteResource = Vue.resource('notes{/noteId}/resolve');
this.discussionResource = Vue.resource('merge_requests{/mergeRequestId}/discussions{/discussionId}/resolve');
}
setCSRF() {
Vue.http.headers.common['X-CSRF-Token'] = $.rails.csrfToken();
}
resolve(namespace, noteId) {
this.setCSRF();
if (Vue.http.options.root !== `/${namespace}`) {
Vue.http.options.root = `/${namespace}`;
}
return this.noteResource.save({ noteId }, {});
}
unresolve(namespace, noteId) {
this.setCSRF();
if (Vue.http.options.root !== `/${namespace}`) {
Vue.http.options.root = `/${namespace}`;
}
return this.noteResource.delete({ noteId }, {});
}
toggleResolveForDiscussion(namespace, mergeRequestId, discussionId) {
const isResolved = CommentsStore.state[discussionId].isResolved();
if (isResolved) {
return this.unResolveAll(namespace, mergeRequestId, discussionId);
} else {
return this.resolveAll(namespace, mergeRequestId, discussionId);
}
}
resolveAll(namespace, mergeRequestId, discussionId) {
const discussion = CommentsStore.state[discussionId];
this.setCSRF();
if (Vue.http.options.root !== `/${namespace}`) {
Vue.http.options.root = `/${namespace}`;
}
discussion.loading = true;
return this.discussionResource.save({
mergeRequestId,
discussionId
}, {}).then((response) => {
if (response.status === 200) {
const data = response.json();
const resolved_by = data ? data.resolved_by : null;
discussion.resolveAllNotes(resolved_by);
discussion.loading = false;
this.updateDiscussionHeadline(discussionId, data);
} else {
new Flash('An error occurred when trying to resolve a discussion. Please try again.', 'alert');
}
});
}
unResolveAll(namespace, mergeRequestId, discussionId) {
const discussion = CommentsStore.state[discussionId];
this.setCSRF();
Vue.http.options.root = `/${namespace}`;
discussion.loading = true;
return this.discussionResource.delete({
mergeRequestId,
discussionId
}, {}).then((response) => {
if (response.status === 200) {
const data = response.json();
discussion.unResolveAllNotes();
discussion.loading = false;
this.updateDiscussionHeadline(discussionId, data);
} else {
new Flash('An error occurred when trying to unresolve a discussion. Please try again.', 'alert');
}
});
}
updateDiscussionHeadline(discussionId, data) {
const $discussionHeadline = $(`.discussion[data-discussion-id="${discussionId}"] .js-discussion-headline`);
if (data.discussion_headline_html) {
if ($discussionHeadline.length) {
$discussionHeadline.replaceWith(data.discussion_headline_html);
} else {
$(`.discussion[data-discussion-id="${discussionId}"] .discussion-header`).append(data.discussion_headline_html);
}
} else {
$discussionHeadline.remove();
}
}
}
w.ResolveService = new ResolveServiceClass();
})(window);
|