Now remembers the last used file name for dropbox and local (browser) storage in a cookie and presents again at next load.

Rewrote some of the code to make all dropbox related functions part of the IO class.
This commit is contained in:
Thomas Tempelmann
2015-06-11 17:24:08 +02:00
parent 62152c38a7
commit 8f5da3a871
+47 -41
View File
@@ -1272,7 +1272,8 @@ SQL.IO = OZ.Class();
SQL.IO.prototype.init = function(owner) { SQL.IO.prototype.init = function(owner) {
this.owner = owner; this.owner = owner;
this._name = ""; /* last used keyword */ this._name = ""; /* last used name with server load/save */
this.lastUsedName = ""; /* last used name with local storage or dropbox load/save */
this.dom = { this.dom = {
container:OZ.$("io") container:OZ.$("io")
}; };
@@ -1325,6 +1326,8 @@ SQL.IO.prototype.init = function(owner) {
OZ.Event.add(this.dom.serverimport, "click", this.bind(this.serverimport)); OZ.Event.add(this.dom.serverimport, "click", this.bind(this.serverimport));
OZ.Event.add(document, "keydown", this.bind(this.press)); OZ.Event.add(document, "keydown", this.bind(this.press));
this.build(); this.build();
this.dropBoxInit ();
} }
SQL.IO.prototype.build = function() { SQL.IO.prototype.build = function() {
@@ -1398,6 +1401,19 @@ SQL.IO.prototype.clientload = function() {
this.fromXMLText(xml); this.fromXMLText(xml);
} }
SQL.IO.prototype.promptName = function(title, suffix) {
var lastUsedName = this.owner.getOption("lastUsedName") || this.lastUsedName;
var name = prompt(_(title), lastUsedName);
if (!name) { return null; }
if (suffix && name.endsWith(suffix)) {
// remove suffix from name
name = name.substr(0, name.length-4);
}
this.owner.setOption("lastUsedName", name);
this.lastUsedName = name; // save this also in variable in case cookies are disabled
return name;
}
SQL.IO.prototype.clientlocalsave = function() { SQL.IO.prototype.clientlocalsave = function() {
if (!window.localStorage) { if (!window.localStorage) {
alert("Sorry, your browser does not seem to support localStorage."); alert("Sorry, your browser does not seem to support localStorage.");
@@ -1410,9 +1426,8 @@ SQL.IO.prototype.clientlocalsave = function() {
return; return;
} }
var key = prompt(_("serversaveprompt"), this._name); var key = this.promptName("serversaveprompt");
if (!key) { return; } if (!key) { return; }
this._name = key;
key = "wwwsqldesigner_databases_" + (key || "default"); key = "wwwsqldesigner_databases_" + (key || "default");
try { try {
@@ -1429,9 +1444,7 @@ SQL.IO.prototype.clientlocalload = function() {
return; return;
} }
var key = prompt(_("serverloadprompt"), this._name); var key = this.promptName("serverloadprompt");
if (!key) { return; }
this._name = key;
key = "wwwsqldesigner_databases_" + (key || "default"); key = "wwwsqldesigner_databases_" + (key || "default");
try { try {
@@ -1484,13 +1497,13 @@ SQL.IO.prototype.clientlocallist = function() {
* The following code uses this lib: https://github.com/dropbox/dropbox-js * The following code uses this lib: https://github.com/dropbox/dropbox-js
*/ */
var dropboxClient = null; SQL.IO.prototype.dropBoxInit = function()
var dropBoxInit = function()
{ {
if (dropboxAppKey && Dropbox.isBrowserSupported()) { if (dropboxAppKey) {
dropboxClient = new Dropbox.Client({ key: dropboxAppKey }); this.dropboxClient = new Dropbox.Client({ key: dropboxAppKey });
} else { } else {
this.dropboxClient = null;
// Hide the Dropbox buttons and divider // Hide the Dropbox buttons and divider
var elems = document.querySelectorAll("[id^=dropbox]"); // gets all tags whose id start with "dropbox" var elems = document.querySelectorAll("[id^=dropbox]"); // gets all tags whose id start with "dropbox"
[].slice.call(elems).forEach( [].slice.call(elems).forEach(
@@ -1499,7 +1512,7 @@ var dropBoxInit = function()
} }
} }
var showDropboxError = function(error) { SQL.IO.prototype.showDropboxError = function(error) {
var prefix = _("Dropbox error")+": "; var prefix = _("Dropbox error")+": ";
var msg = error.status; var msg = error.status;
@@ -1546,17 +1559,17 @@ var showDropboxError = function(error) {
alert (prefix+msg); alert (prefix+msg);
}; };
var showDropboxAuthenticate = function() { SQL.IO.prototype.showDropboxAuthenticate = function() {
if (!dropboxClient) return false; if (!this.dropboxClient) return false;
// We want to use a popup window for authentication as the default redirection won't work for us as it'll make us lose our schema data // We want to use a popup window for authentication as the default redirection won't work for us as it'll make us lose our schema data
dropboxClient.authDriver(new Dropbox.AuthDriver.Popup({ receiverUrl: "dropbox-oauth-receiver.html" })); this.dropboxClient.authDriver(new Dropbox.AuthDriver.Popup({ receiverUrl: "dropbox-oauth-receiver.html" }));
// Now let's authenticate us // Now let's authenticate us
var success = false; var success = false;
dropboxClient.authenticate( function(error, client) { this.dropboxClient.authenticate( function(error, client) {
if (error) { if (error) {
showDropboxError(error); this.showDropboxError(error);
return; return;
} }
success = true; success = true;
@@ -1566,56 +1579,51 @@ var showDropboxAuthenticate = function() {
} }
SQL.IO.prototype.dropboxsave = function() { SQL.IO.prototype.dropboxsave = function() {
if (!showDropboxAuthenticate()) return; if (!this.showDropboxAuthenticate()) return;
var key = prompt(_("serversaveprompt"), this._name); var key = this.promptName("serversaveprompt", ".xml");
if (!key) { return; } if (!key) { return; }
if (key.endsWith(".xml")) {
// remove ".xml" from name
key = key.substr(0, key.length-4);
}
this._name = key;
var filename = (key || "default") + ".xml"; var filename = (key || "default") + ".xml";
var sql_io = this;
sql_io.listresponse("Saving...", 200);
var xml = this.owner.toXML(); var xml = this.owner.toXML();
dropboxClient.writeFile(filename, xml, function(error, stat) { this.dropboxClient.writeFile(filename, xml, function(error, stat) {
if (error) { if (error) {
return showDropboxError(error); sql_io.listresponse("", 200);
return this.showDropboxError(error);
} }
alert(filename+" "+_("was saved to Dropbox")); sql_io.listresponse(filename+" "+_("was saved to Dropbox"), 200);
}); });
} }
SQL.IO.prototype.dropboxload = function() { SQL.IO.prototype.dropboxload = function() {
if (!showDropboxAuthenticate()) return; if (!this.showDropboxAuthenticate()) return;
var key = prompt(_("serverloadprompt"), this._name); var key = this.promptName("serverloadprompt", ".xml");
if (!key) { return; } if (!key) { return; }
if (key.endsWith(".xml")) {
// remove ".xml" from name
key = key.substr(0, key.length-4);
}
this._name = key;
var filename = (key || "default") + ".xml"; var filename = (key || "default") + ".xml";
var sql_io = this; var sql_io = this;
dropboxClient.readFile(filename, function(error, data) { sql_io.listresponse("Loading...", 200);
this.dropboxClient.readFile(filename, function(error, data) {
sql_io.listresponse("", 200);
if (error) { if (error) {
return showDropboxError(error); return this.showDropboxError(error);
} }
sql_io.fromXMLText(data); sql_io.fromXMLText(data);
}); });
} }
SQL.IO.prototype.dropboxlist = function() { SQL.IO.prototype.dropboxlist = function() {
if (!showDropboxAuthenticate()) return; if (!this.showDropboxAuthenticate()) return;
var sql_io = this; var sql_io = this;
sql_io.listresponse("Loading...", 200); sql_io.listresponse("Loading...", 200);
dropboxClient.readdir("/", function(error, entries) { this.dropboxClient.readdir("/", function(error, entries) {
if (error) { if (error) {
sql_io.listresponse("", 200); sql_io.listresponse("", 200);
return showDropboxError(error); return this.showDropboxError(error);
} }
var data = entries.join("\n")+"\n"; var data = entries.join("\n")+"\n";
sql_io.listresponse(data, 200); sql_io.listresponse(data, 200);
@@ -2601,8 +2609,6 @@ SQL.Designer.prototype.init = function() {
this.flag = 2; this.flag = 2;
this.requestLanguage(); this.requestLanguage();
this.requestDB(); this.requestDB();
dropBoxInit ();
} }
/* update area size */ /* update area size */