Scrollable.js
4.17 KB
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
/**
* Scrollable.js
*
* Released under LGPL License.
* Copyright (c) 1999-2015 Ephox Corp. All rights reserved
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
/**
* This mixin makes controls scrollable using custom scrollbars.
*
* @-x-less Scrollable.less
* @mixin tinymce.ui.Scrollable
*/
define("tinymce/ui/Scrollable", [
"tinymce/dom/DomQuery",
"tinymce/ui/DragHelper"
], function($, DragHelper) {
"use strict";
return {
init: function() {
var self = this;
self.on('repaint', self.renderScroll);
},
renderScroll: function() {
var self = this, margin = 2;
function repaintScroll() {
var hasScrollH, hasScrollV, bodyElm;
function repaintAxis(axisName, posName, sizeName, contentSizeName, hasScroll, ax) {
var containerElm, scrollBarElm, scrollThumbElm;
var containerSize, scrollSize, ratio, rect;
var posNameLower, sizeNameLower;
scrollBarElm = self.getEl('scroll' + axisName);
if (scrollBarElm) {
posNameLower = posName.toLowerCase();
sizeNameLower = sizeName.toLowerCase();
$(self.getEl('absend')).css(posNameLower, self.layoutRect()[contentSizeName] - 1);
if (!hasScroll) {
$(scrollBarElm).css('display', 'none');
return;
}
$(scrollBarElm).css('display', 'block');
containerElm = self.getEl('body');
scrollThumbElm = self.getEl('scroll' + axisName + "t");
containerSize = containerElm["client" + sizeName] - (margin * 2);
containerSize -= hasScrollH && hasScrollV ? scrollBarElm["client" + ax] : 0;
scrollSize = containerElm["scroll" + sizeName];
ratio = containerSize / scrollSize;
rect = {};
rect[posNameLower] = containerElm["offset" + posName] + margin;
rect[sizeNameLower] = containerSize;
$(scrollBarElm).css(rect);
rect = {};
rect[posNameLower] = containerElm["scroll" + posName] * ratio;
rect[sizeNameLower] = containerSize * ratio;
$(scrollThumbElm).css(rect);
}
}
bodyElm = self.getEl('body');
hasScrollH = bodyElm.scrollWidth > bodyElm.clientWidth;
hasScrollV = bodyElm.scrollHeight > bodyElm.clientHeight;
repaintAxis("h", "Left", "Width", "contentW", hasScrollH, "Height");
repaintAxis("v", "Top", "Height", "contentH", hasScrollV, "Width");
}
function addScroll() {
function addScrollAxis(axisName, posName, sizeName, deltaPosName, ax) {
var scrollStart, axisId = self._id + '-scroll' + axisName, prefix = self.classPrefix;
$(self.getEl()).append(
'<div id="' + axisId + '" class="' + prefix + 'scrollbar ' + prefix + 'scrollbar-' + axisName + '">' +
'<div id="' + axisId + 't" class="' + prefix + 'scrollbar-thumb"></div>' +
'</div>'
);
self.draghelper = new DragHelper(axisId + 't', {
start: function() {
scrollStart = self.getEl('body')["scroll" + posName];
$('#' + axisId).addClass(prefix + 'active');
},
drag: function(e) {
var ratio, hasScrollH, hasScrollV, containerSize, layoutRect = self.layoutRect();
hasScrollH = layoutRect.contentW > layoutRect.innerW;
hasScrollV = layoutRect.contentH > layoutRect.innerH;
containerSize = self.getEl('body')["client" + sizeName] - (margin * 2);
containerSize -= hasScrollH && hasScrollV ? self.getEl('scroll' + axisName)["client" + ax] : 0;
ratio = containerSize / self.getEl('body')["scroll" + sizeName];
self.getEl('body')["scroll" + posName] = scrollStart + (e["delta" + deltaPosName] / ratio);
},
stop: function() {
$('#' + axisId).removeClass(prefix + 'active');
}
});
}
self.classes.add('scroll');
addScrollAxis("v", "Top", "Height", "Y", "Width");
addScrollAxis("h", "Left", "Width", "X", "Height");
}
if (self.settings.autoScroll) {
if (!self._hasScroll) {
self._hasScroll = true;
addScroll();
self.on('wheel', function(e) {
var bodyEl = self.getEl('body');
bodyEl.scrollLeft += (e.deltaX || 0) * 10;
bodyEl.scrollTop += e.deltaY * 10;
repaintScroll();
});
$(self.getEl('body')).on("scroll", repaintScroll);
}
repaintScroll();
}
}
};
});