summaryrefslogtreecommitdiffstats
path: root/comm/suite/browser/sessionHistoryUI.js
blob: f8fdcc79aac7a06e8cc1ec9c97fc9814d45d9169 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var {AppConstants} = ChromeUtils.import(
  "resource://gre/modules/AppConstants.jsm"
);

function toggleTabsFromOtherComputers()
  {
    // enable/disable the Tabs From Other Computers menu
    let menuitem = document.getElementById("sync-tabs-menuitem");

    // If Sync isn't configured yet, then don't show the menuitem.
    if (Weave.Status.checkSetup() == Weave.CLIENT_NOT_CONFIGURED ||
        Weave.Svc.Prefs.get("firstSync", "") == "notReady") {
      menuitem.hidden = true;
      return;
    }

    // The tabs engine might never be inited (if services.sync.registerEngines
    // is modified), so make sure we avoid undefined errors.
    let enabled = Weave.Service.isLoggedIn &&
                  Weave.Service.engineManager.get("tabs") &&
                  Weave.Service.engineManager.get("tabs").enabled;
    menuitem.setAttribute("disabled", !enabled);
    menuitem.hidden = false;
  }

function FillHistoryMenu(aParent, aMenu)
{
  // Remove old entries if any
  deleteHistoryItems(aParent);

  var sessionHistory = getWebNavigation().sessionHistory;

  var count = sessionHistory.count;
  var index = sessionHistory.index;
  var end;
  const MAX_HISTORY_MENU_ITEMS = 15;

  switch (aMenu)
  {
    case "back":
      end = index > MAX_HISTORY_MENU_ITEMS ? index - MAX_HISTORY_MENU_ITEMS
                                           : 0;
      if (index <= end)
        return false;
      for (let j = index - 1; j >= end; j--)
      {
        let entry = sessionHistory.getEntryAtIndex(j);
        if (entry)
          createHistoryMenuItem(aParent, j, entry);
      }
      break;
    case "forward":
      end = count - index > MAX_HISTORY_MENU_ITEMS ? index + MAX_HISTORY_MENU_ITEMS
                                                   : count - 1;
      if (index >= end)
        return false;
      for (let j = index + 1; j <= end; j++)
      {
        let entry = sessionHistory.getEntryAtIndex(j);
        if (entry)
          createHistoryMenuItem(aParent, j, entry);
      }
      break;
    case "go":
      var startHistory = document.getElementById("startHistorySeparator");
      var endHistory = document.getElementById("endHistorySeparator");
      // var syncMenuItem = document.getElementById("sync-tabs-menuitem");
      startHistory.hidden = (count == 0);
      end = count > MAX_HISTORY_MENU_ITEMS ? count - MAX_HISTORY_MENU_ITEMS
                                           : 0;
      for (let j = count - 1; j >= end; j--)
      {
        let entry = sessionHistory.getEntryAtIndex(j);
        if (entry)
          createHistoryMenuItem(aParent, j, entry, endHistory, j == index);
      }
      // toggleTabsFromOtherComputers();
      // endHistory.hidden = (endHistory == aParent.lastChild || syncMenuItem.hidden);
      endHistory.hidden = (endHistory == aParent.lastChild);
      break;
  }
  return true;
}

function executeUrlBarHistoryCommand( aTarget )
  {
    var index = aTarget.getAttribute("index");
    var label = aTarget.getAttribute("label");
    if (index != "nothing_available" && label)
      {
        gURLBar.value = label;
        UpdatePageProxyState();
        handleURLBarCommand();
      }
  }

function createUBHistoryMenu( aParent )
  {
    while (aParent.hasChildNodes())
      aParent.lastChild.remove();

    var file = GetUrlbarHistoryFile();
    if (file.exists()) {
      var connection = Services.storage.openDatabase(file);
      try {
        if (connection.tableExists("urlbarhistory")) {
          var statement = connection.createStatement(
              "SELECT url FROM urlbarhistory ORDER BY ROWID DESC");
          while (statement.executeStep())
            aParent.appendChild(document.createElement("menuitem"))
                   .setAttribute("label", statement.getString(0));
          statement.reset();
          statement.finalize();
          return;
        }
      } finally {
        connection.close();
      }
    }
    //Create the "Nothing Available" Menu item and disable it.
    var na = aParent.appendChild(document.createElement("menuitem"));
    na.setAttribute("label", gNavigatorBundle.getString("nothingAvailable"));
    na.setAttribute("disabled", "true");
  }

function createHistoryMenuItem(aParent, aIndex, aEntry, aAnchor, aChecked)
{
  var menuitem = document.createElement("menuitem");
  menuitem.setAttribute("label", aEntry.title);
  menuitem.setAttribute("index", aIndex);
  if (aChecked)
  {
    menuitem.setAttribute("type", "radio");
    menuitem.setAttribute("checked", "true");
  }

  if (!aChecked || AppConstants.platform == "macosx")
  {
    menuitem.className = "menuitem-iconic bookmark-item menuitem-with-favicon";
    PlacesUtils.favicons.getFaviconURLForPage(aEntry.URI,
      function faviconURLCallback(aURI) {
        if (aURI) {
          menuitem.setAttribute("image",
                                PlacesUtils.favicons
                                           .getFaviconLinkForIcon(aURI).spec);
        }
      }
    );
  }
  aParent.insertBefore(menuitem, aAnchor);
}

function deleteHistoryItems(aParent)
{
  var children = aParent.childNodes;
  for (let i = children.length - 1; i >= 0; --i)
  {
    if (children[i].hasAttribute("index"))
      children[i].remove();
  }
}

function updateGoMenu(event)
  {
    FillHistoryMenu(event.target, "go");
    updateRecentMenuItems();
  }