зеркало из https://github.com/mozilla/gecko-dev.git
Bug 653427 - Prompt to save file before closing Scratchpad window, r=msucan
This commit is contained in:
Родитель
48181cd8d6
Коммит
eb35df5d5b
|
@ -66,6 +66,9 @@ const SCRATCHPAD_CONTEXT_CONTENT = 1;
|
|||
const SCRATCHPAD_CONTEXT_BROWSER = 2;
|
||||
const SCRATCHPAD_L10N = "chrome://browser/locale/devtools/scratchpad.properties";
|
||||
const DEVTOOLS_CHROME_ENABLED = "devtools.chrome.enabled";
|
||||
const BUTTON_POSITION_SAVE = 0;
|
||||
const BUTTON_POSITION_CANCEL = 1;
|
||||
const BUTTON_POSITION_DONT_SAVE = 2;
|
||||
|
||||
/**
|
||||
* The scratchpad object handles the Scratchpad window functionality.
|
||||
|
@ -601,22 +604,34 @@ var Scratchpad = {
|
|||
|
||||
/**
|
||||
* Save the textbox content to the currently open file.
|
||||
*
|
||||
* @param function aCallback
|
||||
* Optional function you want to call when file is saved
|
||||
*/
|
||||
saveFile: function SP_saveFile()
|
||||
saveFile: function SP_saveFile(aCallback)
|
||||
{
|
||||
if (!this.filename) {
|
||||
return this.saveFileAs();
|
||||
return this.saveFileAs(aCallback);
|
||||
}
|
||||
|
||||
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
|
||||
file.initWithPath(this.filename);
|
||||
this.exportToFile(file, true, false, this.onTextSaved.bind(this));
|
||||
|
||||
this.exportToFile(file, true, false, function(aStatus) {
|
||||
this.onTextSaved();
|
||||
if (aCallback) {
|
||||
aCallback(aStatus);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Save the textbox content to a new file.
|
||||
*
|
||||
* @param function aCallback
|
||||
* Optional function you want to call when file is saved
|
||||
*/
|
||||
saveFileAs: function SP_saveFileAs()
|
||||
saveFileAs: function SP_saveFileAs(aCallback)
|
||||
{
|
||||
let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
|
||||
fp.init(window, this.strings.GetStringFromName("saveFileAs"),
|
||||
|
@ -624,7 +639,13 @@ var Scratchpad = {
|
|||
fp.defaultString = "scratchpad.js";
|
||||
if (fp.show() != Ci.nsIFilePicker.returnCancel) {
|
||||
this.setFilename(fp.file.path);
|
||||
this.exportToFile(fp.file, true, false, this.onTextSaved.bind(this));
|
||||
|
||||
this.exportToFile(fp.file, true, false, function(aStatus) {
|
||||
this.onTextSaved();
|
||||
if (aCallback) {
|
||||
aCallback(aStatus);
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
|
@ -845,6 +866,9 @@ var Scratchpad = {
|
|||
if (aStatus && !Components.isSuccessCode(aStatus)) {
|
||||
return;
|
||||
}
|
||||
if (!document) {
|
||||
return; // file saved to disk after window has closed
|
||||
}
|
||||
document.title = document.title.replace(/^\*/, "");
|
||||
this.saved = true;
|
||||
this.editor.addEventListener(SourceEditor.EVENTS.TEXT_CHANGED,
|
||||
|
@ -882,6 +906,66 @@ var Scratchpad = {
|
|||
this.editor = null;
|
||||
},
|
||||
|
||||
/**
|
||||
* Prompt to save scratchpad if it has unsaved changes.
|
||||
*
|
||||
* @param function aCallback
|
||||
* Optional function you want to call when file is saved
|
||||
* @return boolean
|
||||
* Whether the window should be closed
|
||||
*/
|
||||
promptSave: function SP_promptSave(aCallback)
|
||||
{
|
||||
if (this.filename && !this.saved) {
|
||||
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) {
|
||||
return false;
|
||||
}
|
||||
if (button == BUTTON_POSITION_SAVE) {
|
||||
this.saveFile(aCallback);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
||||
/**
|
||||
* Handler for window close event. Prompts to save scratchpad if
|
||||
* there are unsaved changes.
|
||||
*
|
||||
* @param nsIDOMEvent aEvent
|
||||
*/
|
||||
onClose: function SP_onClose(aEvent)
|
||||
{
|
||||
let toClose = this.promptSave();
|
||||
if (!toClose) {
|
||||
aEvent.preventDefault();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
let toClose = this.promptSave(aCallback);
|
||||
if (toClose) {
|
||||
window.close();
|
||||
}
|
||||
},
|
||||
|
||||
_observers: [],
|
||||
|
||||
/**
|
||||
|
@ -951,3 +1035,4 @@ XPCOMUtils.defineLazyGetter(Scratchpad, "strings", function () {
|
|||
|
||||
addEventListener("DOMContentLoaded", Scratchpad.onLoad.bind(Scratchpad), false);
|
||||
addEventListener("unload", Scratchpad.onUnload.bind(Scratchpad), false);
|
||||
addEventListener("close", Scratchpad.onClose.bind(Scratchpad), false);
|
||||
|
|
|
@ -69,7 +69,7 @@
|
|||
<command id="sp-cmd-printFile" oncommand="Scratchpad.printFile();" disabled="true"/>
|
||||
-->
|
||||
|
||||
<command id="sp-cmd-close" oncommand="window.close();"/>
|
||||
<command id="sp-cmd-close" oncommand="Scratchpad.close();"/>
|
||||
<command id="sp-cmd-run" oncommand="Scratchpad.run();"/>
|
||||
<command id="sp-cmd-inspect" oncommand="Scratchpad.inspect();"/>
|
||||
<command id="sp-cmd-display" oncommand="Scratchpad.display();"/>
|
||||
|
|
|
@ -59,6 +59,7 @@ _BROWSER_TEST_FILES = \
|
|||
browser_scratchpad_bug_699130_edit_ui_updates.js \
|
||||
browser_scratchpad_bug_669612_unsaved.js \
|
||||
head.js \
|
||||
browser_scratchpad_bug_653427_confirm_close.js \
|
||||
|
||||
libs:: $(_BROWSER_TEST_FILES)
|
||||
$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/browser/$(relativesrcdir)
|
||||
|
|
|
@ -0,0 +1,188 @@
|
|||
/* vim: set ts=2 et sw=2 tw=80: */
|
||||
/* Any copyright is dedicated to the Public Domain.
|
||||
http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
|
||||
Cu.import("resource://gre/modules/NetUtil.jsm");
|
||||
Cu.import("resource://gre/modules/FileUtils.jsm");
|
||||
|
||||
// only finish() when correct number of tests are done
|
||||
const expected = 5;
|
||||
var count = 0;
|
||||
function done()
|
||||
{
|
||||
if (++count == expected) {
|
||||
cleanup();
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
||||
var ScratchpadManager = Scratchpad.ScratchpadManager;
|
||||
var gFile;
|
||||
|
||||
var oldPrompt = Services.prompt;
|
||||
|
||||
function test()
|
||||
{
|
||||
waitForExplicitFinish();
|
||||
|
||||
gFile = createTempFile("fileForBug653427.tmp");
|
||||
writeFile(gFile, "text", testUnsaved.call(this));
|
||||
|
||||
testNew();
|
||||
testSavedFile();
|
||||
|
||||
content.location = "data:text/html,<p>test scratchpad save file prompt on closing";
|
||||
}
|
||||
|
||||
function testNew()
|
||||
{
|
||||
let win = ScratchpadManager.openScratchpad();
|
||||
|
||||
win.addEventListener("load", function() {
|
||||
win.Scratchpad.close();
|
||||
|
||||
ok(win.closed, "new scratchpad window should close without prompting")
|
||||
done();
|
||||
});
|
||||
}
|
||||
|
||||
function testSavedFile()
|
||||
{
|
||||
let win = ScratchpadManager.openScratchpad();
|
||||
|
||||
win.addEventListener("load", function() {
|
||||
win.Scratchpad.filename = "test.js";
|
||||
win.Scratchpad.saved = true;
|
||||
win.Scratchpad.close();
|
||||
|
||||
ok(win.closed, "scratchpad from file with no changes should close")
|
||||
done();
|
||||
});
|
||||
}
|
||||
|
||||
function testUnsaved()
|
||||
{
|
||||
testUnsavedFileCancel();
|
||||
testUnsavedFileSave();
|
||||
testUnsavedFileDontSave();
|
||||
}
|
||||
|
||||
function testUnsavedFileCancel()
|
||||
{
|
||||
let win = ScratchpadManager.openScratchpad();
|
||||
|
||||
win.addEventListener("load", function() {
|
||||
win.Scratchpad.filename = "test.js";
|
||||
win.Scratchpad.saved = false;
|
||||
|
||||
Services.prompt = {
|
||||
confirmEx: function() {
|
||||
return win.BUTTON_POSITION_CANCEL;
|
||||
}
|
||||
}
|
||||
|
||||
win.Scratchpad.close();
|
||||
|
||||
ok(!win.closed, "cancelling dialog shouldn't close scratchpad");
|
||||
|
||||
win.close();
|
||||
done();
|
||||
});
|
||||
}
|
||||
|
||||
function testUnsavedFileSave()
|
||||
{
|
||||
let win = ScratchpadManager.openScratchpad();
|
||||
|
||||
win.addEventListener("load", function() {
|
||||
win.Scratchpad.importFromFile(gFile, true, function(status, content) {
|
||||
win.Scratchpad.filename = gFile.path;
|
||||
win.Scratchpad.onTextSaved();
|
||||
|
||||
let text = "new text";
|
||||
win.Scratchpad.setText(text);
|
||||
|
||||
Services.prompt = {
|
||||
confirmEx: function() {
|
||||
return win.BUTTON_POSITION_SAVE;
|
||||
}
|
||||
}
|
||||
|
||||
win.Scratchpad.close(function() {
|
||||
readFile(gFile, function(savedContent) {
|
||||
is(savedContent, text, 'prompted "Save" worked when closing scratchpad');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
ok(win.closed, 'pressing "Save" in dialog should close scratchpad');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function testUnsavedFileDontSave()
|
||||
{
|
||||
let win = ScratchpadManager.openScratchpad();
|
||||
|
||||
win.addEventListener("load", function() {
|
||||
win.Scratchpad.filename = gFile.path;
|
||||
win.Scratchpad.saved = false;
|
||||
|
||||
Services.prompt = {
|
||||
confirmEx: function() {
|
||||
return win.BUTTON_POSITION_DONT_SAVE;
|
||||
}
|
||||
}
|
||||
|
||||
win.Scratchpad.close();
|
||||
|
||||
ok(win.closed, 'pressing "Don\'t Save" in dialog should close scratchpad');
|
||||
done();
|
||||
});
|
||||
}
|
||||
|
||||
function cleanup()
|
||||
{
|
||||
Services.prompt = oldPrompt;
|
||||
gFile.remove(false);
|
||||
gFile = null;
|
||||
}
|
||||
|
||||
function createTempFile(name)
|
||||
{
|
||||
let file = FileUtils.getFile("TmpD", [name]);
|
||||
file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
|
||||
file.QueryInterface(Ci.nsILocalFile)
|
||||
return file;
|
||||
}
|
||||
|
||||
function writeFile(file, content, callback)
|
||||
{
|
||||
let fout = Cc["@mozilla.org/network/file-output-stream;1"].
|
||||
createInstance(Ci.nsIFileOutputStream);
|
||||
fout.init(file.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20,
|
||||
0644, fout.DEFER_OPEN);
|
||||
|
||||
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
|
||||
createInstance(Ci.nsIScriptableUnicodeConverter);
|
||||
converter.charset = "UTF-8";
|
||||
let fileContentStream = converter.convertToInputStream(content);
|
||||
|
||||
NetUtil.asyncCopy(fileContentStream, fout, callback);
|
||||
}
|
||||
|
||||
function readFile(file, callback)
|
||||
{
|
||||
let channel = NetUtil.newChannel(file);
|
||||
channel.contentType = "application/javascript";
|
||||
|
||||
NetUtil.asyncFetch(channel, function(inputStream, status) {
|
||||
ok(Components.isSuccessCode(status),
|
||||
"file was read successfully");
|
||||
|
||||
let content = NetUtil.readInputStreamToString(inputStream,
|
||||
inputStream.available());
|
||||
callback(content);
|
||||
});
|
||||
}
|
|
@ -38,6 +38,14 @@ saveFileAs=Save File As
|
|||
# save fails.
|
||||
saveFile.failed=The file save operation failed.
|
||||
|
||||
# LOCALIZATION NOTE (confirmClose): This is message in the prompt dialog when
|
||||
# you try to close a scratchpad with unsaved changes.
|
||||
confirmClose=Do you want to save the changes you made to this scratchpad?
|
||||
|
||||
# LOCALIZATION NOTE (confirmClose.title): This is title of the prompt dialog when
|
||||
# you try to close a scratchpad with unsaved changes.
|
||||
confirmClose.title=Unsaved Changes
|
||||
|
||||
# LOCALIZATION NOTE (scratchpadIntro): This is a multi-line comment explaining
|
||||
# how to use the Scratchpad. Note that this should be a valid JavaScript
|
||||
# comment inside /* and */.
|
||||
|
|
Загрузка…
Ссылка в новой задаче