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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import {
getElementOffset,
getScrollTop,
getAttributeValueByBreakpoint,
getUniqueIdWithPrefix,
getObjectPropertyValueByKey,
stringSnakeToCamel,
getCSS,
ElementAnimateUtil,
ElementStyleUtil,
EventHandlerUtil,
} from '../_utils/index';
export class StickyStore {
static store: Map<string, StickyComponent> = new Map()
public static set(instanceId: string, stickyComponentObj: StickyComponent): void {
if (StickyStore.has(instanceId)) {
return
}
StickyStore.store.set(instanceId, stickyComponentObj);
}
public static get(instanceId: string): StickyComponent | undefined {
if (!StickyStore.has(instanceId)) {
return
}
return StickyStore.store.get(instanceId);
}
public static remove(instanceId: string): void {
if (!StickyStore.has(instanceId)) {
return
}
StickyStore.store.delete(instanceId)
}
public static has(instanceId: string): boolean {
return StickyStore.store.has(instanceId);
}
public static getAllInstances() {
return StickyStore.store;
}
}
export interface StickyOptions {
offset: number
reverse: boolean
animation: boolean
animationSpeed: string
animationClass: string
}
const defaultStickyOptions: StickyOptions = {
offset: 200,
reverse: false,
animation: true,
animationSpeed: '0.3s',
animationClass: 'animation-slide-in-down',
}
class StickyComponent {
element: HTMLElement
options: StickyOptions
instanceUid: string
instanceName: string | null = ''
attributeName: string
eventTriggerState: boolean
lastScrollTop: number
constructor(_element: HTMLElement, options: StickyOptions) {
this.element = _element
this.options = Object.assign(defaultStickyOptions, options)
this.instanceUid = getUniqueIdWithPrefix('sticky')
this.instanceName = this.element.getAttribute('data-kt-sticky-name')
this.attributeName = 'data-kt-sticky-' + this.instanceName
this.eventTriggerState = true
this.lastScrollTop = 0
// Event Handlers
window.addEventListener('scroll', this.scroll)
// Initial Launch
this.scroll()
StickyStore.set(this.element.id, this)
}
private scroll = () => {
let offset: any = this.getOption('offset');
let releaseOffset: any = this.getOption('release-offset');
let reverse: any = this.getOption('reverse');
let st: any;
let diff: any;
// Exit if false
if ( offset === false ) {
return;
}
offset = parseInt(offset);
releaseOffset = releaseOffset ? parseInt(releaseOffset) : 0;
st = getScrollTop();
diff = document.documentElement.scrollHeight - window.innerHeight - getScrollTop();
if ( reverse === true ) { // Release on reverse scroll mode
if ( st > offset && (releaseOffset === 0 || releaseOffset < diff)) {
if (document.body.hasAttribute(this.attributeName) === false) {
this.enable();
document.body.setAttribute(this.attributeName, 'on');
}
if ( this.eventTriggerState === true ) {
EventHandlerUtil.trigger(this.element, 'kt.sticky.on');
EventHandlerUtil.trigger(this.element, 'kt.sticky.change');
this.eventTriggerState = false;
}
} else { // Back scroll mode
if (document.body.hasAttribute(this.attributeName) === true) {
this.disable();
document.body.removeAttribute(this.attributeName);
}
if ( this.eventTriggerState === false ) {
EventHandlerUtil.trigger(this.element, 'kt.sticky.off');
EventHandlerUtil.trigger(this.element, 'kt.sticky.change');
this.eventTriggerState = true;
}
}
this.lastScrollTop = st;
} else { // Classic scroll mode
if ( st>offset && (releaseOffset === 0 || releaseOffset < diff)) {
if (document.body.hasAttribute(this.attributeName) === false) {
this.enable();
document.body.setAttribute(this.attributeName, 'on');
}
if ( this.eventTriggerState === true ) {
EventHandlerUtil.trigger(this.element, 'kt.sticky.on');
EventHandlerUtil.trigger(this.element, 'kt.sticky.change');
this.eventTriggerState = false;
}
} else { // back scroll mode
if (document.body.hasAttribute(this.attributeName) === true) {
this.disable();
document.body.removeAttribute(this.attributeName);
}
if ( this.eventTriggerState === false ) {
EventHandlerUtil.trigger(this.element, 'kt.sticky.off');
EventHandlerUtil.trigger(this.element, 'kt.sticky.change');
this.eventTriggerState = true;
}
}
}
if (releaseOffset > 0) {
if (diff<releaseOffset) {
this.element.setAttribute('data-kt-sticky-released', 'true');
} else {
this.element.removeAttribute('data-kt-sticky-released');
}
}
}
private getOption = (name: string) => {
const dataStickyAttr = 'data-kt-sticky-' + name
if (this.element.hasAttribute(dataStickyAttr) === true) {
const attrValueInStr = this.element.getAttribute(dataStickyAttr)
const attrValue = getAttributeValueByBreakpoint(attrValueInStr || '')
if (attrValue !== null && String(attrValue) === 'true') {
return true
} else if (attrValue !== null && String(attrValue) === 'false') {
return false
}
return attrValue
} else {
const optionName = stringSnakeToCamel(name)
const option = getObjectPropertyValueByKey(this.options, optionName)
if (option) {
return getAttributeValueByBreakpoint(option)
}
}
}
private disable = () => {
ElementStyleUtil.remove(this.element, 'top')
ElementStyleUtil.remove(this.element, 'width')
ElementStyleUtil.remove(this.element, 'left')
ElementStyleUtil.remove(this.element, 'right')
ElementStyleUtil.remove(this.element, 'z-index')
ElementStyleUtil.remove(this.element, 'position')
}
private enable = (update: boolean = false) => {
const top = this.getOption('top')
const left = this.getOption('left')
// const right = this.getOption("right");
let width = this.getOption('width')
const zindex = this.getOption('zindex')
if (update !== true && this.getOption('animation') === true) {
ElementStyleUtil.set(this.element, 'animationDuration', this.getOption('animationSpeed'))
ElementAnimateUtil.animateClass(this.element, 'animation ' + this.getOption('animationClass'))
}
if (zindex !== null) {
ElementStyleUtil.set(this.element, 'z-index', zindex)
ElementStyleUtil.set(this.element, 'position', 'fixed')
}
if (top !== null) {
ElementStyleUtil.set(this.element, 'top', top)
}
if (width !== null && width !== undefined) {
const widthTarget = getObjectPropertyValueByKey(width, 'target')
if (widthTarget) {
const targetElement = document.querySelector(widthTarget)
if (targetElement) {
width = getCSS(targetElement, 'width')
}
}
ElementStyleUtil.set(this.element, 'width', width)
}
if (left !== null) {
if (String(left).toLowerCase() === 'auto') {
var offsetLeft = getElementOffset(this.element).left
if (offsetLeft > 0) {
ElementStyleUtil.set(this.element, 'left', String(offsetLeft) + 'px')
}
}
}
}
public update = () => {
if (document.body.hasAttribute(this.attributeName) === true) {
this.disable()
document.body.removeAttribute(this.attributeName)
this.enable(true)
document.body.setAttribute(this.attributeName, 'on')
}
}
// Event API
public on = (name: string, callBack: Function) => {
return EventHandlerUtil.on(this.element, name, callBack)
}
public one = (name: string, callback: Function) => {
return EventHandlerUtil.one(this.element, name, callback)
}
public off = (name: string) => {
return EventHandlerUtil.off(this.element, name)
}
public trigger = (name: string) => {
return EventHandlerUtil.trigger(this.element, name)
}
// Static methods
public static hasInstace(element: HTMLElement) {
return StickyStore.has(element.id)
}
public static getInstance(element: HTMLElement) {
if (element !== null && StickyComponent.hasInstace(element)) {
return StickyStore.get(element.id)
}
}
// Create Instances
public static createInstances(selector: string) {
const elements = document.body.querySelectorAll(selector)
elements.forEach((element) => {
const item = element as HTMLElement
let sticky = StickyComponent.getInstance(item)
if (!sticky) {
sticky = new StickyComponent(item, defaultStickyOptions)
} else {
sticky.element = item;
sticky.update();
}
})
}
public static createInsance = (
selector: string,
options: StickyOptions = defaultStickyOptions
): StickyComponent | undefined => {
const element = document.body.querySelector(selector)
if (!element) {
return
}
const item = element as HTMLElement
let sticky = StickyComponent.getInstance(item)
if (!sticky) {
sticky = new StickyComponent(item, options)
}
return sticky
}
public static bootstrap(attr: string = '[data-kt-sticky="true"]') {
StickyComponent.createInstances(attr)
}
public static reInitialization(attr: string = '[data-kt-sticky="true"]') {
StickyComponent.createInstances(attr)
}
}
export {StickyComponent, defaultStickyOptions}