-
Notifications
You must be signed in to change notification settings - Fork 58
/
index.ts
164 lines (142 loc) · 4.46 KB
/
index.ts
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { Options } from '../options'
import {
removeClass, addClass, setStyle, setAttr, attachListener, detachListener
} from './dom'
import { State } from '../state/index'
import { Env } from '../env/index'
const shallowTag = 'data-shallow-slider'
const sliderTag = 'data-slider'
export type Renderer = {
init (): void
render (
state: State,
duration?: number,
cb?: Function,
force?: boolean
): void
destroy (): void
updateSize (): void
}
export function Renderer (
env: Env,
options: Options
): Renderer {
function updateItem (state: State): void {
const {
$wrapper
} = env.element
const {
index
} = state
$wrapper.querySelectorAll(`[${sliderTag}]`).forEach($slide => {
// eslint-disable-next-line no-bitwise
const tagNumber = ~~<string>$slide.getAttribute(sliderTag)
removeClass(<HTMLElement>$slide, [
options.slidePrevClass,
options.slideNextClass,
options.slideActiveClass
])
if (tagNumber === index) {
addClass(<HTMLElement>$slide, options.slideActiveClass)
}
if (tagNumber === index - 1) {
addClass(<HTMLElement>$slide, options.slidePrevClass)
}
if (tagNumber === index + 1) {
addClass(<HTMLElement>$slide, options.slideNextClass)
}
})
}
function render (
state: State,
duration?: number,
cb?: Function,
force?: false
) {
const {
$wrapper
} = env.element
const timing = duration === undefined ? options.speed : duration
options.injections.translate(state, env, options, timing)
// Update slide style only if scroll action is end.
if (!state.isStart) updateItem(state)
force && getComputedStyle($wrapper).transform
cb && setTimeout(cb, timing)
}
function appendExpandList (): void {
if (!options.loop) return
const {
element,
limitation
} = env
const {
$list,
$wrapper
} = element
const {
expand
} = limitation
const $leftExpandList = $list.slice(-expand)
.map($slide => <HTMLElement>$slide.cloneNode(true))
const $rightExpandList = $list.slice(0, expand)
.map($slide => <HTMLElement>$slide.cloneNode(true))
$leftExpandList.forEach(($shadowSlide, index) => {
$wrapper.appendChild(setAttr($rightExpandList[index], shallowTag))
$wrapper.insertBefore(setAttr($leftExpandList[index], shallowTag), $list[0])
})
}
function destroyExpandList (): void {
env.element.$wrapper
.querySelectorAll(`[${shallowTag}]`)
.forEach(item => env.element.$wrapper.removeChild(item))
}
function updateDom (): void {
env.element.$list.forEach((el, index) => setAttr(el, sliderTag, <any>index))
destroyExpandList()
appendExpandList()
}
function updateSize (): void {
const {
element,
measure
} = env
const {
$wrapper
} = element
const wrapperStyle = {
display: 'flex',
willChange: 'transform',
flexDirection: options.isHorizontal ? 'row' : 'column'
}
const itemStyle = {
[options.isHorizontal ? 'width' : 'height']: `${measure.slideSize}px`,
[options.isHorizontal ? 'margin-right' : 'margin-bottom']: `${options.spaceBetween}px`
}
setStyle($wrapper, wrapperStyle)
$wrapper.querySelectorAll(`[${sliderTag}]`)
.forEach($slide => setStyle(<HTMLElement>$slide, itemStyle))
}
function init (): void {
updateDom()
updateSize()
}
function destroy (): void {
const {
$list,
$wrapper
} = env.element
const arr = ['display', 'will-change', 'flex-direction']
const itemProp = options.isHorizontal ? 'margin-right' : 'margin-bottom'
arr.forEach((propertyName: string) => {
$wrapper.style.removeProperty(propertyName)
})
$list.forEach($slide => $slide.style.removeProperty(itemProp))
destroyExpandList()
}
return {
init,
render,
destroy,
updateSize
}
}