Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { ElementRef, OnInit, Component, Input, Output, EventEmitter, ViewChild } from "@angular/core";
- import moment from "moment";
- import { KeyCode } from "edyt-core/constants/KeyCode";
- import { Logger } from "edyt-core/logging/Logger";
- import { ErrorI18N } from "../../../../i18n/error-i18n";
- import { CommonI18N } from "../../../../i18n/common-i18n";
- import { ApplicationI18N } from "../../../../i18n/application-i18n";
- import { TpI18NService } from "../../../../services/TpI18NService";
- import { UpdateNotificationModel } from "../../../../models/notification/UpdateNotification";
- import { UserNotificationModel, NotificationModel, CheckBoxModal } from "../../../../models/notification/UserNotification";
- import { UserNotificationService } from "../../../../services/UserNotificationService";
- const ALL = "all";
- const LATEST = "latest";
- const OLDEST = "oldest";
- const USER = "USER";
- const SUCCESS_STATUS = "201";
- const DELETED = "deleted";
- @Component({
- selector: "user-notification",
- providers: [UserNotificationService],
- templateUrl: "./teampatent-core/components/workspace/notification/usernotification/user-notification.html",
- })
- export class UserNotificationComponent implements OnInit {
- @Input() public applicationNumber: string;
- @Output() public userCounterUpdate = new EventEmitter<boolean>();
- public selectedNotification: NotificationModel;
- public sortingData: Object[] = [];
- public sortingDataWithFilter: Object[] = [];
- public sortingDataWithoutFilter: Object[] = [];
- public filterData: Object[] = [];
- public filterValue: string;
- public filterTabInit: string;
- public userNotifications: NotificationModel[] = [];
- public filterNotifications: NotificationModel[] = [];
- public sortingValue: string;
- public defaultSortingValue: string;
- public updateNotificationList: UpdateNotificationModel[] = [];
- public updateNotificationModel: UpdateNotificationModel;
- public appI18N: ApplicationI18N;
- public errI18N: ErrorI18N;
- public showCreate: boolean;
- public showDetail: boolean;
- public commonI18N: CommonI18N;
- public readAction: string;
- public markAction: string;
- public isShowDeleted: boolean;
- public onBackGet: boolean;
- public filterOptions: Object[] = [];
- public selectedNotifications: NotificationModel[] = [];
- public notificationsToDelete: NotificationModel[] = [];
- @ViewChild("snippetSelectAll") public selectAll;
- @ViewChild("scroll") public scroll: ElementRef;
- public checkAll: CheckBoxModal[] = [];
- public selectedValue: Object[];
- public allTabActive: boolean;
- public deleteTabActive: boolean;
- constructor(public elRef: ElementRef, i18nService: TpI18NService, public log: Logger,
- public userNotificationService: UserNotificationService) {
- this.appI18N = i18nService.getApplicationI18N();
- this.errI18N = i18nService.getErrorI18N();
- this.commonI18N = i18nService.getCommonI18N();
- this.readAction = "readAction";
- this.markAction = "markAction";
- this.defaultSortingValue = "latest";
- this.showCreate = false;
- this.showDetail = false;
- this.isShowDeleted = false;
- this.onBackGet = false;
- this.checkAll = [
- {
- value: "1",
- check: false
- }
- ];
- this.allTabActive = true;
- this.deleteTabActive = false;
- }
- public ngOnInit() {
- if (this.filterTabInit === undefined) {
- this.filterTabInit = ALL;
- this.filterValue = "false";
- this.sortingValue = this.defaultSortingValue;
- }
- let url;
- url = "?filter=" + this.filterTabInit + "&sort=" + this.defaultSortingValue;
- url = this.applicationNumber ? url + "&applicationNumber=" + this.applicationNumber : url;
- this.userNotificationService.getData(url).subscribe((data: UserNotificationModel) => {
- let allNotifications = this.updateGrid(data);
- this.onUNotificationFilter(allNotifications , false);
- this.updateSort(data);
- this.updateFilter(data);
- this.updateTab(data);
- },
- (error) => {
- this.log.error(this.errI18N.notificationError, error, false);
- });
- }
- public selectAllCells(event) {
- if (!event.target.checked) {
- if (this.selectAll) {
- this.selectAll.deselectAll();
- }
- this.selectedNotifications = [];
- } else {
- this.selectAll.selectAll();
- this.selectedNotifications = this.filterNotifications;
- this.notificationsToDelete = this.selectedNotifications;
- }
- }
- public selectedItems(items: NotificationModel[]) {
- this.selectedNotifications = [];
- for (let i = items.length - 1; i >= 0; i--) {
- if ( items[i] === undefined ) {
- items.splice(i, 1);
- }
- }
- this.selectedNotifications = items;
- if ((this.selectedNotifications.length !== this.filterNotifications.length) && (this.checkAll[0].check === true)) {
- this.checkAll = [
- {
- value: "1",
- check: false
- }
- ];
- }
- }
- public onBack(filterTabInit) {
- this.selectedValue = [];
- this.showCreate = false;
- this.showDetail = false;
- if (filterTabInit === "all") {
- this.allTabActive = true;
- this.deleteTabActive = false;
- } else {
- this.allTabActive = false;
- this.deleteTabActive = true;
- }
- }
- public onUserCounterUpdate(readFlag: boolean) {
- this.userCounterUpdate.emit(readFlag);
- }
- public updateCounter() {
- this.ngOnInit();
- }
- public showCreateNotification() {
- this.showCreate = true;
- }
- public onUNotificationFilter(listToFilter , isOnlyFilter: boolean) {
- this.onScrollTop();
- if (this.selectAll && this.checkAll[0].check === false && isOnlyFilter) {
- this.selectAll.deselectAll();
- this.checkAll[0].check = false;
- }
- if (this.filterValue === "true" || this.filterValue === "false") {
- let temp;
- if (this.filterValue === "true") {
- temp = true;
- } else {
- temp = false;
- }
- return this.filterNotifications = listToFilter.filter(task => task.sentByMe === temp);
- } else {
- return this.filterNotifications = listToFilter;
- }
- }
- public showFiltered(event: Event, filter: string) {
- if (this.filterTabInit !== filter) {
- if (this.selectAll) {
- this.selectAll.deselectAll();
- }
- if (this.onBackGet === false) {
- this.filterValue = "false";
- this.sortingValue = this.defaultSortingValue;
- }else {
- this.onBackGet = false;
- }
- this.selectedNotifications = [];
- this.checkAll[0].check = false;
- this.isShowDeleted = filter === ALL ? false : true;
- let sortValue;
- this.filterTabInit = filter;
- let url = "?filter=" + this.filterTabInit;
- if (this.sortingValue) {
- sortValue = this.sortingValue === LATEST || this.sortingValue === OLDEST ? this.sortingValue : this.sortingValue + ";" + this.defaultSortingValue;
- url = url + "&sort=" + sortValue;
- } else {
- url = url + "&sort=" + LATEST;
- }
- url = this.applicationNumber ? url + "&applicationNumber=" + this.applicationNumber : url;
- this.userNotificationService.getData(url).subscribe((data: UserNotificationModel) => {
- let listToFilter = this.updateGrid(data);
- this.onScrollTop();
- this.onUNotificationFilter(listToFilter , true);
- },
- (error) => {
- this.log.error(this.errI18N.notificationError, error, false);
- });
- }
- }
- public onUNotificationSort() {
- this.onScrollTop();
- let url = "?filter=" + this.filterTabInit;
- if (this.sortingValue) {
- let sortValue;
- sortValue = this.sortingValue === LATEST || this.sortingValue === OLDEST
- ? this.sortingValue : this.sortingValue + ";" + this.defaultSortingValue;
- url = url + "&sort=" + sortValue;
- url = this.applicationNumber ? url + "&applicationNumber=" + this.applicationNumber : url;
- this.userNotificationService.getData(url).subscribe((data: UserNotificationModel) => {
- let listToFilter = this.updateGrid(data);
- this.onUNotificationFilter(listToFilter , false );
- },
- (error) => {
- this.log.error(this.errI18N.notificationError, error, false);
- });
- }
- }
- public handleUserAction(event: KeyboardEvent, selectedItem: NotificationModel, changeOn: string) {
- event.stopPropagation();
- if (event.keyCode === KeyCode.KEY_SPACE) {
- event.preventDefault();
- this.onUserChange(event, selectedItem, changeOn);
- }
- }
- public onUserChange(event: KeyboardEvent, selectedItem: NotificationModel, changeOn: string) {
- event.preventDefault();
- let counterFlag = false;
- let deleted = this.filterTabInit === ALL ? false : true;
- event.stopPropagation();
- let read = selectedItem.read;
- let marked = selectedItem.userMarked;
- this.updateNotificationList = [];
- if (changeOn === this.readAction) {
- read = selectedItem.read === true ? false : true;
- counterFlag = true;
- } else if (changeOn === this.markAction) {
- marked = selectedItem.userMarked === true ? false : true;
- }
- this.updateNotificationModel =
- new UpdateNotificationModel(selectedItem.notificationId, selectedItem.sentByMe, USER, read, marked, deleted);
- this.updateNotificationList.push(this.updateNotificationModel);
- this.userNotificationService.updateNotification(this.updateNotificationList).subscribe(
- data => {
- if (data.statusCode === SUCCESS_STATUS) {
- selectedItem.userMarked = marked;
- selectedItem.read = read;
- if (counterFlag && !deleted) {
- this.userCounterUpdate.emit(read);
- }
- if (this.sortingValue === "read" || this.sortingValue === "unread" || this.sortingValue === "markedImportant") {
- this.onUNotificationSort();
- }
- }
- },
- (error) => {
- this.log.error(this.errI18N.notificationError, error, false);
- });
- }
- public showDetailPopup(notification: NotificationModel): void {
- this.updateNotificationList = [];
- this.selectedNotifications = [];
- if (this.selectAll) {
- this.selectAll.deselectAll();
- }
- if (notification.read !== true) {
- let deleted = this.filterTabInit === ALL ? false : true;
- let userMarked;
- userMarked = notification.userMarked ? true : false;
- this.updateNotificationModel = new UpdateNotificationModel(notification.notificationId, notification.sentByMe, USER, true, userMarked, deleted);
- this.updateNotificationList.push(this.updateNotificationModel);
- this.userNotificationService.updateNotification(this.updateNotificationList)
- .subscribe(data => {
- if (data.statusCode === SUCCESS_STATUS && !deleted) {
- this.userCounterUpdate.emit(notification.read);
- }
- this.log.info(this.errI18N.notificationError, "", false);
- },
- (error) => {
- this.log.error(this.errI18N.notificationError, error, false);
- });
- }
- this.selectedNotification = notification;
- this.showDetail = true;
- }
- public updateGrid(data: UserNotificationModel) {
- let notificationData = data.userNotificationData.userNotifications;
- this.userNotifications = [];
- for (let i = 0; i < data.userNotificationData.userNotifications.length; i++) {
- let dateToPass = moment(notificationData[i].date, "DD-MM-YYYY h:mm").format("DD.MM.YYYY HH:mm");
- let dateTime = moment(notificationData[i].date, "DD-MM-YYYY h:mm").format("DD.MM.YYYY");
- this.userNotifications.push(new NotificationModel(notificationData[i].notificationId,
- notificationData[i].applicationNumber, notificationData[i].subject, dateTime, notificationData[i].fromUser, notificationData[i].message
- , notificationData[i].read, notificationData[i].userMarkedImportant, notificationData[i].senderMarkedImportant, dateToPass, notificationData[i].sentByMe,
- notificationData[i].recipient
- ));
- }
- return this.userNotifications;
- }
- public updateSort(data: UserNotificationModel) {
- this.sortingData = [];
- for (let key in data.sort.option) {
- if (key) {
- this.sortingData.push({ "label": data.sort.option[key], "value": key });
- }
- }
- }
- public updateTab(data: UserNotificationModel) {
- this.filterOptions = [];
- for (let key in data.filter.option) {
- if (key) {
- this.filterOptions.push({ "label": data.filter.option[key], "value": key });
- }
- }
- }
- public updateFilter(data: UserNotificationModel) {
- this.filterData = [];
- this.filterData.push({ "label": "None", "value": "none" });
- this.filterData.push({ "label": "Sent", "value": "true" });
- this.filterData.push({ "label": "Received", "value": "false" });
- }
- public handleOnDelete(event: KeyboardEvent, selectedNotification: NotificationModel) {
- event.stopPropagation();
- if (event.keyCode === KeyCode.KEY_RETURN) {
- event.preventDefault();
- this.callDelete(event, selectedNotification);
- }
- }
- public removeItem(id: number, removeFromFilter: boolean) {
- if (removeFromFilter) {
- this.filterNotifications = this.filterNotifications.filter(item => item.notificationId !== id);
- } else {
- this.selectedNotifications = this.selectedNotifications.filter(item => item.notificationId !== id);
- }
- }
- public callDelete(event: Event, selectedNotification: NotificationModel) {
- event.stopPropagation();
- this.notificationsToDelete = [];
- this.notificationsToDelete.push(selectedNotification);
- this.deleteMultipleNotification(true);
- }
- public deleteMultipleNotification(isSingle: boolean) {
- this.updateNotificationList = [];
- this.updateNotificationList.length = 0;
- let isDelete;
- if (this.filterTabInit === ALL) {
- isDelete = true;
- }
- if (this.filterTabInit === DELETED) {
- isDelete = false;
- }
- if (!isSingle) {
- this.notificationsToDelete = this.selectedNotifications;
- }
- for (let i = 0; i < this.notificationsToDelete.length; i++) {
- this.updateNotificationModel =
- new UpdateNotificationModel(this.notificationsToDelete[i].notificationId, this.notificationsToDelete[i].sentByMe, USER, this.notificationsToDelete[i].read,
- this.notificationsToDelete[i].userMarked, isDelete);
- this.updateNotificationList.push(this.updateNotificationModel);
- }
- this.userNotificationService.updateNotification(this.updateNotificationList).subscribe(
- data => {
- if (data.statusCode === SUCCESS_STATUS) {
- if (this.notificationsToDelete.length === 1 && !this.checkAll[0].check) {
- this.onUNotificationFilter(this.filterNotifications, false);
- }
- if (this.notificationsToDelete.length === 1 && this.checkAll[0].check) {
- this.onUNotificationFilter(this.filterNotifications, true);
- }
- for (let j = 0; j < this.notificationsToDelete.length; j++) {
- this.removeItem(this.notificationsToDelete[j].notificationId, true);
- this.removeItem(this.notificationsToDelete[j].notificationId, false);
- if (isDelete && !this.notificationsToDelete[j].read && !this.notificationsToDelete[j].sentByMe) {
- this.userCounterUpdate.emit(!this.notificationsToDelete[j].read);
- }
- if (!isDelete && !this.notificationsToDelete[j].read && !this.notificationsToDelete[j].sentByMe) {
- this.userCounterUpdate.emit(this.notificationsToDelete[j].read);
- }
- }
- this.notificationsToDelete.length = 0;
- if (!this.checkAll[0].check && !isSingle) {
- if (this.selectAll) {
- this.selectAll.deselectAll();
- this.selectedNotifications = [];
- }
- } else if ((this.checkAll[0].check && !isSingle) || this.notificationsToDelete.length === this.filterNotifications.length) {
- this.filterNotifications = [];
- this.selectedNotifications = [];
- if (this.selectAll) {
- this.selectAll.deselectAll();
- }
- this.checkAll[0].check = false;
- this.selectedValue = this.filterNotifications = [];
- this.notificationsToDelete.length = 0;
- }
- }
- },
- (error) => {
- this.log.error(this.errI18N.systemnotificationError, error, true);
- });
- }
- public onScrollTop() {
- if (this.scroll) {
- this.scroll.nativeElement.scrollTop = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement