Masse salariale HTML
[auf_rh_dae.git] / project / rh / static / admin / js / jquery-stickytableheaders.js
1
2 /*! Copyright (c) 2011 by Jonas Mosbech - https://github.com/jmosbech/StickyTableHeaders
3 MIT license info: https://github.com/jmosbech/StickyTableHeaders/blob/master/license.txt */
4
5 (function ($) {
6 $.StickyTableHeaders = function (el, options) {
7 // To avoid scope issues, use 'base' instead of 'this'
8 // to reference this class from internal events and functions.
9 var base = this;
10
11 // Access to jQuery and DOM versions of element
12 base.$el = $(el);
13 base.el = el;
14
15 // Cache DOM refs for performance reasons
16 base.$window = $(window);
17 base.$clonedHeader = null;
18 base.$originalHeader = null;
19
20 // Add a reverse reference to the DOM object
21 base.$el.data('StickyTableHeaders', base);
22
23 base.init = function () {
24 base.options = $.extend({}, $.StickyTableHeaders.defaultOptions, options);
25
26 base.$el.each(function () {
27 var $this = $(this);
28
29 // remove padding on <table> to fix issue #7
30 $this.css('padding', 0);
31
32 $this.wrap('<div class="divTableWithFloatingHeader"></div>');
33
34 base.$originalHeader = $('thead:first', this);
35 base.$clonedHeader = base.$originalHeader.clone();
36
37 base.$clonedHeader.addClass('tableFloatingHeader');
38 base.$clonedHeader.css({
39 'position': 'fixed',
40 'top': 0,
41 'left': $this.css('margin-left'),
42 'display': 'none'
43 });
44
45 base.$originalHeader.addClass('tableFloatingHeaderOriginal');
46
47 base.$originalHeader.before(base.$clonedHeader);
48
49 // enabling support for jquery.tablesorter plugin
50 // forward clicks on clone to original
51 $('th', base.$clonedHeader).click(function(e){
52 var index = $('th', base.$clonedHeader).index(this);
53 $('th', base.$originalHeader).eq(index).click();
54 });
55 $this.bind('sortEnd', base.updateCloneFromOriginal );
56 });
57
58 base.updateTableHeaders();
59 base.$window.scroll(base.updateTableHeaders);
60 base.$window.resize(base.updateTableHeaders);
61 };
62
63 base.updateTableHeaders = function () {
64 base.$el.each(function () {
65 var $this = $(this);
66
67 var fixedHeaderHeight = isNaN(base.options.fixedOffset) ? base.options.fixedOffset.height() : base.options.fixedOffset;
68
69 var offset = $this.offset();
70 var scrollTop = base.$window.scrollTop() + fixedHeaderHeight;
71 var scrollLeft = base.$window.scrollLeft();
72
73 if ((scrollTop > offset.top) && (scrollTop < offset.top + $this.height())) {
74 base.$clonedHeader.css({
75 'top': fixedHeaderHeight,
76 'margin-top': 0,
77 'left': offset.left - scrollLeft,
78 'display': 'block'
79 });
80
81 base.updateCloneFromOriginal();
82 }
83 else {
84 base.$clonedHeader.css('display', 'none');
85 }
86 });
87 };
88
89 base.updateCloneFromOriginal = function () {
90 // Copy cell widths and classes from original header
91 $('th', base.$clonedHeader).each(function (index) {
92 var $this = $(this);
93 var origCell = $('th', base.$originalHeader).eq(index);
94 $this.removeClass().addClass(origCell.attr('class'));
95 $this.css('width', origCell.width());
96 });
97
98 // Copy row width from whole table
99 base.$clonedHeader.css('width', base.$originalHeader.width());
100 };
101
102 // Run initializer
103 base.init();
104 };
105
106 $.StickyTableHeaders.defaultOptions = {
107 fixedOffset: 0
108 };
109
110 $.fn.stickyTableHeaders = function (options) {
111 return this.each(function () {
112 (new $.StickyTableHeaders(this, options));
113 });
114 };
115
116 })(jQuery);