summaryrefslogtreecommitdiffstats
path: root/comm/suite/mailnews/components/compose/content/prefs/pref-formatting.js
blob: b5c31d424d8c2223a0cf64711ab5044e88d3f9a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var gListbox;
var gPref;
var gError;

function Startup()
{
  // Store some useful elements in globals.
  gListbox =
  {
    html:      document.getElementById("html_domains"),
    plaintext: document.getElementById("plaintext_domains")
  };
  gPref =
  {
    html_domains:      document.getElementById("mailnews.html_domains"),
    plaintext_domains: document.getElementById("mailnews.plaintext_domains")
  };
  gError = document.getElementById("formatting_error_msg");

  // Make it easier to access the pref pane from onsync.
  gListbox.html.pane = this;
  gListbox.plaintext.pane = this;
}

function AddDomain(aType)
{
  var domains = null;
  var result = {value: null};
  if (Services.prompt.prompt(window, gListbox[aType].getAttribute("title"),
                             gListbox[aType].getAttribute("msg"), result,
                             null, {value: 0}))
    domains = result.value.replace(/ /g, "").split(",");

  if (domains)
  {
    var added = false;
    var removed = false;
    var listbox = gListbox[aType];
    var other = aType == "html" ? gListbox.plaintext : gListbox.html;
    for (var i = 0; i < domains.length; i++)
    {
      var domainName = TidyDomainName(domains[i], true);
      if (domainName)
      {
        if (!DomainFirstMatch(listbox, domainName))
        {
          var match = DomainFirstMatch(other, domainName);
          if (match)
          {
            match.remove();
            removed = true;
          }
          listbox.appendItem(domainName);
          added = true;
        }
      }
    }
    if (added)
      listbox.doCommand();
    if (removed)
      other.doCommand();
  }
}

function TidyDomainName(aDomain, aWarn)
{
  // See if it is an email address and if so take just the domain part.
  aDomain = aDomain.replace(/.*@/, "");

  // See if it is a valid domain otherwise return null.
  if (!/.\../.test(aDomain))
  {
    if (aWarn)
    {
      var errorMsg = gError.getAttribute("inverr").replace(/@string@/, aDomain);
      Services.prompt.alert(window, gError.getAttribute("title"), errorMsg);
    }
    return null;
  }

  // Finally make sure the domain is in lowercase.
  return aDomain.toLowerCase();
}

function DomainFirstMatch(aListbox, aDomain)
{
  return aListbox.getElementsByAttribute("label", aDomain).item(0);
}

function RemoveDomains(aType, aEvent)
{
  if (aEvent && aEvent.keyCode != KeyEvent.DOM_VK_DELETE &&
      aEvent.keyCode != KeyEvent.DOM_VK_BACK_SPACE)
    return;

  var nextNode = null;
  var listbox = gListbox[aType];

  while (listbox.selectedItem)
  {
    var selectedNode = listbox.selectedItem;
    nextNode = selectedNode.nextSibling || selectedNode.previousSibling;
    selectedNode.remove();
  }

  if (nextNode)
    listbox.selectItem(nextNode);

  listbox.doCommand();
}

function ReadDomains(aListbox)
{
  var arrayOfPrefs = gPref[aListbox.id].value.replace(/ /g, "").split(",");
  if (arrayOfPrefs)
  {
    var i;
    // Check all the existing items, remove any that are not needed and
    // make sure we do not duplicate any by removing from pref array.
    var domains = aListbox.getElementsByAttribute("label", "*");
    if (domains)
    {
      for (i = domains.length; --i >= 0; )
      {
        var domain = domains[i];
        var index = arrayOfPrefs.indexOf(domain.label);
        if (index > -1)
          arrayOfPrefs.splice(index, 1);
        else
          domain.remove();
      }
    }
    for (i = 0; i < arrayOfPrefs.length; i++)
    {
      var str = TidyDomainName(arrayOfPrefs[i], false);
      if (str)
        aListbox.appendItem(str);
    }
  }
}

function WriteDomains(aListbox)
{
  var domains = aListbox.getElementsByAttribute("label", "*");
  return Array.from(domains, e => e.label).join(",");
}