storage system refactored
This commit is contained in:
@@ -26,5 +26,4 @@ var CONFIG = {
|
||||
* Copy the shown "App key" and paste it here below:
|
||||
*/
|
||||
DROPBOX_KEY: null // "your app key";
|
||||
|
||||
}
|
||||
|
||||
@@ -1,90 +1,64 @@
|
||||
SQL.IO = function(owner) {
|
||||
this.owner = owner;
|
||||
this._name = ""; /* last used name with server load/save */
|
||||
this.lastUsedName = ""; /* last used name with local storage or dropbox load/save */
|
||||
this._storages = [];
|
||||
this._currentStorage = null;
|
||||
|
||||
this.dom = {
|
||||
container:OZ.$("io")
|
||||
container: OZ.$("io"),
|
||||
content: OZ.$("io-content"),
|
||||
seelct: null
|
||||
};
|
||||
|
||||
var ids = ["saveload","clientlocalsave", "clientsave", "clientlocalload", "clientlocallist","clientload", "clientsql",
|
||||
"dropboxsave", "dropboxload", "dropboxlist",
|
||||
"quicksave", "serversave", "serverload",
|
||||
"serverlist", "serverimport"];
|
||||
this.dom.select = this.dom.container.querySelector("select");
|
||||
this.dom.ta = OZ.$("textarea");
|
||||
|
||||
var ids = ["saveload", "storage", "quicksave", "clientsql"];
|
||||
for (var i=0;i<ids.length;i++) {
|
||||
var id = ids[i];
|
||||
var elm = OZ.$(id);
|
||||
this.dom[id] = elm;
|
||||
elm.value = _(id);
|
||||
}
|
||||
|
||||
this.dom.quicksave.value += " (F2)";
|
||||
|
||||
var ids = ["client","server","output","backendlabel"];
|
||||
for (var i=0;i<ids.length;i++) {
|
||||
var id = ids[i];
|
||||
var elm = OZ.$(id);
|
||||
elm.innerHTML = _(id);
|
||||
}
|
||||
this.dom.quicksave.innerHTML += " (F2)";
|
||||
|
||||
this.dom.ta = OZ.$("textarea");
|
||||
this.dom.backend = OZ.$("backend");
|
||||
|
||||
/* init dropbox before hiding the container so it can adjust its buttons */
|
||||
this.dropBoxInit();
|
||||
|
||||
this.dom.container.parentNode.removeChild(this.dom.container);
|
||||
this.dom.container.style.visibility = "";
|
||||
|
||||
this.saveresponse = this.saveresponse.bind(this);
|
||||
this.loadresponse = this.loadresponse.bind(this);
|
||||
this.listresponse = this.listresponse.bind(this);
|
||||
this.importresponse = this.importresponse.bind(this);
|
||||
|
||||
|
||||
OZ.Event.add(this.dom.saveload, "click", this.click.bind(this));
|
||||
OZ.Event.add(this.dom.clientlocalsave, "click", this.clientlocalsave.bind(this));
|
||||
OZ.Event.add(this.dom.clientsave, "click", this.clientsave.bind(this));
|
||||
OZ.Event.add(this.dom.clientlocalload, "click", this.clientlocalload.bind(this));
|
||||
OZ.Event.add(this.dom.clientlocallist, "click", this.clientlocallist.bind(this));
|
||||
OZ.Event.add(this.dom.clientload, "click", this.clientload.bind(this));
|
||||
OZ.Event.add(this.dom.dropboxload, "click", this.dropboxload.bind(this));
|
||||
OZ.Event.add(this.dom.dropboxsave, "click", this.dropboxsave.bind(this));
|
||||
OZ.Event.add(this.dom.dropboxlist, "click", this.dropboxlist.bind(this));
|
||||
OZ.Event.add(this.dom.clientsql, "click", this.clientsql.bind(this));
|
||||
OZ.Event.add(this.dom.quicksave, "click", this.quicksave.bind(this));
|
||||
OZ.Event.add(this.dom.serversave, "click", this.serversave.bind(this));
|
||||
OZ.Event.add(this.dom.serverload, "click", this.serverload.bind(this));
|
||||
OZ.Event.add(this.dom.serverlist, "click", this.serverlist.bind(this));
|
||||
OZ.Event.add(this.dom.serverimport, "click", this.serverimport.bind(this));
|
||||
OZ.Event.add(document, "keydown", this.press.bind(this));
|
||||
this.build();
|
||||
OZ.Event.add(this.dom.clientsql, "click", this._clientsql.bind(this));
|
||||
OZ.Event.add(this.dom.quicksave, "click", this._quicksave.bind(this));
|
||||
OZ.Event.add(this.dom.select, "change", this._storageChange.bind(this));
|
||||
OZ.Event.add(window, "keydown", this._keydown.bind(this));
|
||||
this._build();
|
||||
}
|
||||
|
||||
SQL.IO.prototype.build = function() {
|
||||
OZ.DOM.clear(this.dom.backend);
|
||||
SQL.IO.prototype._build = function() {
|
||||
var local = document.createElement("optgroup");
|
||||
local.label = "Local";
|
||||
this.dom.select.appendChild(local);
|
||||
|
||||
var bs = CONFIG.AVAILABLE_BACKENDS;
|
||||
var be = CONFIG.DEFAULT_BACKEND;
|
||||
var r = window.location.search.substring(1).match(/backend=([^&]*)/);
|
||||
if (r) {
|
||||
req = r[1];
|
||||
if (bs.indexOf(req) != -1) {
|
||||
be = req;
|
||||
}
|
||||
}
|
||||
for (var i=0;i<bs.length;i++) {
|
||||
var o = OZ.DOM.elm("option");
|
||||
o.value = bs[i];
|
||||
o.innerHTML = bs[i];
|
||||
this.dom.backend.appendChild(o);
|
||||
if (bs[i] == be) { this.dom.backend.selectedIndex = i; }
|
||||
}
|
||||
var remote = document.createElement("optgroup");
|
||||
remote.label = "Remote";
|
||||
this.dom.select.appendChild(remote);
|
||||
|
||||
this._addStorage(SQL.Storage.Browser, local);
|
||||
this._addStorage(SQL.Storage.Clipboard, local);
|
||||
this._addStorage(SQL.Storage.REST, remote);
|
||||
this._addStorage(SQL.Storage.Dropbox, remote);
|
||||
|
||||
this._setStorage(2);
|
||||
}
|
||||
|
||||
SQL.IO.prototype.getArea = function() {
|
||||
return this.dom.ta;
|
||||
}
|
||||
|
||||
SQL.IO.prototype.click = function() { /* open io dialog */
|
||||
this.build();
|
||||
this.dom.ta.value = "";
|
||||
this.dom.clientsql.value = _("clientsql") + " (" + window.DATATYPES.getAttribute("db") + ")";
|
||||
this.owner.window.open(_("saveload"),this.dom.container);
|
||||
this.owner.window.open(_("saveload"), this.dom.container);
|
||||
|
||||
var index = this._storages.indexOf(this._currentStorage);
|
||||
this._setStorage(index);
|
||||
}
|
||||
|
||||
SQL.IO.prototype.fromXMLText = function(xml) {
|
||||
@@ -115,258 +89,14 @@ SQL.IO.prototype.fromXML = function(xmlDoc) {
|
||||
return true;
|
||||
}
|
||||
|
||||
SQL.IO.prototype.clientsave = function() {
|
||||
var xml = this.owner.toXML();
|
||||
this.dom.ta.value = xml;
|
||||
}
|
||||
|
||||
SQL.IO.prototype.clientload = function() {
|
||||
var xml = this.dom.ta.value;
|
||||
if (!xml) {
|
||||
alert(_("empty"));
|
||||
return;
|
||||
}
|
||||
|
||||
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() {
|
||||
if (!window.localStorage) {
|
||||
alert("Sorry, your browser does not seem to support localStorage.");
|
||||
return;
|
||||
}
|
||||
|
||||
var xml = this.owner.toXML();
|
||||
if (xml.length >= (5*1024*1024)/2) { /* this is a very big db structure... */
|
||||
alert("Warning: your database structure is above 5 megabytes in size, this is above the localStorage single key limit allowed by some browsers, example Mozilla Firefox 10");
|
||||
return;
|
||||
}
|
||||
|
||||
var key = this.promptName("serversaveprompt");
|
||||
if (!key) { return; }
|
||||
key = "wwwsqldesigner_databases_" + (key || "default");
|
||||
|
||||
try {
|
||||
localStorage.setItem(key, xml);
|
||||
if (localStorage.getItem(key) != xml) { throw new Error("Content verification failed"); }
|
||||
} catch (e) {
|
||||
alert("Error saving database structure to localStorage! ("+e.message+")");
|
||||
}
|
||||
}
|
||||
|
||||
SQL.IO.prototype.clientlocalload = function() {
|
||||
if (!window.localStorage) {
|
||||
alert("Sorry, your browser does not seem to support localStorage.");
|
||||
return;
|
||||
}
|
||||
|
||||
var key = this.promptName("serverloadprompt");
|
||||
key = "wwwsqldesigner_databases_" + (key || "default");
|
||||
|
||||
try {
|
||||
var xml = localStorage.getItem(key);
|
||||
if (!xml) { throw new Error("No data available"); }
|
||||
} catch (e) {
|
||||
alert("Error loading database structure from localStorage! ("+e.message+")");
|
||||
return;
|
||||
}
|
||||
|
||||
this.fromXMLText(xml);
|
||||
}
|
||||
|
||||
SQL.IO.prototype.clientlocallist = function() {
|
||||
if (!window.localStorage) {
|
||||
alert("Sorry, your browser does not seem to support localStorage.");
|
||||
return;
|
||||
}
|
||||
|
||||
/* --- Define some useful vars --- */
|
||||
var baseKeysName = "wwwsqldesigner_databases_";
|
||||
var localLen = localStorage.length;
|
||||
var data = "";
|
||||
var schemasFound = false;
|
||||
var code = 200;
|
||||
|
||||
/* --- work --- */
|
||||
try {
|
||||
for (var i = 0; i< localLen; ++i) {
|
||||
var key = localStorage.key(i);
|
||||
if((new RegExp(baseKeysName)).test(key)) {
|
||||
var result = key.substring(baseKeysName.length);
|
||||
schemasFound = true;
|
||||
data += result + "\n";
|
||||
}
|
||||
}
|
||||
if (!schemasFound) {
|
||||
throw new Error("No data available");
|
||||
}
|
||||
} catch (e) {
|
||||
alert("Error loading database names from localStorage! ("+e.message+")");
|
||||
return;
|
||||
}
|
||||
this.listresponse(data, code);
|
||||
}
|
||||
|
||||
/* ------------------------- Dropbox start ------------------------ */
|
||||
|
||||
/**
|
||||
* The following code uses this lib: https://github.com/dropbox/dropbox-js
|
||||
*/
|
||||
SQL.IO.prototype.dropBoxInit = function() {
|
||||
if (CONFIG.DROPBOX_KEY) {
|
||||
this.dropboxClient = new Dropbox.Client({ key: CONFIG.DROPBOX_KEY });
|
||||
} else {
|
||||
this.dropboxClient = null;
|
||||
// Hide the Dropbox buttons
|
||||
var elems = document.querySelectorAll("[id^=dropbox]"); // gets all tags whose id start with "dropbox"
|
||||
[].slice.call(elems).forEach(
|
||||
function(elem) { elem.style.display = "none"; }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
SQL.IO.prototype.showDropboxError = function(error) {
|
||||
var prefix = _("Dropbox error")+": ";
|
||||
var msg = error.status;
|
||||
|
||||
switch (error.status) {
|
||||
case Dropbox.ApiError.INVALID_TOKEN:
|
||||
// If you're using dropbox.js, the only cause behind this error is that
|
||||
// the user token expired.
|
||||
// Get the user through the authentication flow again.
|
||||
msg = _("Invalid Token (expired)");
|
||||
break;
|
||||
|
||||
case Dropbox.ApiError.NOT_FOUND:
|
||||
// The file or folder you tried to access is not in the user's Dropbox.
|
||||
// Handling this error is specific to your application.
|
||||
msg = _("File not found");
|
||||
break;
|
||||
|
||||
case Dropbox.ApiError.OVER_QUOTA:
|
||||
// The user is over their Dropbox quota.
|
||||
// Tell them their Dropbox is full. Refreshing the page won't help.
|
||||
msg = _("Dropbox is full");
|
||||
break;
|
||||
|
||||
case Dropbox.ApiError.RATE_LIMITED:
|
||||
// Too many API requests. Tell the user to try again later.
|
||||
// Long-term, optimize your code to use fewer API calls.
|
||||
break;
|
||||
|
||||
case Dropbox.ApiError.NETWORK_ERROR:
|
||||
// An error occurred at the XMLHttpRequest layer.
|
||||
// Most likely, the user's network connection is down.
|
||||
// API calls will not succeed until the user gets back online.
|
||||
msg = _("Network error");
|
||||
break;
|
||||
|
||||
case Dropbox.ApiError.INVALID_PARAM:
|
||||
case Dropbox.ApiError.OAUTH_ERROR:
|
||||
case Dropbox.ApiError.INVALID_METHOD:
|
||||
default:
|
||||
// Caused by a bug in dropbox.js, in your application, or in Dropbox.
|
||||
// Tell the user an error occurred, ask them to refresh the page.
|
||||
}
|
||||
|
||||
alert (prefix+msg);
|
||||
};
|
||||
|
||||
SQL.IO.prototype.showDropboxAuthenticate = function() {
|
||||
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
|
||||
this.dropboxClient.authDriver(new Dropbox.AuthDriver.Popup({ receiverUrl: "dropbox-oauth-receiver.html" }));
|
||||
|
||||
// Now let's authenticate us
|
||||
var success = false;
|
||||
this.dropboxClient.authenticate( function(error, client) {
|
||||
if (error) {
|
||||
this.showDropboxError(error);
|
||||
return;
|
||||
}
|
||||
success = true;
|
||||
return;
|
||||
});
|
||||
return success;
|
||||
}
|
||||
|
||||
SQL.IO.prototype.dropboxsave = function() {
|
||||
if (!this.showDropboxAuthenticate()) return;
|
||||
|
||||
var key = this.promptName("serversaveprompt", ".xml");
|
||||
if (!key) { return; }
|
||||
var filename = (key || "default") + ".xml";
|
||||
|
||||
var sql_io = this;
|
||||
sql_io.listresponse("Saving...", 200);
|
||||
var xml = this.owner.toXML();
|
||||
this.dropboxClient.writeFile(filename, xml, function(error, stat) {
|
||||
if (error) {
|
||||
sql_io.listresponse("", 200);
|
||||
return this.showDropboxError(error);
|
||||
}
|
||||
sql_io.listresponse(filename+" "+_("was saved to Dropbox"), 200);
|
||||
});
|
||||
}
|
||||
|
||||
SQL.IO.prototype.dropboxload = function() {
|
||||
if (!this.showDropboxAuthenticate()) return;
|
||||
|
||||
var key = this.promptName("serverloadprompt", ".xml");
|
||||
if (!key) { return; }
|
||||
var filename = (key || "default") + ".xml";
|
||||
|
||||
var sql_io = this;
|
||||
sql_io.listresponse("Loading...", 200);
|
||||
this.dropboxClient.readFile(filename, function(error, data) {
|
||||
sql_io.listresponse("", 200);
|
||||
if (error) {
|
||||
return this.showDropboxError(error);
|
||||
}
|
||||
sql_io.fromXMLText(data);
|
||||
});
|
||||
}
|
||||
|
||||
SQL.IO.prototype.dropboxlist = function() {
|
||||
if (!this.showDropboxAuthenticate()) return;
|
||||
|
||||
var sql_io = this;
|
||||
sql_io.listresponse("Loading...", 200);
|
||||
this.dropboxClient.readdir("/", function(error, entries) {
|
||||
if (error) {
|
||||
sql_io.listresponse("", 200);
|
||||
return this.showDropboxError(error);
|
||||
}
|
||||
var data = entries.join("\n")+"\n";
|
||||
sql_io.listresponse(data, 200);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------- Dropbox end ------------------------ */
|
||||
|
||||
SQL.IO.prototype.clientsql = function() {
|
||||
SQL.IO.prototype._clientsql = function() {
|
||||
var bp = this.owner.getOption("staticpath");
|
||||
var path = bp + "db/"+window.DATATYPES.getAttribute("db")+"/output.xsl";
|
||||
this.owner.window.showThrobber();
|
||||
OZ.Request(path, this.finish.bind(this), {xml:true});
|
||||
OZ.Request(path, this._transform.bind(this), {xml:true});
|
||||
}
|
||||
|
||||
SQL.IO.prototype.finish = function(xslDoc) {
|
||||
SQL.IO.prototype._transform = function(xslDoc) {
|
||||
this.owner.window.hideThrobber();
|
||||
var xml = this.owner.toXML();
|
||||
var sql = "";
|
||||
@@ -392,98 +122,40 @@ SQL.IO.prototype.finish = function(xslDoc) {
|
||||
this.dom.ta.value = sql.trim();
|
||||
}
|
||||
|
||||
SQL.IO.prototype.serversave = function(e, keyword) {
|
||||
var name = keyword || prompt(_("serversaveprompt"), this._name);
|
||||
if (!name) { return; }
|
||||
this._name = name;
|
||||
var xml = this.owner.toXML();
|
||||
var bp = this.owner.getOption("xhrpath");
|
||||
var url = bp + "backend/"+this.dom.backend.value+"/?action=save&keyword="+encodeURIComponent(name);
|
||||
var h = {"Content-type":"application/xml"};
|
||||
this.owner.window.showThrobber();
|
||||
this.owner.setTitle(name);
|
||||
OZ.Request(url, this.saveresponse, {xml:true, method:"post", data:xml, headers:h});
|
||||
SQL.IO.prototype._quicksave = function(e) {
|
||||
this._currentStorage.quicksave(e);
|
||||
}
|
||||
|
||||
SQL.IO.prototype.quicksave = function(e) {
|
||||
this.serversave(e, this._name);
|
||||
}
|
||||
|
||||
SQL.IO.prototype.serverload = function(e, keyword) {
|
||||
var name = keyword || prompt(_("serverloadprompt"), this._name);
|
||||
if (!name) { return; }
|
||||
this._name = name;
|
||||
var bp = this.owner.getOption("xhrpath");
|
||||
var url = bp + "backend/"+this.dom.backend.value+"/?action=load&keyword="+encodeURIComponent(name);
|
||||
this.owner.window.showThrobber();
|
||||
this.name = name;
|
||||
OZ.Request(url, this.loadresponse, {xml:true});
|
||||
}
|
||||
|
||||
SQL.IO.prototype.serverlist = function(e) {
|
||||
var bp = this.owner.getOption("xhrpath");
|
||||
var url = bp + "backend/"+this.dom.backend.value+"/?action=list";
|
||||
this.owner.window.showThrobber();
|
||||
OZ.Request(url, this.listresponse);
|
||||
}
|
||||
|
||||
SQL.IO.prototype.serverimport = function(e) {
|
||||
var name = prompt(_("serverimportprompt"), "");
|
||||
if (!name) { return; }
|
||||
var bp = this.owner.getOption("xhrpath");
|
||||
var url = bp + "backend/"+this.dom.backend.value+"/?action=import&database="+name;
|
||||
this.owner.window.showThrobber();
|
||||
OZ.Request(url, this.importresponse, {xml:true});
|
||||
}
|
||||
|
||||
SQL.IO.prototype.check = function(code) {
|
||||
switch (code) {
|
||||
case 201:
|
||||
case 404:
|
||||
case 500:
|
||||
case 501:
|
||||
case 503:
|
||||
var lang = "http"+code;
|
||||
this.dom.ta.value = _("httpresponse")+": "+_(lang);
|
||||
return false;
|
||||
break;
|
||||
default: return true;
|
||||
}
|
||||
}
|
||||
|
||||
SQL.IO.prototype.saveresponse = function(data, code) {
|
||||
this.owner.window.hideThrobber();
|
||||
this.check(code);
|
||||
}
|
||||
|
||||
SQL.IO.prototype.loadresponse = function(data, code) {
|
||||
this.owner.window.hideThrobber();
|
||||
if (!this.check(code)) { return; }
|
||||
this.fromXML(data);
|
||||
this.owner.setTitle(this.name);
|
||||
}
|
||||
|
||||
SQL.IO.prototype.listresponse = function(data, code) {
|
||||
this.owner.window.hideThrobber();
|
||||
if (!this.check(code)) { return; }
|
||||
this.dom.ta.value = data;
|
||||
}
|
||||
|
||||
SQL.IO.prototype.importresponse = function(data, code) {
|
||||
this.owner.window.hideThrobber();
|
||||
if (!this.check(code)) { return; }
|
||||
if (this.fromXML(data)) {
|
||||
this.owner.alignTables();
|
||||
}
|
||||
}
|
||||
|
||||
SQL.IO.prototype.press = function(e) {
|
||||
SQL.IO.prototype._keydown = function(e) {
|
||||
switch (e.keyCode) {
|
||||
case 113:
|
||||
if (OZ.opera) {
|
||||
e.preventDefault();
|
||||
}
|
||||
this.quicksave(e);
|
||||
this._currentStorage.quicksave(e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
SQL.IO.prototype._addStorage = function(ctor, parent) {
|
||||
var storage = new ctor(this);
|
||||
if (!storage.isSupported()) { return; }
|
||||
|
||||
var option = storage.getOption();
|
||||
option.value = this._storages.length;
|
||||
parent.appendChild(option);
|
||||
this._storages.push(storage);
|
||||
}
|
||||
|
||||
SQL.IO.prototype._storageChange = function(e) {
|
||||
var index = Number(e.target.selectedIndex);
|
||||
this._setStorage(index);
|
||||
}
|
||||
|
||||
SQL.IO.prototype._setStorage = function(index) {
|
||||
this._currentStorage && this._currentStorage.deactivate();
|
||||
this.dom.content.innerHTML = "";
|
||||
|
||||
this._currentStorage = this._storages[index];
|
||||
this._currentStorage.activate(this.dom.content);
|
||||
}
|
||||
|
||||
+2
-2
@@ -13,8 +13,8 @@ SQL.Relation = function(owner, row1, row2) {
|
||||
if (all.length) { /* inherit */
|
||||
this.color = all[0].getColor();
|
||||
} else if (CONFIG.RELATION_COLORS) { /* pick next */
|
||||
this.constructor._counter++;
|
||||
var colorIndex = this.constructor._counter - 1;
|
||||
SQL.Relation._counter++;
|
||||
var colorIndex = SQL.Relation._counter - 1;
|
||||
this.color = CONFIG.RELATION_COLORS[colorIndex % CONFIG.RELATION_COLORS.length];
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@ SQL.RowManager = function(owner) {
|
||||
OZ.Event.add(this.dom.foreigncreate, "click", this.foreigncreate.bind(this));
|
||||
OZ.Event.add(this.dom.foreignconnect, "click", this.foreignconnect.bind(this));
|
||||
OZ.Event.add(this.dom.foreigndisconnect, "click", this.foreigndisconnect.bind(this));
|
||||
OZ.Event.add(document, "keydown", this.press.bind(this));
|
||||
OZ.Event.add(window, "keydown", this.press.bind(this));
|
||||
|
||||
SQL.subscribe("tableclick", this.tableClick.bind(this));
|
||||
SQL.subscribe("rowclick", this.rowClick.bind(this));
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
SQL.Storage.Browser = function(owner) {
|
||||
SQL.Storage.call(this, owner);
|
||||
this._dom.option.innerHTML = _("storage.browser");
|
||||
this._prefix = "wwwsqldesigner_databases_";
|
||||
|
||||
this._buildButton("clientlocalsave", "_clickSave");
|
||||
this._buildButton("clientlocalload", "_clickLoad");
|
||||
this._dom.list = document.createElement("select");
|
||||
}
|
||||
SQL.Storage.Browser.prototype = Object.create(SQL.Storage.prototype);
|
||||
|
||||
SQL.Storage.Browser.prototype.activate = function(parent) {
|
||||
parent.appendChild(this._dom.list);
|
||||
|
||||
SQL.Storage.prototype.activate.call(this, parent);
|
||||
|
||||
this._list();
|
||||
}
|
||||
|
||||
SQL.Storage.Browser.prototype._save = function(keyword) {
|
||||
var xml = this.owner.owner.toXML();
|
||||
if (xml.length >= (5*1024*1024)/2) { /* this is a very big db structure... */
|
||||
alert("Warning: your database structure is above 5 megabytes in size, this is above the localStorage single key limit allowed by some browsers, example Mozilla Firefox 10");
|
||||
return;
|
||||
}
|
||||
|
||||
var key = this._prefix + keyword;
|
||||
|
||||
try {
|
||||
localStorage.setItem(key, xml);
|
||||
if (localStorage.getItem(key) != xml) { throw new Error("Content verification failed"); }
|
||||
this._keyword = keyword;
|
||||
this._list();
|
||||
} catch (e) {
|
||||
alert("Error saving database structure to localStorage! ("+e.message+")");
|
||||
}
|
||||
}
|
||||
|
||||
SQL.Storage.Browser.prototype._load = function(keyword) {
|
||||
try {
|
||||
var xml = localStorage.getItem(this._prefix + keyword);
|
||||
if (!xml) { throw new Error("No data available"); }
|
||||
} catch (e) {
|
||||
alert("Error loading database structure from localStorage! ("+e.message+")");
|
||||
return;
|
||||
}
|
||||
|
||||
this.owner.fromXMLText(xml);
|
||||
this._keyword = keyword;
|
||||
}
|
||||
|
||||
SQL.Storage.Browser.prototype._clickSave = function(e) {
|
||||
var keyword = this._dom.list.value;
|
||||
if (!keyword) {
|
||||
keyword = this.owner.promptName("serversaveprompt");
|
||||
if (!keyword) { return; }
|
||||
}
|
||||
|
||||
this._save(keyword);
|
||||
}
|
||||
|
||||
SQL.Storage.Browser.prototype._clickLoad = function(e) {
|
||||
var keyword = this._dom.list.value;
|
||||
if (!keyword) {
|
||||
alert("Error loading saved data");
|
||||
return;
|
||||
}
|
||||
|
||||
this._load(keyword);
|
||||
}
|
||||
|
||||
SQL.Storage.Browser.prototype._list = function() {
|
||||
var re = new RegExp("^" + this._prefix);
|
||||
var localLen = localStorage.length;
|
||||
var results = [];
|
||||
|
||||
for (var i = 0; i<localStorage.length; i++) {
|
||||
var key = localStorage.key(i);
|
||||
if (!key.match(re)) { continue; }
|
||||
results.push(key.substring(this._prefix.length));
|
||||
}
|
||||
|
||||
this._dom.list.innerHTML = "";
|
||||
|
||||
if (results.length) {
|
||||
results.forEach(function(result) {
|
||||
var option = document.createElement("option");
|
||||
option.innerHTML = result;
|
||||
this._dom.list.appendChild(option);
|
||||
}.bind(this));
|
||||
} else {
|
||||
var empty = document.createElement("option");
|
||||
empty.innerHTML = "[no saved designs]";
|
||||
empty.value = "";
|
||||
this._dom.list.appendChild(empty);
|
||||
}
|
||||
this._dom.list.value = this._keyword;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
SQL.Storage.Clipboard = function(owner) {
|
||||
SQL.Storage.call(this, owner);
|
||||
this._dom.option.innerHTML = _("storage.clipboard");
|
||||
|
||||
this._buildButton("clientsave", "_save");
|
||||
this._buildButton("clientload", "_load");
|
||||
}
|
||||
SQL.Storage.Clipboard.prototype = Object.create(SQL.Storage.prototype);
|
||||
|
||||
SQL.Storage.Clipboard.prototype._save = function() {
|
||||
this.owner.getArea().value = this.owner.owner.toXML();
|
||||
}
|
||||
|
||||
SQL.Storage.Clipboard.prototype._load = function() {
|
||||
var xml = this.owner.getArea().value;
|
||||
if (!xml) {
|
||||
alert(_("empty"));
|
||||
return;
|
||||
}
|
||||
|
||||
this.owner.fromXMLText(xml);
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
/**
|
||||
* The following code uses this lib: https://github.com/dropbox/dropbox-js
|
||||
*/
|
||||
SQL.Storage.Dropbox = function(owner) {
|
||||
SQL.Storage.call(this, owner);
|
||||
this._dom.option.innerHTML = _("storage.dropbox");
|
||||
|
||||
if (CONFIG.DROPBOX_KEY) { this.dropboxClient = new Dropbox.Client({ key: CONFIG.DROPBOX_KEY }); }
|
||||
|
||||
this._buildButton("dropboxsave", "_clickSave");
|
||||
this._buildButton("dropboxload", "_clickLoad");
|
||||
this._buildButton("dropboxlist", "_clickList");
|
||||
|
||||
}
|
||||
SQL.Storage.Dropbox.prototype = Object.create(SQL.Storage.prototype);
|
||||
|
||||
SQL.Storage.Dropbox.prototype.isSupported = function() {
|
||||
return !!CONFIG.DROPBOX_KEY;
|
||||
}
|
||||
|
||||
SQL.Storage.Dropbox.prototype.showDropboxError = function(error) {
|
||||
var prefix = _("Dropbox error")+": ";
|
||||
var msg = error.status;
|
||||
|
||||
switch (error.status) {
|
||||
case Dropbox.ApiError.INVALID_TOKEN:
|
||||
// If you're using dropbox.js, the only cause behind this error is that
|
||||
// the user token expired.
|
||||
// Get the user through the authentication flow again.
|
||||
msg = _("Invalid Token (expired)");
|
||||
break;
|
||||
|
||||
case Dropbox.ApiError.NOT_FOUND:
|
||||
// The file or folder you tried to access is not in the user's Dropbox.
|
||||
// Handling this error is specific to your application.
|
||||
msg = _("File not found");
|
||||
break;
|
||||
|
||||
case Dropbox.ApiError.OVER_QUOTA:
|
||||
// The user is over their Dropbox quota.
|
||||
// Tell them their Dropbox is full. Refreshing the page won't help.
|
||||
msg = _("Dropbox is full");
|
||||
break;
|
||||
|
||||
case Dropbox.ApiError.RATE_LIMITED:
|
||||
// Too many API requests. Tell the user to try again later.
|
||||
// Long-term, optimize your code to use fewer API calls.
|
||||
break;
|
||||
|
||||
case Dropbox.ApiError.NETWORK_ERROR:
|
||||
// An error occurred at the XMLHttpRequest layer.
|
||||
// Most likely, the user's network connection is down.
|
||||
// API calls will not succeed until the user gets back online.
|
||||
msg = _("Network error");
|
||||
break;
|
||||
|
||||
case Dropbox.ApiError.INVALID_PARAM:
|
||||
case Dropbox.ApiError.OAUTH_ERROR:
|
||||
case Dropbox.ApiError.INVALID_METHOD:
|
||||
default:
|
||||
// Caused by a bug in dropbox.js, in your application, or in Dropbox.
|
||||
// Tell the user an error occurred, ask them to refresh the page.
|
||||
}
|
||||
|
||||
alert(prefix+msg);
|
||||
};
|
||||
|
||||
SQL.Storage.Dropbox.prototype.showDropboxAuthenticate = function() {
|
||||
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
|
||||
this.dropboxClient.authDriver(new Dropbox.AuthDriver.Popup({ receiverUrl: "dropbox-oauth-receiver.html" }));
|
||||
|
||||
// Now let's authenticate us
|
||||
var success = false;
|
||||
this.dropboxClient.authenticate( function(error, client) {
|
||||
if (error) {
|
||||
this.showDropboxError(error);
|
||||
return;
|
||||
}
|
||||
success = true;
|
||||
return;
|
||||
});
|
||||
return success;
|
||||
}
|
||||
|
||||
SQL.Storage.Dropbox.prototype._save = function(keyword) {
|
||||
if (!this.showDropboxAuthenticate()) return;
|
||||
|
||||
var xml = this.owner.owner.toXML();
|
||||
var filename = keyword + ".xml";
|
||||
this.owner.owner.window.showThrobber();
|
||||
|
||||
this.dropboxClient.writeFile(filename, xml, function(error, stat) {
|
||||
this.owner.owner.window.hideThrobber();
|
||||
if (error) {
|
||||
this.showDropboxError(error);
|
||||
} else {
|
||||
this._keyword = keyword;
|
||||
this.owner.getArea().value = filename+" "+_("was saved to Dropbox");
|
||||
}
|
||||
}.bind(this));
|
||||
}
|
||||
|
||||
SQL.Storage.Dropbox.prototype._load = function(keyword) {
|
||||
if (!this.showDropboxAuthenticate()) return;
|
||||
|
||||
var filename = keyword + ".xml";
|
||||
this.owner.owner.window.showThrobber();
|
||||
|
||||
this.dropboxClient.readFile(filename, function(error, data) {
|
||||
this.owner.owner.window.hideThrobber();
|
||||
if (error) {
|
||||
this.showDropboxError(error);
|
||||
} else {
|
||||
this.owner.fromXMLText(data);
|
||||
this._keyword = keyword;
|
||||
}
|
||||
}.bind(this));
|
||||
}
|
||||
|
||||
SQL.Storage.Dropbox.prototype._clickSave = function(e) {
|
||||
var keyword = prompt(_("serversaveprompt"), this._keyword);
|
||||
if (!keyword) { return; }
|
||||
this._save(keyword);
|
||||
}
|
||||
|
||||
SQL.Storage.Dropbox.prototype._clickLoad = function(e) {
|
||||
var keyword = prompt(_("serverloadprompt"), this._keyword);
|
||||
if (!keyword) { return; }
|
||||
this._load(keyword);
|
||||
}
|
||||
|
||||
SQL.Storage.Dropbox.prototype._clickList = function(e) {
|
||||
if (!this.showDropboxAuthenticate()) return;
|
||||
|
||||
this.owner.owner.window.showThrobber();
|
||||
this.dropboxClient.readdir("/", function(error, entries) {
|
||||
this.owner.owner.window.hideThrobber();
|
||||
if (error) {
|
||||
this.showDropboxError(error);
|
||||
} else {
|
||||
this.owner.getArea().value = entries.join("\n")+"\n"
|
||||
}
|
||||
}.bind(this));
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
SQL.Storage = function(owner) {
|
||||
this.owner = owner;
|
||||
this._keyword = "";
|
||||
this._dom = {
|
||||
option: document.createElement("option"),
|
||||
buttons: {}
|
||||
}
|
||||
}
|
||||
|
||||
SQL.Storage.prototype.getOption = function() {
|
||||
return this._dom.option;
|
||||
}
|
||||
|
||||
SQL.Storage.prototype.isSupported = function() {
|
||||
return true;
|
||||
}
|
||||
|
||||
SQL.Storage.prototype.activate = function(parent) {
|
||||
for (var p in this._dom.buttons) {
|
||||
parent.appendChild(this._dom.buttons[p]);
|
||||
}
|
||||
}
|
||||
|
||||
SQL.Storage.prototype.deactivate = function() {
|
||||
}
|
||||
|
||||
SQL.Storage.prototype.quicksave = function() {
|
||||
if (!this._keyword) { return; }
|
||||
this._save(this._keyword);
|
||||
}
|
||||
|
||||
SQL.Storage.prototype._save = function(keyword) {
|
||||
this._keyword = keyword;
|
||||
}
|
||||
|
||||
SQL.Storage.prototype._load = function(keyword) {
|
||||
this._keyword = keyword;
|
||||
}
|
||||
|
||||
SQL.Storage.prototype._buildButton = function(labelKey, method) {
|
||||
var button = document.createElement("button");
|
||||
button.innerHTML = _(labelKey);
|
||||
this._dom.buttons[labelKey] = button;
|
||||
OZ.Event.add(button, "click", this[method].bind(this));
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
SQL.Storage.REST = function(owner) {
|
||||
SQL.Storage.call(this, owner);
|
||||
this._dom.option.innerHTML = _("storage.rest");
|
||||
|
||||
this._dom.label = document.createElement("label");
|
||||
this._dom.label.innerHTML = _("backendlabel");
|
||||
|
||||
this._dom.backend = document.createElement("select");
|
||||
this._dom.label.appendChild(this._dom.backend);
|
||||
this._buildButton("serversave", "_clickSave");
|
||||
this._buildButton("serverload", "_clickLoad");
|
||||
this._buildButton("serverlist", "_clickList");
|
||||
this._buildButton("serverimport", "_clickImport");
|
||||
|
||||
var bs = CONFIG.AVAILABLE_BACKENDS;
|
||||
var be = CONFIG.DEFAULT_BACKEND;
|
||||
var r = window.location.search.substring(1).match(/backend=([^&]*)/);
|
||||
if (r) {
|
||||
req = r[1];
|
||||
if (bs.indexOf(req) != -1) {
|
||||
be = req;
|
||||
}
|
||||
}
|
||||
for (var i=0;i<bs.length;i++) {
|
||||
var o = document.createElement("option");
|
||||
o.value = bs[i];
|
||||
o.innerHTML = bs[i];
|
||||
this._dom.backend.appendChild(o);
|
||||
if (bs[i] == be) { this._dom.backend.selectedIndex = i; }
|
||||
}
|
||||
|
||||
var url = window.location.href;
|
||||
var r = url.match(/keyword=([^&]+)/);
|
||||
if (r) { this._load(r[1]); }
|
||||
}
|
||||
SQL.Storage.REST.prototype = Object.create(SQL.Storage.prototype);
|
||||
|
||||
SQL.Storage.REST.prototype.activate = function(parent) {
|
||||
parent.appendChild(this._dom.label);
|
||||
SQL.Storage.prototype.activate.call(this, parent);
|
||||
}
|
||||
|
||||
SQL.Storage.REST.prototype._save = function(keyword) {
|
||||
var xml = this.owner.owner.toXML();
|
||||
var bp = this.owner.owner.getOption("xhrpath");
|
||||
var url = bp + "backend/"+this._dom.backend.value+"/?action=save&keyword="+encodeURIComponent(keyword);
|
||||
var h = {"Content-type":"application/xml"};
|
||||
this.owner.owner.window.showThrobber();
|
||||
this.owner.owner.setTitle(keyword);
|
||||
this._keyword = keyword;
|
||||
OZ.Request(url, this._responseSave.bind(this), {xml:true, method:"post", data:xml, headers:h});
|
||||
}
|
||||
|
||||
SQL.Storage.REST.prototype._load = function(keyword) {
|
||||
var bp = this.owner.owner.getOption("xhrpath");
|
||||
var url = bp + "backend/"+this._dom.backend.value+"/?action=load&keyword="+encodeURIComponent(keyword);
|
||||
this.owner.owner.window.showThrobber();
|
||||
this._keyword = keyword;
|
||||
OZ.Request(url, this._responseLoad.bind(this), {xml:true});
|
||||
}
|
||||
|
||||
SQL.Storage.REST.prototype._clickSave = function(e) {
|
||||
var keyword = prompt(_("serversaveprompt"), this._keyword);
|
||||
if (!keyword) { return; }
|
||||
this._save(keyword);
|
||||
}
|
||||
|
||||
SQL.Storage.REST.prototype._clickLoad = function(e) {
|
||||
var keyword = prompt(_("serverloadprompt"), this._keyword);
|
||||
if (!keyword) { return; }
|
||||
this._load(keyword);
|
||||
}
|
||||
|
||||
SQL.Storage.REST.prototype._clickList = function(e) {
|
||||
var bp = this.owner.owner.getOption("xhrpath");
|
||||
var url = bp + "backend/"+this._dom.backend.value+"/?action=list";
|
||||
this.owner.owner.window.showThrobber();
|
||||
OZ.Request(url, this._responseList.bind(this));
|
||||
}
|
||||
|
||||
SQL.Storage.REST.prototype._clickImport = function(e) {
|
||||
var name = prompt(_("serverimportprompt"), "");
|
||||
if (!name) { return; }
|
||||
var bp = this.owner.owner.getOption("xhrpath");
|
||||
var url = bp + "backend/"+this._dom.backend.value+"/?action=import&database="+name;
|
||||
this.owner.owner.window.showThrobber();
|
||||
OZ.Request(url, this._responseImport.bind(this), {xml:true});
|
||||
}
|
||||
|
||||
SQL.Storage.REST.prototype._check = function(code) {
|
||||
switch (code) {
|
||||
case 201:
|
||||
case 404:
|
||||
case 500:
|
||||
case 501:
|
||||
case 503:
|
||||
var lang = "http"+code;
|
||||
this.owner.getArea().value = _("httpresponse")+": "+_(lang);
|
||||
return false;
|
||||
break;
|
||||
default: return true;
|
||||
}
|
||||
}
|
||||
|
||||
SQL.Storage.REST.prototype._responseSave = function(data, code) {
|
||||
this.owner.owner.window.hideThrobber();
|
||||
this._check(code);
|
||||
}
|
||||
|
||||
SQL.Storage.REST.prototype._responseLoad = function(data, code) {
|
||||
this.owner.owner.window.hideThrobber();
|
||||
if (!this._check(code)) { return; }
|
||||
this.owner.fromXML(data);
|
||||
this.owner.owner.setTitle(this._keyword);
|
||||
}
|
||||
|
||||
SQL.Storage.REST.prototype._responseList = function(data, code) {
|
||||
this.owner.owner.window.hideThrobber();
|
||||
if (!this._check(code)) { return; }
|
||||
this.owner.getArea().value = data;
|
||||
}
|
||||
|
||||
SQL.Storage.REST.prototype._responseImport = function(data, code) {
|
||||
this.owner.owner.window.hideThrobber();
|
||||
if (!this._check(code)) { return; }
|
||||
if (this.owner.fromXML(data)) {
|
||||
this.owner.owner.alignTables();
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -38,7 +38,7 @@ SQL.TableManager = function(owner) {
|
||||
OZ.Event.add(this.dom.aligntables, "click", this.owner.alignTables.bind(this.owner));
|
||||
OZ.Event.add(this.dom.edittable, "click", this.edit.bind(this));
|
||||
OZ.Event.add(this.dom.tablekeys, "click", this.keys.bind(this));
|
||||
OZ.Event.add(document, "keydown", this.press.bind(this));
|
||||
OZ.Event.add(window, "keydown", this.press.bind(this));
|
||||
|
||||
this.dom.container.parentNode.removeChild(this.dom.container);
|
||||
}
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@ SQL.Window = function(owner) {
|
||||
this.dom.throbber.alt = this.dom.throbber.title = _("throbber");
|
||||
OZ.Event.add(this.dom.ok, "click", this.ok.bind(this));
|
||||
OZ.Event.add(this.dom.cancel, "click", this.close.bind(this));
|
||||
OZ.Event.add(document, "keydown", this.key.bind(this));
|
||||
OZ.Event.add(window, "keydown", this.key.bind(this));
|
||||
|
||||
this.sync = this.sync.bind(this);
|
||||
|
||||
|
||||
@@ -93,20 +93,14 @@ SQL.Designer.prototype.init2 = function() { /* secondary init, after locale & da
|
||||
this.tableManager = new SQL.TableManager(this);
|
||||
this.rowManager = new SQL.RowManager(this);
|
||||
this.keyManager = new SQL.KeyManager(this);
|
||||
this.io = new SQL.IO(this);
|
||||
this.options = new SQL.Options(this);
|
||||
this.window = new SQL.Window(this);
|
||||
this.io = new SQL.IO(this);
|
||||
|
||||
this.sync();
|
||||
|
||||
OZ.$("docs").value = _("docs");
|
||||
|
||||
var url = window.location.href;
|
||||
var r = url.match(/keyword=([^&]+)/);
|
||||
if (r) {
|
||||
var keyword = r[1];
|
||||
this.io.serverload(false, keyword);
|
||||
}
|
||||
document.body.style.visibility = "visible";
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user