opendash
Version:
open.DASH data visualization framework
81 lines • 3.52 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { BaseService, SourceAdapterContext, } from "..";
var SourceService = /** @class */ (function (_super) {
__extends(SourceService, _super);
function SourceService(app, adapter) {
var _this = _super.call(this) || this;
_this.app = app;
_this.adapter = adapter;
_this.context = new SourceAdapterContext(_this, _this.app);
_this.initAdapter(adapter, _this.context, app.services);
return _this;
}
SourceService.prototype.getCurrent = function () {
return this.app.state.select(function (state) { return state.sources.current; });
};
SourceService.prototype.getChildren = function (source) {
return this.app.state.select(function (state) {
return state.sources.all.filter(function (s) { return s.parent === source.id; });
});
};
SourceService.prototype.getDescendents = function (rootSource) {
var descendentsIds = this._getDescendentsIds(rootSource);
return this.getAll().filter(function (source) { return descendentsIds.includes(source.id); });
};
SourceService.prototype._getDescendentsIds = function (rootSource) {
var all = this.getAll()
.filter(function (source) { return source.parent && source.id !== rootSource.id; })
.map(function (source) { return [source.id, source.parent]; });
var descendents = [];
var added = [rootSource.id];
var lastAdded = [];
while (true) {
lastAdded = added;
added = [];
for (var _i = 0, lastAdded_1 = lastAdded; _i < lastAdded_1.length; _i++) {
var addedSourceId = lastAdded_1[_i];
for (var _a = 0, all_1 = all; _a < all_1.length; _a++) {
var _b = all_1[_a], id = _b[0], parent_1 = _b[1];
if (parent_1 === addedSourceId) {
added.push(id);
}
}
}
if (added.length > 0) {
descendents.push.apply(descendents, added);
all = all.filter(function (_a) {
var id = _a[0];
return !added.includes(id);
});
}
else {
break;
}
}
return descendents;
};
SourceService.prototype.getAll = function () {
return this.app.state.select(function (state) { return state.sources.all; });
};
SourceService.prototype.setCurrent = function (id) {
this.app.state.update(function (draft) {
draft.sources.current = draft.sources.all.find(function (source) { return source.id === id; });
});
this.notifySubscribers();
};
return SourceService;
}(BaseService));
export { SourceService };
//# sourceMappingURL=SourceService.js.map