<commit_before>/*
nbgrader functionality: the create assignment toolbar.
<Form inline>
  <FormGroup controlId="formInlineName">
*/

import {
  Button,
  FormControl,
  FormGroup,
  ControlLabel,
  Form
} from "react-bootstrap";
import { Map } from "immutable";

import { React, Component, Rendered } from "../../app-framework";

import { Icon } from "../../r_misc/icon";

import { JupyterActions } from "../browser-actions";

import { Metadata } from "./types";

import { popup } from "../../frame-editors/frame-tree/print";

import {
  CELLTYPE_INFO_LIST,
  CELLTYPE_INFO_MAP,
  value_to_state,
  state_to_value
} from "./cell-types";

const OPTIONS_CODE: Rendered[] = [];
const OPTIONS_NOTCODE: Rendered[] = [];

for (let x of CELLTYPE_INFO_LIST) {
  const option = (
    <option key={x.value} value={x.value}>
      {x.title}
    </option>
  );
  OPTIONS_CODE.push(option);
  if (!x.code_only) {
    OPTIONS_NOTCODE.push(option);
  }
}
interface CreateAssignmentProps {
  actions: JupyterActions;
  cell: Map<string, any>;
}

export class CreateAssignmentToolbar extends Component<CreateAssignmentProps> {
  private select(value: string): void {
    if (value == "") {
      // clearing state
      this.props.actions.nbgrader_actions.set_metadata(
        this.props.cell.get("id")
      );
      return;
    }
    const metadata: Metadata = value_to_state(value);
    metadata.grade_id = this.props.cell.getIn(
      ["metadata", "nbgrader", "grade_id"],
      ""
    );
    if (!metadata.grade_id) {
      // TODO -- check if default is globally unique...?
      metadata.grade_id = this.props.cell.get("id");
    }
    this.props.actions.nbgrader_actions.set_metadata(
      this.props.cell.get("id"),
      metadata
    );
  }

  private set_points(points: number): void {
    if (points <= 0) {
      points = 1;
    }
    this.props.actions.nbgrader_actions.set_metadata(
      this.props.cell.get("id"),
      { points }
    );
  }

  private get_value(): string {
    const x = this.props.cell.getIn(["metadata", "nbgrader"], Map());
    if (x == null) return "";
    try {
      return state_to_value(x.toJS());
    } catch (err) {
      this.select(""); // clear all the metadata.
      return "";
    }
  }

  private render_locked(): Rendered {
    const locked: boolean = !!this.props.cell.getIn([
      "metadata",
      "nbgrader",
      "locked"
    ]);
    if (!locked) return;
    return <Icon name={"lock"} style={{ float: "left", padding: "5px" }} />;
  }

  private render_points(): Rendered {
    const points: number | undefined = this.props.cell.getIn([
      "metadata",
      "nbgrader",
      "points"
    ]);
    if (points == null) return;
    return (
      <FormGroup>
        <ControlLabel style={{ fontWeight: 400 }}>Points:</ControlLabel>
        <FormControl
          type="number"
          value={points}
          onChange={e => this.set_points(parseInt((e.target as any).value))}
          style={{
            color: "#666",
            width: "64px",
            marginLeft: "5px",
            fontSize: "14px"
          }}
        />
      </FormGroup>
    );
  }

  private set_grade_id(grade_id: string): void {
    // TODO: check globally unique... or change to always just equal the cell id...
    this.props.actions.nbgrader_actions.set_metadata(
      this.props.cell.get("id"),
      { grade_id }
    );
  }

  private render_id(): Rendered {
    const grade_id: string | undefined = this.props.cell.getIn([
      "metadata",
      "nbgrader",
      "grade_id"
    ]);
    if (grade_id == null) return;
    return (
      <FormGroup>
        <ControlLabel style={{ marginLeft: "15px", fontWeight: 400 }}>
          ID:<commit_msg>nbgrader bar: points can be fractions<commit_after>/*
nbgrader functionality: the create assignment toolbar.
<Form inline>
  <FormGroup controlId="formInlineName">
*/

import {
  Button,
  FormControl,
  FormGroup,
  ControlLabel,
  Form
} from "react-bootstrap";
import { Map } from "immutable";

import { React, Component, Rendered } from "../../app-framework";

import { Icon } from "../../r_misc/icon";

import { JupyterActions } from "../browser-actions";

import { Metadata } from "./types";

import { popup } from "../../frame-editors/frame-tree/print";

import {
  CELLTYPE_INFO_LIST,
  CELLTYPE_INFO_MAP,
  value_to_state,
  state_to_value
} from "./cell-types";

const OPTIONS_CODE: Rendered[] = [];
const OPTIONS_NOTCODE: Rendered[] = [];

for (let x of CELLTYPE_INFO_LIST) {
  const option = (
    <option key={x.value} value={x.value}>
      {x.title}
    </option>
  );
  OPTIONS_CODE.push(option);
  if (!x.code_only) {
    OPTIONS_NOTCODE.push(option);
  }
}
interface CreateAssignmentProps {
  actions: JupyterActions;
  cell: Map<string, any>;
}

export class CreateAssignmentToolbar extends Component<CreateAssignmentProps> {
  private select(value: string): void {
    if (value == "") {
      // clearing state
      this.props.actions.nbgrader_actions.set_metadata(
        this.props.cell.get("id")
      );
      return;
    }
    const metadata: Metadata = value_to_state(value);
    metadata.grade_id = this.props.cell.getIn(
      ["metadata", "nbgrader", "grade_id"],
      ""
    );
    if (!metadata.grade_id) {
      // TODO -- check if default is globally unique...?
      metadata.grade_id = this.props.cell.get("id");
    }
    this.props.actions.nbgrader_actions.set_metadata(
      this.props.cell.get("id"),
      metadata
    );
  }

  private set_points(points: any): void {
    points = parseFloat(points);
    if (!Number.isFinite(points) || points < 0) {
      points = 0;
    }
    this.props.actions.nbgrader_actions.set_metadata(
      this.props.cell.get("id"),
      { points }
    );
  }

  private get_value(): string {
    const x = this.props.cell.getIn(["metadata", "nbgrader"], Map());
    if (x == null) return "";
    try {
      return state_to_value(x.toJS());
    } catch (err) {
      this.select(""); // clear all the metadata.
      return "";
    }
  }

  private render_locked(): Rendered {
    const locked: boolean = !!this.props.cell.getIn([
      "metadata",
      "nbgrader",
      "locked"
    ]);
    if (!locked) return;
    return <Icon name={"lock"} style={{ float: "left", padding: "5px" }} />;
  }

  private render_points(): Rendered {
    const points: number | undefined = this.props.cell.getIn([
      "metadata",
      "nbgrader",
      "points"
    ]);
    if (points == null) return;
    return (
      <FormGroup>
        <ControlLabel style={{ fontWeight: 400 }}>Points:</ControlLabel>
        <FormControl
          type="number"
          defaultValue={`${points}`}
          onChange={e => this.set_points((e.target as any).value)}
          style={{
            color: "#666",
            width: "64px",
            marginLeft: "5px",
            fontSize: "14px"
          }}
        />
      </FormGroup>
    );
  }

  private set_grade_id(grade_id: string): void {
    // TODO: check globally unique... or change to always just equal the cell id...
    this.props.actions.nbgrader_actions.set_metadata(
      this.props.cell.get("id"),
      { grade_id }
    );
  }

  private render_id(): Rendered {
    const grade_id: string | undefined = this.props.cell.getIn([
      "metadata",
      "nbgrader",
      "grade_id"
    ]);
    if (grade_id == null) return;
    return (
      <FormGroup>
        <ControlLabel style={{ marginLeft: "15px", fontWeight: 400 }}>
          ID:
{'fraction_non_alphanumeric': 0.15259696685008722, 'fraction_numerical': 0.005099986578982687, 'mean_word_length': 2.5655502392344496, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4670200', 'n_tokens_mistral': 2447, 'n_tokens_neox': 2409, 'n_words': 525}