GridItem.js 7.1 KB

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