1- import 'simplebar/src/simplebar.js'
1+ import {
2+ SCROLLABLE_SELECTOR ,
3+ SCROLLABLE_DATA_KEY ,
4+ SCROLLABLE_DATA ,
5+ SCROLLABLE_EVENTS
6+ } from './config'
27
3- export default function ( ) {
8+ class Scrollable {
49
5- $ . fn . blScrollable = function ( ) {
10+ constructor ( ) {
11+ jQuery ( SCROLLABLE_SELECTOR ) . each ( el => this . init ( jQuery ( el ) ) )
12+ }
13+
14+ init ( el ) {
615
7- if ( ! this . length ) return
8- if ( this . length > 1 ) {
9- this . each ( function ( ) {
10- $ ( this ) . blScrollable ( )
11- } )
16+ if ( jQuery . fn . simplebar === undefined ) {
17+ return
18+ }
19+
20+ if ( el . data ( SCROLLABLE_DATA_KEY ) ) {
1221 return
1322 }
1423
15- var el = this
24+ el . data ( SCROLLABLE_DATA_KEY , true )
1625 el . addClass ( 'simplebar' )
17-
18- if ( el . data ( 'horizontal' ) ) {
19- el . addClass ( 'horizontal' )
26+ if ( el . data ( 'scrollable-direction' ) === 'horizontal' ) {
27+ el . addClass ( 'simplebar-horizontal' )
2028 }
29+ el . simplebar ( )
2130
22- if ( $ . fn . simplebar === undefined ) {
23- el . css ( 'overflow-y' , 'scroll' )
24- return
25- }
31+ el . simplebar ( ) . on ( 'scroll' , ( e ) => {
32+ const scrollable = jQuery ( e . target )
33+ const scrollTop = scrollable . simplebar ( 'getScrollElement' ) . scrollTop ( )
34+ scrollable . trigger ( SCROLLABLE_EVENTS . scroll , [ scrollTop ] )
2635
27- el . simplebar ( )
28- el . simplebar ( ) . on ( 'scroll' , function ( ) {
29- var scrollable = $ ( this )
30- let scrollTop = scrollable . simplebar ( 'getScrollElement' ) . scrollTop ( )
31- $ ( 'body' ) . trigger ( 'scrolling.bl.scrollable' , [ scrollTop ] )
32- clearTimeout ( this . scrollTimer )
33- this . scrollTimer = setTimeout ( function ( ) {
36+ clearTimeout ( scrollable . data ( SCROLLABLE_DATA . scrollTimer ) )
37+ scrollable . data ( SCROLLABLE_DATA . scrollTimer , setTimeout ( ( ) => {
3438 let scrollTop = scrollable . simplebar ( 'getScrollElement' ) . scrollTop ( )
35- $ ( 'body' ) . trigger ( 'end-scrolling.bl.scrollable' , [ scrollTop ] )
36- } , 100 )
39+ scrollable . trigger ( SCROLLABLE_EVENTS . scrollEnd , [ scrollTop ] )
40+ } , 100 ) )
3741 } )
38- el . on ( 'scroll-to.bl.scrollable' , ( id ) => {
39- let toElement = document . querySelector ( id )
42+
43+ el . on ( SCROLLABLE_EVENTS . scrollTo , ( id ) => {
44+ const toElement = document . querySelector ( id )
4045 if ( toElement ) {
4146 el . simplebar ( 'getScrollElement' ) . animate ( {
4247 scrollTop : toElement . offsetTop
4348 } )
4449 }
4550 } )
4651 }
47-
48- $ ( '[data-scrollable]' ) . blScrollable ( )
52+
53+ destroy ( el ) {
54+ el . off ( [ SCROLLABLE_EVENTS . scroll , SCROLLABLE_EVENTS . scrollTo , SCROLLABLE_EVENTS . scrollEnd ] . join ( ' ' ) )
55+ el . removeData ( [ SCROLLABLE_DATA . scrollTimer , SCROLLABLE_DATA_KEY ] )
56+ }
4957}
5058
51- module . exports = exports . default
59+ // export instance
60+ export default new Scrollable ( )
61+
62+ // export class
63+ export { Scrollable }
0 commit comments