forked from opendatateam/udata
/
threads.vue
261 lines (234 loc) · 7.74 KB
/
threads.vue
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<style scoped lang="less">
.sort {
margin-top: -32px;
margin-bottom: 1em;
text-align: right;
}
.loading {
margin: 2em;
text-align: center;
}
.discussion-card.add {
cursor: pointer;
.card-logo {
background-color: #eeeeee;
font-size: 2em;
display: flex;
justify-content: center;
align-items: center;
height: 60px;
}
.card-body {
min-height: auto;
justify-content: left;
h4 {
margin-bottom: 0;
}
}
}
.create {
& > div:first-child {
display: flex;
flex-direction: row;
& > div:nth-child(3) {
padding: 0 1em;
}
.control {
text-align: right;
width: 4em;
order: 3;
a:hover {
cursor: pointer;
}
}
}
}
</style>
<template>
<div class="discussion-threads">
<div class="loading" v-if="loading">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr-only">{{ _('Loading') }}...</span>
</div>
<div class="sort" v-show="discussions.length > 1">
<div class="btn-group">
<button class="btn btn-default btn-sm dropdown-toogle" type="button"
data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
{{ _('sort by') }} <span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-right">
<li><a class="by_created" @click="sortBy('created')">{{ _('topic creation') }}</a></li>
<li><a class="last_response" @click="sortBy('response')">{{ _('last response') }}</a></li>
</ul>
</div>
</div>
<discussion-thread
v-ref:threads
v-for="discussion in discussions"
:discussion="discussion"
id="discussion-{{ discussion.id }}"
track-by="id">
</discussion-thread>
<!-- New discussion -->
<a class="card discussion-card add" @click="displayForm" v-show="!formDisplayed">
<div class="card-logo"><span>+</span></div>
<div class="card-body">
<h4>{{ _('Start a new discussion') }}</h4>
</div>
</a>
<div v-el:form id="discussion-create" v-show="formDisplayed" v-if="currentUser"
class="create list-group-item animated">
<div>
<div class="avatar">
<avatar :user="currentUser"></avatar>
</div>
<div class="control">
<a href="#discussion-create"><span class="fa fa-link"></span></a>
<a @click="hideForm"><span class="fa fa-times"></span></a>
</div>
<div>
<h4 class="list-group-item-heading">
{{ _('Starting a new discussion thread') }}
</h4>
<p class="list-group-item-text">
{{ _("You're about to start a new discussion thread. Make sure that a thread about the same topic doesn't exist yet just above.") }}
</p>
</div>
</div>
<thread-form-create v-ref:form :subject-id="subjectId" :subject-class="subjectClass"></thread-form-create>
</div>
</div>
</template>
<script>
import config from 'config';
import Avatar from 'components/avatar.vue';
import DiscussionThread from 'components/discussions/thread.vue';
import ThreadFormCreate from 'components/discussions/thread-create.vue';
import log from 'logger';
const DISCUSSION_REGEX = /^#discussion-([0-9a-f]{24})$/;
const COMMENT_REGEX = /^#discussion-([0-9a-f]{24})-(\d+)$/;
const NEW_COMMENT_REGEX = /^#discussion-([0-9a-f]{24})-new-comment$/;
export default {
components: {Avatar, DiscussionThread, ThreadFormCreate},
data() {
return {
discussions: [],
loading: true,
formDisplayed: false,
currentUser: config.user,
}
},
props: {
subjectId: String,
subjectClass: String
},
events: {
/**
* Add the created discussion in the list and display it
* @type {Discussion} the newly created discussion
*/
'discussion:created': function(discussion) {
this.hideForm()
this.discussions.unshift(discussion);
this.$nextTick(() => {
// Scroll to new discussion when displayed and expand it
const $thread = this.threadFor(discussion.id);
$thread.detailed = true;
this.$scrollTo($thread);
});
},
/**
* Replace the updated discussion in the list
* @type {Discussion} the updated discussion
*/
'discussion:updated': function(discussion) {
const index = this.discussions.indexOf(this.discussions.find(d => d.id == discussion.id));
// See: https://v1.vuejs.org/guide/list.html#Array-Change-Detection
this.discussions.$set(index, discussion);
}
},
ready() {
this.$api.get('discussions/', {for: this.subjectId}).then(response => {
this.loading = false;
this.discussions = response.data;
if (document.location.hash) {
this.$nextTick(() => { // Wait for data to be binded
this.jumpToHash(document.location.hash);
});
}
}).catch(log.error.bind(log));
},
methods: {
/**
* Display the start discussion form or triggers an authentication if required
*/
displayForm() {
this.$auth(this._('You need to be logged in to start a discussion.'));
this.formDisplayed = true;
},
/**
* Close the discussion form
*/
hideForm() {
this.formDisplayed = false;
},
/**
* Trigger a new prefilled discussion.
*/
start(title, comment) {
this.displayForm()
// Wait for next tick because the form needs to be visible to scroll
this.$nextTick(() => {
if (this.$els.form && this.$refs.form) { // Avoid logging errors
this.$scrollTo(this.$els.form);
this.$refs.form.prefill(title, comment);
}
})
},
/**
* Get the thread compoent for a given discussion id
*/
threadFor(id) {
return this.$refs.threads.find($thread => $thread.discussion.id == id);
},
/**
* Sort threads by creation date or by last response date
*/
sortBy(key) {
if (key === 'created') {
this.discussions.sort( (a,b) => new Date(b['created']) - new Date(a['created']) );
} else if (key === 'response') {
this.discussions.sort( (a,b) => new Date(b.discussion.slice(-1)[0]['posted_on']) - new Date(a.discussion.slice(-1)[0]['posted_on']) );
}
},
/**
* Handle deep linking
*/
jumpToHash(hash) {
if (hash === '#discussion-create') {
this.start();
} else if (DISCUSSION_REGEX.test(hash)) {
const [, id] = hash.match(DISCUSSION_REGEX);
this.threadFor(id).focus();
} else if (COMMENT_REGEX.test(hash)) {
const [, id, index] = hash.match(COMMENT_REGEX)
this.threadFor(id).focus(index);
} else if (NEW_COMMENT_REGEX.test(hash)) {
const [, id] = hash.match(NEW_COMMENT_REGEX);
this.threadFor(id).start();
}
}
}
}
</script>
<style lang="less">
.discussion-threads {
.list-group-form {
height: inherit;
form {
padding: 1em;
}
}
}
</style>