<commit_before>$ = require 'jquery'
_ = require 'underscore'

{getContent} = require '../../models/helpers/content_helper.coffee'

WRAP_VERTICAL_UNUSABLE_PX = 50 # account for footer (45px) and add a small epsilon for borders etc.

exports.getPictureAreaDimensions = ->
  $wrap = $('#wrap')
  [$wrap.width, $wrap.height - WRAP_VERTICAL_UNUSABLE_PX]

exports.selectMedia = (picture) ->
  [maxWidth, maxHeight] = exports.getPictureAreaDimensions()

  media = picture.get('media')

  # find the largest that fits
  byWidth = (medium) -> medium.width
  mediaThatFit = _.filter media, (medium) -> medium.width <= maxWidth && medium.height <= maxHeight
  return _.max mediaThatFit, byWidth unless _.isEmpty mediaThatFit

  # fall back to the smallest
  _.min media, byWidth

exports.preloadMedia = (selector, path) ->
  getContent(path).then (content) ->
    {album, picture} = content
    selectedMedia = selectMedia picture
    return unless selectedMedia and selectedMedia.src
    $('<img/>').attr 'src', selectedMedia.src
  .done()

window.edegalViewHelperMedia = exports if window?<commit_msg>fix invalid reference to selectMedia<commit_after>$ = require 'jquery'
_ = require 'underscore'

{getContent} = require '../../models/helpers/content_helper.coffee'

WRAP_VERTICAL_UNUSABLE_PX = 50 # account for footer (45px) and add a small epsilon for borders etc.

exports.getPictureAreaDimensions = ->
  $wrap = $('#wrap')
  [$wrap.width, $wrap.height - WRAP_VERTICAL_UNUSABLE_PX]

exports.selectMedia = (picture) ->
  [maxWidth, maxHeight] = exports.getPictureAreaDimensions()

  media = picture.get('media')

  # find the largest that fits
  byWidth = (medium) -> medium.width
  mediaThatFit = _.filter media, (medium) -> medium.width <= maxWidth && medium.height <= maxHeight
  return _.max mediaThatFit, byWidth unless _.isEmpty mediaThatFit

  # fall back to the smallest
  _.min media, byWidth

exports.preloadMedia = (selector, path) ->
  getContent(path).then (content) ->
    {album, picture} = content
    selectedMedia = exports.selectMedia picture
    $('<img/>').attr 'src', selectedMedia.src
  .done()

window.edegalViewHelperMedia = exports if window?
{'fraction_non_alphanumeric': 0.12140287769784172, 'fraction_numerical': 0.0035971223021582736, 'mean_word_length': 3.744136460554371, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, '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': '663438', 'n_tokens_mistral': 728, 'n_tokens_neox': 654, 'n_words': 178}