~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/extensions/inspector/resources/content/inspector.js

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ***** BEGIN LICENSE BLOCK *****
 
2
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 
3
 *
 
4
 * The contents of this file are subject to the Netscape Public License
 
5
 * Version 1.1 (the "License"); you may not use this file except in
 
6
 * compliance with the License. You may obtain a copy of the License at
 
7
 * http://www.mozilla.org/NPL/
 
8
 *
 
9
 * Software distributed under the License is distributed on an "AS IS" basis,
 
10
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
11
 * for the specific language governing rights and limitations under the
 
12
 * License.
 
13
 *
 
14
 * The Original Code is mozilla.org code.
 
15
 *
 
16
 * The Initial Developer of the Original Code is 
 
17
 * Netscape Communications Corporation.
 
18
 * Portions created by the Initial Developer are Copyright (C) 2001
 
19
 * the Initial Developer. All Rights Reserved.
 
20
 *
 
21
 * Contributor(s):
 
22
 *   Joe Hewitt <hewitt@netscape.com> (original author)
 
23
 *
 
24
 *
 
25
 * Alternatively, the contents of this file may be used under the terms of
 
26
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
27
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
28
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
29
 * of those above. If you wish to allow use of your version of this file only
 
30
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
31
 * use your version of this file under the terms of the NPL, indicate your
 
32
 * decision by deleting the provisions above and replace them with the notice
 
33
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
34
 * the provisions above, a recipient may use your version of this file under
 
35
 * the terms of any one of the NPL, the GPL or the LGPL.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
/***************************************************************
 
40
* InspectorApp -------------------------------------------------
 
41
*  The primary object that controls the Inspector application.
 
42
****************************************************************/
 
43
 
 
44
//////////// global variables /////////////////////
 
45
 
 
46
var inspector;
 
47
 
 
48
//////////// global constants ////////////////////
 
49
 
 
50
const kSearchRegURL        = "resource:///res/inspector/search-registry.rdf";
 
51
 
 
52
const kWindowDataSourceCID = "@mozilla.org/rdf/datasource;1?name=window-mediator";
 
53
const kClipboardHelperCID  = "@mozilla.org/widget/clipboardhelper;1";
 
54
const kPromptServiceCID    = "@mozilla.org/embedcomp/prompt-service;1";
 
55
const nsIWebNavigation     = Components.interfaces.nsIWebNavigation;
 
56
 
 
57
//////////////////////////////////////////////////
 
58
 
 
59
window.addEventListener("load", InspectorApp_initialize, false);
 
60
window.addEventListener("unload", InspectorApp_destroy, false);
 
61
 
 
62
function InspectorApp_initialize()
 
63
{
 
64
  inspector = new InspectorApp();
 
65
 
 
66
  // window.arguments may be either a string or a node.
 
67
  // If passed via a command line handler, it will be a uri string.
 
68
  // If passed via navigator hooks, it will be a dom node to inspect.
 
69
  var initNode, initURI;
 
70
  if (window.arguments.length) {
 
71
    if (typeof window.arguments[0] == "string") {
 
72
      initURI = window.arguments[0];
 
73
    }
 
74
    else if (window.arguments[0] instanceof Components.interfaces.nsIDOMNode) {
 
75
      initNode = window.arguments[0];
 
76
    }
 
77
  }
 
78
  inspector.initialize(initNode, initURI);
 
79
}
 
80
 
 
81
function InspectorApp_destroy()
 
82
{
 
83
  inspector.destroy();
 
84
}
 
85
 
 
86
////////////////////////////////////////////////////////////////////////////
 
87
//// class InspectorApp
 
88
 
 
89
function InspectorApp()
 
90
{
 
91
}
 
92
 
 
93
InspectorApp.prototype = 
 
94
{
 
95
  ////////////////////////////////////////////////////////////////////////////
 
96
  //// Initialization
 
97
 
 
98
  mSearchService: null,
 
99
  mShowBrowser: false,
 
100
  mClipboardHelper: null,
 
101
  mPromptService: null,
 
102
  
 
103
  get document() { return this.mDocPanel.viewer.subject },
 
104
  get searchRegistry() { return this.mSearchService },
 
105
  get panelset() { return this.mPanelSet; },
 
106
  
 
107
  initialize: function(aTarget, aURI)
 
108
  {
 
109
    this.mInitTarget = aTarget;
 
110
    
 
111
    //this.initSearch();
 
112
 
 
113
    var el = document.getElementById("bxBrowser");
 
114
    el.addEventListener("load", BrowserLoadListener, true);
 
115
 
 
116
    this.setBrowser(false, true);
 
117
    //this.setSearch(false, true);
 
118
 
 
119
    this.mClipboardHelper = XPCU.getService(kClipboardHelperCID, "nsIClipboardHelper");
 
120
    this.mPromptService = XPCU.getService(kPromptServiceCID, "nsIPromptService");
 
121
 
 
122
    this.mPanelSet = document.getElementById("bxPanelSet");
 
123
    this.mPanelSet.addObserver("panelsetready", this, false);
 
124
    this.mPanelSet.initialize();
 
125
 
 
126
    if (aURI) {
 
127
      this.gotoURL(aURI);
 
128
    }
 
129
  },
 
130
 
 
131
  destroy: function()
 
132
  {
 
133
    InsUtil.persistAll("bxDocPanel");
 
134
    InsUtil.persistAll("bxObjectPanel");
 
135
  },
 
136
  
 
137
  ////////////////////////////////////////////////////////////////////////////
 
138
  //// Viewer Panels
 
139
  
 
140
  initViewerPanels: function()
 
141
  {
 
142
    this.mDocPanel = this.mPanelSet.getPanel(0);
 
143
    this.mDocPanel.addObserver("subjectChange", this, false);
 
144
    this.mObjectPanel = this.mPanelSet.getPanel(1);
 
145
 
 
146
    if (this.mInitTarget) {
 
147
      if (this.mInitTarget.nodeType == Node.DOCUMENT_NODE)
 
148
        this.setTargetDocument(this.mInitTarget);
 
149
      else if (this.mInitTarget.nodeType == Node.ELEMENT_NODE) {
 
150
        this.setTargetDocument(this.mInitTarget.ownerDocument);
 
151
        this.mDocPanel.params = this.mInitTarget;
 
152
      }
 
153
      this.mInitTarget = null;
 
154
    }
 
155
  },
 
156
 
 
157
  onEvent: function(aEvent)
 
158
  {
 
159
    switch (aEvent.type) {
 
160
      case "panelsetready":
 
161
        this.initViewerPanels();
 
162
        break;
 
163
      case "subjectChange":
 
164
        if (aEvent.target == this.mDocPanel.viewer &&
 
165
            aEvent.subject && "location" in aEvent.subject) {
 
166
          this.locationText = aEvent.subject.location; // display document url
 
167
        }
 
168
    }
 
169
  },
 
170
  
 
171
  ////////////////////////////////////////////////////////////////////////////
 
172
  //// UI Commands
 
173
 
 
174
  doViewerCommand: function(aCommand)
 
175
  {
 
176
    this.mPanelSet.execCommand(aCommand);
 
177
  },
 
178
  
 
179
  showOpenURLDialog: function()
 
180
  {
 
181
    var bundle = this.mPanelSet.stringBundle;
 
182
    var msg = bundle.getString("inspectURL.message");
 
183
    var title = bundle.getString("inspectURL.title");
 
184
    var url = { value: "http://" };
 
185
    var dummy = { value: false };
 
186
    var go = this.mPromptService.prompt(window, title, msg, url, null, dummy);
 
187
    if (go) {
 
188
      this.gotoURL(url.value);
 
189
    }
 
190
  },
 
191
 
 
192
  showPrefsDialog: function()
 
193
  {
 
194
    goPreferences("advancedItem", "chrome://inspector/content/prefs/pref-inspector.xul", "inspector");
 
195
  },
 
196
  
 
197
  toggleBrowser: function(aToggleSplitter)
 
198
  {
 
199
    this.setBrowser(!this.mShowBrowser, aToggleSplitter)
 
200
  },
 
201
 
 
202
  setBrowser: function(aValue, aToggleSplitter)
 
203
  {
 
204
    this.mShowBrowser = aValue;
 
205
    if (aToggleSplitter)
 
206
      this.openSplitter("Browser", aValue);
 
207
    var cmd = document.getElementById("cmdToggleBrowser");
 
208
    cmd.setAttribute("checked", aValue);
 
209
  },
 
210
 
 
211
  toggleSearch: function(aToggleSplitter)
 
212
  {
 
213
    this.setSearch(!this.mShowSearch, aToggleSplitter);
 
214
  },
 
215
 
 
216
  setSearch: function(aValue, aToggleSplitter)
 
217
  {
 
218
    this.mShowSearch = aValue;
 
219
    if (aToggleSplitter)
 
220
      this.openSplitter("Search", aValue);
 
221
    var cmd = document.getElementById("cmdToggleSearch");
 
222
    cmd.setAttribute("checked", aValue);
 
223
  },
 
224
 
 
225
  openSplitter: function(aName, aTruth)
 
226
  {
 
227
    var splitter = document.getElementById("spl" + aName);
 
228
    if (aTruth)
 
229
      splitter.open();
 
230
    else
 
231
      splitter.close();
 
232
  },
 
233
 
 
234
/*
 
235
  XXXcaa
 
236
  The following code needs to evaluated.  What does it do?  Why does nobody
 
237
  call it?  If deemed necessary, turn it back on with the right callers,
 
238
  and localize it.
 
239
 */
 
240
/*
 
241
  runSearch: function()
 
242
  {
 
243
    var path = null; // TODO: should persist last path chosen in a pref
 
244
    var file = FilePickerUtils.pickFile("Find Search File", path, ["filterXML"], "Open");
 
245
    if (file) {
 
246
      var ioService = XPCU.getService("@mozilla.org/network/io-service;1","nsIIOService");
 
247
      var fileHandler = XPCU.QI(ioService.getProtocolHandler("file"), "nsIFileProtocolHandler");
 
248
 
 
249
      var url = fileHandler.getURLSpecFromFile(file);
 
250
 
 
251
      this.startSearchModule(url);
 
252
    }
 
253
  },
 
254
 
 
255
  viewSearchItem: function()
 
256
  {
 
257
    if (this.mCurrentSearch.canViewItems)
 
258
      window.openDialog("chrome://inspector/content/utilWindow.xul", "viewItem", "chrome,resizable");
 
259
  },
 
260
 
 
261
  doViewSearchItem: function(aWindow)
 
262
  {
 
263
    var idx = this.getSelectedSearchIndex();
 
264
    var el = this.mCurrentSearch.viewItemAt(idx);
 
265
 
 
266
    aWindow.title = this.mCurrentSearch.getItemDescription(idx);
 
267
    aWindow.document.getElementById("bxCenter").appendChild(el);
 
268
  },
 
269
 
 
270
  editSearchItem: function()
 
271
  {
 
272
  },
 
273
  
 
274
  onSearchTreeClick: function(aEvent)
 
275
  {
 
276
    if (aEvent.detail == 2) { // double click
 
277
      this.viewSearchItem();
 
278
    }
 
279
  },
 
280
 
 
281
  copySearchItemLine: function()
 
282
  {
 
283
    var mod = this.mSearchService.currentModule;
 
284
    var idx = this.mSearchService.getSelectedIndex(0);
 
285
    var text = mod.getItemText(idx);
 
286
    this.mClipboardHelper.copyString(text);
 
287
  },
 
288
 
 
289
  // XXX what is this?  It doesn't seem to get called from anywhere?
 
290
  copySearchItemAll: function()
 
291
  {
 
292
    var text = this.getAllSearchItemText();
 
293
    this.mClipboardHelper.copyString(text);
 
294
  },
 
295
 
 
296
  saveSearchItemText: function()
 
297
  {
 
298
    var target = FilePickerUtils.pickFile("Save Results As", null, ["filterAll", "filterText"], "Save");
 
299
 
 
300
    var text = this.getAllSearchItemText();
 
301
 
 
302
    var file = new File(target.path);
 
303
    file.open('w');
 
304
    file.write(text);
 
305
    file.close();
 
306
  },
 
307
 
 
308
  getAllSearchItemText: function()
 
309
  {
 
310
    var mod = this.mSearchService.currentModule;
 
311
    var len = mod.resultCount;
 
312
    var text = "";
 
313
    for (var i = 0; i < len; i++) {
 
314
      text += mod.getItemText(i) + "\r";
 
315
    }
 
316
 
 
317
    return text;
 
318
  },
 
319
*/
 
320
 
 
321
  exit: function()
 
322
  {
 
323
    window.close();
 
324
    // Todo: remove observer service here
 
325
  },
 
326
 
 
327
  ////////////////////////////////////////////////////////////////////////////
 
328
  //// Navigation
 
329
  
 
330
  gotoTypedURL: function()
 
331
  {
 
332
    var url = document.getElementById("tfURLBar").value;
 
333
    this.gotoURL(url);
 
334
  },
 
335
 
 
336
  gotoURL: function(aURL, aNoSaveHistory)
 
337
  {
 
338
    this.mPendingURL = aURL;
 
339
    this.mPendingNoSave = aNoSaveHistory;
 
340
    this.browseToURL(aURL);
 
341
    this.setBrowser(true, true);
 
342
  },
 
343
 
 
344
  browseToURL: function(aURL)
 
345
  {
 
346
    try {
 
347
      this.webNavigation.loadURI(aURL, nsIWebNavigation.LOAD_FLAGS_NONE, null, null, null);
 
348
    }
 
349
    catch(ex) {
 
350
      // nsIWebNavigation.loadURI will spit out an appropriate user prompt, so
 
351
      // we don't need to do anything here.  See nsDocShell::DisplayLoadError()
 
352
    }
 
353
  },
 
354
 
 
355
  goToWindow: function(aMenuitem)
 
356
  {
 
357
    this.setTargetWindowById(aMenuitem.id);
 
358
  },
 
359
 
 
360
  setTargetWindowById: function(aResId)
 
361
  {
 
362
    var windowManager = XPCU.getService(kWindowDataSourceCID, "nsIWindowDataSource");
 
363
    var win = windowManager.getWindowForResource(aResId);
 
364
 
 
365
    if (win) {
 
366
      this.setTargetWindow(win);
 
367
      this.setBrowser(false, true);
 
368
    } else {
 
369
      var bundle = this.mPanelSet.stringBundle;
 
370
      var msg = bundle.getString("inspectWindow.error.message");
 
371
      var title = bundle.getString("inspectWindow.error.title");
 
372
      this.mPromptService.alert(window, title, msg);
 
373
    }
 
374
  },
 
375
 
 
376
  setTargetWindow: function(aWindow)
 
377
  {
 
378
    this.setTargetDocument(aWindow.document);
 
379
  },
 
380
 
 
381
  setTargetDocument: function(aDoc)
 
382
  {
 
383
    this.mDocPanel.subject = aDoc;
 
384
  },
 
385
 
 
386
  get webNavigation()
 
387
  {
 
388
    var browser = document.getElementById("ifBrowser");
 
389
    return browser.webNavigation;
 
390
  },
 
391
 
 
392
  ////////////////////////////////////////////////////////////////////////////
 
393
  //// UI Labels getters and setters
 
394
 
 
395
  get locationText() { return document.getElementById("tfURLBar").value; },
 
396
  set locationText(aText) { document.getElementById("tfURLBar").value = aText; },
 
397
 
 
398
  get statusText() { return document.getElementById("txStatus").value; },
 
399
  set statusText(aText) { document.getElementById("txStatus").value = aText; },
 
400
 
 
401
  get progress() { return document.getElementById("pmStatus").value; },
 
402
  set progress(aPct) { document.getElementById("pmStatus").value = aPct; },
 
403
 
 
404
/*
 
405
  XXXcaa -- What is this?
 
406
 
 
407
 
 
408
  get searchTitle(aTitle) { return document.getElementById("splSearch").label; },
 
409
  set searchTitle(aTitle)
 
410
  {
 
411
    var splitter = document.getElementById("splSearch");
 
412
    splitter.setAttribute("label", "Search" + (aTitle ? " - " + aTitle : ""));
 
413
  },
 
414
*/
 
415
  ////////////////////////////////////////////////////////////////////////////
 
416
  //// Document Loading 
 
417
 
 
418
  documentLoaded: function()
 
419
  {
 
420
    this.setTargetWindow(_content);
 
421
 
 
422
    var url = this.webNavigation.currentURI.spec;
 
423
    
 
424
    // put the url into the urlbar
 
425
    this.locationText = url;
 
426
 
 
427
    // add url to the history, unless explicity told not to
 
428
    if (!this.mPendingNoSave)
 
429
      this.addToHistory(url);
 
430
 
 
431
    this.mPendingURL = null;
 
432
    this.mPendingNoSave = null;
 
433
  },
 
434
 
 
435
  ////////////////////////////////////////////////////////////////////////////
 
436
  //// Search 
 
437
/*
 
438
  initSearch: function()
 
439
  {
 
440
    var ss = new inSearchService();
 
441
    this.mSearchService = ss;
 
442
    
 
443
    ss.addSearchObserver(this);
 
444
    ss.resultsTree = document.getElementById("trSearch");
 
445
    ss.contextMenu = document.getElementById("ppSearchResults");
 
446
    ss.contextMenuInsertPt = document.getElementById("ppSearchResults-insertion");
 
447
    ss.contextMenuInsert = inSearchService.INSERT_BEFORE;
 
448
  },
 
449
 
 
450
  startSearchModule: function(aModuleURL)
 
451
  {
 
452
    this.mSearchService.startModule(aModuleURL);
 
453
  },
 
454
  
 
455
  clearSearchResults: function()
 
456
  {
 
457
    this.searchTitle = null;
 
458
    this.statusText = "";
 
459
    this.progress = 0;
 
460
    
 
461
    this.mSearchService.clearSearch();
 
462
  },
 
463
  
 
464
  ////////////////////////////////////////////////////////////////////////////
 
465
  //// interface inISearchObserver
 
466
 
 
467
  onSearchStart: function(aModule) 
 
468
  {
 
469
    this.searchTitle = aModule.title;
 
470
    this.toggleSearch(true, true);
 
471
  },
 
472
 
 
473
  onSearchResult: function(aModule)
 
474
  {
 
475
    if (aModule.isPastMilestone) {
 
476
      this.statusText = "Searching - " + aModule.resultCount + " results found."; // XXX localize
 
477
      this.progress = aModule.progressPercent;
 
478
    }
 
479
  },
 
480
 
 
481
  onSearchEnd: function(aModule, aResult)
 
482
  {
 
483
    var diff = Math.round(aModule.elapsed / 100) / 10;
 
484
    this.progress = 100;
 
485
    this.statusText = "Search complete - " + aModule.resultCount + " results found ("+diff+"s)"; // XXX localize
 
486
  },
 
487
 
 
488
  onSearchError: function(aModule, aMessage)
 
489
  {
 
490
    alert("Unable to complete this search due to the following error:\n" + aMessage);
 
491
  },
 
492
 
 
493
*/
 
494
 
 
495
  ////////////////////////////////////////////////////////////////////////////
 
496
  //// History 
 
497
 
 
498
  addToHistory: function(aURL)
 
499
  {
 
500
  },
 
501
 
 
502
  ////////////////////////////////////////////////////////////////////////////
 
503
  //// Uncategorized
 
504
  
 
505
  get isViewingContent() { return this.mPanelSet.getPanel(0).subject != null; },
 
506
  
 
507
  fillInTooltip: function(tipElement)
 
508
  {
 
509
    var retVal = false;
 
510
    var textNode = document.getElementById("txTooltip");
 
511
    if (textNode) {
 
512
      try {  
 
513
        var tipText = tipElement.getAttribute("tooltiptext");
 
514
        if (tipText != "") {
 
515
          textNode.setAttribute("value", tipText);
 
516
          retVal = true;
 
517
        }
 
518
      }
 
519
      catch (e) { }
 
520
    }
 
521
    
 
522
    return retVal;
 
523
  },
 
524
 
 
525
  initPopup: function(aPopup)
 
526
  {
 
527
    var items = aPopup.getElementsByTagName("menuitem");
 
528
    var js, fn, item;
 
529
    for (var i = 0; i < items.length; i++) {
 
530
      item = items[i];
 
531
      fn = "isDisabled" in item ? item.isDisabled : null;
 
532
      if (!fn) {
 
533
        js = item.getAttribute("isDisabled");
 
534
        if (js) {
 
535
          fn = new Function(js);
 
536
          item.isDisabled = fn;
 
537
        } else {
 
538
          item.isDisabled = null; // to prevent annoying "strict" warning messages
 
539
        }
 
540
      } 
 
541
      if (fn) {
 
542
        if (item.isDisabled())
 
543
          item.setAttribute("disabled", "true");
 
544
        else
 
545
          item.removeAttribute("disabled");
 
546
      }
 
547
 
 
548
      fn = null;
 
549
    }
 
550
  },
 
551
 
 
552
  emptyChildren: function(aNode)
 
553
  {
 
554
    while (aNode.childNodes.length > 0) {
 
555
      aNode.removeChild(aNode.lastChild);
 
556
    }
 
557
  },
 
558
  
 
559
  onSplitterOpen: function(aSplitter)
 
560
  {
 
561
    if (aSplitter.id == "splBrowser") {
 
562
      this.setBrowser(aSplitter.isOpened, false);
 
563
    } else if (aSplitter.id == "splSearch") {
 
564
      this.setSearch(aSplitter.isOpened, false);
 
565
    }
 
566
  },
 
567
  
 
568
  // needed by overlayed commands from viewer to get references to a specific
 
569
  // viewer object by name
 
570
  getViewer: function(aUID)
 
571
  {
 
572
    return this.mPanelSet.registry.getViewerByUID(aUID);
 
573
  }
 
574
  
 
575
};
 
576
 
 
577
////////////////////////////////////////////////////////////////////////////
 
578
//// event listeners
 
579
 
 
580
function BrowserLoadListener(aEvent) 
 
581
{
 
582
  // since we will also get load events for frame documents,
 
583
  // make sure we respond to the top-level document load
 
584
  if (aEvent.target.defaultView == _content)
 
585
    inspector.documentLoaded();
 
586
}
 
587
 
 
588
function UtilWindowOpenListener(aWindow)
 
589
{
 
590
  inspector.doViewSearchItem(aWindow);
 
591
}