mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-05-26 17:24:56 +00:00
* remove useless locale * remove useless id values, update tests * code cleanup * fix formatting * css cleanup * code cleanup * style cleanup * fix css scope * cleanup styles * remove sanitise and don't bind to innerHTML * reduce ng-container * move model specific logic to Comment Model * update tests, remove sanitise tests * drop carma coverage to 72 as code removed * drop selection animation as selection operations are not supported by the component itself * cleanup css * fix tests and lint * update stories and tests * fix line breaks * move e2e to unit test * disable search tests * disable search tests * disable search tests
152 lines
4.0 KiB
TypeScript
152 lines
4.0 KiB
TypeScript
/*!
|
|
* @license
|
|
* Copyright © 2005-2023 Hyland Software, Inc. and its affiliates. All rights reserved.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
import { CommentModel } from '../models/comment.model';
|
|
import {
|
|
Component,
|
|
EventEmitter,
|
|
inject,
|
|
Input,
|
|
OnChanges,
|
|
Output,
|
|
SimpleChanges,
|
|
ViewEncapsulation
|
|
} from '@angular/core';
|
|
import { ADF_COMMENTS_SERVICE } from './interfaces/comments.token';
|
|
import { CommentsService } from './interfaces/comments-service.interface';
|
|
|
|
@Component({
|
|
selector: 'adf-comments',
|
|
templateUrl: './comments.component.html',
|
|
styleUrls: ['./comments.component.scss'],
|
|
encapsulation: ViewEncapsulation.None
|
|
})
|
|
export class CommentsComponent implements OnChanges {
|
|
|
|
/** The numeric ID of the task. */
|
|
@Input()
|
|
id: string;
|
|
|
|
/** Are the comments read only? */
|
|
@Input()
|
|
readOnly: boolean = false;
|
|
|
|
/** Emitted when an error occurs while displaying/adding a comment. */
|
|
@Output()
|
|
error = new EventEmitter<any>();
|
|
|
|
comments: CommentModel[] = [];
|
|
message: string;
|
|
beingAdded: boolean = false;
|
|
|
|
private commentsService = inject<CommentsService>(ADF_COMMENTS_SERVICE);
|
|
|
|
ngOnChanges(changes: SimpleChanges): void {
|
|
this.id = null;
|
|
|
|
this.id = changes['id'] ? changes['id'].currentValue : null;
|
|
|
|
if (this.id) {
|
|
this.loadComments();
|
|
} else {
|
|
this.resetComments();
|
|
}
|
|
}
|
|
|
|
loadComments() {
|
|
this.resetComments();
|
|
|
|
if (!this.hasId()) {
|
|
return;
|
|
}
|
|
|
|
this.commentsService.get(this.id).subscribe(
|
|
(comments: CommentModel[]) => {
|
|
if (!this.isArrayInstance(comments)) {
|
|
return;
|
|
}
|
|
|
|
comments = this.sortedComments(comments);
|
|
this.comments.push(...comments);
|
|
},
|
|
(err) => {
|
|
this.error.emit(err);
|
|
}
|
|
);
|
|
}
|
|
|
|
addComment() {
|
|
if (!this.canAddComment()) {
|
|
return;
|
|
}
|
|
|
|
this.beingAdded = true;
|
|
|
|
this.commentsService.add(this.id, this.message)
|
|
.subscribe(
|
|
(res: CommentModel) => {
|
|
this.addToComments(res);
|
|
this.resetMessage();
|
|
},
|
|
(err) => {
|
|
this.error.emit(err);
|
|
},
|
|
() => {
|
|
this.beingAdded = false;
|
|
}
|
|
);
|
|
}
|
|
|
|
clearMessage(event: Event): void {
|
|
event.stopPropagation();
|
|
this.resetMessage();
|
|
}
|
|
|
|
private addToComments(comment: CommentModel): void {
|
|
this.comments.unshift(comment);
|
|
}
|
|
|
|
private resetMessage(): void {
|
|
this.message = '';
|
|
}
|
|
|
|
private canAddComment(): boolean {
|
|
return this.hasId() && this.message && this.message.trim() && !this.beingAdded;
|
|
}
|
|
|
|
private hasId(): boolean {
|
|
return !!this.id;
|
|
}
|
|
|
|
private isArrayInstance(entity: any): boolean {
|
|
return entity && entity instanceof Array;
|
|
}
|
|
|
|
private sortedComments(comments: CommentModel[]): CommentModel[] {
|
|
return comments.sort((comment1: CommentModel, comment2: CommentModel) => {
|
|
const date1 = new Date(comment1.created);
|
|
const date2 = new Date(comment2.created);
|
|
|
|
return date1 > date2 ? -1 : date1 < date2 ? 1 : 0;
|
|
});
|
|
}
|
|
|
|
private resetComments(): void {
|
|
this.comments = [];
|
|
}
|
|
}
|