ui-grid.auto-resize.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*!
  2. * ui-grid - v4.4.6 - 2018-04-06
  3. * Copyright (c) 2018 ; License: MIT
  4. */
  5. (function() {
  6. 'use strict';
  7. /**
  8. * @ngdoc overview
  9. * @name ui.grid.autoResize
  10. *
  11. * @description
  12. *
  13. * #ui.grid.autoResize
  14. *
  15. * <div class="alert alert-warning" role="alert"><strong>Beta</strong> This feature is ready for testing, but it either hasn't seen a lot of use or has some known bugs.</div>
  16. *
  17. * This module provides auto-resizing functionality to UI-Grid.
  18. */
  19. var module = angular.module('ui.grid.autoResize', ['ui.grid']);
  20. module.directive('uiGridAutoResize', ['gridUtil', function(gridUtil) {
  21. return {
  22. require: 'uiGrid',
  23. scope: false,
  24. link: function($scope, $elm, $attrs, uiGridCtrl) {
  25. var elementWidth,
  26. elementHeight;
  27. var updateWidth = gridUtil.throttle(function() {
  28. elementWidth = gridUtil.elementWidth($elm);
  29. }, 200);
  30. var updateHeight = gridUtil.throttle(function() {
  31. elementHeight = gridUtil.elementHeight($elm);
  32. }, 200);
  33. var refresh = gridUtil.throttle(function(width, height) {
  34. uiGridCtrl.grid.gridWidth = width;
  35. uiGridCtrl.grid.gridHeight = height;
  36. uiGridCtrl.grid.refresh();
  37. }, 300);
  38. $scope.$watchGroup([
  39. function() {
  40. updateWidth();
  41. return elementWidth;
  42. },
  43. function() {
  44. updateHeight();
  45. return elementHeight;
  46. }
  47. ], function(newValues, oldValues, scope) {
  48. if (!angular.equals(newValues, oldValues)) {
  49. refresh(newValues[0], newValues[1]);
  50. }
  51. });
  52. }
  53. };
  54. }]);
  55. })();