<commit_before>React = require 'react'
FavoritesButton = require '../collections/favorites-button'
Dialog = require 'modal-form/dialog'
{Markdown} = (require 'markdownz').default
getSubjectLocation = require '../lib/get-subject-location'
CollectionsManagerIcon = require '../collections/manager-icon'
FrameViewer = require './frame-viewer'
classnames = require 'classnames'
FlagSubjectButton = require './flag-subject-button'
SignInPrompt = require '../partials/sign-in-prompt'

NOOP = Function.prototype

subjectHasMixedLocationTypes = (subject) ->
  allTypes = []
  (subject?.locations ? []).forEach (location) ->
    Object.keys(location).forEach (typeAndFormat) ->
      type = typeAndFormat.split('/')[0]
      unless type in allTypes
        allTypes.push type
  allTypes.length > 1

CONTAINER_STYLE = display: 'flex', flexWrap: 'wrap', position: 'relative'

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

  contextTypes:
    geordi: React.PropTypes.object

  signInAttentionTimeout: NaN

  getDefaultProps: ->
    subject: null
    user: null
    playFrameDuration: 667
    playIterations: 3
    onFrameChange: NOOP
    onLoad: NOOP
    defaultStyle: true
    project: null
    linkToFullImage: false
    frameWrapper: null
    allowFlipbook: true
    allowSeparateFrames: false
    metadataPrefixes: ['#', '!']
    metadataFilters: ['#', '!']
    workflow: null

  getInitialState: ->
    loading: true
    playing: false
    frame: @props.frame ? 0
    frameDimensions: {}
    inFlipbookMode: @props.allowFlipbook
    promptingToSignIn: false

  componentWillReceiveProps: (nextProps) ->
    unless nextProps.subject is @props.subject
      clearTimeout @signInAttentionTimeout
      @setState loading: true

  logSubjClick: (logType) ->
    @context.geordi?.logEvent
      type: logType

  render: ->
    rootClasses = classnames('subject-viewer', {
      'default-root-style': @props.defaultStyle
      'subject-viewer--flipbook': @state.inFlipbookMode
      "subject-viewer--layout-#{@props.workflow?.configuration?.multi_image_layout}": @props.workflow?.configuration?.multi_image_layout
    })

    isIE = 'ActiveXObject' of window
    if isIE
      rootStyle = flex: '1 1 auto'

    mainDisplay = ''
    {type, format, src} = getSubjectLocation @props.subject, @state.frame
    if @state.inFlipbookMode
      mainDisplay = @renderFrame @state.frame
    else
      mainDisplay = (@renderFrame frame, {key: "frame-#{frame}"} for frame of @props.subject.locations)

    tools = switch type
      when 'image'
        if not @state.inFlipbookMode or @props.subject?.locations.length < 2 or subjectHasMixedLocationTypes @props.subject
          if @props.allowFlipbook and @props.allowSeparateFrames
            <button className="secret-button" aria-label="Toggle flipbook mode" title="Toggle flipbook mode" onClick={@toggleInFlipbookMode}>
              <i className={"fa fa-fw " + if @state.inFlipbookMode then "fa-th-large" else "fa-film"}></i>
            </button>
        else
          <span className="tools">
            {if @props.allowFlipbook and @props.allowSeparateFrames<commit_msg>Fix subject-viewer's for X of Y bug<commit_after>React = require 'react'
FavoritesButton = require '../collections/favorites-button'
Dialog = require 'modal-form/dialog'
{Markdown} = (require 'markdownz').default
getSubjectLocation = require '../lib/get-subject-location'
CollectionsManagerIcon = require '../collections/manager-icon'
FrameViewer = require './frame-viewer'
classnames = require 'classnames'
FlagSubjectButton = require './flag-subject-button'
SignInPrompt = require '../partials/sign-in-prompt'

NOOP = Function.prototype

subjectHasMixedLocationTypes = (subject) ->
  allTypes = []
  (subject?.locations ? []).forEach (location) ->
    Object.keys(location).forEach (typeAndFormat) ->
      type = typeAndFormat.split('/')[0]
      unless type in allTypes
        allTypes.push type
  allTypes.length > 1

CONTAINER_STYLE = display: 'flex', flexWrap: 'wrap', position: 'relative'

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

  contextTypes:
    geordi: React.PropTypes.object

  signInAttentionTimeout: NaN

  getDefaultProps: ->
    subject: null
    user: null
    playFrameDuration: 667
    playIterations: 3
    onFrameChange: NOOP
    onLoad: NOOP
    defaultStyle: true
    project: null
    linkToFullImage: false
    frameWrapper: null
    allowFlipbook: true
    allowSeparateFrames: false
    metadataPrefixes: ['#', '!']
    metadataFilters: ['#', '!']
    workflow: null

  getInitialState: ->
    loading: true
    playing: false
    frame: @props.frame ? 0
    frameDimensions: {}
    inFlipbookMode: @props.allowFlipbook
    promptingToSignIn: false

  componentWillReceiveProps: (nextProps) ->
    unless nextProps.subject is @props.subject
      clearTimeout @signInAttentionTimeout
      @setState loading: true

  logSubjClick: (logType) ->
    @context.geordi?.logEvent
      type: logType

  render: ->
    rootClasses = classnames('subject-viewer', {
      'default-root-style': @props.defaultStyle
      'subject-viewer--flipbook': @state.inFlipbookMode
      "subject-viewer--layout-#{@props.workflow?.configuration?.multi_image_layout}": @props.workflow?.configuration?.multi_image_layout
    })

    isIE = 'ActiveXObject' of window
    if isIE
      rootStyle = flex: '1 1 auto'

    mainDisplay = ''
    {type, format, src} = getSubjectLocation @props.subject, @state.frame
    if @state.inFlipbookMode
      mainDisplay = @renderFrame @state.frame
    else
      mainDisplay = @props.subject.locations.map (frame, index) =>
        @renderFrame index, {key: "frame-#{index}"}

    tools = switch type
      when 'image'
        if not @state.inFlipbookMode or @props.subject?.locations.length < 2 or subjectHasMixedLocationTypes @props.subject
          if @props.allowFlipbook and @props.allowSeparateFrames
            <button className="secret-button" aria-label="Toggle flipbook mode" title="Toggle flipbook mode" onClick={@toggleInFlipbookMode}>
              <i className={"fa fa-fw " + if @state.inFlipbookMode then "fa-th-large" else "fa-film"}></i>
            </button>
        else
          <span className="tools">
            {if @props.allowFlipbook and @props.allowSeparateFrames
{'fraction_non_alphanumeric': 0.11759097409820435, 'fraction_numerical': 0.0031781344350866043, 'mean_word_length': 3.4669978708303764, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 32, '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': '4312894', 'n_tokens_mistral': 1932, 'n_tokens_neox': 1876, 'n_words': 424}