@gooddata/gooddata-js
Version:
GoodData JavaScript SDK
76 lines (75 loc) • 2.92 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// (C) 2007-2018 GoodData Corporation
var get_1 = __importDefault(require("lodash/get"));
var isEmpty_1 = __importDefault(require("lodash/isEmpty"));
var isEqual_1 = __importDefault(require("lodash/isEqual"));
var AfmUtils_1 = require("./utils/AfmUtils");
var async_1 = require("./utils/async");
var DataTable = /** @class */ (function () {
function DataTable(adapter) {
var _this = this;
this.dataSubscribers = [];
this.errorSubscribers = [];
this.adapter = adapter;
this.subject = async_1.createSubject(function (result) { return _this.dataSubscribers.forEach(function (handler) { return handler(result); }); }, function (error) { return _this.errorSubscribers.forEach(function (handler) { return handler(error); }); });
}
DataTable.getDefaultDimensionsForTable = function (afm) {
return [
{
itemIdentifiers: (afm.attributes || []).map(function (a) { return a.localIdentifier; }),
},
{
itemIdentifiers: ["measureGroup"],
},
];
};
DataTable.prototype.getData = function (afm, resultSpec) {
var _this = this;
if (resultSpec === void 0) { resultSpec = {}; }
if (!AfmUtils_1.isAfmExecutable(afm)) {
return;
}
if (isEmpty_1.default(get_1.default(resultSpec, "dimensions"))) {
resultSpec.dimensions = DataTable.getDefaultDimensionsForTable(afm);
}
if (!isEqual_1.default(afm, this.afm)) {
this.afm = afm;
this.adapter.createDataSource(afm).then(function (dataSource) {
_this.dataSource = dataSource;
_this.fetchData(resultSpec);
}, function (error) {
_this.errorSubscribers.forEach(function (handler) { return handler(error); });
});
}
else if (this.dataSource) {
this.fetchData(resultSpec);
}
};
DataTable.prototype.onData = function (callback) {
this.dataSubscribers.push(callback);
return this;
};
DataTable.prototype.onError = function (callback) {
this.errorSubscribers.push(callback);
return this;
};
DataTable.prototype.resetDataSubscribers = function () {
this.dataSubscribers = [];
return this;
};
DataTable.prototype.resetErrorSubscribers = function () {
this.errorSubscribers = [];
return this;
};
DataTable.prototype.fetchData = function (resultSpec) {
if (this.dataSource) {
this.subject.next(this.dataSource.getData(resultSpec));
}
};
return DataTable;
}());
exports.DataTable = DataTable;