Dragger.js 12 KB

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