context_menu.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. exports.ContextMenu = void 0;
  17. var abstract_menu_js_1 = require("./abstract_menu.js");
  18. var html_classes_js_1 = require("./html_classes.js");
  19. var menu_store_js_1 = require("./menu_store.js");
  20. var variable_pool_js_1 = require("./variable_pool.js");
  21. var ContextMenu = (function (_super) {
  22. __extends(ContextMenu, _super);
  23. function ContextMenu(factory) {
  24. var _this = _super.call(this) || this;
  25. _this.factory = factory;
  26. _this.id = '';
  27. _this.moving = false;
  28. _this._store = new menu_store_js_1.MenuStore(_this);
  29. _this.widgets = [];
  30. _this.variablePool = new variable_pool_js_1.VariablePool();
  31. return _this;
  32. }
  33. ContextMenu.fromJson = function (factory, _a) {
  34. var pool = _a.pool, items = _a.items, _b = _a.id, id = _b === void 0 ? '' : _b;
  35. var ctxtMenu = new this(factory);
  36. ctxtMenu.id = id;
  37. var varParser = factory.get('variable');
  38. pool.forEach(function (x) { return varParser(factory, x, ctxtMenu.pool); });
  39. var itemList = factory.get('items')(factory, items, ctxtMenu);
  40. ctxtMenu.items = itemList;
  41. return ctxtMenu;
  42. };
  43. ContextMenu.prototype.generateHtml = function () {
  44. if (this.isPosted()) {
  45. this.unpost();
  46. }
  47. _super.prototype.generateHtml.call(this);
  48. this._frame = document.createElement('div');
  49. this._frame.classList.add(html_classes_js_1.HtmlClasses['MENUFRAME']);
  50. var styleString = 'left: 0px; top: 0px; z-index: 200; width: 100%; ' +
  51. 'height: 100%; border: 0px; padding: 0px; margin: 0px;';
  52. this._frame.setAttribute('style', 'position: absolute; ' + styleString);
  53. var innerDiv = document.createElement('div');
  54. innerDiv.setAttribute('style', 'position: fixed; ' + styleString);
  55. this._frame.appendChild(innerDiv);
  56. innerDiv.addEventListener('mousedown', function (event) {
  57. this.unpost();
  58. this.unpostWidgets();
  59. this.stop(event);
  60. }.bind(this));
  61. };
  62. ContextMenu.prototype.display = function () {
  63. document.body.appendChild(this.frame);
  64. this.frame.appendChild(this.html);
  65. this.focus();
  66. };
  67. ContextMenu.prototype.escape = function (_event) {
  68. this.unpost();
  69. this.unpostWidgets();
  70. };
  71. ContextMenu.prototype.unpost = function () {
  72. _super.prototype.unpost.call(this);
  73. if (this.widgets.length > 0) {
  74. return;
  75. }
  76. this.frame.parentNode.removeChild(this.frame);
  77. var store = this.store;
  78. if (!this.moving) {
  79. store.insertTaborder();
  80. }
  81. store.active.focus();
  82. };
  83. ContextMenu.prototype.left = function (_event) {
  84. this.move_(this.store.previous());
  85. };
  86. ContextMenu.prototype.right = function (_event) {
  87. this.move_(this.store.next());
  88. };
  89. Object.defineProperty(ContextMenu.prototype, "frame", {
  90. get: function () {
  91. return this._frame;
  92. },
  93. enumerable: false,
  94. configurable: true
  95. });
  96. Object.defineProperty(ContextMenu.prototype, "store", {
  97. get: function () {
  98. return this._store;
  99. },
  100. enumerable: false,
  101. configurable: true
  102. });
  103. ContextMenu.prototype.post = function (numberOrEvent, isY) {
  104. if (typeof (isY) !== 'undefined') {
  105. if (!this.moving) {
  106. this.store.removeTaborder();
  107. }
  108. _super.prototype.post.call(this, numberOrEvent, isY);
  109. return;
  110. }
  111. var event = numberOrEvent;
  112. var node;
  113. if (event instanceof Event) {
  114. node = event.target;
  115. this.stop(event);
  116. }
  117. else {
  118. node = event;
  119. }
  120. var x;
  121. var y;
  122. if (event instanceof MouseEvent) {
  123. x = event.pageX, y = event.pageY;
  124. if (!x && !y && event.clientX) {
  125. x = event.clientX + document.body.scrollLeft +
  126. document.documentElement.scrollLeft;
  127. y = event.clientY + document.body.scrollTop +
  128. document.documentElement.scrollTop;
  129. }
  130. }
  131. if (!x && !y && node) {
  132. var offsetX = window.pageXOffset || document.documentElement.scrollLeft;
  133. var offsetY = window.pageYOffset || document.documentElement.scrollTop;
  134. var rect = node.getBoundingClientRect();
  135. x = (rect.right + rect.left) / 2 + offsetX;
  136. y = (rect.bottom + rect.top) / 2 + offsetY;
  137. }
  138. this.store.active = node;
  139. this.anchor = this.store.active;
  140. var menu = this.html;
  141. var margin = 5;
  142. if (x + menu.offsetWidth > document.body.offsetWidth - margin) {
  143. x = document.body.offsetWidth - menu.offsetWidth - margin;
  144. }
  145. this.post(x, y);
  146. };
  147. ContextMenu.prototype.registerWidget = function (widget) {
  148. this.widgets.push(widget);
  149. };
  150. ContextMenu.prototype.unregisterWidget = function (widget) {
  151. var index = this.widgets.indexOf(widget);
  152. if (index > -1) {
  153. this.widgets.splice(index, 1);
  154. }
  155. if (this.widgets.length === 0) {
  156. this.unpost();
  157. }
  158. };
  159. ContextMenu.prototype.unpostWidgets = function () {
  160. this.widgets.forEach(function (x) { return x.unpost(); });
  161. };
  162. ContextMenu.prototype.toJson = function () {
  163. return { type: '' };
  164. };
  165. ContextMenu.prototype.move_ = function (next) {
  166. if (this.anchor && next !== this.anchor) {
  167. this.moving = true;
  168. this.unpost();
  169. this.post(next);
  170. this.moving = false;
  171. }
  172. };
  173. return ContextMenu;
  174. }(abstract_menu_js_1.AbstractMenu));
  175. exports.ContextMenu = ContextMenu;
  176. //# sourceMappingURL=context_menu.js.map