2011-04-20 01:18:00 -07:00
|
|
|
/* vim:set ts=2 sw=2 sts=2 et:
|
2012-05-21 04:12:37 -07:00
|
|
|
* 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/. */
|
2011-04-20 01:18:00 -07:00
|
|
|
|
2011-05-09 07:51:52 -07:00
|
|
|
/*
|
|
|
|
* Original version history can be found here:
|
|
|
|
* https://github.com/mozilla/workspace
|
|
|
|
*
|
|
|
|
* Copied and relicensed from the Public Domain.
|
|
|
|
* See bug 653934 for details.
|
|
|
|
* https://bugzilla.mozilla.org/show_bug.cgi?id=653934
|
|
|
|
*/
|
|
|
|
|
2011-08-11 14:35:10 -07:00
|
|
|
"use strict";
|
|
|
|
|
2011-04-20 01:18:00 -07:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/NetUtil.jsm");
|
|
|
|
Cu.import("resource:///modules/PropertyPanel.jsm");
|
2011-08-11 14:35:10 -07:00
|
|
|
Cu.import("resource:///modules/source-editor.jsm");
|
2011-11-02 12:32:55 -07:00
|
|
|
Cu.import("resource:///modules/devtools/scratchpad-manager.jsm");
|
2012-05-01 08:17:32 -07:00
|
|
|
Cu.import("resource://gre/modules/jsdebugger.jsm");
|
2011-11-02 12:32:55 -07:00
|
|
|
|
2011-04-20 01:18:00 -07:00
|
|
|
|
2011-05-09 07:51:52 -07:00
|
|
|
const SCRATCHPAD_CONTEXT_CONTENT = 1;
|
2011-05-19 14:10:41 -07:00
|
|
|
const SCRATCHPAD_CONTEXT_BROWSER = 2;
|
2011-10-27 08:35:13 -07:00
|
|
|
const SCRATCHPAD_L10N = "chrome://browser/locale/devtools/scratchpad.properties";
|
2011-04-19 13:42:56 -07:00
|
|
|
const DEVTOOLS_CHROME_ENABLED = "devtools.chrome.enabled";
|
2012-07-03 06:40:12 -07:00
|
|
|
const PREF_RECENT_FILES_MAX = "devtools.scratchpad.recentFilesMax";
|
2011-11-23 18:49:03 -08:00
|
|
|
const BUTTON_POSITION_SAVE = 0;
|
|
|
|
const BUTTON_POSITION_CANCEL = 1;
|
|
|
|
const BUTTON_POSITION_DONT_SAVE = 2;
|
2012-09-05 21:20:05 -07:00
|
|
|
const BUTTON_POSITION_REVERT=0;
|
2011-04-20 01:18:00 -07:00
|
|
|
|
2012-07-30 14:39:00 -07:00
|
|
|
let keysbundle = Services.strings.createBundle("chrome://global-platform/locale/platformKeys.properties");
|
|
|
|
|
|
|
|
|
|
|
|
function SP_Pretty_Key(aElemKey) {
|
|
|
|
|
|
|
|
let elemString = "";
|
|
|
|
let elemMod = aElemKey.getAttribute("modifiers");
|
|
|
|
|
|
|
|
if (elemMod.match("accel")) {
|
|
|
|
if (navigator.platform.indexOf("Mac") !== -1) {
|
2012-08-28 02:56:30 -07:00
|
|
|
// XXX bug 779642 Use "Cmd-" literal vs cloverleaf meta-key until
|
|
|
|
// Orion adds variable height lines
|
|
|
|
// elemString += keysbundle.GetStringFromName("VK_META_CMD") +
|
|
|
|
// keysbundle.GetStringFromName("MODIFIER_SEPARATOR");
|
|
|
|
elemString += "Cmd-";
|
2012-07-30 14:39:00 -07:00
|
|
|
} else {
|
|
|
|
elemString += keysbundle.GetStringFromName("VK_CONTROL") +
|
|
|
|
keysbundle.GetStringFromName("MODIFIER_SEPARATOR");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (elemMod.match("shift")) {
|
|
|
|
elemString += keysbundle.GetStringFromName("VK_SHIFT") +
|
|
|
|
keysbundle.GetStringFromName("MODIFIER_SEPARATOR");
|
|
|
|
}
|
|
|
|
if (elemMod.match("alt")) {
|
|
|
|
elemString += keysbundle.GetStringFromName("VK_ALT") +
|
|
|
|
keysbundle.GetStringFromName("MODIFIER_SEPARATOR");
|
|
|
|
}
|
|
|
|
if (elemMod.match("ctrl")) {
|
|
|
|
elemString += keysbundle.GetStringFromName("VK_CONTROL") +
|
|
|
|
keysbundle.GetStringFromName("MODIFIER_SEPARATOR");
|
|
|
|
}
|
|
|
|
if (elemMod.match("meta")) {
|
|
|
|
elemString += keysbundle.GetStringFromName("VK_META") +
|
|
|
|
keysbundle.GetStringFromName("MODIFIER_SEPARATOR");
|
|
|
|
}
|
|
|
|
|
|
|
|
return elemString + aElemKey.getAttribute("key").toUpperCase();
|
|
|
|
}
|
|
|
|
|
2011-04-20 01:18:00 -07:00
|
|
|
/**
|
2011-05-09 07:51:52 -07:00
|
|
|
* The scratchpad object handles the Scratchpad window functionality.
|
2011-04-20 01:18:00 -07:00
|
|
|
*/
|
2011-05-09 07:51:52 -07:00
|
|
|
var Scratchpad = {
|
2012-02-17 09:11:17 -08:00
|
|
|
_initialWindowTitle: document.title,
|
|
|
|
|
2011-04-20 01:18:00 -07:00
|
|
|
/**
|
2011-05-09 07:51:52 -07:00
|
|
|
* The script execution context. This tells Scratchpad in which context the
|
2011-04-20 01:18:00 -07:00
|
|
|
* script shall execute.
|
|
|
|
*
|
|
|
|
* Possible values:
|
2011-05-09 07:51:52 -07:00
|
|
|
* - SCRATCHPAD_CONTEXT_CONTENT to execute code in the context of the current
|
2011-04-20 01:18:00 -07:00
|
|
|
* tab content window object.
|
2011-05-19 14:10:41 -07:00
|
|
|
* - SCRATCHPAD_CONTEXT_BROWSER to execute code in the context of the
|
2011-04-20 01:18:00 -07:00
|
|
|
* currently active chrome window object.
|
|
|
|
*/
|
2011-05-09 07:51:52 -07:00
|
|
|
executionContext: SCRATCHPAD_CONTEXT_CONTENT,
|
2011-04-20 01:18:00 -07:00
|
|
|
|
2011-12-10 09:03:57 -08:00
|
|
|
/**
|
|
|
|
* Tells if this Scratchpad is initialized and ready for use.
|
|
|
|
* @boolean
|
|
|
|
* @see addObserver
|
|
|
|
*/
|
|
|
|
initialized: false,
|
|
|
|
|
2011-04-20 01:18:00 -07:00
|
|
|
/**
|
2011-10-26 04:35:32 -07:00
|
|
|
* Retrieve the xul:notificationbox DOM element. It notifies the user when
|
|
|
|
* the current code execution context is SCRATCHPAD_CONTEXT_BROWSER.
|
2011-04-20 01:18:00 -07:00
|
|
|
*/
|
2011-10-26 04:35:32 -07:00
|
|
|
get notificationBox() document.getElementById("scratchpad-notificationbox"),
|
2011-04-20 01:18:00 -07:00
|
|
|
|
|
|
|
/**
|
2011-08-11 14:35:10 -07:00
|
|
|
* Get the selected text from the editor.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* The selected text.
|
2011-04-20 01:18:00 -07:00
|
|
|
*/
|
2011-08-11 14:35:10 -07:00
|
|
|
get selectedText() this.editor.getSelectedText(),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the editor content, in the given range. If no range is given you get
|
|
|
|
* the entire editor content.
|
|
|
|
*
|
|
|
|
* @param number [aStart=0]
|
|
|
|
* Optional, start from the given offset.
|
|
|
|
* @param number [aEnd=content char count]
|
|
|
|
* Optional, end offset for the text you want. If this parameter is not
|
|
|
|
* given, then the text returned goes until the end of the editor
|
|
|
|
* content.
|
|
|
|
* @return string
|
|
|
|
* The text in the given range.
|
|
|
|
*/
|
|
|
|
getText: function SP_getText(aStart, aEnd)
|
2011-04-20 01:18:00 -07:00
|
|
|
{
|
2011-08-11 14:35:10 -07:00
|
|
|
return this.editor.getText(aStart, aEnd);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace text in the source editor with the given text, in the given range.
|
|
|
|
*
|
|
|
|
* @param string aText
|
|
|
|
* The text you want to put into the editor.
|
|
|
|
* @param number [aStart=0]
|
|
|
|
* Optional, the start offset, zero based, from where you want to start
|
|
|
|
* replacing text in the editor.
|
|
|
|
* @param number [aEnd=char count]
|
|
|
|
* Optional, the end offset, zero based, where you want to stop
|
|
|
|
* replacing text in the editor.
|
|
|
|
*/
|
|
|
|
setText: function SP_setText(aText, aStart, aEnd)
|
|
|
|
{
|
|
|
|
this.editor.setText(aText, aStart, aEnd);
|
2011-04-20 01:18:00 -07:00
|
|
|
},
|
|
|
|
|
2011-11-02 12:32:55 -07:00
|
|
|
/**
|
|
|
|
* Set the filename in the scratchpad UI and object
|
|
|
|
*
|
|
|
|
* @param string aFilename
|
|
|
|
* The new filename
|
|
|
|
*/
|
|
|
|
setFilename: function SP_setFilename(aFilename)
|
|
|
|
{
|
2012-02-17 09:11:17 -08:00
|
|
|
this.filename = aFilename;
|
|
|
|
this._updateTitle();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the Scratchpad window title based on the current state.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_updateTitle: function SP__updateTitle()
|
|
|
|
{
|
|
|
|
if (this.filename) {
|
|
|
|
document.title = (this.editor && this.editor.dirty ? "*" : "") +
|
|
|
|
this.filename;
|
|
|
|
} else {
|
|
|
|
document.title = this._initialWindowTitle;
|
|
|
|
}
|
2011-11-02 12:32:55 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current state of the scratchpad. Called by the
|
|
|
|
* Scratchpad Manager for session storing.
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
* An object with 3 properties: filename, text, and
|
|
|
|
* executionContext.
|
|
|
|
*/
|
|
|
|
getState: function SP_getState()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
filename: this.filename,
|
|
|
|
text: this.getText(),
|
2011-11-09 10:09:54 -08:00
|
|
|
executionContext: this.executionContext,
|
2012-02-17 09:11:17 -08:00
|
|
|
saved: !this.editor.dirty,
|
2011-11-02 12:32:55 -07:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the filename and execution context using the given state. Called
|
|
|
|
* when scratchpad is being restored from a previous session.
|
|
|
|
*
|
|
|
|
* @param object aState
|
|
|
|
* An object with filename and executionContext properties.
|
|
|
|
*/
|
2012-07-03 06:40:12 -07:00
|
|
|
setState: function SP_setState(aState)
|
2011-11-02 12:32:55 -07:00
|
|
|
{
|
|
|
|
if (aState.filename) {
|
|
|
|
this.setFilename(aState.filename);
|
|
|
|
}
|
2012-02-17 09:11:17 -08:00
|
|
|
if (this.editor) {
|
|
|
|
this.editor.dirty = !aState.saved;
|
|
|
|
}
|
2011-11-02 12:32:55 -07:00
|
|
|
|
|
|
|
if (aState.executionContext == SCRATCHPAD_CONTEXT_BROWSER) {
|
|
|
|
this.setBrowserContext();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.setContentContext();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-04-20 01:18:00 -07:00
|
|
|
/**
|
|
|
|
* Get the most recent chrome window of type navigator:browser.
|
|
|
|
*/
|
|
|
|
get browserWindow() Services.wm.getMostRecentWindow("navigator:browser"),
|
|
|
|
|
2011-04-21 01:24:30 -07:00
|
|
|
/**
|
|
|
|
* Reference to the last chrome window of type navigator:browser. We use this
|
|
|
|
* to check if the chrome window changed since the last code evaluation.
|
|
|
|
*/
|
|
|
|
_previousWindow: null,
|
|
|
|
|
2011-04-20 01:18:00 -07:00
|
|
|
/**
|
|
|
|
* Get the gBrowser object of the most recent browser window.
|
|
|
|
*/
|
|
|
|
get gBrowser()
|
|
|
|
{
|
|
|
|
let recentWin = this.browserWindow;
|
|
|
|
return recentWin ? recentWin.gBrowser : null;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2011-04-21 01:24:30 -07:00
|
|
|
* Cached Cu.Sandbox object for the active tab content window object.
|
|
|
|
*/
|
|
|
|
_contentSandbox: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Cu.Sandbox object for the active tab content window object. Note
|
|
|
|
* that the returned object is cached for later reuse. The cached object is
|
2011-05-21 04:59:23 -07:00
|
|
|
* kept only for the current location in the current tab of the current
|
|
|
|
* browser window and it is reset for each context switch,
|
|
|
|
* navigator:browser window switch, tab switch or navigation.
|
2011-04-20 01:18:00 -07:00
|
|
|
*/
|
|
|
|
get contentSandbox()
|
|
|
|
{
|
|
|
|
if (!this.browserWindow) {
|
|
|
|
Cu.reportError(this.strings.
|
|
|
|
GetStringFromName("browserWindow.unavailable"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-21 01:24:30 -07:00
|
|
|
if (!this._contentSandbox ||
|
2011-05-21 04:59:23 -07:00
|
|
|
this.browserWindow != this._previousBrowserWindow ||
|
|
|
|
this._previousBrowser != this.gBrowser.selectedBrowser ||
|
|
|
|
this._previousLocation != this.gBrowser.contentWindow.location.href) {
|
2011-04-21 01:24:30 -07:00
|
|
|
let contentWindow = this.gBrowser.selectedBrowser.contentWindow;
|
|
|
|
this._contentSandbox = new Cu.Sandbox(contentWindow,
|
2011-08-21 16:02:24 -07:00
|
|
|
{ sandboxPrototype: contentWindow, wantXrays: false,
|
|
|
|
sandboxName: 'scratchpad-content'});
|
2011-04-21 01:24:30 -07:00
|
|
|
|
|
|
|
this._previousBrowserWindow = this.browserWindow;
|
2011-05-21 04:59:23 -07:00
|
|
|
this._previousBrowser = this.gBrowser.selectedBrowser;
|
|
|
|
this._previousLocation = contentWindow.location.href;
|
2011-04-21 01:24:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._contentSandbox;
|
2011-04-20 01:18:00 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2011-04-21 01:24:30 -07:00
|
|
|
* Cached Cu.Sandbox object for the most recently active navigator:browser
|
2011-04-20 01:18:00 -07:00
|
|
|
* chrome window object.
|
|
|
|
*/
|
2011-04-21 01:24:30 -07:00
|
|
|
_chromeSandbox: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Cu.Sandbox object for the most recently active navigator:browser
|
|
|
|
* chrome window object. Note that the returned object is cached for later
|
|
|
|
* reuse. The cached object is kept only for the current browser window and it
|
|
|
|
* is reset for each context switch or navigator:browser window switch.
|
|
|
|
*/
|
2011-04-20 01:18:00 -07:00
|
|
|
get chromeSandbox()
|
|
|
|
{
|
|
|
|
if (!this.browserWindow) {
|
|
|
|
Cu.reportError(this.strings.
|
|
|
|
GetStringFromName("browserWindow.unavailable"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-21 01:24:30 -07:00
|
|
|
if (!this._chromeSandbox ||
|
|
|
|
this.browserWindow != this._previousBrowserWindow) {
|
|
|
|
this._chromeSandbox = new Cu.Sandbox(this.browserWindow,
|
2011-08-21 16:02:24 -07:00
|
|
|
{ sandboxPrototype: this.browserWindow, wantXrays: false,
|
|
|
|
sandboxName: 'scratchpad-chrome'});
|
2012-05-01 08:17:32 -07:00
|
|
|
addDebuggerToGlobal(this._chromeSandbox);
|
2011-04-21 01:24:30 -07:00
|
|
|
|
|
|
|
this._previousBrowserWindow = this.browserWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._chromeSandbox;
|
2011-04-20 01:18:00 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2011-08-11 14:35:10 -07:00
|
|
|
* Drop the editor selection.
|
2011-04-20 01:18:00 -07:00
|
|
|
*/
|
2011-05-09 07:51:52 -07:00
|
|
|
deselect: function SP_deselect()
|
2011-04-20 01:18:00 -07:00
|
|
|
{
|
2011-08-11 14:35:10 -07:00
|
|
|
this.editor.dropSelection();
|
2011-04-20 01:18:00 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2011-08-11 14:35:10 -07:00
|
|
|
* Select a specific range in the Scratchpad editor.
|
2011-04-20 01:18:00 -07:00
|
|
|
*
|
|
|
|
* @param number aStart
|
|
|
|
* Selection range start.
|
|
|
|
* @param number aEnd
|
|
|
|
* Selection range end.
|
|
|
|
*/
|
2011-05-09 07:51:52 -07:00
|
|
|
selectRange: function SP_selectRange(aStart, aEnd)
|
2011-04-20 01:18:00 -07:00
|
|
|
{
|
2011-08-11 14:35:10 -07:00
|
|
|
this.editor.setSelection(aStart, aEnd);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current selection range.
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
* An object with two properties, start and end, that give the
|
|
|
|
* selection range (zero based offsets).
|
|
|
|
*/
|
|
|
|
getSelectionRange: function SP_getSelection()
|
|
|
|
{
|
|
|
|
return this.editor.getSelection();
|
2011-04-20 01:18:00 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluate a string in the active tab content window.
|
|
|
|
*
|
|
|
|
* @param string aString
|
|
|
|
* The script you want evaluated.
|
|
|
|
* @return mixed
|
|
|
|
* The script evaluation result.
|
|
|
|
*/
|
2011-05-09 07:51:52 -07:00
|
|
|
evalInContentSandbox: function SP_evalInContentSandbox(aString)
|
2011-04-20 01:18:00 -07:00
|
|
|
{
|
2011-11-03 08:39:32 -07:00
|
|
|
let error, result;
|
2011-04-20 01:18:00 -07:00
|
|
|
try {
|
|
|
|
result = Cu.evalInSandbox(aString, this.contentSandbox, "1.8",
|
2011-05-09 07:51:52 -07:00
|
|
|
"Scratchpad", 1);
|
2011-04-20 01:18:00 -07:00
|
|
|
}
|
|
|
|
catch (ex) {
|
2012-01-03 08:53:39 -08:00
|
|
|
error = ex;
|
2011-04-20 01:18:00 -07:00
|
|
|
}
|
|
|
|
|
2011-11-03 08:39:32 -07:00
|
|
|
return [error, result];
|
2011-04-20 01:18:00 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluate a string in the most recent navigator:browser chrome window.
|
|
|
|
*
|
|
|
|
* @param string aString
|
|
|
|
* The script you want evaluated.
|
|
|
|
* @return mixed
|
|
|
|
* The script evaluation result.
|
|
|
|
*/
|
2011-05-09 07:51:52 -07:00
|
|
|
evalInChromeSandbox: function SP_evalInChromeSandbox(aString)
|
2011-04-20 01:18:00 -07:00
|
|
|
{
|
2011-11-03 08:39:32 -07:00
|
|
|
let error, result;
|
2011-04-20 01:18:00 -07:00
|
|
|
try {
|
|
|
|
result = Cu.evalInSandbox(aString, this.chromeSandbox, "1.8",
|
2011-05-09 07:51:52 -07:00
|
|
|
"Scratchpad", 1);
|
2011-04-20 01:18:00 -07:00
|
|
|
}
|
|
|
|
catch (ex) {
|
2012-01-03 08:53:39 -08:00
|
|
|
error = ex;
|
2011-04-20 01:18:00 -07:00
|
|
|
}
|
|
|
|
|
2011-11-03 08:39:32 -07:00
|
|
|
return [error, result];
|
2011-04-20 01:18:00 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluate a string in the currently desired context, that is either the
|
|
|
|
* chrome window or the tab content window object.
|
|
|
|
*
|
|
|
|
* @param string aString
|
|
|
|
* The script you want to evaluate.
|
|
|
|
* @return mixed
|
|
|
|
* The script evaluation result.
|
|
|
|
*/
|
2011-05-09 07:51:52 -07:00
|
|
|
evalForContext: function SP_evaluateForContext(aString)
|
2011-04-20 01:18:00 -07:00
|
|
|
{
|
2011-05-09 07:51:52 -07:00
|
|
|
return this.executionContext == SCRATCHPAD_CONTEXT_CONTENT ?
|
2011-04-20 01:18:00 -07:00
|
|
|
this.evalInContentSandbox(aString) :
|
|
|
|
this.evalInChromeSandbox(aString);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2011-08-11 14:35:10 -07:00
|
|
|
* Execute the selected text (if any) or the entire editor content in the
|
2011-04-20 01:18:00 -07:00
|
|
|
* current context.
|
2012-01-03 08:53:39 -08:00
|
|
|
* @return mixed
|
|
|
|
* The script evaluation result.
|
2011-04-20 01:18:00 -07:00
|
|
|
*/
|
2012-01-03 08:53:39 -08:00
|
|
|
execute: function SP_execute()
|
2011-04-20 01:18:00 -07:00
|
|
|
{
|
2011-08-11 14:35:10 -07:00
|
|
|
let selection = this.selectedText || this.getText();
|
2011-11-03 08:39:32 -07:00
|
|
|
let [error, result] = this.evalForContext(selection);
|
2012-01-03 08:53:39 -08:00
|
|
|
return [selection, error, result];
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the selected text (if any) or the entire editor content in the
|
|
|
|
* current context.
|
|
|
|
*/
|
|
|
|
run: function SP_run()
|
|
|
|
{
|
|
|
|
let [selection, error, result] = this.execute();
|
|
|
|
|
|
|
|
if (!error) {
|
|
|
|
this.deselect();
|
|
|
|
} else {
|
|
|
|
this.writeAsErrorComment(error);
|
|
|
|
}
|
|
|
|
|
2011-11-03 08:39:32 -07:00
|
|
|
return [selection, error, result];
|
2011-04-20 01:18:00 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2011-08-11 14:35:10 -07:00
|
|
|
* Execute the selected text (if any) or the entire editor content in the
|
2011-04-20 01:18:00 -07:00
|
|
|
* current context. The resulting object is opened up in the Property Panel
|
|
|
|
* for inspection.
|
|
|
|
*/
|
2011-05-09 07:51:52 -07:00
|
|
|
inspect: function SP_inspect()
|
2011-04-20 01:18:00 -07:00
|
|
|
{
|
2012-01-03 08:53:39 -08:00
|
|
|
let [selection, error, result] = this.execute();
|
2011-04-20 01:18:00 -07:00
|
|
|
|
2011-11-03 08:39:32 -07:00
|
|
|
if (!error) {
|
2012-01-03 08:53:39 -08:00
|
|
|
this.deselect();
|
2011-04-20 01:18:00 -07:00
|
|
|
this.openPropertyPanel(selection, result);
|
2012-01-03 08:53:39 -08:00
|
|
|
} else {
|
|
|
|
this.writeAsErrorComment(error);
|
2011-04-20 01:18:00 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2011-08-11 14:35:10 -07:00
|
|
|
* Execute the selected text (if any) or the entire editor content in the
|
|
|
|
* current context. The evaluation result is inserted into the editor after
|
|
|
|
* the selected text, or at the end of the editor content if there is no
|
2011-04-20 01:18:00 -07:00
|
|
|
* selected text.
|
|
|
|
*/
|
2011-05-21 04:59:23 -07:00
|
|
|
display: function SP_display()
|
2012-01-03 08:53:39 -08:00
|
|
|
{
|
|
|
|
let [selectedText, error, result] = this.execute();
|
|
|
|
|
|
|
|
if (!error) {
|
|
|
|
this.writeAsComment(result);
|
|
|
|
} else {
|
|
|
|
this.writeAsErrorComment(error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2012-02-24 06:41:06 -08:00
|
|
|
* Write out a value at the next line from the current insertion point.
|
|
|
|
* The comment block will always be preceded by a newline character.
|
2012-01-03 08:53:39 -08:00
|
|
|
* @param object aValue
|
|
|
|
* The Object to write out as a string
|
|
|
|
*/
|
|
|
|
writeAsComment: function SP_writeAsComment(aValue)
|
2011-04-20 01:18:00 -07:00
|
|
|
{
|
2011-08-11 14:35:10 -07:00
|
|
|
let selection = this.getSelectionRange();
|
|
|
|
let insertionPoint = selection.start != selection.end ?
|
|
|
|
selection.end : // after selected text
|
|
|
|
this.editor.getCharCount(); // after text end
|
2012-01-03 08:53:39 -08:00
|
|
|
|
2012-02-24 06:41:06 -08:00
|
|
|
let newComment = "\n/*\n" + aValue + "\n*/";
|
2012-01-03 08:53:39 -08:00
|
|
|
|
2011-08-11 14:35:10 -07:00
|
|
|
this.setText(newComment, insertionPoint, insertionPoint);
|
2011-04-20 01:18:00 -07:00
|
|
|
|
2011-08-11 14:35:10 -07:00
|
|
|
// Select the new comment.
|
|
|
|
this.selectRange(insertionPoint, insertionPoint + newComment.length);
|
2011-04-20 01:18:00 -07:00
|
|
|
},
|
|
|
|
|
2012-01-03 08:53:39 -08:00
|
|
|
/**
|
|
|
|
* Write out an error at the current insertion point as a block comment
|
|
|
|
* @param object aValue
|
|
|
|
* The Error object to write out the message and stack trace
|
|
|
|
*/
|
|
|
|
writeAsErrorComment: function SP_writeAsErrorComment(aError)
|
|
|
|
{
|
2012-05-22 14:24:00 -07:00
|
|
|
let stack = "";
|
|
|
|
if (aError.stack) {
|
|
|
|
stack = aError.stack;
|
|
|
|
}
|
|
|
|
else if (aError.fileName) {
|
|
|
|
if (aError.lineNumber) {
|
|
|
|
stack = "@" + aError.fileName + ":" + aError.lineNumber;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
stack = "@" + aError.fileName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (aError.lineNumber) {
|
|
|
|
stack = "@" + aError.lineNumber;
|
|
|
|
}
|
2012-01-03 08:53:39 -08:00
|
|
|
|
2012-05-22 14:24:00 -07:00
|
|
|
let newComment = "Exception: " + ( aError.message || aError) + ( stack == "" ? stack : "\n" + stack.replace(/\n$/, "") );
|
|
|
|
|
2012-01-03 08:53:39 -08:00
|
|
|
this.writeAsComment(newComment);
|
|
|
|
},
|
|
|
|
|
2011-04-20 01:18:00 -07:00
|
|
|
/**
|
|
|
|
* Open the Property Panel to inspect the given object.
|
|
|
|
*
|
|
|
|
* @param string aEvalString
|
|
|
|
* The string that was evaluated. This is re-used when the user updates
|
|
|
|
* the properties list, by clicking the Update button.
|
|
|
|
* @param object aOutputObject
|
|
|
|
* The object to inspect, which is the aEvalString evaluation result.
|
|
|
|
* @return object
|
|
|
|
* The PropertyPanel object instance.
|
|
|
|
*/
|
2011-05-09 07:51:52 -07:00
|
|
|
openPropertyPanel: function SP_openPropertyPanel(aEvalString, aOutputObject)
|
2011-04-20 01:18:00 -07:00
|
|
|
{
|
|
|
|
let self = this;
|
|
|
|
let propPanel;
|
|
|
|
// The property panel has a button:
|
|
|
|
// `Update`: reexecutes the string executed on the command line. The
|
|
|
|
// result will be inspected by this panel.
|
|
|
|
let buttons = [];
|
|
|
|
|
|
|
|
// If there is a evalString passed to this function, then add a `Update`
|
|
|
|
// button to the panel so that the evalString can be reexecuted to update
|
|
|
|
// the content of the panel.
|
|
|
|
if (aEvalString !== null) {
|
|
|
|
buttons.push({
|
|
|
|
label: this.strings.
|
|
|
|
GetStringFromName("propertyPanel.updateButton.label"),
|
|
|
|
accesskey: this.strings.
|
|
|
|
GetStringFromName("propertyPanel.updateButton.accesskey"),
|
2012-05-25 03:28:47 -07:00
|
|
|
oncommand: function _SP_PP_Update_onCommand() {
|
2011-11-03 08:39:32 -07:00
|
|
|
let [error, result] = self.evalForContext(aEvalString);
|
2011-04-20 01:18:00 -07:00
|
|
|
|
2011-11-03 08:39:32 -07:00
|
|
|
if (!error) {
|
2012-05-25 03:28:47 -07:00
|
|
|
propPanel.treeView.data = { object: result };
|
2011-04-20 01:18:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let doc = this.browserWindow.document;
|
|
|
|
let parent = doc.getElementById("mainPopupSet");
|
2012-05-25 03:28:47 -07:00
|
|
|
let title = String(aOutputObject);
|
|
|
|
propPanel = new PropertyPanel(parent, title, { object: aOutputObject },
|
|
|
|
buttons);
|
2011-04-20 01:18:00 -07:00
|
|
|
|
|
|
|
let panel = propPanel.panel;
|
2011-05-09 07:51:52 -07:00
|
|
|
panel.setAttribute("class", "scratchpad_propertyPanel");
|
2011-04-20 01:18:00 -07:00
|
|
|
panel.openPopup(null, "after_pointer", 0, 0, false, false);
|
|
|
|
panel.sizeTo(200, 400);
|
|
|
|
|
|
|
|
return propPanel;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Menu Operations
|
|
|
|
|
|
|
|
/**
|
2011-05-09 07:51:52 -07:00
|
|
|
* Open a new Scratchpad window.
|
2011-11-15 00:41:37 -08:00
|
|
|
*
|
|
|
|
* @return nsIWindow
|
2011-04-20 01:18:00 -07:00
|
|
|
*/
|
2011-05-09 07:51:52 -07:00
|
|
|
openScratchpad: function SP_openScratchpad()
|
2011-04-20 01:18:00 -07:00
|
|
|
{
|
2011-11-15 00:41:37 -08:00
|
|
|
return ScratchpadManager.openScratchpad();
|
2011-04-20 01:18:00 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export the textbox content to a file.
|
|
|
|
*
|
|
|
|
* @param nsILocalFile aFile
|
|
|
|
* The file where you want to save the textbox content.
|
|
|
|
* @param boolean aNoConfirmation
|
|
|
|
* If the file already exists, ask for confirmation?
|
|
|
|
* @param boolean aSilentError
|
|
|
|
* True if you do not want to display an error when file save fails,
|
|
|
|
* false otherwise.
|
|
|
|
* @param function aCallback
|
|
|
|
* Optional function you want to call when file save completes. It will
|
|
|
|
* get the following arguments:
|
|
|
|
* 1) the nsresult status code for the export operation.
|
|
|
|
*/
|
2011-05-09 07:51:52 -07:00
|
|
|
exportToFile: function SP_exportToFile(aFile, aNoConfirmation, aSilentError,
|
2011-04-20 01:18:00 -07:00
|
|
|
aCallback)
|
|
|
|
{
|
|
|
|
if (!aNoConfirmation && aFile.exists() &&
|
|
|
|
!window.confirm(this.strings.
|
|
|
|
GetStringFromName("export.fileOverwriteConfirmation"))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let fs = Cc["@mozilla.org/network/file-output-stream;1"].
|
|
|
|
createInstance(Ci.nsIFileOutputStream);
|
|
|
|
let modeFlags = 0x02 | 0x08 | 0x20;
|
2011-08-11 14:35:10 -07:00
|
|
|
fs.init(aFile, modeFlags, 420 /* 0644 */, fs.DEFER_OPEN);
|
2011-04-20 01:18:00 -07:00
|
|
|
|
|
|
|
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
|
|
|
|
createInstance(Ci.nsIScriptableUnicodeConverter);
|
|
|
|
converter.charset = "UTF-8";
|
2011-08-11 14:35:10 -07:00
|
|
|
let input = converter.convertToInputStream(this.getText());
|
2011-04-20 01:18:00 -07:00
|
|
|
|
|
|
|
let self = this;
|
|
|
|
NetUtil.asyncCopy(input, fs, function(aStatus) {
|
|
|
|
if (!aSilentError && !Components.isSuccessCode(aStatus)) {
|
|
|
|
window.alert(self.strings.GetStringFromName("saveFile.failed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aCallback) {
|
|
|
|
aCallback.call(self, aStatus);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read the content of a file and put it into the textbox.
|
|
|
|
*
|
|
|
|
* @param nsILocalFile aFile
|
|
|
|
* The file you want to save the textbox content into.
|
|
|
|
* @param boolean aSilentError
|
|
|
|
* True if you do not want to display an error when file load fails,
|
|
|
|
* false otherwise.
|
|
|
|
* @param function aCallback
|
|
|
|
* Optional function you want to call when file load completes. It will
|
|
|
|
* get the following arguments:
|
|
|
|
* 1) the nsresult status code for the import operation.
|
|
|
|
* 2) the data that was read from the file, if any.
|
|
|
|
*/
|
2011-05-09 07:51:52 -07:00
|
|
|
importFromFile: function SP_importFromFile(aFile, aSilentError, aCallback)
|
2011-04-20 01:18:00 -07:00
|
|
|
{
|
|
|
|
// Prevent file type detection.
|
|
|
|
let channel = NetUtil.newChannel(aFile);
|
|
|
|
channel.contentType = "application/javascript";
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
NetUtil.asyncFetch(channel, function(aInputStream, aStatus) {
|
|
|
|
let content = null;
|
|
|
|
|
|
|
|
if (Components.isSuccessCode(aStatus)) {
|
2011-12-22 09:55:49 -08:00
|
|
|
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
|
|
|
|
createInstance(Ci.nsIScriptableUnicodeConverter);
|
|
|
|
converter.charset = "UTF-8";
|
2011-04-20 01:18:00 -07:00
|
|
|
content = NetUtil.readInputStreamToString(aInputStream,
|
|
|
|
aInputStream.available());
|
2011-12-22 09:55:49 -08:00
|
|
|
content = converter.ConvertToUnicode(content);
|
2011-08-11 14:35:10 -07:00
|
|
|
self.setText(content);
|
2011-12-20 10:03:16 -08:00
|
|
|
self.editor.resetUndo();
|
2011-04-20 01:18:00 -07:00
|
|
|
}
|
|
|
|
else if (!aSilentError) {
|
|
|
|
window.alert(self.strings.GetStringFromName("openFile.failed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aCallback) {
|
|
|
|
aCallback.call(self, aStatus, content);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2011-05-09 07:51:52 -07:00
|
|
|
* Open a file to edit in the Scratchpad.
|
2012-07-03 06:40:12 -07:00
|
|
|
*
|
|
|
|
* @param integer aIndex
|
|
|
|
* Optional integer: clicked menuitem in the 'Open Recent'-menu.
|
2011-04-20 01:18:00 -07:00
|
|
|
*/
|
2012-07-03 06:40:12 -07:00
|
|
|
openFile: function SP_openFile(aIndex)
|
2011-04-20 01:18:00 -07:00
|
|
|
{
|
2012-07-03 06:40:12 -07:00
|
|
|
let fp;
|
|
|
|
if (!aIndex && aIndex !== 0) {
|
|
|
|
fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
|
|
|
|
fp.init(window, this.strings.GetStringFromName("openFile.title"),
|
|
|
|
Ci.nsIFilePicker.modeOpen);
|
|
|
|
fp.defaultString = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aIndex > -1 || fp.show() != Ci.nsIFilePicker.returnCancel) {
|
|
|
|
this.promptSave(function(aCloseFile, aSaved, aStatus) {
|
|
|
|
let shouldOpen = aCloseFile;
|
|
|
|
if (aSaved && !Components.isSuccessCode(aStatus)) {
|
|
|
|
shouldOpen = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldOpen) {
|
|
|
|
this._skipClosePrompt = true;
|
|
|
|
|
|
|
|
let file;
|
|
|
|
if (fp) {
|
|
|
|
file = fp.file;
|
|
|
|
} else {
|
|
|
|
file = Components.classes["@mozilla.org/file/local;1"].
|
|
|
|
createInstance(Components.interfaces.nsILocalFile);
|
|
|
|
let filePath = this.getRecentFiles()[aIndex];
|
|
|
|
file.initWithPath(filePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setFilename(file.path);
|
|
|
|
this.importFromFile(file, false);
|
|
|
|
this.setRecentFile(file);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get recent files.
|
|
|
|
*
|
|
|
|
* @return Array
|
|
|
|
* File paths.
|
|
|
|
*/
|
|
|
|
getRecentFiles: function SP_getRecentFiles()
|
|
|
|
{
|
|
|
|
let maxRecent = Services.prefs.getIntPref(PREF_RECENT_FILES_MAX);
|
|
|
|
let branch = Services.prefs.
|
|
|
|
getBranch("devtools.scratchpad.");
|
|
|
|
|
|
|
|
let filePaths = [];
|
|
|
|
if (branch.prefHasUserValue("recentFilePaths")) {
|
|
|
|
filePaths = JSON.parse(branch.getCharPref("recentFilePaths"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return filePaths;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save a recent file in a JSON parsable string.
|
|
|
|
*
|
|
|
|
* @param nsILocalFile aFile
|
|
|
|
* The nsILocalFile we want to save as a recent file.
|
|
|
|
*/
|
|
|
|
setRecentFile: function SP_setRecentFile(aFile)
|
|
|
|
{
|
|
|
|
let maxRecent = Services.prefs.getIntPref(PREF_RECENT_FILES_MAX);
|
|
|
|
if (maxRecent < 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let filePaths = this.getRecentFiles();
|
|
|
|
let filesCount = filePaths.length;
|
|
|
|
let pathIndex = filePaths.indexOf(aFile.path);
|
|
|
|
|
|
|
|
// We are already storing this file in the list of recent files.
|
|
|
|
if (pathIndex > -1) {
|
|
|
|
// If it's already the most recent file, we don't have to do anything.
|
|
|
|
if (pathIndex === (filesCount - 1)) {
|
|
|
|
// Updating the menu to clear the disabled state from the wrong menuitem
|
|
|
|
// in rare cases when two or more Scratchpad windows are open and the
|
|
|
|
// same file has been opened in two or more windows.
|
|
|
|
this.populateRecentFilesMenu();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is not the most recent file. Remove it from the list, we add it as
|
|
|
|
// the most recent farther down.
|
|
|
|
filePaths.splice(pathIndex, 1);
|
|
|
|
}
|
|
|
|
// If we are not storing the file and the 'recent files'-list is full,
|
|
|
|
// remove the oldest file from the list.
|
|
|
|
else if (filesCount === maxRecent) {
|
|
|
|
filePaths.shift();
|
|
|
|
}
|
|
|
|
|
|
|
|
filePaths.push(aFile.path);
|
|
|
|
|
|
|
|
let branch = Services.prefs.
|
|
|
|
getBranch("devtools.scratchpad.");
|
|
|
|
branch.setCharPref("recentFilePaths", JSON.stringify(filePaths));
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populates the 'Open Recent'-menu.
|
|
|
|
*/
|
|
|
|
populateRecentFilesMenu: function SP_populateRecentFilesMenu()
|
|
|
|
{
|
|
|
|
let maxRecent = Services.prefs.getIntPref(PREF_RECENT_FILES_MAX);
|
|
|
|
let recentFilesMenu = document.getElementById("sp-open_recent-menu");
|
|
|
|
|
|
|
|
if (maxRecent < 1) {
|
|
|
|
recentFilesMenu.setAttribute("hidden", true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let recentFilesPopup = recentFilesMenu.firstChild;
|
|
|
|
let filePaths = this.getRecentFiles();
|
|
|
|
let filename = this.getState().filename;
|
|
|
|
|
|
|
|
recentFilesMenu.setAttribute("disabled", true);
|
|
|
|
while (recentFilesPopup.hasChildNodes()) {
|
|
|
|
recentFilesPopup.removeChild(recentFilesPopup.firstChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filePaths.length > 0) {
|
|
|
|
recentFilesMenu.removeAttribute("disabled");
|
|
|
|
|
|
|
|
// Print out menuitems with the most recent file first.
|
|
|
|
for (let i = filePaths.length - 1; i >= 0; --i) {
|
|
|
|
let menuitem = document.createElement("menuitem");
|
|
|
|
menuitem.setAttribute("type", "radio");
|
|
|
|
menuitem.setAttribute("label", filePaths[i]);
|
|
|
|
|
|
|
|
if (filePaths[i] === filename) {
|
|
|
|
menuitem.setAttribute("checked", true);
|
|
|
|
menuitem.setAttribute("disabled", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
menuitem.setAttribute("oncommand", "Scratchpad.openFile(" + i + ");");
|
|
|
|
recentFilesPopup.appendChild(menuitem);
|
|
|
|
}
|
|
|
|
|
|
|
|
recentFilesPopup.appendChild(document.createElement("menuseparator"));
|
|
|
|
let clearItems = document.createElement("menuitem");
|
|
|
|
clearItems.setAttribute("id", "sp-menu-clear_recent");
|
|
|
|
clearItems.setAttribute("label",
|
|
|
|
this.strings.
|
|
|
|
GetStringFromName("clearRecentMenuItems.label"));
|
|
|
|
clearItems.setAttribute("command", "sp-cmd-clearRecentFiles");
|
|
|
|
recentFilesPopup.appendChild(clearItems);
|
2011-04-20 01:18:00 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-07-03 06:40:12 -07:00
|
|
|
/**
|
|
|
|
* Clear all recent files.
|
|
|
|
*/
|
|
|
|
clearRecentFiles: function SP_clearRecentFiles()
|
|
|
|
{
|
|
|
|
Services.prefs.clearUserPref("devtools.scratchpad.recentFilePaths");
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle changes to the 'PREF_RECENT_FILES_MAX'-preference.
|
|
|
|
*/
|
|
|
|
handleRecentFileMaxChange: function SP_handleRecentFileMaxChange()
|
|
|
|
{
|
|
|
|
let maxRecent = Services.prefs.getIntPref(PREF_RECENT_FILES_MAX);
|
|
|
|
let menu = document.getElementById("sp-open_recent-menu");
|
|
|
|
|
|
|
|
// Hide the menu if the 'PREF_RECENT_FILES_MAX'-pref is set to zero or less.
|
|
|
|
if (maxRecent < 1) {
|
|
|
|
menu.setAttribute("hidden", true);
|
|
|
|
} else {
|
|
|
|
if (menu.hasAttribute("hidden")) {
|
|
|
|
if (!menu.firstChild.hasChildNodes()) {
|
|
|
|
this.populateRecentFilesMenu();
|
|
|
|
}
|
|
|
|
|
|
|
|
menu.removeAttribute("hidden");
|
|
|
|
}
|
|
|
|
|
|
|
|
let filePaths = this.getRecentFiles();
|
|
|
|
if (maxRecent < filePaths.length) {
|
|
|
|
let branch = Services.prefs.
|
|
|
|
getBranch("devtools.scratchpad.");
|
|
|
|
let diff = filePaths.length - maxRecent;
|
|
|
|
filePaths.splice(0, diff);
|
|
|
|
branch.setCharPref("recentFilePaths", JSON.stringify(filePaths));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2011-04-20 01:18:00 -07:00
|
|
|
/**
|
|
|
|
* Save the textbox content to the currently open file.
|
2011-11-23 18:49:03 -08:00
|
|
|
*
|
|
|
|
* @param function aCallback
|
|
|
|
* Optional function you want to call when file is saved
|
2011-04-20 01:18:00 -07:00
|
|
|
*/
|
2011-11-23 18:49:03 -08:00
|
|
|
saveFile: function SP_saveFile(aCallback)
|
2011-04-20 01:18:00 -07:00
|
|
|
{
|
|
|
|
if (!this.filename) {
|
2011-11-23 18:49:03 -08:00
|
|
|
return this.saveFileAs(aCallback);
|
2011-04-20 01:18:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
|
|
|
|
file.initWithPath(this.filename);
|
2011-11-23 18:49:03 -08:00
|
|
|
|
|
|
|
this.exportToFile(file, true, false, function(aStatus) {
|
2012-02-17 09:11:17 -08:00
|
|
|
if (Components.isSuccessCode(aStatus)) {
|
|
|
|
this.editor.dirty = false;
|
2012-07-03 06:40:12 -07:00
|
|
|
this.setRecentFile(file);
|
2012-02-17 09:11:17 -08:00
|
|
|
}
|
2011-11-23 18:49:03 -08:00
|
|
|
if (aCallback) {
|
|
|
|
aCallback(aStatus);
|
|
|
|
}
|
|
|
|
});
|
2011-04-20 01:18:00 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the textbox content to a new file.
|
2011-11-23 18:49:03 -08:00
|
|
|
*
|
|
|
|
* @param function aCallback
|
|
|
|
* Optional function you want to call when file is saved
|
2011-04-20 01:18:00 -07:00
|
|
|
*/
|
2011-11-23 18:49:03 -08:00
|
|
|
saveFileAs: function SP_saveFileAs(aCallback)
|
2011-04-20 01:18:00 -07:00
|
|
|
{
|
|
|
|
let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
|
|
|
|
fp.init(window, this.strings.GetStringFromName("saveFileAs"),
|
|
|
|
Ci.nsIFilePicker.modeSave);
|
2011-05-09 07:51:52 -07:00
|
|
|
fp.defaultString = "scratchpad.js";
|
2011-04-20 01:18:00 -07:00
|
|
|
if (fp.show() != Ci.nsIFilePicker.returnCancel) {
|
2011-11-09 10:09:54 -08:00
|
|
|
this.setFilename(fp.file.path);
|
2011-11-23 18:49:03 -08:00
|
|
|
|
|
|
|
this.exportToFile(fp.file, true, false, function(aStatus) {
|
2012-02-17 09:11:17 -08:00
|
|
|
if (Components.isSuccessCode(aStatus)) {
|
|
|
|
this.editor.dirty = false;
|
2012-07-03 06:40:12 -07:00
|
|
|
this.setRecentFile(fp.file);
|
2012-02-17 09:11:17 -08:00
|
|
|
}
|
2011-11-23 18:49:03 -08:00
|
|
|
if (aCallback) {
|
|
|
|
aCallback(aStatus);
|
|
|
|
}
|
|
|
|
});
|
2011-04-20 01:18:00 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-09-05 21:20:05 -07:00
|
|
|
/**
|
|
|
|
* Restore content from saved version of current file.
|
|
|
|
*
|
|
|
|
* @param function aCallback
|
|
|
|
* Optional function you want to call when file is saved
|
|
|
|
*/
|
|
|
|
revertFile: function SP_revertFile(aCallback)
|
|
|
|
{
|
|
|
|
|
|
|
|
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
|
|
|
|
file.initWithPath(this.filename);
|
|
|
|
|
|
|
|
if (!file.exists()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.importFromFile(file, false, function(aStatus, aContent) {
|
|
|
|
if (aCallback) {
|
|
|
|
aCallback(aStatus);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prompt to revert scratchpad if it has unsaved changes.
|
|
|
|
*
|
|
|
|
* @param function aCallback
|
|
|
|
* Optional function you want to call when file is saved. The callback
|
|
|
|
* receives three arguments:
|
|
|
|
* - aRevert (boolean) - tells if the file has been reverted.
|
|
|
|
* - status (number) - the file revert status result (if the file was
|
|
|
|
* saved).
|
|
|
|
*/
|
|
|
|
promptRevert: function SP_promptRervert(aCallback)
|
|
|
|
{
|
|
|
|
if (this.filename) {
|
|
|
|
let ps = Services.prompt;
|
|
|
|
let flags = ps.BUTTON_POS_0 * ps.BUTTON_TITLE_REVERT +
|
|
|
|
ps.BUTTON_POS_1 * ps.BUTTON_TITLE_CANCEL;
|
|
|
|
|
|
|
|
let button = ps.confirmEx(window,
|
|
|
|
this.strings.GetStringFromName("confirmRevert.title"),
|
|
|
|
this.strings.GetStringFromName("confirmRevert"),
|
|
|
|
flags, null, null, null, null, {});
|
|
|
|
if (button == BUTTON_POSITION_CANCEL) {
|
|
|
|
if (aCallback) {
|
|
|
|
aCallback(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (button == BUTTON_POSITION_REVERT) {
|
|
|
|
this.revertFile(function(aStatus) {
|
|
|
|
if(aCallback){
|
|
|
|
aCallback(true, aStatus);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (aCallback) {
|
|
|
|
aCallback(false);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-04-20 01:18:00 -07:00
|
|
|
/**
|
|
|
|
* Open the Error Console.
|
|
|
|
*/
|
2011-05-09 07:51:52 -07:00
|
|
|
openErrorConsole: function SP_openErrorConsole()
|
2011-04-20 01:18:00 -07:00
|
|
|
{
|
|
|
|
this.browserWindow.toJavaScriptConsole();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the Web Console.
|
|
|
|
*/
|
2011-05-09 07:51:52 -07:00
|
|
|
openWebConsole: function SP_openWebConsole()
|
2011-04-20 01:18:00 -07:00
|
|
|
{
|
|
|
|
if (!this.browserWindow.HUDConsoleUI.getOpenHUD()) {
|
|
|
|
this.browserWindow.HUDConsoleUI.toggleHUD();
|
|
|
|
}
|
|
|
|
this.browserWindow.focus();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the current execution context to be the active tab content window.
|
|
|
|
*/
|
2011-05-09 07:51:52 -07:00
|
|
|
setContentContext: function SP_setContentContext()
|
2011-04-20 01:18:00 -07:00
|
|
|
{
|
2011-10-26 04:35:32 -07:00
|
|
|
if (this.executionContext == SCRATCHPAD_CONTEXT_CONTENT) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-09 07:51:52 -07:00
|
|
|
let content = document.getElementById("sp-menu-content");
|
2011-05-19 14:10:41 -07:00
|
|
|
document.getElementById("sp-menu-browser").removeAttribute("checked");
|
2011-04-20 01:18:00 -07:00
|
|
|
content.setAttribute("checked", true);
|
2011-05-09 07:51:52 -07:00
|
|
|
this.executionContext = SCRATCHPAD_CONTEXT_CONTENT;
|
2011-10-26 04:35:32 -07:00
|
|
|
this.notificationBox.removeAllNotifications(false);
|
2011-04-21 01:24:30 -07:00
|
|
|
this.resetContext();
|
2011-04-20 01:18:00 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the current execution context to be the most recent chrome window.
|
|
|
|
*/
|
2011-05-19 14:10:41 -07:00
|
|
|
setBrowserContext: function SP_setBrowserContext()
|
2011-04-20 01:18:00 -07:00
|
|
|
{
|
2011-10-26 04:35:32 -07:00
|
|
|
if (this.executionContext == SCRATCHPAD_CONTEXT_BROWSER) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-21 04:59:23 -07:00
|
|
|
let browser = document.getElementById("sp-menu-browser");
|
2011-05-09 07:51:52 -07:00
|
|
|
document.getElementById("sp-menu-content").removeAttribute("checked");
|
2011-05-21 04:59:23 -07:00
|
|
|
browser.setAttribute("checked", true);
|
2011-05-19 14:10:41 -07:00
|
|
|
this.executionContext = SCRATCHPAD_CONTEXT_BROWSER;
|
2011-10-26 04:35:32 -07:00
|
|
|
this.notificationBox.appendNotification(
|
|
|
|
this.strings.GetStringFromName("browserContext.notification"),
|
|
|
|
SCRATCHPAD_CONTEXT_BROWSER,
|
|
|
|
null,
|
|
|
|
this.notificationBox.PRIORITY_WARNING_HIGH,
|
|
|
|
null);
|
2011-04-21 01:24:30 -07:00
|
|
|
this.resetContext();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the cached Cu.Sandbox object for the current context.
|
|
|
|
*/
|
2011-05-09 07:51:52 -07:00
|
|
|
resetContext: function SP_resetContext()
|
2011-04-21 01:24:30 -07:00
|
|
|
{
|
|
|
|
this._chromeSandbox = null;
|
|
|
|
this._contentSandbox = null;
|
|
|
|
this._previousWindow = null;
|
2011-05-21 04:59:23 -07:00
|
|
|
this._previousBrowser = null;
|
|
|
|
this._previousLocation = null;
|
2011-04-20 01:18:00 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2011-08-24 13:44:35 -07:00
|
|
|
* Gets the ID of the inner window of the given DOM window object.
|
2011-04-20 01:18:00 -07:00
|
|
|
*
|
|
|
|
* @param nsIDOMWindow aWindow
|
|
|
|
* @return integer
|
2011-08-24 13:44:35 -07:00
|
|
|
* the inner window ID
|
2011-04-20 01:18:00 -07:00
|
|
|
*/
|
2011-08-24 13:44:35 -07:00
|
|
|
getInnerWindowId: function SP_getInnerWindowId(aWindow)
|
2011-04-20 01:18:00 -07:00
|
|
|
{
|
|
|
|
return aWindow.QueryInterface(Ci.nsIInterfaceRequestor).
|
2011-08-24 13:44:35 -07:00
|
|
|
getInterface(Ci.nsIDOMWindowUtils).currentInnerWindowID;
|
2011-04-20 01:18:00 -07:00
|
|
|
},
|
2011-04-19 13:42:56 -07:00
|
|
|
|
|
|
|
/**
|
2011-12-10 09:03:57 -08:00
|
|
|
* The Scratchpad window load event handler. This method
|
2011-08-11 14:35:10 -07:00
|
|
|
* initializes the Scratchpad window and source editor.
|
|
|
|
*
|
|
|
|
* @param nsIDOMEvent aEvent
|
2011-04-19 13:42:56 -07:00
|
|
|
*/
|
2011-08-11 14:35:10 -07:00
|
|
|
onLoad: function SP_onLoad(aEvent)
|
2011-04-19 13:42:56 -07:00
|
|
|
{
|
2011-08-11 14:35:10 -07:00
|
|
|
if (aEvent.target != document) {
|
|
|
|
return;
|
|
|
|
}
|
2011-04-19 13:42:56 -07:00
|
|
|
let chrome = Services.prefs.getBoolPref(DEVTOOLS_CHROME_ENABLED);
|
|
|
|
if (chrome) {
|
2011-10-26 04:35:32 -07:00
|
|
|
let environmentMenu = document.getElementById("sp-environment-menu");
|
|
|
|
let errorConsoleCommand = document.getElementById("sp-cmd-errorConsole");
|
|
|
|
let chromeContextCommand = document.getElementById("sp-cmd-browserContext");
|
|
|
|
environmentMenu.removeAttribute("hidden");
|
2011-04-19 13:42:56 -07:00
|
|
|
chromeContextCommand.removeAttribute("disabled");
|
2011-10-26 04:35:32 -07:00
|
|
|
errorConsoleCommand.removeAttribute("disabled");
|
2011-04-19 13:42:56 -07:00
|
|
|
}
|
2011-06-14 03:44:51 -07:00
|
|
|
|
2012-02-17 09:11:17 -08:00
|
|
|
let state = null;
|
2012-07-30 14:39:00 -07:00
|
|
|
|
|
|
|
let initialText = this.strings.formatStringFromName(
|
2012-07-31 15:25:29 -07:00
|
|
|
"scratchpadIntro1",
|
2012-07-30 14:39:00 -07:00
|
|
|
[SP_Pretty_Key(document.getElementById("sp-key-run")),
|
|
|
|
SP_Pretty_Key(document.getElementById("sp-key-inspect")),
|
|
|
|
SP_Pretty_Key(document.getElementById("sp-key-display"))],
|
|
|
|
3);
|
|
|
|
|
2011-11-02 12:32:55 -07:00
|
|
|
if ("arguments" in window &&
|
|
|
|
window.arguments[0] instanceof Ci.nsIDialogParamBlock) {
|
2012-02-17 09:11:17 -08:00
|
|
|
state = JSON.parse(window.arguments[0].GetString(0));
|
2011-11-02 12:32:55 -07:00
|
|
|
this.setState(state);
|
|
|
|
initialText = state.text;
|
|
|
|
}
|
|
|
|
|
2011-08-11 14:35:10 -07:00
|
|
|
this.editor = new SourceEditor();
|
2011-06-14 03:44:51 -07:00
|
|
|
|
2011-08-11 14:35:10 -07:00
|
|
|
let config = {
|
|
|
|
mode: SourceEditor.MODES.JAVASCRIPT,
|
|
|
|
showLineNumbers: true,
|
2012-02-18 02:58:54 -08:00
|
|
|
initialText: initialText,
|
2012-02-27 10:08:45 -08:00
|
|
|
contextMenu: "scratchpad-text-popup",
|
2011-08-11 14:35:10 -07:00
|
|
|
};
|
2011-07-06 06:24:46 -07:00
|
|
|
|
2011-08-11 14:35:10 -07:00
|
|
|
let editorPlaceholder = document.getElementById("scratchpad-editor");
|
2012-02-17 09:11:17 -08:00
|
|
|
this.editor.init(editorPlaceholder, config,
|
|
|
|
this._onEditorLoad.bind(this, state));
|
2011-08-11 14:35:10 -07:00
|
|
|
},
|
2011-06-14 03:44:51 -07:00
|
|
|
|
2011-08-11 14:35:10 -07:00
|
|
|
/**
|
|
|
|
* The load event handler for the source editor. This method does post-load
|
|
|
|
* editor initialization.
|
2012-02-17 09:11:17 -08:00
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param object aState
|
|
|
|
* The initial Scratchpad state object.
|
2011-08-11 14:35:10 -07:00
|
|
|
*/
|
2012-02-17 09:11:17 -08:00
|
|
|
_onEditorLoad: function SP__onEditorLoad(aState)
|
2011-08-11 14:35:10 -07:00
|
|
|
{
|
2012-02-17 09:11:17 -08:00
|
|
|
this.editor.addEventListener(SourceEditor.EVENTS.DIRTY_CHANGED,
|
|
|
|
this._onDirtyChanged);
|
2011-08-11 14:35:10 -07:00
|
|
|
this.editor.focus();
|
|
|
|
this.editor.setCaretOffset(this.editor.getCharCount());
|
2012-02-17 09:11:17 -08:00
|
|
|
if (aState) {
|
|
|
|
this.editor.dirty = !aState.saved;
|
|
|
|
}
|
2011-12-10 09:03:57 -08:00
|
|
|
|
|
|
|
this.initialized = true;
|
|
|
|
|
2011-11-15 00:41:37 -08:00
|
|
|
this._triggerObservers("Ready");
|
2012-07-03 06:40:12 -07:00
|
|
|
|
|
|
|
this.populateRecentFilesMenu();
|
|
|
|
PreferenceObserver.init();
|
2011-08-11 14:35:10 -07:00
|
|
|
},
|
2011-06-14 03:44:51 -07:00
|
|
|
|
2011-08-11 14:35:10 -07:00
|
|
|
/**
|
|
|
|
* Insert text at the current caret location.
|
|
|
|
*
|
|
|
|
* @param string aText
|
|
|
|
* The text you want to insert.
|
|
|
|
*/
|
|
|
|
insertTextAtCaret: function SP_insertTextAtCaret(aText)
|
|
|
|
{
|
|
|
|
let caretOffset = this.editor.getCaretOffset();
|
|
|
|
this.setText(aText, caretOffset, caretOffset);
|
|
|
|
this.editor.setCaretOffset(caretOffset + aText.length);
|
2011-06-14 03:44:51 -07:00
|
|
|
},
|
|
|
|
|
2012-02-17 09:11:17 -08:00
|
|
|
/**
|
|
|
|
* The Source Editor DirtyChanged event handler. This function updates the
|
|
|
|
* Scratchpad window title to show an asterisk when there are unsaved changes.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @see SourceEditor.EVENTS.DIRTY_CHANGED
|
|
|
|
* @param object aEvent
|
|
|
|
* The DirtyChanged event object.
|
|
|
|
*/
|
|
|
|
_onDirtyChanged: function SP__onDirtyChanged(aEvent)
|
|
|
|
{
|
|
|
|
Scratchpad._updateTitle();
|
2012-09-05 21:20:05 -07:00
|
|
|
if (Scratchpad.filename) {
|
|
|
|
if (Scratchpad.editor.dirty) {
|
|
|
|
document.getElementById("sp-cmd-revert").removeAttribute("disabled");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
document.getElementById("sp-cmd-revert").setAttribute("disabled", true);
|
|
|
|
}
|
|
|
|
}
|
2012-02-17 09:11:17 -08:00
|
|
|
},
|
|
|
|
|
2011-08-11 14:35:10 -07:00
|
|
|
/**
|
|
|
|
* Undo the last action of the user.
|
|
|
|
*/
|
|
|
|
undo: function SP_undo()
|
|
|
|
{
|
|
|
|
this.editor.undo();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Redo the previously undone action.
|
|
|
|
*/
|
|
|
|
redo: function SP_redo()
|
|
|
|
{
|
|
|
|
this.editor.redo();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Scratchpad window unload event handler. This method unloads/destroys
|
|
|
|
* the source editor.
|
2011-06-14 03:44:51 -07:00
|
|
|
*
|
2011-08-11 14:35:10 -07:00
|
|
|
* @param nsIDOMEvent aEvent
|
2011-06-14 03:44:51 -07:00
|
|
|
*/
|
2011-08-11 14:35:10 -07:00
|
|
|
onUnload: function SP_onUnload(aEvent)
|
2011-06-14 03:44:51 -07:00
|
|
|
{
|
2011-08-11 14:35:10 -07:00
|
|
|
if (aEvent.target != document) {
|
|
|
|
return;
|
|
|
|
}
|
2011-06-14 03:44:51 -07:00
|
|
|
|
2011-08-11 14:35:10 -07:00
|
|
|
this.resetContext();
|
2012-02-17 09:11:17 -08:00
|
|
|
this.editor.removeEventListener(SourceEditor.EVENTS.DIRTY_CHANGED,
|
|
|
|
this._onDirtyChanged);
|
2012-07-03 06:40:12 -07:00
|
|
|
PreferenceObserver.uninit();
|
|
|
|
|
2011-08-11 14:35:10 -07:00
|
|
|
this.editor.destroy();
|
|
|
|
this.editor = null;
|
2011-12-10 09:03:57 -08:00
|
|
|
this.initialized = false;
|
2011-04-19 13:42:56 -07:00
|
|
|
},
|
2011-11-15 00:41:37 -08:00
|
|
|
|
2011-11-23 18:49:03 -08:00
|
|
|
/**
|
|
|
|
* Prompt to save scratchpad if it has unsaved changes.
|
|
|
|
*
|
|
|
|
* @param function aCallback
|
2012-02-17 09:11:17 -08:00
|
|
|
* Optional function you want to call when file is saved. The callback
|
|
|
|
* receives three arguments:
|
|
|
|
* - toClose (boolean) - tells if the window should be closed.
|
|
|
|
* - saved (boolen) - tells if the file has been saved.
|
|
|
|
* - status (number) - the file save status result (if the file was
|
|
|
|
* saved).
|
2011-11-23 18:49:03 -08:00
|
|
|
* @return boolean
|
|
|
|
* Whether the window should be closed
|
|
|
|
*/
|
|
|
|
promptSave: function SP_promptSave(aCallback)
|
|
|
|
{
|
2012-02-17 09:11:17 -08:00
|
|
|
if (this.filename && this.editor.dirty) {
|
2011-11-23 18:49:03 -08:00
|
|
|
let ps = Services.prompt;
|
|
|
|
let flags = ps.BUTTON_POS_0 * ps.BUTTON_TITLE_SAVE +
|
|
|
|
ps.BUTTON_POS_1 * ps.BUTTON_TITLE_CANCEL +
|
|
|
|
ps.BUTTON_POS_2 * ps.BUTTON_TITLE_DONT_SAVE;
|
|
|
|
|
|
|
|
let button = ps.confirmEx(window,
|
|
|
|
this.strings.GetStringFromName("confirmClose.title"),
|
|
|
|
this.strings.GetStringFromName("confirmClose"),
|
|
|
|
flags, null, null, null, null, {});
|
|
|
|
|
|
|
|
if (button == BUTTON_POSITION_CANCEL) {
|
2012-02-17 09:11:17 -08:00
|
|
|
if (aCallback) {
|
|
|
|
aCallback(false, false);
|
|
|
|
}
|
2011-11-23 18:49:03 -08:00
|
|
|
return false;
|
|
|
|
}
|
2012-02-17 09:11:17 -08:00
|
|
|
|
2011-11-23 18:49:03 -08:00
|
|
|
if (button == BUTTON_POSITION_SAVE) {
|
2012-02-17 09:11:17 -08:00
|
|
|
this.saveFile(function(aStatus) {
|
|
|
|
if (aCallback) {
|
|
|
|
aCallback(true, true, aStatus);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return true;
|
2011-11-23 18:49:03 -08:00
|
|
|
}
|
|
|
|
}
|
2012-02-17 09:11:17 -08:00
|
|
|
|
|
|
|
if (aCallback) {
|
|
|
|
aCallback(true, false);
|
|
|
|
}
|
2011-11-23 18:49:03 -08:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for window close event. Prompts to save scratchpad if
|
|
|
|
* there are unsaved changes.
|
|
|
|
*
|
|
|
|
* @param nsIDOMEvent aEvent
|
|
|
|
*/
|
|
|
|
onClose: function SP_onClose(aEvent)
|
|
|
|
{
|
2012-02-17 09:11:17 -08:00
|
|
|
if (this._skipClosePrompt) {
|
|
|
|
return;
|
2011-11-23 18:49:03 -08:00
|
|
|
}
|
2012-02-17 09:11:17 -08:00
|
|
|
|
|
|
|
this.promptSave(function(aShouldClose, aSaved, aStatus) {
|
|
|
|
let shouldClose = aShouldClose;
|
|
|
|
if (aSaved && !Components.isSuccessCode(aStatus)) {
|
|
|
|
shouldClose = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldClose) {
|
|
|
|
this._skipClosePrompt = true;
|
|
|
|
window.close();
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
aEvent.preventDefault();
|
2011-11-23 18:49:03 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close the scratchpad window. Prompts before closing if the scratchpad
|
|
|
|
* has unsaved changes.
|
|
|
|
*
|
|
|
|
* @param function aCallback
|
|
|
|
* Optional function you want to call when file is saved
|
|
|
|
*/
|
|
|
|
close: function SP_close(aCallback)
|
|
|
|
{
|
2012-02-17 09:11:17 -08:00
|
|
|
this.promptSave(function(aShouldClose, aSaved, aStatus) {
|
|
|
|
let shouldClose = aShouldClose;
|
|
|
|
if (aSaved && !Components.isSuccessCode(aStatus)) {
|
|
|
|
shouldClose = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldClose) {
|
|
|
|
this._skipClosePrompt = true;
|
|
|
|
window.close();
|
|
|
|
}
|
|
|
|
if (aCallback) {
|
|
|
|
aCallback();
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2011-11-23 18:49:03 -08:00
|
|
|
},
|
|
|
|
|
2011-11-15 00:41:37 -08:00
|
|
|
_observers: [],
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an observer for Scratchpad events.
|
|
|
|
*
|
|
|
|
* The observer implements IScratchpadObserver := {
|
|
|
|
* onReady: Called when the Scratchpad and its SourceEditor are ready.
|
|
|
|
* Arguments: (Scratchpad aScratchpad)
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* All observer handlers are optional.
|
|
|
|
*
|
|
|
|
* @param IScratchpadObserver aObserver
|
|
|
|
* @see removeObserver
|
|
|
|
*/
|
|
|
|
addObserver: function SP_addObserver(aObserver)
|
|
|
|
{
|
|
|
|
this._observers.push(aObserver);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an observer for Scratchpad events.
|
|
|
|
*
|
|
|
|
* @param IScratchpadObserver aObserver
|
|
|
|
* @see addObserver
|
|
|
|
*/
|
|
|
|
removeObserver: function SP_removeObserver(aObserver)
|
|
|
|
{
|
|
|
|
let index = this._observers.indexOf(aObserver);
|
|
|
|
if (index != -1) {
|
|
|
|
this._observers.splice(index, 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger named handlers in Scratchpad observers.
|
|
|
|
*
|
|
|
|
* @param string aName
|
|
|
|
* Name of the handler to trigger.
|
|
|
|
* @param Array aArgs
|
|
|
|
* Optional array of arguments to pass to the observer(s).
|
|
|
|
* @see addObserver
|
|
|
|
*/
|
|
|
|
_triggerObservers: function SP_triggerObservers(aName, aArgs)
|
|
|
|
{
|
|
|
|
// insert this Scratchpad instance as the first argument
|
|
|
|
if (!aArgs) {
|
|
|
|
aArgs = [this];
|
|
|
|
} else {
|
|
|
|
aArgs.unshift(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// trigger all observers that implement this named handler
|
|
|
|
for (let i = 0; i < this._observers.length; ++i) {
|
|
|
|
let observer = this._observers[i];
|
|
|
|
let handler = observer["on" + aName];
|
|
|
|
if (handler) {
|
|
|
|
handler.apply(observer, aArgs);
|
|
|
|
}
|
|
|
|
}
|
2012-02-21 03:09:29 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
openDocumentationPage: function SP_openDocumentationPage()
|
|
|
|
{
|
|
|
|
let url = this.strings.GetStringFromName("help.openDocumentationPage");
|
|
|
|
let newTab = this.gBrowser.addTab(url);
|
|
|
|
this.browserWindow.focus();
|
|
|
|
this.gBrowser.selectedTab = newTab;
|
|
|
|
},
|
2011-04-20 01:18:00 -07:00
|
|
|
};
|
|
|
|
|
2012-07-03 06:40:12 -07:00
|
|
|
/**
|
|
|
|
* The PreferenceObserver listens for preference changes while Scratchpad is
|
|
|
|
* running.
|
|
|
|
*/
|
|
|
|
var PreferenceObserver = {
|
|
|
|
_initialized: false,
|
|
|
|
|
|
|
|
init: function PO_init()
|
|
|
|
{
|
|
|
|
if (this._initialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.branch = Services.prefs.getBranch("devtools.scratchpad.");
|
|
|
|
this.branch.addObserver("", this, false);
|
|
|
|
this._initialized = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
observe: function PO_observe(aMessage, aTopic, aData)
|
|
|
|
{
|
|
|
|
if (aTopic != "nsPref:changed") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aData == "recentFilesMax") {
|
|
|
|
Scratchpad.handleRecentFileMaxChange();
|
|
|
|
}
|
|
|
|
else if (aData == "recentFilePaths") {
|
|
|
|
Scratchpad.populateRecentFilesMenu();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
uninit: function PO_uninit () {
|
|
|
|
if (!this.branch) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.branch.removeObserver("", this);
|
|
|
|
this.branch = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-05-09 07:51:52 -07:00
|
|
|
XPCOMUtils.defineLazyGetter(Scratchpad, "strings", function () {
|
|
|
|
return Services.strings.createBundle(SCRATCHPAD_L10N);
|
2011-04-20 01:18:00 -07:00
|
|
|
});
|
|
|
|
|
2011-12-10 09:03:57 -08:00
|
|
|
addEventListener("load", Scratchpad.onLoad.bind(Scratchpad), false);
|
2011-08-11 14:35:10 -07:00
|
|
|
addEventListener("unload", Scratchpad.onUnload.bind(Scratchpad), false);
|
2011-11-23 18:49:03 -08:00
|
|
|
addEventListener("close", Scratchpad.onClose.bind(Scratchpad), false);
|