summaryrefslogtreecommitdiffstats
path: root/comm/suite/editor/base/content/editorApplicationOverlay.js
blob: 17d02a510b5da1e7dfaffb891812496a874f6086 (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
/* -*- Mode: Java; tab-width: 4; 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/. */

/* Implementations of nsIControllerCommand for composer commands */

function initEditorContextMenuItems(aEvent)
{
  var shouldShowEditPage = !gContextMenu.onImage && !gContextMenu.onLink && !gContextMenu.onTextInput && !gContextMenu.inDirList;
  gContextMenu.showItem( "context-editpage", shouldShowEditPage );

  var shouldShowEditLink = gContextMenu.onSaveableLink;
  gContextMenu.showItem( "context-editlink", shouldShowEditLink );

  // Hide the applications separator if there's no add-on apps present.
  gContextMenu.showItem("context-sep-apps", gContextMenu.shouldShowSeparator("context-sep-apps"));
}

function initEditorContextMenuListener(aEvent)
{
  var popup = document.getElementById("contentAreaContextMenu");
  if (popup)
    popup.addEventListener("popupshowing", initEditorContextMenuItems);
}

addEventListener("load", initEditorContextMenuListener, false);

function editDocument(aDocument)
{
  if (!aDocument)
    aDocument = window.content.document;

  editPage(aDocument.URL);
}

function editPageOrFrame()
{
  var focusedWindow = document.commandDispatcher.focusedWindow;

  // if the uri is a specific frame, grab it, else use the frameset uri
  // and let Composer handle error if necessary
  editPage(getContentFrameURI(focusedWindow));
}

function getContentFrameURI(aFocusedWindow)
{
  let isContentFrame = aFocusedWindow ?
                         (aFocusedWindow.top == window.content) : false;

  let contentFrame = isContentFrame ?
                       aFocusedWindow : window.content;

  return contentFrame.location.href;
}

// Any non-editor window wanting to create an editor with a URL
//   should use this instead of "window.openDialog..."
//  We must always find an existing window with requested URL
function editPage(url, aFileType)
{
  // aFileType is optional and needs to default to html.
  aFileType = aFileType || "html";

  // Always strip off "view-source:" and #anchors
  url = url.replace(/^view-source:/, "").replace(/#.*/, "");

  // if the current window is a browser window, then extract the current charset menu setting from the current
  // document and use it to initialize the new composer window...

  var wintype = document.documentElement.getAttribute('windowtype');
  var charsetArg;

  if (wintype == "navigator:browser" && content.document)
    charsetArg = "charset=" + content.document.characterSet;

  try {
    let uri = createURI(url, null, null);

    let enumerator = Services.wm.getEnumerator("composer:" + aFileType);
    let emptyWindow;
    while ( enumerator.hasMoreElements() )
    {
      var win = enumerator.getNext();
      if (win && !win.closed && win.IsWebComposer())
      {
        if (CheckOpenWindowForURIMatch(uri, win))
        {
          // We found an editor with our url
          win.focus();
          return;
        }
        else if (!emptyWindow && win.PageIsEmptyAndUntouched())
        {
          emptyWindow = win;
        }
      }
    }

    if (emptyWindow)
    {
      // we have an empty window we can use
      if (aFileType == "html" && emptyWindow.IsInHTMLSourceMode())
        emptyWindow.SetEditMode(emptyWindow.PreviousNonSourceDisplayMode);
      emptyWindow.EditorLoadUrl(url);
      emptyWindow.focus();
      emptyWindow.SetSaveAndPublishUI(url);
      return;
    }

    // Create new Composer / Text Editor window.
    if (aFileType == "text" && ("EditorNewPlaintext" in window))
      EditorNewPlaintext(url, charsetArg);
    else
      NewEditorWindow(url, charsetArg);

  } catch(e) {}
}

function createURI(urlstring)
{
  try {
    return Services.io.newURI(urlstring);
  } catch (e) {}

  return null;
}

function CheckOpenWindowForURIMatch(uri, win)
{
  try {
    return createURI(win.content.document.URL).equals(uri);
  } catch (e) {}

  return false;
}

function toEditor()
{
  if (!CycleWindow("composer:html"))
    NewEditorWindow();
}

function NewEditorWindow(aUrl, aCharsetArg)
{
  window.openDialog("chrome://editor/content",
                    "_blank",
                    "chrome,all,dialog=no",
                    aUrl || "about:blank",
                    aCharsetArg);
}

function NewEditorFromTemplate()
{
  // XXX not implemented
}

function NewEditorFromDraft()
{
  // XXX not implemented
}