2016-12-13 22:01:05 -05:00
|
|
|
/* eslint-disable space-before-function-paren, camelcase, guard-for-in, no-restricted-syntax, no-unused-vars, max-len */
|
|
|
|
/* global NoteModel */
|
|
|
|
|
2017-03-17 13:21:25 -04:00
|
|
|
import Vue from 'vue';
|
2017-12-07 06:09:17 -05:00
|
|
|
import { localTimeAgo } from '../../lib/utils/datetime_utility';
|
2017-03-17 13:21:25 -04:00
|
|
|
|
2016-07-29 06:19:56 -04:00
|
|
|
class DiscussionModel {
|
|
|
|
constructor (discussionId) {
|
2016-07-29 08:31:32 -04:00
|
|
|
this.id = discussionId;
|
2016-07-29 06:19:56 -04:00
|
|
|
this.notes = {};
|
2016-07-29 06:52:08 -04:00
|
|
|
this.loading = false;
|
2016-08-12 16:10:05 -04:00
|
|
|
this.canResolve = false;
|
2016-07-29 06:19:56 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 11:07:26 -05:00
|
|
|
createNote (noteObj) {
|
|
|
|
Vue.set(this.notes, noteObj.noteId, new NoteModel(this.id, noteObj));
|
2016-07-29 06:19:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
deleteNote (noteId) {
|
|
|
|
Vue.delete(this.notes, noteId);
|
|
|
|
}
|
|
|
|
|
|
|
|
getNote (noteId) {
|
|
|
|
return this.notes[noteId];
|
|
|
|
}
|
|
|
|
|
2016-08-01 05:06:31 -04:00
|
|
|
notesCount() {
|
|
|
|
return Object.keys(this.notes).length;
|
|
|
|
}
|
|
|
|
|
2016-07-29 06:19:56 -04:00
|
|
|
isResolved () {
|
|
|
|
for (const noteId in this.notes) {
|
|
|
|
const note = this.notes[noteId];
|
|
|
|
|
|
|
|
if (!note.resolved) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-01 05:06:31 -04:00
|
|
|
resolveAllNotes (resolved_by) {
|
2016-07-29 06:19:56 -04:00
|
|
|
for (const noteId in this.notes) {
|
|
|
|
const note = this.notes[noteId];
|
|
|
|
|
|
|
|
if (!note.resolved) {
|
|
|
|
note.resolved = true;
|
2016-08-01 05:06:31 -04:00
|
|
|
note.resolved_by = resolved_by;
|
2016-07-29 06:19:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 05:06:31 -04:00
|
|
|
unResolveAllNotes () {
|
2016-07-29 06:19:56 -04:00
|
|
|
for (const noteId in this.notes) {
|
|
|
|
const note = this.notes[noteId];
|
|
|
|
|
|
|
|
if (note.resolved) {
|
|
|
|
note.resolved = false;
|
2016-08-01 05:06:31 -04:00
|
|
|
note.resolved_by = null;
|
2016-07-29 06:19:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-01 06:31:35 -04:00
|
|
|
|
|
|
|
updateHeadline (data) {
|
2016-11-22 06:47:26 -05:00
|
|
|
const discussionSelector = `.discussion[data-discussion-id="${this.id}"]`;
|
|
|
|
const $discussionHeadline = $(`${discussionSelector} .js-discussion-headline`);
|
2016-08-01 06:31:35 -04:00
|
|
|
|
|
|
|
if (data.discussion_headline_html) {
|
|
|
|
if ($discussionHeadline.length) {
|
|
|
|
$discussionHeadline.replaceWith(data.discussion_headline_html);
|
|
|
|
} else {
|
2016-11-22 06:47:26 -05:00
|
|
|
$(`${discussionSelector} .discussion-header`).append(data.discussion_headline_html);
|
2016-08-01 06:31:35 -04:00
|
|
|
}
|
2016-11-22 06:47:26 -05:00
|
|
|
|
2017-12-07 06:09:17 -05:00
|
|
|
localTimeAgo($('.js-timeago', `${discussionSelector}`));
|
2016-08-01 06:31:35 -04:00
|
|
|
} else {
|
2016-12-13 22:01:05 -05:00
|
|
|
$discussionHeadline.remove();
|
2016-08-01 06:31:35 -04:00
|
|
|
}
|
|
|
|
}
|
2016-08-04 04:52:17 -04:00
|
|
|
|
2016-08-12 16:10:05 -04:00
|
|
|
isResolvable () {
|
|
|
|
if (!this.canResolve) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-22 06:47:26 -05:00
|
|
|
|
2016-08-04 04:52:17 -04:00
|
|
|
for (const noteId in this.notes) {
|
|
|
|
const note = this.notes[noteId];
|
|
|
|
|
|
|
|
if (note.canResolve) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-29 06:19:56 -04:00
|
|
|
}
|
2016-12-14 01:51:53 -05:00
|
|
|
|
|
|
|
window.DiscussionModel = DiscussionModel;
|