File: options.js

package info (click to toggle)
xnote 4.6.51-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 960 kB
  • sloc: javascript: 7,147; makefile: 22; sh: 9
file content (167 lines) | stat: -rw-r--r-- 5,436 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
import '/extlib/l10n.js';

//The object only works if retrieved through chrome.extension, but not
//through browser.extension or messenger.extension
var bgPage = chrome.extension.getBackgroundPage();
if (bgPage== null) console.error("no bgr page is found in options.js");

var prefs = bgPage.getPreferences();

var btnSelectStoragePath = document.getElementById("btnSelectStoragePath");

var btnHlp = document.getElementById("btnHlp");

function isInputType(node, type) {
  return node.nodeName.toLowerCase() == "input" && node.type.toLowerCase() == type.toLowerCase();
}

async function selectStoragePath() {
  //  console.debug("selectStoragePath called");
  let startDir = prefs.storage_path;
  let profileDir = await bgPage.getProfileDirectory();
  if (startDir.startsWith("[ProfD]")) {
    try {
      //     console.debug(`profileDir: ${profileDir}; startDir: ${startDir}`);
      startDir = await bgPage.appendRelativePath(profileDir, startDir.substring(7));
      //     console.debug(`startDir for selectStoragePath: ${startDir}`);
    }
    catch (e) {
      //     console.debug(`Directory does not exist: ${startDir}.`, e);
      startDir = profileDir;
    }
  }
  try {
    bgPage.selectDirectory(startDir, bgPage.browser.i18n.getMessage("Select.storage.dir")).then((storagePath) => {
      //     console.debug(`selected storage path: ${storagePath}`);
      if (storagePath == null) return;
      //Check whether the new path is inside the profile directory
      //and if yes, make the path relative to the profile.
      if (storagePath.indexOf(profileDir) == 0) {
        if (storagePath.length == profileDir.length) {
          storagePath = "[ProfD]";
        }
        else {
          storagePath = "[ProfD]" + storagePath.substr(profileDir.length + 1);
        }
      }
      let prefPath = document.getElementById("storage.path");
      prefPath.value = storagePath;
      prefs.storage_path = storagePath;
    });
  }
  catch (e) {
    console.error(e);
  }
}

async function savePrefs() {
  const storagePathChanged = document.getElementById('storage.path').value != prefs.storage_path;
  for (const node of document.querySelectorAll('[data-preference]')) {
    const pref = node.dataset.preference;
    //console.debug(`Saving preference: ${pref}`);
    if (pref.startsWith("tag.")) {
      switch (pref) {
        case "tag.color":
          bgPage.setTbPref("mailnews.tags.xnote.color", node.value);
          break;
        case "tag.name":
          bgPage.setTbPref("mailnews.tags.xnote.tag", node.value);
          break;
        default:
          console.error(`Unknown tag preference ${pref}`);
      }
    }
    else {
      switch (node.nodeName) {
        case "SELECT":
          for (let option of node.querySelectorAll("option")) {
            if (option.selected) {
              prefs[pref] = node.value;
              break;
            }
          }
          break;
        case "INPUT":
          if (isInputType(node, "checkbox")) {
            //          debugger;
            prefs[pref] = node.checked;
          } else if (isInputType(node, "radio") && node.checked) {
            prefs[pref] = node.value;
          } else if (isInputType(node, "number")) {
            prefs[pref] = parseInt(node.value);
          }
          else {
            prefs[pref] = node.value;
          }
          break;
        default:
          console.error(`Unknown node type ${node.nodeName}`);
          console.error(node);
      }
    }
  };
//  console.log("prefs", prefs);
  bgPage.setPreferences(prefs);
}

async function initOptions() {
  //console.debug(prefs);

  for (const node of document.querySelectorAll('[data-preference]')) {
    const pref = node.dataset.preference;
    //console.debug(`Loading preference: ${pref}`);
    const value = prefs[pref];

    if (pref.startsWith("tag.")) {
      switch (pref) {
        case "tag.color":
          node.value = await bgPage.getTbPref("mailnews.tags.xnote.color");
          break;
        case "tag.name":
          node.value = await bgPage.getTbPref("mailnews.tags.xnote.tag");
          break;
        default:
          console.error(`Unknown tag preference ${pref}`);
      }
    }
    else {
      switch (node.nodeName) {
        case "SELECT":
          for (let option of node.querySelectorAll("option")) {
            if (option.value == value) {
              option.selected = true;
              break;
            }
          }
          break;
        case "INPUT":
          if (isInputType(node, "checkbox")) {
            node.checked = value;
          } else if (isInputType(node, "radio")) {
            node.checked = (value === node.value);
          } else {
            node.value = value;
          }
          break;
        default:
          console.error(`Unknown node type ${node.nodeName}`);
          console.error(node);
      }
    }
  }


  function openHelpPage() {
    let url = browser.runtime.getURL("popup/update.html");
    messenger.tabs.create({url});
  }

  btnSave.addEventListener('click', savePrefs);
  btnSelectStoragePath.addEventListener('click', selectStoragePath);
  btnHlp.addEventListener('click', openHelpPage);

}

document.addEventListener('DOMContentLoaded', () => {
  initOptions();
}, { once: true });