UNPKG

7.37 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var wolfy87_eventemitter_1 = tslib_1.__importDefault(require("wolfy87-eventemitter"));
5var util_1 = require("@antv/util");
6function cloneOptions(options) {
7 var result = {};
8 util_1.forIn(options, function (value, key) {
9 if (util_1.isObject(value) && value.isView) {
10 result[key] = value;
11 }
12 else if (util_1.isArray(value)) {
13 result[key] = value.concat([]);
14 }
15 else if (util_1.isPlainObject(value)) {
16 result[key] = util_1.clone(value);
17 }
18 else {
19 result[key] = value;
20 }
21 });
22 return result;
23}
24/**
25 * 数据视图
26 * @public
27 */
28var View = /** @class */ (function (_super) {
29 tslib_1.__extends(View, _super);
30 function View(dataSet, options) {
31 var _this = _super.call(this) || this;
32 /**
33 * 是否是View
34 */
35 _this.isView = true;
36 /**
37 * 是否是View
38 */
39 _this.isDataView = true; // alias
40 /**
41 *
42 */
43 _this.watchingStates = null;
44 /**
45 * 数据视图类型
46 */
47 _this.dataType = 'table';
48 /**
49 * 已应用的 transform
50 */
51 _this.transforms = [];
52 /**
53 * 原始数据
54 */
55 _this.origin = [];
56 /**
57 * 存储处理后的数据
58 */
59 _this.rows = [];
60 if (dataSet && dataSet.isDataSet) {
61 _this.dataSet = dataSet;
62 }
63 else {
64 _this.dataSet = null;
65 options = dataSet;
66 }
67 _this.loose = !_this.dataSet;
68 // TODO:
69 // assign(me, options);
70 if (options) {
71 _this.watchingStates = options.watchingStates;
72 }
73 if (!_this.loose) {
74 var watchingStates_1 = _this.watchingStates;
75 dataSet.on('statechange', function (name) {
76 if (util_1.isArray(watchingStates_1)) {
77 if (watchingStates_1.indexOf(name) > -1) {
78 _this._reExecute();
79 }
80 }
81 else {
82 _this._reExecute();
83 }
84 });
85 }
86 return _this;
87 }
88 View.prototype._parseStateExpression = function (expr) {
89 var dataSet = this.dataSet;
90 if (dataSet === null)
91 return undefined;
92 var matched = /^\$state\.(\w+)/.exec(expr);
93 if (matched) {
94 return dataSet.state[matched[1]];
95 }
96 return expr;
97 };
98 View.prototype._preparseOptions = function (options) {
99 var _this = this;
100 var optionsCloned = cloneOptions(options);
101 if (this.loose) {
102 return optionsCloned;
103 }
104 util_1.forIn(optionsCloned, function (value, key) {
105 if (util_1.isString(value) && /^\$state\./.test(value)) {
106 optionsCloned[key] = _this._parseStateExpression(value);
107 }
108 });
109 return optionsCloned;
110 };
111 // connectors
112 View.prototype._prepareSource = function (source, options) {
113 // warning me.origin is protected
114 this._source = { source: source, options: options };
115 if (!options) {
116 if (source instanceof View || util_1.isString(source)) {
117 this.origin = View.DataSet.getConnector('default')(source, this.dataSet);
118 }
119 else if (util_1.isArray(source)) {
120 // TODO branch: if source is like ['dataview1', 'dataview2']
121 this.origin = source;
122 }
123 else if (util_1.isObject(source) && source.type) {
124 var opts = this._preparseOptions(source); // connector without source
125 this.origin = View.DataSet.getConnector(opts.type)(opts, this);
126 }
127 else {
128 throw new TypeError('Invalid source');
129 }
130 }
131 else {
132 var opts = this._preparseOptions(options);
133 this.origin = View.DataSet.getConnector(opts.type)(source, opts, this);
134 }
135 this.rows = util_1.deepMix([], this.origin);
136 return this;
137 };
138 View.prototype.source = function (source, options) {
139 this._prepareSource(source, options)._reExecuteTransforms();
140 this.trigger('change', []);
141 return this;
142 };
143 /**
144 * 执行数据处理数据。执行完这个函数后,transform 会被存储
145 * @param options - 某种类型的transform
146 */
147 View.prototype.transform = function (options) {
148 if (options && options.type) {
149 this.transforms.push(options);
150 this._executeTransform(options);
151 }
152 return this;
153 };
154 View.prototype._executeTransform = function (options) {
155 options = this._preparseOptions(options);
156 var transform = View.DataSet.getTransform(options.type);
157 transform(this, options);
158 };
159 View.prototype._reExecuteTransforms = function () {
160 var _this = this;
161 this.transforms.forEach(function (options) {
162 _this._executeTransform(options);
163 });
164 };
165 View.prototype.addRow = function (row) {
166 this.rows.push(row);
167 };
168 View.prototype.removeRow = function (index) {
169 this.rows.splice(index, 1);
170 };
171 View.prototype.updateRow = function (index, newRow) {
172 util_1.assign(this.rows[index], newRow);
173 };
174 View.prototype.findRows = function (query) {
175 return this.rows.filter(function (row) { return util_1.isMatch(row, query); });
176 };
177 View.prototype.findRow = function (query) {
178 return util_1.find(this.rows, query);
179 };
180 // columns
181 View.prototype.getColumnNames = function () {
182 var firstRow = this.rows[0];
183 if (firstRow) {
184 return util_1.keys(firstRow);
185 }
186 return [];
187 };
188 View.prototype.getColumnName = function (index) {
189 return this.getColumnNames()[index];
190 };
191 View.prototype.getColumnIndex = function (columnName) {
192 var columnNames = this.getColumnNames();
193 return columnNames.indexOf(columnName);
194 };
195 View.prototype.getColumn = function (columnName) {
196 return this.rows.map(function (row) { return row[columnName]; });
197 };
198 View.prototype.getColumnData = function (columnName) {
199 return this.getColumn(columnName);
200 };
201 // data process
202 View.prototype.getSubset = function (startRowIndex, endRowIndex, columnNames) {
203 var subset = [];
204 for (var i = startRowIndex; i <= endRowIndex; i++) {
205 subset.push(util_1.pick(this.rows[i], columnNames));
206 }
207 return subset;
208 };
209 View.prototype.toString = function (prettyPrint) {
210 if (prettyPrint === void 0) { prettyPrint = false; }
211 if (prettyPrint) {
212 return JSON.stringify(this.rows, null, 2);
213 }
214 return JSON.stringify(this.rows);
215 };
216 View.prototype._reExecute = function () {
217 var _a = this._source, source = _a.source, options = _a.options;
218 this._prepareSource(source, options);
219 this._reExecuteTransforms();
220 this.trigger('change', []);
221 };
222 return View;
223}(wolfy87_eventemitter_1.default));
224exports.View = View;