{"size": 4173, "ext": "js", "max_stars_repo_path": "src/visualizers/widgets/InteractiveEditor/InteractiveEditorWidget.js", "max_stars_repo_name": "KyleAMoore/deepforge", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/visualizers/widgets/InteractiveEditor/InteractiveEditorWidget.js", "max_issues_repo_name": "KyleAMoore/deepforge", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/visualizers/widgets/InteractiveEditor/InteractiveEditorWidget.js", "max_forks_repo_name": "KyleAMoore/deepforge", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 33.9268292683, "max_line_length": 104, "alphanum_fraction": 0.5005990894}
/* globals define, $ */
define([
    'deepforge/compute/interactive/session-with-queue',
    'deepforge/viz/ConfigDialog',
    'deepforge/viz/InformDialog',
    'deepforge/compute/index',
    'deepforge/globals',
    'css!./styles/InteractiveEditorWidget.css',
], function(
    Session,
    ConfigDialog,
    InformDialog,
    Compute,
    DeepForge,
) {
    const COMPUTE_MESSAGE = 'Compute Required. Click to configure.';
    class InteractiveEditorWidget {
        constructor(container) {
            this.showComputeShield(container);
        }

        showComputeShield(container) {
            const overlay = $('<div>', {class: 'compute-shield'});
            container.append(overlay);
            const msg = $('<span>');
            msg.text(COMPUTE_MESSAGE);
            overlay.append(msg);
            overlay.on('click', async () => {
                const {id, config} = await this.promptComputeConfig();
                try {
                    this.session = await this.createInteractiveSession(id, config);
                    const features = this.getCapabilities();
                    if (features.save) {
                        DeepForge.registerAction('Save', 'save', 10, () => this.save());
                    }
                    overlay.remove();
                    this.onComputeInitialized(this.session);
                } catch (err) {
                    const title = 'Compute Creation Error';
                    const body = 'Unable to create compute. Please verify the credentials are correct.';
                    // TODO: Detect authorization errors...
                    const dialog = new InformDialog(title, body);
                    dialog.show();
                }
            });
        }

        onComputeInitialized(/*session*/) {
        }

        getCapabilities() {
            return {
                suspend: this.isOveridden('getEditorState') &&
                    this.isOveridden('resume'),
                save: this.isOveridden('getSnapshot') &&
                    this.isOveridden('getOperation') &&
                    this.isOveridden('getEditorState'),
            };
        }

        isOveridden(name) {
            return this[name] !== InteractiveEditorWidget.prototype[name];
        }

        async promptComputeConfig() {
            const dialog = new ConfigDialog();
            const computeMetadata = Compute.getAvailableBackends().map(id => Compute.getMetadata(id));
            const metadata = {
                id: 'InteractiveComputeConfig',
                name: 'Create Compute Instance',
                version: '1.0.0',
                description: '',
                icon: {
                    class: 'glyphicon glyphicon-cog',
                    src: ''
                },
                disableServerSideExecution: false,
                disableBrowserSideExecution: false,
                writeAccessRequired: false,
                configStructure: [
                    {
                        name: 'compute',
                        displayName: 'Compute',
                        description: 'Computational resources to use for execution.',
                        valueType: 'dict',
                        value: Compute.getBackend(Compute.getAvailableBackends()[0]).name,
                        valueItems: computeMetadata,
                    }
                ]
            };
            const allConfigs = await dialog.show(metadata);
            const {name, config} = allConfigs[metadata.id].compute;
            const id = computeMetadata.find(md => md.name === name).id;
            return {id, config};
        }

        async createInteractiveSession(computeId, config) {
            return await Session.new(computeId, config);
        }

        destroy() {
            const features = this.getCapabilities();
            if (features.save) {
                DeepForge.unregisterAction('Save');
            }
        }

        updateNode(/*desc*/) {
        }

        onActivate() {
        }

        onDeactivate() {
        }

        onWidgetContainerResize(/*width, height*/) {
        }
    }

    return InteractiveEditorWidget;
});
