Skip to content

Commit b7d1a96

Browse files
committed
convert manager to ts
1 parent 5727bf2 commit b7d1a96

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/Manager.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
}

0 commit comments

Comments
 (0)