Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { ElementRef, OnInit, Component, Input, Output, EventEmitter, ViewChild } from "@angular/core";
  2. import moment from "moment";
  3. import { KeyCode } from "edyt-core/constants/KeyCode";
  4. import { Logger } from "edyt-core/logging/Logger";
  5. import { ErrorI18N } from "../../../../i18n/error-i18n";
  6. import { CommonI18N } from "../../../../i18n/common-i18n";
  7. import { ApplicationI18N } from "../../../../i18n/application-i18n";
  8. import { TpI18NService } from "../../../../services/TpI18NService";
  9. import { UpdateNotificationModel } from "../../../../models/notification/UpdateNotification";
  10. import { UserNotificationModel, NotificationModel, CheckBoxModal } from "../../../../models/notification/UserNotification";
  11. import { UserNotificationService } from "../../../../services/UserNotificationService";
  12.  
  13. const ALL = "all";
  14. const LATEST = "latest";
  15. const OLDEST = "oldest";
  16. const USER = "USER";
  17. const SUCCESS_STATUS = "201";
  18. const DELETED = "deleted";
  19.  
  20. @Component({
  21.     selector: "user-notification",
  22.     providers: [UserNotificationService],
  23.     templateUrl: "./teampatent-core/components/workspace/notification/usernotification/user-notification.html",
  24. })
  25. export class UserNotificationComponent implements OnInit {
  26.  
  27.     @Input() public applicationNumber: string;
  28.     @Output() public userCounterUpdate = new EventEmitter<boolean>();
  29.     public selectedNotification: NotificationModel;
  30.     public sortingData: Object[] = [];
  31.     public sortingDataWithFilter: Object[] = [];
  32.     public sortingDataWithoutFilter: Object[] = [];
  33.     public filterData: Object[] = [];
  34.     public filterValue: string;
  35.     public filterTabInit: string;
  36.     public userNotifications: NotificationModel[] = [];
  37.     public filterNotifications: NotificationModel[] = [];
  38.     public sortingValue: string;
  39.     public defaultSortingValue: string;
  40.     public updateNotificationList: UpdateNotificationModel[] = [];
  41.     public updateNotificationModel: UpdateNotificationModel;
  42.     public appI18N: ApplicationI18N;
  43.     public errI18N: ErrorI18N;
  44.     public showCreate: boolean;
  45.     public showDetail: boolean;
  46.     public commonI18N: CommonI18N;
  47.     public readAction: string;
  48.     public markAction: string;
  49.     public isShowDeleted: boolean;
  50.     public onBackGet: boolean;
  51.     public filterOptions: Object[] = [];
  52.     public selectedNotifications: NotificationModel[] = [];
  53.     public notificationsToDelete: NotificationModel[] = [];
  54.     @ViewChild("snippetSelectAll") public selectAll;
  55.     @ViewChild("scroll") public scroll: ElementRef;
  56.     public checkAll: CheckBoxModal[] = [];
  57.     public selectedValue: Object[];
  58.     public allTabActive: boolean;
  59.     public deleteTabActive: boolean;
  60.  
  61.     constructor(public elRef: ElementRef, i18nService: TpI18NService, public log: Logger,
  62.         public userNotificationService: UserNotificationService) {
  63.         this.appI18N = i18nService.getApplicationI18N();
  64.         this.errI18N = i18nService.getErrorI18N();
  65.         this.commonI18N = i18nService.getCommonI18N();
  66.         this.readAction = "readAction";
  67.         this.markAction = "markAction";
  68.         this.defaultSortingValue = "latest";
  69.         this.showCreate = false;
  70.         this.showDetail = false;
  71.         this.isShowDeleted = false;
  72.         this.onBackGet = false;
  73.         this.checkAll = [
  74.             {
  75.                 value: "1",
  76.                 check: false
  77.             }
  78.         ];
  79.         this.allTabActive = true;
  80.         this.deleteTabActive = false;
  81.     }
  82.  
  83.     public ngOnInit() {
  84.         if (this.filterTabInit === undefined) {
  85.             this.filterTabInit = ALL;
  86.             this.filterValue = "false";
  87.             this.sortingValue = this.defaultSortingValue;
  88.         }
  89.         let url;
  90.         url = "?filter=" + this.filterTabInit + "&sort=" + this.defaultSortingValue;
  91.         url = this.applicationNumber ? url + "&applicationNumber=" + this.applicationNumber : url;
  92.         this.userNotificationService.getData(url).subscribe((data: UserNotificationModel) => {
  93.             let allNotifications = this.updateGrid(data);
  94.             this.onUNotificationFilter(allNotifications , false);
  95.             this.updateSort(data);
  96.             this.updateFilter(data);
  97.             this.updateTab(data);
  98.         },
  99.             (error) => {
  100.                 this.log.error(this.errI18N.notificationError, error, false);
  101.             });
  102.     }
  103.  
  104.     public selectAllCells(event) {
  105.         if (!event.target.checked) {
  106.             if (this.selectAll) {
  107.                 this.selectAll.deselectAll();
  108.             }
  109.             this.selectedNotifications = [];
  110.         } else {
  111.             this.selectAll.selectAll();
  112.             this.selectedNotifications = this.filterNotifications;
  113.             this.notificationsToDelete = this.selectedNotifications;
  114.         }
  115.     }
  116.  
  117.  
  118.     public selectedItems(items: NotificationModel[]) {
  119.         this.selectedNotifications = [];
  120.         for (let i = items.length - 1; i >= 0; i--) {
  121.             if ( items[i] === undefined ) {
  122.                 items.splice(i, 1);
  123.             }
  124.         }
  125.         this.selectedNotifications = items;
  126.         if ((this.selectedNotifications.length !== this.filterNotifications.length) && (this.checkAll[0].check === true)) {
  127.             this.checkAll = [
  128.                 {
  129.                     value: "1",
  130.                     check: false
  131.                 }
  132.             ];
  133.         }
  134.     }
  135.  
  136.  
  137.     public onBack(filterTabInit) {
  138.         this.selectedValue = [];
  139.         this.showCreate = false;
  140.         this.showDetail = false;
  141.         if (filterTabInit === "all") {
  142.             this.allTabActive = true;
  143.             this.deleteTabActive = false;
  144.         } else {
  145.             this.allTabActive = false;
  146.             this.deleteTabActive = true;
  147.         }
  148.     }
  149.  
  150.     public onUserCounterUpdate(readFlag: boolean) {
  151.          this.userCounterUpdate.emit(readFlag);
  152.     }
  153.     public updateCounter() {
  154.         this.ngOnInit();
  155.     }
  156.     public showCreateNotification() {
  157.         this.showCreate = true;
  158.     }
  159.  
  160.     public onUNotificationFilter(listToFilter , isOnlyFilter: boolean) {
  161.         this.onScrollTop();
  162.         if (this.selectAll && this.checkAll[0].check === false && isOnlyFilter) {
  163.             this.selectAll.deselectAll();
  164.             this.checkAll[0].check = false;
  165.         }
  166.         if (this.filterValue === "true" || this.filterValue === "false") {
  167.             let temp;
  168.             if (this.filterValue === "true") {
  169.                 temp = true;
  170.             } else {
  171.                 temp = false;
  172.             }
  173.             return this.filterNotifications = listToFilter.filter(task => task.sentByMe === temp);
  174.         } else {
  175.             return this.filterNotifications = listToFilter;
  176.         }
  177.     }
  178.  
  179.     public showFiltered(event: Event, filter: string) {
  180.         if (this.filterTabInit !== filter) {
  181.             if (this.selectAll) {
  182.                 this.selectAll.deselectAll();
  183.             }
  184.             if (this.onBackGet === false) {
  185.                 this.filterValue = "false";
  186.                 this.sortingValue = this.defaultSortingValue;
  187.             }else {
  188.                 this.onBackGet = false;
  189.             }
  190.             this.selectedNotifications = [];
  191.             this.checkAll[0].check = false;
  192.             this.isShowDeleted = filter === ALL ? false : true;
  193.             let sortValue;
  194.             this.filterTabInit = filter;
  195.             let url = "?filter=" + this.filterTabInit;
  196.             if (this.sortingValue) {
  197.                 sortValue = this.sortingValue === LATEST || this.sortingValue === OLDEST ? this.sortingValue : this.sortingValue + ";" + this.defaultSortingValue;
  198.                 url = url + "&sort=" + sortValue;
  199.             } else {
  200.                 url = url + "&sort=" + LATEST;
  201.             }
  202.             url = this.applicationNumber ? url + "&applicationNumber=" + this.applicationNumber : url;
  203.             this.userNotificationService.getData(url).subscribe((data: UserNotificationModel) => {
  204.                 let listToFilter = this.updateGrid(data);
  205.                 this.onScrollTop();
  206.                 this.onUNotificationFilter(listToFilter , true);
  207.             },
  208.                 (error) => {
  209.                     this.log.error(this.errI18N.notificationError, error, false);
  210.                 });
  211.         }
  212.     }
  213.  
  214.     public onUNotificationSort() {
  215.         this.onScrollTop();
  216.         let url = "?filter=" + this.filterTabInit;
  217.         if (this.sortingValue) {
  218.             let sortValue;
  219.             sortValue = this.sortingValue === LATEST || this.sortingValue === OLDEST
  220.                 ? this.sortingValue : this.sortingValue + ";" + this.defaultSortingValue;
  221.             url = url + "&sort=" + sortValue;
  222.             url = this.applicationNumber ? url + "&applicationNumber=" + this.applicationNumber : url;
  223.             this.userNotificationService.getData(url).subscribe((data: UserNotificationModel) => {
  224.                 let listToFilter = this.updateGrid(data);
  225.                 this.onUNotificationFilter(listToFilter , false );
  226.             },
  227.                 (error) => {
  228.                     this.log.error(this.errI18N.notificationError, error, false);
  229.                 });
  230.         }
  231.     }
  232.  
  233.     public handleUserAction(event: KeyboardEvent, selectedItem: NotificationModel, changeOn: string) {
  234.         event.stopPropagation();
  235.         if (event.keyCode === KeyCode.KEY_SPACE) {
  236.             event.preventDefault();
  237.             this.onUserChange(event, selectedItem, changeOn);
  238.         }
  239.     }
  240.  
  241.     public onUserChange(event: KeyboardEvent, selectedItem: NotificationModel, changeOn: string) {
  242.         event.preventDefault();
  243.         let counterFlag = false;
  244.         let deleted = this.filterTabInit === ALL ? false : true;
  245.         event.stopPropagation();
  246.         let read = selectedItem.read;
  247.         let marked = selectedItem.userMarked;
  248.         this.updateNotificationList = [];
  249.         if (changeOn === this.readAction) {
  250.             read = selectedItem.read === true ? false : true;
  251.             counterFlag = true;
  252.         } else if (changeOn === this.markAction) {
  253.             marked = selectedItem.userMarked === true ? false : true;
  254.         }
  255.  
  256.         this.updateNotificationModel =
  257.             new UpdateNotificationModel(selectedItem.notificationId, selectedItem.sentByMe, USER, read, marked, deleted);
  258.         this.updateNotificationList.push(this.updateNotificationModel);
  259.         this.userNotificationService.updateNotification(this.updateNotificationList).subscribe(
  260.             data => {
  261.                 if (data.statusCode === SUCCESS_STATUS) {
  262.                     selectedItem.userMarked = marked;
  263.                     selectedItem.read = read;
  264.                     if (counterFlag && !deleted) {
  265.                         this.userCounterUpdate.emit(read);
  266.                     }
  267.                     if (this.sortingValue === "read" || this.sortingValue === "unread" || this.sortingValue === "markedImportant") {
  268.                         this.onUNotificationSort();
  269.                     }
  270.                 }
  271.             },
  272.             (error) => {
  273.                 this.log.error(this.errI18N.notificationError, error, false);
  274.             });
  275.     }
  276.  
  277.  
  278.     public showDetailPopup(notification: NotificationModel): void {
  279.         this.updateNotificationList = [];
  280.         this.selectedNotifications = [];
  281.         if (this.selectAll) {
  282.             this.selectAll.deselectAll();
  283.         }
  284.         if (notification.read !== true) {
  285.             let deleted = this.filterTabInit === ALL ? false : true;
  286.             let userMarked;
  287.             userMarked = notification.userMarked ? true : false;
  288.             this.updateNotificationModel = new UpdateNotificationModel(notification.notificationId, notification.sentByMe, USER, true, userMarked, deleted);
  289.             this.updateNotificationList.push(this.updateNotificationModel);
  290.             this.userNotificationService.updateNotification(this.updateNotificationList)
  291.                 .subscribe(data => {
  292.                      if (data.statusCode === SUCCESS_STATUS && !deleted) {
  293.                          this.userCounterUpdate.emit(notification.read);
  294.                      }
  295.                     this.log.info(this.errI18N.notificationError, "", false);
  296.                 },
  297.                 (error) => {
  298.                     this.log.error(this.errI18N.notificationError, error, false);
  299.                 });
  300.         }
  301.         this.selectedNotification = notification;
  302.         this.showDetail = true;
  303.     }
  304.  
  305.  
  306.     public updateGrid(data: UserNotificationModel) {
  307.         let notificationData = data.userNotificationData.userNotifications;
  308.         this.userNotifications = [];
  309.         for (let i = 0; i < data.userNotificationData.userNotifications.length; i++) {
  310.             let dateToPass = moment(notificationData[i].date, "DD-MM-YYYY h:mm").format("DD.MM.YYYY HH:mm");
  311.             let dateTime = moment(notificationData[i].date, "DD-MM-YYYY h:mm").format("DD.MM.YYYY");
  312.             this.userNotifications.push(new NotificationModel(notificationData[i].notificationId,
  313.                 notificationData[i].applicationNumber, notificationData[i].subject, dateTime, notificationData[i].fromUser, notificationData[i].message
  314.                 , notificationData[i].read, notificationData[i].userMarkedImportant, notificationData[i].senderMarkedImportant, dateToPass, notificationData[i].sentByMe,
  315.                 notificationData[i].recipient
  316.             ));
  317.         }
  318.         return this.userNotifications;
  319.     }
  320.  
  321.     public updateSort(data: UserNotificationModel) {
  322.         this.sortingData = [];
  323.         for (let key in data.sort.option) {
  324.             if (key) {
  325.                 this.sortingData.push({ "label": data.sort.option[key], "value": key });
  326.             }
  327.         }
  328.     }
  329.  
  330.     public updateTab(data: UserNotificationModel) {
  331.         this.filterOptions = [];
  332.         for (let key in data.filter.option) {
  333.             if (key) {
  334.                 this.filterOptions.push({ "label": data.filter.option[key], "value": key });
  335.             }
  336.         }
  337.     }
  338.  
  339.     public updateFilter(data: UserNotificationModel) {
  340.         this.filterData = [];
  341.         this.filterData.push({ "label": "None", "value": "none" });
  342.         this.filterData.push({ "label": "Sent", "value": "true" });
  343.         this.filterData.push({ "label": "Received", "value": "false" });
  344.     }
  345.  
  346.     public handleOnDelete(event: KeyboardEvent, selectedNotification: NotificationModel) {
  347.         event.stopPropagation();
  348.         if (event.keyCode === KeyCode.KEY_RETURN) {
  349.             event.preventDefault();
  350.             this.callDelete(event, selectedNotification);
  351.         }
  352.     }
  353.  
  354.     public removeItem(id: number, removeFromFilter: boolean) {
  355.         if (removeFromFilter) {
  356.             this.filterNotifications = this.filterNotifications.filter(item => item.notificationId !== id);
  357.         } else {
  358.             this.selectedNotifications = this.selectedNotifications.filter(item => item.notificationId !== id);
  359.         }
  360.     }
  361.  
  362.     public callDelete(event: Event, selectedNotification: NotificationModel) {
  363.         event.stopPropagation();
  364.         this.notificationsToDelete = [];
  365.         this.notificationsToDelete.push(selectedNotification);
  366.         this.deleteMultipleNotification(true);
  367.     }
  368.  
  369.     public deleteMultipleNotification(isSingle: boolean) {
  370.         this.updateNotificationList = [];
  371.         this.updateNotificationList.length = 0;
  372.         let isDelete;
  373.         if (this.filterTabInit === ALL) {
  374.             isDelete = true;
  375.         }
  376.         if (this.filterTabInit === DELETED) {
  377.             isDelete = false;
  378.         }
  379.         if (!isSingle) {
  380.             this.notificationsToDelete = this.selectedNotifications;
  381.         }
  382.         for (let i = 0; i < this.notificationsToDelete.length; i++) {
  383.             this.updateNotificationModel =
  384.                 new UpdateNotificationModel(this.notificationsToDelete[i].notificationId, this.notificationsToDelete[i].sentByMe, USER, this.notificationsToDelete[i].read,
  385.                     this.notificationsToDelete[i].userMarked, isDelete);
  386.             this.updateNotificationList.push(this.updateNotificationModel);
  387.         }
  388.  
  389.         this.userNotificationService.updateNotification(this.updateNotificationList).subscribe(
  390.             data => {
  391.                 if (data.statusCode === SUCCESS_STATUS) {
  392.                         if (this.notificationsToDelete.length === 1 && !this.checkAll[0].check) {
  393.                            this.onUNotificationFilter(this.filterNotifications, false);
  394.                         }
  395.                         if (this.notificationsToDelete.length === 1 && this.checkAll[0].check) {
  396.                            this.onUNotificationFilter(this.filterNotifications, true);
  397.                         }
  398.                         for (let j = 0; j < this.notificationsToDelete.length; j++) {
  399.                             this.removeItem(this.notificationsToDelete[j].notificationId, true);
  400.                             this.removeItem(this.notificationsToDelete[j].notificationId, false);
  401.                             if (isDelete && !this.notificationsToDelete[j].read && !this.notificationsToDelete[j].sentByMe) {
  402.                                 this.userCounterUpdate.emit(!this.notificationsToDelete[j].read);
  403.                             }
  404.                             if (!isDelete && !this.notificationsToDelete[j].read && !this.notificationsToDelete[j].sentByMe) {
  405.                                 this.userCounterUpdate.emit(this.notificationsToDelete[j].read);
  406.                             }
  407.                         }
  408.                         this.notificationsToDelete.length = 0;
  409.                         if (!this.checkAll[0].check && !isSingle) {
  410.                             if (this.selectAll) {
  411.                                 this.selectAll.deselectAll();
  412.                                 this.selectedNotifications = [];
  413.                             }
  414.                         } else if ((this.checkAll[0].check && !isSingle) || this.notificationsToDelete.length === this.filterNotifications.length) {
  415.                             this.filterNotifications = [];
  416.                             this.selectedNotifications = [];
  417.                             if (this.selectAll) {
  418.                                 this.selectAll.deselectAll();
  419.                             }
  420.                             this.checkAll[0].check = false;
  421.                             this.selectedValue = this.filterNotifications = [];
  422.                             this.notificationsToDelete.length = 0;
  423.                         }
  424.                 }
  425.             },
  426.             (error) => {
  427.                 this.log.error(this.errI18N.systemnotificationError, error, true);
  428.             });
  429.     }
  430.  
  431.     public onScrollTop() {
  432.         if (this.scroll) {
  433.             this.scroll.nativeElement.scrollTop = 0;
  434.         }
  435.     }
  436.  
  437. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement