Advertisement
MindKooper

Untitled

Aug 14th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. /* global $, jsGrid, localInit */
  2.  
  3. /* eslint-disable no-var */
  4.  
  5. function isEmptyGridFilter(filter) {
  6. var isEmpty = true;
  7. $.each(filter, function () {
  8. $.each(this, function (prop, value) {
  9. if (value != "") {
  10. isEmpty = false;
  11. return false;
  12. }
  13. });
  14. });
  15. return isEmpty;
  16. }
  17.  
  18. function applyGridFilter(filter, item) {
  19. if (!filter || !item)
  20. return false;
  21. for (var prop in filter) {
  22. if (typeof filter[prop] === "object") { //look for nested
  23. if (applyGridFilter(filter[prop], item[prop]))
  24. return true;
  25. continue;
  26. }
  27. var regexp = new RegExp(filter[prop], 'gi');
  28. if (filter[prop] && filter[prop].length > 0) {
  29. if (item[prop] && item[prop].match(regexp))
  30. return true;
  31. }
  32. }
  33. return false;
  34. }
  35.  
  36. getStateFromStorage = () => JSON.parse(window.sessionStorage.getItem(window.location.hash.substring(1)));
  37. generateStateId = (gridInstance) => `pi=${gridInstance.pageIndex}ps=${gridInstance.pageSize}`;
  38. initializeFromState = (grid, pageState) => {grid.state = Object.assign({}, pageState, {'status': true})};
  39.  
  40. function saveState(grid, stateId) {
  41. const pageState = {
  42. 'filter': grid.getFilter(),
  43. 'pageIndex': grid.pageIndex,
  44. 'pageSize': grid.pageSize,
  45. 'sortOrder': grid._sortOrder,
  46. 'sortField': {'name': (grid._sortField !== null ) ? grid._sortField.name : grid._sortField, 'title': (grid._sortField !== null ) ? grid._sortField.title : grid._sortField}
  47. };
  48. console.log('sortField test',pageState.sortField)
  49. window.sessionStorage.setItem(stateId, JSON.stringify(pageState));
  50. }
  51.  
  52. function getExtendedFilter(grid) {
  53. if (grid.state && grid.state.status === true) {
  54. const filter = grid.state.filter;
  55. this.pageIndex = grid.state.pageIndex;
  56. this.pageSize = grid.state.pageSize;
  57. this._sortField = grid.state.sortField;
  58. this._sortOrder = grid.state.sortOrder;
  59. this.filtering = true;
  60. setFilter.call(this, filter);
  61. setSorting.call(this);
  62. delete grid.state;
  63. return filter;
  64. }
  65. }
  66.  
  67. function setFilter(filter) {
  68. this.fields.forEach((field, index) => {
  69. if(field.filtering) {
  70. // console.log('test',$('.jsgrid-filter-row>.jsgrid-cell>input:not([class])').get(index).attr('value', 123123));
  71. document.querySelectorAll("tr.jsgrid-filter-row > .jsgrid-cell > input")[index].setAttribute('value', filter[field['name']])
  72. }
  73. });
  74. }
  75.  
  76. function setSorting() {
  77. (document.querySelectorAll("tr.jsgrid-header-row>.jsgrid-header-cell.jsgrid-header-sortable")).forEach(item => {
  78. // console.log('item', document.querySelectorAll("tr.jsgrid-header-row>.jsgrid-header-cell.jsgrid-header-sortable"));
  79. if (item.innerHTML === this.state.sortField.title) {
  80. item.className += " jsgrid-header-sort jsgrid-header-sort-" + this.state.sortOrder;
  81. }
  82. })
  83. }
  84.  
  85. $(document).ready(function () {
  86. var gridInstance;
  87. window.addEventListener('hashchange', function (event) {
  88. const pageState = getStateFromStorage();
  89. if (gridInstance.pageSize !== pageState.pageSize || gridInstance.pageIndex !== pageState.pageIndex) {
  90. gridInstance.openPage(pageState.pageIndex);
  91. }
  92. });
  93. jsGrid.Grid.prototype.onOptionChanged = function (args) {
  94. const stateId = generateStateId(gridInstance);
  95. saveState(gridInstance, stateId);
  96. location.hash = stateId;
  97. };
  98. jsGrid.Grid.prototype.onInit = function (args) {
  99. gridInstance = args.grid;
  100. const pageState = getStateFromStorage();
  101. if (pageState !== null) {
  102. initializeFromState(gridInstance, pageState);
  103. window.sessionStorage.removeItem(window.location.hash.substring(1));
  104. };
  105. };
  106. (function (loadData) {
  107. jsGrid.Grid.prototype.loadData = function (filter) {
  108. filter = (filter === undefined) ? getExtendedFilter.call(this, gridInstance) : filter;
  109. //Just for test
  110. loadData.call(this, filter);
  111. };
  112. }(jsGrid.Grid.prototype.loadData));
  113.  
  114. jsGrid.loadStrategies.DirectLoadingStrategy.prototype.finishDelete = function (deletedItem, deletedItemIndex) {
  115. var grid = this._grid;
  116. grid.option("data").splice(deletedItemIndex, 1);
  117. grid.refresh();
  118. };
  119. jsGrid.Grid.prototype._sortData = function () { //compensate sorting bug for nested data
  120. var self = this,
  121. sortFactor = this._sortFactor(),
  122. sortField = this._sortField;
  123. if (sortField) {
  124. this.data.sort(function (item1, item2) {
  125. var value1 = self._getItemFieldValue(item1, sortField);
  126. var value2 = self._getItemFieldValue(item2, sortField);
  127. return sortFactor * sortField.sortingFunc(value1, value2);
  128. });
  129. }
  130. };
  131.  
  132. if (window.localInit) {
  133. localInit();
  134. }
  135. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement