Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ContextMenuTrigger.js 7.6 KiB

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. 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; }; }();
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. 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; }
  4. 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; }
  5. import React, { Component } from 'react';
  6. import PropTypes from 'prop-types';
  7. import cx from 'classnames';
  8. import assign from 'object-assign';
  9. import { showMenu, hideMenu } from './actions';
  10. import { callIfExists, cssClasses } from './helpers';
  11. var ContextMenuTrigger = function (_Component) {
  12. _inherits(ContextMenuTrigger, _Component);
  13. function ContextMenuTrigger() {
  14. var _ref;
  15. var _temp, _this, _ret;
  16. _classCallCheck(this, ContextMenuTrigger);
  17. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  18. args[_key] = arguments[_key];
  19. }
  20. return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = ContextMenuTrigger.__proto__ || Object.getPrototypeOf(ContextMenuTrigger)).call.apply(_ref, [this].concat(args))), _this), _this.touchHandled = false, _this.handleMouseDown = function (event) {
  21. if (_this.props.holdToDisplay >= 0 && event.button === 0) {
  22. event.persist();
  23. event.stopPropagation();
  24. _this.mouseDownTimeoutId = setTimeout(function () {
  25. return _this.handleContextClick(event);
  26. }, _this.props.holdToDisplay);
  27. }
  28. callIfExists(_this.props.attributes.onMouseDown, event);
  29. }, _this.handleMouseUp = function (event) {
  30. if (event.button === 0) {
  31. clearTimeout(_this.mouseDownTimeoutId);
  32. }
  33. callIfExists(_this.props.attributes.onMouseUp, event);
  34. }, _this.handleMouseOut = function (event) {
  35. if (event.button === 0) {
  36. clearTimeout(_this.mouseDownTimeoutId);
  37. }
  38. callIfExists(_this.props.attributes.onMouseOut, event);
  39. }, _this.handleTouchstart = function (event) {
  40. _this.touchHandled = false;
  41. if (_this.props.holdToDisplay >= 0) {
  42. event.persist();
  43. event.stopPropagation();
  44. _this.touchstartTimeoutId = setTimeout(function () {
  45. _this.handleContextClick(event);
  46. _this.touchHandled = true;
  47. }, _this.props.holdToDisplay);
  48. }
  49. callIfExists(_this.props.attributes.onTouchStart, event);
  50. }, _this.handleTouchEnd = function (event) {
  51. if (_this.touchHandled) {
  52. event.preventDefault();
  53. }
  54. clearTimeout(_this.touchstartTimeoutId);
  55. callIfExists(_this.props.attributes.onTouchEnd, event);
  56. }, _this.handleContextMenu = function (event) {
  57. if (event.button === _this.props.mouseButton) {
  58. _this.handleContextClick(event);
  59. }
  60. callIfExists(_this.props.attributes.onContextMenu, event);
  61. }, _this.handleMouseClick = function (event) {
  62. if (event.button === _this.props.mouseButton) {
  63. _this.handleContextClick(event);
  64. }
  65. callIfExists(_this.props.attributes.onClick, event);
  66. }, _this.handleContextClick = function (event) {
  67. if (_this.props.disable) return;
  68. if (_this.props.disableIfShiftIsPressed && event.shiftKey) return;
  69. event.preventDefault();
  70. event.stopPropagation();
  71. var x = event.clientX || event.touches && event.touches[0].pageX;
  72. var y = event.clientY || event.touches && event.touches[0].pageY;
  73. if (_this.props.posX) {
  74. x -= _this.props.posX;
  75. }
  76. if (_this.props.posY) {
  77. y -= _this.props.posY;
  78. }
  79. hideMenu();
  80. var data = callIfExists(_this.props.collect, _this.props);
  81. var showMenuConfig = {
  82. position: { x: x, y: y },
  83. target: _this.elem,
  84. id: _this.props.id
  85. };
  86. if (data && typeof data.then === 'function') {
  87. // it's promise
  88. data.then(function (resp) {
  89. showMenuConfig.data = assign({}, resp, {
  90. target: event.target
  91. });
  92. showMenu(showMenuConfig);
  93. });
  94. } else {
  95. showMenuConfig.data = assign({}, data, {
  96. target: event.target
  97. });
  98. showMenu(showMenuConfig);
  99. }
  100. }, _this.elemRef = function (c) {
  101. _this.elem = c;
  102. }, _temp), _possibleConstructorReturn(_this, _ret);
  103. }
  104. _createClass(ContextMenuTrigger, [{
  105. key: 'render',
  106. value: function render() {
  107. var _props = this.props,
  108. renderTag = _props.renderTag,
  109. attributes = _props.attributes,
  110. children = _props.children;
  111. var newAttrs = assign({}, attributes, {
  112. className: cx(cssClasses.menuWrapper, attributes.className),
  113. onContextMenu: this.handleContextMenu,
  114. onClick: this.handleMouseClick,
  115. onMouseDown: this.handleMouseDown,
  116. onMouseUp: this.handleMouseUp,
  117. onTouchStart: this.handleTouchstart,
  118. onTouchEnd: this.handleTouchEnd,
  119. onMouseOut: this.handleMouseOut,
  120. ref: this.elemRef
  121. });
  122. return React.createElement(renderTag, newAttrs, children);
  123. }
  124. }]);
  125. return ContextMenuTrigger;
  126. }(Component);
  127. ContextMenuTrigger.propTypes = {
  128. id: PropTypes.string.isRequired,
  129. children: PropTypes.node.isRequired,
  130. attributes: PropTypes.object,
  131. collect: PropTypes.func,
  132. disable: PropTypes.bool,
  133. holdToDisplay: PropTypes.number,
  134. posX: PropTypes.number,
  135. posY: PropTypes.number,
  136. renderTag: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
  137. mouseButton: PropTypes.number,
  138. disableIfShiftIsPressed: PropTypes.bool
  139. };
  140. ContextMenuTrigger.defaultProps = {
  141. attributes: {},
  142. collect: function collect() {
  143. return null;
  144. },
  145. disable: false,
  146. holdToDisplay: 1000,
  147. renderTag: 'div',
  148. posX: 0,
  149. posY: 0,
  150. mouseButton: 2, // 0 is left click, 2 is right click
  151. disableIfShiftIsPressed: false
  152. };
  153. export default ContextMenuTrigger;