@easyquery/core 7.2.4 → 7.2.5-rc01

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. package/dist/easyquery.core.cjs.js +6165 -0
  2. package/dist/easyquery.core.esm.js +6126 -0
  3. package/dist/{lib → types}/common/eq_services.d.ts +16 -12
  4. package/dist/{lib → types}/dm/data_model.d.ts +3 -23
  5. package/dist/{lib → types}/dm/operator.d.ts +2 -2
  6. package/dist/{lib → types}/main/context.d.ts +17 -2
  7. package/dist/types/main/context_resolver.d.ts +3 -0
  8. package/dist/types/main/init.d.ts +1 -0
  9. package/dist/{lib → types}/public_api.d.ts +2 -0
  10. package/dist/{lib → types}/query/query.d.ts +19 -8
  11. package/package.json +13 -37
  12. package/dist/bundles/easyquery.core.js +0 -9584
  13. package/dist/bundles/easyquery.core.js.map +0 -1
  14. package/dist/bundles/easyquery.core.min.js +0 -2
  15. package/dist/bundles/easyquery.core.min.js.map +0 -1
  16. package/dist/lib/common/eq_services.js +0 -160
  17. package/dist/lib/common/eq_services.js.map +0 -1
  18. package/dist/lib/common/query_param.js +0 -2
  19. package/dist/lib/common/query_param.js.map +0 -1
  20. package/dist/lib/common/tree.js +0 -58
  21. package/dist/lib/common/tree.js.map +0 -1
  22. package/dist/lib/data/eq_aggr_container.js +0 -109
  23. package/dist/lib/data/eq_aggr_container.js.map +0 -1
  24. package/dist/lib/data/eq_data_loader.js +0 -50
  25. package/dist/lib/data/eq_data_loader.js.map +0 -1
  26. package/dist/lib/data/google_data_converter.js +0 -174
  27. package/dist/lib/data/google_data_converter.js.map +0 -1
  28. package/dist/lib/dm/aggr_function.js +0 -43
  29. package/dist/lib/dm/aggr_function.js.map +0 -1
  30. package/dist/lib/dm/data_model.js +0 -935
  31. package/dist/lib/dm/data_model.js.map +0 -1
  32. package/dist/lib/dm/dto/af_data.js +0 -2
  33. package/dist/lib/dm/dto/af_data.js.map +0 -1
  34. package/dist/lib/dm/dto/dm_data.js +0 -2
  35. package/dist/lib/dm/dto/dm_data.js.map +0 -1
  36. package/dist/lib/dm/dto/entity_data.js +0 -2
  37. package/dist/lib/dm/dto/entity_data.js.map +0 -1
  38. package/dist/lib/dm/dto/operator_data.js +0 -2
  39. package/dist/lib/dm/dto/operator_data.js.map +0 -1
  40. package/dist/lib/dm/dto/ve_data.js +0 -2
  41. package/dist/lib/dm/dto/ve_data.js.map +0 -1
  42. package/dist/lib/dm/entity.js +0 -91
  43. package/dist/lib/dm/entity.js.map +0 -1
  44. package/dist/lib/dm/link.js +0 -2
  45. package/dist/lib/dm/link.js.map +0 -1
  46. package/dist/lib/dm/operator.js +0 -94
  47. package/dist/lib/dm/operator.js.map +0 -1
  48. package/dist/lib/dm/value_editor.js +0 -44
  49. package/dist/lib/dm/value_editor.js.map +0 -1
  50. package/dist/lib/eqs/eqs_data_fetcher.js +0 -37
  51. package/dist/lib/eqs/eqs_data_fetcher.js.map +0 -1
  52. package/dist/lib/eqs/eqs_exporter.js +0 -60
  53. package/dist/lib/eqs/eqs_exporter.js.map +0 -1
  54. package/dist/lib/eqs/eqs_model_loader.js +0 -43
  55. package/dist/lib/eqs/eqs_model_loader.js.map +0 -1
  56. package/dist/lib/eqs/eqs_query_file_loader.js +0 -55
  57. package/dist/lib/eqs/eqs_query_file_loader.js.map +0 -1
  58. package/dist/lib/eqs/eqs_query_synchronizer.js +0 -24
  59. package/dist/lib/eqs/eqs_query_synchronizer.js.map +0 -1
  60. package/dist/lib/eqs/eqs_value_list_resolver.js +0 -29
  61. package/dist/lib/eqs/eqs_value_list_resolver.js.map +0 -1
  62. package/dist/lib/export/exporter.js +0 -7
  63. package/dist/lib/export/exporter.js.map +0 -1
  64. package/dist/lib/i18n/text_resources.js +0 -27
  65. package/dist/lib/i18n/text_resources.js.map +0 -1
  66. package/dist/lib/main/action.js +0 -6
  67. package/dist/lib/main/action.js.map +0 -1
  68. package/dist/lib/main/context.js +0 -1492
  69. package/dist/lib/main/context.js.map +0 -1
  70. package/dist/lib/main/context_options.js +0 -2
  71. package/dist/lib/main/context_options.js.map +0 -1
  72. package/dist/lib/main/data_fetcher.js +0 -2
  73. package/dist/lib/main/data_fetcher.js.map +0 -1
  74. package/dist/lib/main/eq_actions.js +0 -16
  75. package/dist/lib/main/eq_actions.js.map +0 -1
  76. package/dist/lib/main/list_cache.js +0 -2
  77. package/dist/lib/main/list_cache.js.map +0 -1
  78. package/dist/lib/main/model_loader.js +0 -33
  79. package/dist/lib/main/model_loader.js.map +0 -1
  80. package/dist/lib/main/model_storage.js +0 -18
  81. package/dist/lib/main/model_storage.js.map +0 -1
  82. package/dist/lib/main/query_file_loader.js +0 -2
  83. package/dist/lib/main/query_file_loader.js.map +0 -1
  84. package/dist/lib/main/query_storage.js +0 -125
  85. package/dist/lib/main/query_storage.js.map +0 -1
  86. package/dist/lib/main/query_synchronizer.js +0 -2
  87. package/dist/lib/main/query_synchronizer.js.map +0 -1
  88. package/dist/lib/main/value_list_resolver.js +0 -2
  89. package/dist/lib/main/value_list_resolver.js.map +0 -1
  90. package/dist/lib/main/widget.js +0 -102
  91. package/dist/lib/main/widget.js.map +0 -1
  92. package/dist/lib/public_api.js +0 -60
  93. package/dist/lib/public_api.js.map +0 -1
  94. package/dist/lib/query/condition.js +0 -332
  95. package/dist/lib/query/condition.js.map +0 -1
  96. package/dist/lib/query/descriptors.js +0 -2
  97. package/dist/lib/query/descriptors.js.map +0 -1
  98. package/dist/lib/query/dto/condition_data.js +0 -2
  99. package/dist/lib/query/dto/condition_data.js.map +0 -1
  100. package/dist/lib/query/dto/expression_data.js +0 -2
  101. package/dist/lib/query/dto/expression_data.js.map +0 -1
  102. package/dist/lib/query/dto/query_column_data.js +0 -2
  103. package/dist/lib/query/dto/query_column_data.js.map +0 -1
  104. package/dist/lib/query/dto/query_data.js +0 -2
  105. package/dist/lib/query/dto/query_data.js.map +0 -1
  106. package/dist/lib/query/expression.js +0 -203
  107. package/dist/lib/query/expression.js.map +0 -1
  108. package/dist/lib/query/query.js +0 -1503
  109. package/dist/lib/query/query.js.map +0 -1
  110. package/dist/lib/query/query_column.js +0 -205
  111. package/dist/lib/query/query_column.js.map +0 -1
  112. package/dist/lib/query/query_events.js +0 -20
  113. package/dist/lib/query/query_events.js.map +0 -1
  114. package/dist/lib/types/cond_part.js +0 -8
  115. package/dist/lib/types/cond_part.js.map +0 -1
  116. package/dist/lib/types/cond_tag.js +0 -14
  117. package/dist/lib/types/cond_tag.js.map +0 -1
  118. package/dist/lib/types/data_kind.js +0 -17
  119. package/dist/lib/types/data_kind.js.map +0 -1
  120. package/dist/lib/types/eq_editor_tag.js +0 -21
  121. package/dist/lib/types/eq_editor_tag.js.map +0 -1
  122. package/dist/lib/types/expr_tag.js +0 -21
  123. package/dist/lib/types/expr_tag.js.map +0 -1
  124. package/dist/lib/types/link_type.js +0 -19
  125. package/dist/lib/types/link_type.js.map +0 -1
  126. package/dist/lib/types/widget_group.js +0 -19
  127. package/dist/lib/types/widget_group.js.map +0 -1
  128. package/dist/lib/utils/constants.js +0 -51
  129. package/dist/lib/utils/constants.js.map +0 -1
  130. package/dist/lib/utils/format_parser.js +0 -88
  131. package/dist/lib/utils/format_parser.js.map +0 -1
  132. package/dist/lib/utils/utils.js +0 -103
  133. package/dist/lib/utils/utils.js.map +0 -1
  134. package/dist/lib/utils/version_info.js +0 -13
  135. package/dist/lib/utils/version_info.js.map +0 -1
  136. package/tsconfig.json +0 -22
  137. package/webpack.config.js +0 -51
  138. /package/dist/{lib → types}/common/query_param.d.ts +0 -0
  139. /package/dist/{lib → types}/common/tree.d.ts +0 -0
  140. /package/dist/{lib → types}/data/eq_aggr_container.d.ts +0 -0
  141. /package/dist/{lib → types}/data/eq_data_loader.d.ts +0 -0
  142. /package/dist/{lib → types}/data/google_data_converter.d.ts +0 -0
  143. /package/dist/{lib → types}/dm/aggr_function.d.ts +0 -0
  144. /package/dist/{lib → types}/dm/dto/af_data.d.ts +0 -0
  145. /package/dist/{lib → types}/dm/dto/dm_data.d.ts +0 -0
  146. /package/dist/{lib → types}/dm/dto/entity_data.d.ts +0 -0
  147. /package/dist/{lib → types}/dm/dto/operator_data.d.ts +0 -0
  148. /package/dist/{lib → types}/dm/dto/ve_data.d.ts +0 -0
  149. /package/dist/{lib → types}/dm/entity.d.ts +0 -0
  150. /package/dist/{lib → types}/dm/link.d.ts +0 -0
  151. /package/dist/{lib → types}/dm/value_editor.d.ts +0 -0
  152. /package/dist/{lib → types}/eqs/eqs_data_fetcher.d.ts +0 -0
  153. /package/dist/{lib → types}/eqs/eqs_exporter.d.ts +0 -0
  154. /package/dist/{lib → types}/eqs/eqs_model_loader.d.ts +0 -0
  155. /package/dist/{lib → types}/eqs/eqs_query_file_loader.d.ts +0 -0
  156. /package/dist/{lib → types}/eqs/eqs_query_synchronizer.d.ts +0 -0
  157. /package/dist/{lib → types}/eqs/eqs_value_list_resolver.d.ts +0 -0
  158. /package/dist/{lib → types}/export/exporter.d.ts +0 -0
  159. /package/dist/{lib → types}/i18n/text_resources.d.ts +0 -0
  160. /package/dist/{lib → types}/main/action.d.ts +0 -0
  161. /package/dist/{lib → types}/main/context_options.d.ts +0 -0
  162. /package/dist/{lib → types}/main/data_fetcher.d.ts +0 -0
  163. /package/dist/{lib → types}/main/eq_actions.d.ts +0 -0
  164. /package/dist/{lib → types}/main/list_cache.d.ts +0 -0
  165. /package/dist/{lib → types}/main/model_loader.d.ts +0 -0
  166. /package/dist/{lib → types}/main/model_storage.d.ts +0 -0
  167. /package/dist/{lib → types}/main/query_file_loader.d.ts +0 -0
  168. /package/dist/{lib → types}/main/query_storage.d.ts +0 -0
  169. /package/dist/{lib → types}/main/query_synchronizer.d.ts +0 -0
  170. /package/dist/{lib → types}/main/value_list_resolver.d.ts +0 -0
  171. /package/dist/{lib → types}/main/widget.d.ts +0 -0
  172. /package/dist/{lib → types}/query/condition.d.ts +0 -0
  173. /package/dist/{lib → types}/query/descriptors.d.ts +0 -0
  174. /package/dist/{lib → types}/query/dto/condition_data.d.ts +0 -0
  175. /package/dist/{lib → types}/query/dto/expression_data.d.ts +0 -0
  176. /package/dist/{lib → types}/query/dto/query_column_data.d.ts +0 -0
  177. /package/dist/{lib → types}/query/dto/query_data.d.ts +0 -0
  178. /package/dist/{lib → types}/query/expression.d.ts +0 -0
  179. /package/dist/{lib → types}/query/query_column.d.ts +0 -0
  180. /package/dist/{lib → types}/query/query_events.d.ts +0 -0
  181. /package/dist/{lib → types}/types/cond_part.d.ts +0 -0
  182. /package/dist/{lib → types}/types/cond_tag.d.ts +0 -0
  183. /package/dist/{lib → types}/types/data_kind.d.ts +0 -0
  184. /package/dist/{lib → types}/types/eq_editor_tag.d.ts +0 -0
  185. /package/dist/{lib → types}/types/expr_tag.d.ts +0 -0
  186. /package/dist/{lib → types}/types/link_type.d.ts +0 -0
  187. /package/dist/{lib → types}/types/widget_group.d.ts +0 -0
  188. /package/dist/{lib → types}/utils/constants.d.ts +0 -0
  189. /package/dist/{lib → types}/utils/format_parser.d.ts +0 -0
  190. /package/dist/{lib → types}/utils/utils.d.ts +0 -0
  191. /package/dist/{lib → types}/utils/version_info.d.ts +0 -0
@@ -1,1503 +0,0 @@
1
- var __extends = (this && this.__extends) || (function () {
2
- var extendStatics = function (d, b) {
3
- extendStatics = Object.setPrototypeOf ||
4
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
6
- return extendStatics(d, b);
7
- };
8
- return function (d, b) {
9
- if (typeof b !== "function" && b !== null)
10
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
11
- extendStatics(d, b);
12
- function __() { this.constructor = d; }
13
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
14
- };
15
- })();
16
- import { EventEmitter, utils as dataUtils, AggregationSettings, DataType } from '@easydata/core';
17
- import { i18n } from '@easydata/core';
18
- import { equtils } from '../utils/utils';
19
- import { DataModel } from '../dm/data_model';
20
- import { Expression } from './expression';
21
- import { QueryColumn, SortDirection } from './query_column';
22
- import { Condition } from './condition';
23
- import { DataKind } from '../types/data_kind';
24
- import { LinkType } from '../types/link_type';
25
- import { ExprTag } from '../types/expr_tag';
26
- import { CondTag } from '../types/cond_tag';
27
- import { QueryChangePart, QueryChangeAction } from './query_events';
28
- /**
29
- * The error that appears when the query is not valid
30
- */
31
- var InvalidQueryError = /** @class */ (function (_super) {
32
- __extends(InvalidQueryError, _super);
33
- /**
34
- * Creates an instance of error type.
35
- * @param message The error message
36
- */
37
- function InvalidQueryError(message) {
38
- var _this = _super.call(this, message) || this;
39
- if (Error.captureStackTrace) {
40
- Error.captureStackTrace(_this, InvalidQueryError);
41
- }
42
- _this.name = 'InvalidQueryError';
43
- return _this;
44
- }
45
- return InvalidQueryError;
46
- }(Error));
47
- export { InvalidQueryError };
48
- /**
49
- * Represents Query.
50
- */
51
- var Query = /** @class */ (function () {
52
- /**
53
- * The default constructor.
54
- * @param model The data model.
55
- * @param data JSON representation object.
56
- * @param options The options.
57
- * @throws {InvalidQueryError} The error throws when query is not valid.
58
- */
59
- function Query(model, data, options) {
60
- var _this = this;
61
- /**
62
- * Contains user-defined extra data for current query.
63
- */
64
- this.extraData = {};
65
- this.innerData = {};
66
- this.isNewbie = true;
67
- this._isModified = false;
68
- this._enableAggregation = false;
69
- this.parentQuery = null;
70
- this.drillDowns = [];
71
- this.syncSortOrderWithColumnsOrder = true;
72
- if (options) {
73
- this.context = options.context;
74
- if (this.context) {
75
- this.syncSortOrderWithColumnsOrder = this.context.options.syncSortOrderWithColumnsOrder;
76
- }
77
- }
78
- this.eventEmitter = new EventEmitter(this);
79
- this.root = new Condition(this, CondTag.Group);
80
- this.extraConditions = new Condition(this, CondTag.Group);
81
- this.columns = new Array();
82
- //this.justsorted = new Array<QueryColumn>();
83
- this.sortedColumns = new Array();
84
- if (!this.id) {
85
- this.regenerateId();
86
- }
87
- this.setDefaultName();
88
- this.description = "";
89
- this.model = (model) ? model : new DataModel();
90
- if (data) {
91
- this.loadFromData(data);
92
- }
93
- this.attributeExprTag = (options) ? options.attrTag || ExprTag.EntityAttribute : ExprTag.EntityAttribute;
94
- this.clientListRequestHandler = (options) ? options.clientListRequestHandler : new Array();
95
- this.serverListRequestHandler = (options) ? options.serverListRequestHandler : new Array();
96
- this.timezoneOffset = (new Date()).getTimezoneOffset();
97
- this.aggrSettings = new AggregationSettings(this);
98
- if (options) {
99
- this.parentQuery = options.parent;
100
- }
101
- this.addChangedCallback(function (ev) {
102
- if (ev.data
103
- && ((ev.data.part & QueryChangePart.Columns) > 0)
104
- && !ev.data.aggregation) {
105
- if (_this._enableAggregation && _this.updateAggregationSettings()) {
106
- _this.fireChangedEvent({
107
- part: QueryChangePart.Aggregation
108
- });
109
- }
110
- }
111
- });
112
- }
113
- Query.prototype.isModified = function () {
114
- return this._isModified;
115
- };
116
- Query.prototype.resetModified = function () {
117
- this._isModified = false;
118
- };
119
- Object.defineProperty(Query.prototype, "enableAggregation", {
120
- get: function () {
121
- return this._enableAggregation;
122
- },
123
- set: function (value) {
124
- var oldValue = this._enableAggregation;
125
- this._enableAggregation = value;
126
- if (!oldValue && value) {
127
- this.processQueryForAggregation();
128
- var aggrCols = this.columns.filter(function (c) { return c.enabled
129
- && c.expr.tag == ExprTag.AggregateFunction; });
130
- }
131
- if (!this._enableAggregation) {
132
- this.aggrSettings.clear();
133
- }
134
- },
135
- enumerable: false,
136
- configurable: true
137
- });
138
- Query.prototype.updateAggregationSettings = function () {
139
- var hasChanged = false;
140
- var aggrSettings = this.getAggregationSettings();
141
- for (var i = aggrSettings.groups.length - 1; i >= 0; i--) {
142
- var group = aggrSettings.groups[i];
143
- var _loop_1 = function (j) {
144
- var colId = group.columns[j];
145
- if (!this_1.columns.find(function (col) { return col.id === colId; })) {
146
- group.columns.splice(j, 1);
147
- hasChanged = true;
148
- }
149
- };
150
- var this_1 = this;
151
- for (var j = group.columns.length - 1; j >= 0; j--) {
152
- _loop_1(j);
153
- }
154
- if (group.columns.length == 0) {
155
- aggrSettings.groups.slice(i, 1);
156
- }
157
- }
158
- var aggregates = aggrSettings.getAggregates();
159
- var _loop_2 = function (i) {
160
- var aggr = aggregates[i];
161
- if (!this_2.columns.find(function (col) { return col.id === aggr.colId; })) {
162
- aggregates.splice(i, 1);
163
- hasChanged = true;
164
- }
165
- };
166
- var this_2 = this;
167
- for (var i = aggregates.length - 1; i >= 0; i--) {
168
- _loop_2(i);
169
- }
170
- return hasChanged;
171
- };
172
- Query.prototype.createSubQuery = function () {
173
- return new Query(this.model, null, { context: this.context });
174
- };
175
- Query.prototype.useAggregation = function (settingsTuner, silent) {
176
- if (silent === void 0) { silent = false; }
177
- this.aggrSettings.clear();
178
- settingsTuner(this.aggrSettings);
179
- if (this.enableAggregation) {
180
- this.processQueryForAggregation(silent);
181
- }
182
- else if (!silent) {
183
- console.warn('Aggregation is turned off. Turn it on with query.enableAggregation = true');
184
- return;
185
- }
186
- };
187
- Query.prototype.useDefaultAggregation = function () {
188
- var _this = this;
189
- this._enableAggregation = true;
190
- this.useAggregation(function (settings) {
191
- var aggrCols = _this.columns.filter(function (c) { return c.enabled
192
- && c.expr.tag == ExprTag.AggregateFunction; });
193
- if (aggrCols.length) {
194
- // query contains aggregate columns
195
- var colsToGroup = _this.columns.filter(function (c) { return c.enabled
196
- && c.expr.tag == ExprTag.EntityAttribute; });
197
- var aggrsToAdd = [];
198
- for (var _i = 0, aggrCols_1 = aggrCols; _i < aggrCols_1.length; _i++) {
199
- var col = aggrCols_1[_i];
200
- aggrsToAdd.push({ id: col.id, func: col.expr.func });
201
- _this.changeColumnType(col, ExprTag.EntityAttribute);
202
- }
203
- for (var _a = 0, aggrsToAdd_1 = aggrsToAdd; _a < aggrsToAdd_1.length; _a++) {
204
- var aggr = aggrsToAdd_1[_a];
205
- settings.addAggregateColumn(aggr.id, aggr.func);
206
- }
207
- for (var _b = 0, colsToGroup_1 = colsToGroup; _b < colsToGroup_1.length; _b++) {
208
- var col = colsToGroup_1[_b];
209
- settings.addGroup({ columns: [col.id], name: col.caption });
210
- }
211
- settings.addGrandTotals();
212
- _this.fireChangedEvent({
213
- part: QueryChangePart.Columns,
214
- wasModified: false
215
- });
216
- }
217
- else {
218
- // query does not has any aggr columns
219
- var colToAggr = _this.getColumns()
220
- .filter(function (c) { return c.enabled && c.expr.tag == ExprTag.EntityAttribute
221
- && _this.model.getAttributeById(c.expr.value).dataType === DataType.Currency; })[0];
222
- var colToGroup = _this.getColumns()
223
- .filter(function (c) { return c.enabled && c.expr.tag == ExprTag.EntityAttribute
224
- && _this.model.getAttributeById(c.expr.value).dataType === DataType.String; })[0];
225
- if ((!colToAggr && !settings.hasAggregates()) || !colToGroup)
226
- return;
227
- settings.addGrandTotals()
228
- .addAggregateColumn(colToAggr.id, 'SUM')
229
- .addGroup({ columns: [colToGroup.id], name: 'Default' });
230
- }
231
- });
232
- };
233
- Query.prototype.processQueryForAggregation = function (silent) {
234
- var _this = this;
235
- if (silent === void 0) { silent = false; }
236
- var aggrSettings = this.getAggregationSettings();
237
- var groups = aggrSettings.getGroups();
238
- if (groups.length > 0) {
239
- var groupColIds = groups[groups.length - 1].columns;
240
- //take the list of grouped columns from the latest group
241
- var groupedCols = [];
242
- var sortedCols = [];
243
- for (var _i = 0, _a = this.getColumns(); _i < _a.length; _i++) {
244
- var col = _a[_i];
245
- if (groupColIds.indexOf(col.id) >= 0) {
246
- groupedCols.push(col);
247
- }
248
- else if (this.getColumnSorting(col) != SortDirection.None) {
249
- sortedCols.push(col);
250
- }
251
- }
252
- var sortIndex_1 = 0;
253
- groupedCols.forEach(function (col) {
254
- if (_this.getColumnSorting(col) == SortDirection.None) {
255
- _this.setColumnSorting(col, SortDirection.Ascending);
256
- }
257
- _this.setColumnSortIndex(col, sortIndex_1++);
258
- //col.sortIndex = sortIndex++;
259
- });
260
- sortedCols.forEach(function (col) {
261
- _this.setColumnSortIndex(col, sortIndex_1++);
262
- // col.sortIndex = sortIndex++;
263
- });
264
- if (!silent) {
265
- this.fireChangedEvent({
266
- part: QueryChangePart.Columns,
267
- aggregation: true,
268
- wasModified: false
269
- });
270
- }
271
- }
272
- };
273
- Query.prototype.getAggregationSettings = function () {
274
- return this.aggrSettings;
275
- };
276
- Query.prototype.getColumnIds = function (from, to) {
277
- to = to || from;
278
- return this.columns.slice(from, to - from + 1)
279
- .map(function (c) { return c.id; });
280
- };
281
- /**
282
- * Checks if the columns with IDs passed in function's parameter exist and are valid
283
- */
284
- Query.prototype.validateColumns = function (colIds) {
285
- var enabledColIds = this.columns.filter(function (c) { return c.enabled; }).map(function (c) { return c.id; });
286
- var prevIndex = -1;
287
- for (var _i = 0, colIds_1 = colIds; _i < colIds_1.length; _i++) {
288
- var col = colIds_1[_i];
289
- var index = enabledColIds.indexOf(col);
290
- if (index < 0)
291
- return false;
292
- if (prevIndex > index)
293
- return false;
294
- prevIndex = index;
295
- }
296
- return true;
297
- };
298
- Query.prototype.validateAggregate = function (colId, funcId) {
299
- var col = this.columns.filter(function (c) { return c.enabled && c.id === colId; })[0];
300
- var func = this.model.findAggrFunctionById(funcId);
301
- if (!func || !col || func.appliedTypes.indexOf(col.expr.dataType) < 0)
302
- return false;
303
- return true;
304
- };
305
- Query.prototype.isEx = function () {
306
- return false;
307
- };
308
- /**
309
- * Resets the query: clears all its conditions and columns, regenerate the ID and sets the default name.
310
- * @param silent Indicates whether we need to avoid firing the QueryChanged event.
311
- */
312
- Query.prototype.reset = function (clearQuery) {
313
- if (clearQuery === void 0) { clearQuery = true; }
314
- if (clearQuery) {
315
- this.clear();
316
- }
317
- this.regenerateId();
318
- this.setDefaultName();
319
- this._isModified = false;
320
- this.isNewbie = true;
321
- };
322
- Query.prototype.hasEnabledAggrColumns = function () {
323
- return this.columns.filter(function (col) { return col.enabled
324
- && col.expr.tag === ExprTag.AggregateFunction; }).length > 0;
325
- };
326
- Query.prototype.setDefaultName = function () {
327
- this.setName(i18n.getText('NewQueryName'));
328
- return this.getName();
329
- };
330
- /**
331
- * Validates wether the query corresponds to the model.
332
- * @param options Validate options.
333
- * @throws {InvalidQueryError} If query is not valid.
334
- */
335
- Query.prototype.validate = function (options) {
336
- options = options || {};
337
- var entityAttrNotExistError = "EntityAttribute isn't declared in the model: ";
338
- var wrongExprTag = "The expression has wrond tag: ";
339
- if (!this.model) {
340
- throw new InvalidQueryError("Model is not declared");
341
- }
342
- if (this.columns) {
343
- for (var _i = 0, _a = this.columns; _i < _a.length; _i++) {
344
- var column = _a[_i];
345
- if (column.expr.tag == ExprTag.EntityAttribute
346
- || column.expr.tag === ExprTag.ParentEntityAttribute) {
347
- var attr = this.model.getAttributeById(column.expr.value);
348
- if (!attr) {
349
- throw new InvalidQueryError(entityAttrNotExistError + column.expr.value);
350
- }
351
- }
352
- else if (column.expr.tag === ExprTag.AggregateFunction) {
353
- var attr = this.model.getAttributeById(column.expr.args[0].value);
354
- if (!attr) {
355
- throw new InvalidQueryError(entityAttrNotExistError + column.expr.args[0].value);
356
- }
357
- }
358
- else if (column.expr.tag === ExprTag.Unknown) {
359
- throw new InvalidQueryError(wrongExprTag + column.expr.tag);
360
- }
361
- }
362
- }
363
- if (this.root && this.root.getConditions()) {
364
- this.validateConditionGroup(this.root, options);
365
- }
366
- };
367
- Query.prototype.getAggregatedColumns = function () {
368
- return this.getColumns().filter(function (col) { return col.enabled
369
- && col.expr.tag === ExprTag.AggregateFunction; });
370
- };
371
- /**
372
- * Validates wether the query corresponds to the model.
373
- * @param options Validate options.
374
- * @returns `true` if it is valid, otherwise `false`
375
- */
376
- Query.prototype.tryValidate = function (options) {
377
- try {
378
- this.validate(options);
379
- return true;
380
- }
381
- catch (e) {
382
- if (e instanceof InvalidQueryError) {
383
- return false;
384
- }
385
- else {
386
- throw e;
387
- }
388
- }
389
- };
390
- Query.prototype.validateConditionGroup = function (curCond, options) {
391
- if (curCond.isGroup()) {
392
- for (var _i = 0, _a = curCond.getConditions(); _i < _a.length; _i++) {
393
- var condition = _a[_i];
394
- if (!condition.enabled)
395
- continue;
396
- this.validateConditionGroup(condition, options);
397
- }
398
- }
399
- else if (curCond.expressions[0].tag === ExprTag.EntityAttribute) {
400
- var attrId = curCond.expressions[0].value;
401
- var attr = this.model.getAttributeById(attrId);
402
- if (!attr) {
403
- throw new InvalidQueryError("EntityAttribute isn't declared in the model: " + attrId);
404
- }
405
- if (!options.checkExpressions)
406
- return;
407
- for (var i = 1; i < curCond.expressions.length; i++) {
408
- var expr = curCond.expressions[i];
409
- if (expr.tag !== ExprTag.Constant)
410
- continue;
411
- //validate number exprs
412
- if (dataUtils.isNumericType(expr.dataType)) {
413
- if (!expr.value)
414
- continue;
415
- var values = [];
416
- if (expr.kind === DataKind.List) {
417
- values = expr.value.split(/\s*,\s*/);
418
- }
419
- else {
420
- values = [expr.value];
421
- }
422
- for (var _b = 0, values_1 = values; _b < values_1.length; _b++) {
423
- var value = values_1[_b];
424
- var parsedVal = +value;
425
- if (isNaN(parsedVal)) {
426
- throw new InvalidQueryError("Expression constant is NaN in condition:" + curCond.id);
427
- }
428
- if (dataUtils.isIntType(expr.dataType) && parsedVal != parseInt(value, 10)) {
429
- throw new InvalidQueryError("Expression constant is wrong interger in condition: " + curCond.id);
430
- }
431
- }
432
- }
433
- }
434
- }
435
- };
436
- /**
437
- * Sets data to the query.
438
- * @param data JSON representation object or JSON string
439
- * @param changeStatus Indicate whether we should change the "isModified" status (by default == `true`)
440
- */
441
- Query.prototype.loadFromDataOrJson = function (data, options) {
442
- if (data) {
443
- if (typeof data === "string") {
444
- this.loadFromJson(data, options);
445
- }
446
- else {
447
- this.loadFromData(data, options);
448
- }
449
- }
450
- };
451
- /**
452
- * Loads query from JSON.
453
- * @param json The JSON string.
454
- * @param options The options.
455
- * @throws {InvalidQueryError} The error throws when query is not valid.
456
- */
457
- Query.prototype.loadFromJson = function (json, options) {
458
- var data = JSON.parse(json);
459
- this.loadFromData(data, options);
460
- };
461
- /**
462
- * Loads query from data representation.
463
- * @param data The query DTO.
464
- * @param options The options.
465
- * @throws {InvalidQueryError} The error throws when query is not valid.
466
- */
467
- Query.prototype.loadFromData = function (data, options) {
468
- var _this = this;
469
- options = dataUtils.assign({
470
- changeStatus: true,
471
- validate: true
472
- }, options || {});
473
- var noErrors = false;
474
- this.clear();
475
- this.clearDrillDowns();
476
- if (data) {
477
- this.id = data.id;
478
- this.root.loadFromData(this.model, data.root);
479
- if (data.root && data.root.conds) {
480
- for (var i = 0; i < data.root.conds.length; i++) {
481
- var condData = data.root.conds[i];
482
- if (condData.conds && !this.isEx()) {
483
- console.error("Community version doesn't support working with groups.");
484
- continue;
485
- }
486
- var cond = this.createCondition();
487
- cond.loadFromData(this.model, condData);
488
- this.root.addCondition(cond);
489
- }
490
- }
491
- this.name = data.name;
492
- this.description = data.desc;
493
- //this.justsorted = [];
494
- this.sortedColumns = [];
495
- if (data.justsortcols) {
496
- for (var i = 0; i < data.justsortcols.length; i++) {
497
- var newCol = this.createColumn(true);
498
- newCol.loadFromData(this.model, data.justsortcols[i]);
499
- //this.justsorted.push(newCol);
500
- this.addColumnToSorted(newCol);
501
- }
502
- }
503
- if (data.cols) {
504
- for (var i = 0; i < data.cols.length; i++) {
505
- var newCol = this.createColumn();
506
- newCol.loadFromData(this.model, data.cols[i]);
507
- this.columns.push(newCol);
508
- if (newCol._sorting != SortDirection.None) {
509
- this.addColumnToSorted(newCol);
510
- }
511
- }
512
- }
513
- this.sortedColumns.sort(function (col1, col2) {
514
- return col1._sortIndex - col2._sortIndex;
515
- });
516
- this.fixupSortingIndices();
517
- if (data.extraData) {
518
- this.extraData = data.extraData;
519
- }
520
- this.innerData = data.innerData || this.innerData;
521
- if (this.innerData && this.innerData.aggr) {
522
- this._enableAggregation = this.innerData.aggr.enabled || false;
523
- this.useAggregation(function (settings) {
524
- settings.loadFromData(_this.innerData.aggr);
525
- }, true);
526
- }
527
- if (data.dds && data.dds.length) {
528
- for (var _i = 0, _a = data.dds; _i < _a.length; _i++) {
529
- var dd = _a[_i];
530
- var query = this.addDrillDown();
531
- query.loadFromData(dd);
532
- }
533
- }
534
- }
535
- noErrors = true;
536
- if (noErrors) {
537
- if (options.changeStatus) {
538
- this.isNewbie = false;
539
- this._isModified = false;
540
- }
541
- }
542
- if (options.validate) {
543
- this.validate();
544
- }
545
- };
546
- /**
547
- *
548
- */
549
- Query.prototype.addDrillDown = function () {
550
- var query = this.model.createQuery();
551
- query.parentQuery = this;
552
- this.drillDowns.push(query);
553
- return query;
554
- };
555
- Query.prototype.getDrillDowns = function () {
556
- return this.drillDowns;
557
- };
558
- Query.prototype.getParentQuery = function () {
559
- return this.parentQuery;
560
- };
561
- /**
562
- *
563
- */
564
- Query.prototype.findColumnById = function (colId) {
565
- for (var _i = 0, _a = this.columns; _i < _a.length; _i++) {
566
- var col = _a[_i];
567
- if (col.id === colId)
568
- return col;
569
- }
570
- return null;
571
- };
572
- /**
573
- * Gets DataModel object associated with this query.
574
- * @retruns The data model.
575
- */
576
- Query.prototype.getModel = function () {
577
- return this.model;
578
- };
579
- /**
580
- * Sets DataModel object associated with this query.
581
- * @param model The data model.
582
- */
583
- Query.prototype.setModel = function (model) {
584
- this.model = model;
585
- };
586
- /**
587
- * Loads data model.
588
- * @param model JSON representation object or JSON string.
589
- */
590
- Query.prototype.loadModelData = function (model) {
591
- this.model.setData(model);
592
- };
593
- /**
594
- * Returns JSON representation of this query.
595
- * @retruns JSON string.
596
- */
597
- Query.prototype.toJSON = function () {
598
- return JSON.stringify(this.toJSONData());
599
- };
600
- /**
601
- * Returns JSON representation of this query.
602
- * @retruns JSON representation object.
603
- */
604
- Query.prototype.toJSONData = function () {
605
- var _this = this;
606
- this.fixupSortingIndices();
607
- var obj = {
608
- modelId: this.model ? this.model.getId() : null,
609
- modelName: this.model ? this.model.getName() : null
610
- };
611
- obj.id = this.id;
612
- obj.name = this.name;
613
- if (this.description) {
614
- obj.desc = this.description;
615
- }
616
- obj.cols = [];
617
- for (var i = 0; i < this.columns.length; i++) {
618
- obj.cols.push(this.columns[i].saveToData());
619
- }
620
- obj.justsortcols = [];
621
- // for (let i = 0; i < this.justsorted.length; i++) {
622
- // obj.justsortcols.push(this.justsorted[i].saveToData());
623
- // }
624
- this.sortedColumns.forEach(function (col) {
625
- if (col.isJustSorted()) {
626
- obj.justsortcols.push(col.saveToData());
627
- }
628
- });
629
- if (this.extraData) {
630
- obj.extraData = this.extraData;
631
- }
632
- if (this.innerData) {
633
- obj.innerData = this.innerData;
634
- obj.innerData.aggr = this.aggrSettings.saveToData();
635
- obj.innerData.aggr.enabled = this._enableAggregation;
636
- }
637
- var rootCondToData = function (condGroup) {
638
- var result = condGroup.saveToData();
639
- result.conds = [];
640
- var conditions = condGroup.getConditions();
641
- for (var i = 0; i < conditions.length; i++) {
642
- if (conditions[i].isGroup() && !_this.isEx()) {
643
- console.error("Community version doesn't support working with groups.");
644
- continue;
645
- }
646
- result.conds.push(conditions[i].saveToData());
647
- }
648
- return result;
649
- };
650
- obj.root = rootCondToData(this.root);
651
- obj.extraConds = rootCondToData(this.extraConditions);
652
- if (this.drillDowns.length > 0) {
653
- obj.dds = [];
654
- for (var _i = 0, _a = this.drillDowns; _i < _a.length; _i++) {
655
- var dd = _a[_i];
656
- obj.dds.push(dd.toJSONData());
657
- }
658
- }
659
- obj.timezoneOffset = this.timezoneOffset;
660
- obj.locale = i18n.getCurrentLocale();
661
- return obj;
662
- };
663
- /**
664
- * Checks wether query does not contains any condtion.
665
- * @retruns `true` if query does not contain any condition, otherwise `false`.
666
- */
667
- Query.prototype.isEmptyConditions = function () {
668
- return this.root.isEmpty();
669
- };
670
- /**
671
- * Checks wether query does not contains any column.
672
- * @retruns `true` if query does not contain any column, otherwise `false`.
673
- */
674
- Query.prototype.isEmptyColumns = function () {
675
- return !this.columns || this.columns.length === 0;
676
- };
677
- /**
678
- * Checks wether query does not contains any column and any condition.
679
- * @retruns `true` if query does not contain any column and any condition, otherwise `false`.
680
- */
681
- Query.prototype.isEmpty = function () {
682
- return this.isEmptyColumns() && this.isEmptyConditions();
683
- };
684
- /**
685
- * Clears the query (all conditions, columns, extraData, etc).
686
- */
687
- Query.prototype.clear = function () {
688
- this._enableAggregation = false;
689
- this.aggrSettings.clear();
690
- this.clearColumns();
691
- this.clearConditions();
692
- this.clearExtraConditions();
693
- this.innerData = {};
694
- this.extraData = {};
695
- };
696
- /**
697
- * Clears all columns in query
698
- */
699
- Query.prototype.clearColumns = function () {
700
- this.columns = [];
701
- //this.justsorted = [];
702
- this.sortedColumns = [];
703
- };
704
- /**
705
- * Clears all conditions in query.
706
- */
707
- Query.prototype.clearConditions = function () {
708
- this.root.clearConditions();
709
- this.root.linkType = LinkType.All;
710
- this.root.enabled = true;
711
- };
712
- /**
713
- * Clears all extra conditions in query
714
- */
715
- Query.prototype.clearExtraConditions = function () {
716
- this.extraConditions.clearConditions();
717
- };
718
- /**
719
- * Clears all drill downs
720
- */
721
- Query.prototype.clearDrillDowns = function () {
722
- this.drillDowns = [];
723
- };
724
- /**
725
- * Gets query ID.
726
- * @returns The ID>
727
- */
728
- Query.prototype.getId = function () {
729
- return this.id;
730
- };
731
- /**
732
- * Sets query ID.
733
- * @param id New Query ID.
734
- */
735
- Query.prototype.setId = function (id) {
736
- this.id = id;
737
- };
738
- /**
739
- * Regenerates current query ID.
740
- * @param id New Query ID.
741
- */
742
- Query.prototype.regenerateId = function () {
743
- this.id = dataUtils.generateId("quer");
744
- };
745
- /**
746
- * Gets name of the query.
747
- * @retruns The name.
748
- */
749
- Query.prototype.getName = function () {
750
- return this.name;
751
- };
752
- /**
753
- * Sets name of the query
754
- * @param name New name.
755
- */
756
- Query.prototype.setName = function (name) {
757
- this.name = name;
758
- };
759
- /**
760
- * Gets description of the query.
761
- * @retruns The description.
762
- */
763
- Query.prototype.getDescription = function () {
764
- return this.description;
765
- };
766
- /**
767
- * Sets description of the query
768
- * @param description New description.
769
- */
770
- Query.prototype.setDescription = function (description) {
771
- this.description = description;
772
- };
773
- /**
774
- * Get array of columns.
775
- * @retruns Columns.
776
- */
777
- Query.prototype.getColumns = function () {
778
- return this.columns;
779
- };
780
- /**
781
- * Gets array of columns that are used as keys
782
- * for totals.
783
- */
784
- Query.prototype.getUsedInTotalsColumns = function () {
785
- var aggrs = this.getAggregatedColumns();
786
- if (aggrs.length === 0)
787
- return [];
788
- var cols = this.getColumns()
789
- .filter(function (col) { return col.enabled && col.expr.tag === ExprTag.EntityAttribute; });
790
- if (cols.length)
791
- cols.pop();
792
- return cols;
793
- };
794
- Query.prototype.addColumnToSorted = function (col) {
795
- var colJson = JSON.stringify(col.expr.saveToData());
796
- var isUnique = true;
797
- this.sortedColumns.forEach(function (scol) {
798
- var qcolJson = JSON.stringify(scol.expr.saveToData());
799
- if (qcolJson === colJson) {
800
- isUnique = false;
801
- }
802
- });
803
- if (isUnique) {
804
- this.sortedColumns.push(col);
805
- }
806
- };
807
- /**
808
- * Get array of columns used only in sorting.
809
- * @retruns Columns.
810
- */
811
- Query.prototype.getJustSortedColumns = function () {
812
- return this.sortedColumns.filter(function (col) {
813
- return col.isJustSorted();
814
- });
815
- };
816
- /**
817
- * Gets array of columns used in sorting
818
- * @retruns Columns
819
- */
820
- Query.prototype.getSortedColumns = function () {
821
- return this.sortedColumns;
822
- };
823
- /**
824
- * Gets Colummn sorting index.
825
- * @param col The column.
826
- * @retruns The index.
827
- */
828
- Query.prototype.getColumnSortIndex = function (col) {
829
- return this.sortedColumns.indexOf(col);
830
- };
831
- Query.prototype.setColumnSortIndex = function (col, index) {
832
- if (index < 0)
833
- return;
834
- var colIdx = this.getColumnSortIndex(col);
835
- if (colIdx < 0) {
836
- this.addColumnToSorted(col);
837
- colIdx = this.sortedColumns.length - 1;
838
- }
839
- dataUtils.moveArrayItem(this.sortedColumns, colIdx, index);
840
- this.fixupSortingIndices();
841
- };
842
- Query.prototype.getColumnSorting = function (col) {
843
- var idx = this.getColumnSortIndex(col);
844
- if (idx >= 0) {
845
- return col._sorting != SortDirection.None ? col._sorting : SortDirection.Ascending;
846
- }
847
- else {
848
- return SortDirection.None;
849
- }
850
- };
851
- Query.prototype.setColumnSorting = function (col, direction) {
852
- var idx = this.getColumnSortIndex(col);
853
- col._sorting = direction;
854
- if (idx >= 0) {
855
- if (direction == SortDirection.None) {
856
- dataUtils.removeArrayItem(this.sortedColumns, col);
857
- this.fixupSortingIndices();
858
- }
859
- }
860
- else {
861
- if (direction != SortDirection.None) {
862
- this.addColumnToSorted(col);
863
- this.fixupSortingIndices();
864
- }
865
- }
866
- this.adjustColumnsSorting();
867
- };
868
- Query.prototype.fixupSortingIndices = function () {
869
- this.sortedColumns.forEach(function (col, index) {
870
- col._sortIndex = index;
871
- });
872
- };
873
- Query.prototype.adjustColumnsSorting = function () {
874
- var _this = this;
875
- if (!this.syncSortOrderWithColumnsOrder) {
876
- return;
877
- }
878
- if (this.getJustSortedColumns().length > 0) {
879
- return;
880
- }
881
- var idx = 0;
882
- this.columns.forEach(function (col) {
883
- if (col.sorting != SortDirection.None) {
884
- var colIdx = _this.getColumnSortIndex(col);
885
- if (colIdx >= 0) {
886
- dataUtils.moveArrayItem(_this.sortedColumns, colIdx, idx);
887
- idx++;
888
- }
889
- }
890
- });
891
- this.fixupSortingIndices();
892
- };
893
- Query.prototype.createColumn = function (justsorted) {
894
- if (justsorted === void 0) { justsorted = false; }
895
- return new QueryColumn(this, justsorted);
896
- };
897
- Query.prototype.getColumnById = function (id) {
898
- return dataUtils.findItemById(this.columns, id);
899
- };
900
- /**
901
- * Adds a new column with specified parameters
902
- * @param desriptor The descriptor of the new column. Can contain different parameters like:
903
- * `attribute` - a reference to a EntityAttr object,
904
- * `attributeId` - an ID of the entity attribute we need (if we don't indicate `attribute`),
905
- * `index`, sorting`, 'sortIndex` and other
906
- *
907
- * @param author The author of this operation. This parameter is used in EasyQuery widgets only to indicate which of the initiates the change.
908
- * @retruns The column.
909
- * @example
910
- * query.addColumn({
911
- * attributeId: 'Customers.CompanyName',
912
- * sortIndex: SortDirection.Ascending
913
- * }});
914
- */
915
- Query.prototype.addColumn = function (descriptor, silent) {
916
- if (silent === void 0) { silent = false; }
917
- var newColumn;
918
- if (descriptor.column) {
919
- newColumn = descriptor.column;
920
- }
921
- else {
922
- newColumn = this.createColumn(descriptor.justsorted);
923
- var attr = void 0;
924
- if (descriptor.attribute) {
925
- attr = descriptor.attribute;
926
- }
927
- else if (descriptor.attributeId) {
928
- attr = this.model.getAttributeById(descriptor.attributeId);
929
- if (!attr) {
930
- throw new Error("Incorrect attribute id (".concat(descriptor.attributeId, "): addColumn"));
931
- }
932
- }
933
- else if (typeof descriptor.constValue === 'undefined' && !descriptor.customSql) {
934
- throw new Error('Not enought data for addColumn operation. One of the following options must be defined: attribute, attributeId, constValue or customSlq');
935
- }
936
- if (attr && !attr.useInResult) {
937
- if (attr.lookupAttr) {
938
- attr = this.model.getAttributeById(attr.lookupAttr);
939
- }
940
- if (!attr || !attr.useInResult) {
941
- throw new Error("Can't use this attribute for a column");
942
- }
943
- }
944
- if (descriptor.aggrFuncId && descriptor.customSql) {
945
- console.warn("Column cannot be created using aggrFuncId and customSql at the same time. " +
946
- "cusomSql will be used.");
947
- }
948
- if (typeof descriptor.constValue !== 'undefined') {
949
- newColumn.expr.tag = ExprTag.Constant;
950
- newColumn.expr.setValue(descriptor.constValue);
951
- newColumn.expr.kind = DataKind.Scalar;
952
- newColumn.expr.dataType = descriptor.constType || DataType.String;
953
- }
954
- else if (descriptor.customSql) {
955
- newColumn.caption = 'Custom SQL Column';
956
- newColumn.expr.tag = ExprTag.CustomSql;
957
- newColumn.expr.sql = descriptor.customSql;
958
- newColumn.expr.kind = DataKind.Query;
959
- newColumn.expr.dataType = DataType.Unknown;
960
- newColumn.expr.baseAttrId = descriptor.attributeId;
961
- if (!this.context.options.allowCustomExpressions) {
962
- newColumn.enabled = false;
963
- }
964
- }
965
- else if (descriptor.aggrFuncId) {
966
- var funcId = descriptor.aggrFuncId;
967
- newColumn.expr.tag = ExprTag.AggregateFunction;
968
- newColumn.expr.func = descriptor.aggrFuncId;
969
- newColumn.expr.kind = DataKind.Scalar;
970
- newColumn.expr.dataType = funcId == 'COUNT' || funcId == 'CNTDST' ? 4 : attr.dataType;
971
- newColumn.expr.distinct = false;
972
- var arg = new Expression(newColumn);
973
- arg.setValue(attr.id, true);
974
- arg.tag = ExprTag.EntityAttribute;
975
- arg.kind = DataKind.Attribute;
976
- arg.dataType = attr.dataType;
977
- newColumn.expr.args.push(arg);
978
- }
979
- else {
980
- newColumn.expr.setValue(attr.id, true);
981
- newColumn.expr.tag = ExprTag.EntityAttribute;
982
- newColumn.expr.kind = DataKind.Attribute;
983
- newColumn.expr.dataType = attr.dataType;
984
- newColumn.params.concat(attr.params);
985
- }
986
- newColumn.caption = descriptor.caption || this.context.getDefaultColumnCaption(newColumn);
987
- newColumn._sorting = descriptor.sorting || SortDirection.None;
988
- newColumn._sortIndex = descriptor.sortIndex || -1;
989
- newColumn.enabled = dataUtils.getIfDefined(descriptor.enabled, true);
990
- newColumn.setReadOnly(dataUtils.getIfDefined(descriptor.readonly, false));
991
- }
992
- var justsorted = descriptor.justsorted || (descriptor.column && descriptor.column.isJustSorted());
993
- if (!justsorted) {
994
- if (descriptor.index && descriptor.index >= 0 && descriptor.index < this.columns.length) {
995
- this.columns.splice(descriptor.index, 0, newColumn);
996
- }
997
- else {
998
- this.columns.push(newColumn);
999
- }
1000
- }
1001
- this.setColumnSorting(newColumn, newColumn._sorting);
1002
- if (!silent) {
1003
- this.fireColumnsChangedEvent(QueryChangeAction.Add, newColumn, justsorted);
1004
- }
1005
- return newColumn;
1006
- };
1007
- /**
1008
- * Adds a new column with specified index.
1009
- * @param column The column or array of columns
1010
- * @param index The index of new column
1011
- * @param author The author.
1012
- * @example
1013
- * const column = new Column();
1014
- * column.caption = "Company name";
1015
- *
1016
- * query.addColumn(column, 1)
1017
- */
1018
- Query.prototype.addColumnObj = function (column, index, author) {
1019
- var _this = this;
1020
- var columns = this.columns;
1021
- if (typeof column.length == "undefined") {
1022
- if (typeof index == "number") {
1023
- columns.splice(index, 0, column);
1024
- }
1025
- else {
1026
- columns.push(column);
1027
- }
1028
- if (column.sorting != SortDirection.None) {
1029
- this.addColumnToSorted(column);
1030
- }
1031
- this.fixupSortingIndices();
1032
- }
1033
- else {
1034
- if (typeof index == "number") {
1035
- columns.push.apply(columns, [index, 0].concat(column));
1036
- }
1037
- else {
1038
- columns.push.apply(columns, [].concat(column));
1039
- }
1040
- column.forEach(function (col) {
1041
- if (col.sorting != SortDirection.None) {
1042
- _this.addColumnToSorted(col);
1043
- }
1044
- });
1045
- this.fixupSortingIndices();
1046
- }
1047
- };
1048
- /**
1049
- * Changes column to Aggregate function, custom expression or simple column.
1050
- * @param column The column to change.
1051
- * @param tag New column expression tag.
1052
- * @param args The arguments (Contains aggregate function id)
1053
- */
1054
- Query.prototype.changeColumnType = function (column, tag, args) {
1055
- if (column.isJustSorted())
1056
- return;
1057
- args = args || {};
1058
- if (tag === ExprTag.AggregateFunction) {
1059
- if (!args.funcId)
1060
- throw Error('"funcId" argument is not defined');
1061
- if (column.expr.tag == ExprTag.AggregateFunction) {
1062
- column.expr.func = args.funcId;
1063
- return;
1064
- }
1065
- var expr = new Expression(column);
1066
- expr.tag = ExprTag.AggregateFunction;
1067
- expr.func = args.funcId;
1068
- expr.distinct = false;
1069
- expr.dataType = args.funcId == 'COUNT' || args.funcId == 'CNTDST' ? 4 : column.expr.dataType;
1070
- var arg = new Expression(column);
1071
- arg.loadFromData(this.model, column.expr.saveToData());
1072
- expr.args.push(arg);
1073
- column.expr = expr;
1074
- }
1075
- else if (tag === ExprTag.CustomSql) {
1076
- var attrId = (column.expr.tag == ExprTag.AggregateFunction)
1077
- ? column.expr.args[0].value
1078
- : column.expr.value;
1079
- var sqlText = '';
1080
- if (this.model) {
1081
- sqlText = this.model.getAttributeById(attrId).expr;
1082
- }
1083
- var customSqlExpr = new Expression(column);
1084
- customSqlExpr.tag = ExprTag.CustomSql;
1085
- customSqlExpr.dataType = DataType.String; //the default data type
1086
- customSqlExpr.sql = sqlText;
1087
- customSqlExpr.baseAttrId = attrId;
1088
- column.expr = customSqlExpr;
1089
- }
1090
- else if (tag === ExprTag.EntityAttribute) {
1091
- if (column.expr.tag === ExprTag.EntityAttribute
1092
- || column.expr.tag === ExprTag.ParentEntityAttribute) {
1093
- return;
1094
- }
1095
- var attrId = (column.expr.tag == ExprTag.AggregateFunction)
1096
- ? column.expr.args[0].value
1097
- : column.expr.baseAttrId;
1098
- var attr = attrId
1099
- ? this.model.getAttributeById(attrId)
1100
- : this.model.getFirstUICAttr();
1101
- var expr = new Expression(column);
1102
- expr.tag = ExprTag.EntityAttribute;
1103
- expr.dataType = attr.dataType;
1104
- expr.setValue(attr.id, true);
1105
- column.expr = expr;
1106
- }
1107
- column.caption = this.context.getDefaultColumnCaption(column);
1108
- };
1109
- /**
1110
- * Moves the column with specified index to another position.
1111
- * @param index1 The index of the column that should be moved.
1112
- * @param index2 The index of the position the column should be moved to
1113
- */
1114
- Query.prototype.moveColumn = function (index1, index2) {
1115
- var columns = this.getColumns();
1116
- dataUtils.moveArrayItem(columns, index1, index2);
1117
- this.adjustColumnsSorting();
1118
- };
1119
- /**
1120
- * Removes the column.
1121
- * @param column The column to be removed.
1122
- */
1123
- Query.prototype.removeColumn = function (column) {
1124
- dataUtils.removeArrayItem(this.columns, column);
1125
- dataUtils.removeArrayItem(this.sortedColumns, column);
1126
- };
1127
- /**
1128
- * Adds a condition group - a group of simple conditions (predicates) linked by AND or OR
1129
- * @param descriptor The descriptor of the new condition group.
1130
- * @returns The new search condition.
1131
- */
1132
- Query.prototype.addConditionGroup = function (descriptor, addChildCondition) {
1133
- if (addChildCondition === void 0) { addChildCondition = true; }
1134
- console.error("Community version doesn't support working with groups.");
1135
- return null;
1136
- };
1137
- Query.prototype.addExtraConditionGroup = function (descriptor, addChildCondition) {
1138
- if (addChildCondition === void 0) { addChildCondition = true; }
1139
- console.error("Community version doesn't support working with groups.");
1140
- return null;
1141
- };
1142
- Query.prototype.createCondition = function (tag) {
1143
- if (tag === void 0) { tag = CondTag.Simple; }
1144
- return new Condition(this, tag);
1145
- };
1146
- /**
1147
- * Adds a new simple condition (a predicate)
1148
- * @param descriptor The descriptor which defines different parameters of the new condition
1149
- * @returns The new condition.
1150
- */
1151
- Query.prototype.addSimpleCondition = function (descriptor) {
1152
- var parent = descriptor.parent || this.getRootCondition();
1153
- var model = this.getModel();
1154
- if (!model)
1155
- return null;
1156
- if (!descriptor.attribute && !descriptor.attributeId) {
1157
- throw new Error("Either `attribute` or `attributeId` parameter must have a value");
1158
- }
1159
- if (!descriptor.attribute) {
1160
- descriptor.attribute = model.getAttributeById(descriptor.attributeId);
1161
- if (!descriptor.attribute) {
1162
- throw new Error("Could not find this attribute: " + descriptor.attributeId);
1163
- }
1164
- }
1165
- if (!descriptor.operator && descriptor.operatorId) {
1166
- descriptor.operator = model.getOperatorById(descriptor.operatorId);
1167
- }
1168
- if (!descriptor.operator) {
1169
- descriptor.operator = model.getDefaultOperatorForAttr(descriptor.attribute);
1170
- }
1171
- var cond = this.createSimpleConditionObject(descriptor.attribute, descriptor.operator, descriptor.value);
1172
- parent.addCondition(cond);
1173
- cond.enabled = dataUtils.getIfDefined(descriptor.enabled, true);
1174
- cond.setReadOnly(dataUtils.getIfDefined(descriptor.readonly, false));
1175
- return cond;
1176
- };
1177
- Query.prototype.addSimpleExtraCondition = function (descriptor) {
1178
- descriptor.parent = descriptor.parent || this.extraConditions;
1179
- return this.addSimpleCondition(descriptor);
1180
- };
1181
- Query.prototype.createSimpleConditionObject = function (attr, operator, inputValues) {
1182
- if (!attr || !attr.useInConditions) {
1183
- var attrCaption = attr ? attr.caption : 'NULL';
1184
- throw "Can't add a condition with such attribute: " + attrCaption;
1185
- }
1186
- var cond = this.createCondition(CondTag.Simple);
1187
- cond.setOperatorId(operator.id, true);
1188
- var attrExpr = new Expression(cond);
1189
- attrExpr.setValue(attr.id, true);
1190
- attrExpr.tag = this.attributeExprTag;
1191
- attrExpr.kind = DataKind.Attribute;
1192
- attrExpr.dataType = attr.dataType;
1193
- cond.expressions.push(attrExpr);
1194
- var model = this.getModel();
1195
- var valCount = operator.paramCount;
1196
- var values = dataUtils.fillArray(new Array(valCount - 1), null);
1197
- if (inputValues !== null && typeof inputValues !== "undefined") {
1198
- if (Array.isArray(inputValues)) {
1199
- dataUtils.copyArrayTo(inputValues, values);
1200
- }
1201
- else if (valCount > 0) {
1202
- values[0] = inputValues;
1203
- }
1204
- }
1205
- var valLength = values.length;
1206
- for (var i = 0; i < valLength; i++) {
1207
- var operand = model.getOperand(attr, operator, i + 1);
1208
- cond.addExpressionByOperand(operand, values[i]);
1209
- }
1210
- return cond;
1211
- };
1212
- /**
1213
- * Removes several columns passed as array.
1214
- * @param columnsToRemove The list of columns to be removed.
1215
- * @param author The author.
1216
- */
1217
- Query.prototype.removeColumns = function (columnsToRemove, author) {
1218
- var justSortedColumns = columnsToRemove.filter(function (c) { return c.isJustSorted(); });
1219
- var columns = columnsToRemove.filter(function (c) { return !c.isJustSorted(); });
1220
- function removeFromArray(arr, arrToRemove) {
1221
- var ctrLen = arrToRemove.length;
1222
- for (var i = 0; i < ctrLen; i++) {
1223
- var index = arr.indexOf(arrToRemove[i]);
1224
- if (index >= 0) {
1225
- arr.splice(index, 1);
1226
- }
1227
- }
1228
- }
1229
- if (columns.length > 0) {
1230
- removeFromArray(this.getColumns(), columns);
1231
- removeFromArray(this.sortedColumns, columns);
1232
- }
1233
- if (justSortedColumns.length > 0) {
1234
- //removeFromArray(this.getJustSortedColumns(), justSortedColumns);
1235
- removeFromArray(this.sortedColumns, justSortedColumns);
1236
- }
1237
- this.fixupSortingIndices();
1238
- };
1239
- /**
1240
- * Fires process event.
1241
- * @param data The data.
1242
- */
1243
- Query.prototype.fireProcessEvent = function (data) {
1244
- this.eventEmitter.fire('query.process', data);
1245
- };
1246
- Query.prototype.fireChangedEvent = function (params, postpone, wasModified) {
1247
- params = params || { part: QueryChangePart.All };
1248
- if (typeof (postpone) != 'undefined' || typeof (wasModified) != 'undefined') {
1249
- params.postpone = postpone || 0;
1250
- params.wasModified = wasModified;
1251
- console.warn('Method with \'postpone\' and \'wasModified\' args is deprecated. Pass params in data object');
1252
- }
1253
- this._isModified = typeof (params.wasModified) != 'undefined'
1254
- ? params.wasModified
1255
- : true;
1256
- var eventData = params || {
1257
- part: QueryChangePart.All
1258
- };
1259
- this.eventEmitter.fire('query.change', eventData, params.postpone || 0);
1260
- };
1261
- Query.prototype.fireColumnsChangedEvent = function (action, column, justsorted) {
1262
- if (justsorted === void 0) { justsorted = false; }
1263
- this.fireChangedEvent({
1264
- part: (justsorted)
1265
- ? QueryChangePart.SortingColumns
1266
- : QueryChangePart.Columns,
1267
- action: action,
1268
- changee: column
1269
- });
1270
- };
1271
- Query.prototype.fireConditionsChangedEvent = function (action, condition) {
1272
- this.fireChangedEvent({
1273
- part: QueryChangePart.Conditions,
1274
- action: action,
1275
- changee: condition
1276
- });
1277
- };
1278
- /**
1279
- * Runs through condition with this callback.
1280
- * @param processCondition The function to process condition.
1281
- */
1282
- Query.prototype.runThroughConditions = function (processCondition) {
1283
- var processGroup = function (predicate) {
1284
- for (var i = 0; i < predicate.getConditions().length; i++) {
1285
- var cond = predicate.getConditions()[i];
1286
- if (cond.isGroup()) {
1287
- processGroup(cond);
1288
- }
1289
- else {
1290
- if (processCondition)
1291
- processCondition(cond);
1292
- }
1293
- }
1294
- };
1295
- processGroup(this.getRootCondition());
1296
- };
1297
- /**
1298
- * Gets one value for the attribute
1299
- * @param attrId The attribute ID.
1300
- * @retruns The value.
1301
- */
1302
- Query.prototype.getOneValueForAttr = function (attrId) {
1303
- var res = null;
1304
- this.runThroughConditions(function (cond) {
1305
- if (cond.enabled) {
1306
- var expr = cond.expressions[0];
1307
- var opid = cond.getOperatorId();
1308
- if (expr.value == attrId && cond.expressions[1] &&
1309
- (opid == "Equal" || opid == "InList" || opid == "StartsWith")) {
1310
- res = cond.expressions[1].value;
1311
- }
1312
- }
1313
- });
1314
- return res;
1315
- };
1316
- /**
1317
- * Gets root predicate
1318
- * @retruns The condition.
1319
- */
1320
- Query.prototype.getRootCondition = function () {
1321
- return this.root;
1322
- };
1323
- /**
1324
- * Add process callback
1325
- * @param callback The callback.
1326
- * @retruns The callback ID.
1327
- */
1328
- Query.prototype.addProcessCallback = function (callback) {
1329
- return this.eventEmitter.subscribe('query.process', callback);
1330
- };
1331
- /**
1332
- * Remove process callback
1333
- * @param callbackId The callback ID.
1334
- */
1335
- Query.prototype.removeProcessCallback = function (callbackId) {
1336
- this.eventEmitter.unsubscribe('query.process', callbackId);
1337
- };
1338
- /**
1339
- * Add changed callback
1340
- * @param callback The callback ID.
1341
- */
1342
- Query.prototype.addChangedCallback = function (callback) {
1343
- return this.eventEmitter.subscribe('query.change', callback);
1344
- };
1345
- /**
1346
- * Remove changed callback
1347
- * @param callbackId The callback ID.
1348
- */
1349
- Query.prototype.removeChangedCallback = function (callbackId) {
1350
- this.eventEmitter.unsubscribe('query.change', callbackId);
1351
- };
1352
- /**
1353
- * Retruns conditions text
1354
- */
1355
- Query.prototype.getConditionsText = function () {
1356
- var _this = this;
1357
- var model = this.getModel();
1358
- if (!model)
1359
- return '';
1360
- var getConditionGroupText = function (group) {
1361
- var result = '', conjKey = "Conj" + equtils.linkTypeToStr(group.linkType), conjText = i18n.getText(conjKey), condText, i;
1362
- for (i = 0; i < group.getConditions().length; i++) {
1363
- condText = "";
1364
- var cond = group.getConditions()[i];
1365
- if (cond.enabled || typeof (cond.enabled) === "undefined") {
1366
- if (cond.isGroup()) {
1367
- condText = getConditionGroupText(cond);
1368
- if (condText) {
1369
- condText = "(" + condText + ")";
1370
- }
1371
- }
1372
- else {
1373
- var opId = cond.getOperatorId();
1374
- var operator = model.getOperatorById(opId);
1375
- var parts = equtils.parseOperatorFormat(operator);
1376
- var plen = parts.length;
1377
- for (var pi = 0; pi < plen; pi++) {
1378
- var part = parts[pi];
1379
- var txt = part.text;
1380
- if (part.type == "expression") {
1381
- var expr = cond.expressions[part.index];
1382
- if (expr.kind == DataKind.Attribute || expr.tag == ExprTag.EntityAttribute) {
1383
- var attr = model.getAttributeById(expr.value);
1384
- if (attr != null) {
1385
- txt = model.getAttributeText(attr, "{entity} {attr}");
1386
- }
1387
- }
1388
- else {
1389
- txt = expr.getText();
1390
- if (_this.model.isDateMacro(txt) || _this.model.isTimeMacro(txt)) {
1391
- var macroText = txt.substring(3, txt.length - 2);
1392
- var res = i18n.getText(macroText);
1393
- txt = res || txt;
1394
- }
1395
- }
1396
- }
1397
- if (pi > 0)
1398
- condText += " ";
1399
- condText += txt;
1400
- }
1401
- }
1402
- }
1403
- if (condText) {
1404
- if (i > 0 && result) {
1405
- result += " " + conjText + " ";
1406
- }
1407
- result += condText;
1408
- }
1409
- }
1410
- if (group.linkType == LinkType.None || group.linkType == LinkType.NotAll) {
1411
- result = "not ( " + result + " )";
1412
- }
1413
- return result;
1414
- };
1415
- return getConditionGroupText(this.getRootCondition());
1416
- };
1417
- /**
1418
- * Retruns conditions text
1419
- */
1420
- Query.prototype.getConditionsTextAsHtml = function () {
1421
- var _this = this;
1422
- var model = this.getModel();
1423
- if (!model)
1424
- return '<div class="eqjs-query-text"></div>';
1425
- var quoteExprText = function (expr) {
1426
- if (expr.kind == DataKind.Query) {
1427
- return "&lt;".concat(expr.getText(), "&gt;");
1428
- }
1429
- else if (expr.kind == DataKind.List) {
1430
- return "[".concat(expr.getText(), "]");
1431
- }
1432
- else if (expr.dataType == DataType.String
1433
- || expr.dataType == DataType.Memo
1434
- || expr.dataType == DataType.FixedChar) {
1435
- return "&quot;".concat(expr.getText(), "&quot;");
1436
- }
1437
- else {
1438
- var txt = expr.getText();
1439
- if (_this.model.isDateMacro(txt) || _this.model.isTimeMacro(txt)) {
1440
- var macroText = txt.substring(3, txt.length - 2);
1441
- var res = i18n.getText(macroText);
1442
- txt = res || txt;
1443
- }
1444
- return txt;
1445
- }
1446
- };
1447
- var getConditionGroupText = function (group) {
1448
- var result = '', conjKey = "Conj" + equtils.linkTypeToStr(group.linkType), conjText = "<span class=\"eqjs-query-text-conj\">".concat(i18n.getText(conjKey), "</span>"), condText, i;
1449
- for (i = 0; i < group.getConditions().length; i++) {
1450
- condText = "";
1451
- var cond = group.getConditions()[i];
1452
- if (cond.enabled || typeof (cond.enabled) === "undefined") {
1453
- if (cond.isGroup()) {
1454
- condText = getConditionGroupText(cond);
1455
- if (condText) {
1456
- condText = "(" + condText + ")";
1457
- }
1458
- }
1459
- else {
1460
- var opId = cond.getOperatorId();
1461
- var operator = model.getOperatorById(opId);
1462
- var parts = equtils.parseOperatorFormat(operator);
1463
- var plen = parts.length;
1464
- for (var pi = 0; pi < plen; pi++) {
1465
- var part = parts[pi];
1466
- var txt = part.text;
1467
- if (part.type == "expression") {
1468
- var expr = cond.expressions[part.index];
1469
- if (expr.kind == DataKind.Attribute || expr.tag == ExprTag.EntityAttribute) {
1470
- var attr = model.getAttributeById(expr.value);
1471
- txt = "<span class=\"eqjs-query-text-attr\">".concat(model.getAttributeText(attr, "{entity} {attr}"), "</span>");
1472
- }
1473
- else {
1474
- txt = "<span class=\"eqjs-query-text-expr\">".concat(quoteExprText(expr), "</span>");
1475
- }
1476
- }
1477
- else if (part.type == "operator") {
1478
- txt = "<span class=\"eqjs-query-text-op\">".concat(txt, "</span>");
1479
- }
1480
- if (pi > 0)
1481
- condText += " ";
1482
- condText += txt;
1483
- }
1484
- }
1485
- }
1486
- if (condText) {
1487
- if (i > 0 && result) {
1488
- result += " " + conjText + " ";
1489
- }
1490
- result += condText;
1491
- }
1492
- }
1493
- if (group.linkType == LinkType.None || group.linkType == LinkType.NotAll) {
1494
- result = "<span class=\"eqjs-query-text-conj\">not</span> ( " + result + " )";
1495
- }
1496
- return result;
1497
- };
1498
- return "<div class=\"eqjs-query-text\">".concat(getConditionGroupText(this.getRootCondition()), "</div>");
1499
- };
1500
- return Query;
1501
- }());
1502
- export { Query };
1503
- //# sourceMappingURL=query.js.map