'use strict' ; import type DataEventEmitter from '../DataEventEmitter' ; import DataModifier from './DataModifier.js' ; import DataTable from '../DataTable.js' ; import U from '../../Core/Utilities.js' ; const { merge } = U ; private class SortModifier extends DataModifier { /**
     * Default options to group table rows.
     */ public static readonly defaultOptions : SortModifier . Options = { modifier : 'Order' , direction : 'desc' , orderByColumn : 'y' } ; private static ascending ( a , b ) { return ( ( a || 0 ) < ( b || 0 ) ? - 1 : ( a || 0 ) > ( b || 0 ) ? 1 : 0 ) ; } private static descending ( a , b ) { return ( ( b || 0 ) < ( a || 0 ) ? - 1 : ( b || 0 ) > ( a || 0 ) ? 1 : 0 ) ; } param { RangeDataModifier . Options } options public constructor ( options ? ) { super ( ) ; this . options = merge ( SortModifier . defaultOptions , options ) ; } public options : SortModifier . Options ; param { Highcharts . DataTable } table param { string } columnName param { number | undefined } rowIndex param { Highcharts . DataTableCellType } cellValue param { Highcharts . DataTableEventDetail } eventDetail return { Highcharts . DataTable } public modifyCell < T extends DataTable > ( table , columnName , rowIndex , cellValue , eventDetail ? ) { const modifier = this , { orderByColumn , orderInColumn } = modifier . options ; if ( columnName === orderByColumn ) { if ( orderInColumn ) { table . modified . setCell ( columnName , rowIndex , cellValue ) ; table . modified . setColumn ( orderInColumn , modifier . modifyTable ( new DataTable ( table . getColumns ( [ orderByColumn , orderInColumn ] ) ) ) . modified . getColumn ( orderInColumn ) ) ; } else { modifier . modifyTable ( table , eventDetail ) ; } } return table ; } param { Highcharts . DataTable } table param { Highcharts . DataTableColumnCollection } columns param { number } rowIndex param { Highcharts . DataTableEventDetail } eventDetail return { Highcharts . DataTable } public modifyColumns < T extends DataTable > ( table , columns , rowIndex , eventDetail ? ) { const modifier = this , { orderByColumn , orderInColumn } = modifier . options , columnNames = Object . keys ( columns ) ; if ( columnNames . indexOf ( orderByColumn ) > - 1 ) { if ( orderInColumn && columns [ columnNames [ 0 ] ] . length ) { table . modified . setColumns ( columns , rowIndex ) ; table . modified . setColumn ( orderInColumn , modifier . modifyTable ( new DataTable ( table . getColumns ( [ orderByColumn , orderInColumn ] ) ) ) . modified . getColumn ( orderInColumn ) ) ; } else { modifier . modifyTable ( table , eventDetail ) ; } } return table ; } param { Highcharts . DataTable } table param { Array < ( Highcharts . DataTableRow | Highcharts . DataTableRowObject ) > } rows param { number } rowIndex param { Highcharts . DataTableEventDetail } eventDetail return { Highcharts . DataTable } public modifyRows < T extends DataTable > ( table , rows , rowIndex , eventDetail ? ) { const modifier = this , { orderByColumn , orderInColumn } = modifier . options ; if ( orderInColumn && rows . length ) { table . modified . setRows ( rows , rowIndex ) ; table . modified . setColumn ( orderInColumn , modifier . modifyTable ( new DataTable ( table . getColumns ( [ orderByColumn , orderInColumn ] ) ) ) . modified . getColumn ( orderInColumn ) ) ; } else { modifier . modifyTable ( table , eventDetail ) ; } return table ; } param { DataTable } table param { DataEventEmitter . EventDetail } eventDetail return { DataTable } public modifyTable < T extends DataTable > ( table , eventDetail ? ) { const modifier = this ; modifier . emit ( { type : 'modify' , detail : eventDetail , table } ) ; const columnNames = table . getColumnNames ( ) , rowCount = table . getRowCount ( ) , rowReferences = table . getRows ( ) . map ( ( row , index ) : SortModifier . RowReference => ( { index , row } ) ) , { direction , orderByColumn , orderInColumn } = modifier . options , compare = ( direction === 'asc' ? SortModifier . ascending : SortModifier . descending ) , orderByColumnIndex = columnNames . indexOf ( orderByColumn ) , modified = table . modified ; if ( orderByColumnIndex !== - 1 ) { rowReferences . sort ( ( a , b ) : number => compare ( a . row [ orderByColumnIndex ] , b . row [ orderByColumnIndex ] ) ) ; } if ( orderInColumn ) { const column = [ ] ; for ( let i = 0 ; i < rowCount ; ++ i ) { column [ rowReferences [ i ] . index ] = i ; } modified . setColumns ( { [ orderInColumn ] : column } ) ; } else { const rows = [ ] ; for ( let i = 0 ; i < rowCount ; ++ i ) { rows . push ( rowReferences [ i ] . row ) ; } modified . setRows ( rows , 0 ) ; } modifier . emit ( { type : 'afterModify' , detail : eventDetail , table } ) ; return table ; } } /**
 * Additionally provided types for modifier events and options, and JSON
 * conversion.
 */ namespace SortModifier { /**
     * Options to configure the modifier.
     */ export interface Options extends DataModifier . Options { default direction : ( 'asc' | 'desc' ) ; default orderByColumn : string ; /**
         * Column to update with order index instead of change order of rows.
         */ orderInColumn ? : string ; } private export interface RowReference { index : number ; row : DataTable . Row ; } } export default SortModifier ;