bootstrap-table-mobile.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @author: Dennis Hernández
  3. * @webSite: http://djhvscf.github.io/Blog
  4. * @version: v1.1.0
  5. */
  6. !function ($) {
  7. 'use strict';
  8. var resetView = function (that) {
  9. if (that.options.height || that.options.showFooter) {
  10. setTimeout(that.resetView, 1);
  11. }
  12. };
  13. var changeView = function (that, width, height) {
  14. if (that.options.minHeight) {
  15. if (checkValuesLessEqual(width, that.options.minWidth) && checkValuesLessEqual(height, that.options.minHeight)) {
  16. conditionCardView(that);
  17. } else if (checkValuesGreater(width, that.options.minWidth) && checkValuesGreater(height, that.options.minHeight)) {
  18. conditionFullView(that);
  19. }
  20. } else {
  21. if (checkValuesLessEqual(width, that.options.minWidth)) {
  22. conditionCardView(that);
  23. } else if (checkValuesGreater(width, that.options.minWidth)) {
  24. conditionFullView(that);
  25. }
  26. }
  27. resetView(that);
  28. };
  29. var checkValuesLessEqual = function (currentValue, targetValue) {
  30. return currentValue <= targetValue;
  31. };
  32. var checkValuesGreater = function (currentValue, targetValue) {
  33. return currentValue > targetValue;
  34. };
  35. var conditionCardView = function (that) {
  36. changeTableView(that, false);
  37. };
  38. var conditionFullView = function (that) {
  39. changeTableView(that, true);
  40. };
  41. var changeTableView = function (that, cardViewState) {
  42. that.options.cardView = cardViewState;
  43. that.toggleView();
  44. };
  45. var debounce = function(func,wait) {
  46. var timeout;
  47. return function() {
  48. var context = this, args = arguments;
  49. var later = function() {
  50. timeout = null;
  51. func.apply(context,args);
  52. };
  53. clearTimeout(timeout);
  54. timeout = setTimeout(later,wait);
  55. };
  56. };
  57. $.extend($.fn.bootstrapTable.defaults, {
  58. mobileResponsive: false,
  59. minWidth: 562,
  60. minHeight: undefined,
  61. checkOnInit: true,
  62. toggled: false
  63. });
  64. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  65. _init = BootstrapTable.prototype.init;
  66. BootstrapTable.prototype.init = function () {
  67. _init.apply(this, Array.prototype.slice.apply(arguments));
  68. if (!this.options.mobileResponsive) {
  69. return;
  70. }
  71. if (!this.options.minWidth) {
  72. return;
  73. }
  74. var that = this, old = { w: $(window).width(), h: $(window).height() };
  75. $(window).on('resize orientationchange',debounce(function (evt) {
  76. // reset view if height has only changed by at least the threshold.
  77. var h = $(this).height(), w = $(this).width();
  78. if (Math.abs(old.h - h) > that.options.heightThreshold || old.w != w) {
  79. changeView(that, w, h);
  80. old = { w: w, h: h };
  81. }
  82. },200));
  83. if (this.options.checkOnInit) {
  84. var h = $(window).height(), w = $(window).width();
  85. changeView(this, w, h);
  86. old = { w: w, h: h };
  87. }
  88. };
  89. }(jQuery);