<commit_before>$ = require('jquery')
_ = require('lodash')

Configuration = require('./configuration.coffee')
Theme = require('./theme.coffee')
Player = require('./player.coffee')
ProgressBar = require('./progress_bar.coffee')
Embed = require('./embed.coffee')
Feed = require('./feed.coffee')
ExternalData = require('./external_data.coffee')

ChapterMarks = require('./extensions/chaptermarks.coffee')
Download = require('./extensions/download.coffee')
EpisodeInfo = require('./extensions/episode_info.coffee')
Playlist = require('./extensions/playlist.coffee')
Playerjs = require('./extensions/playerjs.coffee')
Share = require('./extensions/share.coffee')
Transcript = require('./extensions/transcript.coffee')

class PodigeePodcastPlayer
  @defaultExtensions: [
    ProgressBar,
    ChapterMarks,
    Download,
    EpisodeInfo,
    Playlist,
    Playerjs,
    Share,
    Transcript,
  ]

  constructor: (@elemClass) ->
    @getConfiguration().loaded.done =>
      @externalData = new ExternalData(this)
      @renderTheme().done =>
        @initPlayer()

  extensions: {}

  getFeed: () ->
    return unless @podcast.feed

    @podcast.feed = new Feed(this)

  getProductionData: () ->
    return unless @episode.productionDataUrl

    self = @
    $.getJSON(@episode.productionDataUrl).done (data) =>
      self.episode.productionData = data.data

  getConfiguration: () ->
    new Configuration(this)

  renderTheme: =>
    rendered = $.Deferred()
    @theme = new Theme(this)
    @theme.loaded.done =>
      @elem = @theme.render()
      window.setTimeout @sendSizeChange, 0
      rendered.resolve()

    rendered.promise()

  initPlayer: =>
    mediaElem = @elem.find('audio')[0]
    new Player(this, mediaElem, @options, @init)

  init: (player) =>
    @player = player
    @bindButtons()
    @bindPlayerEvents()
    @initializeExtensions()
    @bindWindowResizing()

  mediaLoaded: =>
    window.setTimeout @sendSizeChange, 0
    @theme.removeLoadingClass()

  mediaLoadError: =>
    window.setTimeout @sendSizeChange, 0
    @theme.removeLoadingClass()
    @theme.addFailedLoadingClass()

  # initialize elements

  togglePlayState: () =>
    return unless @player?
    if @player.playing
      @elem.addClass('playing')
    else
      @elem.removeClass('playing')

  # event handlers

  bindPlayerEvents: () ->
    $(@player.media).on('timeupdate', @updateTime)
      .on('ended', @triggerEnded)
      .on('error', @triggerError)

  updateTime: () =>
    timeString = @extensions.ProgressBar.updateTime()
    @adjustPlaySpeed(timeString)

  triggerEnded: =>
    @player.media.currentTime = 0
    @extensions.ProgressBar.updateTime()
    @togglePlayState()

  triggerError: =>
    @extensions.ProgressBar.hideBuffering()

  tempPlayBackSpeed: null
  adjustPlaySpeed: (timeString) =>
    return unless @episode.productionData

    currentTime = @player.media.currentTime
    item = $.grep @episode.productionData.statistics.music_speech, (item, index) ->
      item.start.indexOf(timeString) != -1

    if item.length
      if item[0].label == 'music'
        unless @options.currentPlaybackRate == 1.0
          @tempPlayBackSpeed = @options.currentPlaybackRate
          @player.setPlaySpeed(1.0)
      else
        if @tempPlayBackSpeed
          @player.setPlaySpeed(@tempPlayBackSpeed)
          @tempPlayBackSpeed = null

    @updateSpeedDisplay()

  bindButtons: () =>
    triggerPlayPause = (event) =>
      event.preventDefault()
      if @extensions.ChromeCast && @extensions.ChromeCast.active
        @extensions.ChromeCast.togglePlayState()
      else
        @player.playPause()
    @theme.playPauseElement.on 'click', triggerPlayPause
    @theme.playPauseElement.on 'touchstart', triggerPlayPause

    @theme.backwardElement.click =>
      @player.jumpBackward()

    @theme.forwardElement.click =>
      @player.jumpForward()

    @theme.speedElement.click (event) =>
      @player.changePlaySpeed()
      @updateSpeedDisplay()<commit_msg>try to fix chrome not playing<commit_after>$ = require('jquery')
_ = require('lodash')

Configuration = require('./configuration.coffee')
Theme = require('./theme.coffee')
Player = require('./player.coffee')
ProgressBar = require('./progress_bar.coffee')
Embed = require('./embed.coffee')
Feed = require('./feed.coffee')
ExternalData = require('./external_data.coffee')

ChapterMarks = require('./extensions/chaptermarks.coffee')
Download = require('./extensions/download.coffee')
EpisodeInfo = require('./extensions/episode_info.coffee')
Playlist = require('./extensions/playlist.coffee')
Playerjs = require('./extensions/playerjs.coffee')
Share = require('./extensions/share.coffee')
Transcript = require('./extensions/transcript.coffee')

class PodigeePodcastPlayer
  @defaultExtensions: [
    ProgressBar,
    ChapterMarks,
    Download,
    EpisodeInfo,
    Playlist,
    Playerjs,
    Share,
    Transcript,
  ]

  constructor: (@elemClass) ->
    @getConfiguration().loaded.done =>
      @externalData = new ExternalData(this)
      @renderTheme().done =>
        @initPlayer()

  extensions: {}

  getFeed: () ->
    return unless @podcast.feed

    @podcast.feed = new Feed(this)

  getProductionData: () ->
    return unless @episode.productionDataUrl

    self = @
    $.getJSON(@episode.productionDataUrl).done (data) =>
      self.episode.productionData = data.data

  getConfiguration: () ->
    new Configuration(this)

  renderTheme: =>
    rendered = $.Deferred()
    @theme = new Theme(this)
    @theme.loaded.done =>
      @elem = @theme.render()
      window.setTimeout @sendSizeChange, 0
      rendered.resolve()

    rendered.promise()

  initPlayer: =>
    mediaElem = @elem.find('audio')[0]
    new Player(this, mediaElem, @options, @init)

  init: (player) =>
    @player = player
    @bindButtons()
    @bindPlayerEvents()
    @initializeExtensions()
    @bindWindowResizing()

  mediaLoaded: =>
    window.setTimeout @sendSizeChange, 0
    @theme.removeLoadingClass()

  mediaLoadError: =>
    window.setTimeout @sendSizeChange, 0
    @theme.removeLoadingClass()
    @theme.addFailedLoadingClass()

  # initialize elements

  togglePlayState: () =>
    return unless @player?
    if @player.playing
      @elem.addClass('playing')
    else
      @elem.removeClass('playing')

  # event handlers

  bindPlayerEvents: () ->
    $(@player.media).on('timeupdate', @updateTime)
      .on('ended', @triggerEnded)
      .on('error', @triggerError)

  updateTime: () =>
    timeString = @extensions.ProgressBar.updateTime()
    @adjustPlaySpeed(timeString)

  triggerEnded: =>
    @player.media.currentTime = 0
    @extensions.ProgressBar.updateTime()
    @togglePlayState()

  triggerError: =>
    @extensions.ProgressBar.hideBuffering()

  tempPlayBackSpeed: null
  adjustPlaySpeed: (timeString) =>
    return unless @episode.productionData

    currentTime = @player.media.currentTime
    item = $.grep @episode.productionData.statistics.music_speech, (item, index) ->
      item.start.indexOf(timeString) != -1

    if item.length
      if item[0].label == 'music'
        unless @options.currentPlaybackRate == 1.0
          @tempPlayBackSpeed = @options.currentPlaybackRate
          @player.setPlaySpeed(1.0)
      else
        if @tempPlayBackSpeed
          @player.setPlaySpeed(@tempPlayBackSpeed)
          @tempPlayBackSpeed = null

    @updateSpeedDisplay()

  bindButtons: () =>
    triggerPlayPause = (event) =>
      event.preventDefault()
      if @extensions.ChromeCast && @extensions.ChromeCast.active
        @extensions.ChromeCast.togglePlayState()
      else
        @player.playPause()
    @theme.playPauseElement.on 'click', triggerPlayPause

    @theme.backwardElement.click =>
      @player.jumpBackward()

    @theme.forwardElement.click =>
      @player.jumpForward()

    @theme.speedElement.click (event) =>
      @player.changePlaySpeed()
      @updateSpeedDisplay()
{'fraction_non_alphanumeric': 0.13632901751713633, 'fraction_numerical': 0.0027925869510027927, 'mean_word_length': 3.433877321328081, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5971966', 'n_tokens_mistral': 2436, 'n_tokens_neox': 2302, 'n_words': 401}