-
Notifications
You must be signed in to change notification settings - Fork 352
/
styled-scroll-bar.js
199 lines (166 loc) · 5.19 KB
/
styled-scroll-bar.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
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
define(function(require, exports, module) {
'use strict';
const NAME = 'styledScrollBar';
const DATA_KEY = 'oro.' + NAME;
const $ = require('jquery');
const _ = require('underscore');
const BaseClass = require('oroui/js/base-class');
const error = require('oroui/js/error');
const OverlayScrollBars = require('overlayScrollbars');
const config = require('module-config').default(module.id);
const allowedConfig = _.omit(config, 'callbacks');
const ScrollBar = BaseClass.extend({
scrollBar: null,
/**
* @inheritdoc
*/
cidPrefix: 'scrollBar',
/**
* @inheritdoc
*/
listen: function() {
const listenTo = {};
listenTo['layout:reposition mediator'] = _.debounce(this.update.bind(this),
this.options('autoUpdateInterval'));
return listenTo;
},
/**
* @inheritdoc
*/
constructor: function ScrollBar(options, element) {
this.element = element;
ScrollBar.__super__.constructor.call(this, options, element);
},
/**
* @inheritdoc
*/
initialize: function(options) {
this.defaults = $.extend(true, {}, $.fn[NAME].defaults, allowedConfig, options || {});
this.init();
$(window).on(`beforeprint${this.eventNamespace()}`, () => this.destroy());
$(window).on(`afterprint${this.eventNamespace()}`, () => this.init());
},
eventNamespace: function() {
return '.delegateEvents' + this.cid;
},
/**
* Initialize scroll bar
*/
init() {
if (this.scrollBar) {
this.destroy();
}
this.scrollBar = new OverlayScrollBars(this.element, this.defaults);
},
/**
* destroy scroll bar
*/
destroy() {
if (this.scrollBar) {
this.scrollBar.destroy();
delete this.scrollBar;
}
},
/**
* Destroy plugin
*/
dispose: function() {
if (this.disposed) {
return;
}
this.destroy();
$.removeData(this.element, DATA_KEY);
$(window).off(this.eventNamespace());
delete this.defaults;
ScrollBar.__super__.dispose.call(this);
},
/**
* Proxy for OverlayScrollBars.options method
* @returns {Object|undefined}
*/
options: function(...args) {
return this.scrollBar.options(...args);
},
/**
* Proxy for OverlayScrollBars.update method
*/
update: function(...args) {
if (!this.scrollBar || this.disposed) {
return;
}
this.scrollBar.update(...args);
},
/**
* Proxy for OverlayScrollBars.sleep method
*/
sleep: function() {
this.scrollBar.sleep();
},
/**
* Proxy for OverlayScrollBars.scroll method
* @returns {Object|undefined}
*/
scroll: function(...args) {
return this.scrollBar.scroll(...args);
},
/**
* Proxy for OverlayScrollBars.scrollStop method
* @returns {Object}
*/
scrollStop: function() {
return this.scrollBar.scrollStop();
},
/**
* Proxy for OverlayScrollBars.getElements method
* @returns {Object}
*/
getElements: function() {
return this.scrollBar.getElements();
},
/**
* Proxy for OverlayScrollBars.getState method
* @returns {Object}
*/
getState: function() {
return this.scrollBar.getState();
},
/**
* @returns {Object}
*/
getDefaults: function() {
return $.extend(true, {}, $.fn[NAME].defaults);
}
});
$.fn[NAME] = function(options, ...args) {
const isMethodCall = typeof options === 'string';
let response = this;
this.each(function(index) {
const $element = $(this);
const instance = $element.data(DATA_KEY);
if (!instance) {
$element.data(DATA_KEY, new ScrollBar(options, this));
return;
}
if (isMethodCall) {
if (options === 'instance') {
response = instance;
return response;
}
if (!_.isFunction(instance[options]) || options.charAt(0) === '_') {
error.showErrorInConsole(new Error('Instance ' + NAME + ' doesn\'t support method ' + options ));
return false;
}
const result = instance[options](...args);
if (result !== void 0 && index === 0) {
response = result;
}
}
});
return response;
};
$.fn[NAME].constructor = ScrollBar;
$.fn[NAME].defaults = {
className: 'os-theme-dark',
autoUpdateInterval: 50
};
});