File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change 1+ import { find , sortBy } from 'lodash' ;
2+
3+ export interface SortableNode extends HTMLElement {
4+ sortableInfo : {
5+ index : number ,
6+ collection : string ,
7+ manager : Manager ,
8+ } ;
9+ }
10+
11+ export default class Manager {
12+ refs : { [ collection : string ] : HTMLElement [ ] } ;
13+ active : { collection : string , index : number } ;
14+
15+ constructor ( ) {
16+ this . refs = { } ;
17+ }
18+
19+ add ( collection : string , ref : HTMLElement ) {
20+ if ( ! this . refs [ collection ] ) {
21+ this . refs [ collection ] = [ ] ;
22+ }
23+
24+ this . refs [ collection ] . push ( ref ) ;
25+ }
26+
27+ remove ( collection : string , ref : HTMLElement ) {
28+ const index = this . getIndex ( collection , ref ) ;
29+
30+ if ( index !== - 1 ) {
31+ this . refs [ collection ] . splice ( index , 1 ) ;
32+ }
33+ }
34+
35+ isActive ( ) {
36+ return this . active ;
37+ }
38+
39+ getActive ( ) {
40+ return find (
41+ this . refs [ this . active . collection ] ,
42+ // eslint-disable-next-line eqeqeq
43+ ( { node } : { node : SortableNode } ) => node . sortableInfo . index == this . active . index
44+ ) ;
45+ }
46+
47+ getIndex ( collection : string , ref : HTMLElement ) {
48+ return this . refs [ collection ] . indexOf ( ref ) ;
49+ }
50+
51+ getOrderedRefs ( collection = this . active . collection ) {
52+ return sortBy ( this . refs [ collection ] , ( { node } : { node : SortableNode } ) => node . sortableInfo . index ) ;
53+ }
54+ }
You can’t perform that action at this time.
0 commit comments