<commit_before>MAX_PAGE_WIDTH = 600

require './readymade/overrides.coffee'
SubjectMetadata = require './subject-metadata'
OrchidType = require './orchid-type'
SubjectViewer = require 'zooniverse-readymade/lib/subject-viewer'
ClassifyPage = require 'zooniverse-readymade/lib/classify-page'
SiteHeader = require 'zooniverse-readymade/lib/site-header'

OrchidFilterTask = require './tasks/filter'
DecisionTree = require 'zooniverse-decision-tree'
  
ClassifyPage::showPageHelp = () ->
  @fieldGuideContainer.attr 'aria-hidden', !@help.checked

SubjectViewer::template = require './templates/subject-viewer'
SiteHeader::template = require './templates/site-header'

SubjectViewer::rescale = ()->
  width = Math.min MAX_PAGE_WIDTH, .5 * window.innerWidth
  scale = width / @maxWidth
  @zoom scale

SubjectViewer::crop = (rectangle, margin = 25, limit = 1.5)->
  w = rectangle.width + margin * 2
  h = rectangle.height + margin * 2
  scale = @markingSurface.el.parentNode.offsetWidth / w
  scale = Math.min scale, limit
  @markingSurface.svg.attr 'width', parseInt scale * w
  @markingSurface.svg.attr 'height', parseInt scale * h
  @markingSurface.rescale rectangle.left - margin, rectangle.top - margin, w, h
  @maxWidth = w
  @maxHeight = h
  @zoom @markingSurface.scaleX

SubjectViewer::zoom = (scale = 1) ->
  @scale = Math.min scale, 1.0
  @scale = Math.max @scale, .4
  width = Math.min @scale * @maxWidth, @markingSurface.el.parentNode.offsetWidth
  @scale = width / @maxWidth
  @markingSurface.svg.attr
    width: parseInt width
    height: parseInt @scale * @maxHeight
  @markingSurface.rescale()

currentProject = require 'zooniverse-readymade/current-project'
herbarium_page = currentProject.classifyPages[0]
field_page = currentProject.classifyPages[1]
  
herbarium_species = herbarium_page.decisionTree.el.querySelector('input[name=species]')

currentProject.homePage.querySelector( '.readymade-footer' ).insertAdjacentHTML 'beforeEnd', '<a href="#/upload" class="readymade-call-to-action">Upload photos</a>'

for page in currentProject.classifyPages
  do (page) ->
    {decisionTree, subjectViewer} = page
    ms = subjectViewer.markingSurface
    
    page.Subject.trackSeenSubject = (subject) ->

    # set the image scale if not already set  
    ms.on 'marking-surface:add-tool', (tool) ->
      @rescale() if @scaleX is 0
      if tool.constructor.count > 1
        tool.deselect()
        tool.destroy()
        @selection = null
    
      {label} = decisionTree.currentTask.getChoice() ? ''
      legend = tool.controls?.el.querySelector 'legend'
      legend.textContent = label if legend?
    
    page.on page.LOAD_SUBJECT, (e, subject)->
      ms.rescale 0, 0, subjectViewer.maxWidth, subjectViewer.maxHeight
    
    page.el.on decisionTree.LOAD_TASK, ({originalEvent: detail: {task}})->
      if task.key is 'verify'
        rectangle = tool.mark for tool in ms.tools when tool.mark.value is 'specimen-label'
        subjectViewer.crop rectangle
      else
        image = ms.root.el.querySelector 'g.frames image'
        subjectViewer.maxWidth = parseInt image.getAttribute 'width'
        subjectViewer.maxHeight = parseInt image.getAttribute 'height'
        subjectViewer.rescale()
        ms.rescale 0, 0, subjectViewer.maxWidth, subjectViewer.maxHeight
    
    page.Subject.on 'select', (e, subject)->
      {metadata} = subject
      
      if page.workflow is 'herbarium'
        page.decisionTree.tasks.verify.setDefaults
          species: metadata.scientific_name
          date: metadata.collection_date_from
          locality: metadata.locality_string
          comments: metadata.label_comments
        
        page.decisionTree.tasks.vc.setDefaults
          vc: metadata.vc_number
          registration: metadata.registration
          
        # disable the herbarium species if NHM already know it.
        herbarium_species.disabled = !!metadata.scientific_name.length
      
      if page.workflow is 'field'
        page.decisionTree.tasks.species.clearFilters()
      

subject_metadata = new SubjectMetadata
herbarium_page.el.find('.decision-tree').prepend subject_metadata.el

orchid_type = new OrchidType
field_page.el.find('.decision-tree').before orchid_type.el
    
# herbarium_page.el.on decisionTree.LOAD_TASK, ({originalEvent: detail: {task, index}})->
  
for page in [herbarium_page, field_page]
  page.help = page.el[0].querySelector('input[name=help]')
  page.fieldGuideContainer.attr 'aria-hidden', !page.help.checked
    
  do (page)->
    page.help.addEventListener 'change', (e)->
      page.showPageHelp()
    
    zoom_timeout = null

    onZoomIn = (e)->
      e.preventDefault()
      onZoom e.currentTarget, .1
  <commit_msg>Allow for herbarium subjects with null metadata (batch 2)<commit_after>MAX_PAGE_WIDTH = 600

require './readymade/overrides.coffee'
SubjectMetadata = require './subject-metadata'
OrchidType = require './orchid-type'
SubjectViewer = require 'zooniverse-readymade/lib/subject-viewer'
ClassifyPage = require 'zooniverse-readymade/lib/classify-page'
SiteHeader = require 'zooniverse-readymade/lib/site-header'

OrchidFilterTask = require './tasks/filter'
DecisionTree = require 'zooniverse-decision-tree'
  
ClassifyPage::showPageHelp = () ->
  @fieldGuideContainer.attr 'aria-hidden', !@help.checked

SubjectViewer::template = require './templates/subject-viewer'
SiteHeader::template = require './templates/site-header'

SubjectViewer::rescale = ()->
  width = Math.min MAX_PAGE_WIDTH, .5 * window.innerWidth
  scale = width / @maxWidth
  @zoom scale

SubjectViewer::crop = (rectangle, margin = 25, limit = 1.5)->
  w = rectangle.width + margin * 2
  h = rectangle.height + margin * 2
  scale = @markingSurface.el.parentNode.offsetWidth / w
  scale = Math.min scale, limit
  @markingSurface.svg.attr 'width', parseInt scale * w
  @markingSurface.svg.attr 'height', parseInt scale * h
  @markingSurface.rescale rectangle.left - margin, rectangle.top - margin, w, h
  @maxWidth = w
  @maxHeight = h
  @zoom @markingSurface.scaleX

SubjectViewer::zoom = (scale = 1) ->
  @scale = Math.min scale, 1.0
  @scale = Math.max @scale, .4
  width = Math.min @scale * @maxWidth, @markingSurface.el.parentNode.offsetWidth
  @scale = width / @maxWidth
  @markingSurface.svg.attr
    width: parseInt width
    height: parseInt @scale * @maxHeight
  @markingSurface.rescale()

currentProject = require 'zooniverse-readymade/current-project'
herbarium_page = currentProject.classifyPages[0]
field_page = currentProject.classifyPages[1]
  
herbarium_species = herbarium_page.decisionTree.el.querySelector('input[name=species]')

currentProject.homePage.querySelector( '.readymade-footer' ).insertAdjacentHTML 'beforeEnd', '<a href="#/upload" class="readymade-call-to-action">Upload photos</a>'

for page in currentProject.classifyPages
  do (page) ->
    {decisionTree, subjectViewer} = page
    ms = subjectViewer.markingSurface
    
    page.Subject.trackSeenSubject = (subject) ->

    # set the image scale if not already set  
    ms.on 'marking-surface:add-tool', (tool) ->
      @rescale() if @scaleX is 0
      if tool.constructor.count > 1
        tool.deselect()
        tool.destroy()
        @selection = null
    
      {label} = decisionTree.currentTask.getChoice() ? ''
      legend = tool.controls?.el.querySelector 'legend'
      legend.textContent = label if legend?
    
    page.on page.LOAD_SUBJECT, (e, subject)->
      ms.rescale 0, 0, subjectViewer.maxWidth, subjectViewer.maxHeight
    
    page.el.on decisionTree.LOAD_TASK, ({originalEvent: detail: {task}})->
      if task.key is 'verify'
        rectangle = tool.mark for tool in ms.tools when tool.mark.value is 'specimen-label'
        subjectViewer.crop rectangle
      else
        image = ms.root.el.querySelector 'g.frames image'
        subjectViewer.maxWidth = parseInt image.getAttribute 'width'
        subjectViewer.maxHeight = parseInt image.getAttribute 'height'
        subjectViewer.rescale()
        ms.rescale 0, 0, subjectViewer.maxWidth, subjectViewer.maxHeight
    
    page.Subject.on 'select', (e, subject)->
      {metadata} = subject
      
      if page.workflow is 'herbarium'
        page.decisionTree.tasks.verify.setDefaults
          species: metadata.scientific_name ? ''
          date: metadata.collection_date_from ? ''
          locality: metadata.locality_string ? ''
          comments: metadata.label_comments ? ''
        
        page.decisionTree.tasks.vc.setDefaults
          vc: metadata.vc_number ? ''
          registration: metadata.registration ? ''
          
        # disable the herbarium species if NHM already know it.
        herbarium_species.disabled = !!herbarium_species.value.length
      
      if page.workflow is 'field'
        page.decisionTree.tasks.species.clearFilters()
      

subject_metadata = new SubjectMetadata
herbarium_page.el.find('.decision-tree').prepend subject_metadata.el

orchid_type = new OrchidType
field_page.el.find('.decision-tree').before orchid_type.el
    
# herbarium_page.el.on decisionTree.LOAD_TASK, ({originalEvent: detail: {task, index}})->
  
for page in [herbarium_page, field_page]
  page.help = page.el[0].querySelector('input[name=help]')
  page.fieldGuideContainer.attr 'aria-hidden', !page.help.checked
    
  do (page)->
    page.help.addEventListener 'change', (e)->
      page.showPageHelp()
    
    zoom_timeout = null

    onZoomIn = (e)->
      e.preventDefault()
      onZoom e.currentTarget, .1
  
{'fraction_non_alphanumeric': 0.11598183162564699, 'fraction_numerical': 0.00517587408894053, 'mean_word_length': 3.418105459636024, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, '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': '300377', 'n_tokens_mistral': 2975, 'n_tokens_neox': 2913, 'n_words': 626}