jquery.tablescroller.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. /*
  17. * jHue table scroller plugin
  18. *
  19. * Three data attributes can be set on the table to modify the default behavior of the plugin:
  20. * - data-tablescroller-min-height="N" where N is a value in pixels: specifies a minimum height for the scrolling table,
  21. * overriding the default 500px set by the plugin
  22. * - data-tablescroller-min-height-disable="true": disable enforcing a minimum height for the table
  23. * - data-tablescroller-disable="true": disable the plugin for the specific table
  24. * - data-tablescroller-enforce-height="true": displays the table at its maximum height accordingly to the page
  25. */
  26. ;
  27. (function ($, window, document, undefined) {
  28. var pluginName = "jHueTableScroller",
  29. defaults = {
  30. minHeight: 300,
  31. heightAfterCorrection: 40
  32. };
  33. function Plugin(element, options) {
  34. this.element = element;
  35. this.options = $.extend({}, defaults, options);
  36. this._defaults = defaults;
  37. this._name = pluginName;
  38. this.init();
  39. }
  40. Plugin.prototype.setOptions = function (options) {
  41. this.options = $.extend({}, defaults, options);
  42. };
  43. Plugin.prototype.init = function () {
  44. var _this = this;
  45. $(_this.element).data("original-height", $(_this.element).height());
  46. var disableScrollingTable = $(_this.element).find("table").eq(0).data("tablescroller-disable");
  47. if (disableScrollingTable == null || disableScrollingTable != true) {
  48. resizeScrollingTable(_this.element);
  49. var _resizeTimeout = -1;
  50. var winWidth = $(window).width();
  51. var winHeight = $(window).height();
  52. $(window).resize(function () {
  53. window.clearTimeout(_resizeTimeout);
  54. _resizeTimeout = window.setTimeout(function(){
  55. // prevents endless loop in IE8
  56. if (winWidth != $(window).width() || winHeight != $(window).height()) {
  57. resizeScrollingTable(_this.element);
  58. winWidth = $(window).width();
  59. winHeight = $(window).height();
  60. }
  61. }, 400);
  62. });
  63. }
  64. function resizeScrollingTable(el) {
  65. $(el).css("overflow-y", "").css("height", "");
  66. var heightAfter = _this.options.heightAfterCorrection;
  67. $(el).nextAll(":visible").each(function () {
  68. heightAfter += $(this).outerHeight(true);
  69. });
  70. var heightCondition = $(el).height() > ($(window).height() - $(el).offset().top - heightAfter);
  71. var enforceHeight = $(_this.element).find("table").eq(0).data("tablescroller-enforce-height");
  72. if (enforceHeight !== undefined && enforceHeight == true) {
  73. heightCondition = true;
  74. }
  75. if (heightCondition) {
  76. var specificMinHeight = $(el).find("table").eq(0).data("tablescroller-min-height");
  77. var minHeightVal = _this.options.minHeight;
  78. if (!isNaN(parseFloat(specificMinHeight)) && isFinite(specificMinHeight)) {
  79. minHeightVal = parseFloat(specificMinHeight);
  80. }
  81. var disableMinHeight = $(_this.element).find("table").eq(0).data("tablescroller-min-height-disable");
  82. if (disableMinHeight != null && disableMinHeight == true) {
  83. if (heightCondition) {
  84. $(el).css("overflow-y", "auto").height($(window).height() - $(el).offset().top - heightAfter);
  85. }
  86. }
  87. else {
  88. if (($(window).height() - $(el).offset().top - heightAfter) > minHeightVal){
  89. $(el).css("overflow-y", "auto").height($(window).height() - $(el).offset().top - heightAfter);
  90. }
  91. else {
  92. if ($(el).data("original-height") > minHeightVal){
  93. $(el).css("overflow-y", "auto").height(minHeightVal);
  94. }
  95. }
  96. }
  97. }
  98. }
  99. };
  100. $.fn[pluginName] = function (options) {
  101. return this.each(function () {
  102. if (!$.data(this, 'plugin_' + pluginName)) {
  103. $.data(this, 'plugin_' + pluginName, new Plugin(this, options));
  104. }
  105. else {
  106. $.data(this, 'plugin_' + pluginName).setOptions(options);
  107. }
  108. });
  109. }
  110. })(jQuery, window, document);