UNPKG

micro-ui-y

Version:

289 lines (251 loc) 11.2 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); require('./test.css'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var list = [{ text: "姓名", width: "10rem", id: "name" }, { text: "ID", width: "10rem", id: "id" }, { text: "年龄", width: "10rem", id: "age" }, { text: "性别", width: "10rem", id: "sex" }, { text: "出生日期", width: "10rem", id: "date" }, { text: "家庭住址", width: "10rem", id: "adress" }]; var table = [{ name: "古福艳", id: "9346", age: "30", sex: "男", date: "1990-01-01", adress: "北京市朝阳区" }, { name: "吴小蕾", id: "10825", age: "27", sex: "女", date: "1990-01-01", adress: "上海" }, { name: "申伯华", id: "1308", age: "45", sex: "男", date: "1990-01-01", adress: "山东济南" }]; var Test = function (_React$Component) { _inherits(Test, _React$Component); function Test() { var _ref; var _temp, _this, _ret; _classCallCheck(this, Test); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Test.__proto__ || Object.getPrototypeOf(Test)).call.apply(_ref, [this].concat(args))), _this), _this.state = { moveElement: {}, showColResize: false, mouseDown: false, oldElemX: "", oldMouseX: "" }, _this.onMouseDownFn = function (item, e) { console.log('onMouseDownFn', e); // debugger var fontsize = parseFloat(getComputedStyle(window.document.documentElement)['font-size']); if (_this.state.showColResize) { var oldElemW = e.target.offsetWidth; var oldElemX = e.target.offsetLeft; var oldElemR = e.target.offsetLeft + e.target.offsetWidth; var oldMouseX = e.clientX; _this.setState({ mouseDown: true, oldElemW: oldElemW, oldElemX: oldElemX, oldElemR: oldElemR, oldMouseX: oldMouseX, oldItem: item }); } }, _this.onMouseMoveFn = function (item, e) { // console.log('onMouseMoveFn', e); // // scrollWidth:对象的实际内容的宽度,不包边线宽度,会随对象中内容超过可视区后而变大。 // // clientWidth:对象内容的可视区的宽度,不包滚动条等边线,会随对象显示大小的变化而改变。 // // offsetWidth:对象整体的实际宽度,包滚动条等边线,会随对象显示大小的变化而改变。 // // innerWidth: // // width // // // offsetWidth属性可以返回对象的padding+border+元素width属性值之和,style.width返回值就是定义的width属性值。 // // offsetWidth属性仅是可读属性,而style.width是可读写的。 // // offsetWidth属性返回值是整数,而style.width的返回值是字符串,并且带有单位。 // // console.log('clientX', e.clientX); // console.log('scrollLeft', e.target.scrollLeft); // console.log('offsetLeft', e.target.offsetLeft); var elemX = e.target.offsetLeft; console.log(e.target, e.target.offsetLeft); var elemR = e.target.offsetLeft + e.target.offsetWidth; var mouseX = e.clientX; if (elemR > mouseX - 20 && elemR < mouseX + 20) { // if (elemR - mouseX <= 20) { _this.setState({ moveElement: item, showColResize: true }); } else { _this.setState({ moveElement: item, showColResize: false }); } if (_this.state.mouseDown) { if (mouseX > elemX) { var fontsize = parseFloat(getComputedStyle(window.document.documentElement)['font-size']); // const newW = this.state.oldElemW - (this.state.oldElemR - mouseX); var newW = mouseX - _this.state.oldElemX; if (newW > 0) { list.forEach(function (item1) { if (item1.id == _this.state.oldItem.id) { var w = (newW / fontsize * (16 - fontsize) + newW) / 16 + "rem"; // const w = (((16 - fontsize) * newW + fontsize * newW) / 16) / 16 + "rem"; item1.width = w; } }); } } } }, _this.onMouseUpFn = function (item, e) { _this.setState({ moveElement: {}, mouseDown: false, showColResize: false }); }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(Test, [{ key: 'render', // // //用户开始拖动元素时触发 // onDragStartFn = (item, e)=> { // console.log('ondragstartFn', e); // this.setState({ // dragElement: item // }) // }; // //用户完成元素拖动后触发 // onDragEndFn = (item, e)=> { // console.log('ondragend', e); // if (this.state.dragElement != this.state.moveElement) { // const indexDrag = list.indexOf(this.state.dragElement); // list.splice(indexDrag, 1); // const indexMove = list.indexOf(this.state.moveElement); // list.splice(indexMove, 0, this.state.dragElement); // } // // // this.setState({ // dragElement: "", // moveElement: "", // }) // }; // // // 当被鼠标拖动的对象进入其容器范围内时触发此事件 // onDragEnterFn = (item, e)=> { // console.log('ondragenter', e); // this.setState({ // moveElement: item // }) // }; value: function render() { var _this2 = this; var w = {}; list.forEach(function (item) { w[item.id] = item.width; }); return _react2.default.createElement( 'div', null, _react2.default.createElement( 'div', { className: 'a-box' }, list.map(function (item, index) { return _react2.default.createElement( 'div', { key: index, className: _this2.state.moveElement.text == item.text && _this2.state.showColResize ? "a-item col-resize" : "a-item" // className={"a-item"} , onMouseDown: _this2.onMouseDownFn.bind(_this2, item), onMouseUp: _this2.onMouseUpFn.bind(_this2, item), onMouseMove: _this2.onMouseMoveFn.bind(_this2, item), style: { width: item.width } }, item.text ); }) ), table.map(function (item, index) { return _react2.default.createElement( 'div', { key: index, className: 'a-box' }, _react2.default.createElement( 'div', { className: 'a-item', style: { width: w["name"] } }, item.name ), _react2.default.createElement( 'div', { className: 'a-item', style: { width: w["id"] } }, item.id ), _react2.default.createElement( 'div', { className: 'a-item', style: { width: w["age"] } }, item.age ), _react2.default.createElement( 'div', { className: 'a-item', style: { width: w["sex"] } }, item.sex ), _react2.default.createElement( 'div', { className: 'a-item', style: { width: w["date"] } }, item.date ), _react2.default.createElement( 'div', { className: 'a-item', style: { width: w["adress"] } }, item.adress ) ); }) ); } }]); return Test; }(_react2.default.Component); exports.default = Test; //# sourceMappingURL=index1.js.map