-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kit):
Tiles
add reorder strategies (#10360)
Co-authored-by: d.kulagin <[email protected]> Co-authored-by: Alex Inkin <[email protected]>
- Loading branch information
1 parent
189ee07
commit 1277754
Showing
6 changed files
with
260 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
projects/kit/components/tiles/helpers/reorder-functions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
export type TuiReorderFunction = ( | ||
order: Map<number, number>, | ||
currentIndex: number, | ||
newIndex: number, | ||
) => Map<number, number>; | ||
|
||
export const tuiTileSwap: TuiReorderFunction = (order, currentIndex, newIndex) => { | ||
if (!order.has(currentIndex) || !order.has(newIndex)) { | ||
return order; | ||
} | ||
|
||
const dragged = order.get(currentIndex) ?? currentIndex; | ||
const placement = order.get(newIndex) ?? newIndex; | ||
|
||
const newOrder = new Map(order); | ||
|
||
newOrder.set(currentIndex, placement); | ||
newOrder.set(newIndex, dragged); | ||
|
||
return newOrder; | ||
}; | ||
|
||
export const tuiTileShift: TuiReorderFunction = (order, currentIndex, newIndex) => { | ||
if (!order.has(currentIndex) || !order.has(newIndex)) { | ||
return order; | ||
} | ||
|
||
const dragged = order.get(currentIndex) ?? currentIndex; | ||
const placement = order.get(newIndex) ?? newIndex; | ||
|
||
const reversedOrder = new Map( | ||
Array.from(order).map(([elemIndex, orderIndex]) => [orderIndex, elemIndex]), | ||
); | ||
|
||
const newOrder = new Map(order); | ||
|
||
const direction = (placement - dragged) / Math.abs(placement - dragged); | ||
|
||
if (direction > 0) { | ||
for (let i = placement; i > dragged; i--) { | ||
const tmp = reversedOrder.get(i) ?? i; | ||
|
||
newOrder.set(tmp, i - 1); | ||
} | ||
} else { | ||
for (let i = placement; i < dragged; i++) { | ||
const tmp = reversedOrder.get(i) ?? i; | ||
|
||
newOrder.set(tmp, i + 1); | ||
} | ||
} | ||
|
||
newOrder.set(currentIndex, placement); | ||
|
||
return newOrder; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
export * from './helpers/reorder-functions'; | ||
export * from './tile.component'; | ||
export * from './tile.service'; | ||
export * from './tile-handle.directive'; | ||
export * from './tile-reorder.provider'; | ||
export * from './tiles'; | ||
export * from './tiles.component'; |
181 changes: 181 additions & 0 deletions
181
projects/kit/components/tiles/test/reorder-functions.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import {tuiTileShift, tuiTileSwap} from '../helpers/reorder-functions'; | ||
|
||
describe('reorder functions', () => { | ||
describe('swap', () => { | ||
it('should swap 1 and 3', () => { | ||
const source = new Map([ | ||
[0, 0], | ||
[1, 1], | ||
[2, 2], | ||
[3, 3], | ||
]); | ||
|
||
/** | ||
* 1 -> 3 | ||
* 3 -> 1 | ||
*/ | ||
const result = tuiTileSwap(source, 1, 3); | ||
|
||
expect(result.get(0)).toBe(0); | ||
expect(result.get(1)).toBe(3); | ||
expect(result.get(2)).toBe(2); | ||
expect(result.get(3)).toBe(1); | ||
expect(result.size).toBe(source.size); | ||
}); | ||
|
||
it('should swap 3 and 1', () => { | ||
const source = new Map([ | ||
[0, 0], | ||
[1, 1], | ||
[2, 2], | ||
[3, 3], | ||
]); | ||
|
||
/** | ||
* 3 -> 1 | ||
* 1 -> 3 | ||
*/ | ||
const result = tuiTileSwap(source, 3, 1); | ||
|
||
expect(result.get(0)).toBe(0); | ||
expect(result.get(1)).toBe(3); | ||
expect(result.get(2)).toBe(2); | ||
expect(result.get(3)).toBe(1); | ||
expect(result.size).toBe(source.size); | ||
}); | ||
|
||
it('should return source order if currentIndex is out of range', () => { | ||
const source = new Map([ | ||
[0, 0], | ||
[1, 1], | ||
[2, 2], | ||
]); | ||
|
||
const result = tuiTileSwap(source, 3, 0); | ||
|
||
expect(result).toBe(result); | ||
expect(result.size).toBe(source.size); | ||
}); | ||
|
||
it('should return source order if newIndex is out of range', () => { | ||
const source = new Map([ | ||
[0, 0], | ||
[1, 1], | ||
[2, 2], | ||
]); | ||
|
||
const result = tuiTileSwap(source, 0, 3); | ||
|
||
expect(result).toBe(result); | ||
expect(result.size).toBe(source.size); | ||
}); | ||
|
||
it('should not change order if current and new indexes are the same', () => { | ||
const source = new Map([ | ||
[0, 0], | ||
[1, 1], | ||
[2, 2], | ||
]); | ||
|
||
const result = tuiTileSwap(source, 1, 1); | ||
|
||
expect(result.get(0)).toBe(0); | ||
expect(result.get(1)).toBe(1); | ||
expect(result.get(2)).toBe(2); | ||
expect(result.size).toBe(source.size); | ||
}); | ||
}); | ||
|
||
describe('shift', () => { | ||
it('should make right-left shift 1 -> 4', () => { | ||
const source = new Map([ | ||
[0, 0], | ||
[1, 1], | ||
[2, 2], | ||
[3, 3], | ||
[4, 4], | ||
]); | ||
|
||
/** | ||
* 1 -> 4 | ||
* 4 -> 3 | ||
* 3 -> 2 | ||
* 2 -> 1 | ||
*/ | ||
const result = tuiTileShift(source, 1, 4); | ||
|
||
expect(result.get(0)).toBe(0); | ||
expect(result.get(1)).toBe(4); | ||
expect(result.get(2)).toBe(1); | ||
expect(result.get(3)).toBe(2); | ||
expect(result.get(4)).toBe(3); | ||
expect(result.size).toBe(source.size); | ||
}); | ||
|
||
it('should make left-right shift 4 -> 1', () => { | ||
const source = new Map([ | ||
[0, 0], | ||
[1, 1], | ||
[2, 2], | ||
[3, 3], | ||
[4, 4], | ||
]); | ||
|
||
/** | ||
* 4 -> 1 | ||
* 1 -> 2 | ||
* 2 -> 3 | ||
* 3 -> 4 | ||
*/ | ||
const result = tuiTileShift(source, 4, 1); | ||
|
||
expect(result.get(0)).toBe(0); | ||
expect(result.get(1)).toBe(2); | ||
expect(result.get(2)).toBe(3); | ||
expect(result.get(3)).toBe(4); | ||
expect(result.get(4)).toBe(1); | ||
expect(result.size).toBe(source.size); | ||
}); | ||
|
||
it('should return source order if currentIndex is out of range', () => { | ||
const source = new Map([ | ||
[0, 0], | ||
[1, 1], | ||
[2, 2], | ||
]); | ||
|
||
const result = tuiTileShift(source, 3, 0); | ||
|
||
expect(result).toBe(result); | ||
expect(result.size).toBe(source.size); | ||
}); | ||
|
||
it('should return source order if newIndex is out of range', () => { | ||
const source = new Map([ | ||
[0, 0], | ||
[1, 1], | ||
[2, 2], | ||
]); | ||
|
||
const result = tuiTileShift(source, 0, 3); | ||
|
||
expect(result).toBe(result); | ||
expect(result.size).toBe(source.size); | ||
}); | ||
|
||
it('should not change order if current and new indexes are the same', () => { | ||
const source = new Map([ | ||
[0, 0], | ||
[1, 1], | ||
[2, 2], | ||
]); | ||
|
||
const result = tuiTileShift(source, 1, 1); | ||
|
||
expect(result.get(0)).toBe(0); | ||
expect(result.get(1)).toBe(1); | ||
expect(result.get(2)).toBe(2); | ||
expect(result.size).toBe(source.size); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import {tuiCreateToken} from '@taiga-ui/cdk/utils/miscellaneous'; | ||
|
||
import type {TuiReorderFunction} from './helpers/reorder-functions'; | ||
import {tuiTileSwap} from './helpers/reorder-functions'; | ||
|
||
export const TUI_TILE_REORDER = tuiCreateToken<TuiReorderFunction>(tuiTileSwap); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters