2018-12-03 10:02:57 +08:00
'use strict' ;
2018-05-11 09:29:43 +08:00
Object . defineProperty ( exports , "__esModule" , {
value : true
} ) ;
var _extends = Object . assign || function ( target ) { for ( var i = 1 ; i < arguments . length ; i ++ ) { var source = arguments [ i ] ; for ( var key in source ) { if ( Object . prototype . hasOwnProperty . call ( source , key ) ) { target [ key ] = source [ key ] ; } } } return target ; } ;
2018-12-03 10:02:57 +08:00
var _typeof = typeof Symbol === "function" && typeof Symbol . iterator === "symbol" ? function ( obj ) { return typeof obj ; } : function ( obj ) { return obj && typeof Symbol === "function" && obj . constructor === Symbol && obj !== Symbol . prototype ? "symbol" : typeof obj ; } ;
2018-05-11 09:29:43 +08:00
exports [ "default" ] = dragColumn ;
2018-12-03 10:02:57 +08:00
var _react = require ( 'react' ) ;
2018-05-11 09:29:43 +08:00
var _react2 = _interopRequireDefault ( _react ) ;
2018-12-03 10:02:57 +08:00
var _util = require ( './util' ) ;
2018-05-11 09:29:43 +08:00
function _interopRequireDefault ( obj ) { return obj && obj . _ _esModule ? obj : { "default" : obj } ; }
function _defaults ( obj , defaults ) { var keys = Object . getOwnPropertyNames ( defaults ) ; for ( var i = 0 ; i < keys . length ; i ++ ) { var key = keys [ i ] ; var value = Object . getOwnPropertyDescriptor ( defaults , key ) ; if ( value && value . configurable && obj [ key ] === undefined ) { Object . defineProperty ( obj , key , value ) ; } } return obj ; }
2018-05-22 20:08:17 +08:00
function _objectWithoutProperties ( obj , keys ) { var target = { } ; for ( var i in obj ) { if ( keys . indexOf ( i ) >= 0 ) continue ; if ( ! Object . prototype . hasOwnProperty . call ( obj , i ) ) continue ; target [ i ] = obj [ i ] ; } return target ; }
2018-05-11 09:29:43 +08:00
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 ) : _defaults ( subClass , superClass ) ; }
2018-12-03 16:03:32 +08:00
var cloneDeep = require ( 'lodash.clonedeep' ) ;
2018-05-11 09:29:43 +08:00
/ * *
* 参数 : 列拖拽
* @ param { * } Table
* /
function dragColumn ( Table ) {
2018-07-06 14:33:29 +08:00
return function ( _Component ) {
2018-05-22 19:38:15 +08:00
_inherits ( DragColumn , _Component ) ;
2018-05-11 09:29:43 +08:00
2018-05-22 19:38:15 +08:00
function DragColumn ( props ) {
_classCallCheck ( this , DragColumn ) ;
2018-05-11 09:29:43 +08:00
var _this = _possibleConstructorReturn ( this , _Component . call ( this , props ) ) ;
2018-07-06 14:33:29 +08:00
_this . setColumOrderByIndex = function ( _column ) {
_column . forEach ( function ( da , i ) {
da . dragIndex = i ;
da . drgHover = false ;
} ) ;
return _column ;
} ;
2018-05-11 09:29:43 +08:00
2018-07-06 14:33:29 +08:00
_this . onDrop = function ( event , data ) {
2018-12-02 16:53:06 +08:00
var dragSource = data . dragSource ,
dragTarg = data . dragTarg ;
2018-07-06 14:33:29 +08:00
var columns = _this . state . columns ;
2018-12-02 16:53:06 +08:00
var dragSourceColum = columns . find ( function ( da , i ) {
return da . key == dragSource . key ;
2018-07-06 14:33:29 +08:00
} ) ;
2018-12-02 16:53:06 +08:00
var dragTargColum = columns . find ( function ( da , i ) {
return da . key == dragTarg . key ;
2018-07-06 14:33:29 +08:00
} ) ;
2018-12-02 16:53:06 +08:00
for ( var index = 0 ; index < columns . length ; index ++ ) {
var da = columns [ index ] ;
if ( da . key === dragSource . key ) {
columns [ index ] = dragTargColum ;
2018-07-06 14:33:29 +08:00
}
2018-12-02 16:53:06 +08:00
if ( da . key === dragTarg . key ) {
columns [ index ] = dragSourceColum ;
2018-07-06 14:33:29 +08:00
}
2018-12-02 16:53:06 +08:00
}
2018-12-03 16:03:32 +08:00
// let titles = [];
// columns.forEach(da=>{
// for(let attr of da){
// }
// if(typeof da.title != "string"){
// titles.push(da.title);
// delete da.title;
// }
// });
// let newColumns = null;
// if(titles.length != 0){
// newColumns = JSON.parse(JSON.stringify(columns));
// for (let index = 0; index < newColumns.length; index++) {
// newColumns[index].title = titles[index];
// }
// console.log("----columns----",newColumns);
// }else{
// newColumns = JSON.parse(JSON.stringify(columns));
// }
// this.setState({
// columns:newColumns
// });
2018-07-06 14:33:29 +08:00
_this . setState ( {
2018-12-03 16:03:32 +08:00
columns : cloneDeep ( columns )
2018-07-06 14:33:29 +08:00
} ) ;
2018-12-03 16:03:32 +08:00
if ( _this . props . dragDrop ) {
_this . props . dragDrop ( event , data , newColumns ) ;
}
2018-07-06 14:33:29 +08:00
} ;
_this . getTarget = function ( evt ) {
return evt . target || evt . srcElement ;
} ;
_this . state = {
columns : _this . setColumOrderByIndex ( props . columns )
} ;
2018-05-11 09:29:43 +08:00
return _this ;
}
2018-05-22 19:38:15 +08:00
DragColumn . prototype . componentWillReceiveProps = function componentWillReceiveProps ( nextProps ) {
2018-05-11 09:29:43 +08:00
if ( nextProps . columns != this . props . columns ) {
2018-07-06 14:33:29 +08:00
this . setState ( {
columns : this . setColumOrderByIndex ( nextProps . columns )
} ) ;
2018-05-11 09:29:43 +08:00
}
} ;
2018-12-03 10:02:57 +08:00
DragColumn . prototype . cloneDeep = function cloneDeep ( obj ) {
if ( ( typeof obj === 'undefined' ? 'undefined' : _typeof ( obj ) ) !== 'object' || Object . keys ( obj ) . length === 0 ) {
return obj ;
}
var resultData = { } ;
return this . recursion ( obj , resultData ) ;
} ;
DragColumn . prototype . recursion = function ( _recursion ) {
function recursion ( _x ) {
return _recursion . apply ( this , arguments ) ;
}
recursion . toString = function ( ) {
return _recursion . toString ( ) ;
} ;
return recursion ;
} ( function ( obj ) {
var data = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : { } ;
for ( key in obj ) {
if ( _typeof ( obj [ key ] ) == 'object' && Object . keys ( obj [ key ] . length > 0 ) ) {
data [ key ] = recursion ( obj [ key ] ) ;
} else {
data [ key ] = obj [ key ] ;
}
}
return data ;
} ) ;
2018-05-22 19:38:15 +08:00
DragColumn . prototype . render = function render ( ) {
2018-05-11 09:29:43 +08:00
var _props = this . props ,
data = _props . data ,
dragborder = _props . dragborder ,
draggable = _props . draggable ,
2018-05-22 20:08:17 +08:00
className = _props . className ,
columns = _props . columns ,
onDragStart = _props . onDragStart ,
onDragEnter = _props . onDragEnter ,
onDragOver = _props . onDragOver ,
onDrop = _props . onDrop ,
2018-12-03 10:02:57 +08:00
others = _objectWithoutProperties ( _props , [ 'data' , 'dragborder' , 'draggable' , 'className' , 'columns' , 'onDragStart' , 'onDragEnter' , 'onDragOver' , 'onDrop' ] ) ;
2018-05-11 09:29:43 +08:00
2018-05-14 10:01:06 +08:00
var key = new Date ( ) . getTime ( ) ;
2018-05-22 20:08:17 +08:00
return _react2 [ "default" ] . createElement ( Table , _extends ( { } , others , {
columns : this . state . columns ,
data : data ,
2018-12-03 10:02:57 +08:00
className : className + ' u-table-drag-border' ,
2018-05-22 20:08:17 +08:00
onDragStart : this . onDragStart ,
onDragOver : this . onDragOver ,
onDrop : this . onDrop ,
2018-05-11 09:29:43 +08:00
onDragEnter : this . onDragEnter ,
2018-09-12 14:14:05 +08:00
draggable : draggable ,
dragborder : dragborder
// dragborder={false}
, dragborderKey : key
2018-05-11 09:29:43 +08:00
} ) ) ;
} ;
2018-05-22 19:38:15 +08:00
return DragColumn ;
2018-07-06 14:33:29 +08:00
} ( _react . Component ) ;
2018-05-11 09:29:43 +08:00
}
2018-12-03 10:02:57 +08:00
module . exports = exports [ 'default' ] ;