aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBogdan Gheorghe2013-04-24 14:56:58 (EDT)
committerSilenio Quarti2013-04-24 15:01:21 (EDT)
commitceb1f40aea06dd4c97cc34d765cdfefa4e8bb63f (patch)
tree5f4ff629e8955322a84fbe8891092783461c0bef
parent339514144d4015beb40ef1f8962ab1d89c83f9b8 (diff)
downloadorg.eclipse.orion.client-ceb1f40aea06dd4c97cc34d765cdfefa4e8bb63f.zip
org.eclipse.orion.client-ceb1f40aea06dd4c97cc34d765cdfefa4e8bb63f.tar.gz
org.eclipse.orion.client-ceb1f40aea06dd4c97cc34d765cdfefa4e8bb63f.tar.bz2
Bug 369885 - Auto-save feature in editor
-rw-r--r--bundles/org.eclipse.orion.client.editor/web/orion/editor/editor.js62
-rw-r--r--bundles/org.eclipse.orion.client.ui/web/edit/setup.js3
-rw-r--r--bundles/org.eclipse.orion.client.ui/web/orion/edit/nls/root/messages.js5
-rw-r--r--bundles/org.eclipse.orion.client.ui/web/orion/editorCommands.js7
4 files changed, 72 insertions, 5 deletions
diff --git a/bundles/org.eclipse.orion.client.editor/web/orion/editor/editor.js b/bundles/org.eclipse.orion.client.editor/web/orion/editor/editor.js
index 7c888bb..36cbe1a 100644
--- a/bundles/org.eclipse.orion.client.editor/web/orion/editor/editor.js
+++ b/bundles/org.eclipse.orion.client.editor/web/orion/editor/editor.js
@@ -13,9 +13,47 @@
/*jslint maxerr:150 browser:true devel:true laxbreak:true regexp:false*/
define("orion/editor/editor", ['i18n!orion/editor/nls/messages', 'orion/keyBinding', 'orion/editor/eventTarget', 'orion/editor/tooltip', 'orion/editor/annotations', 'orion/util'], function(messages, mKeyBinding, mEventTarget, mTooltip, mAnnotations, util) { //$NON-NLS-6$ //$NON-NLS-5$ //$NON-NLS-4$ //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$ //$NON-NLS-0$
-
+
var HIGHLIGHT_ERROR_ANNOTATION = "orion.annotation.highlightError"; //$NON-NLS-0$
+ function Idle(options){
+ this._document = options.document || document;
+ this._timeout = options.timeout;
+ //TODO: remove listeners if there are no clients
+ //TODO: add support for multiple clients with different timeouts
+ var events = ["keypress","keydown","keyup","mousemove","mousedown","mousemove"]; //$NON-NLS-0$ //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
+ var reset = function (e) { this._resetTimer(); }.bind(this);
+ for (var i=0; i<events.length; i++) {
+ var event = events[i];
+ this._document.addEventListener(event, reset, true);
+ }
+ }
+
+ Idle.prototype = {
+ _resetTimer: function() {
+ var window = document.defaultView || document.parentWindow;
+ if (this._timer) {
+ window.clearTimeout(this._timer);
+ this._timer = null;
+ }
+ if (this._timeout !== -1) {
+ this._timer = window.setTimeout(function() {
+ this.onIdle({type:"Idle"}); //$NON-NLS-0$
+ this._timer = null;
+ this._resetTimer();
+ }.bind(this), this._timeout);
+ }
+ },
+ onIdle: function (idleEvent) {
+ return this.dispatchEvent(idleEvent);
+ },
+ setTimeout: function(timeout) {
+ this._timeout = timeout;
+ this._resetTimer();
+ }
+ };
+ mEventTarget.EventTarget.addMixin(Idle.prototype);
+
/**
* @name orion.editor.Editor
* @class An <code>Editor</code> is a user interface for editing text that provides additional features over the basic {@link orion.editor.TextView}.
@@ -826,7 +864,27 @@ define("orion/editor/editor", ['i18n!orion/editor/nls/messages', 'orion/keyBindi
contentsSaved: contentsSaved
});
},
-
+ /**
+ * Set the autosave timeout. If the timeout is <code>-1</code>, autosave is
+ * disabled.
+ * @param {Number} timeout - the autosave timeout in milliseconds
+ */
+ setAutoSaveTimeout: function(timeout){
+ if (!this._idle) {
+ var document = this._textView.getOptions("parent").ownerDocument; //$NON-NLS-0$
+ var options = {
+ document: document,
+ timeout: timeout
+ };
+ this._idle = new Idle(options);
+ this._idle.addEventListener("Idle", function () { //$NON-NLS-0$
+ if (this.isDirty()) {
+ this._textView.invokeAction("save"); //$NON-NLS-0$
+ }
+ }.bind(this));
+ }
+ this._idle.setTimeout(timeout);
+ },
/**
* Called when the editor's contents have changed.
* @param {Event} inputChangedEvent
diff --git a/bundles/org.eclipse.orion.client.ui/web/edit/setup.js b/bundles/org.eclipse.orion.client.ui/web/edit/setup.js
index ebc841f..32518ff 100644
--- a/bundles/org.eclipse.orion.client.ui/web/edit/setup.js
+++ b/bundles/org.eclipse.orion.client.ui/web/edit/setup.js
@@ -170,6 +170,7 @@ exports.setUpEditor = function(serviceRegistry, preferences, isReadOnly){
}
// Contents
editor.setInput(fileURI, null, contents);
+ editor.setAutoSaveTimeout(1000);
editor.showSelection(input.start, input.end, input.line, input.offset, input.length);
commandRegistry.processURL(window.location.href);
});
@@ -590,4 +591,4 @@ exports.setUpEditor = function(serviceRegistry, preferences, isReadOnly){
};
};
return exports;
-}); \ No newline at end of file
+});
diff --git a/bundles/org.eclipse.orion.client.ui/web/orion/edit/nls/root/messages.js b/bundles/org.eclipse.orion.client.ui/web/orion/edit/nls/root/messages.js
index 334c128..bfcae50 100644
--- a/bundles/org.eclipse.orion.client.ui/web/orion/edit/nls/root/messages.js
+++ b/bundles/org.eclipse.orion.client.ui/web/orion/edit/nls/root/messages.js
@@ -31,5 +31,6 @@ define({
"Find": "Find",
"No response from server. Check your internet connection and try again.": "No response from server. Check your internet connection and try again.",
"Saving file {0}": "Saving file ${0}",
- "Running {0}": "Running ${0}"
-}); \ No newline at end of file
+ "Running {0}": "Running ${0}",
+ "Saving..." : "Saving..."
+});
diff --git a/bundles/org.eclipse.orion.client.ui/web/orion/editorCommands.js b/bundles/org.eclipse.orion.client.ui/web/orion/editorCommands.js
index fa02bcd..33f5896 100644
--- a/bundles/org.eclipse.orion.client.ui/web/orion/editorCommands.js
+++ b/bundles/org.eclipse.orion.client.ui/web/orion/editorCommands.js
@@ -94,15 +94,18 @@ exports.EditorCommandFactory = (function() {
var self = this;
if (this.inputManager.onSave) {
editor.getTextView().setAction("save", function () { //$NON-NLS-0$
+ editor.reportStatus(messages['Saving...']);
var contents = editor.getText();
self.inputManager.onSave(self.inputManager.getInput(), contents,
function(result) {
editor.setInput(self.inputManager.getInput(), null, contents, true);
+ editor.reportStatus("");
if(self.inputManager.afterSave){
self.inputManager.afterSave();
}
},
function(error) {
+ editor.reportStatus("");
error.log = true;
}
);
@@ -110,6 +113,7 @@ exports.EditorCommandFactory = (function() {
}, {name: messages['Save']});
} else {
editor.getTextView().setAction("save", function () { //$NON-NLS-0$
+ editor.reportStatus(messages['Saving...']);
var contents = editor.getText();
var etag = self.inputManager.getFileMetadata().ETag;
var args = { "ETag" : etag }; //$NON-NLS-0$
@@ -122,6 +126,7 @@ exports.EditorCommandFactory = (function() {
function(result) {
self.inputManager.getFileMetadata().ETag = result.ETag;
editor.setInput(self.inputManager.getInput(), null, contents, true);
+ editor.reportStatus("");
if(self.inputManager.afterSave){
self.inputManager.afterSave();
}
@@ -141,6 +146,7 @@ exports.EditorCommandFactory = (function() {
function(result) {
self.inputManager.getFileMetadata().ETag = result.ETag;
editor.setInput(self.inputManager.getInput(), null, contents, true);
+ editor.reportStatus("");
if(self.inputManager.afterSave){
self.inputManager.afterSave();
}
@@ -148,6 +154,7 @@ exports.EditorCommandFactory = (function() {
}
} else {
// unknown error
+ editor.reportStatus("");
handleError(error);
}
}