contextmenu.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 common_1 = require("@angular/common");
  17. var domhandler_1 = require("../dom/domhandler");
  18. var router_1 = require("@angular/router");
  19. var ContextMenuSub = /** @class */ (function () {
  20. function ContextMenuSub(contextMenu) {
  21. this.contextMenu = contextMenu;
  22. }
  23. Object.defineProperty(ContextMenuSub.prototype, "parentActive", {
  24. get: function () {
  25. return this._parentActive;
  26. },
  27. set: function (value) {
  28. this._parentActive = value;
  29. if (!value) {
  30. this.activeItem = null;
  31. }
  32. },
  33. enumerable: true,
  34. configurable: true
  35. });
  36. ContextMenuSub.prototype.onItemMouseEnter = function (event, item, menuitem) {
  37. if (this.hideTimeout) {
  38. clearTimeout(this.hideTimeout);
  39. this.hideTimeout = null;
  40. }
  41. this.activeItem = item;
  42. if (menuitem.disabled) {
  43. return;
  44. }
  45. var nextElement = item.children[0].nextElementSibling;
  46. if (nextElement) {
  47. var sublist = nextElement.children[0];
  48. sublist.style.zIndex = ++domhandler_1.DomHandler.zindex;
  49. this.position(sublist, item);
  50. }
  51. };
  52. ContextMenuSub.prototype.itemClick = function (event, item) {
  53. if (item.disabled) {
  54. event.preventDefault();
  55. return;
  56. }
  57. if (!item.url) {
  58. event.preventDefault();
  59. }
  60. if (item.command) {
  61. item.command({
  62. originalEvent: event,
  63. item: item
  64. });
  65. }
  66. };
  67. ContextMenuSub.prototype.listClick = function (event) {
  68. this.activeItem = null;
  69. };
  70. ContextMenuSub.prototype.position = function (sublist, item) {
  71. this.containerOffset = domhandler_1.DomHandler.getOffset(item.parentElement);
  72. var viewport = domhandler_1.DomHandler.getViewport();
  73. var sublistWidth = sublist.offsetParent ? sublist.offsetWidth : domhandler_1.DomHandler.getHiddenElementOuterWidth(sublist);
  74. var itemOuterWidth = domhandler_1.DomHandler.getOuterWidth(item.children[0]);
  75. var itemOuterHeight = domhandler_1.DomHandler.getOuterHeight(item.children[0]);
  76. var sublistHeight = sublist.offsetHeight ? sublist.offsetHeight : domhandler_1.DomHandler.getHiddenElementOuterHeight(sublist);
  77. if ((parseInt(this.containerOffset.top) + itemOuterHeight + sublistHeight) > (viewport.height - domhandler_1.DomHandler.calculateScrollbarHeight())) {
  78. sublist.style.bottom = '0px';
  79. }
  80. else {
  81. sublist.style.top = '0px';
  82. }
  83. if ((parseInt(this.containerOffset.left) + itemOuterWidth + sublistWidth) > (viewport.width - domhandler_1.DomHandler.calculateScrollbarWidth())) {
  84. sublist.style.left = -sublistWidth + 'px';
  85. }
  86. else {
  87. sublist.style.left = itemOuterWidth + 'px';
  88. }
  89. };
  90. __decorate([
  91. core_1.Input(),
  92. __metadata("design:type", Object)
  93. ], ContextMenuSub.prototype, "item", void 0);
  94. __decorate([
  95. core_1.Input(),
  96. __metadata("design:type", Boolean)
  97. ], ContextMenuSub.prototype, "root", void 0);
  98. __decorate([
  99. core_1.Input(),
  100. __metadata("design:type", Boolean),
  101. __metadata("design:paramtypes", [Object])
  102. ], ContextMenuSub.prototype, "parentActive", null);
  103. ContextMenuSub = __decorate([
  104. core_1.Component({
  105. selector: 'p-contextMenuSub',
  106. template: "\n <ul [ngClass]=\"{'ui-widget-content ui-corner-all ui-submenu-list ui-shadow':!root}\" class=\"ui-menu-list\" (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\" #item [ngClass]=\"{'ui-menuitem ui-corner-all':true,'ui-menuitem-active':item==activeItem,'ui-helper-hidden': child.visible === false}\"\n (mouseenter)=\"onItemMouseEnter($event,item,child)\">\n <a *ngIf=\"!child.routerLink\" [href]=\"child.url||'#'\" [attr.target]=\"child.target\" [attr.title]=\"child.title\" [attr.id]=\"child.id\" [attr.tabindex]=\"child.tabindex ? child.tabindex : '0'\" (click)=\"itemClick($event, child)\"\n [ngClass]=\"{'ui-menuitem-link ui-corner-all':true,'ui-state-disabled':child.disabled}\" [ngStyle]=\"child.style\" [class]=\"child.styleClass\">\n <span class=\"ui-submenu-icon pi pi-fw pi-caret-right\" *ngIf=\"child.items\"></span>\n <span class=\"ui-menuitem-icon\" *ngIf=\"child.icon\" [ngClass]=\"child.icon\"></span>\n <span class=\"ui-menuitem-text\">{{child.label}}</span>\n </a>\n <a *ngIf=\"child.routerLink\" [routerLink]=\"child.routerLink\" [queryParams]=\"child.queryParams\" [routerLinkActive]=\"'ui-state-active'\"\n [routerLinkActiveOptions]=\"child.routerLinkActiveOptions||{exact:false}\" [attr.target]=\"child.target\" [attr.title]=\"child.title\" [attr.id]=\"child.id\" [attr.tabindex]=\"child.tabindex ? child.tabindex : '0'\"\n (click)=\"itemClick($event, child)\" [ngClass]=\"{'ui-menuitem-link ui-corner-all':true,'ui-state-disabled':child.disabled}\"\n [ngStyle]=\"child.style\" [class]=\"child.styleClass\">\n <span class=\"ui-submenu-icon pi pi-fw pi-caret-right\" *ngIf=\"child.items\"></span>\n <span class=\"ui-menuitem-icon\" *ngIf=\"child.icon\" [ngClass]=\"child.icon\"></span>\n <span class=\"ui-menuitem-text\">{{child.label}}</span>\n </a>\n <p-contextMenuSub class=\"ui-submenu\" [parentActive]=\"item==activeItem\" [item]=\"child\" *ngIf=\"child.items\"></p-contextMenuSub>\n </li>\n </ng-template>\n </ul>\n "
  107. }),
  108. __param(0, core_1.Inject(core_1.forwardRef(function () { return ContextMenu; }))),
  109. __metadata("design:paramtypes", [Object])
  110. ], ContextMenuSub);
  111. return ContextMenuSub;
  112. }());
  113. exports.ContextMenuSub = ContextMenuSub;
  114. var ContextMenu = /** @class */ (function () {
  115. function ContextMenu(el, renderer, zone) {
  116. this.el = el;
  117. this.renderer = renderer;
  118. this.zone = zone;
  119. this.autoZIndex = true;
  120. this.baseZIndex = 0;
  121. this.triggerEvent = 'contextmenu';
  122. this.onShow = new core_1.EventEmitter();
  123. this.onHide = new core_1.EventEmitter();
  124. }
  125. ContextMenu.prototype.ngAfterViewInit = function () {
  126. var _this = this;
  127. if (this.global) {
  128. this.triggerEventListener = this.renderer.listen('document', this.triggerEvent, function (event) {
  129. _this.show(event);
  130. event.preventDefault();
  131. });
  132. }
  133. else if (this.target) {
  134. this.triggerEventListener = this.renderer.listen(this.target, this.triggerEvent, function (event) {
  135. _this.show(event);
  136. event.preventDefault();
  137. event.stopPropagation();
  138. });
  139. }
  140. if (this.appendTo) {
  141. if (this.appendTo === 'body')
  142. document.body.appendChild(this.containerViewChild.nativeElement);
  143. else
  144. domhandler_1.DomHandler.appendChild(this.containerViewChild.nativeElement, this.appendTo);
  145. }
  146. };
  147. ContextMenu.prototype.show = function (event) {
  148. this.position(event);
  149. this.moveOnTop();
  150. this.containerViewChild.nativeElement.style.display = 'block';
  151. this.parentActive = true;
  152. domhandler_1.DomHandler.fadeIn(this.containerViewChild.nativeElement, 250);
  153. this.bindGlobalListeners();
  154. if (event) {
  155. event.preventDefault();
  156. }
  157. this.onShow.emit();
  158. };
  159. ContextMenu.prototype.hide = function () {
  160. this.containerViewChild.nativeElement.style.display = 'none';
  161. this.parentActive = false;
  162. this.unbindGlobalListeners();
  163. this.onHide.emit();
  164. };
  165. ContextMenu.prototype.moveOnTop = function () {
  166. if (this.autoZIndex) {
  167. this.containerViewChild.nativeElement.style.zIndex = String(this.baseZIndex + (++domhandler_1.DomHandler.zindex));
  168. }
  169. };
  170. ContextMenu.prototype.toggle = function (event) {
  171. if (this.containerViewChild.nativeElement.offsetParent)
  172. this.hide();
  173. else
  174. this.show(event);
  175. };
  176. ContextMenu.prototype.position = function (event) {
  177. if (event) {
  178. var left = event.pageX + 1;
  179. var top_1 = event.pageY + 1;
  180. var width = this.containerViewChild.nativeElement.offsetParent ? this.containerViewChild.nativeElement.offsetWidth : domhandler_1.DomHandler.getHiddenElementOuterWidth(this.containerViewChild.nativeElement);
  181. var height = this.containerViewChild.nativeElement.offsetParent ? this.containerViewChild.nativeElement.offsetHeight : domhandler_1.DomHandler.getHiddenElementOuterHeight(this.containerViewChild.nativeElement);
  182. var viewport = domhandler_1.DomHandler.getViewport();
  183. //flip
  184. if (left + width - document.body.scrollLeft > viewport.width) {
  185. left -= width;
  186. }
  187. //flip
  188. if (top_1 + height - document.body.scrollTop > viewport.height) {
  189. top_1 -= height;
  190. }
  191. //fit
  192. if (left < document.body.scrollLeft) {
  193. left = document.body.scrollLeft;
  194. }
  195. //fit
  196. if (top_1 < document.body.scrollTop) {
  197. top_1 = document.body.scrollTop;
  198. }
  199. this.containerViewChild.nativeElement.style.left = left + 'px';
  200. this.containerViewChild.nativeElement.style.top = top_1 + 'px';
  201. }
  202. };
  203. ContextMenu.prototype.bindGlobalListeners = function () {
  204. var _this = this;
  205. if (!this.documentClickListener) {
  206. this.documentClickListener = this.renderer.listen('document', 'click', function (event) {
  207. if (_this.containerViewChild.nativeElement.offsetParent && event.button !== 2) {
  208. _this.hide();
  209. }
  210. });
  211. }
  212. this.zone.runOutsideAngular(function () {
  213. if (!_this.windowResizeListener) {
  214. _this.windowResizeListener = _this.onWindowResize.bind(_this);
  215. window.addEventListener('resize', _this.windowResizeListener);
  216. }
  217. });
  218. };
  219. ContextMenu.prototype.unbindGlobalListeners = function () {
  220. if (this.documentClickListener) {
  221. this.documentClickListener();
  222. this.documentClickListener = null;
  223. }
  224. if (this.windowResizeListener) {
  225. window.removeEventListener('resize', this.windowResizeListener);
  226. this.windowResizeListener = null;
  227. }
  228. };
  229. ContextMenu.prototype.onWindowResize = function (event) {
  230. if (this.containerViewChild.nativeElement.offsetParent) {
  231. this.hide();
  232. }
  233. };
  234. ContextMenu.prototype.ngOnDestroy = function () {
  235. this.unbindGlobalListeners();
  236. if (this.triggerEventListener) {
  237. this.triggerEventListener();
  238. }
  239. if (this.appendTo) {
  240. this.el.nativeElement.appendChild(this.containerViewChild.nativeElement);
  241. }
  242. };
  243. __decorate([
  244. core_1.Input(),
  245. __metadata("design:type", Array)
  246. ], ContextMenu.prototype, "model", void 0);
  247. __decorate([
  248. core_1.Input(),
  249. __metadata("design:type", Boolean)
  250. ], ContextMenu.prototype, "global", void 0);
  251. __decorate([
  252. core_1.Input(),
  253. __metadata("design:type", Object)
  254. ], ContextMenu.prototype, "target", void 0);
  255. __decorate([
  256. core_1.Input(),
  257. __metadata("design:type", Object)
  258. ], ContextMenu.prototype, "style", void 0);
  259. __decorate([
  260. core_1.Input(),
  261. __metadata("design:type", String)
  262. ], ContextMenu.prototype, "styleClass", void 0);
  263. __decorate([
  264. core_1.Input(),
  265. __metadata("design:type", Object)
  266. ], ContextMenu.prototype, "appendTo", void 0);
  267. __decorate([
  268. core_1.Input(),
  269. __metadata("design:type", Boolean)
  270. ], ContextMenu.prototype, "autoZIndex", void 0);
  271. __decorate([
  272. core_1.Input(),
  273. __metadata("design:type", Number)
  274. ], ContextMenu.prototype, "baseZIndex", void 0);
  275. __decorate([
  276. core_1.Input(),
  277. __metadata("design:type", String)
  278. ], ContextMenu.prototype, "triggerEvent", void 0);
  279. __decorate([
  280. core_1.Output(),
  281. __metadata("design:type", core_1.EventEmitter)
  282. ], ContextMenu.prototype, "onShow", void 0);
  283. __decorate([
  284. core_1.Output(),
  285. __metadata("design:type", core_1.EventEmitter)
  286. ], ContextMenu.prototype, "onHide", void 0);
  287. __decorate([
  288. core_1.ViewChild('container', { static: false }),
  289. __metadata("design:type", core_1.ElementRef)
  290. ], ContextMenu.prototype, "containerViewChild", void 0);
  291. ContextMenu = __decorate([
  292. core_1.Component({
  293. selector: 'p-contextMenu',
  294. template: "\n <div #container [ngClass]=\"'ui-contextmenu ui-widget ui-widget-content ui-corner-all ui-shadow'\"\n [class]=\"styleClass\" [ngStyle]=\"style\">\n <p-contextMenuSub [item]=\"model\" [parentActive]=\"parentActive\" root=\"root\"></p-contextMenuSub>\n </div>\n "
  295. }),
  296. __metadata("design:paramtypes", [core_1.ElementRef, core_1.Renderer2, core_1.NgZone])
  297. ], ContextMenu);
  298. return ContextMenu;
  299. }());
  300. exports.ContextMenu = ContextMenu;
  301. var ContextMenuModule = /** @class */ (function () {
  302. function ContextMenuModule() {
  303. }
  304. ContextMenuModule = __decorate([
  305. core_1.NgModule({
  306. imports: [common_1.CommonModule, router_1.RouterModule],
  307. exports: [ContextMenu, router_1.RouterModule],
  308. declarations: [ContextMenu, ContextMenuSub]
  309. })
  310. ], ContextMenuModule);
  311. return ContextMenuModule;
  312. }());
  313. exports.ContextMenuModule = ContextMenuModule;
  314. //# sourceMappingURL=contextmenu.js.map