<commit_before>define (require) ->
  $ = require('jquery')
  linksHelper = require('cs!helpers/links')
  router = require('cs!router')
  analytics = require('cs!helpers/handlers/analytics')
  Content = require('cs!models/content')
  BaseView = require('cs!helpers/backbone/views/base')
  MainPageView = require('cs!modules/main-page/main-page')

  ContentsView = require('cs!modules/media/tabs/contents/contents')

  MediaEndorsedView = require('cs!./endorsed/endorsed')
  LatestView = require('cs!./latest/latest')
  MediaTitleView = require('cs!./title/title')
  MediaNavView = require('cs!./nav/nav')
  MediaHeaderView = require('cs!./header/header')
  WindowWithSidebarView = require('cs!modules/window-with-sidebar/window-with-sidebar')
  MediaBodyView = require('cs!./body/body')
  MediaFooterView = require('cs!./footer/footer')

  template = require('hbs!./media-template')
  require('less!./media')

  return class MediaView extends BaseView
    key = []
    canonical: () ->
      uuid = @model.getUuid()
      if uuid
        return "//#{location.host}/contents/#{uuid}/"
      else
        return null

    template: template
    regions:
      media: '.media'
      pinnable: '.pinnable'
      editbar: '.editbar'

    summary:() -> @updateSummary()
    description: () -> @updateDescription()

    events:
      'keydown .media-title > .title input': 'checkKeySequence'
      'keyup .media-title > .title input': 'resetKeySequence'

    initialize: (options) ->
      super()

      if not options or not options.uuid
        throw new Error('A media view must be instantiated with the uuid of the media to display')

      @uuid = options.uuid
      @model = new Content({id: @uuid, version: options.version, page: options.page})
      @minimal = options.minimal

      @listenTo(@model, 'change:googleAnalytics', @trackAnalytics)
      @listenTo(@model, 'change:title change:parent.id', @updatePageInfo)
      if not @minimal
        @listenTo(@model, 'change:legacy_id change:legacy_version change:currentPage
          change:currentPage.loaded', @updateLegacyLink)
      @listenTo(@model, 'change:error', @displayError)
      @listenTo(@model, 'change:editable', @toggleEditor)
      @listenTo(@model, 'change:title change:currentPage change:currentPage.loaded', @updateUrl)
      @listenTo(@model, 'change:title change:currentPage change:currentPage.loaded', @updatePageInfo)
      @listenTo(@model, 'change:abstract', @updateSummary)

    onRender: () =>
      @regions.media.append(new MediaEndorsedView({model: @model}))
      @regions.media.append(new LatestView({model: @model}))
      mediaTitleView = new MediaTitleView({model: @model})
      navView = new MediaNavView({model: @model})
      windowWithSidebar = new WindowWithSidebarView()
      tocView = new ContentsView({model: @model})
      footerNav = new MediaNavView({model: @model, hideProgress: true, mediaParent: @})
      @regions.pinnable.append(mediaTitleView)
      @regions.pinnable.append(navView)
      @regions.media.append(windowWithSidebar)
      mainPage = new MainPageView()
      windowWithSidebar.regions.main.append(mainPage)
      mainPage.regions.main.append(new MediaHeaderView({model: @model}))
      windowWithSidebar.regions.sidebar.append(tocView)
      mediaBodyView = new MediaBodyView({model: @model})
      mainPage.regions.main.append(mediaBodyView)
      mainPage.regions.main.append(new MediaFooterView({model: @model}))
      mainPage.regions.main.append(footerNav)
      footerNav.$el.addClass('footer-nav')
      @mainContent = windowWithSidebar.regions.main

      $pinnable = @regions.pinnable.$el
      pinnableTop = $pinnable.offset().top
      $titleArea = -> mediaTitleView.$el.find('.media-title')
      $toc = tocView.$el
      isPinned = false
      setTocHeight = ->
        tocTop = $pinnable.height()
        if not isPinned
          tocTop += $pinnable.offset().top
        $toc.css('top', "#{tocTop}px")
        newHeight = window.innerHeight - tocTop
        $toc.height("#{newHeight}px")
      adjustMainMargin = (height) ->
        mainPage.regions.main.$el.css('margin-top', "#{height}px")
      pinNavBar = ->
        $pinnable.addClass('pinned')
        $titleArea().addClass('compact')
        $toc.addClass('pinned')
        isPinned = true
        adjustMainMargin($pinnable.height())
      unpinNavBar = ->
        $pinnable.removeClass('pinned')
        $titleArea().removeClass('compact')
        $toc.removeClass('pinned')
        isPinned = false
        adjustMainMargin(0)
        pinnableTop = $pinnable.offset().top
      mediaTitleView.on('render', ->
        if isPinned
          $titleArea().addClass('compact')
      )

      Backbone.on('window:optimizedResize', setTocHeight)
      handleHeaderViewPinning = ->
        top = $(window).scrollTop()
        if top > pinnableTop
          if not isPinned
            pinNavBar()
        else if isPinned
          unpinNavBar()
        setTocHeight()

      Backbone.on('window:optimizedScroll', handleHeaderViewPinning)
      navView.on('tocIsOpen', (whether) ->
        windowWithSidebar.open(whether)
        # On small screens, when the contents is opened,
        # auto-scroll to make header minimize
        if window.innerWidth < 640 and whether
          top = $(window).scrollTop()
          if top < pinnableTop
            $(window).scrollTop(pinnableTop + 10)
        setTocHeight()
        )
      wasPinnedAtChange = false
      @model.on('change:currentPage', ->
        wasPinnedAtChange = isPinned
      )
      mediaBodyView.on('render', ->
        scrollTo = if wasPinnedAtChange then pinnableTop + 1 else 0
        $(window).scrollTop(scrollTo)
      )

    updateSummary: () ->
      abstract = @model.get('abstract')
      if abstract
        return $("<div>#{abstract}</div>").text()
      else<commit_msg>Remove resize/scroll callbacks when the view closes<commit_after>define (require) ->
  $ = require('jquery')
  linksHelper = require('cs!helpers/links')
  router = require('cs!router')
  analytics = require('cs!helpers/handlers/analytics')
  Content = require('cs!models/content')
  BaseView = require('cs!helpers/backbone/views/base')
  MainPageView = require('cs!modules/main-page/main-page')

  ContentsView = require('cs!modules/media/tabs/contents/contents')

  MediaEndorsedView = require('cs!./endorsed/endorsed')
  LatestView = require('cs!./latest/latest')
  MediaTitleView = require('cs!./title/title')
  MediaNavView = require('cs!./nav/nav')
  MediaHeaderView = require('cs!./header/header')
  WindowWithSidebarView = require('cs!modules/window-with-sidebar/window-with-sidebar')
  MediaBodyView = require('cs!./body/body')
  MediaFooterView = require('cs!./footer/footer')

  template = require('hbs!./media-template')
  require('less!./media')

  return class MediaView extends BaseView
    key = []
    canonical: () ->
      uuid = @model.getUuid()
      if uuid
        return "//#{location.host}/contents/#{uuid}/"
      else
        return null

    template: template
    regions:
      media: '.media'
      pinnable: '.pinnable'
      editbar: '.editbar'

    summary:() -> @updateSummary()
    description: () -> @updateDescription()

    events:
      'keydown .media-title > .title input': 'checkKeySequence'
      'keyup .media-title > .title input': 'resetKeySequence'

    initialize: (options) ->
      super()

      if not options or not options.uuid
        throw new Error('A media view must be instantiated with the uuid of the media to display')

      @uuid = options.uuid
      @model = new Content({id: @uuid, version: options.version, page: options.page})
      @minimal = options.minimal

      @listenTo(@model, 'change:googleAnalytics', @trackAnalytics)
      @listenTo(@model, 'change:title change:parent.id', @updatePageInfo)
      if not @minimal
        @listenTo(@model, 'change:legacy_id change:legacy_version change:currentPage
          change:currentPage.loaded', @updateLegacyLink)
      @listenTo(@model, 'change:error', @displayError)
      @listenTo(@model, 'change:editable', @toggleEditor)
      @listenTo(@model, 'change:title change:currentPage change:currentPage.loaded', @updateUrl)
      @listenTo(@model, 'change:title change:currentPage change:currentPage.loaded', @updatePageInfo)
      @listenTo(@model, 'change:abstract', @updateSummary)

    onRender: () =>
      @regions.media.append(new MediaEndorsedView({model: @model}))
      @regions.media.append(new LatestView({model: @model}))
      mediaTitleView = new MediaTitleView({model: @model})
      navView = new MediaNavView({model: @model})
      windowWithSidebar = new WindowWithSidebarView()
      tocView = new ContentsView({model: @model})
      footerNav = new MediaNavView({model: @model, hideProgress: true, mediaParent: @})
      @regions.pinnable.append(mediaTitleView)
      @regions.pinnable.append(navView)
      @regions.media.append(windowWithSidebar)
      mainPage = new MainPageView()
      windowWithSidebar.regions.main.append(mainPage)
      mainPage.regions.main.append(new MediaHeaderView({model: @model}))
      windowWithSidebar.regions.sidebar.append(tocView)
      mediaBodyView = new MediaBodyView({model: @model})
      mainPage.regions.main.append(mediaBodyView)
      mainPage.regions.main.append(new MediaFooterView({model: @model}))
      mainPage.regions.main.append(footerNav)
      footerNav.$el.addClass('footer-nav')
      @mainContent = windowWithSidebar.regions.main

      $pinnable = @regions.pinnable.$el
      pinnableTop = $pinnable.offset().top
      $toc = tocView.$el
      isPinned = false
      setTocHeight = ->
        tocTop = $pinnable.height()
        if not isPinned
          tocTop += $pinnable.offset().top
        $toc.css('top', "#{tocTop}px")
        newHeight = window.innerHeight - tocTop
        $toc.height("#{newHeight}px")
      Backbone.on('window:optimizedResize', setTocHeight)
      @on('closing', ->
        Backbone.off('window:optimizedResize', setTocHeight)
      )

      adjustMainMargin = (height) ->
        mainPage.regions.main.$el.css('margin-top', "#{height}px")

      $titleArea = mediaTitleView.$el.find('.media-title')
      pinNavBar = ->
        $pinnable.addClass('pinned')
        $titleArea.addClass('compact')
        $toc.addClass('pinned')
        isPinned = true
        adjustMainMargin($pinnable.height())
      unpinNavBar = ->
        $pinnable.removeClass('pinned')
        $titleArea.removeClass('compact')
        $toc.removeClass('pinned')
        isPinned = false
        adjustMainMargin(0)
        pinnableTop = $pinnable.offset().top
      mediaTitleView.on('render', ->
        $titleArea = mediaTitleView.$el.find('.media-title')
        $titleArea.addClass('compact') if isPinned
      )

      handleHeaderViewPinning = ->
        top = $(window).scrollTop()
        if top > pinnableTop
          if not isPinned
            pinNavBar()
        else if isPinned
          unpinNavBar()
        setTocHeight()
      Backbone.on('window:optimizedScroll', handleHeaderViewPinning)
      @on('closing', ->
        Backbone.off('window:optimizedScroll', handleHeaderViewPinning)
      )

      navView.on('tocIsOpen', (whether) ->
        windowWithSidebar.open(whether)
        # On small screens, when the contents is opened,
        # auto-scroll to make header minimize
        if window.innerWidth < 640 and whether
          top = $(window).scrollTop()
          if top < pinnableTop
            $(window).scrollTop(pinnableTop + 10)
        setTocHeight()
        )
      wasPinnedAtChange = false
      @model.on('change:currentPage', ->
        wasPinnedAtChange = isPinned
      )
      mediaBodyView.on('render', ->
        scrollTo = if wasPinnedAtChange then pinnableTop + 1 else 0
        $(window).scrollTop(scrollTo)
      )

    close: ->
      @trigger('closing')

    updateSummary: () ->
      abstract = @model.get('abstract')
      if abstract
        return $("<div>#{abstract}</div>").text()
      else
{'fraction_non_alphanumeric': 0.13621207069023009, 'fraction_numerical': 0.001333777925975325, 'mean_word_length': 3.0612728503723763, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 49, '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': '4724929', 'n_tokens_mistral': 3668, 'n_tokens_neox': 3560, 'n_words': 653}