You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

145 rivejä
4.1 KiB

  1. (function ($) {
  2. $.extend(true, window, {
  3. Slick: {
  4. Data: {
  5. GroupItemMetadataProvider: GroupItemMetadataProvider
  6. }
  7. }
  8. });
  9. /***
  10. * Provides item metadata for group (Slick.Group) and totals (Slick.Totals) rows produced by the DataView.
  11. * This metadata overrides the default behavior and formatting of those rows so that they appear and function
  12. * correctly when processed by the grid.
  13. *
  14. * This class also acts as a grid plugin providing event handlers to expand & collapse groups.
  15. * If "grid.registerPlugin(...)" is not called, expand & collapse will not work.
  16. *
  17. * @class GroupItemMetadataProvider
  18. * @module Data
  19. * @namespace Slick.Data
  20. * @constructor
  21. * @param options
  22. */
  23. function GroupItemMetadataProvider(options) {
  24. var _grid;
  25. var _defaults = {
  26. groupCssClass: "slick-group",
  27. groupTitleCssClass: "slick-group-title",
  28. totalsCssClass: "slick-group-totals",
  29. groupFocusable: true,
  30. totalsFocusable: false,
  31. toggleCssClass: "slick-group-toggle",
  32. toggleExpandedCssClass: "expanded",
  33. toggleCollapsedCssClass: "collapsed",
  34. enableExpandCollapse: true
  35. };
  36. options = $.extend(true, {}, _defaults, options);
  37. function defaultGroupCellFormatter(row, cell, value, columnDef, item) {
  38. if (!options.enableExpandCollapse) {
  39. return item.title;
  40. }
  41. var indentation = item.level * 15 + "px";
  42. return "<span class='" + options.toggleCssClass + " " +
  43. (item.collapsed ? options.toggleCollapsedCssClass : options.toggleExpandedCssClass) +
  44. "' style='margin-left:" + indentation +"'>" +
  45. "</span>" +
  46. "<span class='" + options.groupTitleCssClass + "' level='" + item.level + "'>" +
  47. item.title +
  48. "</span>";
  49. }
  50. function defaultTotalsCellFormatter(row, cell, value, columnDef, item) {
  51. return (columnDef.groupTotalsFormatter && columnDef.groupTotalsFormatter(item, columnDef)) || "";
  52. }
  53. function init(grid) {
  54. _grid = grid;
  55. _grid.onClick.subscribe(handleGridClick);
  56. _grid.onKeyDown.subscribe(handleGridKeyDown);
  57. }
  58. function destroy() {
  59. if (_grid) {
  60. _grid.onClick.unsubscribe(handleGridClick);
  61. _grid.onKeyDown.unsubscribe(handleGridKeyDown);
  62. }
  63. }
  64. function handleGridClick(e, args) {
  65. var item = this.getDataItem(args.row);
  66. if (item && item instanceof Slick.Group && $(e.target).hasClass(options.toggleCssClass)) {
  67. if (item.collapsed) {
  68. this.getData().expandGroup(item.groupingKey);
  69. } else {
  70. this.getData().collapseGroup(item.groupingKey);
  71. }
  72. e.stopImmediatePropagation();
  73. e.preventDefault();
  74. }
  75. }
  76. // TODO: add -/+ handling
  77. function handleGridKeyDown(e, args) {
  78. if (options.enableExpandCollapse && (e.which == $.ui.keyCode.SPACE)) {
  79. var activeCell = this.getActiveCell();
  80. if (activeCell) {
  81. var item = this.getDataItem(activeCell.row);
  82. if (item && item instanceof Slick.Group) {
  83. if (item.collapsed) {
  84. this.getData().expandGroup(item.groupingKey);
  85. } else {
  86. this.getData().collapseGroup(item.groupingKey);
  87. }
  88. e.stopImmediatePropagation();
  89. e.preventDefault();
  90. }
  91. }
  92. }
  93. }
  94. function getGroupRowMetadata(item) {
  95. return {
  96. selectable: false,
  97. focusable: options.groupFocusable,
  98. cssClasses: options.groupCssClass,
  99. columns: {
  100. 0: {
  101. colspan: "*",
  102. formatter: defaultGroupCellFormatter,
  103. editor: null
  104. }
  105. }
  106. };
  107. }
  108. function getTotalsRowMetadata(item) {
  109. return {
  110. selectable: false,
  111. focusable: options.totalsFocusable,
  112. cssClasses: options.totalsCssClass,
  113. formatter: defaultTotalsCellFormatter,
  114. editor: null
  115. };
  116. }
  117. return {
  118. "init": init,
  119. "destroy": destroy,
  120. "getGroupRowMetadata": getGroupRowMetadata,
  121. "getTotalsRowMetadata": getTotalsRowMetadata
  122. };
  123. }
  124. })(jQuery);