jquery.tablescroller.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. resizeScrollingTable(this);
  43. };
  44. Plugin.prototype.init = function () {
  45. var _this = this;
  46. $(_this.element).data("original-height", $(_this.element).height());
  47. var disableScrollingTable = $(_this.element).find("table").eq(0).data("tablescroller-disable");
  48. if (disableScrollingTable == null || disableScrollingTable != true) {
  49. resizeScrollingTable(_this);
  50. var _resizeTimeout = -1;
  51. var winWidth = $(window).width();
  52. var winHeight = $(window).height();
  53. $(window).resize(function () {
  54. window.clearTimeout(_resizeTimeout);
  55. _resizeTimeout = window.setTimeout(function(){
  56. // prevents endless loop in IE8
  57. if (winWidth != $(window).width() || winHeight != $(window).height()) {
  58. resizeScrollingTable(_this);
  59. winWidth = $(window).width();
  60. winHeight = $(window).height();
  61. }
  62. }, 400);
  63. });
  64. }
  65. };
  66. function resizeScrollingTable(_this) {
  67. var el = _this.element;
  68. $(el).css("overflow-y", "").css("height", "");
  69. $(el).css("overflow-x", "auto");
  70. var heightAfter = _this.options.heightAfterCorrection;
  71. $(el).nextAll(":visible").each(function () {
  72. heightAfter += $(this).outerHeight(true);
  73. });
  74. var heightCondition = $(el).height() > ($(window).height() - $(el).offset().top - heightAfter);
  75. var enforceHeight = $(_this.element).find("table").eq(0).data("tablescroller-enforce-height");
  76. if (enforceHeight !== undefined && enforceHeight == true) {
  77. heightCondition = true;
  78. }
  79. if (heightCondition) {
  80. var specificMinHeight = $(el).find("table").eq(0).data("tablescroller-min-height");
  81. var minHeightVal = _this.options.minHeight;
  82. if (!isNaN(parseFloat(specificMinHeight)) && isFinite(specificMinHeight)) {
  83. minHeightVal = parseFloat(specificMinHeight);
  84. }
  85. var disableMinHeight = $(_this.element).find("table").eq(0).data("tablescroller-min-height-disable");
  86. if (disableMinHeight != null && disableMinHeight == true) {
  87. if (heightCondition) {
  88. $(el).css("overflow-y", "auto").height($(window).height() - $(el).offset().top - heightAfter);
  89. }
  90. }
  91. else {
  92. if (($(window).height() - $(el).offset().top - heightAfter) > minHeightVal){
  93. $(el).css("overflow-y", "auto").height($(window).height() - $(el).offset().top - heightAfter);
  94. }
  95. else {
  96. if ($(el).data("original-height") > minHeightVal){
  97. $(el).css("overflow-y", "auto").height(minHeightVal);
  98. }
  99. }
  100. }
  101. }
  102. }
  103. $.fn[pluginName] = function (options) {
  104. return this.each(function () {
  105. if (!$.data(this, 'plugin_' + pluginName)) {
  106. $.data(this, 'plugin_' + pluginName, new Plugin(this, options));
  107. }
  108. else {
  109. $.data(this, 'plugin_' + pluginName).setOptions(options);
  110. }
  111. });
  112. }
  113. })(jQuery, window, document);