debian-mirror-gitlab/app/assets/javascripts/diff_notes/models/discussion.js

100 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-03-13 15:44:24 +05:30
/* eslint-disable 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 {
2018-12-13 13:39:08 +05:30
constructor(discussionId) {
2016-09-13 17:45:13 +05:30
this.id = discussionId;
this.notes = {};
this.loading = false;
this.canResolve = false;
}
2018-12-13 13:39:08 +05:30
createNote(noteObj) {
2017-08-17 22:00:37 +05:30
Vue.set(this.notes, noteObj.noteId, new NoteModel(this.id, noteObj));
2016-09-13 17:45:13 +05:30
}
2018-12-13 13:39:08 +05:30
deleteNote(noteId) {
2016-09-13 17:45:13 +05:30
Vue.delete(this.notes, noteId);
}
2018-12-13 13:39:08 +05:30
getNote(noteId) {
2016-09-13 17:45:13 +05:30
return this.notes[noteId];
}
notesCount() {
return Object.keys(this.notes).length;
}
2018-12-13 13:39:08 +05:30
isResolved() {
2016-09-13 17:45:13 +05:30
for (const noteId in this.notes) {
const note = this.notes[noteId];
if (!note.resolved) {
return false;
}
}
return true;
}
2020-03-13 15:44:24 +05:30
resolveAllNotes(resolvedBy) {
2016-09-13 17:45:13 +05:30
for (const noteId in this.notes) {
const note = this.notes[noteId];
if (!note.resolved) {
note.resolved = true;
2020-03-13 15:44:24 +05:30
note.resolved_by = resolvedBy;
2016-09-13 17:45:13 +05:30
}
}
}
2018-12-13 13:39:08 +05:30
unResolveAllNotes() {
2016-09-13 17:45:13 +05:30
for (const noteId in this.notes) {
const note = this.notes[noteId];
if (note.resolved) {
note.resolved = false;
note.resolved_by = null;
}
}
}
2018-12-13 13:39:08 +05:30
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
}
}
2018-12-13 13:39:08 +05:30
isResolvable() {
2016-09-13 17:45:13 +05:30
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;