aboutsummaryrefslogtreecommitdiffstats
path: root/completionDialog.js
blob: eec089a059992e32b59c1805556051f3302cbfe0 (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
(function(window, document) {

  var CompletionDialog = function(options) { this.options = options; }

  CompletionDialog.prototype = {
    show: function() {
      if (!this.isShown) {
        this.isShown=true;
        this.query = [];
        if (!this.initialized) {
          initialize.call(this);
          this.initialized = true;
        }
        handlerStack.push({ keydown: this.onKeydown });
        render.call(this);
        clearInterval(this._tweenId);
        this.container.style.display = "block";
        this._tweenId = Tween.fade(this.container, 1.0, 150);
      }
    },

    hide: function() {
      if (this.isShown) {
        handlerStack.pop();
        this.isShown = false;
        this.currentSelection = 0;
        clearInterval(this._tweenId);
        var completionContainer = this.container;
        var cssHide = function() { completionContainer.style.display = "none"; }
        this._tweenId = Tween.fade(this.container, 0, 150, cssHide);
      }
    },

    getDisplayElement: function() {
      if (!this.container)
        this.container = createDivInside(document.body);
      return this.container;
    },

    getQueryString: function() { return this.query.join(""); }
  }

  var initialize = function() {
    var self = this;

    self.currentSelection = 0;

    self.onKeydown = function(event) {
      var keyChar = getKeyChar(event);
      // change selection with up or Shift-Tab
      if (keyChar==="up" || (event.keyCode == 9 && event.shiftKey)) {
        if (self.currentSelection>0) {
          self.currentSelection-=1;
        }
        render.call(self,self.getQueryString(), self.completions);
      }
      // change selection with down or Tab
      else if (keyChar==="down" || (event.keyCode == 9 && !event.shiftKey)) {
        if (self.currentSelection < self.completions.length - 1) {
          self.currentSelection += 1;
        }
        render.call(self,self.getQueryString(), self.completions);
      }
      else if (event.keyCode == keyCodes.enter) {
        self.options.onSelect(self.completions[self.currentSelection]);
      }
      else if (event.keyCode == keyCodes.backspace || event.keyCode == keyCodes.deleteKey) {
        if (self.query.length > 0) {
          self.query.pop();
          self.options.source(self.getQueryString(), function(completions) {
            render.call(self, self.getQueryString(), completions);
          })
        }
      }
      else if (keyChar!=="left" && keyChar!="right") {
        self.query.push(keyChar);
        self.options.source(self.getQueryString(), function(completions) {
          render.call(self, self.getQueryString(), completions);
        });
      }

      event.stopPropagation();
      event.preventDefault();
      return true;
    }
  }

  var render = function(searchString, completions) {
    if (this.isShown) {
      this.searchString = searchString;
      this.completions = completions;
      var container = this.getDisplayElement();
      clearChildren(container);
      container.style.display = "";

      if (searchString === undefined) {
        this.container.className = "vimiumReset vimium-dialog";
        createDivInside(container).innerHTML = this.options.initialSearchText || "Begin typing";
      }
      else {
        this.container.className = "vimiumReset vimium-dialog vimium-completions";
        var searchBar = createDivInside(container);
        searchBar.innerHTML=searchString;
        searchBar.className="vimiumReset vimium-searchBar";

        searchResults = createDivInside(container);
        searchResults.className="vimiumReset vimium-searchResults";
        if (completions.length<=0) {
          var resultDiv = createDivInside(searchResults);
          resultDiv.className="vimiumReset vimium-noResults";
          resultDiv.innerHTML="No results found";
        }
        else {
          for (var i = 0; i < completions.length; i++) {
            var resultDiv = createDivInside(searchResults);
            if (i === this.currentSelection) {
              resultDiv.className="vimiumReset vimium-selected";
            }
            resultDiv.innerHTML=this.options.renderOption(searchString, completions[i]);
          }
        }
      }

      container.style.top = Math.max(0, (window.innerHeight/2-container.clientHeight/2)) + "px";
      container.style.left = (window.innerWidth/2-container.clientWidth/2) + "px";
    }
  };
  var createDivInside = function(parent) {
    var element = document.createElement("div");
    element.className = "vimiumReset";
    parent.appendChild(element);
    return element;
  }

  var clearChildren = function(elem) {
    if (elem.hasChildNodes()) {
      while (elem.childNodes.length >= 1) {
        elem.removeChild(elem.firstChild);
      }
    }
  }

  window.CompletionDialog = CompletionDialog;

}(window, document))