contextMenu.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
  4. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  5. var _base = require('./../_base');
  6. var _base2 = _interopRequireDefault(_base);
  7. var _pluginHooks = require('./../../pluginHooks');
  8. var _pluginHooks2 = _interopRequireDefault(_pluginHooks);
  9. var _array = require('./../../helpers/array');
  10. var _commandExecutor = require('./commandExecutor');
  11. var _commandExecutor2 = _interopRequireDefault(_commandExecutor);
  12. var _eventManager = require('./../../eventManager');
  13. var _eventManager2 = _interopRequireDefault(_eventManager);
  14. var _itemsFactory = require('./itemsFactory');
  15. var _itemsFactory2 = _interopRequireDefault(_itemsFactory);
  16. var _menu = require('./menu');
  17. var _menu2 = _interopRequireDefault(_menu);
  18. var _plugins = require('./../../plugins');
  19. var _event = require('./../../helpers/dom/event');
  20. var _element = require('./../../helpers/dom/element');
  21. var _predefinedItems = require('./predefinedItems');
  22. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  23. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  24. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  25. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  26. _pluginHooks2.default.getSingleton().register('afterContextMenuDefaultOptions');
  27. _pluginHooks2.default.getSingleton().register('afterContextMenuShow');
  28. _pluginHooks2.default.getSingleton().register('afterContextMenuHide');
  29. _pluginHooks2.default.getSingleton().register('afterContextMenuExecute');
  30. /**
  31. * @description
  32. * This plugin creates the Handsontable Context Menu. It allows to create a new row or
  33. * column at any place in the grid among [other features](http://docs.handsontable.com/demo-context-menu.html).
  34. * Possible values:
  35. * * `true` (to enable default options),
  36. * * `false` (to disable completely)
  37. *
  38. * or array of any available strings:
  39. * * `["row_above", "row_below", "col_left", "col_right",
  40. * "remove_row", "remove_col", "---------", "undo", "redo"]`.
  41. *
  42. * See [the context menu demo](http://docs.handsontable.com/demo-context-menu.html) for examples.
  43. *
  44. * @example
  45. * ```js
  46. * ...
  47. * // as a boolean
  48. * contextMenu: true
  49. * ...
  50. * // as a array
  51. * contextMenu: ['row_above', 'row_below', '---------', 'undo', 'redo']
  52. * ...
  53. * ```
  54. *
  55. * @plugin ContextMenu
  56. */
  57. var ContextMenu = function (_BasePlugin) {
  58. _inherits(ContextMenu, _BasePlugin);
  59. _createClass(ContextMenu, null, [{
  60. key: 'DEFAULT_ITEMS',
  61. /**
  62. * Default menu items order when `contextMenu` is enabled by `true`.
  63. *
  64. * @returns {Array}
  65. */
  66. get: function get() {
  67. return [_predefinedItems.ROW_ABOVE, _predefinedItems.ROW_BELOW, _predefinedItems.SEPARATOR, _predefinedItems.COLUMN_LEFT, _predefinedItems.COLUMN_RIGHT, _predefinedItems.SEPARATOR, _predefinedItems.REMOVE_ROW, _predefinedItems.REMOVE_COLUMN, _predefinedItems.SEPARATOR, _predefinedItems.UNDO, _predefinedItems.REDO, _predefinedItems.SEPARATOR, _predefinedItems.READ_ONLY, _predefinedItems.SEPARATOR, _predefinedItems.ALIGNMENT];
  68. }
  69. }]);
  70. function ContextMenu(hotInstance) {
  71. _classCallCheck(this, ContextMenu);
  72. /**
  73. * Instance of {@link EventManager}.
  74. *
  75. * @type {EventManager}
  76. */
  77. var _this = _possibleConstructorReturn(this, (ContextMenu.__proto__ || Object.getPrototypeOf(ContextMenu)).call(this, hotInstance));
  78. _this.eventManager = new _eventManager2.default(_this);
  79. /**
  80. * Instance of {@link CommandExecutor}.
  81. *
  82. * @type {CommandExecutor}
  83. */
  84. _this.commandExecutor = new _commandExecutor2.default(_this.hot);
  85. /**
  86. * Instance of {@link ItemsFactory}.
  87. *
  88. * @type {ItemsFactory}
  89. */
  90. _this.itemsFactory = null;
  91. /**
  92. * Instance of {@link Menu}.
  93. *
  94. * @type {Menu}
  95. */
  96. _this.menu = null;
  97. return _this;
  98. }
  99. /**
  100. * Check if the plugin is enabled in the Handsontable settings.
  101. *
  102. * @returns {Boolean}
  103. */
  104. _createClass(ContextMenu, [{
  105. key: 'isEnabled',
  106. value: function isEnabled() {
  107. return this.hot.getSettings().contextMenu;
  108. }
  109. /**
  110. * Enable plugin for this Handsontable instance.
  111. */
  112. }, {
  113. key: 'enablePlugin',
  114. value: function enablePlugin() {
  115. var _this2 = this;
  116. if (this.enabled) {
  117. return;
  118. }
  119. this.itemsFactory = new _itemsFactory2.default(this.hot, ContextMenu.DEFAULT_ITEMS);
  120. var settings = this.hot.getSettings().contextMenu;
  121. var predefinedItems = {
  122. items: this.itemsFactory.getItems(settings)
  123. };
  124. this.registerEvents();
  125. if (typeof settings.callback === 'function') {
  126. this.commandExecutor.setCommonCallback(settings.callback);
  127. }
  128. _get(ContextMenu.prototype.__proto__ || Object.getPrototypeOf(ContextMenu.prototype), 'enablePlugin', this).call(this);
  129. this.callOnPluginsReady(function () {
  130. _this2.hot.runHooks('afterContextMenuDefaultOptions', predefinedItems);
  131. _this2.itemsFactory.setPredefinedItems(predefinedItems.items);
  132. var menuItems = _this2.itemsFactory.getItems(settings);
  133. _this2.menu = new _menu2.default(_this2.hot, {
  134. className: 'htContextMenu',
  135. keepInViewport: true
  136. });
  137. _this2.hot.runHooks('beforeContextMenuSetItems', menuItems);
  138. _this2.menu.setMenuItems(menuItems);
  139. _this2.menu.addLocalHook('afterOpen', function () {
  140. return _this2.onMenuAfterOpen();
  141. });
  142. _this2.menu.addLocalHook('afterClose', function () {
  143. return _this2.onMenuAfterClose();
  144. });
  145. _this2.menu.addLocalHook('executeCommand', function () {
  146. for (var _len = arguments.length, params = Array(_len), _key = 0; _key < _len; _key++) {
  147. params[_key] = arguments[_key];
  148. }
  149. return _this2.executeCommand.apply(_this2, params);
  150. });
  151. // Register all commands. Predefined and added by user or by plugins
  152. (0, _array.arrayEach)(menuItems, function (command) {
  153. return _this2.commandExecutor.registerCommand(command.key, command);
  154. });
  155. });
  156. }
  157. /**
  158. * Updates the plugin to use the latest options you have specified.
  159. */
  160. }, {
  161. key: 'updatePlugin',
  162. value: function updatePlugin() {
  163. this.disablePlugin();
  164. this.enablePlugin();
  165. _get(ContextMenu.prototype.__proto__ || Object.getPrototypeOf(ContextMenu.prototype), 'updatePlugin', this).call(this);
  166. }
  167. /**
  168. * Disable plugin for this Handsontable instance.
  169. */
  170. }, {
  171. key: 'disablePlugin',
  172. value: function disablePlugin() {
  173. this.close();
  174. if (this.menu) {
  175. this.menu.destroy();
  176. this.menu = null;
  177. }
  178. _get(ContextMenu.prototype.__proto__ || Object.getPrototypeOf(ContextMenu.prototype), 'disablePlugin', this).call(this);
  179. }
  180. /**
  181. * Register dom listeners.
  182. *
  183. * @private
  184. */
  185. }, {
  186. key: 'registerEvents',
  187. value: function registerEvents() {
  188. var _this3 = this;
  189. this.eventManager.addEventListener(this.hot.rootElement, 'contextmenu', function (event) {
  190. return _this3.onContextMenu(event);
  191. });
  192. }
  193. /**
  194. * Open menu and re-position it based on dom event object.
  195. *
  196. * @param {Event} event The event object.
  197. */
  198. }, {
  199. key: 'open',
  200. value: function open(event) {
  201. if (!this.menu) {
  202. return;
  203. }
  204. this.menu.open();
  205. this.menu.setPosition({
  206. top: parseInt((0, _event.pageY)(event), 10) - (0, _element.getWindowScrollTop)(),
  207. left: parseInt((0, _event.pageX)(event), 10) - (0, _element.getWindowScrollLeft)()
  208. });
  209. // ContextMenu is not detected HotTableEnv correctly because is injected outside hot-table
  210. this.menu.hotMenu.isHotTableEnv = this.hot.isHotTableEnv;
  211. // Handsontable.eventManager.isHotTableEnv = this.hot.isHotTableEnv;
  212. }
  213. /**
  214. * Close menu.
  215. */
  216. }, {
  217. key: 'close',
  218. value: function close() {
  219. if (!this.menu) {
  220. return;
  221. }
  222. this.menu.close();
  223. }
  224. /**
  225. * Execute context menu command.
  226. *
  227. * You can execute all predefined commands:
  228. * * `'row_above'` - Insert row above
  229. * * `'row_below'` - Insert row below
  230. * * `'col_left'` - Insert column on the left
  231. * * `'col_right'` - Insert column on the right
  232. * * `'clear_column'` - Clear selected column
  233. * * `'remove_row'` - Remove row
  234. * * `'remove_col'` - Remove column
  235. * * `'undo'` - Undo last action
  236. * * `'redo'` - Redo last action
  237. * * `'make_read_only'` - Make cell read only
  238. * * `'alignment:left'` - Alignment to the left
  239. * * `'alignment:top'` - Alignment to the top
  240. * * `'alignment:right'` - Alignment to the right
  241. * * `'alignment:bottom'` - Alignment to the bottom
  242. * * `'alignment:middle'` - Alignment to the middle
  243. * * `'alignment:center'` - Alignment to the center (justify)
  244. *
  245. * Or you can execute command registered in settings where `key` is your command name.
  246. *
  247. * @param {String} commandName
  248. * @param {*} params
  249. */
  250. }, {
  251. key: 'executeCommand',
  252. value: function executeCommand() {
  253. for (var _len2 = arguments.length, params = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  254. params[_key2] = arguments[_key2];
  255. }
  256. this.commandExecutor.execute.apply(this.commandExecutor, params);
  257. }
  258. /**
  259. * On context menu listener.
  260. *
  261. * @private
  262. * @param {Event} event
  263. */
  264. }, {
  265. key: 'onContextMenu',
  266. value: function onContextMenu(event) {
  267. var settings = this.hot.getSettings();
  268. var showRowHeaders = settings.rowHeaders;
  269. var showColHeaders = settings.colHeaders;
  270. function isValidElement(element) {
  271. return element.nodeName === 'TD' || element.parentNode.nodeName === 'TD';
  272. }
  273. // if event is from hot-table we must get web component element not element inside him
  274. var element = event.realTarget;
  275. this.close();
  276. if ((0, _element.hasClass)(element, 'handsontableInput')) {
  277. return;
  278. }
  279. event.preventDefault();
  280. (0, _event.stopPropagation)(event);
  281. if (!(showRowHeaders || showColHeaders)) {
  282. if (!isValidElement(element) && !((0, _element.hasClass)(element, 'current') && (0, _element.hasClass)(element, 'wtBorder'))) {
  283. return;
  284. }
  285. }
  286. this.open(event);
  287. }
  288. /**
  289. * On menu after open listener.
  290. *
  291. * @private
  292. */
  293. }, {
  294. key: 'onMenuAfterOpen',
  295. value: function onMenuAfterOpen() {
  296. this.hot.runHooks('afterContextMenuShow', this);
  297. }
  298. /**
  299. * On menu after close listener.
  300. *
  301. * @private
  302. */
  303. }, {
  304. key: 'onMenuAfterClose',
  305. value: function onMenuAfterClose() {
  306. this.hot.listen();
  307. this.hot.runHooks('afterContextMenuHide', this);
  308. }
  309. /**
  310. * Destroy instance.
  311. */
  312. }, {
  313. key: 'destroy',
  314. value: function destroy() {
  315. this.close();
  316. if (this.menu) {
  317. this.menu.destroy();
  318. }
  319. _get(ContextMenu.prototype.__proto__ || Object.getPrototypeOf(ContextMenu.prototype), 'destroy', this).call(this);
  320. }
  321. }]);
  322. return ContextMenu;
  323. }(_base2.default);
  324. ContextMenu.SEPARATOR = {
  325. name: _predefinedItems.SEPARATOR
  326. };
  327. (0, _plugins.registerPlugin)('contextMenu', ContextMenu);
  328. exports.default = ContextMenu;