<commit_before>class app.views.DocPicker extends app.View
  @className: '_list _list-picker'
  @attributes:
    role: 'form'

  @elements:
    saveLink: '._sidebar-footer-save'

  @events:
    click: 'onClick'

  @shortcuts:
    enter: 'onEnter'

  init: ->
    @addSubview @listFold = new app.views.ListFold(@el)
    return

  activate: ->
    if super
      @render()
      @findByTag('input')?.focus()
      app.appCache?.on 'progress', @onAppCacheProgress
      $.on @el, 'focus', @onDOMFocus, true
    return

  deactivate: ->
    if super
      @empty()
      app.appCache?.off 'progress', @onAppCacheProgress
      $.off @el, 'focus', @onDOMFocus, true
    return

  render: ->
    html = ''
    docs = app.docs.all().concat(app.disabledDocs.all()...)

    while doc = docs.shift()
      if doc.version?
        [docs, versions] = @extractVersions(docs, doc)
        html += @tmpl('sidebarVersionedDoc', doc, @renderVersions(versions), open: app.docs.contains(doc))
      else
        html += @tmpl('sidebarLabel', doc, checked: app.docs.contains(doc))

    @html html + @tmpl('sidebarPickerNote') + @tmpl('sidebarSave')
    @refreshElements()

    @delay -> # trigger animation
      @el.offsetWidth
      @addClass '_in'
    return

  renderVersions: (docs) ->
    html = ''
    html += @tmpl('sidebarLabel', doc, checked: app.docs.contains(doc)) for doc in docs
    html

  extractVersions: (originalDocs, version) ->
    docs = []
    versions = [version]
    for doc in originalDocs
      (if doc.name is version.name then versions else docs).push(doc)
    [docs, versions]

  empty: ->
    @resetClass()
    super
    return

  save: ->
    unless @saving
      @saving = true
      docs = @getSelectedDocs()
      app.settings.setDocs(docs)
      @saveLink.textContent = if app.appCache then 'Downloading\u2026' else 'Saving\u2026'
      disabledDocs = new app.collections.Docs(doc for doc in app.docs.all() when docs.indexOf(doc.slug) is -1)
      disabledDocs.uninstall ->
        app.db.migrate()
        app.reload()
    return

  getSelectedDocs: ->
    for input in @findAllByTag 'input' when input?.checked
      input.name

  onClick: (event) =>
    if @focusTimeout
      clearTimeout @focusTimeout
      @focusTimeout = null
    return if event.which isnt 1
    if event.target is @saveLink
      $.stopEvent(event)
      @save()
    return

  onDOMFocus: (event) =>
    target = event.target
    if target.tagName is 'INPUT'
      $.scrollTo target.parentNode, null, 'continuous', bottomGap: 2
    else if target.classList.contains(app.views.ListFold.targetClass)
      target.blur()
      @focusTimeout = setTimeout =>
        @listFold.open(target) unless target.classList.contains(app.views.ListFold.activeClass)
        $('input', target.nextElementSibling).focus()
        @focusTimeout = null
      , 10
    return

  onEnter: =>
    @save()
    return

  onAppCacheProgress: (event) =>
    if event.lengthComputable
      percentage = Math.round event.loaded * 100 / event.total
      @saveLink.textContent = "Downloading\u2026 (#{percentage}%)"
    return
<commit_msg>Fix clicking on folded docs in the doc picker<commit_after>class app.views.DocPicker extends app.View
  @className: '_list _list-picker'
  @attributes:
    role: 'form'

  @elements:
    saveLink: '._sidebar-footer-save'

  @events:
    click: 'onClick'
    mousedown: 'onMouseDown'

  @shortcuts:
    enter: 'onEnter'

  init: ->
    @addSubview @listFold = new app.views.ListFold(@el)
    return

  activate: ->
    if super
      @render()
      @findByTag('input')?.focus()
      app.appCache?.on 'progress', @onAppCacheProgress
      $.on @el, 'focus', @onDOMFocus, true
    return

  deactivate: ->
    if super
      @empty()
      app.appCache?.off 'progress', @onAppCacheProgress
      $.off @el, 'focus', @onDOMFocus, true
    return

  render: ->
    html = ''
    docs = app.docs.all().concat(app.disabledDocs.all()...)

    while doc = docs.shift()
      if doc.version?
        [docs, versions] = @extractVersions(docs, doc)
        html += @tmpl('sidebarVersionedDoc', doc, @renderVersions(versions), open: app.docs.contains(doc))
      else
        html += @tmpl('sidebarLabel', doc, checked: app.docs.contains(doc))

    @html html + @tmpl('sidebarPickerNote') + @tmpl('sidebarSave')
    @refreshElements()

    @delay -> # trigger animation
      @el.offsetWidth
      @addClass '_in'
    return

  renderVersions: (docs) ->
    html = ''
    html += @tmpl('sidebarLabel', doc, checked: app.docs.contains(doc)) for doc in docs
    html

  extractVersions: (originalDocs, version) ->
    docs = []
    versions = [version]
    for doc in originalDocs
      (if doc.name is version.name then versions else docs).push(doc)
    [docs, versions]

  empty: ->
    @resetClass()
    super
    return

  save: ->
    unless @saving
      @saving = true
      docs = @getSelectedDocs()
      app.settings.setDocs(docs)
      @saveLink.textContent = if app.appCache then 'Downloading\u2026' else 'Saving\u2026'
      disabledDocs = new app.collections.Docs(doc for doc in app.docs.all() when docs.indexOf(doc.slug) is -1)
      disabledDocs.uninstall ->
        app.db.migrate()
        app.reload()
    return

  getSelectedDocs: ->
    for input in @findAllByTag 'input' when input?.checked
      input.name

  onClick: (event) =>
    return if event.which isnt 1
    if event.target is @saveLink
      $.stopEvent(event)
      @save()
    return

  onMouseDown: =>
    @mouseDown = Date.now()

  onDOMFocus: (event) =>
    target = event.target
    if target.tagName is 'INPUT'
      $.scrollTo target.parentNode, null, 'continuous', bottomGap: 2
    else if target.classList.contains(app.views.ListFold.targetClass)
      target.blur()
      unless @mouseDown and @mouseDown > Date.now() - 100
        @listFold.open(target) unless target.classList.contains(app.views.ListFold.activeClass)
        $('input', target.nextElementSibling).focus()
    return

  onEnter: =>
    @save()
    return

  onAppCacheProgress: (event) =>
    if event.lengthComputable
      percentage = Math.round event.loaded * 100 / event.total
      @saveLink.textContent = "Downloading\u2026 (#{percentage}%)"
    return

{'fraction_non_alphanumeric': 0.1308967960070842, 'fraction_numerical': 0.006601191434551602, 'mean_word_length': 2.7557436517533254, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3948709', 'n_tokens_mistral': 2050, 'n_tokens_neox': 1978, 'n_words': 468}