molstar
Version:
A comprehensive macromolecular library.
185 lines • 6.66 kB
JavaScript
/**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { UUID } from '../mol-util';
import { StateSelection } from '../mol-state';
export { StateObject, StateObjectCell };
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 = 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: UUID.create22(),
type: { name: 'Null', typeClass: 'Null' },
data: void 0,
label: 'Null'
};
})(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 = {}));
// TODO: improve the API?
var StateObjectTracker = /** @class */ (function () {
function StateObjectTracker(state) {
this.state = state;
this.version = '';
}
StateObjectTracker.prototype.setQuery = function (sel) {
this.query = 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;
}());
export { 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;
}());
export { StateObjectSelector };
export 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 || (StateObjectRef = {}));
//# sourceMappingURL=object.js.map