Skip to content

Business intelligence made simple

Cache unsaved SQL into sessionStorage

Fred Liangf69d0223102b
diff --git a/app/assets/javascripts/blazer/application.js b/app/assets/javascripts/blazer/application.js
index 05f9438b..407b5751 100644
--- a/app/assets/javascripts/blazer/application.js
+++ b/app/assets/javascripts/blazer/application.js
@@ -16,6 +16,7 @@
//= require ./routes
//= require ./queries
//= require ./fuzzysearch
+//= require ./draft_store
Vue.config.devtools = false
@@ -76,4 +77,3 @@ function preventBackspaceNav() {
}
preventBackspaceNav()
-
diff --git a/app/assets/javascripts/blazer/draft_store.js b/app/assets/javascripts/blazer/draft_store.js
new file mode 100644
index 00000000..c0c37eef
--- /dev/null
+++ b/app/assets/javascripts/blazer/draft_store.js
@@ -0,0 +1,23 @@
+var DraftStore = {
+ isSessionStorageSupported: function() {
+ return typeof(Storage) !== "undefined"
+ },
+
+ tabID: function() {
+ return sessionStorage.tabID ? sessionStorage.tabID : sessionStorage.tabID = Math.random()
+ },
+
+ saveDraft: function(value) {
+ if (!this.isSessionStorageSupported()) {
+ return
+ }
+ sessionStorage.setItem("blazer-query-" + this.tabID(), value)
+ },
+
+ restoreDraft: function() {
+ if (!this.isSessionStorageSupported()) {
+ return
+ }
+ return sessionStorage.getItem("blazer-query-" + this.tabID())
+ }
+}
diff --git a/app/views/blazer/queries/_form.html.erb b/app/views/blazer/queries/_form.html.erb
index 87885ce7..b658770a 100644
--- a/app/views/blazer/queries/_form.html.erb
+++ b/app/views/blazer/queries/_form.html.erb
@@ -98,6 +98,8 @@
_this.running = false
_this.showResults(data)
+ DraftStore.saveDraft(_this.getSQL())
+
errorLine = _this.getErrorLine()
if (errorLine) {
editor.getSession().addGutterDecoration(errorLine - 1, "error")
@@ -165,6 +167,9 @@
this.editor = editor
+ var draftSQL = DraftStore.restoreDraft()
+ if (draftSQL) { editor.setValue(draftSQL) }
+
editor.getSession().on("change", function () {
$("#query_statement").val(editor.getValue())
_this.adjustHeight()