From b3bf2bd8ecc9bd175793f67c46a8319b7d67174c Mon Sep 17 00:00:00 2001 From: Ondrej Zara Date: Fri, 12 Jun 2015 09:30:51 +0200 Subject: [PATCH 1/4] js modularized --- index.html | 31 +- js/globals.js | 31 + js/io.js | 495 ++++++++ js/key.js | 74 ++ js/keymanager.js | 204 ++++ js/map.js | 119 ++ js/options.js | 107 ++ js/relation.js | 158 +++ js/row.js | 425 +++++++ js/rowmanager.js | 190 ++++ js/rubberband.js | 49 + js/table.js | 362 ++++++ js/tablemanager.js | 211 ++++ js/visual.js | 42 + js/window.js | 95 ++ js/wwwsqldesigner.js | 2576 ------------------------------------------ 16 files changed, 2585 insertions(+), 2584 deletions(-) create mode 100644 js/globals.js create mode 100644 js/io.js create mode 100644 js/key.js create mode 100644 js/keymanager.js create mode 100644 js/map.js create mode 100644 js/options.js create mode 100644 js/relation.js create mode 100644 js/row.js create mode 100644 js/rowmanager.js create mode 100644 js/rubberband.js create mode 100644 js/table.js create mode 100644 js/tablemanager.js create mode 100644 js/visual.js create mode 100644 js/window.js diff --git a/index.html b/index.html index 6d8ffa6..8085c9d 100755 --- a/index.html +++ b/index.html @@ -1,22 +1,37 @@ - + - + WWW SQL Designer - - + + - - - + + + + + + + + + + + + + + + + + + diff --git a/js/globals.js b/js/globals.js new file mode 100644 index 0000000..1d59315 --- /dev/null +++ b/js/globals.js @@ -0,0 +1,31 @@ +/* -------------------- configuration -------------------- */ + +/* + * The key below needs to be set individually by you if you want to use the Dropbox load/save feature. + * To do that, first sign up with Dropbox (may require a specific developer / SDK sign-up), go to + * https://www.dropbox.com/developers/apps and use "Create app" to add a new app. Call it, for instance, + * "wwwsqldesigner", and give it the "App Folder" permission. Unter "OAuth 2", "Redirect URIs", add + * the URL to the "dropbox-oauth-receiver.html" file on your server. E.g, if you install wwwsqldesigner + * on your local web server under "http://localhost/sqldesigner/", then add + * http://localhost/sqldesigner/dropbox-oauth-receiver.html as a Redirection URI. + * Copy the shown "App key" and paste it here below: + */ +var dropboxAppKey = null; // "your app key"; + + +/* -------------------- globals -------------------- */ + +function _(str) { /* getText */ + if (!(str in window.LOCALE)) { return str; } + return window.LOCALE[str]; +} + +if (typeof String.prototype.endsWith !== 'function') { + String.prototype.endsWith = function(suffix) { + return this.indexOf(suffix, this.length - suffix.length) !== -1; + }; +} + +var DATATYPES = false; +var LOCALE = {}; +var SQL = {}; diff --git a/js/io.js b/js/io.js new file mode 100644 index 0000000..1fdc3c4 --- /dev/null +++ b/js/io.js @@ -0,0 +1,495 @@ +/* --------------------- io ------------ */ + +SQL.IO = OZ.Class(); + +SQL.IO.prototype.init = 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.dom = { + container:OZ.$("io") + }; + + var ids = ["saveload","clientlocalsave", "clientsave", "clientlocalload", "clientlocallist","clientload", "clientsql", + "dropboxsave", "dropboxload", "dropboxlist", + "quicksave", "serversave", "serverload", + "serverlist", "serverimport"]; + for (var i=0;i= (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 (dropboxAppKey) { + this.dropboxClient = new Dropbox.Client({ key: dropboxAppKey }); +} else { + this.dropboxClient = null; + + // Hide the Dropbox buttons and divider + 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() { + var bp = this.owner.getOption("staticpath"); + var path = bp + "db/"+window.DATATYPES.getAttribute("db")+"/output.xsl"; + this.owner.window.showThrobber(); + OZ.Request(path, this.bind(this.finish), {xml:true}); +} + +SQL.IO.prototype.finish = function(xslDoc) { + this.owner.window.hideThrobber(); + var xml = this.owner.toXML(); + var sql = ""; + try { + if (window.XSLTProcessor && window.DOMParser) { + var parser = new DOMParser(); + var xmlDoc = parser.parseFromString(xml, "text/xml"); + var xsl = new XSLTProcessor(); + xsl.importStylesheet(xslDoc); + var result = xsl.transformToDocument(xmlDoc); + sql = result.documentElement.textContent; + } else if (window.ActiveXObject || "ActiveXObject" in window) { + var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); + xmlDoc.loadXML(xml); + sql = xmlDoc.transformNode(xslDoc); + } else { + throw new Error("No XSLT processor available"); + } + } catch(e) { + alert(_("xmlerror")+': '+e.message); + return; + } + 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.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) { + switch (e.keyCode) { + case 113: + if (OZ.opera) { + e.preventDefault(); + } + this.quicksave(e); + break; + } +} diff --git a/js/key.js b/js/key.js new file mode 100644 index 0000000..3b39d42 --- /dev/null +++ b/js/key.js @@ -0,0 +1,74 @@ +/* --------------------- db index ------------ */ + +SQL.Key = OZ.Class().extend(SQL.Visual); + +SQL.Key.prototype.init = function(owner, type, name) { + this.owner = owner; + this.rows = []; + this.type = type || "INDEX"; + this.name = name || ""; + SQL.Visual.prototype.init.apply(this); +} + +SQL.Key.prototype.setName = function(n) { + this.name = n; +} + +SQL.Key.prototype.getName = function() { + return this.name; +} + +SQL.Key.prototype.setType = function(t) { + if (!t) { return; } + this.type = t; + for (var i=0;i\n'; + for (var i=0;i\n'; + } + xml += '\n'; + return xml; +} + +SQL.Key.prototype.fromXML = function(node) { + this.setType(node.getAttribute("type")); + this.setName(node.getAttribute("name")); + var parts = node.getElementsByTagName("part"); + for (var i=0;i=0;i--) { + var k = this.table.keys[i]; + if (!k.rows.length) { this.table.removeKey(k); } + } +} + +SQL.KeyManager.prototype.sync = function(table) { /* sync content with given table */ + this.table = table; + this.dom.listlabel.innerHTML = _("keyslistlabel").replace(/%s/,table.getTitle()); + + OZ.DOM.clear(this.dom.list); + for (var i=0;i 1) { return; } + var event = e.touches[0]; + } else { + var event = e; + } + + var dx = event.clientX - this.x; + var dy = event.clientY - this.y; + if (this.l + dx < 0) { dx = -this.l; } + if (this.t + dy < 0) { dy = -this.t; } + if (this.l + this.w + 4 + dx > this.width) { dx = this.width - 4 - this.l - this.w; } + if (this.t + this.h + 4 + dy > this.height) { dy = this.height - 4 - this.t - this.h; } + + + this.x += dx; + this.y += dy; + + this.l += dx; + this.t += dy; + + var coefX = this.width / this.owner.width; + var coefY = this.height / this.owner.height; + var left = this.l / coefX; + var top = this.t / coefY; + + if (OZ.webkit) { + document.body.scrollLeft = Math.round(left); + document.body.scrollTop = Math.round(top); + } else { + document.documentElement.scrollLeft = Math.round(left); + document.documentElement.scrollTop = Math.round(top); + } + + this.redraw(); +} + +SQL.Map.prototype.up = function(e) { /* mouseup */ + this.flag = false; + this.dom.container.style.cursor = ""; + OZ.Event.remove(this.documentMove); + OZ.Event.remove(this.documentUp); +} + +SQL.Map.prototype.sync = function() { /* when window changes, adjust map */ + var dims = OZ.DOM.win(); + var scroll = OZ.DOM.scroll(); + var scaleX = this.width / this.owner.width; + var scaleY = this.height / this.owner.height; + + var w = dims[0] * scaleX - 4 - 0; + var h = dims[1] * scaleY - 4 - 0; + var x = scroll[0] * scaleX; + var y = scroll[1] * scaleY; + + this.w = Math.round(w); + this.h = Math.round(h); + this.l = Math.round(x); + this.t = Math.round(y); + + this.redraw(); +} + +SQL.Map.prototype.redraw = function() { + this.dom.port.style.width = this.w+"px"; + this.dom.port.style.height = this.h+"px"; + this.dom.port.style.left = this.l+"px"; + this.dom.port.style.top = this.t+"px"; +} diff --git a/js/options.js b/js/options.js new file mode 100644 index 0000000..64dbd95 --- /dev/null +++ b/js/options.js @@ -0,0 +1,107 @@ +/* --------------------- options ------------ */ + +SQL.Options = OZ.Class(); + +SQL.Options.prototype.init = function(owner) { + this.owner = owner; + this.dom = { + container:OZ.$("opts"), + btn:OZ.$("options") + } + this.dom.btn.value = _("options"); + this.save = this.bind(this.save); + this.build(); +} + +SQL.Options.prototype.build = function() { + this.dom.optionlocale = OZ.$("optionlocale"); + this.dom.optiondb = OZ.$("optiondb"); + this.dom.optionsnap = OZ.$("optionsnap"); + this.dom.optionpattern = OZ.$("optionpattern"); + this.dom.optionhide = OZ.$("optionhide"); + this.dom.optionvector = OZ.$("optionvector"); + this.dom.optionshowsize = OZ.$("optionshowsize"); + this.dom.optionshowtype = OZ.$("optionshowtype"); + + var ids = ["language","db","snap","pattern","hide","vector","showsize","showtype","optionsnapnotice","optionpatternnotice","optionsnotice"]; + for (var i=0;i\n'; + + var elm = this.getDataType(); + var t = elm.getAttribute("sql"); + if (this.data.size.length) { t += "("+this.data.size+")"; } + xml += ""+t+"\n"; + + if (this.data.def || this.data.def === null) { + var q = elm.getAttribute("quote"); + var d = this.data.def; + if (d === null) { + d = "NULL"; + } else if (d != "CURRENT_TIMESTAMP") { + d = q+d+q; + } + xml += ""+d+""; + } + + for (var i=0;i\n'; + } + + if (this.data.comment) { + var escaped = this.data.comment.replace(/&/g, "&").replace(/>/g, ">").replace(/\n"; + } + + xml += "\n"; + return xml; +} + +SQL.Row.prototype.fromXML = function(node) { + var name = node.getAttribute("name"); + + var obj = { type:0, size:"" }; + obj.nll = (node.getAttribute("null") == "1"); + obj.ai = (node.getAttribute("autoincrement") == "1"); + + var cs = node.getElementsByTagName("comment"); + if (cs.length && cs[0].firstChild) { obj.comment = cs[0].firstChild.nodeValue; } + + var d = node.getElementsByTagName("datatype"); + if (d.length && d[0].firstChild) { + var s = d[0].firstChild.nodeValue; + var r = s.match(/^([^\(]+)(\((.*)\))?.*$/); + var type = r[1]; + if (r[3]) { obj.size = r[3]; } + var types = window.DATATYPES.getElementsByTagName("type"); + for (var i=0;i 0; +} + +SQL.Row.prototype.enter = function(e) { + if (e.keyCode == 13) { + this.collapse(); + } +} diff --git a/js/rowmanager.js b/js/rowmanager.js new file mode 100644 index 0000000..a0257f6 --- /dev/null +++ b/js/rowmanager.js @@ -0,0 +1,190 @@ +/* --------------------- row manager ------------ */ + +SQL.RowManager = OZ.Class(); + +SQL.RowManager.prototype.init = function(owner) { + this.owner = owner; + this.dom = {}; + this.selected = null; + this.creating = false; + this.connecting = false; + + var ids = ["editrow","removerow","uprow","downrow","foreigncreate","foreignconnect","foreigndisconnect"]; + for (var i=0;i=0;i--) { + var r = rels[i]; + if (r.row2 == this.selected) { this.owner.removeRelation(r); } + } + this.redraw(); +} + +SQL.RowManager.prototype.endCreate = function() { + this.creating = false; + this.dom.foreigncreate.value = _("foreigncreate"); +} + +SQL.RowManager.prototype.endConnect = function() { + this.connecting = false; + this.dom.foreignconnect.value = _("foreignconnect"); +} + +SQL.RowManager.prototype.up = function(e) { + this.selected.up(); + this.redraw(); +} + +SQL.RowManager.prototype.down = function(e) { + this.selected.down(); + this.redraw(); +} + +SQL.RowManager.prototype.remove = function(e) { + var result = confirm(_("confirmrow")+" '"+this.selected.getTitle()+"' ?"); + if (!result) { return; } + var t = this.selected.owner; + this.selected.owner.removeRow(this.selected); + + var next = false; + if (t.rows) { next = t.rows[t.rows.length-1]; } + this.select(next); +} + +SQL.RowManager.prototype.redraw = function() { + this.endCreate(); + this.endConnect(); + if (this.selected) { + var table = this.selected.owner; + var rows = table.rows; + this.dom.uprow.disabled = (rows[0] == this.selected); + this.dom.downrow.disabled = (rows[rows.length-1] == this.selected); + this.dom.removerow.disabled = false; + this.dom.editrow.disabled = false; + this.dom.foreigncreate.disabled = !(this.selected.isUnique()); + this.dom.foreignconnect.disabled = !(this.selected.isUnique()); + + this.dom.foreigndisconnect.disabled = true; + var rels = this.selected.relations; + for (var i=0;i\n'; + for (var i=0;i/g, ">").replace(/\n"; + } + xml += "\n"; + return xml; +} + +SQL.Table.prototype.fromXML = function(node) { + var name = node.getAttribute("name"); + this.setTitle(name); + var x = parseInt(node.getAttribute("x")) || 0; + var y = parseInt(node.getAttribute("y")) || 0; + this.moveTo(x, y); + var rows = node.getElementsByTagName("row"); + for (var i=0;i 1) { return; } + var event = e.touches[0]; + } else { + var event = e; + } + + for (var i=0;i 1) { this.dom.removetable.value = _("removetables"); } + } else { + this.dom.removetable.disabled = true; + this.dom.removetable.value = _("removetable"); + } + for (var i=0;i=x && tx=x && tx1x1)) && + ((ty>=y && ty=y && ty1y1))) + { this.selection.push(t); } + } + this.processSelection(); +} + +SQL.TableManager.prototype.click = function(e) { /* finish adding new table */ + var newtable = false; + if (this.adding) { + this.adding = false; + OZ.DOM.removeClass("area","adding"); + this.dom.addtable.value = this.oldvalue; + var scroll = OZ.DOM.scroll(); + var x = e.clientX + scroll[0]; + var y = e.clientY + scroll[1]; + newtable = this.owner.addTable(_("newtable"),x,y); + var r = newtable.addRow("id",{ai:true}); + var k = newtable.addKey("PRIMARY",""); + k.addRow(r); + } + this.select(newtable); + this.owner.rowManager.select(false); + if (this.selection.length == 1) { this.edit(e); } +} + +SQL.TableManager.prototype.preAdd = function(e) { /* click add new table */ + if (this.adding) { + this.adding = false; + OZ.DOM.removeClass("area","adding"); + this.dom.addtable.value = this.oldvalue; + } else { + this.adding = true; + OZ.DOM.addClass("area","adding"); + this.oldvalue = this.dom.addtable.value; + this.dom.addtable.value = "["+_("addpending")+"]"; + } +} + +SQL.TableManager.prototype.clear = function(e) { /* remove all tables */ + if (!this.owner.tables.length) { return; } + var result = confirm(_("confirmall")+" ?"); + if (!result) { return; } + this.owner.clearTables(); +} + +SQL.TableManager.prototype.remove = function(e) { + var titles = this.selection.slice(0); + for (var i=0;i 1) { this.dom.title.removeChild(this.dom.title.childNodes[1]); } + + var txt = OZ.DOM.text(title); + this.dom.title.appendChild(txt); + this.dom.background.style.visibility = "visible"; + OZ.DOM.clear(this.dom.content); + this.dom.content.appendChild(content); + + var win = OZ.DOM.win(); + var scroll = OZ.DOM.scroll(); + this.dom.container.style.left = Math.round(scroll[0] + (win[0] - this.dom.container.offsetWidth)/2)+"px"; + this.dom.container.style.top = Math.round(scroll[1] + (win[1] - this.dom.container.offsetHeight)/2)+"px"; + + this.dom.cancel.style.visibility = (this.callback ? "" : "hidden"); + this.dom.container.style.visibility = "visible"; + + var formElements = ["input","select","textarea"]; + var all = this.dom.container.getElementsByTagName("*"); + for (var i=0;i\n'; - - var elm = this.getDataType(); - var t = elm.getAttribute("sql"); - if (this.data.size.length) { t += "("+this.data.size+")"; } - xml += ""+t+"\n"; - - if (this.data.def || this.data.def === null) { - var q = elm.getAttribute("quote"); - var d = this.data.def; - if (d === null) { - d = "NULL"; - } else if (d != "CURRENT_TIMESTAMP") { - d = q+d+q; - } - xml += ""+d+""; - } - - for (var i=0;i\n'; - } - - if (this.data.comment) { - var escaped = this.data.comment.replace(/&/g, "&").replace(/>/g, ">").replace(/\n"; - } - - xml += "\n"; - return xml; -} - -SQL.Row.prototype.fromXML = function(node) { - var name = node.getAttribute("name"); - - var obj = { type:0, size:"" }; - obj.nll = (node.getAttribute("null") == "1"); - obj.ai = (node.getAttribute("autoincrement") == "1"); - - var cs = node.getElementsByTagName("comment"); - if (cs.length && cs[0].firstChild) { obj.comment = cs[0].firstChild.nodeValue; } - - var d = node.getElementsByTagName("datatype"); - if (d.length && d[0].firstChild) { - var s = d[0].firstChild.nodeValue; - var r = s.match(/^([^\(]+)(\((.*)\))?.*$/); - var type = r[1]; - if (r[3]) { obj.size = r[3]; } - var types = window.DATATYPES.getElementsByTagName("type"); - for (var i=0;i 0; -} - -SQL.Row.prototype.enter = function(e) { - if (e.keyCode == 13) { - this.collapse(); - } -} - -/* --------------------------- relation (connector) ----------- */ - -SQL.Relation = OZ.Class().extend(SQL.Visual); -SQL.Relation._counter = 0; -SQL.Relation.prototype.init = function(owner, row1, row2) { - this.owner = owner; - this.row1 = row1; - this.row2 = row2; - this.color = "#000"; - this.hidden = false; - SQL.Visual.prototype.init.apply(this); - - /* if one of the rows already has relations, inherit color */ - var all = row1.relations.concat(row2.relations); - 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; - this.color = CONFIG.RELATION_COLORS[colorIndex % CONFIG.RELATION_COLORS.length]; - } - - this.row1.addRelation(this); - this.row2.addRelation(this); - this.dom = []; - - if (this.owner.vector) { - var path = document.createElementNS(this.owner.svgNS, "path"); - path.setAttribute("stroke", this.color); - path.setAttribute("stroke-width", CONFIG.RELATION_THICKNESS); - path.setAttribute("fill", "none"); - this.owner.dom.svg.appendChild(path); - this.dom.push(path); - } else { - for (var i=0;i<3;i++) { - var div = OZ.DOM.elm("div",{position:"absolute",className:"relation",backgroundColor:this.color}); - this.dom.push(div); - if (i & 1) { /* middle */ - OZ.Style.set(div, {width:CONFIG.RELATION_THICKNESS+"px"}); - } else { /* first & last */ - OZ.Style.set(div, {height:CONFIG.RELATION_THICKNESS+"px"}); - } - this.owner.dom.container.appendChild(div); - } - } - - this.redraw(); -} - -SQL.Relation.prototype.getColor = function() { - return this.color; -} - -SQL.Relation.prototype.show = function() { - this.hidden = false; - for (var i=0;i\n'; - for (var i=0;i/g, ">").replace(/\n"; - } - xml += "\n"; - return xml; -} - -SQL.Table.prototype.fromXML = function(node) { - var name = node.getAttribute("name"); - this.setTitle(name); - var x = parseInt(node.getAttribute("x")) || 0; - var y = parseInt(node.getAttribute("y")) || 0; - this.moveTo(x, y); - var rows = node.getElementsByTagName("row"); - for (var i=0;i 1) { return; } - var event = e.touches[0]; - } else { - var event = e; - } - - for (var i=0;i\n'; - for (var i=0;i\n'; - } - xml += '\n'; - return xml; -} - -SQL.Key.prototype.fromXML = function(node) { - this.setType(node.getAttribute("type")); - this.setName(node.getAttribute("name")); - var parts = node.getElementsByTagName("part"); - for (var i=0;i 1) { return; } - var event = e.touches[0]; - } else { - var event = e; - } - - var dx = event.clientX - this.x; - var dy = event.clientY - this.y; - if (this.l + dx < 0) { dx = -this.l; } - if (this.t + dy < 0) { dy = -this.t; } - if (this.l + this.w + 4 + dx > this.width) { dx = this.width - 4 - this.l - this.w; } - if (this.t + this.h + 4 + dy > this.height) { dy = this.height - 4 - this.t - this.h; } - - - this.x += dx; - this.y += dy; - - this.l += dx; - this.t += dy; - - var coefX = this.width / this.owner.width; - var coefY = this.height / this.owner.height; - var left = this.l / coefX; - var top = this.t / coefY; - - if (OZ.webkit) { - document.body.scrollLeft = Math.round(left); - document.body.scrollTop = Math.round(top); - } else { - document.documentElement.scrollLeft = Math.round(left); - document.documentElement.scrollTop = Math.round(top); - } - - this.redraw(); -} - -SQL.Map.prototype.up = function(e) { /* mouseup */ - this.flag = false; - this.dom.container.style.cursor = ""; - OZ.Event.remove(this.documentMove); - OZ.Event.remove(this.documentUp); -} - -SQL.Map.prototype.sync = function() { /* when window changes, adjust map */ - var dims = OZ.DOM.win(); - var scroll = OZ.DOM.scroll(); - var scaleX = this.width / this.owner.width; - var scaleY = this.height / this.owner.height; - - var w = dims[0] * scaleX - 4 - 0; - var h = dims[1] * scaleY - 4 - 0; - var x = scroll[0] * scaleX; - var y = scroll[1] * scaleY; - - this.w = Math.round(w); - this.h = Math.round(h); - this.l = Math.round(x); - this.t = Math.round(y); - - this.redraw(); -} - -SQL.Map.prototype.redraw = function() { - this.dom.port.style.width = this.w+"px"; - this.dom.port.style.height = this.h+"px"; - this.dom.port.style.left = this.l+"px"; - this.dom.port.style.top = this.t+"px"; -} - -/* --------------------- io ------------ */ - -SQL.IO = OZ.Class(); - -SQL.IO.prototype.init = 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.dom = { - container:OZ.$("io") - }; - - var ids = ["saveload","clientlocalsave", "clientsave", "clientlocalload", "clientlocallist","clientload", "clientsql", - "dropboxsave", "dropboxload", "dropboxlist", - "quicksave", "serversave", "serverload", - "serverlist", "serverimport"]; - for (var i=0;i= (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 (dropboxAppKey) { - this.dropboxClient = new Dropbox.Client({ key: dropboxAppKey }); -} else { - this.dropboxClient = null; - - // Hide the Dropbox buttons and divider - 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() { - var bp = this.owner.getOption("staticpath"); - var path = bp + "db/"+window.DATATYPES.getAttribute("db")+"/output.xsl"; - this.owner.window.showThrobber(); - OZ.Request(path, this.bind(this.finish), {xml:true}); -} - -SQL.IO.prototype.finish = function(xslDoc) { - this.owner.window.hideThrobber(); - var xml = this.owner.toXML(); - var sql = ""; - try { - if (window.XSLTProcessor && window.DOMParser) { - var parser = new DOMParser(); - var xmlDoc = parser.parseFromString(xml, "text/xml"); - var xsl = new XSLTProcessor(); - xsl.importStylesheet(xslDoc); - var result = xsl.transformToDocument(xmlDoc); - sql = result.documentElement.textContent; - } else if (window.ActiveXObject || "ActiveXObject" in window) { - var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); - xmlDoc.loadXML(xml); - sql = xmlDoc.transformNode(xslDoc); - } else { - throw new Error("No XSLT processor available"); - } - } catch(e) { - alert(_("xmlerror")+': '+e.message); - return; - } - 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.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) { - switch (e.keyCode) { - case 113: - if (OZ.opera) { - e.preventDefault(); - } - this.quicksave(e); - break; - } -} - -/* --------------------- table manager ------------ */ - -SQL.TableManager = OZ.Class(); - -SQL.TableManager.prototype.init = function(owner) { - this.owner = owner; - this.dom = { - container:OZ.$("table"), - name:OZ.$("tablename"), - comment:OZ.$("tablecomment") - }; - this.selection = []; - this.adding = false; - - var ids = ["addtable","removetable","aligntables","cleartables","addrow","edittable","tablekeys"]; - for (var i=0;i 1) { this.dom.removetable.value = _("removetables"); } - } else { - this.dom.removetable.disabled = true; - this.dom.removetable.value = _("removetable"); - } - for (var i=0;i=x && tx=x && tx1x1)) && - ((ty>=y && ty=y && ty1y1))) - { this.selection.push(t); } - } - this.processSelection(); -} - -SQL.TableManager.prototype.click = function(e) { /* finish adding new table */ - var newtable = false; - if (this.adding) { - this.adding = false; - OZ.DOM.removeClass("area","adding"); - this.dom.addtable.value = this.oldvalue; - var scroll = OZ.DOM.scroll(); - var x = e.clientX + scroll[0]; - var y = e.clientY + scroll[1]; - newtable = this.owner.addTable(_("newtable"),x,y); - var r = newtable.addRow("id",{ai:true}); - var k = newtable.addKey("PRIMARY",""); - k.addRow(r); - } - this.select(newtable); - this.owner.rowManager.select(false); - if (this.selection.length == 1) { this.edit(e); } -} - -SQL.TableManager.prototype.preAdd = function(e) { /* click add new table */ - if (this.adding) { - this.adding = false; - OZ.DOM.removeClass("area","adding"); - this.dom.addtable.value = this.oldvalue; - } else { - this.adding = true; - OZ.DOM.addClass("area","adding"); - this.oldvalue = this.dom.addtable.value; - this.dom.addtable.value = "["+_("addpending")+"]"; - } -} - -SQL.TableManager.prototype.clear = function(e) { /* remove all tables */ - if (!this.owner.tables.length) { return; } - var result = confirm(_("confirmall")+" ?"); - if (!result) { return; } - this.owner.clearTables(); -} - -SQL.TableManager.prototype.remove = function(e) { - var titles = this.selection.slice(0); - for (var i=0;i=0;i--) { - var r = rels[i]; - if (r.row2 == this.selected) { this.owner.removeRelation(r); } - } - this.redraw(); -} - -SQL.RowManager.prototype.endCreate = function() { - this.creating = false; - this.dom.foreigncreate.value = _("foreigncreate"); -} - -SQL.RowManager.prototype.endConnect = function() { - this.connecting = false; - this.dom.foreignconnect.value = _("foreignconnect"); -} - -SQL.RowManager.prototype.up = function(e) { - this.selected.up(); - this.redraw(); -} - -SQL.RowManager.prototype.down = function(e) { - this.selected.down(); - this.redraw(); -} - -SQL.RowManager.prototype.remove = function(e) { - var result = confirm(_("confirmrow")+" '"+this.selected.getTitle()+"' ?"); - if (!result) { return; } - var t = this.selected.owner; - this.selected.owner.removeRow(this.selected); - - var next = false; - if (t.rows) { next = t.rows[t.rows.length-1]; } - this.select(next); -} - -SQL.RowManager.prototype.redraw = function() { - this.endCreate(); - this.endConnect(); - if (this.selected) { - var table = this.selected.owner; - var rows = table.rows; - this.dom.uprow.disabled = (rows[0] == this.selected); - this.dom.downrow.disabled = (rows[rows.length-1] == this.selected); - this.dom.removerow.disabled = false; - this.dom.editrow.disabled = false; - this.dom.foreigncreate.disabled = !(this.selected.isUnique()); - this.dom.foreignconnect.disabled = !(this.selected.isUnique()); - - this.dom.foreigndisconnect.disabled = true; - var rels = this.selected.relations; - for (var i=0;i=0;i--) { - var k = this.table.keys[i]; - if (!k.rows.length) { this.table.removeKey(k); } - } -} - -SQL.KeyManager.prototype.sync = function(table) { /* sync content with given table */ - this.table = table; - this.dom.listlabel.innerHTML = _("keyslistlabel").replace(/%s/,table.getTitle()); - - OZ.DOM.clear(this.dom.list); - for (var i=0;i 1) { this.dom.title.removeChild(this.dom.title.childNodes[1]); } - - var txt = OZ.DOM.text(title); - this.dom.title.appendChild(txt); - this.dom.background.style.visibility = "visible"; - OZ.DOM.clear(this.dom.content); - this.dom.content.appendChild(content); - - var win = OZ.DOM.win(); - var scroll = OZ.DOM.scroll(); - this.dom.container.style.left = Math.round(scroll[0] + (win[0] - this.dom.container.offsetWidth)/2)+"px"; - this.dom.container.style.top = Math.round(scroll[1] + (win[1] - this.dom.container.offsetHeight)/2)+"px"; - - this.dom.cancel.style.visibility = (this.callback ? "" : "hidden"); - this.dom.container.style.visibility = "visible"; - - var formElements = ["input","select","textarea"]; - var all = this.dom.container.getElementsByTagName("*"); - for (var i=0;i Date: Fri, 12 Jun 2015 09:40:15 +0200 Subject: [PATCH 2/4] dropbox conf in config.js, fixed button hiding --- js/config.js | 15 ++++++++- js/globals.js | 17 ----------- js/io.js | 84 +++++++++++++++++++++++++-------------------------- 3 files changed, 55 insertions(+), 61 deletions(-) diff --git a/js/config.js b/js/config.js index f455200..f591ab1 100644 --- a/js/config.js +++ b/js/config.js @@ -13,5 +13,18 @@ var CONFIG = { RELATION_COLORS: ["#000", "#800", "#080", "#008", "#088", "#808", "#088"], STATIC_PATH: "", - XHR_PATH: "" + XHR_PATH: "", + + /* + * The key below needs to be set individually by you if you want to use the Dropbox load/save feature. + * To do that, first sign up with Dropbox (may require a specific developer / SDK sign-up), go to + * https://www.dropbox.com/developers/apps and use "Create app" to add a new app. Call it, for instance, + * "wwwsqldesigner", and give it the "App Folder" permission. Unter "OAuth 2", "Redirect URIs", add + * the URL to the "dropbox-oauth-receiver.html" file on your server. E.g, if you install wwwsqldesigner + * on your local web server under "http://localhost/sqldesigner/", then add + * http://localhost/sqldesigner/dropbox-oauth-receiver.html as a Redirection URI. + * Copy the shown "App key" and paste it here below: + */ + DROPBOX_KEY: null // "your app key"; + } diff --git a/js/globals.js b/js/globals.js index 1d59315..1dad0dc 100644 --- a/js/globals.js +++ b/js/globals.js @@ -1,20 +1,3 @@ -/* -------------------- configuration -------------------- */ - -/* - * The key below needs to be set individually by you if you want to use the Dropbox load/save feature. - * To do that, first sign up with Dropbox (may require a specific developer / SDK sign-up), go to - * https://www.dropbox.com/developers/apps and use "Create app" to add a new app. Call it, for instance, - * "wwwsqldesigner", and give it the "App Folder" permission. Unter "OAuth 2", "Redirect URIs", add - * the URL to the "dropbox-oauth-receiver.html" file on your server. E.g, if you install wwwsqldesigner - * on your local web server under "http://localhost/sqldesigner/", then add - * http://localhost/sqldesigner/dropbox-oauth-receiver.html as a Redirection URI. - * Copy the shown "App key" and paste it here below: - */ -var dropboxAppKey = null; // "your app key"; - - -/* -------------------- globals -------------------- */ - function _(str) { /* getText */ if (!(str in window.LOCALE)) { return str; } return window.LOCALE[str]; diff --git a/js/io.js b/js/io.js index 1fdc3c4..55dadfb 100644 --- a/js/io.js +++ b/js/io.js @@ -32,7 +32,10 @@ SQL.IO.prototype.init = function(owner) { 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 = ""; @@ -58,8 +61,6 @@ SQL.IO.prototype.init = function(owner) { OZ.Event.add(this.dom.serverimport, "click", this.bind(this.serverimport)); OZ.Event.add(document, "keydown", this.bind(this.press)); this.build(); - - this.dropBoxInit (); } SQL.IO.prototype.build = function() { @@ -191,52 +192,49 @@ SQL.IO.prototype.clientlocalload = function() { } 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); + 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 (dropboxAppKey) { - this.dropboxClient = new Dropbox.Client({ key: dropboxAppKey }); -} else { +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 and divider + // 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"; } @@ -349,7 +347,7 @@ SQL.IO.prototype.dropboxload = function() { SQL.IO.prototype.dropboxlist = function() { if (!this.showDropboxAuthenticate()) return; - + var sql_io = this; sql_io.listresponse("Loading...", 200); this.dropboxClient.readdir("/", function(error, entries) { From 097b9e89c95e4de2c184f40a1e8ef5e05bda9ddc Mon Sep 17 00:00:00 2001 From: Ondrej Zara Date: Fri, 12 Jun 2015 13:11:35 +0200 Subject: [PATCH 3/4] no more OZ.Class(), dedicated pubsub, std bind --- index.html | 1 + js/globals.js | 55 ++++++++++++++++++++++++++++++++++++++++---- js/io.js | 48 ++++++++++++++++++-------------------- js/key.js | 8 +++---- js/keymanager.js | 20 ++++++++-------- js/map.js | 17 +++++++------- js/options.js | 37 +++-------------------------- js/relation.js | 8 +++---- js/row.js | 18 +++++++-------- js/rowmanager.js | 26 ++++++++++----------- js/rubberband.js | 13 +++++------ js/table.js | 22 ++++++++---------- js/tablemanager.js | 24 +++++++++---------- js/visual.js | 4 +--- js/window.js | 12 ++++------ js/wwwsqldesigner.js | 17 ++++---------- 16 files changed, 159 insertions(+), 171 deletions(-) diff --git a/index.html b/index.html index 8085c9d..24b98ad 100755 --- a/index.html +++ b/index.html @@ -25,6 +25,7 @@ + diff --git a/js/globals.js b/js/globals.js index 1dad0dc..29f62c5 100644 --- a/js/globals.js +++ b/js/globals.js @@ -4,11 +4,58 @@ function _(str) { /* getText */ } if (typeof String.prototype.endsWith !== 'function') { - String.prototype.endsWith = function(suffix) { - return this.indexOf(suffix, this.length - suffix.length) !== -1; - }; + String.prototype.endsWith = function(suffix) { + return this.indexOf(suffix, this.length - suffix.length) !== -1; + }; +} + +if (!String.prototype.trim) { + String.prototype.trim = function() { + return this.match(/^\s*([\s\S]*?)\s*$/)[1]; + } +} + +if (!String.trim) { + String.trim = function(obj) { return String.prototype.trim.call(obj);} +} + +if (!Object.create) { + Object.create = function (o) { + if (arguments.length > 1) { throw new Error("Object.create polyfill only accepts the first parameter"); } + var tmp = function() {}; + tmp.prototype = o; + return new tmp(); + }; } var DATATYPES = false; var LOCALE = {}; -var SQL = {}; +var SQL = { + _subscribers: {}, + + publish: function(message, publisher, data) { + var subscribers = this._subscribers[message] || []; + var obj = { + target: publisher, + data: data + } + subscribers.forEach(function(subscriber) { subscriber(obj); }); + }, + + subscribe: function(message, subscriber) { + if (!(message in this._subscribers)) { + this._subscribers[message] = []; + } + var index = this._subscribers[message].indexOf(subscriber); + if (index == -1) { this._subscribers[message].push(subscriber); } + }, + + unsubscribe: function(message, subscriber) { + var index = this._subscribers[message].indexOf(subscriber); + if (index > -1) { this._subscribers[message].splice(index, 1); } + } +} + +window.onbeforeunload = function(e) { + return ""; /* some browsers will show this text, some won't. */ +} diff --git a/js/io.js b/js/io.js index 55dadfb..ea1b4b7 100644 --- a/js/io.js +++ b/js/io.js @@ -1,8 +1,4 @@ -/* --------------------- io ------------ */ - -SQL.IO = OZ.Class(); - -SQL.IO.prototype.init = function(owner) { +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 */ @@ -39,27 +35,27 @@ SQL.IO.prototype.init = function(owner) { this.dom.container.parentNode.removeChild(this.dom.container); this.dom.container.style.visibility = ""; - this.saveresponse = this.bind(this.saveresponse); - this.loadresponse = this.bind(this.loadresponse); - this.listresponse = this.bind(this.listresponse); - this.importresponse = this.bind(this.importresponse); + 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.bind(this.click)); - OZ.Event.add(this.dom.clientlocalsave, "click", this.bind(this.clientlocalsave)); - OZ.Event.add(this.dom.clientsave, "click", this.bind(this.clientsave)); - OZ.Event.add(this.dom.clientlocalload, "click", this.bind(this.clientlocalload)); - OZ.Event.add(this.dom.clientlocallist, "click", this.bind(this.clientlocallist)); - OZ.Event.add(this.dom.clientload, "click", this.bind(this.clientload)); - OZ.Event.add(this.dom.dropboxload, "click", this.bind(this.dropboxload)); - OZ.Event.add(this.dom.dropboxsave, "click", this.bind(this.dropboxsave)); - OZ.Event.add(this.dom.dropboxlist, "click", this.bind(this.dropboxlist)); - OZ.Event.add(this.dom.clientsql, "click", this.bind(this.clientsql)); - OZ.Event.add(this.dom.quicksave, "click", this.bind(this.quicksave)); - OZ.Event.add(this.dom.serversave, "click", this.bind(this.serversave)); - OZ.Event.add(this.dom.serverload, "click", this.bind(this.serverload)); - OZ.Event.add(this.dom.serverlist, "click", this.bind(this.serverlist)); - OZ.Event.add(this.dom.serverimport, "click", this.bind(this.serverimport)); - OZ.Event.add(document, "keydown", this.bind(this.press)); + 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(); } @@ -367,7 +363,7 @@ 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.bind(this.finish), {xml:true}); + OZ.Request(path, this.finish.bind(this), {xml:true}); } SQL.IO.prototype.finish = function(xslDoc) { diff --git a/js/key.js b/js/key.js index 3b39d42..c1eb8b2 100644 --- a/js/key.js +++ b/js/key.js @@ -1,14 +1,12 @@ /* --------------------- db index ------------ */ - -SQL.Key = OZ.Class().extend(SQL.Visual); - -SQL.Key.prototype.init = function(owner, type, name) { +SQL.Key = function(owner, type, name) { this.owner = owner; this.rows = []; this.type = type || "INDEX"; this.name = name || ""; - SQL.Visual.prototype.init.apply(this); + SQL.Visual.apply(this); } +SQL.Key.prototype = Object.create(SQL.Visual.prototype); SQL.Key.prototype.setName = function(n) { this.name = n; diff --git a/js/keymanager.js b/js/keymanager.js index e053a0a..5e08e2a 100644 --- a/js/keymanager.js +++ b/js/keymanager.js @@ -1,8 +1,6 @@ /* ----------------- key manager ---------- */ -SQL.KeyManager = OZ.Class(); - -SQL.KeyManager.prototype.init = function(owner) { +SQL.KeyManager = function(owner) { this.owner = owner; this.dom = { container:OZ.$("keys") @@ -44,15 +42,15 @@ SQL.KeyManager.prototype.build = function() { this.dom.type.appendChild(o); } - this.purge = this.bind(this.purge); + this.purge = this.purge.bind(this); - OZ.Event.add(this.dom.list, "change", this.bind(this.listchange)); - OZ.Event.add(this.dom.type, "change", this.bind(this.typechange)); - OZ.Event.add(this.dom.name, "keyup", this.bind(this.namechange)); - OZ.Event.add(this.dom.keyadd, "click", this.bind(this.add)); - OZ.Event.add(this.dom.keyremove, "click", this.bind(this.remove)); - OZ.Event.add(this.dom.left, "click", this.bind(this.left)); - OZ.Event.add(this.dom.right, "click", this.bind(this.right)); + OZ.Event.add(this.dom.list, "change", this.listchange.bind(this)); + OZ.Event.add(this.dom.type, "change", this.typechange.bind(this)); + OZ.Event.add(this.dom.name, "keyup", this.namechange.bind(this)); + OZ.Event.add(this.dom.keyadd, "click", this.add.bind(this)); + OZ.Event.add(this.dom.keyremove, "click", this.remove.bind(this)); + OZ.Event.add(this.dom.left, "click", this.left.bind(this)); + OZ.Event.add(this.dom.right, "click", this.right.bind(this)); this.dom.container.parentNode.removeChild(this.dom.container); } diff --git a/js/map.js b/js/map.js index 8a82646..c280d34 100644 --- a/js/map.js +++ b/js/map.js @@ -1,27 +1,26 @@ /* --------------------- minimap ------------ */ -SQL.Map = OZ.Class().extend(SQL.Visual); - -SQL.Map.prototype.init = function(owner) { +SQL.Map = function(owner) { this.owner = owner; - SQL.Visual.prototype.init.apply(this); + SQL.Visual.apply(this); this.dom.container = OZ.$("minimap"); this.width = this.dom.container.offsetWidth - 2; this.height = this.dom.container.offsetHeight - 2; this.dom.port = OZ.DOM.elm("div",{className:"port", zIndex:1}); this.dom.container.appendChild(this.dom.port); - this.sync = this.bind(this.sync); + this.sync = this.sync.bind(this); this.flag = false; this.sync(); OZ.Event.add(window, "resize", this.sync); OZ.Event.add(window, "scroll", this.sync); - OZ.Event.add(this.dom.container, "mousedown", this.bind(this.down)); - OZ.Event.add(this.dom.container, "touchstart", this.bind(this.down)); + OZ.Event.add(this.dom.container, "mousedown", this.down.bind(this)); + OZ.Event.add(this.dom.container, "touchstart", this.down.bind(this)); OZ.Event.add(this.dom.container, "touchmove", OZ.Event.prevent); } +SQL.Map.prototype = Object.create(SQL.Visual.prototype); SQL.Map.prototype.down = function(e) { /* mousedown - move view and start drag */ this.flag = true; @@ -40,8 +39,8 @@ SQL.Map.prototype.down = function(e) { /* mousedown - move view and start drag * var eventUp = "mouseup"; } - this.documentMove = OZ.Event.add(document, eventMove, this.bind(this.move)); - this.documentUp = OZ.Event.add(document, eventUp, this.bind(this.up)); + this.documentMove = OZ.Event.add(document, eventMove, this.move.bind(this)); + this.documentUp = OZ.Event.add(document, eventUp, this.up.bind(this)); } SQL.Map.prototype.move = function(e) { /* mousemove */ diff --git a/js/options.js b/js/options.js index 64dbd95..90854e5 100644 --- a/js/options.js +++ b/js/options.js @@ -1,15 +1,13 @@ /* --------------------- options ------------ */ -SQL.Options = OZ.Class(); - -SQL.Options.prototype.init = function(owner) { +SQL.Options = function(owner) { this.owner = owner; this.dom = { container:OZ.$("opts"), btn:OZ.$("options") } this.dom.btn.value = _("options"); - this.save = this.bind(this.save); + this.save = this.save.bind(this); this.build(); } @@ -51,7 +49,7 @@ SQL.Options.prototype.build = function() { } - OZ.Event.add(this.dom.btn, "click", this.bind(this.click)); + OZ.Event.add(this.dom.btn, "click", this.click.bind(this)); this.dom.container.parentNode.removeChild(this.dom.container); } @@ -76,32 +74,3 @@ SQL.Options.prototype.click = function() { this.dom.optionshowsize.checked = this.owner.getOption("showsize"); this.dom.optionshowtype.checked = this.owner.getOption("showtype"); } - -/* ------------------ minimize/restore bar ----------- */ - -SQL.Toggle = OZ.Class(); - -SQL.Toggle.prototype.init = function(elm) { - this._state = null; - this._elm = elm; - OZ.Event.add(elm, "click", this._click.bind(this)); - - var defaultState = true; - if (document.location.href.match(/toolbar=hidden/)) { defaultState = false; } - this._switch(defaultState); -} - -SQL.Toggle.prototype._click = function(e) { - this._switch(!this._state); -} - -SQL.Toggle.prototype._switch = function(state) { - this._state = state; - if (this._state) { - OZ.$("bar").style.height = ""; - } else { - OZ.$("bar").style.overflow = "hidden"; - OZ.$("bar").style.height = this._elm.offsetHeight + "px"; - } - this._elm.className = (this._state ? "on" : "off"); -} diff --git a/js/relation.js b/js/relation.js index 40e48c8..f593759 100644 --- a/js/relation.js +++ b/js/relation.js @@ -1,14 +1,12 @@ /* --------------------------- relation (connector) ----------- */ -SQL.Relation = OZ.Class().extend(SQL.Visual); -SQL.Relation._counter = 0; -SQL.Relation.prototype.init = function(owner, row1, row2) { +SQL.Relation = function(owner, row1, row2) { this.owner = owner; this.row1 = row1; this.row2 = row2; this.color = "#000"; this.hidden = false; - SQL.Visual.prototype.init.apply(this); + SQL.Visual.apply(this); /* if one of the rows already has relations, inherit color */ var all = row1.relations.concat(row2.relations); @@ -46,6 +44,8 @@ SQL.Relation.prototype.init = function(owner, row1, row2) { this.redraw(); } +SQL.Relation._counter = 0; +SQL.Relation.prototype = Object.create(SQL.Visual.prototype); SQL.Relation.prototype.getColor = function() { return this.color; diff --git a/js/row.js b/js/row.js index e09afd5..ac1c94b 100644 --- a/js/row.js +++ b/js/row.js @@ -1,15 +1,12 @@ /* --------------------- table row ( = db column) ------------ */ - -SQL.Row = OZ.Class().extend(SQL.Visual); - -SQL.Row.prototype.init = function(owner, title, data) { +SQL.Row = function(owner, title, data) { this.owner = owner; this.relations = []; this.keys = []; this.selected = false; this.expanded = false; - SQL.Visual.prototype.init.apply(this); + SQL.Visual.apply(this); this.data.type = 0; this.data.size = ""; @@ -21,6 +18,7 @@ SQL.Row.prototype.init = function(owner, title, data) { if (data) { this.update(data); } this.setTitle(title); } +SQL.Row.prototype = Object.create(SQL.Visual.prototype); SQL.Row.prototype._build = function() { this.dom.container = OZ.DOM.elm("tbody"); @@ -38,11 +36,11 @@ SQL.Row.prototype._build = function() { [td1, this.dom.selected, this.dom.title] ); - this.enter = this.bind(this.enter); - this.changeComment = this.bind(this.changeComment); + this.enter = this.enter.bind(this); + this.changeComment = this.changeComment.bind(this); - OZ.Event.add(this.dom.container, "click",this.bind(this.click)); - OZ.Event.add(this.dom.container, "dblclick",this.bind(this.dblclick)); + OZ.Event.add(this.dom.container, "click",this.click.bind(this)); + OZ.Event.add(this.dom.container, "dblclick",this.dblclick.bind(this)); } SQL.Row.prototype.select = function() { @@ -71,7 +69,7 @@ SQL.Row.prototype.setTitle = function(t) { } SQL.Row.prototype.click = function(e) { /* clicked on row */ - this.dispatch("rowclick", this); + SQL.publish("rowclick", this); this.owner.owner.rowManager.select(this); } diff --git a/js/rowmanager.js b/js/rowmanager.js index a0257f6..baa643d 100644 --- a/js/rowmanager.js +++ b/js/rowmanager.js @@ -1,8 +1,5 @@ /* --------------------- row manager ------------ */ - -SQL.RowManager = OZ.Class(); - -SQL.RowManager.prototype.init = function(owner) { +SQL.RowManager = function(owner) { this.owner = owner; this.dom = {}; this.selected = null; @@ -19,16 +16,17 @@ SQL.RowManager.prototype.init = function(owner) { this.select(false); - OZ.Event.add(this.dom.editrow, "click", this.bind(this.edit)); - OZ.Event.add(this.dom.uprow, "click", this.bind(this.up)); - OZ.Event.add(this.dom.downrow, "click", this.bind(this.down)); - OZ.Event.add(this.dom.removerow, "click", this.bind(this.remove)); - OZ.Event.add(this.dom.foreigncreate, "click", this.bind(this.foreigncreate)); - OZ.Event.add(this.dom.foreignconnect, "click", this.bind(this.foreignconnect)); - OZ.Event.add(this.dom.foreigndisconnect, "click", this.bind(this.foreigndisconnect)); - OZ.Event.add(false, "tableclick", this.bind(this.tableClick)); - OZ.Event.add(false, "rowclick", this.bind(this.rowClick)); - OZ.Event.add(document, "keydown", this.bind(this.press)); + OZ.Event.add(this.dom.editrow, "click", this.edit.bind(this)); + OZ.Event.add(this.dom.uprow, "click", this.up.bind(this)); + OZ.Event.add(this.dom.downrow, "click", this.down.bind(this)); + OZ.Event.add(this.dom.removerow, "click", this.remove.bind(this)); + 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)); + + SQL.subscribe("tableclick", this.tableClick.bind(this)); + SQL.subscribe("rowclick", this.rowClick.bind(this)); } SQL.RowManager.prototype.select = function(row) { /* activate a row */ diff --git a/js/rubberband.js b/js/rubberband.js index 1c2d0c2..9a08771 100644 --- a/js/rubberband.js +++ b/js/rubberband.js @@ -1,13 +1,12 @@ /* --------------------- rubberband -------------------- */ -SQL.Rubberband = OZ.Class().extend(SQL.Visual); - -SQL.Rubberband.prototype.init = function(owner) { +SQL.Rubberband = function(owner) { this.owner = owner; - SQL.Visual.prototype.init.apply(this); + SQL.Visual.apply(this); this.dom.container = OZ.$("rubberband"); - OZ.Event.add("area", "mousedown", this.bind(this.down)); + OZ.Event.add("area", "mousedown", this.down.bind(this)); } +SQL.Rubberband.prototype = Object.create(SQL.Visual.prototype); SQL.Rubberband.prototype.down = function(e) { OZ.Event.prevent(e); @@ -17,8 +16,8 @@ SQL.Rubberband.prototype.down = function(e) { this.width = 0; this.height = 0; this.redraw(); - this.documentMove = OZ.Event.add(document, "mousemove", this.bind(this.move)); - this.documentUp = OZ.Event.add(document, "mouseup", this.bind(this.up)); + this.documentMove = OZ.Event.add(document, "mousemove", this.move.bind(this)); + this.documentUp = OZ.Event.add(document, "mouseup", this.up.bind(this)); } SQL.Rubberband.prototype.move = function(e) { diff --git a/js/table.js b/js/table.js index b9c53a0..b72ce11 100644 --- a/js/table.js +++ b/js/table.js @@ -1,8 +1,5 @@ /* --------------------- db table ------------ */ - -SQL.Table = OZ.Class().extend(SQL.Visual); - -SQL.Table.prototype.init = function(owner, name, x, y, z) { +SQL.Table = function(owner, name, x, y, z) { this.owner = owner; this.rows = []; this.keys = []; @@ -11,7 +8,7 @@ SQL.Table.prototype.init = function(owner, name, x, y, z) { this.flag = false; this.selected = false; - SQL.Visual.prototype.init.apply(this); + SQL.Visual.apply(this); this.data.comment = ""; this.setTitle(name); @@ -20,6 +17,7 @@ SQL.Table.prototype.init = function(owner, name, x, y, z) { this.setZ(z); this.snap(); } +SQL.Table.prototype = Object.create(SQL.Visual.prototype); SQL.Table.prototype._build = function() { this.dom.container = OZ.DOM.elm("div", {className:"table"}); @@ -38,10 +36,10 @@ SQL.Table.prototype._build = function() { this.dom.mini = OZ.DOM.elm("div", {className:"mini"}); this.owner.map.dom.container.appendChild(this.dom.mini); - this._ec.push(OZ.Event.add(this.dom.container, "click", this.bind(this.click))); - this._ec.push(OZ.Event.add(this.dom.container, "dblclick", this.bind(this.dblclick))); - this._ec.push(OZ.Event.add(this.dom.container, "mousedown", this.bind(this.down))); - this._ec.push(OZ.Event.add(this.dom.container, "touchstart", this.bind(this.down))); + this._ec.push(OZ.Event.add(this.dom.container, "click", this.click.bind(this))); + this._ec.push(OZ.Event.add(this.dom.container, "dblclick", this.dblclick.bind(this))); + this._ec.push(OZ.Event.add(this.dom.container, "mousedown", this.down.bind(this))); + this._ec.push(OZ.Event.add(this.dom.container, "touchstart", this.down.bind(this))); this._ec.push(OZ.Event.add(this.dom.container, "touchmove", OZ.Event.prevent)); } @@ -88,7 +86,7 @@ SQL.Table.prototype.click = function(e) { if (t != this.dom.title) { return; } /* click on row */ - this.dispatch("tableclick",this); + SQL.publish("tableclick", this); this.owner.rowManager.select(false); } @@ -231,8 +229,8 @@ SQL.Table.prototype.down = function(e) { /* mousedown - start drag */ } } - this.documentMove = OZ.Event.add(document, moveEvent, this.bind(this.move)); - this.documentUp = OZ.Event.add(document, upEvent, this.bind(this.up)); + this.documentMove = OZ.Event.add(document, moveEvent, this.move.bind(this)); + this.documentUp = OZ.Event.add(document, upEvent, this.up.bind(this)); } SQL.Table.prototype.toXML = function() { diff --git a/js/tablemanager.js b/js/tablemanager.js index 29f1b6a..4c340bc 100644 --- a/js/tablemanager.js +++ b/js/tablemanager.js @@ -1,8 +1,6 @@ /* --------------------- table manager ------------ */ -SQL.TableManager = OZ.Class(); - -SQL.TableManager.prototype.init = function(owner) { +SQL.TableManager = function(owner) { this.owner = owner; this.dom = { container:OZ.$("table"), @@ -30,17 +28,17 @@ SQL.TableManager.prototype.init = function(owner) { this.select(false); - this.save = this.bind(this.save); + this.save = this.save.bind(this); - OZ.Event.add("area", "click", this.bind(this.click)); - OZ.Event.add(this.dom.addtable, "click", this.bind(this.preAdd)); - OZ.Event.add(this.dom.removetable, "click", this.bind(this.remove)); - OZ.Event.add(this.dom.cleartables, "click", this.bind(this.clear)); - OZ.Event.add(this.dom.addrow, "click", this.bind(this.addRow)); - OZ.Event.add(this.dom.aligntables, "click", this.owner.bind(this.owner.alignTables)); - OZ.Event.add(this.dom.edittable, "click", this.bind(this.edit)); - OZ.Event.add(this.dom.tablekeys, "click", this.bind(this.keys)); - OZ.Event.add(document, "keydown", this.bind(this.press)); + OZ.Event.add("area", "click", this.click.bind(this)); + OZ.Event.add(this.dom.addtable, "click", this.preAdd.bind(this)); + OZ.Event.add(this.dom.removetable, "click", this.remove.bind(this)); + OZ.Event.add(this.dom.cleartables, "click", this.clear.bind(this)); + OZ.Event.add(this.dom.addrow, "click", this.addRow.bind(this)); + 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)); this.dom.container.parentNode.removeChild(this.dom.container); } diff --git a/js/visual.js b/js/visual.js index 4fb7045..5b84e4f 100644 --- a/js/visual.js +++ b/js/visual.js @@ -1,7 +1,5 @@ /* -------------------- base visual element -------------------- */ - -SQL.Visual = OZ.Class(); /* abstract parent */ -SQL.Visual.prototype.init = function() { +SQL.Visual = function() { this._init(); this._build(); } diff --git a/js/window.js b/js/window.js index fd25bd6..c3cceae 100644 --- a/js/window.js +++ b/js/window.js @@ -1,8 +1,6 @@ /* --------------------- window ------------ */ -SQL.Window = OZ.Class(); - -SQL.Window.prototype.init = function(owner) { +SQL.Window = function(owner) { this.owner = owner; this.dom = { container:OZ.$("window"), @@ -16,11 +14,11 @@ SQL.Window.prototype.init = function(owner) { this.dom.ok.value = _("windowok"); this.dom.cancel.value = _("windowcancel"); this.dom.throbber.alt = this.dom.throbber.title = _("throbber"); - OZ.Event.add(this.dom.ok, "click", this.bind(this.ok)); - OZ.Event.add(this.dom.cancel, "click", this.bind(this.close)); - OZ.Event.add(document, "keydown", this.bind(this.key)); + 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)); - this.sync = this.bind(this.sync); + this.sync = this.sync.bind(this); OZ.Event.add(window, "scroll", this.sync); OZ.Event.add(window, "resize", this.sync); diff --git a/js/wwwsqldesigner.js b/js/wwwsqldesigner.js index d1bd0a4..c860d79 100755 --- a/js/wwwsqldesigner.js +++ b/js/wwwsqldesigner.js @@ -1,15 +1,11 @@ -/* --------------------- www sql designer ------------ */ - -SQL.Designer = OZ.Class().extend(SQL.Visual); - -SQL.Designer.prototype.init = function() { +SQL.Designer = function() { SQL.Designer = this; this.tables = []; this.relations = []; this.title = document.title; - SQL.Visual.prototype.init.apply(this); + SQL.Visual.apply(this); new SQL.Toggle(OZ.$("toggle")); this.dom.container = OZ.$("area"); @@ -34,6 +30,7 @@ SQL.Designer.prototype.init = function() { this.requestLanguage(); this.requestDB(); } +SQL.Designer.prototype = Object.create(SQL.Visual.prototype); /* update area size */ SQL.Designer.prototype.sync = function() { @@ -59,7 +56,7 @@ SQL.Designer.prototype.requestLanguage = function() { /* get locale file */ var lang = this.getOption("locale") var bp = this.getOption("staticpath"); var url = bp + "locale/"+lang+".xml"; - OZ.Request(url, this.bind(this.languageResponse), {method:"get", xml:true}); + OZ.Request(url, this.languageResponse.bind(this), {method:"get", xml:true}); } SQL.Designer.prototype.languageResponse = function(xmlDoc) { @@ -79,7 +76,7 @@ SQL.Designer.prototype.requestDB = function() { /* get datatypes file */ var db = this.getOption("db"); var bp = this.getOption("staticpath"); var url = bp + "db/"+db+"/datatypes.xml"; - OZ.Request(url, this.bind(this.dbResponse), {method:"get", xml:true}); + OZ.Request(url, this.dbResponse.bind(this), {method:"get", xml:true}); } SQL.Designer.prototype.dbResponse = function(xmlDoc) { @@ -348,7 +345,3 @@ SQL.Designer.prototype.getFKTypeFor = function(typeIndex) { } return this.fkTypeFor[typeIndex]; } - -window.onbeforeunload = function(e) { - return ""; /* some browsers will show this text, some won't. */ -} From dcea4c24e5efcb66cefb18eec8e7c9b596000867 Mon Sep 17 00:00:00 2001 From: Ondrej Zara Date: Mon, 15 Jun 2015 08:25:02 +0200 Subject: [PATCH 4/4] added missing toggle.js --- js/toggle.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 js/toggle.js diff --git a/js/toggle.js b/js/toggle.js new file mode 100644 index 0000000..6b1ec05 --- /dev/null +++ b/js/toggle.js @@ -0,0 +1,26 @@ +/* ------------------ minimize/restore bar ----------- */ + +SQL.Toggle = function(elm) { + this._state = null; + this._elm = elm; + OZ.Event.add(elm, "click", this._click.bind(this)); + + var defaultState = true; + if (document.location.href.match(/toolbar=hidden/)) { defaultState = false; } + this._switch(defaultState); +} + +SQL.Toggle.prototype._click = function(e) { + this._switch(!this._state); +} + +SQL.Toggle.prototype._switch = function(state) { + this._state = state; + if (this._state) { + OZ.$("bar").style.height = ""; + } else { + OZ.$("bar").style.overflow = "hidden"; + OZ.$("bar").style.height = this._elm.offsetHeight + "px"; + } + this._elm.className = (this._state ? "on" : "off"); +}