-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable-scroll-button.jquery.js
110 lines (96 loc) · 3.04 KB
/
table-scroll-button.jquery.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
;(function ($) {
/**
* Handles adding the table scroll button.
*
* @param element
*/
const addTableScrollButton = function (element) {
$(window).on('load resize', function () {
// const viewsTables = $('.views-table');
const viewsTables = element;
viewsTables.each(function (i) {
const table = $(viewsTables[i]);
const tableWidth = table.width();
const parent = table.parent();
const parentWidth = parent.width();
if (tableWidth > parentWidth) {
parent.prepend('<button class="button--table-scroll">Scroll table right</button>');
}
else {
parent.find('.button--table-scroll').remove();
}
});
});
};
/**
* Handle scrolling one screen on click.
*/
const handleTableScrollButtonClick = function () {
$('body').on('click', '.button--table-scroll', function(e) {
e.preventDefault();
const table = $(this).next('table');
const tableScrollPosition = table.position();
const parent = $(this).parent();
const parentWidth = parent.width();
parent.animate({
scrollLeft: Math.abs(tableScrollPosition.left) + parentWidth
}, 1000);
});
};
/**
* Handle when to hide the button.
*
* @param element
*/
const handleTableScrollButtonHide = function (element) {
element.parent().on('scroll', function () {
const table = $(this).find('.views-table');
const tableScrollPosition = table.position();
const tableWidth = table.width();
const parent = $(this).parent();
const parentWidth = parent.width();
const tableRight = Math.ceil(Math.abs(tableScrollPosition.left)) + parentWidth;
const button = $(this).find('.button--table-scroll');
// Give 1 pixel of wiggle room to hide the button.
if (tableRight >= tableWidth - 1) {
button.addClass('js-hidden');
}
else {
button.removeClass('js-hidden');
}
});
};
/**
* Handle the vertical position of the scroll button.
*
* @param element
*/
const handleTableScrollButtonPosition = function (element) {
$(window).on('load resize', function () {
const viewportHeight = $(window).height();
const tables = element;
tables.each(function (i) {
const table = $(tables[i]);
// If this table has a button.
if (table.parent().find('.button--table-scroll').length > 0) {
const tableHeight = table.height();
const smallerOfWindowOrTable = Math.ceil(Math.min(viewportHeight, tableHeight));
const button = table.parent().find('.button--table-scroll');
button.attr('style', 'transform: translateY(' + (smallerOfWindowOrTable / 2) + 'px)');
}
});
});
};
/**
* Jquery plugin entry point.
*
* @returns {jQuery}
*/
$.fn.tableScrollButton = function () {
addTableScrollButton(this);
handleTableScrollButtonClick();
handleTableScrollButtonHide(this);
handleTableScrollButtonPosition(this);
return this;
};
}(jQuery));