2018-09-27 15:24:29 +08:00
'use strict' ;
2017-09-25 15:36:23 +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 ; } ;
exports [ "default" ] = sort ;
2018-09-27 15:24:29 +08:00
var _react = require ( 'react' ) ;
2017-09-25 15:36:23 +08:00
var _react2 = _interopRequireDefault ( _react ) ;
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 ; }
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 ) ; }
/ * *
* 参数 : prefixCls , 默认bee - table , 用于设置图标的样式
2017-11-29 16:51:07 +08:00
* @ param { * } Table
2018-05-22 19:38:15 +08:00
* @ param { * } Icon
2017-09-25 15:36:23 +08:00
* /
2018-05-22 19:38:15 +08:00
function sort ( Table , Icon ) {
2018-09-27 15:24:29 +08:00
var _class , _temp ;
var IconType = [ {
'type' : 'flat' ,
'icon' : 'uf-symlist' ,
'order' : 'flatscend'
} , {
'type' : 'up' ,
'icon' : 'uf-sortup' ,
'order' : 'ascend'
} , {
'type' : 'down' ,
'icon' : 'uf-sortdown' ,
'order' : 'descend'
} ] ;
return _temp = _class = function ( _Component ) {
2018-05-22 19:38:15 +08:00
_inherits ( SortTable , _Component ) ;
2017-09-25 15:36:23 +08:00
2018-05-22 19:38:15 +08:00
function SortTable ( props ) {
_classCallCheck ( this , SortTable ) ;
2017-09-25 15:36:23 +08:00
var _this = _possibleConstructorReturn ( this , _Component . call ( this , props ) ) ;
2018-09-27 15:24:29 +08:00
_this . getOrderNum = function ( ) {
var orderNum = 0 ;
_this . state . columns . forEach ( function ( item , index ) {
if ( item . order == 'ascend' || item . order == 'descend' ) {
orderNum ++ ;
}
} ) ;
return orderNum ? orderNum : 1 ;
} ;
_this . changeOrderNum = function ( column ) {
var columns = _this . state . columns ;
columns . forEach ( function ( col ) {
if ( col . orderNum > column . orderNum ) {
col . orderNum -- ;
}
if ( column . key == col . key ) {
col . orderNum = '' ;
}
} ) ;
_this . setState ( { columns : columns } ) ;
} ;
_this . _sortBy = function ( pre , after , orderCols , orderColslen , currentIndex ) {
var preKey = pre [ orderCols [ currentIndex ] . key ] ;
var afterKey = after [ orderCols [ currentIndex ] . key ] ;
if ( preKey == afterKey && currentIndex + 1 <= orderColslen ) {
return _this . _sortBy ( pre , after , orderCols , orderColslen , currentIndex + 1 ) ;
}
if ( orderCols [ currentIndex ] . order == 'ascend' ) {
return preKey - afterKey ;
} else {
return afterKey - preKey ;
}
} ;
_this . multiSort = function ( columns ) {
2017-09-25 15:36:23 +08:00
var _this$state = _this . state ,
data = _this$state . data ,
2018-09-27 15:24:29 +08:00
oldData = _this$state . oldData ;
var self = _this ;
var orderCols = { } ,
orderColslen = 0 ;
columns . forEach ( function ( item ) {
if ( item . orderNum ) {
orderColslen ++ ;
orderCols [ item . orderNum ] = item ;
}
} ) ;
if ( orderColslen > 0 ) {
data = data . sort ( function ( a , b ) {
return self . _sortBy ( a , b , orderCols , orderColslen , 1 ) ;
} ) ;
} else {
data = oldData . concat ( ) ;
}
return data ;
} ;
_this . toggleSortOrder = function ( order , column ) {
var _this$state2 = _this . state ,
data = _this$state2 . data ,
oldData = _this$state2 . oldData ,
columns = _this$state2 . columns ;
var sort = _this . props . sort ;
var seleObj = void 0 ;
2017-09-25 15:36:23 +08:00
if ( ! oldData ) {
oldData = data . concat ( ) ;
}
2018-09-27 15:24:29 +08:00
//单列排序,清空其他列的排序
if ( sort . mode == 'single' ) {
columns . forEach ( function ( da ) {
if ( da . key == column . key ) {
seleObj = da ;
} else {
if ( da . order ) {
da . order = 'flatscend' ;
}
}
2017-09-25 15:36:23 +08:00
} ) ;
2018-09-27 15:24:29 +08:00
seleObj . order = order ;
//通过后端请求
if ( sort . backSource ) {
//获取排序的字段和方式
} else {
if ( order === "ascend" ) {
data = data . sort ( function ( a , b ) {
return column . sorter ( a , b ) ;
} ) ;
} else if ( order === "descend" ) {
data = data . sort ( function ( a , b ) {
return column . sorter ( b , a ) ;
} ) ;
} else {
data = oldData . concat ( ) ;
}
}
2017-09-25 15:36:23 +08:00
} else {
2018-09-27 15:24:29 +08:00
seleObj = columns . find ( function ( da ) {
return da . key == column . key ;
} ) ;
seleObj . order = order ;
if ( order === "flatscend" ) {
_this . changeOrderNum ( column ) ;
}
if ( ! seleObj . orderNum && ( order == 'ascend' || order == 'descend' ) ) {
seleObj . orderNum = _this . getOrderNum ( ) ;
}
data = _this . multiSort ( columns ) ;
2017-09-25 15:36:23 +08:00
}
_this . setState ( {
data : data ,
2018-08-02 14:29:34 +08:00
oldData : oldData ,
columns : columns
} ) ;
} ;
_this . renderColumnsDropdown = function ( columns ) {
2018-09-04 10:16:53 +08:00
var prefixCls = "bee-table" ;
2018-09-27 15:24:29 +08:00
var mode = _this . props . sort . mode ;
2018-08-02 14:29:34 +08:00
return columns . map ( function ( originColumn ) {
2018-09-27 15:24:29 +08:00
var iconTypeIndex = 0 ;
2018-08-02 14:29:34 +08:00
var column = _extends ( { } , originColumn ) ;
2018-09-27 15:24:29 +08:00
var sorterClass = 'flat' ;
if ( column . order === "ascend" ) {
iconTypeIndex = 1 ;
sorterClass = 'up' ;
} else if ( column . order === "descend" ) {
iconTypeIndex = 2 ;
sorterClass = 'down' ;
}
2018-08-02 14:29:34 +08:00
var sortButton = void 0 ;
if ( column . sorter ) {
2018-09-27 15:24:29 +08:00
//大于0说明不是升序就是降序, 判断orderNum有没有值, 没有值赋值
if ( iconTypeIndex > 0 && ! column . orderNum && mode == 'multiple' ) {
column . orderNum = _this . getOrderNum ( ) ;
}
2018-08-02 14:29:34 +08:00
sortButton = _react2 [ "default" ] . createElement (
2018-09-27 15:24:29 +08:00
'div' ,
{ className : prefixCls + '-column-sorter' } ,
2018-08-02 14:29:34 +08:00
_react2 [ "default" ] . createElement (
2018-09-27 15:24:29 +08:00
'span' ,
2018-08-02 14:29:34 +08:00
{
2018-09-27 15:24:29 +08:00
className : prefixCls + '-column-sorter-' + sorterClass ,
2018-08-02 14:29:34 +08:00
onClick : function onClick ( ) {
2018-09-27 15:24:29 +08:00
_this . toggleSortOrder ( IconType [ iconTypeIndex == 2 ? 0 : iconTypeIndex + 1 ] . order , column ) ;
2018-08-02 14:29:34 +08:00
if ( column . sorterClick ) {
2018-09-27 15:24:29 +08:00
column . sorterClick ( column , IconType [ iconTypeIndex ] . type ) ;
2018-08-02 14:29:34 +08:00
}
}
} ,
2018-09-27 15:24:29 +08:00
_react2 [ "default" ] . createElement ( Icon , { type : IconType [ iconTypeIndex ] . icon } ) ,
_react2 [ "default" ] . createElement (
'span' ,
null ,
column . orderNum
)
2018-08-02 14:29:34 +08:00
)
) ;
}
column . title = _react2 [ "default" ] . createElement (
2018-09-27 15:24:29 +08:00
'span' ,
2018-08-02 14:29:34 +08:00
null ,
column . title ,
sortButton
) ;
return column ;
2017-09-25 15:36:23 +08:00
} ) ;
} ;
_this . state = {
2018-08-02 14:29:34 +08:00
data : _this . props . data ,
columns : props . columns
2017-09-25 15:36:23 +08:00
} ;
return _this ;
}
2018-05-22 19:38:15 +08:00
SortTable . prototype . componentWillReceiveProps = function componentWillReceiveProps ( nextProps ) {
2018-01-03 14:19:24 +08:00
if ( nextProps . data !== this . props . data ) {
this . setState ( {
data : nextProps . data ,
oldData : nextProps . data . concat ( )
} ) ;
}
2018-08-02 14:29:34 +08:00
if ( nextProps . columns !== this . props . columns ) {
this . setState ( {
columns : nextProps . columns
} ) ;
}
2017-09-25 15:36:23 +08:00
} ;
2018-09-27 15:24:29 +08:00
/ * *
* column 当前的排序的列
* 当有的列不排序时 , 将该列的orderNum置为 ‘ ’ , 并动态的修改其他列的orderNum 。
* /
/ * *
* pre : 前一条数据
* after : 后一条数据
* orderType : 升序 、 降序
* /
/ * *
* 多列排序 先排order为1的 , 其他的基于已排序的数据排
* /
//每个column上添加orderNum属性, 不排序时为“”。
//点击时orderNum有值则不重新赋值, 如果没有值, 则取当前column下的有oderNum的length值。并排序
//点击置为“”时, 动态的设置相关column的orderNum值。并排序
2018-05-22 19:38:15 +08:00
SortTable . prototype . render = function render ( ) {
2018-08-02 14:29:34 +08:00
var columns = this . renderColumnsDropdown ( this . state . columns . concat ( ) ) ;
2017-09-25 15:36:23 +08:00
return _react2 [ "default" ] . createElement ( Table , _extends ( { } , this . props , { columns : columns , data : this . state . data } ) ) ;
} ;
2018-05-22 19:38:15 +08:00
return SortTable ;
2018-09-27 15:24:29 +08:00
} ( _react . Component ) , _class . defaultProps = {
sort : {
mode : 'single' ,
backSource : false //默认是前端排序, 值为true为后端排序
}
} , _temp ;
2017-09-25 15:36:23 +08:00
}
2018-09-27 15:24:29 +08:00
module . exports = exports [ 'default' ] ;