confirmdialog.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 animations_1 = require("@angular/animations");
  14. var common_1 = require("@angular/common");
  15. var domhandler_1 = require("../dom/domhandler");
  16. var shared_1 = require("../common/shared");
  17. var button_1 = require("../button/button");
  18. var confirmationservice_1 = require("../common/confirmationservice");
  19. var ConfirmDialog = /** @class */ (function () {
  20. function ConfirmDialog(el, renderer, confirmationService, zone) {
  21. var _this = this;
  22. this.el = el;
  23. this.renderer = renderer;
  24. this.confirmationService = confirmationService;
  25. this.zone = zone;
  26. this.acceptIcon = 'pi pi-check';
  27. this.acceptLabel = 'Yes';
  28. this.acceptVisible = true;
  29. this.rejectIcon = 'pi pi-times';
  30. this.rejectLabel = 'No';
  31. this.rejectVisible = true;
  32. this.closeOnEscape = true;
  33. this.blockScroll = true;
  34. this.closable = true;
  35. this.autoZIndex = true;
  36. this.baseZIndex = 0;
  37. this.transitionOptions = '150ms cubic-bezier(0, 0, 0.2, 1)';
  38. this.focusTrap = true;
  39. this.subscription = this.confirmationService.requireConfirmation$.subscribe(function (confirmation) {
  40. if (confirmation.key === _this.key) {
  41. _this.confirmation = confirmation;
  42. _this.message = _this.confirmation.message || _this.message;
  43. _this.icon = _this.confirmation.icon || _this.icon;
  44. _this.header = _this.confirmation.header || _this.header;
  45. _this.rejectVisible = _this.confirmation.rejectVisible == null ? _this.rejectVisible : _this.confirmation.rejectVisible;
  46. _this.acceptVisible = _this.confirmation.acceptVisible == null ? _this.acceptVisible : _this.confirmation.acceptVisible;
  47. _this.acceptLabel = _this.confirmation.acceptLabel || _this.acceptLabel;
  48. _this.rejectLabel = _this.confirmation.rejectLabel || _this.rejectLabel;
  49. if (_this.confirmation.accept) {
  50. _this.confirmation.acceptEvent = new core_1.EventEmitter();
  51. _this.confirmation.acceptEvent.subscribe(_this.confirmation.accept);
  52. }
  53. if (_this.confirmation.reject) {
  54. _this.confirmation.rejectEvent = new core_1.EventEmitter();
  55. _this.confirmation.rejectEvent.subscribe(_this.confirmation.reject);
  56. }
  57. if (_this.confirmation.blockScroll === false) {
  58. _this.blockScroll = _this.confirmation.blockScroll;
  59. }
  60. _this.visible = true;
  61. }
  62. });
  63. }
  64. Object.defineProperty(ConfirmDialog.prototype, "width", {
  65. get: function () {
  66. return this._width;
  67. },
  68. set: function (val) {
  69. this._width = val;
  70. console.warn("width property is deprecated, use style to define the width of the Dialog.");
  71. },
  72. enumerable: true,
  73. configurable: true
  74. });
  75. Object.defineProperty(ConfirmDialog.prototype, "height", {
  76. get: function () {
  77. return this._height;
  78. },
  79. set: function (val) {
  80. this._height = val;
  81. console.warn("height property is deprecated, use style to define the height of the Dialog.");
  82. },
  83. enumerable: true,
  84. configurable: true
  85. });
  86. ConfirmDialog.prototype.onAnimationStart = function (event) {
  87. switch (event.toState) {
  88. case 'visible':
  89. this.container = event.element;
  90. this.setDimensions();
  91. this.contentContainer = domhandler_1.DomHandler.findSingle(this.container, '.ui-dialog-content');
  92. if (this.acceptVisible || this.rejectVisible) {
  93. domhandler_1.DomHandler.findSingle(this.container, 'button').focus();
  94. }
  95. this.appendContainer();
  96. this.moveOnTop();
  97. this.bindGlobalListeners();
  98. this.enableModality();
  99. break;
  100. case 'void':
  101. this.onOverlayHide();
  102. break;
  103. }
  104. };
  105. ConfirmDialog.prototype.setDimensions = function () {
  106. if (this.width) {
  107. this.container.style.width = this.width + 'px';
  108. }
  109. if (this.height) {
  110. this.container.style.height = this.height + 'px';
  111. }
  112. };
  113. ConfirmDialog.prototype.appendContainer = function () {
  114. if (this.appendTo) {
  115. if (this.appendTo === 'body')
  116. document.body.appendChild(this.container);
  117. else
  118. domhandler_1.DomHandler.appendChild(this.container, this.appendTo);
  119. }
  120. };
  121. ConfirmDialog.prototype.restoreAppend = function () {
  122. if (this.container && this.appendTo) {
  123. this.el.nativeElement.appendChild(this.container);
  124. }
  125. };
  126. ConfirmDialog.prototype.enableModality = function () {
  127. if (!this.mask) {
  128. this.mask = document.createElement('div');
  129. this.mask.style.zIndex = String(parseInt(this.container.style.zIndex) - 1);
  130. domhandler_1.DomHandler.addMultipleClasses(this.mask, 'ui-widget-overlay ui-dialog-mask');
  131. document.body.appendChild(this.mask);
  132. domhandler_1.DomHandler.addClass(document.body, 'ui-overflow-hidden');
  133. if (this.blockScroll) {
  134. domhandler_1.DomHandler.addClass(document.body, 'ui-overflow-hidden');
  135. }
  136. }
  137. };
  138. ConfirmDialog.prototype.disableModality = function () {
  139. if (this.mask) {
  140. document.body.removeChild(this.mask);
  141. domhandler_1.DomHandler.removeClass(document.body, 'ui-overflow-hidden');
  142. if (this.blockScroll) {
  143. domhandler_1.DomHandler.removeClass(document.body, 'ui-overflow-hidden');
  144. }
  145. this.mask = null;
  146. }
  147. };
  148. ConfirmDialog.prototype.close = function (event) {
  149. if (this.confirmation.rejectEvent) {
  150. this.confirmation.rejectEvent.emit();
  151. }
  152. this.hide();
  153. event.preventDefault();
  154. };
  155. ConfirmDialog.prototype.hide = function () {
  156. this.visible = false;
  157. };
  158. ConfirmDialog.prototype.moveOnTop = function () {
  159. if (this.autoZIndex) {
  160. this.container.style.zIndex = String(this.baseZIndex + (++domhandler_1.DomHandler.zindex));
  161. }
  162. };
  163. ConfirmDialog.prototype.bindGlobalListeners = function () {
  164. var _this = this;
  165. if ((this.closeOnEscape && this.closable) || this.focusTrap && !this.documentEscapeListener) {
  166. this.documentEscapeListener = this.renderer.listen('document', 'keydown', function (event) {
  167. if (event.which == 27 && (_this.closeOnEscape && _this.closable)) {
  168. if (parseInt(_this.container.style.zIndex) === (domhandler_1.DomHandler.zindex + _this.baseZIndex) && _this.visible) {
  169. _this.close(event);
  170. }
  171. }
  172. if (event.which === 9 && _this.focusTrap) {
  173. event.preventDefault();
  174. var focusableElements = domhandler_1.DomHandler.getFocusableElements(_this.container);
  175. if (focusableElements && focusableElements.length > 0) {
  176. if (!document.activeElement) {
  177. focusableElements[0].focus();
  178. }
  179. else {
  180. var focusedIndex = focusableElements.indexOf(document.activeElement);
  181. if (event.shiftKey) {
  182. if (focusedIndex == -1 || focusedIndex === 0)
  183. focusableElements[focusableElements.length - 1].focus();
  184. else
  185. focusableElements[focusedIndex - 1].focus();
  186. }
  187. else {
  188. if (focusedIndex == -1 || focusedIndex === (focusableElements.length - 1))
  189. focusableElements[0].focus();
  190. else
  191. focusableElements[focusedIndex + 1].focus();
  192. }
  193. }
  194. }
  195. }
  196. });
  197. }
  198. };
  199. ConfirmDialog.prototype.unbindGlobalListeners = function () {
  200. if (this.documentEscapeListener) {
  201. this.documentEscapeListener();
  202. this.documentEscapeListener = null;
  203. }
  204. };
  205. ConfirmDialog.prototype.onOverlayHide = function () {
  206. this.disableModality();
  207. this.unbindGlobalListeners();
  208. this.container = null;
  209. };
  210. ConfirmDialog.prototype.ngOnDestroy = function () {
  211. this.restoreAppend();
  212. this.onOverlayHide();
  213. this.subscription.unsubscribe();
  214. };
  215. ConfirmDialog.prototype.accept = function () {
  216. if (this.confirmation.acceptEvent) {
  217. this.confirmation.acceptEvent.emit();
  218. }
  219. this.hide();
  220. this.confirmation = null;
  221. };
  222. ConfirmDialog.prototype.reject = function () {
  223. if (this.confirmation.rejectEvent) {
  224. this.confirmation.rejectEvent.emit();
  225. }
  226. this.hide();
  227. this.confirmation = null;
  228. };
  229. __decorate([
  230. core_1.Input(),
  231. __metadata("design:type", Boolean)
  232. ], ConfirmDialog.prototype, "visible", void 0);
  233. __decorate([
  234. core_1.Input(),
  235. __metadata("design:type", String)
  236. ], ConfirmDialog.prototype, "header", void 0);
  237. __decorate([
  238. core_1.Input(),
  239. __metadata("design:type", String)
  240. ], ConfirmDialog.prototype, "icon", void 0);
  241. __decorate([
  242. core_1.Input(),
  243. __metadata("design:type", String)
  244. ], ConfirmDialog.prototype, "message", void 0);
  245. __decorate([
  246. core_1.Input(),
  247. __metadata("design:type", Object)
  248. ], ConfirmDialog.prototype, "style", void 0);
  249. __decorate([
  250. core_1.Input(),
  251. __metadata("design:type", String)
  252. ], ConfirmDialog.prototype, "styleClass", void 0);
  253. __decorate([
  254. core_1.Input(),
  255. __metadata("design:type", String)
  256. ], ConfirmDialog.prototype, "acceptIcon", void 0);
  257. __decorate([
  258. core_1.Input(),
  259. __metadata("design:type", String)
  260. ], ConfirmDialog.prototype, "acceptLabel", void 0);
  261. __decorate([
  262. core_1.Input(),
  263. __metadata("design:type", Boolean)
  264. ], ConfirmDialog.prototype, "acceptVisible", void 0);
  265. __decorate([
  266. core_1.Input(),
  267. __metadata("design:type", String)
  268. ], ConfirmDialog.prototype, "rejectIcon", void 0);
  269. __decorate([
  270. core_1.Input(),
  271. __metadata("design:type", String)
  272. ], ConfirmDialog.prototype, "rejectLabel", void 0);
  273. __decorate([
  274. core_1.Input(),
  275. __metadata("design:type", Boolean)
  276. ], ConfirmDialog.prototype, "rejectVisible", void 0);
  277. __decorate([
  278. core_1.Input(),
  279. __metadata("design:type", String)
  280. ], ConfirmDialog.prototype, "acceptButtonStyleClass", void 0);
  281. __decorate([
  282. core_1.Input(),
  283. __metadata("design:type", String)
  284. ], ConfirmDialog.prototype, "rejectButtonStyleClass", void 0);
  285. __decorate([
  286. core_1.Input(),
  287. __metadata("design:type", Boolean)
  288. ], ConfirmDialog.prototype, "closeOnEscape", void 0);
  289. __decorate([
  290. core_1.Input(),
  291. __metadata("design:type", Boolean)
  292. ], ConfirmDialog.prototype, "blockScroll", void 0);
  293. __decorate([
  294. core_1.Input(),
  295. __metadata("design:type", Boolean)
  296. ], ConfirmDialog.prototype, "rtl", void 0);
  297. __decorate([
  298. core_1.Input(),
  299. __metadata("design:type", Boolean)
  300. ], ConfirmDialog.prototype, "closable", void 0);
  301. __decorate([
  302. core_1.Input(),
  303. __metadata("design:type", Object)
  304. ], ConfirmDialog.prototype, "appendTo", void 0);
  305. __decorate([
  306. core_1.Input(),
  307. __metadata("design:type", String)
  308. ], ConfirmDialog.prototype, "key", void 0);
  309. __decorate([
  310. core_1.Input(),
  311. __metadata("design:type", Boolean)
  312. ], ConfirmDialog.prototype, "autoZIndex", void 0);
  313. __decorate([
  314. core_1.Input(),
  315. __metadata("design:type", Number)
  316. ], ConfirmDialog.prototype, "baseZIndex", void 0);
  317. __decorate([
  318. core_1.Input(),
  319. __metadata("design:type", String)
  320. ], ConfirmDialog.prototype, "transitionOptions", void 0);
  321. __decorate([
  322. core_1.Input(),
  323. __metadata("design:type", Boolean)
  324. ], ConfirmDialog.prototype, "focusTrap", void 0);
  325. __decorate([
  326. core_1.ContentChild(shared_1.Footer, { static: false }),
  327. __metadata("design:type", Object)
  328. ], ConfirmDialog.prototype, "footer", void 0);
  329. __decorate([
  330. core_1.ViewChild('content', { static: false }),
  331. __metadata("design:type", core_1.ElementRef)
  332. ], ConfirmDialog.prototype, "contentViewChild", void 0);
  333. __decorate([
  334. core_1.Input(),
  335. __metadata("design:type", Object),
  336. __metadata("design:paramtypes", [Object])
  337. ], ConfirmDialog.prototype, "width", null);
  338. __decorate([
  339. core_1.Input(),
  340. __metadata("design:type", Object),
  341. __metadata("design:paramtypes", [Object])
  342. ], ConfirmDialog.prototype, "height", null);
  343. ConfirmDialog = __decorate([
  344. core_1.Component({
  345. selector: 'p-confirmDialog',
  346. template: "\n <div [ngClass]=\"{'ui-dialog ui-confirmdialog ui-widget ui-widget-content ui-corner-all ui-shadow':true,'ui-dialog-rtl':rtl}\" [ngStyle]=\"style\" [class]=\"styleClass\" (mousedown)=\"moveOnTop()\"\n [@animation]=\"{value: 'visible', params: {transitionParams: transitionOptions}}\" (@animation.start)=\"onAnimationStart($event)\" *ngIf=\"visible\">\n <div class=\"ui-dialog-titlebar ui-widget-header ui-helper-clearfix ui-corner-top\">\n <span class=\"ui-dialog-title\" *ngIf=\"header\">{{header}}</span>\n <a *ngIf=\"closable\" [ngClass]=\"{'ui-dialog-titlebar-icon ui-dialog-titlebar-close ui-corner-all':true}\" tabindex=\"0\" role=\"button\" (click)=\"close($event)\" (keydown.enter)=\"close($event)\">\n <span class=\"pi pi-fw pi-times\"></span>\n </a>\n </div>\n <div #content class=\"ui-dialog-content ui-widget-content\">\n <i [ngClass]=\"'ui-confirmdialog-icon'\" [class]=\"icon\" *ngIf=\"icon\"></i>\n <span class=\"ui-confirmdialog-message\" [innerHTML]=\"message\"></span>\n </div>\n <div class=\"ui-dialog-footer ui-widget-content\" *ngIf=\"footer\">\n <ng-content select=\"p-footer\"></ng-content>\n </div>\n <div class=\"ui-dialog-footer ui-widget-content\" *ngIf=\"!footer\">\n <button type=\"button\" pButton [icon]=\"acceptIcon\" [label]=\"acceptLabel\" (click)=\"accept()\" [class]=\"acceptButtonStyleClass\" *ngIf=\"acceptVisible\"></button>\n <button type=\"button\" pButton [icon]=\"rejectIcon\" [label]=\"rejectLabel\" (click)=\"reject()\" [class]=\"rejectButtonStyleClass\" *ngIf=\"rejectVisible\"></button>\n </div>\n </div>\n ",
  347. animations: [
  348. animations_1.trigger('animation', [
  349. animations_1.state('void', animations_1.style({
  350. transform: 'translateX(-50%) translateY(-50%) scale(0.7)',
  351. opacity: 0
  352. })),
  353. animations_1.state('visible', animations_1.style({
  354. transform: 'translateX(-50%) translateY(-50%) scale(1)',
  355. opacity: 1
  356. })),
  357. animations_1.transition('* => *', animations_1.animate('{{transitionParams}}'))
  358. ])
  359. ]
  360. }),
  361. __metadata("design:paramtypes", [core_1.ElementRef, core_1.Renderer2, confirmationservice_1.ConfirmationService, core_1.NgZone])
  362. ], ConfirmDialog);
  363. return ConfirmDialog;
  364. }());
  365. exports.ConfirmDialog = ConfirmDialog;
  366. var ConfirmDialogModule = /** @class */ (function () {
  367. function ConfirmDialogModule() {
  368. }
  369. ConfirmDialogModule = __decorate([
  370. core_1.NgModule({
  371. imports: [common_1.CommonModule, button_1.ButtonModule],
  372. exports: [ConfirmDialog, button_1.ButtonModule, shared_1.SharedModule],
  373. declarations: [ConfirmDialog]
  374. })
  375. ], ConfirmDialogModule);
  376. return ConfirmDialogModule;
  377. }());
  378. exports.ConfirmDialogModule = ConfirmDialogModule;
  379. //# sourceMappingURL=confirmdialog.js.map