aboutsummaryrefslogtreecommitdiffstats
path: root/bookmarks.js
blob: 85d12aacd3ec7844b7b2d726f6c6ddbdd3732b80 (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
function activateBookmarkFindModeToOpenInNewTab() {
  BookmarkMode.openInNewTab(true);
  BookmarkMode.enable();
}

function activateBookmarkFindMode() {
  BookmarkMode.openInNewTab(false);
  BookmarkMode.enable();
}

(function() {
  // so when they let go of shift after hitting capital "B" it won't
  // untoggle it
  var shiftWasPressedWhileToggled = false;

  var BookmarkMode = {
    isEnabled: function() {
      return this.enabled;
    },
    openInNewTab: function(newTab) {
      this.newTab = newTab;
    },
    invertNewTabSetting: function() {
      this.newTab = !this.newTab;
      if(this.isEnabled()) {
        this.renderHUD();
      }
    },
    enable: function() {
      this.enabled = true;

      if(!this.initialized) {
        initialize.call(this);
      }

      handlerStack.push({
        keydown: this.onKeydown,
        keypress: this.onKeypress,
        keyup: this.onKeyup
      });

      this.renderHUD();
      this.completionDialog.show();
    },
    disable: function() {
      this.enabled = false;
      this.completionDialog.hide();
      handlerStack.pop();
      HUD.hide();
    },
    renderHUD: function() {
      if (this.newTab)
        HUD.show("Open bookmark in new tab");
      else
        HUD.show("Open bookmark in current tab");
    }

  }

  // private method
  var initialize = function() {
    var self = this;
    self.initialized = true;

    self.completionDialog = new CompletionDialog({
      source: findBookmarks,

      onSelect: function(selection) {
        var url = selection.url;
        var isABookmarklet = function(url) { return url.indexOf("javascript:") === 0; }

        if (isABookmarklet(url))
          window.location = url;
        else if (!self.newTab)
          chrome.extension.sendRequest({ handler: "openUrlInCurrentTab", url: url });
        else
          chrome.extension.sendRequest({ handler: "openUrlInNewTab", url: url });

        self.disable();
      },

      renderOption: function(searchString, selection) {
        var displaytext = selection.title + " (" + selection.url + ")"
        if (displaytext.length > 70)
          displaytext = displaytext.substr(0, 70) + "...";

        return displaytext.split(new RegExp(searchString, "i")).join("<strong>"+searchString+"</strong>")
      },

      initialSearchText: "Type a bookmark name or URL"
    })

    self.onKeydown = function(event) {
      // shift key will toggle between new tab/same tab
      if (event.keyCode == keyCodes.shiftKey) {
        self.invertNewTabSetting();
        shiftWasPressedWhileToggled = true;
        return;
      }

      var keyChar = getKeyChar(event);
      if (!keyChar)
        return;

      // TODO(philc): Ignore keys that have modifiers.
      if (isEscape(event))
        self.disable();
    };

    self.onKeypress = function(event) { return false; }

    self.onKeyup = function(event) {
      // shift key will toggle between new tab/same tab
      if (event.keyCode == keyCodes.shiftKey && shiftWasPressedWhileToggled) {
        self.invertNewTabSetting();
        shiftWasPressedWhileToggled = false;
      }
    };
  }

  var findBookmarks = function(searchString, callback) {
    var port = chrome.extension.connect({ name: "getBookmarks" }) ;
    port.onMessage.addListener(function(msg) {
      callback(msg.bookmarks);
      port = null;
    })
    port.postMessage({query:searchString});
  };

  window.BookmarkMode = BookmarkMode;
}())