summaryrefslogtreecommitdiff
path: root/src/ui/Prompts.js
blob: 03ea9eec783c40ae0d6382fba7466f89f016ed9b (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
var Prompts = (() => {


  var promptData;
  var backlog = [];
  class WindowManager {
    async open(data) {
      promptData = data;
      this.close();
      this.currentWindow = await browser.windows.create({
        url: browser.extension.getURL("ui/prompt.html"),
        type: "panel",
        allowScriptsToClose: true,
      //  titlePreface: "NoScript ",
        width: data.features.width,
        height: data.features.height,
      });
    }
    async close() {
      if (this.currentWindow) {
        try {
          await browser.windows.remove(this.currentWindow.id);
        } catch (e) {
          debug(e);
        }
        this.currentWindow = null;
      }
    }

    async focus() {
      if (this.currentWindow) {
        try {
          await browser.windows.update(this.currentWindow.id,
            {
              focused: true,
            }
          );
        } catch (e) {
          error(e, "Focusing popup window");
        }
      }
    }
  }

  var winMan = new WindowManager();
  var Prompts = {
    DEFAULTS: {
      title: "",
      message: "Proceed?",
      options: [],
      checks: [],
      buttons: [_("Ok"), _("Cancel")],
      multiple: "close", // or "queue", or "focus"
      width: 400,
      height: 300,
    },
    async prompt(features) {
      features = Object.assign({}, this.DEFAULTS, features || {});
      return new Promise((resolve, reject) => {
        let data = {
          features,
          result: {
            button: -1,
            checks: [],
            option: null,
          },
          done() {
            this.done = () => {};
            winMan.close();
            resolve(this.result);
            if (backlog.length) {
              winMan.open(backlog.shift());
            } else {
              promptData = null;
            }
          }
        };
        if (promptData) {
          backlog.push(data);
          switch(promptData.features.multiple) {
            case "focus":
              winMan.focus();
            case "queue":
            break;
            default:
              promptData.done();
          }
        } else {
          winMan.open(data);
        }
      });
    },

    get promptData() {
      return promptData;
    }
  }

  return Prompts;

})();