File: options_ui_listeners.js

package info (click to toggle)
allow-html-temp 10.0.8-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 368 kB
  • sloc: javascript: 1,454; makefile: 25; sh: 7
file content (169 lines) | stat: -rw-r--r-- 6,602 bytes parent folder | download | duplicates (2)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// onLoad listener to load the i18n locale strings
document.addEventListener('DOMContentLoaded', () => {
  i18n.updateDocument();
}, { once: true });

// onLoad listener to load Options and LegacyPrefs
document.addEventListener('DOMContentLoaded', async () => {
  consoleDebug("AHT: DOMContentLoaded, restoreAllOptions() and initLegacyPrefs()");
  restoreAllOptions();
  initLegacyPrefs();

  await messenger.messageContentPolicy.onChanged.addListener(async (newValue) => {
    consoleDebug("AHT: messageContentPolicy newValue:", newValue);
    await initLegacyPrefs();
  });

  document.addEventListener("visibilitychange", async () => {
    consoleDebug("AHT: DOMContent beforeunload");
    await messenger.messageContentPolicy.onChanged.removeListener(async () => {
      await initLegacyPrefs();
    });
  });
});

// onChange listener for all options to save the changed options
OptionsList.forEach((option) => {
  consoleDebug("AHT: OptionsList.forEach(): option: " + option);
  switch(option) {
    case "buttonHtmlMode":
      document.getElementById("buttonMode_html").addEventListener("change", (e) => {
        consoleDebug("AHT: OptionsList.forEach():  Option buttonHtmlMode changed, saveOptions()");
        saveOptions(e);
      });
      document.getElementById("buttonMode_sanitized").addEventListener("change", (e) => {
        consoleDebug("AHT: OptionsList.forEach():  Option buttonHtmlMode changed, saveOptions()");
        saveOptions(e);
      });
      document.getElementById("buttonMode_plaintext").addEventListener("change", (e) => {
        consoleDebug("AHT: OptionsList.forEach():  Option buttonHtmlMode changed, saveOptions()");
        saveOptions(e);
      });
      break;

    case "commandKey":
      document.getElementById("commandKey").addEventListener("change", async (e) => {
        consoleDebug("AHT: OptionsList.forEach():  Option commandKey changed, saveOptions()");
        if(testCommandKey(document.getElementById("commandKey").value) == true) {
          showCommandKeyWarning(false);
          saveOptions(e);
        } else {
          showCommandKeyWarning(true);
          saveOptions(e);
        }
      });
      break;

    default:
      document.getElementById(option).addEventListener("change", (e) => {
        consoleDebug("AHT: OptionsList.forEach():  Option " + option + " changed, saveOptions()");
        saveOptions(e);
        // .then(() => { enableOrDisableOptionUiElements(); });
      });
  }
});

// onChange listener for all options to save the related about:config prefs
PrefsList.forEach((option) => {
  consoleDebug("AHT: pref: " + option);
  switch(option) {
    case "appHtmlMode":
      document.getElementById("appMode_html").addEventListener("change", (e) => {
        consoleDebug("AHT: PrefsList.forEach():  Option appHtmlMode (original HTML) changed, setPrefsMsgBodyAllowHTML()");
        setPrefsMsgBodyAllowHTML();
        prefChangedWarning("warning_appHtmlMode");
      });
      document.getElementById("appMode_sanitized").addEventListener("change", (e) => {
        consoleDebug("AHT: PrefsList.forEach():  Option appHtmlMode (sanitized HTML) changed, setPrefsMsgBodySanitized()");
        setPrefsMsgBodySanitized();
        prefChangedWarning("warning_appHtmlMode");
      });
      document.getElementById("appMode_plaintext").addEventListener("change", (e) => {
        consoleDebug("AHT: PrefsList.forEach():  Option appHtmlMode (plaintext) changed, setPrefsMsgBodyAsPlaintext()");
        setPrefsMsgBodyAsPlaintext();
        prefChangedWarning("warning_appHtmlMode");
      });
      document.getElementById("appMode_allBodyParts").addEventListener("change", (e) => {
        consoleDebug("AHT: PrefsList.forEach():  Option appHtmlMode (AllBodyParts) changed, setPrefsMsgBodyAllParts()");
        setPrefsMsgBodyAllParts();
        prefChangedWarning("warning_appHtmlMode");
      });
      break;

    case "appRemoteContent":
      document.getElementById("appRemoteContent").addEventListener("change", (e) => {
        consoleDebug("AHT: PrefsList.forEach():  Option appRemoteContent changed, setPrefsAppRemoteContent()");
        setPrefsAppRemoteContent();
        prefChangedWarning("warning_appRemoteContent");
      });
      break;

    case "allwaysInline":
      document.getElementById("allwaysInline").addEventListener("change", (e) => {
        consoleDebug("AHT: PrefsList.forEach():  Option allwaysInline changed, setPrefsAppAttachmentsInline()");
        setPrefsAppAttachmentsInline();
        prefChangedWarning("warning_allwaysInline");
      });
      break;
  }
});

// resetToDefault click listener
document.getElementById("resetToDefault").addEventListener("click", () => {
  consoleDebug("AHT: Reset clicked, resetToDefault()");
  resetToDefault();
  showCommandKeyWarning(false);
});

// resetToRecommended click listener
document.getElementById("resetToRecommended").addEventListener("click", () => {
  consoleDebug("AHT: Reset clicked, resetToRecommended()");
  resetToRecommended();
  prefChangedWarning("warning_appHtmlMode");
  prefChangedWarning("warning_appRemoteContent");
  prefChangedWarning("warning_allwaysInline");
  showCommandKeyWarning(false);
});

function prefChangedWarning (elementId) {
  let element = document.getElementById(elementId);
  if (element.classList.contains("hidden")) {
    element.classList.remove("hidden");
    consoleDebug("AHT: options_ui_listeners.js: prefChangedWarning: element.classList.remove");
  }
}

function testCommandKey(testValue) {
  consoleDebug("AHT: testCommandKey: " + testValue);

  let detail = {};
  detail.name = "_execute_message_display_action";
  detail.shortcut = testValue;

  if(detail.shortcut === "") {
    return false;
  }

  try {
    messenger.commands.update(detail)
    return true;
  } catch (e) {
    return false;
  }
}

function showCommandKeyWarning(doWarn) {
  if(doWarn) {
    let element = document.getElementById("warning_optionCommandKey");
    if (element.classList.contains("hidden")) {
      element.classList.remove("hidden");
      consoleDebug("AHT: options_ui_listeners.js: warning_optionCommandKey: element.classList.remove");
    }
  } else {
    let element = document.getElementById("warning_optionCommandKey");
    if (!element.classList.contains("hidden")) {
      element.classList.add("hidden");
      consoleDebug("AHT: options_ui_listeners.js: warning_optionCommandKey: element.classList.add");
    }
  }
}