<commit_before>React = require 'react'
handleInputChange = require '../../lib/handle-input-change'
drawingTools = require '../drawing-tools'
alert = require '../../lib/alert'
DrawingTaskDetailsEditor = require './drawing-task-details-editor'

NOOP = Function.prototype

MAX_TEXT_LENGTH_IN_MENU = 100

NextTaskSelector = React.createClass
  displayName: 'NextTaskSelector'

  getDefaultProps: ->
    workflow: null
    name: ''
    value: ''
    isSubtask: false
    onChange: NOOP
    onDelete: NOOP

  render: ->
    tasks = require '.' # Work around circular dependency.

    <select name={@props.name} value={@props.value} onChange={@props.onChange}>
      <option value="">(End of classification!)</option>
      {for key, definition of @props.workflow.tasks
        text = tasks[definition.type].getTaskText definition
        if text.length > MAX_TEXT_LENGTH_IN_MENU
          text = text[0...MAX_TEXT_LENGTH_IN_MENU] + '...'
        <option key={key}, value={key}>{text}</option>}
    </select>

module.exports = React.createClass
  displayName: 'GenericTaskEditor'

  getDefaultProps: ->
    workflow: null
    task: null
    onChange: NOOP

  render: ->
    [mainTextKey, choicesKey] = switch @props.task.type
      when 'single', 'multiple' then ['question', 'answers']
      when 'drawing' then ['instruction', 'tools']

    <div className="workflow-task-editor #{@props.task.type}">
      <div className="columns-container">
        <div>
          <span className="form-label">Main text</span><br />
          <textarea name={mainTextKey} value={@props.task[mainTextKey]} className="standard-input full" onChange={@handleInputChange} />
        </div>

        {unless @props.isSubtask
          <div>
            <span className="form-label">Help text</span><br />
            <textarea name="help" value={@props.task.help ? ''} className="standard-input full" onChange={@handleInputChange} />
          </div>}
      </div>

      <hr />

      <span className="form-label">Choices</span>
      {' '}
      {if choicesKey is 'answers'
        [<label key="multiple" className="pill-button">
          <input type="checkbox" checked={@props.task.type is 'multiple'} onChange={@toggleMultipleChoice} />{' '}
          Multiple choice
        </label>
        {' '}
        <label key="required" className="pill-button">
          <input type="checkbox" name="required" checked={@props.task.required} onChange={@handleInputChange} />{' '}
          Required
        </label>]}
        <br />

      <div className="workflow-task-editor-choices">
        {if @props.task[choicesKey].length is 0
          <span className="form-help">No <code>{choicesKey}</code> defined for this task.</span>}
        {for choice, index in @props.task[choicesKey]
          choice._key ?= Math.random()
          <div key={choice._key} className="workflow-choice-editor">
            <textarea name="#{choicesKey}.#{index}.label" value={choice.label} className="standard-input full" onChange={@handleInputChange} />

            <div className="workflow-choice-settings">
              {switch @props.task.type
                when 'single'
                  unless @props.isSubtask
                    <div className="workflow-choice-setting">
                      Next task{' '}
                      <NextTaskSelector workflow={@props.workflow} name="#{choicesKey}.#{index}.next" value={choice.next ? ''} onChange={@handleInputChange} />
                    </div>

                when 'drawing'
                  [<div key={choice.type} className="workflow-choice-setting">
                    Type{' '}
                    <select name="#{choicesKey}.#{index}.type" value={choice.type} onChange={@handleInputChange}>
                      {for toolKey of drawingTools
                        <option key={toolKey} value={toolKey}>{toolKey}</option>}
                    </select>
                  </div>

                  <div key={choice.color} className="workflow-choice-setting"><commit_msg>Add workaround for empty arrays becoming null<commit_after>React = require 'react'
handleInputChange = require '../../lib/handle-input-change'
drawingTools = require '../drawing-tools'
alert = require '../../lib/alert'
DrawingTaskDetailsEditor = require './drawing-task-details-editor'

NOOP = Function.prototype

MAX_TEXT_LENGTH_IN_MENU = 100

NextTaskSelector = React.createClass
  displayName: 'NextTaskSelector'

  getDefaultProps: ->
    workflow: null
    name: ''
    value: ''
    isSubtask: false
    onChange: NOOP
    onDelete: NOOP

  render: ->
    tasks = require '.' # Work around circular dependency.

    <select name={@props.name} value={@props.value} onChange={@props.onChange}>
      <option value="">(End of classification!)</option>
      {for key, definition of @props.workflow.tasks
        text = tasks[definition.type].getTaskText definition
        if text.length > MAX_TEXT_LENGTH_IN_MENU
          text = text[0...MAX_TEXT_LENGTH_IN_MENU] + '...'
        <option key={key}, value={key}>{text}</option>}
    </select>

module.exports = React.createClass
  displayName: 'GenericTaskEditor'

  getDefaultProps: ->
    workflow: null
    task: null
    onChange: NOOP

  render: ->
    [mainTextKey, choicesKey] = switch @props.task.type
      when 'single', 'multiple' then ['question', 'answers']
      when 'drawing' then ['instruction', 'tools']

    <div className="workflow-task-editor #{@props.task.type}">
      <div className="columns-container">
        <div>
          <span className="form-label">Main text</span><br />
          <textarea name={mainTextKey} value={@props.task[mainTextKey]} className="standard-input full" onChange={@handleInputChange} />
        </div>

        {unless @props.isSubtask
          <div>
            <span className="form-label">Help text</span><br />
            <textarea name="help" value={@props.task.help ? ''} className="standard-input full" onChange={@handleInputChange} />
          </div>}
      </div>

      <hr />

      <span className="form-label">Choices</span>
      {' '}
      {if choicesKey is 'answers'
        [<label key="multiple" className="pill-button">
          <input type="checkbox" checked={@props.task.type is 'multiple'} onChange={@toggleMultipleChoice} />{' '}
          Multiple choice
        </label>
        {' '}
        <label key="required" className="pill-button">
          <input type="checkbox" name="required" checked={@props.task.required} onChange={@handleInputChange} />{' '}
          Required
        </label>]}
        <br />

      <div className="workflow-task-editor-choices">
        {if @props.task[choicesKey]?.length ? 0 is 0
          <span className="form-help">No <code>{choicesKey}</code> defined for this task.</span>}
        {for choice, index in @props.task[choicesKey] ? []
          choice._key ?= Math.random()
          <div key={choice._key} className="workflow-choice-editor">
            <textarea name="#{choicesKey}.#{index}.label" value={choice.label} className="standard-input full" onChange={@handleInputChange} />

            <div className="workflow-choice-settings">
              {switch @props.task.type
                when 'single'
                  unless @props.isSubtask
                    <div className="workflow-choice-setting">
                      Next task{' '}
                      <NextTaskSelector workflow={@props.workflow} name="#{choicesKey}.#{index}.next" value={choice.next ? ''} onChange={@handleInputChange} />
                    </div>

                when 'drawing'
                  [<div key={choice.type} className="workflow-choice-setting">
                    Type{' '}
                    <select name="#{choicesKey}.#{index}.type" value={choice.type} onChange={@handleInputChange}>
                      {for toolKey of drawingTools
                        <option key={toolKey} value={toolKey}>{toolKey}</option>}
                    </select>
                  </div>

                  <div key={choice.color} className="workflow-choice-setting">
{'fraction_non_alphanumeric': 0.14970134395221504, 'fraction_numerical': 0.0013688402190144351, 'mean_word_length': 2.5019607843137255, 'pattern_counts': {'":': 0, '<': 101, '<?xml version=': 0, '>': 111, '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': '5817880', 'n_tokens_mistral': 2301, 'n_tokens_neox': 2238, 'n_words': 450}