<commit_before>React = require 'react'
BS = require 'react-bootstrap'

TeacherContentToggle = React.createClass

  propTypes:
    onChange: React.PropTypes.func.isRequired
    isShowing: React.PropTypes.bool.isRequired

  onClick: ->
    @props.onChange(not @props.isShowing)

  render: ->
    teacherLinkText = if @props.isShowing
      'Hide Teacher Edition'
    else
      'Show Teacher Edition'

    <BS.NavItem className='teacher-edition' onClick={@onClick}>
      {teacherLinkText}
    </BS.NavItem>

module.exports = TeacherContentToggle<commit_msg>Make active only if 'os-teacher' class is present<commit_after>React = require 'react'
BS = require 'react-bootstrap'

TEACHER_CONTENT_SELECTOR = '.os-teacher'

TeacherContentToggle = React.createClass

  propTypes:
    onChange:   React.PropTypes.func.isRequired
    isShowing:  React.PropTypes.bool
    section:    React.PropTypes.string
    windowImpl: React.PropTypes.object

  getDefaultProps: ->
    windowImpl: window

  getInitialState: ->
    hasTeacherContent: false

  onClick: ->
    @props.onChange(not @props.isShowing) if @state.hasTeacherContent is true

  componentDidMount:      -> @scheduleCheckForTeacherContent()
  componentDidUpdate:     -> @scheduleCheckForTeacherContent()
  componentWillUnmount:   -> clearTimeout(@state.pendingCheck) if @state.pendingCheck
  scheduleCheckForTeacherContent: ->
    return if @state.pendingCheck
    @setState(pendingCheck: _.defer(@checkForTeacherContent))

  checkForTeacherContent: ->
    @setState(
      pendingCheck: false
      hasTeacherContent: !!@props.windowImpl.document.querySelector(TEACHER_CONTENT_SELECTOR)
    )

  renderNoContentTooltip: ->
    <BS.Popover id='no-content'>
      No teacher edition content is available for this page.
    </BS.Popover>

  render: ->
    teacherLinkText = if @props.isShowing
      'Hide Teacher Edition'
    else
      'Show Teacher Edition'

    content = if @state.hasTeacherContent
      <span className="has-content">{teacherLinkText}</span>
    else
      <BS.OverlayTrigger
        placement='bottom' trigger='click' overlay={@renderNoContentTooltip()}
      >
        <span className="no-content">{teacherLinkText}</span>
      </BS.OverlayTrigger>


    <BS.NavItem className='teacher-edition' onClick={@onClick}>
      {content}
    </BS.NavItem>

module.exports = TeacherContentToggle
{'fraction_non_alphanumeric': 0.1059322033898305, 'fraction_numerical': 0.0, 'mean_word_length': 3.7600806451612905, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 27, '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': '3016585', 'n_tokens_mistral': 728, 'n_tokens_neox': 713, 'n_words': 136}