GridItem.js 7.6 KB

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