Dragger.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. import _extends from 'babel-runtime/helpers/extends';
  2. import _Object$getPrototypeOf from 'babel-runtime/core-js/object/get-prototype-of';
  3. import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
  4. import _createClass from 'babel-runtime/helpers/createClass';
  5. import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
  6. import _inherits from 'babel-runtime/helpers/inherits';
  7. import React from 'react';
  8. import PropTypes from 'prop-types';
  9. import { int, innerHeight, innerWidth, outerHeight, outerWidth, parseBounds, isNumber } from './utils';
  10. var doc = document;
  11. var Dragger = function (_React$Component) {
  12. _inherits(Dragger, _React$Component);
  13. function Dragger() {
  14. var _ref;
  15. _classCallCheck(this, Dragger);
  16. for (var _len = arguments.length, props = Array(_len), _key = 0; _key < _len; _key++) {
  17. props[_key] = arguments[_key];
  18. }
  19. var _this = _possibleConstructorReturn(this, (_ref = Dragger.__proto__ || _Object$getPrototypeOf(Dragger)).call.apply(_ref, [this].concat(props)));
  20. _this.state = {
  21. /** x轴位移,单位是px */
  22. x: null,
  23. /** y轴位移,单位是px */
  24. y: null,
  25. /**鼠标点击元素的原始位置,单位是px */
  26. originX: 0,
  27. originY: 0,
  28. isUserMove: true,
  29. /**已经移动的位移,单位是px */
  30. lastX: 0,
  31. lastY: 0
  32. };
  33. _this.move = _this.move.bind(_this);
  34. _this.onDragEnd = _this.onDragEnd.bind(_this);
  35. return _this;
  36. }
  37. /** props end */
  38. /**
  39. * 初始变量设置
  40. */
  41. _createClass(Dragger, [{
  42. key: 'move',
  43. value: function move(event) {
  44. var _state = this.state,
  45. lastX = _state.lastX,
  46. lastY = _state.lastY;
  47. /* event.client - this.state.origin 表示的是移动的距离,
  48. * elX表示的是原来已经有的位移
  49. */
  50. var deltaX = event.clientX - this.state.originX + lastX;
  51. var deltaY = event.clientY - this.state.originY + lastY;
  52. var bounds = this.props.bounds;
  53. if (bounds) {
  54. /**
  55. * 如果用户指定一个边界,那么在这里处理
  56. */
  57. var NewBounds = typeof bounds !== 'string' ? bounds : parseBounds(bounds);
  58. /**
  59. * 网格式移动范围设定,永远移动 n 的倍数
  60. * 注意:设定移动范围的时候,一定要在判断bounds之前,否则会造成bounds不对齐
  61. */
  62. var grid = this.props.grid;
  63. if (Array.isArray(grid) && grid.length === 2) {
  64. deltaX = Math.round(deltaX / grid[0]) * grid[0];
  65. deltaY = Math.round(deltaY / grid[1]) * grid[1];
  66. }
  67. if (this.props.bounds === 'parent') {
  68. NewBounds = {
  69. left: int(this.parent.style.paddingLeft) + int(this.self.style.marginLeft) - this.self.offsetLeft,
  70. top: int(this.parent.style.paddingTop) + int(this.self.style.marginTop) - this.self.offsetTop,
  71. right: innerWidth(this.parent) - outerWidth(this.self) - this.self.offsetLeft + int(this.parent.style.paddingRight) - int(this.self.style.marginRight),
  72. bottom: innerHeight(this.parent) - outerHeight(this.self) - this.self.offsetTop + int(this.parent.style.paddingBottom) - int(this.self.style.marginBottom)
  73. };
  74. }
  75. /**
  76. * 保证不超出右边界和底部
  77. * keep element right and bot can not cross the bounds
  78. */
  79. if (isNumber(NewBounds.right)) deltaX = Math.min(deltaX, NewBounds.right);
  80. if (isNumber(NewBounds.bottom)) deltaY = Math.min(deltaY, NewBounds.bottom);
  81. /**
  82. * 保证不超出左边和上边
  83. * keep element left and top can not cross the bounds
  84. */
  85. if (isNumber(NewBounds.left)) deltaX = Math.max(deltaX, NewBounds.left);
  86. if (isNumber(NewBounds.top)) deltaY = Math.max(deltaY, NewBounds.top);
  87. }
  88. /**如果设置了x,y限制 */
  89. deltaX = this.props.allowX ? deltaX : 0;
  90. deltaY = this.props.allowY ? deltaY : 0;
  91. /**移动时回调,用于外部控制 */
  92. if (this.props.onMove) this.props.onMove(event, deltaX, deltaY);
  93. this.setState({
  94. x: deltaX,
  95. y: deltaY
  96. });
  97. }
  98. }, {
  99. key: 'onDragStart',
  100. value: function onDragStart(event) {
  101. /** 保证用户在移动元素的时候不会选择到元素内部的东西 */
  102. doc.body.style.userSelect = 'none';
  103. if (this.props.hasDraggerHandle) {
  104. if (event.target.className !== 'handle') return;
  105. }
  106. /**
  107. * 把监听事件的回掉函数,绑定在document上
  108. * 当设置边界的时候,用户鼠标会离开元素的范围
  109. * 绑定在document上可以使得其依旧能够监听
  110. * 如果绑定在元素上,则鼠标离开元素,就不会再被监听了
  111. */
  112. doc.addEventListener('mousemove', this.move);
  113. doc.addEventListener('mouseup', this.onDragEnd);
  114. if (this.props.bounds === 'parent' && (
  115. /**为了让 这段代码不会重复执行 */
  116. typeof this.parent === 'undefined' || this.parent === null)) {
  117. /**
  118. * 在这里我们将父节点缓存下来,保证当用户鼠标离开拖拽区域时,我们仍然能获取到父节点
  119. * what we do here is
  120. * making sure that we still can retrieve our parent when user's mouse left this node.
  121. */
  122. this.parent = event.currentTarget.offsetParent;
  123. /**
  124. * 我们自己
  125. * ourself
  126. */
  127. this.self = event.currentTarget;
  128. }
  129. this.props.onDragStart(this.state.x, this.state.y);
  130. this.setState({
  131. originX: event.clientX,
  132. originY: event.clientY,
  133. lastX: this.state.x,
  134. lastY: this.state.y
  135. });
  136. }
  137. }, {
  138. key: 'onDragEnd',
  139. value: function onDragEnd(event) {
  140. /** 取消用户选择限制,用户可以重新选择 */
  141. doc.body.style.userSelect = '';
  142. this.parent = null;
  143. this.self = null;
  144. doc.removeEventListener('mousemove', this.move);
  145. doc.removeEventListener('mouseup', this.onDragEnd);
  146. this.props.onDragEnd(event);
  147. }
  148. }, {
  149. key: 'componentDidMount',
  150. value: function componentDidMount() {
  151. /**
  152. * 这个函数只会调用一次
  153. * 这个只是一个临时的解决方案,因为这样会使得元素进行两次刷新
  154. */
  155. if (typeof this.props.x === 'number' && typeof this.props.y === 'number') {
  156. this.setState({
  157. x: this.props.x,
  158. y: this.props.y
  159. });
  160. }
  161. }
  162. }, {
  163. key: 'componentWillReceiveProps',
  164. value: function componentWillReceiveProps(nextProps) {
  165. /**
  166. * 外部props 改变的时候更新元素的内部位置
  167. * 这个api设计其实很不好
  168. * 以后可能会修改掉
  169. */
  170. var isUserMove = nextProps.isUserMove;
  171. if (!isUserMove) {
  172. if (typeof nextProps.x === 'number' && typeof nextProps.y === 'number') {
  173. this.setState({
  174. x: nextProps.x,
  175. y: nextProps.y,
  176. lastX: nextProps.x,
  177. lastY: nextProps.y
  178. });
  179. }
  180. }
  181. }
  182. }, {
  183. key: 'render',
  184. value: function render() {
  185. var _state2 = this.state,
  186. x = _state2.x,
  187. y = _state2.y;
  188. var _props = this.props,
  189. bounds = _props.bounds,
  190. style = _props.style,
  191. className = _props.className,
  192. others = _props.others;
  193. if (!this.props.isUserMove) {
  194. /**当外部设置其props的x,y初始属性的时候,我们在这里设置元素的初始位移 */
  195. x = this.props.x;
  196. y = this.props.y;
  197. }
  198. /**主要是为了让用户定义自己的className去修改css */
  199. var fixedClassName = typeof className === 'undefined' ? '' : className + ' ';
  200. return React.createElement(
  201. 'div',
  202. _extends({ className: fixedClassName + 'WrapDragger',
  203. style: _extends({}, style, { touchAction: 'none!important', transform: 'translate(' + x + 'px,' + y + 'px)' }),
  204. onMouseDown: this.onDragStart.bind(this),
  205. onMouseUp: this.onDragEnd.bind(this)
  206. }, others),
  207. React.cloneElement(React.Children.only(this.props.children), {})
  208. );
  209. }
  210. }]);
  211. return Dragger;
  212. }(React.Component);
  213. Dragger.propTypes = {
  214. /**
  215. * 给予元素一个x,y的初始位置,单位是px
  216. */
  217. x: PropTypes.number,
  218. y: PropTypes.number,
  219. /**
  220. * 拖动范围限制
  221. * 如果不规定范围,那么子元素就可以随意拖动不受限制
  222. * 1.可以提供自定义的范围限制
  223. * 2.也可以提供父类为边框的范围限制(string === parent)
  224. */
  225. bounds: PropTypes.oneOfType([PropTypes.shape({
  226. left: PropTypes.number,
  227. right: PropTypes.number,
  228. top: PropTypes.number,
  229. bottom: PropTypes.number
  230. }), PropTypes.string]),
  231. /**
  232. * 以网格的方式移动,每次移动并不是平滑的移动
  233. * [20,30],鼠标x轴方向移动了20 px ,y方向移动了30 px,整个子元素才会移动
  234. */
  235. grid: PropTypes.arrayOf(PropTypes.number),
  236. /**只允许移动x轴 */
  237. allowX: PropTypes.bool,
  238. /**只允许移动y轴 */
  239. allowY: PropTypes.bool,
  240. /**
  241. * 内部的移动拖拽把手
  242. * 拖拽把手className一定要设置成handle并且这个属性设置成true
  243. * <Dragger hasDraggerHandle={true}>
  244. * <div className={handle} >点击我拖动</div>
  245. * </Dragger>
  246. */
  247. hasDraggerHandle: PropTypes.bool,
  248. /**
  249. * 是否由用户移动
  250. * 可能是通过外部props改变
  251. */
  252. isUserMove: PropTypes.bool,
  253. /**
  254. * 生命周期回调
  255. */
  256. onDragStart: PropTypes.func,
  257. onMove: PropTypes.func,
  258. onDragEnd: PropTypes.func };
  259. Dragger.defaultProps = {
  260. allowX: true,
  261. allowY: true,
  262. hasDraggerHandle: false,
  263. isUserMove: true
  264. };
  265. var _default = Dragger;
  266. export default _default;
  267. ;
  268. var _temp = function () {
  269. if (typeof __REACT_HOT_LOADER__ === 'undefined') {
  270. return;
  271. }
  272. __REACT_HOT_LOADER__.register(doc, 'doc', 'app/src/Dragger.js');
  273. __REACT_HOT_LOADER__.register(Dragger, 'Dragger', 'app/src/Dragger.js');
  274. __REACT_HOT_LOADER__.register(_default, 'default', 'app/src/Dragger.js');
  275. }();
  276. ;