tieredmenu.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. var __param = (this && this.__param) || function (paramIndex, decorator) {
  12. return function (target, key) { decorator(target, key, paramIndex); }
  13. };
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. var core_1 = require("@angular/core");
  16. var animations_1 = require("@angular/animations");
  17. var common_1 = require("@angular/common");
  18. var domhandler_1 = require("../dom/domhandler");
  19. var router_1 = require("@angular/router");
  20. var TieredMenuSub = /** @class */ (function () {
  21. function TieredMenuSub(tieredMenu, cf, renderer) {
  22. this.cf = cf;
  23. this.renderer = renderer;
  24. this.autoZIndex = true;
  25. this.baseZIndex = 0;
  26. this.tieredMenu = tieredMenu;
  27. }
  28. Object.defineProperty(TieredMenuSub.prototype, "parentActive", {
  29. get: function () {
  30. return this._parentActive;
  31. },
  32. set: function (value) {
  33. this._parentActive = value;
  34. if (!value) {
  35. this.activeItem = null;
  36. }
  37. },
  38. enumerable: true,
  39. configurable: true
  40. });
  41. TieredMenuSub.prototype.ngAfterViewInit = function () {
  42. if (this.root && !this.tieredMenu.popup) {
  43. this.bindDocumentClickListener();
  44. }
  45. };
  46. TieredMenuSub.prototype.onItemMouseEnter = function (event, item, menuitem) {
  47. if (this.tieredMenu.popup || (!this.root || this.activeItem)) {
  48. if (menuitem.disabled) {
  49. return;
  50. }
  51. this.activeItem = item;
  52. var nextElement = item.children[0].nextElementSibling;
  53. if (nextElement) {
  54. var sublist = nextElement.children[0];
  55. if (this.autoZIndex) {
  56. sublist.style.zIndex = String(this.baseZIndex + (++domhandler_1.DomHandler.zindex));
  57. }
  58. sublist.style.zIndex = String(++domhandler_1.DomHandler.zindex);
  59. sublist.style.top = '0px';
  60. sublist.style.left = domhandler_1.DomHandler.getOuterWidth(item.children[0]) + 'px';
  61. }
  62. }
  63. };
  64. TieredMenuSub.prototype.itemClick = function (event, item, menuitem) {
  65. if (menuitem.disabled) {
  66. event.preventDefault();
  67. return true;
  68. }
  69. if (!menuitem.url) {
  70. event.preventDefault();
  71. }
  72. if (menuitem.command) {
  73. menuitem.command({
  74. originalEvent: event,
  75. item: item
  76. });
  77. }
  78. if (this.root && !this.activeItem && !this.tieredMenu.popup) {
  79. this.activeItem = item;
  80. var nextElement = item.children[0].nextElementSibling;
  81. if (nextElement) {
  82. var sublist = nextElement.children[0];
  83. if (this.autoZIndex) {
  84. sublist.style.zIndex = String(this.baseZIndex + (++domhandler_1.DomHandler.zindex));
  85. }
  86. sublist.style.zIndex = String(++domhandler_1.DomHandler.zindex);
  87. sublist.style.top = '0px';
  88. sublist.style.left = domhandler_1.DomHandler.getOuterWidth(item.children[0]) + 'px';
  89. this.rootItemClick = true;
  90. }
  91. }
  92. if (!menuitem.items && this.tieredMenu.popup) {
  93. this.tieredMenu.hide();
  94. }
  95. };
  96. TieredMenuSub.prototype.listClick = function (event) {
  97. if (!this.rootItemClick) {
  98. this.activeItem = null;
  99. }
  100. };
  101. TieredMenuSub.prototype.bindDocumentClickListener = function () {
  102. var _this = this;
  103. if (!this.documentClickListener) {
  104. this.documentClickListener = this.renderer.listen('document', 'click', function () {
  105. if (!_this.rootItemClick) {
  106. _this.parentActive = false;
  107. _this.activeItem = null;
  108. }
  109. _this.rootItemClick = false;
  110. });
  111. }
  112. };
  113. TieredMenuSub.prototype.unbindDocumentClickListener = function () {
  114. if (this.documentClickListener) {
  115. this.documentClickListener();
  116. this.documentClickListener = null;
  117. }
  118. };
  119. TieredMenuSub.prototype.ngOnDestroy = function () {
  120. if (this.root && !this.tieredMenu.popup) {
  121. this.unbindDocumentClickListener();
  122. }
  123. };
  124. __decorate([
  125. core_1.Input(),
  126. __metadata("design:type", Object)
  127. ], TieredMenuSub.prototype, "item", void 0);
  128. __decorate([
  129. core_1.Input(),
  130. __metadata("design:type", Boolean)
  131. ], TieredMenuSub.prototype, "root", void 0);
  132. __decorate([
  133. core_1.Input(),
  134. __metadata("design:type", Boolean)
  135. ], TieredMenuSub.prototype, "autoZIndex", void 0);
  136. __decorate([
  137. core_1.Input(),
  138. __metadata("design:type", Number)
  139. ], TieredMenuSub.prototype, "baseZIndex", void 0);
  140. __decorate([
  141. core_1.Input(),
  142. __metadata("design:type", Boolean),
  143. __metadata("design:paramtypes", [Object])
  144. ], TieredMenuSub.prototype, "parentActive", null);
  145. TieredMenuSub = __decorate([
  146. core_1.Component({
  147. selector: 'p-tieredMenuSub',
  148. template: "\n <ul [ngClass]=\"{'ui-widget-content ui-corner-all ui-shadow ui-submenu-list': !root}\" (click)=\"listClick($event)\">\n <ng-template ngFor let-child [ngForOf]=\"(root ? item : item.items)\">\n <li *ngIf=\"child.separator\" class=\"ui-menu-separator ui-widget-content\" [ngClass]=\"{'ui-helper-hidden': child.visible === false}\">\n <li *ngIf=\"!child.separator\" #listItem [ngClass]=\"{'ui-menuitem ui-widget ui-corner-all':true,'ui-menuitem-active':listItem==activeItem,'ui-helper-hidden': child.visible === false}\"\n [class]=\"child.styleClass\" [ngStyle]=\"child.style\"\n (mouseenter)=\"onItemMouseEnter($event, listItem, child)\">\n <a *ngIf=\"!child.routerLink\" [attr.href]=\"child.url\" class=\"ui-menuitem-link ui-corner-all\" [attr.target]=\"child.target\" [attr.tabindex]=\"child.tabindex ? child.tabindex : '0'\" [attr.title]=\"child.title\" [attr.id]=\"child.id\" \n [ngClass]=\"{'ui-state-disabled':child.disabled}\" (click)=\"itemClick($event, listItem, child)\">\n <span class=\"ui-menuitem-icon\" *ngIf=\"child.icon\" [ngClass]=\"child.icon\"></span>\n <span class=\"ui-menuitem-text\">{{child.label}}</span>\n <span class=\"ui-submenu-icon pi pi-fw pi-caret-right\" *ngIf=\"child.items\"></span>\n </a>\n <a *ngIf=\"child.routerLink\" [routerLink]=\"child.routerLink\" [queryParams]=\"child.queryParams\" [routerLinkActive]=\"'ui-state-active'\" [attr.tabindex]=\"child.tabindex ? child.tabindex : '0'\"\n [routerLinkActiveOptions]=\"child.routerLinkActiveOptions||{exact:false}\"\n class=\"ui-menuitem-link ui-corner-all\" [attr.target]=\"child.target\" [attr.title]=\"child.title\" [attr.id]=\"child.id\"\n [ngClass]=\"{'ui-state-disabled':child.disabled}\" (click)=\"itemClick($event, listItem, child)\">\n \n <span class=\"ui-menuitem-icon\" *ngIf=\"child.icon\" [ngClass]=\"child.icon\"></span>\n <span class=\"ui-menuitem-text\">{{child.label}}</span>\n <span class=\"ui-submenu-icon pi pi-fw pi-caret-right\" *ngIf=\"child.items\"></span>\n </a>\n <p-tieredMenuSub class=\"ui-submenu\" [item]=\"child\" *ngIf=\"child.items\" [baseZIndex]=\"baseZIndex\" [parentActive]=\"listItem==activeItem\" [autoZIndex]=\"autoZIndex\"></p-tieredMenuSub>\n </li>\n </ng-template>\n </ul>\n "
  149. }),
  150. __param(0, core_1.Inject(core_1.forwardRef(function () { return TieredMenu; }))),
  151. __metadata("design:paramtypes", [Object, core_1.ChangeDetectorRef, core_1.Renderer2])
  152. ], TieredMenuSub);
  153. return TieredMenuSub;
  154. }());
  155. exports.TieredMenuSub = TieredMenuSub;
  156. var TieredMenu = /** @class */ (function () {
  157. function TieredMenu(el, renderer) {
  158. this.el = el;
  159. this.renderer = renderer;
  160. this.autoZIndex = true;
  161. this.baseZIndex = 0;
  162. this.showTransitionOptions = '225ms ease-out';
  163. this.hideTransitionOptions = '195ms ease-in';
  164. }
  165. TieredMenu.prototype.toggle = function (event) {
  166. if (this.visible)
  167. this.hide();
  168. else
  169. this.show(event);
  170. this.preventDocumentDefault = true;
  171. };
  172. TieredMenu.prototype.show = function (event) {
  173. this.target = event.currentTarget;
  174. this.visible = true;
  175. this.parentActive = true;
  176. this.preventDocumentDefault = true;
  177. };
  178. TieredMenu.prototype.onOverlayAnimationStart = function (event) {
  179. switch (event.toState) {
  180. case 'visible':
  181. if (this.popup) {
  182. this.container = event.element;
  183. this.moveOnTop();
  184. this.appendOverlay();
  185. domhandler_1.DomHandler.absolutePosition(this.container, this.target);
  186. this.bindDocumentClickListener();
  187. this.bindDocumentResizeListener();
  188. }
  189. break;
  190. case 'void':
  191. this.onOverlayHide();
  192. break;
  193. }
  194. };
  195. TieredMenu.prototype.appendOverlay = function () {
  196. if (this.appendTo) {
  197. if (this.appendTo === 'body')
  198. document.body.appendChild(this.container);
  199. else
  200. domhandler_1.DomHandler.appendChild(this.container, this.appendTo);
  201. }
  202. };
  203. TieredMenu.prototype.restoreOverlayAppend = function () {
  204. if (this.container && this.appendTo) {
  205. this.el.nativeElement.appendChild(this.container);
  206. }
  207. };
  208. TieredMenu.prototype.moveOnTop = function () {
  209. if (this.autoZIndex) {
  210. this.container.style.zIndex = String(this.baseZIndex + (++domhandler_1.DomHandler.zindex));
  211. }
  212. };
  213. TieredMenu.prototype.hide = function () {
  214. this.visible = false;
  215. this.parentActive = false;
  216. };
  217. TieredMenu.prototype.onWindowResize = function () {
  218. this.hide();
  219. };
  220. TieredMenu.prototype.bindDocumentClickListener = function () {
  221. var _this = this;
  222. if (!this.documentClickListener) {
  223. this.documentClickListener = this.renderer.listen('document', 'click', function () {
  224. if (!_this.preventDocumentDefault && _this.popup) {
  225. _this.hide();
  226. }
  227. _this.preventDocumentDefault = false;
  228. });
  229. }
  230. };
  231. TieredMenu.prototype.unbindDocumentClickListener = function () {
  232. if (this.documentClickListener) {
  233. this.documentClickListener();
  234. this.documentClickListener = null;
  235. }
  236. };
  237. TieredMenu.prototype.bindDocumentResizeListener = function () {
  238. this.documentResizeListener = this.onWindowResize.bind(this);
  239. window.addEventListener('resize', this.documentResizeListener);
  240. };
  241. TieredMenu.prototype.unbindDocumentResizeListener = function () {
  242. if (this.documentResizeListener) {
  243. window.removeEventListener('resize', this.documentResizeListener);
  244. this.documentResizeListener = null;
  245. }
  246. };
  247. TieredMenu.prototype.onOverlayHide = function () {
  248. this.unbindDocumentClickListener();
  249. this.unbindDocumentResizeListener();
  250. this.preventDocumentDefault = false;
  251. this.target = null;
  252. };
  253. TieredMenu.prototype.ngOnDestroy = function () {
  254. if (this.popup) {
  255. this.restoreOverlayAppend();
  256. this.onOverlayHide();
  257. }
  258. };
  259. __decorate([
  260. core_1.Input(),
  261. __metadata("design:type", Array)
  262. ], TieredMenu.prototype, "model", void 0);
  263. __decorate([
  264. core_1.Input(),
  265. __metadata("design:type", Boolean)
  266. ], TieredMenu.prototype, "popup", void 0);
  267. __decorate([
  268. core_1.Input(),
  269. __metadata("design:type", Object)
  270. ], TieredMenu.prototype, "style", void 0);
  271. __decorate([
  272. core_1.Input(),
  273. __metadata("design:type", String)
  274. ], TieredMenu.prototype, "styleClass", void 0);
  275. __decorate([
  276. core_1.Input(),
  277. __metadata("design:type", Object)
  278. ], TieredMenu.prototype, "appendTo", void 0);
  279. __decorate([
  280. core_1.Input(),
  281. __metadata("design:type", Boolean)
  282. ], TieredMenu.prototype, "autoZIndex", void 0);
  283. __decorate([
  284. core_1.Input(),
  285. __metadata("design:type", Number)
  286. ], TieredMenu.prototype, "baseZIndex", void 0);
  287. __decorate([
  288. core_1.Input(),
  289. __metadata("design:type", String)
  290. ], TieredMenu.prototype, "showTransitionOptions", void 0);
  291. __decorate([
  292. core_1.Input(),
  293. __metadata("design:type", String)
  294. ], TieredMenu.prototype, "hideTransitionOptions", void 0);
  295. TieredMenu = __decorate([
  296. core_1.Component({
  297. selector: 'p-tieredMenu',
  298. template: "\n <div [ngClass]=\"{'ui-tieredmenu ui-widget ui-widget-content ui-corner-all':true, 'ui-tieredmenu-dynamic ui-shadow':popup}\" [class]=\"styleClass\" [ngStyle]=\"style\"\n [@overlayAnimation]=\"{value: 'visible', params: {showTransitionParams: showTransitionOptions, hideTransitionParams: hideTransitionOptions}}\" [@.disabled]=\"popup !== true\" (@overlayAnimation.start)=\"onOverlayAnimationStart($event)\" *ngIf=\"!popup || visible\">\n <p-tieredMenuSub [item]=\"model\" root=\"root\" [parentActive]=\"parentActive\" [baseZIndex]=\"baseZIndex\" [autoZIndex]=\"autoZIndex\"></p-tieredMenuSub>\n </div>\n ",
  299. animations: [
  300. animations_1.trigger('overlayAnimation', [
  301. animations_1.state('void', animations_1.style({
  302. transform: 'translateY(5%)',
  303. opacity: 0
  304. })),
  305. animations_1.state('visible', animations_1.style({
  306. transform: 'translateY(0)',
  307. opacity: 1
  308. })),
  309. animations_1.transition('void => visible', animations_1.animate('{{showTransitionParams}}')),
  310. animations_1.transition('visible => void', animations_1.animate('{{hideTransitionParams}}'))
  311. ])
  312. ]
  313. }),
  314. __metadata("design:paramtypes", [core_1.ElementRef, core_1.Renderer2])
  315. ], TieredMenu);
  316. return TieredMenu;
  317. }());
  318. exports.TieredMenu = TieredMenu;
  319. var TieredMenuModule = /** @class */ (function () {
  320. function TieredMenuModule() {
  321. }
  322. TieredMenuModule = __decorate([
  323. core_1.NgModule({
  324. imports: [common_1.CommonModule, router_1.RouterModule],
  325. exports: [TieredMenu, router_1.RouterModule],
  326. declarations: [TieredMenu, TieredMenuSub]
  327. })
  328. ], TieredMenuModule);
  329. return TieredMenuModule;
  330. }());
  331. exports.TieredMenuModule = TieredMenuModule;
  332. //# sourceMappingURL=tieredmenu.js.map