<commit_before>prefs = require './prefs'
{join, delay} = require './util'

# This module implements the Preferences dialog.
# The actual content of tabs is in separate modules: prefs-*.coffee
# Each of them can export the following properties:
#   name       a string used as the tab's title
#   init()     called only once, when Preferences is opened for the first time
#   load()     called every time Preferences is opened
#   validate() should return a {message,element} object on failure
#   save()     called when Save is pressed
#   resize()   called when the Preferences dialog is resized or the tab is selected
# All tabs' validate() methods are invoked, if they exist, before any attempt to call save()

tabs = [
  require './prefs-glyphs'
  require './prefs-keys'
  require './prefs-code'
  require './prefs-colours'
  require './prefs-title'
  require './prefs-menu'
]

safe = (s) -> s.toLowerCase().replace /[^a-z\-]/g, '-' # make a string suitable for a DOM id

$d = null # dialog instance, lazily initialized

ok = -> apply(); $d.dialog 'close'; false

apply = ->
  for t in tabs when v = t.validate?()
    delay 1, ->
      $.alert v.message, 'Error', if v.element then -> v.element.focus(); return
      return
    return
  for t in tabs then t.save?()
  false

@showDialog = (tabName) ->
  if !$d
    $d = $ """
      <div id=prefs>
        <ul id=prefs-tabs-nav>
          #{join tabs.map (t) -> "<li><a href=#prefs-tab-#{safe t.name}>#{t.name}</a></li>"}
        </ul>
        #{join tabs.map (t) -> "<div id=prefs-tab-#{safe t.name}></div>"}
      </div>
    """
      .tabs(activate: (e, ui) -> tabs[$(ui.newTab).index()].resize?(); return)
      .keydown (e) -> if e.which == 13 && !e.shiftKey && !e.ctrlKey && !e.altKey then ok(); false
      .on 'dragstart', -> false
      .dialog
        autoOpen: 0, title: 'Preferences', width: 600, minWidth: 600, height: 450, minHeight: 450
        resize: -> (for t in tabs when t.resize then t.resize()); return
        buttons: [
          {text: 'OK',     click: ok}
          {text: 'Apply',  click: apply}
          {text: 'Cancel', click: -> $d.dialog 'close'; return}
        ]
    for t in tabs then t.init? $ "#prefs-tab-#{safe t.name}"
  $d.dialog('option', 'position', at: 'center').dialog 'open'
  if tabName then $d.tabs active: $("#prefs-tabs-nav a[href='#prefs-tab-#{tabName}']").parent().index()
  for t in tabs then t.load?()
  return<commit_msg>Ctrl-Enter to confirm Prefs dialog<commit_after>prefs = require './prefs'
{join, delay} = require './util'

# This module implements the Preferences dialog.
# The actual content of tabs is in separate modules: prefs-*.coffee
# Each of them can export the following properties:
#   name       a string used as the tab's title
#   init()     called only once, when Preferences is opened for the first time
#   load()     called every time Preferences is opened
#   validate() should return a {message,element} object on failure
#   save()     called when Save is pressed
#   resize()   called when the Preferences dialog is resized or the tab is selected
# All tabs' validate() methods are invoked, if they exist, before any attempt to call save()

tabs = [
  require './prefs-glyphs'
  require './prefs-keys'
  require './prefs-code'
  require './prefs-colours'
  require './prefs-title'
  require './prefs-menu'
]

safe = (s) -> s.toLowerCase().replace /[^a-z\-]/g, '-' # make a string suitable for a DOM id

$d = null # dialog instance, lazily initialized

ok = -> apply(); $d.dialog 'close'; false

apply = ->
  for t in tabs when v = t.validate?()
    delay 1, ->
      $.alert v.message, 'Error', if v.element then -> v.element.focus(); return
      return
    return
  for t in tabs then t.save?()
  false

@showDialog = (tabName) ->
  if !$d
    $d = $ """
      <div id=prefs>
        <ul id=prefs-tabs-nav>
          #{join tabs.map (t) -> "<li><a href=#prefs-tab-#{safe t.name}>#{t.name}</a></li>"}
        </ul>
        #{join tabs.map (t) -> "<div id=prefs-tab-#{safe t.name}></div>"}
      </div>
    """
      .tabs(activate: (e, ui) -> tabs[$(ui.newTab).index()].resize?(); return)
      .keydown (e) -> if e.which == 13 && !e.shiftKey && e.ctrlKey && !e.altKey then ok(); false
      .on 'dragstart', -> false
      .dialog
        autoOpen: 0, title: 'Preferences', width: 600, minWidth: 600, height: 450, minHeight: 450
        resize: -> (for t in tabs when t.resize then t.resize()); return
        buttons: [
          {text: 'OK',     click: ok}
          {text: 'Apply',  click: apply}
          {text: 'Cancel', click: -> $d.dialog 'close'; return}
        ]
    for t in tabs then t.init? $ "#prefs-tab-#{safe t.name}"
  $d.dialog('option', 'position', at: 'center').dialog 'open'
  if tabName then $d.tabs active: $("#prefs-tabs-nav a[href='#prefs-tab-#{tabName}']").parent().index()
  for t in tabs then t.load?()
  return
{'fraction_non_alphanumeric': 0.16652994257588188, 'fraction_numerical': 0.006562756357670222, 'mean_word_length': 2.728593272171254, 'pattern_counts': {'":': 0, '<': 23, '<?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': '5734041', 'n_tokens_mistral': 1644, 'n_tokens_neox': 1594, 'n_words': 543}