<commit_before>Zotero.BetterBibTeX.pref.serverURL = (extension) ->
  collectionsView = Zotero.getActiveZoteroPane()?.collectionsView
  itemGroup = collectionsView?._getItemAtRow(collectionsView.selection?.currentIndex)
  return unless itemGroup

  try
    serverPort = Zotero.Prefs.get('httpServer.port')
  catch err
    return

  if itemGroup.isCollection()
    collection = collectionsView.getSelectedCollection()
    url = "collection?/#{collection.libraryID or 0}/#{collection.key + extension}"

  if itemGroup.isLibrary(true)
    libid = collectionsView.getSelectedLibraryID()
    url = if libid then "library?/#{libid}/library#{extension}" else "library?library#{extension}"
  if not url then return

  return "http://localhost:#{serverPort}/better-bibtex/#{url}"

Zotero.BetterBibTeX.pref.styleChanged = (index) ->
  stylebox = document.getElementById('better-bibtex-abbrev-style')
  selectedItem = if typeof index != 'undefined' then stylebox.getItemAtIndex(index) else stylebox.selectedItem
  styleID = selectedItem.getAttribute('value')
  Zotero.BetterBibTeX.pref.set('autoAbbrevStyle', styleID)

Zotero.BetterBibTeX.pref.clone = (obj) ->
  clone = Object.create(null)
  for own key, value of obj
    clone[key] = value
  return clone

Zotero.BetterBibTeX.pref.display = (id, text) ->
  elt = document.getElementById(id)
  elt.value = text
  elt.setAttribute('tooltiptext', text) if text != ''

Zotero.BetterBibTeX.pref.update = ->
  return unless Zotero.BetterBibTeX.initialized # ?!?!

  serverCheckbox = document.getElementById('id-better-bibtex-preferences-server-enabled')
  serverEnabled = serverCheckbox.checked
  serverCheckbox.setAttribute('hidden', Zotero.isStandalone && serverEnabled)

  keyformat = document.getElementById('id-better-bibtex-preferences-citekeyFormat')

  try
    Zotero.BetterBibTeX.formatter(keyformat.value)
    keyformat.setAttribute('style', '')
    keyformat.setAttribute('tooltiptext', '')
  catch err
    keyformat.setAttribute('style', 'color: red')
    keyformat.setAttribute('tooltiptext', '' + err)

  document.getElementById('id-better-bibtex-preferences-pin-citekeys-on-change').setAttribute('disabled', not Zotero.BetterBibTeX.allowAutoPin())
  document.getElementById('id-better-bibtex-preferences-pin-citekeys-on-export').setAttribute('disabled', not Zotero.BetterBibTeX.allowAutoPin())
  document.getElementById('id-zotero-better-bibtex-server-warning').setAttribute('hidden', serverEnabled)
  document.getElementById('id-zotero-better-bibtex-recursive-warning').setAttribute('hidden', not document.getElementById('id-better-bibtex-preferences-getCollections').checked)

  styles = (style for style in Zotero.Styles.getVisible() when style.usesAbbreviation)

  stylebox = document.getElementById('better-bibtex-abbrev-style')
  refill = stylebox.children.length == 0
  selectedStyle = Zotero.BetterBibTeX.pref.get('autoAbbrevStyle')
  selectedIndex = -1
  for style, i in styles
    if refill
      itemNode = document.createElement('listitem')
      itemNode.setAttribute('value', style.styleID)
      itemNode.setAttribute('label', style.title)
      stylebox.appendChild(itemNode)
    if style.styleID == selectedStyle then selectedIndex = i
  selectedIndex = 0 if selectedIndex == -1
  Zotero.BetterBibTeX.pref.styleChanged(selectedIndex)

  window.setTimeout((->
    stylebox.ensureIndexIsVisible(selectedIndex)
    stylebox.selectedIndex = selectedIndex
    return), 0)

  @autoexport.refresh()

Zotero.BetterBibTeX.pref.autoexport =
  selected: (index) ->
    Zotero.BetterBibTeX.debug('pref.autoexport.selected:', index)
    exportbox = document.getElementById('better-bibtex-export-list')
    selectedItem = if (typeof index) == 'undefined' then exportbox.selectedItem else exportbox.getItemAtIndex(index)

    document.getElementById('auto-export-remove').setAttribute('disabled', !selectedItem)
    document.getElementById('auto-export-mark').setAttribute('disabled', !selectedItem)

    if selectedItem
      ae = Zotero.DB.rowQuery('select * from betterbibtex.autoexport where id = ?', [selectedItem.getAttribute('value')])
    ae ||= {status: '', collection: '', path: '', translatorID: '', exportCharset: '', useJournalAbbreviation: false, exportNotes: false, preserveBibTeXVariables: false }
    Zotero.BetterBibTeX.debug('pref.autoexport.selected =', ae)

    Zotero.BetterBibTeX.pref.display('id-better-bibtex-preferences-auto-export-status', ae.status)
    name = switch
      when ae.collection == '' then ''
      when ae.collection == 'library' then Zotero.Libraries.getName() || ae.collection
      when m = /^library:([0-9]+)$/.exec(ae.collection) then Zotero.Libraries.getName(m[1]) || ae.collection
      else Zotero.Collections.get(ae.collection)?.name || "collection:#{ae.collection}"
    Zotero.BetterBibTeX.pref.display('id-better-bibtex-preferences-auto-export-collection', name)
    Zotero.BetterBibTeX.pref.display('id-better-bibtex-preferences-auto-export-target', ae.path)
    Zotero.BetterBibTeX.pref.display('id-better-bibtex-preferences-auto-export-translator', Zotero.BetterBibTeX.translatorName(ae.translatorID))
    Zotero.BetterBibTeX.pref.display('id-better-bibtex-preferences-auto-export-charset', Zotero.BetterBibTeX.translatorName(ae.exportCharset))
    document.getElementById('id-better-bibtex-preferences-auto-export-auto-abbrev').checked = (ae.useJournalAbbreviation == 'true')
    document.getElementById('id-better-bibtex-preferences-auto-export-notes').checked = (ae.exportNotes == 'true')
    document.getElementById('id-better-bibtex-preferences-auto-export-preserve-bibvars').checked = (ae.preserveBibTeXVariables == 'true')

  remove: ->
    exportlist = document.getElementById('better-bibtex-export-list')
    selectedItem = exportlist.selectedItem
    return unless selectedItem
    id = selectedItem.getAttribute('value')
    Zotero.DB.query('delete from betterbibtex.autoexport where id = ?', [id])
    @refresh(true)

  mark: ->
    exportlist = document.getElementById('better-bibtex-export-list')
    selectedItem = exportlist.selectedItem
    return unless selectedItem<commit_msg>show full collection path for auto-exports<commit_after>Zotero.BetterBibTeX.pref.serverURL = (extension) ->
  collectionsView = Zotero.getActiveZoteroPane()?.collectionsView
  itemGroup = collectionsView?._getItemAtRow(collectionsView.selection?.currentIndex)
  return unless itemGroup

  try
    serverPort = Zotero.Prefs.get('httpServer.port')
  catch err
    return

  if itemGroup.isCollection()
    collection = collectionsView.getSelectedCollection()
    url = "collection?/#{collection.libraryID or 0}/#{collection.key + extension}"

  if itemGroup.isLibrary(true)
    libid = collectionsView.getSelectedLibraryID()
    url = if libid then "library?/#{libid}/library#{extension}" else "library?library#{extension}"
  if not url then return

  return "http://localhost:#{serverPort}/better-bibtex/#{url}"

Zotero.BetterBibTeX.pref.styleChanged = (index) ->
  stylebox = document.getElementById('better-bibtex-abbrev-style')
  selectedItem = if typeof index != 'undefined' then stylebox.getItemAtIndex(index) else stylebox.selectedItem
  styleID = selectedItem.getAttribute('value')
  Zotero.BetterBibTeX.pref.set('autoAbbrevStyle', styleID)

Zotero.BetterBibTeX.pref.clone = (obj) ->
  clone = Object.create(null)
  for own key, value of obj
    clone[key] = value
  return clone

Zotero.BetterBibTeX.pref.display = (id, text) ->
  elt = document.getElementById(id)
  elt.value = text
  elt.setAttribute('tooltiptext', text) if text != ''

Zotero.BetterBibTeX.pref.update = ->
  return unless Zotero.BetterBibTeX.initialized # ?!?!

  serverCheckbox = document.getElementById('id-better-bibtex-preferences-server-enabled')
  serverEnabled = serverCheckbox.checked
  serverCheckbox.setAttribute('hidden', Zotero.isStandalone && serverEnabled)

  keyformat = document.getElementById('id-better-bibtex-preferences-citekeyFormat')

  try
    Zotero.BetterBibTeX.formatter(keyformat.value)
    keyformat.setAttribute('style', '')
    keyformat.setAttribute('tooltiptext', '')
  catch err
    keyformat.setAttribute('style', 'color: red')
    keyformat.setAttribute('tooltiptext', '' + err)

  document.getElementById('id-better-bibtex-preferences-pin-citekeys-on-change').setAttribute('disabled', not Zotero.BetterBibTeX.allowAutoPin())
  document.getElementById('id-better-bibtex-preferences-pin-citekeys-on-export').setAttribute('disabled', not Zotero.BetterBibTeX.allowAutoPin())
  document.getElementById('id-zotero-better-bibtex-server-warning').setAttribute('hidden', serverEnabled)
  document.getElementById('id-zotero-better-bibtex-recursive-warning').setAttribute('hidden', not document.getElementById('id-better-bibtex-preferences-getCollections').checked)

  styles = (style for style in Zotero.Styles.getVisible() when style.usesAbbreviation)

  stylebox = document.getElementById('better-bibtex-abbrev-style')
  refill = stylebox.children.length == 0
  selectedStyle = Zotero.BetterBibTeX.pref.get('autoAbbrevStyle')
  selectedIndex = -1
  for style, i in styles
    if refill
      itemNode = document.createElement('listitem')
      itemNode.setAttribute('value', style.styleID)
      itemNode.setAttribute('label', style.title)
      stylebox.appendChild(itemNode)
    if style.styleID == selectedStyle then selectedIndex = i
  selectedIndex = 0 if selectedIndex == -1
  Zotero.BetterBibTeX.pref.styleChanged(selectedIndex)

  window.setTimeout((->
    stylebox.ensureIndexIsVisible(selectedIndex)
    stylebox.selectedIndex = selectedIndex
    return), 0)

  @autoexport.refresh()

Zotero.BetterBibTeX.pref.autoexport =
  selected: (index) ->
    Zotero.BetterBibTeX.debug('pref.autoexport.selected:', index)
    exportbox = document.getElementById('better-bibtex-export-list')
    selectedItem = if (typeof index) == 'undefined' then exportbox.selectedItem else exportbox.getItemAtIndex(index)

    document.getElementById('auto-export-remove').setAttribute('disabled', !selectedItem)
    document.getElementById('auto-export-mark').setAttribute('disabled', !selectedItem)

    if selectedItem
      ae = Zotero.DB.rowQuery('select * from betterbibtex.autoexport where id = ?', [selectedItem.getAttribute('value')])
    ae ||= {status: '', collection: '', path: '', translatorID: '', exportCharset: '', useJournalAbbreviation: false, exportNotes: false, preserveBibTeXVariables: false }
    Zotero.BetterBibTeX.debug('pref.autoexport.selected =', ae)

    Zotero.BetterBibTeX.pref.display('id-better-bibtex-preferences-auto-export-status', ae.status)
    name = switch
      when ae.collection == '' then ''
      when ae.collection == 'library' then Zotero.Libraries.getName() || ae.collection
      when m = /^library:([0-9]+)$/.exec(ae.collection) then Zotero.Libraries.getName(m[1]) || ae.collection
      else @collectionPath(ae.collection) || "collection:#{ae.collection}"
    Zotero.BetterBibTeX.pref.display('id-better-bibtex-preferences-auto-export-collection', name)
    Zotero.BetterBibTeX.pref.display('id-better-bibtex-preferences-auto-export-target', ae.path)
    Zotero.BetterBibTeX.pref.display('id-better-bibtex-preferences-auto-export-translator', Zotero.BetterBibTeX.translatorName(ae.translatorID))
    Zotero.BetterBibTeX.pref.display('id-better-bibtex-preferences-auto-export-charset', Zotero.BetterBibTeX.translatorName(ae.exportCharset))
    document.getElementById('id-better-bibtex-preferences-auto-export-auto-abbrev').checked = (ae.useJournalAbbreviation == 'true')
    document.getElementById('id-better-bibtex-preferences-auto-export-notes').checked = (ae.exportNotes == 'true')
    document.getElementById('id-better-bibtex-preferences-auto-export-preserve-bibvars').checked = (ae.preserveBibTeXVariables == 'true')

  remove: ->
    exportlist = document.getElementById('better-bibtex-export-list')
    selectedItem = exportlist.selectedItem
    return unless selectedItem
    id = selectedItem.getAttribute('value')
    Zotero.DB.query('delete from betterbibtex.autoexport where id = ?', [id])
    @refresh(true)

  mark: ->
    exportlist = document.getElementById('better-bibtex-export-list')
    selectedItem = exportlist.selectedItem
    return unless selectedItem
{'fraction_non_alphanumeric': 0.13268979057591623, 'fraction_numerical': 0.0014725130890052356, 'mean_word_length': 5.118618618618618, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '9583', 'n_tokens_mistral': 3894, 'n_tokens_neox': 3635, 'n_words': 556}