toast.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. "use strict";
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  4. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  5. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  6. return c > 3 && r && Object.defineProperty(target, key, r), r;
  7. };
  8. var __metadata = (this && this.__metadata) || function (k, v) {
  9. if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. var core_1 = require("@angular/core");
  13. var common_1 = require("@angular/common");
  14. var domhandler_1 = require("../dom/domhandler");
  15. var shared_1 = require("../common/shared");
  16. var messageservice_1 = require("../common/messageservice");
  17. var animations_1 = require("@angular/animations");
  18. var ToastItem = /** @class */ (function () {
  19. function ToastItem() {
  20. this.onClose = new core_1.EventEmitter();
  21. }
  22. ToastItem.prototype.ngAfterViewInit = function () {
  23. this.initTimeout();
  24. };
  25. ToastItem.prototype.initTimeout = function () {
  26. var _this = this;
  27. if (!this.message.sticky) {
  28. this.timeout = setTimeout(function () {
  29. _this.onClose.emit({
  30. index: _this.index,
  31. message: _this.message
  32. });
  33. }, this.message.life || 3000);
  34. }
  35. };
  36. ToastItem.prototype.clearTimeout = function () {
  37. if (this.timeout) {
  38. clearTimeout(this.timeout);
  39. this.timeout = null;
  40. }
  41. };
  42. ToastItem.prototype.onMouseEnter = function () {
  43. this.clearTimeout();
  44. };
  45. ToastItem.prototype.onMouseLeave = function () {
  46. this.initTimeout();
  47. };
  48. ToastItem.prototype.onCloseIconClick = function (event) {
  49. this.clearTimeout();
  50. this.onClose.emit({
  51. index: this.index,
  52. message: this.message
  53. });
  54. event.preventDefault();
  55. };
  56. ToastItem.prototype.ngOnDestroy = function () {
  57. this.clearTimeout();
  58. };
  59. __decorate([
  60. core_1.Input(),
  61. __metadata("design:type", Object)
  62. ], ToastItem.prototype, "message", void 0);
  63. __decorate([
  64. core_1.Input(),
  65. __metadata("design:type", Number)
  66. ], ToastItem.prototype, "index", void 0);
  67. __decorate([
  68. core_1.Input(),
  69. __metadata("design:type", core_1.TemplateRef)
  70. ], ToastItem.prototype, "template", void 0);
  71. __decorate([
  72. core_1.Input(),
  73. __metadata("design:type", String)
  74. ], ToastItem.prototype, "showTransitionOptions", void 0);
  75. __decorate([
  76. core_1.Input(),
  77. __metadata("design:type", String)
  78. ], ToastItem.prototype, "hideTransitionOptions", void 0);
  79. __decorate([
  80. core_1.Output(),
  81. __metadata("design:type", core_1.EventEmitter)
  82. ], ToastItem.prototype, "onClose", void 0);
  83. __decorate([
  84. core_1.ViewChild('container', { static: false }),
  85. __metadata("design:type", core_1.ElementRef)
  86. ], ToastItem.prototype, "containerViewChild", void 0);
  87. ToastItem = __decorate([
  88. core_1.Component({
  89. selector: 'p-toastItem',
  90. template: "\n <div #container class=\"ui-toast-message ui-shadow\" [@messageState]=\"{value: 'visible', params: {showTransitionParams: showTransitionOptions, hideTransitionParams: hideTransitionOptions}}\"\n [ngClass]=\"{'ui-toast-message-info': message.severity == 'info','ui-toast-message-warn': message.severity == 'warn',\n 'ui-toast-message-error': message.severity == 'error','ui-toast-message-success': message.severity == 'success'}\"\n (mouseenter)=\"onMouseEnter()\" (mouseleave)=\"onMouseLeave()\">\n <div class=\"ui-toast-message-content\">\n <a tabindex=\"0\" class=\"ui-toast-close-icon pi pi-times\" (click)=\"onCloseIconClick($event)\" (keydown.enter)=\"onCloseIconClick($event)\" *ngIf=\"message.closable !== false\"></a>\n <ng-container *ngIf=\"!template\">\n <span class=\"ui-toast-icon pi\"\n [ngClass]=\"{'pi-info-circle': message.severity == 'info', 'pi-exclamation-triangle': message.severity == 'warn',\n 'pi-times': message.severity == 'error', 'pi-check' :message.severity == 'success'}\"></span>\n <div class=\"ui-toast-message-text-content\">\n <div class=\"ui-toast-summary\">{{message.summary}}</div>\n <div class=\"ui-toast-detail\">{{message.detail}}</div>\n </div>\n </ng-container>\n <ng-container *ngTemplateOutlet=\"template; context: {$implicit: message}\"></ng-container>\n </div>\n </div>\n ",
  91. animations: [
  92. animations_1.trigger('messageState', [
  93. animations_1.state('visible', animations_1.style({
  94. transform: 'translateY(0)',
  95. opacity: 1
  96. })),
  97. animations_1.transition('void => *', [
  98. animations_1.style({ transform: 'translateY(100%)', opacity: 0 }),
  99. animations_1.animate('{{showTransitionParams}}')
  100. ]),
  101. animations_1.transition('* => void', [
  102. animations_1.animate(('{{hideTransitionParams}}'), animations_1.style({
  103. height: 0,
  104. opacity: 0,
  105. transform: 'translateY(-100%)'
  106. }))
  107. ])
  108. ])
  109. ]
  110. })
  111. ], ToastItem);
  112. return ToastItem;
  113. }());
  114. exports.ToastItem = ToastItem;
  115. var Toast = /** @class */ (function () {
  116. function Toast(messageService) {
  117. this.messageService = messageService;
  118. this.autoZIndex = true;
  119. this.baseZIndex = 0;
  120. this.position = 'top-right';
  121. this.showTransitionOptions = '300ms ease-out';
  122. this.hideTransitionOptions = '250ms ease-in';
  123. this.onClose = new core_1.EventEmitter();
  124. }
  125. Toast.prototype.ngOnInit = function () {
  126. var _this = this;
  127. this.messageSubscription = this.messageService.messageObserver.subscribe(function (messages) {
  128. if (messages) {
  129. if (messages instanceof Array) {
  130. var filteredMessages = messages.filter(function (m) { return _this.key === m.key; });
  131. _this.messages = _this.messages ? _this.messages.concat(filteredMessages) : filteredMessages.slice();
  132. }
  133. else if (_this.key === messages.key) {
  134. _this.messages = _this.messages ? _this.messages.concat([messages]) : [messages];
  135. }
  136. if (_this.modal && _this.messages && _this.messages.length) {
  137. _this.enableModality();
  138. }
  139. }
  140. });
  141. this.clearSubscription = this.messageService.clearObserver.subscribe(function (key) {
  142. if (key) {
  143. if (_this.key === key) {
  144. _this.messages = null;
  145. }
  146. }
  147. else {
  148. _this.messages = null;
  149. }
  150. if (_this.modal) {
  151. _this.disableModality();
  152. }
  153. });
  154. };
  155. Toast.prototype.ngAfterContentInit = function () {
  156. var _this = this;
  157. this.templates.forEach(function (item) {
  158. switch (item.getType()) {
  159. case 'message':
  160. _this.template = item.template;
  161. break;
  162. default:
  163. _this.template = item.template;
  164. break;
  165. }
  166. });
  167. };
  168. Toast.prototype.onMessageClose = function (event) {
  169. this.messages.splice(event.index, 1);
  170. if (this.messages.length === 0) {
  171. this.disableModality();
  172. }
  173. this.onClose.emit({
  174. message: event.message
  175. });
  176. };
  177. Toast.prototype.enableModality = function () {
  178. if (!this.mask) {
  179. this.mask = document.createElement('div');
  180. this.mask.style.zIndex = String(parseInt(this.containerViewChild.nativeElement.style.zIndex) - 1);
  181. var maskStyleClass = 'ui-widget-overlay ui-dialog-mask';
  182. domhandler_1.DomHandler.addMultipleClasses(this.mask, maskStyleClass);
  183. document.body.appendChild(this.mask);
  184. }
  185. };
  186. Toast.prototype.disableModality = function () {
  187. if (this.mask) {
  188. document.body.removeChild(this.mask);
  189. this.mask = null;
  190. }
  191. };
  192. Toast.prototype.onAnimationStart = function (event) {
  193. if (event.fromState === 'void' && this.autoZIndex) {
  194. this.containerViewChild.nativeElement.style.zIndex = String(this.baseZIndex + (++domhandler_1.DomHandler.zindex));
  195. }
  196. };
  197. Toast.prototype.ngOnDestroy = function () {
  198. if (this.messageSubscription) {
  199. this.messageSubscription.unsubscribe();
  200. }
  201. if (this.clearSubscription) {
  202. this.clearSubscription.unsubscribe();
  203. }
  204. this.disableModality();
  205. };
  206. __decorate([
  207. core_1.Input(),
  208. __metadata("design:type", String)
  209. ], Toast.prototype, "key", void 0);
  210. __decorate([
  211. core_1.Input(),
  212. __metadata("design:type", Boolean)
  213. ], Toast.prototype, "autoZIndex", void 0);
  214. __decorate([
  215. core_1.Input(),
  216. __metadata("design:type", Number)
  217. ], Toast.prototype, "baseZIndex", void 0);
  218. __decorate([
  219. core_1.Input(),
  220. __metadata("design:type", Object)
  221. ], Toast.prototype, "style", void 0);
  222. __decorate([
  223. core_1.Input(),
  224. __metadata("design:type", String)
  225. ], Toast.prototype, "styleClass", void 0);
  226. __decorate([
  227. core_1.Input(),
  228. __metadata("design:type", String)
  229. ], Toast.prototype, "position", void 0);
  230. __decorate([
  231. core_1.Input(),
  232. __metadata("design:type", Boolean)
  233. ], Toast.prototype, "modal", void 0);
  234. __decorate([
  235. core_1.Input(),
  236. __metadata("design:type", String)
  237. ], Toast.prototype, "showTransitionOptions", void 0);
  238. __decorate([
  239. core_1.Input(),
  240. __metadata("design:type", String)
  241. ], Toast.prototype, "hideTransitionOptions", void 0);
  242. __decorate([
  243. core_1.Output(),
  244. __metadata("design:type", core_1.EventEmitter)
  245. ], Toast.prototype, "onClose", void 0);
  246. __decorate([
  247. core_1.ViewChild('container', { static: false }),
  248. __metadata("design:type", core_1.ElementRef)
  249. ], Toast.prototype, "containerViewChild", void 0);
  250. __decorate([
  251. core_1.ContentChildren(shared_1.PrimeTemplate),
  252. __metadata("design:type", core_1.QueryList)
  253. ], Toast.prototype, "templates", void 0);
  254. Toast = __decorate([
  255. core_1.Component({
  256. selector: 'p-toast',
  257. template: "\n <div #container [ngClass]=\"{'ui-toast ui-widget': true, \n 'ui-toast-top-right': position === 'top-right',\n 'ui-toast-top-left': position === 'top-left',\n 'ui-toast-bottom-right': position === 'bottom-right',\n 'ui-toast-bottom-left': position === 'bottom-left',\n 'ui-toast-top-center': position === 'top-center',\n 'ui-toast-bottom-center': position === 'bottom-center',\n 'ui-toast-center': position === 'center'}\" \n [ngStyle]=\"style\" [class]=\"styleClass\">\n <p-toastItem *ngFor=\"let msg of messages; let i=index\" [message]=\"msg\" [index]=\"i\" (onClose)=\"onMessageClose($event)\"\n [template]=\"template\" @toastAnimation (@toastAnimation.start)=\"onAnimationStart($event)\" [showTransitionOptions]=\"showTransitionOptions\" [hideTransitionOptions]=\"hideTransitionOptions\"></p-toastItem>\n </div>\n ",
  258. animations: [
  259. animations_1.trigger('toastAnimation', [
  260. animations_1.transition(':enter, :leave', [
  261. animations_1.query('@*', animations_1.animateChild())
  262. ])
  263. ])
  264. ]
  265. }),
  266. __metadata("design:paramtypes", [messageservice_1.MessageService])
  267. ], Toast);
  268. return Toast;
  269. }());
  270. exports.Toast = Toast;
  271. var ToastModule = /** @class */ (function () {
  272. function ToastModule() {
  273. }
  274. ToastModule = __decorate([
  275. core_1.NgModule({
  276. imports: [common_1.CommonModule],
  277. exports: [Toast, shared_1.SharedModule],
  278. declarations: [Toast, ToastItem]
  279. })
  280. ], ToastModule);
  281. return ToastModule;
  282. }());
  283. exports.ToastModule = ToastModule;
  284. //# sourceMappingURL=toast.js.map