<commit_before>define (require) ->
  $ = require('jquery')
  router = require('cs!router')
  linksHelper = require('cs!helpers/links')
  BaseView = require('cs!helpers/backbone/views/base')
  ContentsView = require('cs!../tabs/contents/contents')
  template = require('hbs!./nav-template')
  require('less!./nav')

  return class MediaNavView extends BaseView
    template: template
    templateHelpers: () ->
      page = @model.getPageNumber()
      nextPage = @model.getNextPageNumber()
      previousPage = @model.getPreviousPageNumber()

      $('link[rel="next"]').remove()
      if page isnt nextPage
        next = linksHelper.getPath('contents', {model: @model, page: nextPage})
        if next isnt undefined
          $('head').append("<link rel=\"next\" href=\"#{location.host}#{next}\" />") if next

      $('link[rel="prev"]').remove()
      if page isnt previousPage
        back = linksHelper.getPath('contents', {model: @model, page: previousPage})
        if back isnt undefined
          $('head').append("<link rel=\"prev\" href=\"#{location.host}#{back}\" />") if back

      return {
        _hideProgress: @hideProgress
        book: @model.isBook()
        next: next
        back: back
        pages: if @model.get('loaded') then @model.getTotalPages() else 0
        page: if @model.get('loaded') then @model.getPageNumber() else 0
      }

    initialize: (options) ->
      super()
      @hideProgress = options.hideProgress
      @mediaParent = options.mediaParent
      @tocIsOpen = false
      @.trigger('tocIsOpen', @tocIsOpen)
      @listenTo(@model, 'change:loaded change:currentPage removeNode moveNode add:contents', @render)
      @listenTo(@model, 'change:currentPage', @closeContentsOnSmallScreen)

    events:
      'click .next': 'nextPage'
      'click .back': 'previousPage'
      'click .toggle.btn': 'toggleContents'
      'click .back-to-top > a': 'backToTop'

    toggleContents: (e) ->
      @tocIsOpen = not @tocIsOpen
      @.trigger('tocIsOpen', @tocIsOpen)
      @updateToc()

    updateToc: ->
      button = @$el.find('.toggle.btn')
      if (@tocIsOpen)
        button.addClass('open')
      else
        button.removeClass('open')

    nextPage: (e) ->
      nextPage = @model.getNextPageNumber()
      # Show the next page if there is one
      @changePage(e)
      @model.setPage(nextPage)

    previousPage: (e) ->
      previousPage = @model.getPreviousPageNumber()
      # Show the previous page if there is one
      @changePage(e)
      @model.setPage(previousPage)

    changePage: (e) ->
      # Don't intercept cmd/ctrl-clicks intended to open a link in a new tab
      return if e.metaKey or e.which isnt 1

      e.preventDefault()
      e.stopPropagation()
      href = $(e.currentTarget).attr('href')
      router.navigate href, {trigger: false}, () => @mediaParent.trackAnalytics()
      @mediaParent.scrollToTop()

    closeContentsOnSmallScreen: ->
      if window.innerWidth < 640
        if @tocIsOpen
          @toggleContents()
      @mediaParent.scrollToTop()

    backToTop: (e) ->
      e.preventDefault()
      @mediaParent.scrollToTop()

    onRender: ->
      if not @mediaParent?
        @mediaParent = @parent
      @regions.tocPanel?.show(new ContentsView({model: @model}))
      @updateToc()<commit_msg>Indent error<commit_after>define (require) ->
  $ = require('jquery')
  router = require('cs!router')
  linksHelper = require('cs!helpers/links')
  BaseView = require('cs!helpers/backbone/views/base')
  ContentsView = require('cs!../tabs/contents/contents')
  template = require('hbs!./nav-template')
  require('less!./nav')

  return class MediaNavView extends BaseView
    template: template
    templateHelpers: () ->
      page = @model.getPageNumber()
      nextPage = @model.getNextPageNumber()
      previousPage = @model.getPreviousPageNumber()

      $('link[rel="next"]').remove()
      if page isnt nextPage
        next = linksHelper.getPath('contents', {model: @model, page: nextPage})
        if next isnt undefined
          $('head').append("<link rel=\"next\" href=\"#{location.host}#{next}\" />") if next

      $('link[rel="prev"]').remove()
      if page isnt previousPage
        back = linksHelper.getPath('contents', {model: @model, page: previousPage})
        if back isnt undefined
          $('head').append("<link rel=\"prev\" href=\"#{location.host}#{back}\" />") if back

      return {
        _hideProgress: @hideProgress
        book: @model.isBook()
        next: next
        back: back
        pages: if @model.get('loaded') then @model.getTotalPages() else 0
        page: if @model.get('loaded') then @model.getPageNumber() else 0
      }

    initialize: (options) ->
      super()
      @hideProgress = options.hideProgress
      @mediaParent = options.mediaParent
      @tocIsOpen = false
      @.trigger('tocIsOpen', @tocIsOpen)
      @listenTo(@model, 'change:loaded change:currentPage removeNode moveNode add:contents', @render)
      @listenTo(@model, 'change:currentPage', @closeContentsOnSmallScreen)

    events:
      'click .next': 'nextPage'
      'click .back': 'previousPage'
      'click .toggle.btn': 'toggleContents'
      'click .back-to-top > a': 'backToTop'

    toggleContents: (e) ->
      @tocIsOpen = not @tocIsOpen
      @.trigger('tocIsOpen', @tocIsOpen)
      @updateToc()

    updateToc: ->
      button = @$el.find('.toggle.btn')
      if (@tocIsOpen)
        button.addClass('open')
      else
        button.removeClass('open')

    nextPage: (e) ->
      nextPage = @model.getNextPageNumber()
      # Show the next page if there is one
      @changePage(e)
      @model.setPage(nextPage)

    previousPage: (e) ->
      previousPage = @model.getPreviousPageNumber()
      # Show the previous page if there is one
      @changePage(e)
      @model.setPage(previousPage)

    changePage: (e) ->
      # Don't intercept cmd/ctrl-clicks intended to open a link in a new tab
      return if e.metaKey or e.which isnt 1

      e.preventDefault()
      e.stopPropagation()
      href = $(e.currentTarget).attr('href')
      router.navigate href, {trigger: false}, () => @mediaParent.trackAnalytics()
      @mediaParent.scrollToTop()

    closeContentsOnSmallScreen: ->
      if window.innerWidth < 640
        if @tocIsOpen
          @toggleContents()
        @mediaParent.scrollToTop()

    backToTop: (e) ->
      e.preventDefault()
      @mediaParent.scrollToTop()

    onRender: ->
      if not @mediaParent?
        @mediaParent = @parent
      @regions.tocPanel?.show(new ContentsView({model: @model}))
      @updateToc()
{'fraction_non_alphanumeric': 0.1446588664336727, 'fraction_numerical': 0.001823431089500076, 'mean_word_length': 2.8046242774566474, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 33, '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': '5513457', 'n_tokens_mistral': 2023, 'n_tokens_neox': 1972, 'n_words': 446}