data_helper.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * DevExtreme (data_helper.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. var DataSource = require("./data/data_source/data_source").DataSource;
  11. var extend = require("./core/utils/extend").extend;
  12. var normalizeDataSourceOptions = require("./data/data_source/data_source").normalizeDataSourceOptions;
  13. var DATA_SOURCE_OPTIONS_METHOD = "_dataSourceOptions";
  14. var DATA_SOURCE_CHANGED_METHOD = "_dataSourceChangedHandler";
  15. var DATA_SOURCE_LOAD_ERROR_METHOD = "_dataSourceLoadErrorHandler";
  16. var DATA_SOURCE_LOADING_CHANGED_METHOD = "_dataSourceLoadingChangedHandler";
  17. var DATA_SOURCE_FROM_URL_LOAD_MODE_METHOD = "_dataSourceFromUrlLoadMode";
  18. var SPECIFIC_DATA_SOURCE_OPTION = "_getSpecificDataSourceOption";
  19. var DataHelperMixin = {
  20. postCtor: function() {
  21. this.on("disposing", function() {
  22. this._disposeDataSource()
  23. }.bind(this))
  24. },
  25. _refreshDataSource: function() {
  26. this._initDataSource();
  27. this._loadDataSource()
  28. },
  29. _initDataSource: function() {
  30. var dataSourceOptions = SPECIFIC_DATA_SOURCE_OPTION in this ? this[SPECIFIC_DATA_SOURCE_OPTION]() : this.option("dataSource");
  31. var widgetDataSourceOptions;
  32. var dataSourceType;
  33. this._disposeDataSource();
  34. if (dataSourceOptions) {
  35. if (dataSourceOptions instanceof DataSource) {
  36. this._isSharedDataSource = true;
  37. this._dataSource = dataSourceOptions
  38. } else {
  39. widgetDataSourceOptions = DATA_SOURCE_OPTIONS_METHOD in this ? this[DATA_SOURCE_OPTIONS_METHOD]() : {};
  40. dataSourceType = this._dataSourceType ? this._dataSourceType() : DataSource;
  41. dataSourceOptions = normalizeDataSourceOptions(dataSourceOptions, {
  42. fromUrlLoadMode: DATA_SOURCE_FROM_URL_LOAD_MODE_METHOD in this && this[DATA_SOURCE_FROM_URL_LOAD_MODE_METHOD]()
  43. });
  44. this._dataSource = new dataSourceType(extend(true, {}, widgetDataSourceOptions, dataSourceOptions))
  45. }
  46. this._addDataSourceHandlers()
  47. }
  48. },
  49. _addDataSourceHandlers: function() {
  50. if (DATA_SOURCE_CHANGED_METHOD in this) {
  51. this._addDataSourceChangeHandler()
  52. }
  53. if (DATA_SOURCE_LOAD_ERROR_METHOD in this) {
  54. this._addDataSourceLoadErrorHandler()
  55. }
  56. if (DATA_SOURCE_LOADING_CHANGED_METHOD in this) {
  57. this._addDataSourceLoadingChangedHandler()
  58. }
  59. this._addReadyWatcher()
  60. },
  61. _addReadyWatcher: function() {
  62. this._dataSource.on("loadingChanged", function(isLoading) {
  63. this._ready && this._ready(!isLoading)
  64. }.bind(this))
  65. },
  66. _addDataSourceChangeHandler: function() {
  67. var dataSource = this._dataSource;
  68. this._proxiedDataSourceChangedHandler = function(e) {
  69. this[DATA_SOURCE_CHANGED_METHOD](dataSource.items(), e)
  70. }.bind(this);
  71. dataSource.on("changed", this._proxiedDataSourceChangedHandler)
  72. },
  73. _addDataSourceLoadErrorHandler: function() {
  74. this._proxiedDataSourceLoadErrorHandler = this[DATA_SOURCE_LOAD_ERROR_METHOD].bind(this);
  75. this._dataSource.on("loadError", this._proxiedDataSourceLoadErrorHandler)
  76. },
  77. _addDataSourceLoadingChangedHandler: function() {
  78. this._proxiedDataSourceLoadingChangedHandler = this[DATA_SOURCE_LOADING_CHANGED_METHOD].bind(this);
  79. this._dataSource.on("loadingChanged", this._proxiedDataSourceLoadingChangedHandler)
  80. },
  81. _loadDataSource: function() {
  82. if (this._dataSource) {
  83. var dataSource = this._dataSource;
  84. if (dataSource.isLoaded()) {
  85. this._proxiedDataSourceChangedHandler && this._proxiedDataSourceChangedHandler()
  86. } else {
  87. dataSource.load()
  88. }
  89. }
  90. },
  91. _loadSingle: function(key, value) {
  92. key = "this" === key ? this._dataSource.key() || "this" : key;
  93. return this._dataSource.loadSingle(key, value)
  94. },
  95. _isLastPage: function() {
  96. return !this._dataSource || this._dataSource.isLastPage() || !this._dataSource._pageSize
  97. },
  98. _isDataSourceLoading: function() {
  99. return this._dataSource && this._dataSource.isLoading()
  100. },
  101. _disposeDataSource: function() {
  102. if (this._dataSource) {
  103. if (this._isSharedDataSource) {
  104. delete this._isSharedDataSource;
  105. this._proxiedDataSourceChangedHandler && this._dataSource.off("changed", this._proxiedDataSourceChangedHandler);
  106. this._proxiedDataSourceLoadErrorHandler && this._dataSource.off("loadError", this._proxiedDataSourceLoadErrorHandler);
  107. this._proxiedDataSourceLoadingChangedHandler && this._dataSource.off("loadingChanged", this._proxiedDataSourceLoadingChangedHandler)
  108. } else {
  109. this._dataSource.dispose()
  110. }
  111. delete this._dataSource;
  112. delete this._proxiedDataSourceChangedHandler;
  113. delete this._proxiedDataSourceLoadErrorHandler;
  114. delete this._proxiedDataSourceLoadingChangedHandler
  115. }
  116. },
  117. getDataSource: function() {
  118. return this._dataSource || null
  119. }
  120. };
  121. module.exports = DataHelperMixin;