Guest User

Untitled

a guest
May 22nd, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. (function () {
  2. 'use strict';
  3.  
  4. agGrid.LicenseManager.setLicenseKey('A valid License');
  5. var ComponentUtil = agGrid.ComponentUtil;
  6.  
  7. /**
  8. * Takes an array of strings and returns an object containing those strings as properties assigned the provided value
  9. *
  10. * @param {any} array
  11. * @param {any} value
  12. * @returns object
  13. */
  14. function assignValueToAllKeys(array, value) {
  15. return array.reduce(function (acc, curr) {
  16. acc[curr] = value;
  17. return acc;
  18. }, {});
  19. }
  20.  
  21. /**
  22. * Assign all inputs and outputs to the grid component based on those provided by the AgGrid library
  23. *
  24. * @returns object
  25. */
  26. function getBindings() {
  27. return _.merge({
  28. gridType: '<',
  29. gridOptions: '<'
  30. },
  31. assignValueToAllKeys(ComponentUtil.ALL_PROPERTIES, '<'),
  32. assignValueToAllKeys(ComponentUtil.EVENTS, '&'));
  33. }
  34.  
  35. angular
  36. .module('grid')
  37. .component('agGrid', {
  38. bindings: getBindings(),
  39. controller: ['$element', '$scope', '$compile', '$attrs', 'i18n', 'GridTypeService', AgGridComponent]
  40. });
  41.  
  42. function AgGridComponent($element, $scope, $compile, $attrs, i18n, GridTypeService) {
  43. this._intialized = false;
  44. this._destroyed = false;
  45. var grid;
  46. this.$onInit = function () {};
  47. this.$postLink = function () {
  48. this.gridOptions = _.merge({}, this.gridOptions, GridTypeService.lookupGridOptions(this.gridType));
  49. this.gridOptions = ComponentUtil.copyAttributesToGridOptions(this.gridOptions, this);
  50. var gridParams = {
  51. $scope: $scope,
  52. $compile: $compile,
  53. quickFilterOnScope: this.gridOptions.quickFilterText,
  54. globalEventListener: this.globalEventListener.bind(this)
  55. };
  56. if (this.columns && this.columns.length > 0) {
  57. this.gridOptions.columnDefs = this.columns.map(function (column) {
  58. return column.toColDef();
  59. });
  60. }
  61.  
  62. grid = new agGrid.Grid($element[0], this.gridOptions, gridParams);
  63.  
  64. if (this.gridOptions.api) {
  65. this.api = this.gridOptions.api;
  66. }
  67. if (this.gridOptions.columnApi) {
  68. this.columnApi = this.gridOptions.columnApi;
  69. }
  70. this._intialized = true;
  71. };
  72. this.$onChanges = function (changes) {
  73. if (this._intialized) {
  74. ComponentUtil.processOnChange(changes, this.gridOptions, this.api, this.columnApi);
  75. }
  76. };
  77. this.$onDestroy = function () {
  78. if (this._intialized) {
  79. // TODO - Investigate - this is how the Angular 2 version handles destroy - the Angular 1 version remains uncommented
  80. this._destroyed = true;
  81. // this.api.destroy();
  82. grid.destroy();
  83. grid = null;
  84. }
  85. };
  86.  
  87. this.globalEventListener = function (eventType, event) {
  88. if (this._destroyed) {
  89. return;
  90. }
  91. var emitter = this[eventType];
  92. if (emitter) {
  93. emitter({
  94. $event: event
  95. });
  96. }
  97. };
  98. }
  99.  
  100.  
  101. })();
Add Comment
Please, Sign In to add comment