Bug 21139: Floating toolbar - Resize toolbar on window resize
[koha_ffzg] / koha-tmpl / intranet-tmpl / lib / jquery / plugins / jquery.fixFloat.js
1 /* Source: http://www.webspeaks.in/2011/07/new-gmail-like-floating-toolbar-jquery.html
2    Revision: http://jsfiddle.net/pasmalin/AyjeZ/
3 */
4 (function ($, window) {
5     "use strict";
6     $.fn.fixFloat = function (options) {
7         var options = options || {};
8         var tbh = $(this);
9         var defaults = {
10             enabled: true,
11             originalOffset: tbh.offset().top,
12             originalPosition: tbh.position().top,
13         };
14         var originalOffset = typeof options.originalOffset === 'undefined'
15             ? defaults.originalOffset
16             : options.originalOffset;
17
18         var originalPosition = typeof options.originalPosition === 'undefined'
19             ? defaults.originalPosition
20             : options.originalPosition;
21
22         options = $.extend(defaults, options);
23
24         if (tbh.css('position') !== 'absolute') {
25             var tbhBis = tbh.clone();
26             tbhBis.css({
27                 "display": tbh.css("display"),
28                     "visibility": "hidden"
29             });
30             tbhBis.width(tbh.innerWidth(true));
31             tbhBis.height(tbh.innerHeight(true));
32             tbhBis.attr('id', tbh.attr('id')+'Bis'); // Avoid 2 elts with the same id
33             tbh.after(tbhBis);
34             tbh.width(tbh.width());
35             var tbl = tbh.find("th,td");
36             if (tbl.length > 0) {
37                 tbl.each(function () {
38                     var $elt = $(this);
39                     $elt.width($elt.outerWidth(true));
40                 });
41             }
42             tbh.css({
43                 'position': 'absolute',
44                     'top': originalPosition,
45             });
46         }
47
48         if (options.enabled) {
49             $(window).scroll(function () {
50                 var offsetTop = tbh.offset().top;
51
52                 var s = parseInt($(window).scrollTop(), 10);
53
54                 var fixMe = (s > offsetTop);
55                 var repositionMe = (s < originalOffset);
56                 if (fixMe) {
57                     tbh.css({
58                         'position': 'fixed',
59                             'top': '0',
60                         'z-index': '1000'
61                     });
62                     tbh.addClass("floating");
63                 }
64                 if (repositionMe) {
65                     tbh.css({
66                         'position': 'absolute',
67                             'top': originalPosition,
68                         'z-index': '1'
69                     });
70                     tbh.removeClass("floating");
71                 }
72             });
73
74             $(window).resize(function() {
75                 var p = $(tbh).parents('div').first();
76                 $(tbh).width(p.width()-10);
77             });
78         }
79     };
80 })(jQuery, window);