Skip to content

Business intelligence made simple

Removed Vue from query form [skip ci]

Andrew Kanee3453b68c5ab
diff --git a/app/views/blazer/queries/_form.html.erb b/app/views/blazer/queries/_form.html.erb
index 2cfe7cc5..62edbd82 100644
--- a/app/views/blazer/queries/_form.html.erb
+++ b/app/views/blazer/queries/_form.html.erb
@@ -4,29 +4,29 @@
<% @variable_params = @query.persisted? ? variable_params(@query) : nested_variable_params(@query) %>
-<div id="app" v-cloak>
+<div id="app" class="cloak">
<%= form_for @query, url: (@query.persisted? ? query_path(@query, params: @variable_params) : queries_path(params: @variable_params)), html: {autocomplete: "off"} do |f| %>
<div class="row">
<div id="statement-box" class="col-xs-8">
<div class= "form-group">
<%= f.hidden_field :statement %>
<div id="editor-container">
- <div id="editor" :style="{ height: editorHeight }"><%= @query.statement %></div>
+ <div id="editor"><%= @query.statement %></div>
</div>
</div>
<div class="form-group text-right" style="margin-bottom: 8px;">
<div class="pull-left" style="margin-top: 8px;">
<%= link_to "Back", :back %>
- <a :href="docsPath" target="_blank" style="margin-left: 40px;">Docs</a>
- <a :href="schemaPath" target="_blank" style="margin-left: 40px;">Schema</a>
+ <a id="docs-link" target="_blank" style="margin-left: 40px;">Docs</a>
+ <a id="schema-link" target="_blank" style="margin-left: 40px;">Schema</a>
</div>
<%= f.select :data_source, Blazer.data_sources.map { |_, ds| [ds.name, ds.id] }, {}, class: ("hide" if Blazer.data_sources.size <= 1), style: "width: 140px;" %>
<div id="tables" style="display: inline-block; width: 250px; margin-right: 10px;">
<select id="table_names" style="width: 240px;" placeholder="Preview table"></select>
</div>
- <a v-on:click="run" v-if="!running" class="btn btn-info" style="vertical-align: top; width: 70px;">Run</a>
- <a v-on:click="cancel" v-if="running" class="btn btn-danger" style="vertical-align: top; width: 70px;">Cancel</a>
+ <a id="run-button" class="btn btn-info" style="vertical-align: top; width: 70px;">Run</a>
+ <a id="cancel-button" class="btn btn-danger" style="vertical-align: top; width: 70px; display: none;">Cancel</a>
</div>
</div>
<div class="col-xs-4">
@@ -63,8 +63,8 @@
<% end %>
<div id="results">
- <p class="text-muted" v-if="running">Loading...</p>
- <div id="results-html" v-if="!running" :class="{ 'query-error': error }"></div>
+ <p id="loading" class="text-muted" style="display: none;">Loading...</p>
+ <div id="results-html"></div>
</div>
</div>
@@ -72,184 +72,181 @@
<%= blazer_js_var "variableParams", @variable_params %>
<%= blazer_js_var "previewStatement", Blazer.data_sources.to_h { |k, v| [k, (v.preview_statement rescue "")] } %>
- var app = Vue.createApp({
- data: function() {
- return {
- running: false,
- results: "",
- error: false,
- dataSource: "",
- selectize: null,
- editorHeight: "180px"
- }
- },
- computed: {
- schemaPath: function() {
- return Routes.schema_queries_path({data_source: this.dataSource})
- },
- docsPath: function() {
- return Routes.docs_queries_path({data_source: this.dataSource})
+ function cancel(e) {
+ stopRunning()
+ cancelAllQueries()
+ }
+
+ var results = ""
+
+ function getErrorLine() {
+ var errorLine = results.substring(0, 200).includes("alert-danger") && /LINE (\d+)/g.exec(results)
+
+ if (errorLine) {
+ errorLine = parseInt(errorLine[1], 10)
+ if (editor.getSelectedText().length >= 10) {
+ errorLine += editor.getSelectionRange().start.row
}
- },
- methods: {
- run: function(e) {
- this.running = true
- this.results = ""
- this.error = false
- cancelAllQueries()
-
- var data = {statement: this.getSQL(), data_source: $("#query_data_source").val(), variables: variableParams}
-
- var _this = this
-
- runQuery(data, function (data) {
- _this.running = false
- _this.showResults(data)
-
- errorLine = _this.getErrorLine()
- if (errorLine) {
- editor.getSession().addGutterDecoration(errorLine - 1, "error")
- editor.scrollToLine(errorLine, true, true, function () {})
- editor.gotoLine(errorLine, 0, true)
- editor.focus()
- }
- }, function (data) {
- _this.running = false
- _this.error = true
- _this.showResults(data)
- })
- },
- cancel: function(e) {
- this.running = false
- cancelAllQueries()
- },
- updateDataSource: function(dataSource) {
- this.dataSource = dataSource
- var selectize = this.selectize
- selectize.clearOptions()
+ return errorLine
+ }
+ }
+
+ function getSQL() {
+ var selectedText = editor.getSelectedText()
+ var text = selectedText.length < 10 ? editor.getValue() : selectedText
+ return text.replace(/\n/g, "\r\n")
+ }
+
+ function showResults(data) {
+ results = data
+ $("#results-html").html(data)
+ }
+
+ function startRunning() {
+ $("#run-button").hide()
+ $("#results-html").empty().hide().removeClass("query-error")
+ $("#cancel-button").show()
+ $("#loading").show()
+ }
+
+ function stopRunning() {
+ $("#cancel-button").hide()
+ $("#loading").hide()
+ $("#run-button").show()
+ $("#results-html").show()
+ }
+
+ var errorLine
+
+ function run(e) {
+ results = ""
+ if (errorLine) {
+ editor.getSession().removeGutterDecoration(errorLine - 1, "error")
+ }
+ startRunning()
+ cancelAllQueries()
- if (this.tablesXhr) {
- this.tablesXhr.abort()
- }
+ var data = {statement: getSQL(), data_source: $("#query_data_source").val(), variables: variableParams}
- this.tablesXhr = $.getJSON(Routes.tables_queries_path({data_source: this.dataSource}), function(data) {
- var newOptions = []
- for (var i = 0; i < data.length; i++) {
- var table = data[i]
- if (typeof table === "object") {
- newOptions.push({text: table.table, value: table.value})
- } else {
- newOptions.push({text: table, value: table})
- }
- }
- selectize.clearOptions()
- selectize.addOption(newOptions)
- selectize.refreshOptions(false)
- })
- },
- showEditor: function() {
- var _this = this
-
- editor = ace.edit("editor")
- editor.setTheme("ace/theme/twilight")
- editor.getSession().setMode("ace/mode/sql")
- editor.setOptions({
- enableBasicAutocompletion: false,
- enableSnippets: false,
- enableLiveAutocompletion: false,
- highlightActiveLine: false,
- fontSize: 12,
- minLines: 10
- })
- editor.renderer.setShowGutter(true)
- editor.renderer.setPrintMarginColumn(false)
- editor.renderer.setPadding(10)
- editor.getSession().setUseWrapMode(true)
- editor.commands.addCommand({
- name: "run",
- bindKey: {win: "Ctrl-Enter", mac: "Command-Enter"},
- exec: function(editor) {
- _this.run()
- },
- readOnly: false // false if this command should not apply in readOnly mode
- })
- // fix command+L
- editor.commands.removeCommands(["gotoline", "find"])
-
- this.editor = editor
-
- editor.getSession().on("change", function () {
- $("#query_statement").val(editor.getValue())
- _this.adjustHeight()
- })
- this.adjustHeight()
+ runQuery(data, function (data) {
+ stopRunning()
+ showResults(data)
+
+ errorLine = getErrorLine()
+ if (errorLine) {
+ editor.getSession().addGutterDecoration(errorLine - 1, "error")
+ editor.scrollToLine(errorLine, true, true, function () {})
+ editor.gotoLine(errorLine, 0, true)
editor.focus()
- },
- adjustHeight: function() {
- // https://stackoverflow.com/questions/11584061/
- var editor = this.editor
- var lines = editor.getSession().getScreenLength()
- if (lines < 9) {
- lines = 9
- }
+ }
+ }, function (data) {
+ stopRunning()
+ $("#results-html").addClass("query-error")
+ showResults(data)
+ })
+ }
+
+ $("#run-button").click(run)
+ $("#cancel-button").click(cancel)
+
+ var dataSource
+ var $select = $("#table_names").selectize({})
+ var selectize = $select[0].selectize
+ selectize.on("change", function(val) {
+ editor.setValue(previewStatement[dataSource].replace("{table}", val), 1)
+ run()
+ selectize.clear(true)
+ selectize.blur()
+ })
- this.editorHeight = ((lines + 1) * 16).toString() + "px"
+ var tablesXhr
- Vue.nextTick(function () {
- editor.resize()
- })
- },
- getSQL: function() {
- var selectedText = editor.getSelectedText()
- var text = selectedText.length < 10 ? editor.getValue() : selectedText
- return text.replace(/\n/g, "\r\n")
- },
- getErrorLine: function() {
- var editor = this.editor
- var errorLine = this.results.substring(0, 100).includes("alert-danger") && /LINE (\d+)/g.exec(this.results)
-
- if (errorLine) {
- errorLine = parseInt(errorLine[1], 10)
- if (editor.getSelectedText().length >= 10) {
- errorLine += editor.getSelectionRange().start.row
- }
- return errorLine
- }
- },
- showResults(data) {
- // can't do it the Vue way due to script tags in results
- // this.results = data
+ function updateDataSource(dataSource) {
+ window.dataSource = dataSource
+ document.getElementById("schema-link").setAttribute("href", Routes.schema_queries_path({data_source: dataSource}))
+ document.getElementById("docs-link").setAttribute("href", Routes.docs_queries_path({data_source: dataSource}))
- Vue.nextTick(function () {
- $("#results-html").html(data)
- })
- }
- },
- mounted: function() {
- var _this = this
-
- var $select = $("#table_names").selectize({})
- var selectize = $select[0].selectize
- selectize.on("change", function(val) {
- editor.setValue(previewStatement[_this.dataSource].replace("{table}", val), 1)
- _this.run()
- selectize.clear(true)
- selectize.blur()
- })
- this.selectize = selectize
-
- this.updateDataSource($("#query_data_source").val())
-
- var $dsSelect = $("#query_data_source").selectize({})
- var dsSelectize = $dsSelect[0].selectize
- dsSelectize.on("change", function(val) {
- _this.updateDataSource(val)
- dsSelectize.blur()
- })
-
- this.showEditor()
+ selectize.clearOptions()
+
+ if (tablesXhr) {
+ tablesXhr.abort()
}
+
+ tablesXhr = $.getJSON(Routes.tables_queries_path({data_source: dataSource}), function(data) {
+ var newOptions = []
+ for (var i = 0; i < data.length; i++) {
+ var table = data[i]
+ if (typeof table === "object") {
+ newOptions.push({text: table.table, value: table.value})
+ } else {
+ newOptions.push({text: table, value: table})
+ }
+ }
+ selectize.clearOptions()
+ selectize.addOption(newOptions)
+ selectize.refreshOptions(false)
+ })
+ }
+
+ updateDataSource($("#query_data_source").val())
+
+ var $dsSelect = $("#query_data_source").selectize({})
+ var dsSelectize = $dsSelect[0].selectize
+ dsSelectize.on("change", function(val) {
+ updateDataSource(val)
+ dsSelectize.blur()
})
- app.config.compilerOptions.whitespace = "preserve"
- app.mount("#app")
+
+ function adjustHeight() {
+ // https://stackoverflow.com/questions/11584061/
+ var editor = this.editor
+ var lines = editor.getSession().getScreenLength()
+ if (lines < 9) {
+ lines = 9
+ }
+
+ this.editorHeight = ((lines + 1) * 16).toString() + "px"
+ editor.resize()
+ }
+
+ function showEditor() {
+ editor = ace.edit("editor")
+ editor.setTheme("ace/theme/twilight")
+ editor.getSession().setMode("ace/mode/sql")
+ editor.setOptions({
+ enableBasicAutocompletion: false,
+ enableSnippets: false,
+ enableLiveAutocompletion: false,
+ highlightActiveLine: false,
+ fontSize: 12,
+ minLines: 10
+ })
+ editor.renderer.setShowGutter(true)
+ editor.renderer.setPrintMarginColumn(false)
+ editor.renderer.setPadding(10)
+ editor.getSession().setUseWrapMode(true)
+ editor.commands.addCommand({
+ name: "run",
+ bindKey: {win: "Ctrl-Enter", mac: "Command-Enter"},
+ exec: function(editor) {
+ run()
+ },
+ readOnly: false // false if this command should not apply in readOnly mode
+ })
+ // fix command+L
+ editor.commands.removeCommands(["gotoline", "find"])
+
+ this.editor = editor
+
+ editor.getSession().on("change", function () {
+ $("#query_statement").val(editor.getValue())
+ adjustHeight()
+ })
+ adjustHeight()
+ editor.focus()
+ }
+
+ showEditor()
+
+ $(".cloak").removeClass("cloak")
<% end %>