UNPKG

molstar

Version:

A comprehensive macromolecular library.

148 lines 8.48 kB
/** * Copyright (c) 2019-2020 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> */ import { __awaiter, __generator } from "tslib"; import { StateAction } from '../../mol-state'; import { Task } from '../../mol-task'; import { getFileInfo } from '../../mol-util/file-info'; import { ParamDefinition as PD } from '../../mol-util/param-definition'; import { PluginStateObject } from '../objects'; export var OpenFiles = StateAction.build({ display: { name: 'Open Files', description: 'Load one or more files and optionally create default visuals' }, from: PluginStateObject.Root, params: function (a, ctx) { var _a = ctx.dataFormats, extensions = _a.extensions, options = _a.options; return { files: PD.FileList({ accept: Array.from(extensions.values()).map(function (e) { return "." + e; }).join(',') + ',.gz,.zip', multiple: true }), format: PD.MappedStatic('auto', { auto: PD.EmptyGroup(), specific: PD.Select(options[0][0], options) }), visuals: PD.Boolean(true, { description: 'Add default visuals' }), }; } })(function (_a, plugin) { var params = _a.params, state = _a.state; return Task.create('Open Files', function (taskCtx) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: plugin.behaviors.layout.leftPanelTabName.next('data'); return [4 /*yield*/, state.transaction(function () { return __awaiter(void 0, void 0, void 0, function () { var _i, _a, file, info, isBinary, data, provider, parsed, e_1; var _b, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: if (params.files === null) { plugin.log.error('No file(s) selected'); return [2 /*return*/]; } _i = 0, _a = params.files; _d.label = 1; case 1: if (!(_i < _a.length)) return [3 /*break*/, 9]; file = _a[_i]; _d.label = 2; case 2: _d.trys.push([2, 7, , 8]); info = getFileInfo(file.file); isBinary = plugin.dataFormats.binaryExtensions.has(info.ext); return [4 /*yield*/, plugin.builders.data.readFile({ file: file, isBinary: isBinary })]; case 3: data = (_d.sent()).data; provider = params.format.name === 'auto' ? plugin.dataFormats.auto(info, (_b = data.cell) === null || _b === void 0 ? void 0 : _b.obj) : plugin.dataFormats.get(params.format.params); if (!provider) { plugin.log.warn("OpenFiles: could not find data provider for '" + info.name + "." + info.ext + "'"); return [3 /*break*/, 8]; } return [4 /*yield*/, provider.parse(plugin, data)]; case 4: parsed = _d.sent(); if (!params.visuals) return [3 /*break*/, 6]; return [4 /*yield*/, ((_c = provider.visuals) === null || _c === void 0 ? void 0 : _c.call(provider, plugin, parsed))]; case 5: _d.sent(); _d.label = 6; case 6: return [3 /*break*/, 8]; case 7: e_1 = _d.sent(); plugin.log.error(e_1); return [3 /*break*/, 8]; case 8: _i++; return [3 /*break*/, 1]; case 9: return [2 /*return*/]; } }); }); }).runInContext(taskCtx)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }); }); export var DownloadFile = StateAction.build({ display: { name: 'Download File', description: 'Load one or more file from an URL' }, from: PluginStateObject.Root, params: function (a, ctx) { var options = ctx.dataFormats.options; return { url: PD.Url(''), format: PD.Select(options[0][0], options), isBinary: PD.Boolean(false), visuals: PD.Boolean(true, { description: 'Add default visuals' }), }; } })(function (_a, plugin) { var params = _a.params, state = _a.state; return Task.create('Open Files', function (taskCtx) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: plugin.behaviors.layout.leftPanelTabName.next('data'); return [4 /*yield*/, state.transaction(function () { return __awaiter(void 0, void 0, void 0, function () { var provider, data, parsed, e_2; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, 5, , 6]); provider = plugin.dataFormats.get(params.format); if (!provider) { plugin.log.warn("DownloadFile: could not find data provider for '" + params.format + "'"); return [2 /*return*/]; } return [4 /*yield*/, plugin.builders.data.download({ url: params.url, isBinary: params.isBinary })]; case 1: data = _b.sent(); return [4 /*yield*/, provider.parse(plugin, data)]; case 2: parsed = _b.sent(); if (!params.visuals) return [3 /*break*/, 4]; return [4 /*yield*/, ((_a = provider.visuals) === null || _a === void 0 ? void 0 : _a.call(provider, plugin, parsed))]; case 3: _b.sent(); _b.label = 4; case 4: return [3 /*break*/, 6]; case 5: e_2 = _b.sent(); plugin.log.error(e_2); return [3 /*break*/, 6]; case 6: return [2 /*return*/]; } }); }); }).runInContext(taskCtx)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }); }); //# sourceMappingURL=file.js.map