2018-11-08 19:23:39 +05:30
|
|
|
/* eslint-disable camelcase, guard-for-in, no-restricted-syntax */
|
2017-08-17 22:00:37 +05:30
|
|
|
/* global NoteModel */
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
import $ from 'jquery';
|
2017-08-17 22:00:37 +05:30
|
|
|
import Vue from 'vue';
|
2018-03-17 18:26:18 +05:30
|
|
|
import { localTimeAgo } from '../../lib/utils/datetime_utility';
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
class DiscussionModel {
|
|
|
|
constructor (discussionId) {
|
|
|
|
this.id = discussionId;
|
|
|
|
this.notes = {};
|
|
|
|
this.loading = false;
|
|
|
|
this.canResolve = false;
|
|
|
|
}
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
createNote (noteObj) {
|
|
|
|
Vue.set(this.notes, noteObj.noteId, new NoteModel(this.id, noteObj));
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
deleteNote (noteId) {
|
|
|
|
Vue.delete(this.notes, noteId);
|
|
|
|
}
|
|
|
|
|
|
|
|
getNote (noteId) {
|
|
|
|
return this.notes[noteId];
|
|
|
|
}
|
|
|
|
|
|
|
|
notesCount() {
|
|
|
|
return Object.keys(this.notes).length;
|
|
|
|
}
|
|
|
|
|
|
|
|
isResolved () {
|
|
|
|
for (const noteId in this.notes) {
|
|
|
|
const note = this.notes[noteId];
|
|
|
|
|
|
|
|
if (!note.resolved) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolveAllNotes (resolved_by) {
|
|
|
|
for (const noteId in this.notes) {
|
|
|
|
const note = this.notes[noteId];
|
|
|
|
|
|
|
|
if (!note.resolved) {
|
|
|
|
note.resolved = true;
|
|
|
|
note.resolved_by = resolved_by;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unResolveAllNotes () {
|
|
|
|
for (const noteId in this.notes) {
|
|
|
|
const note = this.notes[noteId];
|
|
|
|
|
|
|
|
if (note.resolved) {
|
|
|
|
note.resolved = false;
|
|
|
|
note.resolved_by = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateHeadline (data) {
|
2017-08-17 22:00:37 +05:30
|
|
|
const discussionSelector = `.discussion[data-discussion-id="${this.id}"]`;
|
|
|
|
const $discussionHeadline = $(`${discussionSelector} .js-discussion-headline`);
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
if (data.discussion_headline_html) {
|
|
|
|
if ($discussionHeadline.length) {
|
|
|
|
$discussionHeadline.replaceWith(data.discussion_headline_html);
|
|
|
|
} else {
|
2017-08-17 22:00:37 +05:30
|
|
|
$(`${discussionSelector} .discussion-header`).append(data.discussion_headline_html);
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
localTimeAgo($('.js-timeago', `${discussionSelector}`));
|
2016-09-13 17:45:13 +05:30
|
|
|
} else {
|
2017-08-17 22:00:37 +05:30
|
|
|
$discussionHeadline.remove();
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isResolvable () {
|
|
|
|
if (!this.canResolve) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
for (const noteId in this.notes) {
|
|
|
|
const note = this.notes[noteId];
|
|
|
|
|
|
|
|
if (note.canResolve) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
window.DiscussionModel = DiscussionModel;
|