aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/ui/scroll-bars.reel/scroll-bars.js
blob: 4a9543996a2902005323588d3559d9ea20c076d9 (plain)
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
var Montage = require("montage").Montage,
    Component = require("ui/component").Component;

var ScrollBars = exports.ScrollBars = Montage.create(Component, {

    // Scroll and length are defined in a [0..1] range

    _verticalScroll: {
        enumerable: false,
        value: 0
    },

    _horizontalScroll: {
        enumerable: false,
        value: 0
    },

    _verticalLength: {
        enumerable: false,
        value: 0
    },

    _horizontalLength: {
        enumerable: false,
        value: 0
    },

    verticalScroll: {
        get: function () {
            return this._verticalScroll;
        },
        set: function (value) {
            this._verticalScroll = value;
            this.needsDraw = true;
        }
    },

    horizontalScroll: {
        get: function () {
            return this._horizontalScroll;
        },
        set: function (value) {
            this._horizontalScroll = value;
            this.needsDraw = true;
        }
    },

    verticalLength: {
        get: function () {
            return this._verticalLength;
        },
        set: function (value) {
            this._verticalLength = value;
            this.needsDraw = true;
        }
    },

    horizontalLength: {
        get: function () {
            return this._horizontalLength;
        },
        set: function (value) {
            this._horizontalLength = value;
            this.needsDraw = true;
        }
    },

    _opacity: {
        enumerable: false,
        value: 0
    },

    opacity: {
        get: function () {
            return this._opacity;
        },
        set: function (value) {
            this._opacity = value;
            this.needsDraw = true;
        }
    },

    _isDisplayUpdated: {
        enumerable: false,
        value: false
    },

    _displayVertical: {
        enumerable: false,
        value: false
    },

    displayVertical: {
        get: function () {
            return this._displayVertical;
        },
        set: function (value) {
            if (this._displayVertical !== value) {
                this._displayVertical = value;
                this._isDisplayUpdated = true;
                this.needsDraw = true;
            }
        }
    },

    _displayHorizontal: {
        enumerable: false,
        value: false
    },

    displayHorizontal: {
        get: function () {
            return this._displayHorizontal;
        },
        set: function (value) {
            if (this._displayHorizontal !== value) {
                this._displayHorizontal = value;
                this._isDisplayUpdated = true;
                this.needsDraw = true;
            }
        }
    },

    _hasResizedHorizontal: {
        enumerable: false,
        value: false
    },

    _hasResizedVertical: {
        enumerable: false,
        value: false
    },

    willDraw: {
        value: function () {
            if (this._offsetWidth !== this._element.offsetWidth) {
                this._offsetWidth = this._element.offsetWidth;
                this._hasResizedHorizontal = true;
            }
            if (this._offsetHeight !== this._element.offsetHeight) {
                this._offsetHeight = this._element.offsetHeight;
                this._hasResizedVertical = true;
            }
        }
    },

    draw: {
        value: function () {
            var size,
                pos,
                range,
                max;

            if (this._isDisplayUpdated) {
                var displayVertical = this._displayVertical ? "block" : "none",
                    displayHorizontal = this._displayHorizontal ? "block" : "none";

                this._top.style.display = this._bottomClip.style.display = displayVertical;
                this._left.style.display = this._rightClip.style.display = displayHorizontal;
                this._isDisplayUpdated = false;
            }
            if (this._hasResizedHorizontal && this._displayHorizontal) {
                this._rightClip.style.width = this._right.style.width = (this._offsetWidth - 4) + "px";
                this._rightClip.style.clip = "rect(-1px," + (this._offsetWidth - 3) + "px,6px,3px)";
                this._hasResizedHorizontal = false;
            }
            if (this._hasResizedVertical && this._displayVertical) {
                this._bottomClip.style.height = this._bottom.style.height = (this._offsetHeight - 4) + "px";
                this._bottomClip.style.clip = "rect(3px,6px," + (this._offsetHeight - 3) + "px,-1px)";
                this._hasResizedVertical = false;
            }
            if (this._opacity) {
                if (this._displayHorizontal) {
                    range = this._offsetWidth - 9 - (this._displayVertical ? 6 : 0);
                    size = Math.floor(range * this._horizontalLength);
                    max = range - size;
                    if (1 - this._horizontalLength) {
                        pos = Math.floor((max * this._horizontalScroll) / (1 - this._horizontalLength));
                    } else {
                        pos = 0;
                    }
                    if (pos < 0) {
                        size += pos;
                        if (size < 0) {
                            size = 0;
                        }
                        pos = 0;
                    }
                    if (pos > max) {
                        size += Math.floor(max - pos);
                        if (size < 0) {
                            size = 0;
                        }
                        pos = range - size;
                    }
                    this._right.style.webkitTransform = "translate3d(" + (size - this._offsetWidth + 9) + "px,0,0)";
                    this._left.style.webkitTransform = this._rightClip.style.webkitTransform = "translate3d(" + (pos+2) + "px,0,0)";
                    this._left.style.webkitTransition = this._right.style.webkitTransition = "none";
                    this._left.style.opacity = this._right.style.opacity = this._opacity;
                }
                if (this._displayVertical) {
                    range = this._offsetHeight - 9 - (this._displayHorizontal ? 6 : 0);
                    size = Math.floor(range * this._verticalLength);
                    max = range - size;
                    if (1 - this._verticalLength) {
                        pos = Math.floor((max * this._verticalScroll) / (1 - this._verticalLength));
                    } else {
                        pos = 0;
                    }
                    if (pos < 0) {
                        size += pos;
                        if (size < 0) {
                            size = 0;
                        }
                        pos = 0;
                    }
                    if (pos > max) {
                        size += Math.floor(max - pos);
                        if (size < 0) {
                            size = 0;
                        }
                        pos = range - size;
                    }
                    this._bottom.style.webkitTransform = "translate3d(0," + (size - this._offsetHeight + 9) + "px,0)";
                    this._top.style.webkitTransform = this._bottomClip.style.webkitTransform = "translate3d(0," + (pos+2) + "px,0)";
                    this._top.style.webkitTransition = this._bottom.style.webkitTransition = "none";
                    this._top.style.opacity = this._bottom.style.opacity = this._opacity;
                }
            } else {
                if (this._displayHorizontal) {
                    this._left.style.webkitTransition = this._right.style.webkitTransition = "300ms opacity";
                    this._left.style.opacity = this._right.style.opacity = 0;
                }
                if (this._displayVertical) {
                    this._top.style.webkitTransition = this._bottom.style.webkitTransition = "300ms opacity";
                    this._top.style.opacity = this._bottom.style.opacity = 0;
                }
            }
        }
    }
});