// jshint esversion: 6 import React from "react"; import _ from "lodash"; import { Button, Tooltip, InputGroup, Dialog, Classes, MenuItem, Colors } from "@blueprintjs/core"; import { Select } from "@blueprintjs/select"; import { connect } from "react-redux"; import * as globals from "../../globals"; import Category from "./category"; import { AnnotationsHelpers } from "../../util/stateManager"; @connect(state => ({ categoricalSelection: state.categoricalSelection, writableCategoriesEnabled: state.config?.parameters?.["label_file"] ?? false, schema: state.world?.schema })) class Categories extends React.Component { constructor(props) { super(props); this.state = { createAnnoModeActive: false, newCategoryText: "", categoryToDuplicate: null }; } handleCreateUserAnno = () => { const { dispatch } = this.props; const { newCategoryText, categoryToDuplicate } = this.state; dispatch({ type: "annotation: create category", data: newCategoryText, categoryToDuplicate }); this.setState({ createAnnoModeActive: false, categoryToDuplicate: null, newCategoryText: "" }); }; handleEnableAnnoMode = () => { this.setState({ createAnnoModeActive: true }); }; handleDisableAnnoMode = () => { this.setState({ createAnnoModeActive: false, categoryToDuplicate: null, newCategoryText: "" }); }; handleModalDuplicateCategorySelection = d => { this.setState({ categoryToDuplicate: d }); }; categoryNameError = name => { /* return false if this is a LEGAL/acceptable category name, or return an error type. */ const { categoricalSelection } = this.props; const allCategoryNames = Object.keys(categoricalSelection); if (allCategoryNames.indexOf(name) !== -1) { return "duplicate"; } if (!AnnotationsHelpers.isLegalAnnotationName(name)) { return "characters"; } return false; }; categoryNameErrorMessage = name => { const err = this.categoryNameError(name); if (err == "duplicate") { return ( {name} already exists - no duplicates allowed ); } if (err == "characters") { return ( {name} contains illegal characters. Hint: use alpha-numeric and underscore ); } return err; }; render() { const { createAnnoModeActive, categoryToDuplicate, newCategoryText } = this.state; const { categoricalSelection, writableCategoriesEnabled, schema } = this.props; if (!categoricalSelection) return null; /* all names, sorted in display order. Will be rendered in this order */ const allCategoryNames = Object.keys(categoricalSelection).sort(); return (