GridItem.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 { Component } from 'react';
  9. import PropTypes from 'prop-types';
  10. import Dragger from './Dragger';
  11. var GridItem = function (_Component) {
  12. _inherits(GridItem, _Component);
  13. function GridItem(props) {
  14. _classCallCheck(this, GridItem);
  15. var _this = _possibleConstructorReturn(this, (GridItem.__proto__ || _Object$getPrototypeOf(GridItem)).call(this, props));
  16. _this.onDrag = _this.onDrag.bind(_this);
  17. _this.onDragStart = _this.onDragStart.bind(_this);
  18. _this.onDragEnd = _this.onDragEnd.bind(_this);
  19. _this.calGridXY = _this.calGridXY.bind(_this);
  20. _this.calColWidth = _this.calColWidth.bind(_this);
  21. return _this;
  22. }
  23. _createClass(GridItem, [{
  24. key: 'calColWidth',
  25. /** 计算容器的每一个格子多大 */
  26. value: function calColWidth() {
  27. var _props = this.props,
  28. containerWidth = _props.containerWidth,
  29. col = _props.col,
  30. containerPadding = _props.containerPadding,
  31. margin = _props.margin;
  32. return (containerWidth - containerPadding[0] * 2 - margin[0] * (col + 1)) / col;
  33. }
  34. /**转化,计算网格的GridX,GridY值 */
  35. }, {
  36. key: 'calGridXY',
  37. value: function calGridXY(x, y) {
  38. var _props2 = this.props,
  39. margin = _props2.margin,
  40. containerWidth = _props2.containerWidth,
  41. col = _props2.col,
  42. w = _props2.w;
  43. /**坐标转换成格子的时候,无须计算margin */
  44. var GridX = Math.round(x / containerWidth * col);
  45. var GridY = Math.round(y / (this.props.rowHeight + margin[1]));
  46. /**防止元素出container */
  47. if (GridX + w > col - 1) GridX = col - w; //右边界
  48. if (GridX < 0) GridX = 0; //左边界
  49. if (GridY < 0) GridY = 0; //上边界
  50. return { GridX: GridX, GridY: GridY };
  51. }
  52. /**给予一个grid的位置,算出元素具体的在容器中位置在哪里,单位是px */
  53. }, {
  54. key: 'calGridItemPosition',
  55. value: function calGridItemPosition(GridX, GridY) {
  56. var _props3 = this.props,
  57. w = _props3.w,
  58. margin = _props3.margin,
  59. col = _props3.col,
  60. containerWidth = _props3.containerWidth;
  61. var x = Math.round(GridX * this.calColWidth() + (GridX + 1) * margin[0]);
  62. var y = Math.round(GridY * this.props.rowHeight + margin[1] * (GridY + 1));
  63. return {
  64. x: x,
  65. y: y
  66. };
  67. }
  68. /**宽和高计算成为px */
  69. }, {
  70. key: 'calWHtoPx',
  71. value: function calWHtoPx(w, h) {
  72. var _props4 = this.props,
  73. margin = _props4.margin,
  74. containerPadding = _props4.containerPadding,
  75. containerWidth = _props4.containerWidth,
  76. col = _props4.col;
  77. var wPx = Math.round(w * this.calColWidth() + (w - 1) * margin[0]);
  78. var hPx = Math.round(h * this.props.rowHeight + (h - 1) * margin[1]);
  79. return { wPx: wPx, hPx: hPx };
  80. }
  81. }, {
  82. key: 'onDragStart',
  83. value: function onDragStart(x, y) {
  84. var _props5 = this.props,
  85. w = _props5.w,
  86. h = _props5.h,
  87. UniqueKey = _props5.UniqueKey;
  88. var _calGridXY = this.calGridXY(x, y),
  89. GridX = _calGridXY.GridX,
  90. GridY = _calGridXY.GridY;
  91. this.props.onDragStart({
  92. event: event, GridX: GridX, GridY: GridY, w: w, h: h, UniqueKey: UniqueKey
  93. });
  94. }
  95. }, {
  96. key: 'onDrag',
  97. value: function onDrag(event, x, y) {
  98. var _calGridXY2 = this.calGridXY(x, y),
  99. GridX = _calGridXY2.GridX,
  100. GridY = _calGridXY2.GridY;
  101. var _props6 = this.props,
  102. w = _props6.w,
  103. h = _props6.h,
  104. col = _props6.col,
  105. UniqueKey = _props6.UniqueKey;
  106. this.props.onDrag({ GridX: GridX, GridY: GridY, w: w, h: h }, UniqueKey);
  107. }
  108. }, {
  109. key: 'onDragEnd',
  110. value: function onDragEnd() {
  111. if (this.props.onDragEnd) this.props.onDragEnd(this.props.UniqueKey);
  112. }
  113. }, {
  114. key: 'render',
  115. value: function render() {
  116. var _calGridItemPosition = this.calGridItemPosition(this.props.GridX, this.props.GridY),
  117. x = _calGridItemPosition.x,
  118. y = _calGridItemPosition.y;
  119. var _props7 = this.props,
  120. w = _props7.w,
  121. h = _props7.h,
  122. margin = _props7.margin,
  123. style = _props7.style,
  124. bounds = _props7.bounds;
  125. var _calWHtoPx = this.calWHtoPx(w, h),
  126. wPx = _calWHtoPx.wPx,
  127. hPx = _calWHtoPx.hPx;
  128. return React.createElement(
  129. Dragger,
  130. {
  131. style: _extends({}, style, { width: wPx, height: hPx, position: 'absolute',
  132. transition: this.props.isUserMove ? '' : 'all .15s'
  133. }),
  134. onDragStart: this.onDragStart,
  135. onMove: this.onDrag,
  136. onDragEnd: this.onDragEnd,
  137. x: x,
  138. y: y,
  139. isUserMove: this.props.isUserMove
  140. },
  141. React.createElement(
  142. 'div',
  143. null,
  144. React.Children.map(this.props.children, function (child) {
  145. return child;
  146. })
  147. )
  148. );
  149. }
  150. }]);
  151. return GridItem;
  152. }(Component);
  153. GridItem.PropTypes = {
  154. /**外部容器属性 */
  155. col: PropTypes.number,
  156. containerWidth: PropTypes.number,
  157. containerPadding: PropTypes.array,
  158. /**子元素的属性 */
  159. margin: PropTypes.array,
  160. GridX: PropTypes.number,
  161. GridY: PropTypes.number,
  162. rowHeight: PropTypes.number,
  163. /**子元素的宽高 */
  164. w: PropTypes.number,
  165. h: PropTypes.number,
  166. /**生命周期回掉函数 */
  167. onDragStart: PropTypes.func,
  168. onDragEnd: PropTypes.func
  169. };
  170. GridItem.defaultProps = {
  171. col: 12,
  172. containerWidth: 500,
  173. containerPadding: [0, 0],
  174. margin: [10, 10],
  175. rowHeight: 30,
  176. w: 1,
  177. h: 1 };
  178. var _default = GridItem;
  179. export default _default;
  180. ;
  181. var _temp = function () {
  182. if (typeof __REACT_HOT_LOADER__ === 'undefined') {
  183. return;
  184. }
  185. __REACT_HOT_LOADER__.register(GridItem, 'GridItem', 'app/src/GridItem.js');
  186. __REACT_HOT_LOADER__.register(_default, 'default', 'app/src/GridItem.js');
  187. }();
  188. ;