summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/content/menulist-charsetpicker.js
blob: eb354621a7a031684b83dcd2359a9726161f7e9f (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
/* 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 https://mozilla.org/MPL/2.0/. */

// The menulist CE is defined lazily. Create one now to get menulist defined,
// allowing us to inherit from it.
if (!customElements.get("menulist")) {
  delete document.createXULElement("menulist");
}

// Wrap in a block to prevent leaking to window scope.
{
  /**
   * MozMenulistCharsetpicker is a menulist widget that is automatically
   * populated with charset selections.
   *
   * @augments {MozMenuList}
   */
  class MozMenulistCharsetpickerViewing extends customElements.get("menulist") {
    /**
     * Get the charset values to show in the list.
     *
     * @abstract
     * @returns {string[]} an array of character encoding names
     */
    get charsetValues() {
      return [
        "UTF-8",
        "Big5",
        "EUC-KR",
        "gbk",
        "KOI8-R",
        "ISO-2022-JP",
        "ISO-8859-1",
        "ISO-8859-2",
        "ISO-8859-7",
        "windows-874",
        "windows-1250",
        "windows-1251",
        "windows-1252",
        "windows-1255",
        "windows-1256",
        "windows-1257",
        "windows-1258",
      ];
    }

    connectedCallback() {
      super.connectedCallback();
      if (this.delayConnectedCallback()) {
        return;
      }

      if (this.menupopup) {
        return;
      }

      let charsetBundle = Services.strings.createBundle(
        "chrome://messenger/locale/charsetTitles.properties"
      );
      this.charsetValues
        .map(item => {
          let strCharset = charsetBundle.GetStringFromName(
            item.toLowerCase() + ".title"
          );
          return { label: strCharset, value: item };
        })
        .sort((a, b) => {
          if (a.value == "UTF-8" || a.label < b.label) {
            return -1;
          } else if (b.value == "UTF-8" || a.label > b.label) {
            return 1;
          }
          return 0;
        })
        .forEach(item => {
          this.appendItem(item.label, item.value);
        });
    }
  }
  customElements.define(
    "menulist-charsetpicker-viewing",
    MozMenulistCharsetpickerViewing,
    { extends: "menulist" }
  );
}