<commit_before>React = require 'react'
ReactDOM = require 'react-dom'
TutorPopover = require './tutor-popover'
{ArbitraryHtmlAndMath} = require 'shared'

_ = require 'underscore'
S = require '../helpers/string'

{MediaStore} = require '../flux/media'


MediaPreview = React.createClass
  displayName: 'MediaPreview'
  getInitialState: ->
    popped: false
    stick: false
    media: null

  propTypes:
    mediaId: React.PropTypes.string.isRequired
    bookHref: React.PropTypes.string.isRequired
    cnxId: React.PropTypes.string.isRequired
    mediaDOMOnParent: React.PropTypes.object
    windowImpl: React.PropTypes.object
    buffer: React.PropTypes.number
    shouldLinkOut: React.PropTypes.bool
    originalHref: React.PropTypes.string

  getDefaultProps: ->
    buffer: 160
    shouldLinkOut: false
    windowImpl: window
    trigger: 'focus'

  componentWillMount: ->
    {mediaId, cnxId} = @props
    media = MediaStore.get(mediaId)
    @updateMedia(media) if media?

  componentWillUnmount: ->
    {mediaId} = @props
    MediaStore.off("loaded.#{mediaId}", @updateMedia)

  updateMedia: (media) ->
    @setState({media})

  checkShouldPop: ->
    return true unless @props.mediaDOMOnParent
    not @isMediaInViewport()

  isMediaInViewport: ->
    {mediaDOMOnParent, buffer, windowImpl} = @props
    mediaRect = mediaDOMOnParent.getBoundingClientRect()

    0 <= (mediaRect.top + buffer) <= windowImpl.innerHeight

  highlightMedia: ->
    {mediaDOMOnParent} = @props
    mediaDOMOnParent.classList.add('link-target')

  unhighlightMedia: ->
    {mediaDOMOnParent} = @props
    mediaDOMOnParent.classList.remove('link-target')

  stickMedia: ->
    @setState(stick: true)
    unless @state.popped
      @setState(popped: true)
      @refs.overlay.show()

  showMedia: ->
    return if @state.stick
    shouldPop = @checkShouldPop()
    if shouldPop
      unless @state.popped
        @setState(popped: true)
        @refs.overlay.show()
    else
      @highlightMedia()

  hideMedia: ->
    if @state.popped
      if not @state.stick
        @setState(popped: false)
        @refs.overlay.hide()
    else
      @unhighlightMedia()<commit_msg>test if mediaDOMOnParent is present before calling methods<commit_after>React = require 'react'
ReactDOM = require 'react-dom'
TutorPopover = require './tutor-popover'
{ArbitraryHtmlAndMath} = require 'shared'

_ = require 'underscore'
S = require '../helpers/string'

{MediaStore} = require '../flux/media'


MediaPreview = React.createClass
  displayName: 'MediaPreview'
  getInitialState: ->
    popped: false
    stick: false
    media: null

  propTypes:
    mediaId: React.PropTypes.string.isRequired
    bookHref: React.PropTypes.string.isRequired
    cnxId: React.PropTypes.string.isRequired
    mediaDOMOnParent: React.PropTypes.object
    windowImpl: React.PropTypes.object
    buffer: React.PropTypes.number
    shouldLinkOut: React.PropTypes.bool
    originalHref: React.PropTypes.string

  getDefaultProps: ->
    buffer: 160
    shouldLinkOut: false
    windowImpl: window
    trigger: 'focus'

  componentWillMount: ->
    {mediaId, cnxId} = @props
    media = MediaStore.get(mediaId)
    @updateMedia(media) if media?

  componentWillUnmount: ->
    {mediaId} = @props
    MediaStore.off("loaded.#{mediaId}", @updateMedia)

  updateMedia: (media) ->
    @setState({media})

  checkShouldPop: ->
    return true unless @props.mediaDOMOnParent
    not @isMediaInViewport()

  isMediaInViewport: ->
    {mediaDOMOnParent, buffer, windowImpl} = @props
    mediaRect = mediaDOMOnParent?.getBoundingClientRect()

    0 <= (mediaRect.top + buffer) <= windowImpl.innerHeight

  highlightMedia: ->
    {mediaDOMOnParent} = @props
    mediaDOMOnParent?.classList.add('link-target')

  unhighlightMedia: ->
    {mediaDOMOnParent} = @props
    mediaDOMOnParent?.classList.remove('link-target')

  stickMedia: ->
    @setState(stick: true)
    unless @state.popped
      @setState(popped: true)
      @refs.overlay.show()

  showMedia: ->
    return if @state.stick
    shouldPop = @checkShouldPop()
    if shouldPop
      unless @state.popped
        @setState(popped: true)
        @refs.overlay.show()
    else
      @highlightMedia()

  hideMedia: ->
    if @state.popped
      if not @state.stick
        @setState(popped: false)
        @refs.overlay.hide()
    else
      @unhighlightMedia()
{'fraction_non_alphanumeric': 0.11552595314653193, 'fraction_numerical': 0.0018373909049150207, 'mean_word_length': 3.337649402390438, 'pattern_counts': {'":': 0, '<': 7, '<?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': '3506082', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1366, 'n_words': 274}