summaryrefslogtreecommitdiffstats
path: root/comm/suite/base/content/openLocation.js
blob: 2bb9524d945485e6be689441cfb3cc87850d4d05 (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
/* -*- Mode: C++; 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 gInput;
var gAcceptButton;
var gLastPref = "general.open_location.last_url";
var gOpenAppList;
var gBundle;
var gAction;

function onLoad()
{
  gInput = document.getElementById("dialog.input");
  gAcceptButton = document.documentElement.getButton("accept");
  gOpenAppList = document.getElementById("openAppList");
  gBundle = document.getElementById("openLocationBundle");
  gAction = window.arguments[0].action;
  // Set arguments action to prevent problems on cancel.
  window.arguments[0].action = "-1";

  switch (gAction) {
    case "5": // attach web page
      document.title = gBundle.getString("attachTitle");
      document.getElementById("enterLabel").value = gBundle.getString("attachEnterLabel");
      document.getElementById("openWhereBox").setAttribute("hidden", true);

      // Change accept button text to 'attach'.
      gAcceptButton.label = gBundle.getString("attachButtonLabel");
      gLastPref = "mailnews.attach_web_page.last_url";

      break;

    case "2": // open web page from composer
      gOpenAppList.selectedItem = document.getElementById("editWindow");
      var openTopWindow = document.getElementById("currentTab");

      // Change string to make more sense for Composer.
      openTopWindow.setAttribute("label",
                                 gBundle.getString("existingNavigatorWindow"));

      // Disable existing browser and new tab menuitems and create indicator
      // if no browser windows found.
      if (!Services.wm.getMostRecentWindow("navigator:browser")) {
        openTopWindow.setAttribute("disabled", "true");
        document.getElementById("newTab").setAttribute("disabled", "true");
        gAction = "-1";
      }
      break;

    default: // open web page
      gOpenAppList.value = Services.prefs.getIntPref("general.open_location.last_window_choice");
  }

  gInput.value = Services.prefs.getStringPref(gLastPref, "");
  if (gInput.value)
    gInput.select(); // XXX should probably be done automatically

  doEnabling();
}

function doEnabling()
{
  gAcceptButton.disabled = !gInput.value;
}

function accept()
{
  var params = window.arguments[0];
  params.url = gInput.value;
  params.action = gOpenAppList.value;
  if (gAction == "4" || params.action == "4")
    return; // private, don't set any preferences

  if (gAction != "5") { // open web page
    // If there were no browser windows open and not set to open in composer
    // then set to open in a new window.
    if (gAction == "-1" && params.action != "2")
      params.action = "1";

    // If open web page from navigator window, save last window choice.
    if (gAction == "0")
      Services.prefs.setIntPref("general.open_location.last_window_choice",
                                gOpenAppList.value);
  }

  SetStringPref(gLastPref, gInput.value);
}

function onChooseFile()
{
  const nsIFilePicker = Ci.nsIFilePicker;
  let fp = Cc["@mozilla.org/filepicker;1"]
             .createInstance(nsIFilePicker);
  fp.init(window, gBundle.getString("chooseFileDialogTitle"),
          nsIFilePicker.modeOpen);
  if (window.arguments[0].action != "5" && gOpenAppList.value == "2") {
    // When loading into Composer, direct user to prefer HTML files and text
    // files, so we call separately to control the order of the filter list.
    fp.appendFilters(nsIFilePicker.filterHTML | nsIFilePicker.filterText);
    fp.appendFilters(nsIFilePicker.filterAll);
  } else {
    fp.appendFilters(nsIFilePicker.filterHTML | nsIFilePicker.filterText |
                     nsIFilePicker.filterAll | nsIFilePicker.filterImages |
                     nsIFilePicker.filterXML);
  }
  
  fp.open(rv => {
    if (rv == nsIFilePicker.returnOK && fp.fileURL.spec && 
        fp.fileURL.spec.length > 0) {
      gInput.value = fp.fileURL.spec;
    }

    doEnabling();
  });
}

function useUBHistoryItem(aValue)
{
  gInput.value = aValue;
  gInput.focus();
  doEnabling();
}