-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.js
46 lines (42 loc) · 1 KB
/
container.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import {doNTimes} from './helper'
export default function ({element, windowSize}) {
const instance = {
append(...args){
for (let item of args) {
element.appendChild(item);
if (instance.length > windowSize) {
instance.dropBegin(instance.length - windowSize);
}
}
},
prepend(...args){
for (let item of args) {
element.insertBefore(item, element.firstChild);
if (instance.length > windowSize) {
instance.dropEnd(instance.length - windowSize);
}
}
},
dropBegin: doNTimes(() => {
const firstChild = element.firstChild;
if (firstChild) {
firstChild.remove();
}
}),
dropEnd: doNTimes(() => {
const lastChild = element.lastChild;
if (lastChild) {
lastChild.remove();
}
}),
empty(){
element.innerHTML = '';
}
};
Object.defineProperty(instance, 'length', {
get(){
return element.children.length;
}
});
return instance;
}