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 ) {
2019-01-16 13:57:31 +08:00
var _class , _temp , _initialiseProps ;
2018-09-27 15:24:29 +08:00
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
2019-01-16 13:57:31 +08:00
var _this2 = _possibleConstructorReturn ( this , _Component . call ( this , props ) ) ;
2017-09-25 15:36:23 +08:00
2019-01-16 13:57:31 +08:00
_initialiseProps . call ( _this2 ) ;
2018-09-27 15:24:29 +08:00
2019-01-16 13:57:31 +08:00
var flatColumns = [ ] ;
_this2 . _toFlatColumn ( props . columns , - 1 , flatColumns ) ;
_this2 . state = { data : _this2 . props . data , columns : props . columns , flatColumns : flatColumns } ;
2020-11-02 14:17:11 +08:00
_this2 . _initSort ( ) ;
2019-01-16 13:57:31 +08:00
return _this2 ;
2017-09-25 15:36:23 +08:00
}
2019-01-16 13:57:31 +08:00
//默认是前端排序, 值为true为后端排序
2020-11-02 14:17:11 +08:00
2018-05-22 19:38:15 +08:00
SortTable . prototype . componentWillReceiveProps = function componentWillReceiveProps ( nextProps ) {
2019-01-16 13:57:31 +08:00
2020-11-02 14:17:11 +08:00
if ( nextProps . columns !== this . props . columns ) {
var flatColumns = [ ] ;
this . _toFlatColumn ( nextProps . columns , - 1 , flatColumns ) ;
this . setState ( { columns : nextProps . columns , flatColumns : flatColumns } ) ;
}
2018-01-03 14:19:24 +08:00
if ( nextProps . data !== this . props . data ) {
this . setState ( {
data : nextProps . data ,
oldData : nextProps . data . concat ( )
2020-11-02 14:17:11 +08:00
} , function ( ) {
this . _initSort ( ) ; // 数据更新后需要重新排序
2018-01-03 14:19:24 +08:00
} ) ;
}
2017-09-25 15:36:23 +08:00
} ;
2020-11-02 14:17:11 +08:00
2019-01-16 13:57:31 +08:00
/ * *
* column扁平化处理 , 适应多表头避免递归操作
*
* /
SortTable . prototype . _toFlatColumn = function _toFlatColumn ( columns ) {
var parentIndex = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : - 1 ;
var flatColumns = arguments . length > 2 && arguments [ 2 ] !== undefined ? arguments [ 2 ] : [ ] ;
var _this = this ;
var children = [ ] ;
// const flatColumns = _this.state;
columns . forEach ( function ( item , index ) {
item . parentIndex = parentIndex ;
children = item . children ;
flatColumns . push ( item ) ;
if ( children ) {
2019-10-12 17:38:51 +08:00
// item.children = [];
2019-01-16 13:57:31 +08:00
_this . _toFlatColumn ( children , flatColumns . length - 1 , flatColumns ) ;
}
} ) ;
} ;
2017-09-25 15:36:23 +08:00
2018-09-27 15:24:29 +08:00
/ * *
* column 当前的排序的列
* 当有的列不排序时 , 将该列的orderNum置为 ‘ ’ , 并动态的修改其他列的orderNum 。
* /
2018-10-11 14:12:19 +08:00
/ * *
* 获取排序字段
* /
2018-09-27 15:24:29 +08:00
/ * *
* pre : 前一条数据
* after : 后一条数据
* orderType : 升序 、 降序
* /
/ * *
* 多列排序 先排order为1的 , 其他的基于已排序的数据排
* /
//每个column上添加orderNum属性, 不排序时为“”。
//点击时orderNum有值则不重新赋值, 如果没有值, 则取当前column下的有oderNum的length值。并排序
//点击置为“”时, 动态的设置相关column的orderNum值。并排序
2019-08-23 13:48:27 +08:00
// 默认的比较函数,即字符串比较函数
// 数值比较函数
2019-08-29 20:43:47 +08:00
// 中文比较函数,按拼音排序
2019-01-16 13:57:31 +08:00
SortTable . prototype . _flatToColumn = function _flatToColumn ( flatColumns ) {
var colLen = flatColumns . length ;
var parentIndex = void 0 ,
rsColumns = [ ] ;
//每次渲染需要将父类的children置空, 避免重复
flatColumns . forEach ( function ( item ) {
if ( item . children ) {
item . children = [ ] ;
}
} ) ;
for ( var i = colLen - 1 ; i >= 0 ; i -- ) {
parentIndex = flatColumns [ i ] . parentIndex ;
if ( parentIndex >= 0 ) {
flatColumns [ parentIndex ] . children . unshift ( flatColumns [ i ] ) ;
}
}
rsColumns = flatColumns . filter ( function ( item ) {
return item . parentIndex == - 1 ;
} ) ;
return rsColumns ;
} ;
2018-05-22 19:38:15 +08:00
SortTable . prototype . render = function render ( ) {
2019-01-16 13:57:31 +08:00
var columns = this . renderColumnsDropdown ( this . state . flatColumns . 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 ;
2019-01-16 13:57:31 +08:00
} ( _react . Component ) , _class . defaultProps = { sort : { mode : "single" , backSource : false } } , _initialiseProps = function _initialiseProps ( ) {
var _this3 = this ;
2020-11-02 14:17:11 +08:00
this . _initSort = function ( ) {
var flatColumns = _this3 . state . flatColumns ;
var needSort = false ;
flatColumns . forEach ( function ( item ) {
if ( item . order == 'descend' || item . order == 'ascend' ) {
needSort = true ;
return ;
}
} ) ;
if ( needSort ) {
var data = _this3 . multiSort ( flatColumns ) ;
_this3 . setState ( {
data : data
} ) ;
}
} ;
2019-01-16 13:57:31 +08:00
this . getOrderNum = function ( ) {
var orderNum = 0 ;
//todo 1
_this3 . state . flatColumns . forEach ( function ( item , index ) {
if ( item . order == "ascend" || item . order == "descend" ) {
orderNum ++ ;
}
} ) ;
return orderNum ? orderNum : 1 ;
} ;
this . changeOrderNum = function ( column ) {
var flatColumns = _this3 . state . flatColumns ;
//todo 2
flatColumns . forEach ( function ( col ) {
if ( col . orderNum > column . orderNum ) {
col . orderNum -- ;
}
if ( column . key == col . key ) {
col . orderNum = "" ;
}
} ) ;
_this3 . setState ( { flatColumns : flatColumns } ) ;
} ;
this . getOrderCols = function ( columns ) {
var orderCols = [ ] ;
//todo 3
columns . forEach ( function ( item ) {
if ( item . order == "ascend" || item . order == "descend" ) {
orderCols . push ( {
order : item . order ,
field : item . dataIndex ,
orderNum : item . orderNum
} ) ;
}
} ) ;
return orderCols ;
} ;
this . _sortBy = function ( pre , after , orderCols , orderColslen , currentIndex ) {
2019-06-05 14:12:12 +08:00
var currentCol = orderCols [ currentIndex ] ;
2021-02-24 16:58:38 +08:00
var getMultiSorterValueFunc = currentCol . getMultiSorterValue ;
2019-06-05 14:12:12 +08:00
var preKey = pre [ currentCol . key ] ;
var afterKey = after [ currentCol . key ] ;
2021-02-24 16:58:38 +08:00
if ( getMultiSorterValueFunc ) {
preKey = getMultiSorterValueFunc ( pre , currentCol ) ;
afterKey = getMultiSorterValueFunc ( after , currentCol ) ;
}
2019-06-05 14:12:12 +08:00
var colSortFun = currentCol . sorter ;
if ( typeof colSortFun !== 'function' ) {
colSortFun = function colSortFun ( ) {
return preKey - afterKey ;
} ;
}
2019-01-16 13:57:31 +08:00
if ( preKey == afterKey && currentIndex + 1 <= orderColslen ) {
return _this3 . _sortBy ( pre , after , orderCols , orderColslen , currentIndex + 1 ) ;
}
2019-06-05 14:12:12 +08:00
if ( currentCol . order == "ascend" ) {
return colSortFun ( pre , after ) ;
2019-01-16 13:57:31 +08:00
} else {
2019-06-05 14:12:12 +08:00
return - colSortFun ( pre , after ) ;
2019-01-16 13:57:31 +08:00
}
} ;
this . multiSort = function ( columns ) {
var _state = _this3 . state ,
data = _state . data ,
2020-12-15 20:55:48 +08:00
_state$oldData = _state . oldData ,
oldData = _state$oldData === undefined ? [ ] : _state$oldData ;
2019-01-16 13:57:31 +08:00
var self = _this3 ;
var orderCols = { } ,
orderColslen = 0 ;
//todo 4
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 _state2 = _this3 . state ,
data = _state2 . data ,
2020-12-15 20:55:48 +08:00
_state2$oldData = _state2 . oldData ,
oldData = _state2$oldData === undefined ? [ ] : _state2$oldData ,
2019-01-16 13:57:31 +08:00
flatColumns = _state2 . flatColumns ;
var sort = _this3 . props . sort ;
var seleObj = void 0 ;
if ( ! oldData ) {
oldData = data . concat ( ) ;
}
2019-02-21 19:44:00 +08:00
var sortCol = void 0 ;
2019-01-16 13:57:31 +08:00
//单列排序,清空其他列的排序
if ( sort . mode == "single" ) {
//todo 5
flatColumns . forEach ( function ( da ) {
if ( da . key == column . key ) {
seleObj = da ;
} else {
if ( da . order ) {
da . order = "flatscend" ;
}
}
} ) ;
seleObj . order = order ;
2019-02-21 19:44:00 +08:00
sortCol = [ { order : order , field : seleObj . dataIndex } ] ;
2019-01-16 13:57:31 +08:00
//通过后端请求
if ( sort . backSource && typeof sort . sortFun === "function" ) {
//获取排序的字段和方式
2019-02-21 19:44:00 +08:00
sort . sortFun ( sortCol ) ;
2019-01-16 13:57:31 +08:00
} 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 ( ) ;
}
2020-07-08 14:52:57 +08:00
typeof sort . sortFun === "function" && sort . sortFun ( sortCol , data , oldData ) ;
2019-01-16 13:57:31 +08:00
}
} else {
seleObj = flatColumns . find ( function ( da ) {
return da . key == column . key ;
} ) ;
seleObj . order = order ;
if ( order === "flatscend" ) {
_this3 . changeOrderNum ( column ) ;
}
if ( ! seleObj . orderNum && ( order == "ascend" || order == "descend" ) ) {
seleObj . orderNum = _this3 . getOrderNum ( ) ;
}
2019-02-21 19:44:00 +08:00
sortCol = _this3 . getOrderCols ( flatColumns ) ;
2019-01-16 13:57:31 +08:00
if ( sort . backSource && typeof sort . sortFun === "function" ) {
2019-02-21 19:44:00 +08:00
sort . sortFun ( sortCol ) ;
2019-01-16 13:57:31 +08:00
} else {
data = _this3 . multiSort ( flatColumns ) ;
2020-07-08 14:52:57 +08:00
typeof sort . sortFun === "function" && sort . sortFun ( sortCol , data , oldData ) ;
2019-01-16 13:57:31 +08:00
}
}
_this3 . setState ( { data : data , oldData : oldData , flatColumns : flatColumns } ) ;
} ;
this . renderColumnsDropdown = function ( columns ) {
var tempColumns = [ ] ,
rsColumns = [ ] ;
tempColumns = columns . map ( function ( originColumn ) {
var column = _extends ( { } , originColumn ) ;
return _this3 . sortColumn ( column ) ;
} ) ;
rsColumns = _this3 . _flatToColumn ( tempColumns ) ;
return rsColumns ;
} ;
this . sortColumn = function ( column ) {
var mode = _this3 . props . sort . mode ;
var prefixCls = "bee-table" ;
var iconTypeIndex = 0 ;
var sorterClass = "flat" ;
if ( column . order === "ascend" ) {
iconTypeIndex = 1 ;
sorterClass = "up" ;
} else if ( column . order === "descend" ) {
iconTypeIndex = 2 ;
sorterClass = "down" ;
}
var sortButton = void 0 ;
2019-08-23 13:48:27 +08:00
// sorter和sortEnable均可触发排序,且sorter优先级更高
if ( column . sorter || column . sortEnable ) {
2019-01-16 13:57:31 +08:00
//大于0说明不是升序就是降序, 判断orderNum有没有值, 没有值赋值
2019-08-23 13:48:27 +08:00
if ( column . sortEnable && ! column . sorter ) {
2019-08-29 20:43:47 +08:00
switch ( column . fieldType ) {
case 'number' :
{
column . sorter = _this3 . numberSortFn ( column . dataIndex ) ;
break ;
}
case 'stringChinese' :
{
column . sorter = _this3 . chineseSortFn ( column . dataIndex ) ;
break ;
}
default :
{
column . sorter = _this3 . defaultSortFn ( column . dataIndex ) ;
break ;
}
}
2019-08-23 13:48:27 +08:00
}
2019-01-16 13:57:31 +08:00
if ( iconTypeIndex > 0 && ! column . orderNum && mode == "multiple" ) {
column . orderNum = _this3 . getOrderNum ( ) ;
}
sortButton = _react2 [ "default" ] . createElement (
'div' ,
{ className : prefixCls + '-column-sorter' } ,
_react2 [ "default" ] . createElement (
'span' ,
{ className : prefixCls + '-column-sorter-' + sorterClass , onClick : function onClick ( ) {
_this3 . toggleSortOrder ( IconType [ iconTypeIndex == 2 ? 0 : iconTypeIndex + 1 ] . order , column ) ;
if ( column . sorterClick ) {
column . sorterClick ( column , IconType [ iconTypeIndex ] . type ) ;
}
} } ,
_react2 [ "default" ] . createElement ( 'i' , { className : 'uf ' + IconType [ iconTypeIndex ] . icon } ) ,
_react2 [ "default" ] . createElement (
'span' ,
null ,
column . orderNum
)
)
) ;
}
column . title = _react2 [ "default" ] . createElement (
'span' ,
null ,
column . title ,
sortButton
) ;
return column ;
} ;
2019-08-23 13:48:27 +08:00
this . defaultSortFn = function ( key ) {
return function ( a , b ) {
2019-08-23 16:07:40 +08:00
return a [ key ] >= b [ key ] ? 1 : - 1 ;
2019-08-23 13:48:27 +08:00
} ;
} ;
this . numberSortFn = function ( key ) {
return function ( a , b ) {
var numberA = parseFloat ( a [ key ] ) ;
var numberB = parseFloat ( b [ key ] ) ;
2019-08-23 16:07:40 +08:00
return numberA >= numberB ? 1 : - 1 ;
2019-08-23 13:48:27 +08:00
} ;
} ;
2019-08-29 20:43:47 +08:00
this . chineseSortFn = function ( key ) {
return function ( a , b ) {
return a [ key ] . localeCompare ( b [ key ] , 'zh-Hans-CN' , { sensitivity : 'accent' } ) ;
} ;
} ;
2018-09-27 15:24:29 +08:00
} , _temp ;
2017-09-25 15:36:23 +08:00
}
2018-09-27 15:24:29 +08:00
module . exports = exports [ 'default' ] ;