-
Notifications
You must be signed in to change notification settings - Fork 124
/
baron.js
1748 lines (1434 loc) · 52.4 KB
/
baron.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 5);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
// Test via a getter in the options object to see if the passive property is accessed
// https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md#feature-detection
var supportsPassive = false
try {
var opts = Object.defineProperty({}, 'passive', {
get: function() {
supportsPassive = true
}
})
window.addEventListener('test', null, opts)
} catch (e) {
// pass
}
module.exports.event = function event(elem, _eventNames, handler, mode) {
var eventNames = _eventNames.split(' ')
var prefix = mode == 'on' ? 'add' : 'remove'
eventNames.forEach(function(eventName) {
var options = false
if (['scroll', 'touchstart', 'touchmove'].indexOf(eventName) != -1 && supportsPassive) {
options = { passive: true }
}
elem[prefix + 'EventListener'](eventName, handler, options)
})
}
function each(obj, handler) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
handler(key, obj[key])
}
}
}
module.exports.css = function css(node, key, value) {
var styles
if (value === undefined) {
// Getter mode
if (typeof key == 'string') {
return node.style[key]
}
styles = key
} else {
styles = {}
styles[key] = value
}
each(styles, function(k, val) {
node.style[k] = val
})
}
module.exports.add = function add(node, cls) {
if (!cls) {
return
}
node.classList.add(cls)
}
module.exports.rm = function add(node, cls) {
if (!cls) {
return
}
node.classList.remove(cls)
}
module.exports.has = function has(node, cls) {
if (!cls) {
return false
}
return node.classList.contains(cls)
}
module.exports.clone = function clone(_input) {
var output = {}
var input = _input || {}
each(input, function(key, value) {
output[key] = value
})
return output
}
module.exports.qs = function qs(selector, _ctx) {
if (selector instanceof HTMLElement) {
return selector
}
var ctx = _ctx || document
return ctx.querySelector(selector)
}
module.exports.each = each
/***/ }),
/* 1 */
/***/ (function(module, exports) {
// shim for using process in browser
var process = module.exports = {};
// cached from whatever global is present so that test runners that stub it
// don't break things. But we need to wrap it in a try catch in case it is
// wrapped in strict mode code which doesn't define any globals. It's inside a
// function because try/catches deoptimize in certain engines.
var cachedSetTimeout;
var cachedClearTimeout;
function defaultSetTimout() {
throw new Error('setTimeout has not been defined');
}
function defaultClearTimeout () {
throw new Error('clearTimeout has not been defined');
}
(function () {
try {
if (typeof setTimeout === 'function') {
cachedSetTimeout = setTimeout;
} else {
cachedSetTimeout = defaultSetTimout;
}
} catch (e) {
cachedSetTimeout = defaultSetTimout;
}
try {
if (typeof clearTimeout === 'function') {
cachedClearTimeout = clearTimeout;
} else {
cachedClearTimeout = defaultClearTimeout;
}
} catch (e) {
cachedClearTimeout = defaultClearTimeout;
}
} ())
function runTimeout(fun) {
if (cachedSetTimeout === setTimeout) {
//normal enviroments in sane situations
return setTimeout(fun, 0);
}
// if setTimeout wasn't available but was latter defined
if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
cachedSetTimeout = setTimeout;
return setTimeout(fun, 0);
}
try {
// when when somebody has screwed with setTimeout but no I.E. maddness
return cachedSetTimeout(fun, 0);
} catch(e){
try {
// When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
return cachedSetTimeout.call(null, fun, 0);
} catch(e){
// same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error
return cachedSetTimeout.call(this, fun, 0);
}
}
}
function runClearTimeout(marker) {
if (cachedClearTimeout === clearTimeout) {
//normal enviroments in sane situations
return clearTimeout(marker);
}
// if clearTimeout wasn't available but was latter defined
if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
cachedClearTimeout = clearTimeout;
return clearTimeout(marker);
}
try {
// when when somebody has screwed with setTimeout but no I.E. maddness
return cachedClearTimeout(marker);
} catch (e){
try {
// When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
return cachedClearTimeout.call(null, marker);
} catch (e){
// same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error.
// Some versions of I.E. have different rules for clearTimeout vs setTimeout
return cachedClearTimeout.call(this, marker);
}
}
}
var queue = [];
var draining = false;
var currentQueue;
var queueIndex = -1;
function cleanUpNextTick() {
if (!draining || !currentQueue) {
return;
}
draining = false;
if (currentQueue.length) {
queue = currentQueue.concat(queue);
} else {
queueIndex = -1;
}
if (queue.length) {
drainQueue();
}
}
function drainQueue() {
if (draining) {
return;
}
var timeout = runTimeout(cleanUpNextTick);
draining = true;
var len = queue.length;
while(len) {
currentQueue = queue;
queue = [];
while (++queueIndex < len) {
if (currentQueue) {
currentQueue[queueIndex].run();
}
}
queueIndex = -1;
len = queue.length;
}
currentQueue = null;
draining = false;
runClearTimeout(timeout);
}
process.nextTick = function (fun) {
var args = new Array(arguments.length - 1);
if (arguments.length > 1) {
for (var i = 1; i < arguments.length; i++) {
args[i - 1] = arguments[i];
}
}
queue.push(new Item(fun, args));
if (queue.length === 1 && !draining) {
runTimeout(drainQueue);
}
};
// v8 likes predictible objects
function Item(fun, array) {
this.fun = fun;
this.array = array;
}
Item.prototype.run = function () {
this.fun.apply(null, this.array);
};
process.title = 'browser';
process.browser = true;
process.env = {};
process.argv = [];
process.version = ''; // empty string to avoid regexp issues
process.versions = {};
function noop() {}
process.on = noop;
process.addListener = noop;
process.once = noop;
process.off = noop;
process.removeListener = noop;
process.removeAllListeners = noop;
process.emit = noop;
process.prependListener = noop;
process.prependOnceListener = noop;
process.listeners = function (name) { return [] }
process.binding = function (name) {
throw new Error('process.binding is not supported');
};
process.cwd = function () { return '/' };
process.chdir = function (dir) {
throw new Error('process.chdir is not supported');
};
process.umask = function() { return 0; };
/***/ }),
/* 2 */
/***/ (function(module, exports) {
module.exports = function log(level, msg, more) {
var func = console[level] || console.log
var args = [
'Baron: ' + msg,
more
]
Function.prototype.apply.call(func, console, args)
}
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
/* WEBPACK VAR INJECTION */(function(process) {
var g = (function() {
return this || (1, eval)('this')
}())
var scopedWindow = g && g.window || g
var event = __webpack_require__(0).event
var css = __webpack_require__(0).css
var add = __webpack_require__(0).add
var has = __webpack_require__(0).has
var rm = __webpack_require__(0).rm
var clone = __webpack_require__(0).clone
var qs = __webpack_require__(0).qs
var _baron = baron // Stored baron value for noConflict usage
// var Item = {}
var pos = ['left', 'top', 'right', 'bottom', 'width', 'height']
// Global store for all baron instances (to be able to dispose them on html-nodes)
var instances = []
var origin = {
v: { // Vertical
x: 'Y', pos: pos[1], oppos: pos[3], crossPos: pos[0], crossOpPos: pos[2],
size: pos[5],
crossSize: pos[4], crossMinSize: 'min-' + pos[4], crossMaxSize: 'max-' + pos[4],
client: 'clientHeight', crossClient: 'clientWidth',
scrollEdge: 'scrollLeft',
offset: 'offsetHeight', crossOffset: 'offsetWidth', offsetPos: 'offsetTop',
scroll: 'scrollTop', scrollSize: 'scrollHeight'
},
h: { // Horizontal
x: 'X', pos: pos[0], oppos: pos[2], crossPos: pos[1], crossOpPos: pos[3],
size: pos[4],
crossSize: pos[5], crossMinSize: 'min-' + pos[5], crossMaxSize: 'max-' + pos[5],
client: 'clientWidth', crossClient: 'clientHeight',
scrollEdge: 'scrollTop',
offset: 'offsetWidth', crossOffset: 'offsetHeight', offsetPos: 'offsetLeft',
scroll: 'scrollLeft', scrollSize: 'scrollWidth'
}
}
// Some ugly vars
var opera12maxScrollbarSize = 17
// I hate you https://github.com/Diokuz/baron/issues/110
var macmsxffScrollbarSize = 15
var macosxffRe = /[\s\S]*Macintosh[\s\S]*\) Gecko[\s\S]*/
var isMacFF = macosxffRe.test(scopedWindow.navigator && scopedWindow.navigator.userAgent)
var log, liveBarons, shownErrors
if (process.env.NODE_ENV !== 'production') {
log = __webpack_require__(2)
liveBarons = 0
shownErrors = {
liveTooMany: false,
allTooMany: false
}
}
// window.baron and jQuery.fn.baron points to this function
function baron(user) {
var withParams = !!user
var tryNode = (user && user[0]) || user
var isNode = typeof user == 'string' || tryNode instanceof HTMLElement
var params = isNode ? { root: user } : clone(user)
var jQueryMode
var rootNode
var defaultParams = {
direction: 'v',
barOnCls: '_scrollbar',
resizeDebounce: 0,
event: event,
cssGuru: false,
impact: 'scroller',
position: 'static'
}
params = params || {}
// Extending default params by user-defined params
for (var key in defaultParams) {
if (params[key] == null) { // eslint-disable-line
params[key] = defaultParams[key]
}
}
if (process.env.NODE_ENV !== 'production') {
if (params.position == 'absolute' && params.impact == 'clipper') {
log('error', [
'Simultaneous use of `absolute` position and `clipper` impact values detected.',
'Those values cannot be used together.',
'See more https://github.com/Diokuz/baron/issues/138'
].join(' '), params)
}
}
// `this` could be a jQuery instance
jQueryMode = this && this instanceof scopedWindow.jQuery
if (params._chain) {
rootNode = params.root
} else if (jQueryMode) {
params.root = rootNode = this[0]
} else {
rootNode = qs(params.root || params.scroller)
}
if (process.env.NODE_ENV !== 'production') {
if (!rootNode) {
log('error', [
'Baron initialization failed: root node not found.'
].join(', '), params)
return // or return baron-shell?
}
}
var attr = manageAttr(rootNode, params.direction)
var id = +attr // Could be NaN
params.index = id
// baron() can return existing instances,
// @TODO update params on-the-fly
// https://github.com/Diokuz/baron/issues/124
if (id == id && attr !== null && instances[id]) {
if (process.env.NODE_ENV !== 'production') {
if (withParams) {
log('error', [
'repeated initialization for html-node detected',
'https://github.com/Diokuz/baron/blob/master/docs/logs/repeated.md'
].join(', '), params.root)
}
}
return instances[id]
}
// root and scroller can be different nodes
if (params.root && params.scroller) {
params.scroller = qs(params.scroller, rootNode)
if (process.env.NODE_ENV !== 'production') {
if (!params.scroller) {
log('error', 'Scroller not found!', rootNode, params.scroller)
}
}
} else {
params.scroller = rootNode
}
params.root = rootNode
var instance = init(params)
if (instance.autoUpdate) {
instance.autoUpdate()
}
return instance
}
function arrayEach(_obj, iterator) {
var i = 0
var obj = _obj
if (obj.length === undefined || obj === scopedWindow) obj = [obj]
while (obj[i]) {
iterator.call(this, obj[i], i)
i++
}
}
// shortcut for getTime
function getTime() {
return new Date().getTime()
}
if (process.env.NODE_ENV !== 'production') {
baron._instances = instances
}
function manageEvents(item, eventManager, mode) {
// Creating new functions for one baron item only one time
item._eventHandlers = item._eventHandlers || [
{
// onScroll:
element: item.scroller,
handler: function(e) {
item.scroll(e)
},
type: 'scroll'
}, {
// css transitions & animations
element: item.root,
handler: function() {
item.update()
},
type: 'transitionend animationend'
}, {
// onKeyup (textarea):
element: item.scroller,
handler: function() {
item.update()
},
type: 'keyup'
}, {
// onMouseDown:
element: item.bar,
handler: function(e) {
e.preventDefault() // Text selection disabling in Opera
item.selection() // Disable text selection in ie8
item.drag.now = 1 // Save private byte
if (item.draggingCls) {
add(item.root, item.draggingCls)
}
},
type: 'touchstart mousedown'
}, {
// onMouseUp:
element: document,
handler: function() {
item.selection(1) // Enable text selection
item.drag.now = 0
if (item.draggingCls) {
rm(item.root, item.draggingCls)
}
},
type: 'mouseup blur touchend'
}, {
// onCoordinateReset:
element: document,
handler: function(e) {
if (e.button != 2) { // Not RM
item._pos0(e)
}
},
type: 'touchstart mousedown'
}, {
// onMouseMove:
element: document,
handler: function(e) {
if (item.drag.now) {
item.drag(e)
}
},
type: 'mousemove touchmove'
}, {
// @TODO make one global listener
// onResize:
element: scopedWindow,
handler: function() {
item.update()
},
type: 'resize'
}, {
// @todo remove
// sizeChange:
element: item.root,
handler: function() {
item.update()
},
type: 'sizeChange'
}, {
// Clipper onScroll bug https://github.com/Diokuz/baron/issues/116
element: item.clipper,
handler: function() {
item.clipperOnScroll()
},
type: 'scroll'
}
]
arrayEach(item._eventHandlers, function(evt) {
if (evt.element) {
// workaround for element-elements in `fix` plugin
// @todo dispose `fix` in proper way and remove workaround
if (evt.element.length && evt.element !== scopedWindow) {
for (var i = 0; i < evt.element.length; i++) {
eventManager(evt.element[i], evt.type, evt.handler, mode)
}
} else {
eventManager(evt.element, evt.type, evt.handler, mode)
}
}
})
// if (item.scroller) {
// event(item.scroller, 'scroll', item._eventHandlers.onScroll, mode)
// }
// if (item.bar) {
// event(item.bar, 'touchstart mousedown', item._eventHandlers.onMouseDown, mode)
// }
// event(document, 'mouseup blur touchend', item._eventHandlers.onMouseUp, mode)
// event(document, 'touchstart mousedown', item._eventHandlers.onCoordinateReset, mode)
// event(document, 'mousemove touchmove', item._eventHandlers.onMouseMove, mode)
// event(window, 'resize', item._eventHandlers.onResize, mode)
// if (item.root) {
// event(item.root, 'sizeChange', item._eventHandlers.onResize, mode)
// // Custon event for alternate baron update mechanism
// }
}
// set, remove or read baron-specific id-attribute
// @returns {String|null} - id node value, or null, if there is no attr
function manageAttr(node, direction, mode, id) {
var attrName = 'data-baron-' + direction + '-id'
if (mode == 'on') {
node.setAttribute(attrName, id)
} else if (mode == 'off') {
node.removeAttribute(attrName)
}
return node.getAttribute(attrName)
}
function init(params) {
var out = new baron.prototype.constructor(params)
manageEvents(out, params.event, 'on')
manageAttr(out.root, params.direction, 'on', instances.length)
instances.push(out)
if (process.env.NODE_ENV !== 'production') {
liveBarons++
if (liveBarons > 100 && !shownErrors.liveTooMany) {
log('warn', [
'You have too many live baron instances on page (' + liveBarons + ')!',
'Are you forget to dispose some of them?',
'All baron instances can be found in baron._instances:'
].join(' '), instances)
shownErrors.liveTooMany = true
}
if (instances.length > 1000 && !shownErrors.allTooMany) {
log('warn', [
'You have too many inited baron instances on page (' + instances.length + ')!',
'Some of them are disposed, and thats good news.',
'but baron.init was call too many times, and thats is bad news.',
'All baron instances can be found in baron._instances:'
].join(' '), instances)
shownErrors.allTooMany = true
}
}
out.update()
return out
}
function fire(eventName) {
if (this.events && this.events[eventName]) {
for (var i = 0; i < this.events[eventName].length; i++) {
var args = Array.prototype.slice.call( arguments, 1 )
this.events[eventName][i].apply(this, args)
}
}
}
baron.prototype = {
// underscore.js realization
// used in autoUpdate plugin
_debounce: function(func, wait) {
var self = this,
timeout,
// args, // right now there is no need for arguments
// context, // and for context
timestamp
// result // and for result
var later = function() {
if (self._disposed) {
clearTimeout(timeout)
timeout = self = null
return
}
var last = getTime() - timestamp
if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last)
} else {
timeout = null
// result = func.apply(context, args)
func()
// context = args = null
}
}
return function() {
// context = this
// args = arguments
timestamp = getTime()
if (!timeout) {
timeout = setTimeout(later, wait)
}
// return result
}
},
constructor: function(params) {
var barPos,
scrollerPos0,
track,
resizePauseTimer,
scrollingTimer,
resizeLastFire,
oldBarSize
resizeLastFire = getTime()
this.params = params
this.event = params.event
this.events = {}
// DOM elements
this.root = params.root // Always html node, not just selector
this.scroller = qs(params.scroller)
if (process.env.NODE_ENV !== 'production') {
if (this.scroller.tagName == 'body') {
log('error', [
'Please, do not use BODY as a scroller.',
'https://github.com/Diokuz/baron/blob/master/docs/logs/do-not-use-body.md'
].join(', '), params)
}
}
this.bar = qs(params.bar, this.root)
track = this.track = qs(params.track, this.root)
if (!this.track && this.bar) {
track = this.bar.parentNode
}
this.clipper = this.scroller.parentNode
// Parameters
this.direction = params.direction
this.rtl = params.rtl
this.origin = origin[this.direction]
this.barOnCls = params.barOnCls
this.scrollingCls = params.scrollingCls
this.draggingCls = params.draggingCls
this.impact = params.impact
this.position = params.position
this.rtl = params.rtl
this.barTopLimit = 0
this.resizeDebounce = params.resizeDebounce
// Updating height or width of bar
function setBarSize(_size) {
var barMinSize = this.barMinSize || 20
var size = _size
if (size > 0 && size < barMinSize) {
size = barMinSize
}
if (this.bar) {
css(this.bar, this.origin.size, parseInt(size, 10) + 'px')
}
}
// Updating top or left bar position
function posBar(_pos) {
if (this.bar) {
var was = css(this.bar, this.origin.pos),
will = +_pos + 'px'
if (will && will != was) {
css(this.bar, this.origin.pos, will)
}
}
}
// Free path for bar
function k() {
return track[this.origin.client] - this.barTopLimit - this.bar[this.origin.offset]
}
// Relative content top position to bar top position
function relToPos(r) {
return r * k.call(this) + this.barTopLimit
}
// Bar position to relative content position
function posToRel(t) {
return (t - this.barTopLimit) / k.call(this)
}
// Cursor position in main direction in px // Now with iOs support
this.cursor = function(e) {
return e['client' + this.origin.x] ||
(((e.originalEvent || e).touches || {})[0] || {})['page' + this.origin.x]
}
// Text selection pos preventing
function dontPosSelect() {
return false
}
this.pos = function(x) { // Absolute scroller position in px
var ie = 'page' + this.origin.x + 'Offset',
key = (this.scroller[ie]) ? ie : this.origin.scroll
if (x !== undefined) this.scroller[key] = x
return this.scroller[key]
}
this.rpos = function(r) { // Relative scroller position (0..1)
var free = this.scroller[this.origin.scrollSize] - this.scroller[this.origin.client],
x
if (r) {
x = this.pos(r * free)
} else {
x = this.pos()
}
return x / (free || 1)
}
// Switch on the bar by adding user-defined CSS classname to scroller
this.barOn = function(dispose) {
if (this.barOnCls) {
var noScroll = this.scroller[this.origin.client] >= this.scroller[this.origin.scrollSize]
if (dispose || noScroll) {
if (has(this.root, this.barOnCls)) {
rm(this.root, this.barOnCls)
}
} else if (!has(this.root, this.barOnCls)) {
add(this.root, this.barOnCls)
}
}
}
this._pos0 = function(e) {
scrollerPos0 = this.cursor(e) - barPos
}
this.drag = function(e) {
var rel = posToRel.call(this, this.cursor(e) - scrollerPos0)
var sub = (this.scroller[this.origin.scrollSize] - this.scroller[this.origin.client])
this.scroller[this.origin.scroll] = rel * sub
}
// Text selection preventing on drag
this.selection = function(enable) {
this.event(document, 'selectpos selectstart', dontPosSelect, enable ? 'off' : 'on')
}
// onResize & DOM modified handler
// also fires on init
// Note: max/min-size didnt sets if size did not really changed (for example, on init in Chrome)
this.resize = function() {
var self = this
var minPeriod = (self.resizeDebounce === undefined) ? 300 : self.resizeDebounce
var delay = 0
if (getTime() - resizeLastFire < minPeriod) {
clearTimeout(resizePauseTimer)
delay = minPeriod
}
function upd() {
var offset = self.scroller[self.origin.crossOffset]
var client = self.scroller[self.origin.crossClient]
var padding = 0
var was, will
// https://github.com/Diokuz/baron/issues/110
if (isMacFF) {
padding = macmsxffScrollbarSize
// Opera 12 bug https://github.com/Diokuz/baron/issues/105
} else if (client > 0 && offset === 0) {
// Only Opera 12 in some rare nested flexbox cases goes here
// Sorry guys for magic,
// but I dont want to create temporary html-nodes set
// just for measuring scrollbar size in Opera 12.
// 17px for Windows XP-8.1, 15px for Mac (really rare).
offset = client + opera12maxScrollbarSize
}