molstar
Version:
A comprehensive macromolecular library.
179 lines • 7.57 kB
JavaScript
/**
* 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.PluginBehavior = void 0;
var tslib_1 = require("tslib");
var objects_1 = require("../../mol-plugin-state/objects");
var mol_state_1 = require("../../mol-state");
var mol_task_1 = require("../../mol-task");
var param_definition_1 = require("../../mol-util/param-definition");
var mol_util_1 = require("../../mol-util");
var PluginBehavior;
(function (PluginBehavior) {
var Root = /** @class */ (function (_super) {
(0, tslib_1.__extends)(Root, _super);
function Root() {
return _super !== null && _super.apply(this, arguments) || this;
}
return Root;
}(objects_1.PluginStateObject.Create({ name: 'Root', typeClass: 'Root' })));
PluginBehavior.Root = Root;
var Category = /** @class */ (function (_super) {
(0, tslib_1.__extends)(Category, _super);
function Category() {
return _super !== null && _super.apply(this, arguments) || this;
}
return Category;
}(objects_1.PluginStateObject.Create({ name: 'Category', typeClass: 'Object' })));
PluginBehavior.Category = Category;
var Behavior = /** @class */ (function (_super) {
(0, tslib_1.__extends)(Behavior, _super);
function Behavior() {
return _super !== null && _super.apply(this, arguments) || this;
}
return Behavior;
}(objects_1.PluginStateObject.CreateBehavior({ name: 'Behavior' })));
PluginBehavior.Behavior = Behavior;
PluginBehavior.Categories = {
'common': 'Common',
'representation': 'Representation',
'interaction': 'Interaction',
'custom-props': 'Custom Properties',
'misc': 'Miscellaneous'
};
PluginBehavior.CreateCategory = objects_1.PluginStateTransform.BuiltIn({
name: 'create-behavior-category',
display: { name: 'Behavior Category' },
from: Root,
to: Category,
params: {
label: param_definition_1.ParamDefinition.Text('', { isHidden: true }),
}
})({
apply: function (_a) {
var params = _a.params;
return new Category({}, { label: params.label });
}
});
var categoryMap = new Map();
function getCategoryId(t) {
return categoryMap.get(t.id);
}
PluginBehavior.getCategoryId = getCategoryId;
function create(params) {
var t = objects_1.PluginStateTransform.CreateBuiltIn({
name: params.name,
display: params.display,
from: [Root],
to: [Behavior],
params: params.params,
apply: function (_a, ctx) {
var p = _a.params;
var label = params.label ? params.label(p) : { label: params.display.name, description: params.display.description };
return new Behavior(new params.ctor(ctx, p), label);
},
update: function (_a) {
var _this = this;
var b = _a.b, newParams = _a.newParams;
return mol_task_1.Task.create('Update Behavior', function () { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
var updated;
return (0, tslib_1.__generator)(this, function (_a) {
switch (_a.label) {
case 0:
if (!b.data.update)
return [2 /*return*/, mol_state_1.StateTransformer.UpdateResult.Unchanged];
return [4 /*yield*/, b.data.update(newParams)];
case 1:
updated = _a.sent();
return [2 /*return*/, updated ? mol_state_1.StateTransformer.UpdateResult.Updated : mol_state_1.StateTransformer.UpdateResult.Unchanged];
}
});
}); });
},
canAutoUpdate: params.canAutoUpdate
});
categoryMap.set(t.id, params.category);
return t;
}
PluginBehavior.create = create;
function simpleCommandHandler(cmd, action) {
return /** @class */ (function () {
// TODO can't be private due to bug with generating declerations, see https://github.com/Microsoft/TypeScript/issues/17293
function class_1(/** private */ ctx) {
this.ctx = ctx;
// TODO can't be private due to bug with generating declerations, see https://github.com/Microsoft/TypeScript/issues/17293
/** private */ this.sub = void 0;
}
class_1.prototype.register = function () {
var _this = this;
this.sub = cmd.subscribe(this.ctx, function (data) { return action(data, _this.ctx); });
};
class_1.prototype.unregister = function () {
if (this.sub)
this.sub.unsubscribe();
this.sub = void 0;
};
return class_1;
}());
}
PluginBehavior.simpleCommandHandler = simpleCommandHandler;
var Handler = /** @class */ (function () {
function Handler(ctx, params) {
this.ctx = ctx;
this.params = params;
this.subs = [];
}
Handler.prototype.subscribeCommand = function (cmd, action) {
this.subs.push(cmd.subscribe(this.ctx, action));
};
Handler.prototype.subscribeObservable = function (o, action) {
this.subs.push(o.subscribe(action));
};
Handler.prototype.track = function (sub) {
this.subs.push(sub);
};
Handler.prototype.unregister = function () {
for (var _i = 0, _a = this.subs; _i < _a.length; _i++) {
var s = _a[_i];
s.unsubscribe();
}
this.subs = [];
};
Handler.prototype.update = function (params) {
if ((0, mol_util_1.shallowEqualObjects)(params, this.params))
return false;
this.params = params;
return true;
};
return Handler;
}());
PluginBehavior.Handler = Handler;
var WithSubscribers = /** @class */ (function () {
function WithSubscribers(plugin, params) {
this.plugin = plugin;
this.params = params;
this.subs = [];
}
WithSubscribers.prototype.subscribeCommand = function (cmd, action) {
this.subs.push(cmd.subscribe(this.plugin, action));
};
WithSubscribers.prototype.subscribeObservable = function (o, action) {
this.subs.push(o.subscribe(action));
};
WithSubscribers.prototype.unregister = function () {
for (var _i = 0, _a = this.subs; _i < _a.length; _i++) {
var s = _a[_i];
s.unsubscribe();
}
this.subs = [];
};
return WithSubscribers;
}());
PluginBehavior.WithSubscribers = WithSubscribers;
})(PluginBehavior || (PluginBehavior = {}));
exports.PluginBehavior = PluginBehavior;
//# sourceMappingURL=behavior.js.map
;