UNPKG

molstar

Version:

A comprehensive macromolecular library.

189 lines 7.02 kB
"use strict"; /** * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal <david.sehnal@gmail.com> */ Object.defineProperty(exports, "__esModule", { value: true }); exports.StateObjectRef = exports.StateObjectSelector = exports.StateObjectTracker = exports.StateObjectCell = exports.StateObject = void 0; var mol_util_1 = require("../mol-util"); var mol_state_1 = require("../mol-state"); var StateObject; (function (StateObject) { function factory() { return function (type) { return create(type); }; } StateObject.factory = factory; function create(type) { var _a; return _a = /** @class */ (function () { function O(data, props) { this.data = data; this.id = mol_util_1.UUID.create22(); this.type = type; this.label = props && props.label || type.name; this.description = props && props.description; } O.is = function (obj) { return !!obj && type === obj.type; }; return O; }()), _a.type = type, _a; } StateObject.create = create; function hasTag(o, t) { if (!o.tags) return false; for (var _i = 0, _a = o.tags; _i < _a.length; _i++) { var s = _a[_i]; if (s === t) return true; } return false; } StateObject.hasTag = hasTag; /** A special object indicating a transformer result has no value. */ StateObject.Null = { id: mol_util_1.UUID.create22(), type: { name: 'Null', typeClass: 'Null' }, data: void 0, label: 'Null' }; })(StateObject || (StateObject = {})); exports.StateObject = StateObject; var StateObjectCell; (function (StateObjectCell) { function is(o) { var c = o; return !!c && !!c.transform && !!c.parent && !!c.status; } StateObjectCell.is = is; function resolve(state, refOrCellOrSelector) { var ref = typeof refOrCellOrSelector === 'string' ? refOrCellOrSelector : StateObjectCell.is(refOrCellOrSelector) ? refOrCellOrSelector.transform.ref : refOrCellOrSelector.ref; return state.cells.get(ref); } StateObjectCell.resolve = resolve; })(StateObjectCell || (StateObjectCell = {})); exports.StateObjectCell = StateObjectCell; // TODO: improve the API? var StateObjectTracker = /** @class */ (function () { function StateObjectTracker(state) { this.state = state; this.version = ''; } StateObjectTracker.prototype.setQuery = function (sel) { this.query = mol_state_1.StateSelection.compile(sel); }; StateObjectTracker.prototype.update = function () { var cell = this.state.select(this.query)[0]; var version = cell ? cell.transform.version : void 0; var changed = this.cell !== cell || this.version !== version; this.cell = cell; this.version = version || ''; this.data = cell && cell.obj ? cell.obj.data : void 0; return changed; }; return StateObjectTracker; }()); exports.StateObjectTracker = StateObjectTracker; var StateObjectSelector = /** @class */ (function () { function StateObjectSelector(ref, state) { this.ref = ref; this.state = state; } Object.defineProperty(StateObjectSelector.prototype, "cell", { get: function () { var _a; return (_a = this.state) === null || _a === void 0 ? void 0 : _a.cells.get(this.ref); }, enumerable: false, configurable: true }); Object.defineProperty(StateObjectSelector.prototype, "obj", { get: function () { var _a, _b; return (_b = (_a = this.state) === null || _a === void 0 ? void 0 : _a.cells.get(this.ref)) === null || _b === void 0 ? void 0 : _b.obj; }, enumerable: false, configurable: true }); Object.defineProperty(StateObjectSelector.prototype, "data", { get: function () { var _a; return (_a = this.obj) === null || _a === void 0 ? void 0 : _a.data; }, enumerable: false, configurable: true }); StateObjectSelector.prototype.update = function (params, builder) { if (!this.state) throw new Error("To use update() from StateObjectSelector, 'state' must be defined."); if (!builder) builder = this.state.build(); (builder || this.state.build()).to(this).update(params); return builder; }; /** Checks if the object exists. If not throw an error. */ StateObjectSelector.prototype.checkValid = function () { if (!this.state) { throw new Error('Unassigned State.'); } var cell = this.cell; if (!cell) { throw new Error("Not created at all. Did you await/then the corresponding state update?"); } if (cell.status === 'ok') return true; if (cell.status === 'error') throw new Error(cell.errorText); if (cell.obj === StateObject.Null) throw new Error('The object is Null.'); throw new Error("Unresolved. Did you await/then the corresponding state update?"); }; Object.defineProperty(StateObjectSelector.prototype, "isOk", { get: function () { var cell = this.cell; return cell && cell.status === 'ok' && cell.obj !== StateObject.Null; }, enumerable: false, configurable: true }); return StateObjectSelector; }()); exports.StateObjectSelector = StateObjectSelector; var StateObjectRef; (function (StateObjectRef) { function resolveRef(ref) { var _a; if (!ref) return; if (typeof ref === 'string') return ref; if (StateObjectCell.is(ref)) return ref.transform.ref; return (_a = ref.cell) === null || _a === void 0 ? void 0 : _a.transform.ref; } StateObjectRef.resolveRef = resolveRef; function resolve(state, ref) { if (!ref) return; if (StateObjectCell.is(ref)) return ref; if (typeof ref === 'string') return state.cells.get(ref); return ref.cell; } StateObjectRef.resolve = resolve; function resolveAndCheck(state, ref) { var cell = resolve(state, ref); if (!cell || !cell.obj || cell.status !== 'ok') return; return cell; } StateObjectRef.resolveAndCheck = resolveAndCheck; })(StateObjectRef = exports.StateObjectRef || (exports.StateObjectRef = {})); //# sourceMappingURL=object.js.map