grouped_data_converter_mixin.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * DevExtreme (ui/shared/grouped_data_converter_mixin.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. Object.defineProperty(exports, "__esModule", {
  11. value: true
  12. });
  13. var _type = require("../../core/utils/type");
  14. var isCorrectStructure = function(data) {
  15. return Array.isArray(data) && data.every(function(item) {
  16. var hasTwoFields = 2 === Object.keys(item).length;
  17. var hasCorrectFields = "key" in item && "items" in item;
  18. return hasTwoFields && hasCorrectFields && Array.isArray(item.items)
  19. })
  20. };
  21. exports.default = {
  22. _getSpecificDataSourceOption: function() {
  23. var groupKey = "key";
  24. var dataSource = this.option("dataSource");
  25. var hasSimpleItems = false;
  26. var data = {};
  27. if (this._getGroupedOption() && isCorrectStructure(dataSource)) {
  28. data = dataSource.reduce(function(accumulator, item) {
  29. var items = item.items.map(function(innerItem) {
  30. if (!(0, _type.isObject)(innerItem)) {
  31. innerItem = {
  32. text: innerItem
  33. };
  34. hasSimpleItems = true
  35. }
  36. if (!(groupKey in innerItem)) {
  37. innerItem[groupKey] = item.key
  38. }
  39. return innerItem
  40. });
  41. return accumulator.concat(items)
  42. }, []);
  43. dataSource = {
  44. store: {
  45. type: "array",
  46. data: data
  47. },
  48. group: {
  49. selector: "key",
  50. keepInitialKeyOrder: true
  51. }
  52. };
  53. if (hasSimpleItems) {
  54. dataSource.searchExpr = "text"
  55. }
  56. }
  57. return dataSource
  58. }
  59. };