You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

toast.service.ts 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. * Unless required by applicable law or agreed to in writing,
  12. * software distributed under the License is distributed on an
  13. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. * KIND, either express or implied. See the License for the
  15. * specific language governing permissions and limitations
  16. * under the License.
  17. */
  18. import { Injectable, TemplateRef } from '@angular/core';
  19. import {AppNotification} from "@app/model/app-notification";
  20. import {not} from "rxjs/internal-compatibility";
  21. import {TranslateService} from "@ngx-translate/core";
  22. @Injectable({
  23. providedIn: 'root'
  24. })
  25. export class ToastService {
  26. maxNotifications:number=10
  27. maxHistory:number=100
  28. toasts:AppNotification[]=[]
  29. toastHistory:AppNotification[]=[]
  30. constructor(private translator: TranslateService) { }
  31. public show(origin:string, textOrTpl: string | TemplateRef<any>, options: any = {}): void {
  32. let notification = new AppNotification(origin, textOrTpl, "", options);
  33. this.toasts.push(notification);
  34. this.toastHistory.push(notification);
  35. if (this.toasts.length>this.maxNotifications) {
  36. this.toasts.splice(0, 1);
  37. }
  38. if (this.toastHistory.length>this.maxHistory) {
  39. this.toastHistory.splice(0, 1);
  40. }
  41. console.log("Notification " + notification);
  42. }
  43. public showStandard(origin:string,textOrTpl:string|TemplateRef<any>, options:any={}) : void {
  44. options.classname=['alert','alert-primary']
  45. if (!options.delay) {
  46. options.delay=8000
  47. }
  48. this.show(origin,textOrTpl,options)
  49. }
  50. public showStandardByKey(origin:string,translationKey:string, params:any={}, options:any={} ) : void {
  51. let message:string;
  52. if (params) {
  53. message = this.translator.instant(translationKey, params);
  54. } else {
  55. message = this.translator.instant(translationKey);
  56. }
  57. this.showStandard(origin, message, options);
  58. }
  59. public showError(origin:string,textOrTpl:string|TemplateRef<any>, options:any={}) : void {
  60. options.classname=['alert','alert-danger']
  61. options.type='error'
  62. if (!options.delay) {
  63. options.delay=10000
  64. }
  65. this.show(origin,textOrTpl,options)
  66. }
  67. public showErrorByKey(origin:string,translationKey:string, params:any={}, options:any={} ) : void {
  68. let message:string;
  69. if (params) {
  70. message = this.translator.instant(translationKey, params);
  71. } else {
  72. message = this.translator.instant(translationKey);
  73. }
  74. this.showError(origin, message, options);
  75. }
  76. public showSuccess(origin:string,textOrTpl:string|TemplateRef<any>, options:any={}) : void {
  77. options.classname=['alert','alert-info']
  78. options.type='success'
  79. if (!options.delay) {
  80. options.delay=8000
  81. }
  82. this.show(origin,textOrTpl,options)
  83. }
  84. public showSuccessByKey(origin:string,translationKey:string, params:any={}, options:any={} ) : void {
  85. let message:string;
  86. if (params) {
  87. message = this.translator.instant(translationKey, params);
  88. } else {
  89. message = this.translator.instant(translationKey);
  90. }
  91. this.showSuccess(origin, message, options);
  92. }
  93. remove(toast) {
  94. this.toasts = this.toasts.filter(t => t != toast);
  95. }
  96. }