Business intelligence made simple
Use Vue.js for query editor
Andrew Kaneb3d67ec1e49d
diff --git a/app/assets/javascripts/blazer/application.js b/app/assets/javascripts/blazer/application.jsindex 363b54e5..e4963437 100644--- a/app/assets/javascripts/blazer/application.js+++ b/app/assets/javascripts/blazer/application.js@@ -108,65 +108,6 @@ function getErrorLine() {var error_line = null-$(document).on("click", "#cancel", function (e) {- e.preventDefault()-- cancelAllQueries()-- queryDone()-- $("#results").html("")-})--function queryDone() {- $("#run").removeClass("hide")- $("#cancel").addClass("hide")-}--$(document).on("click", "#run", function (e) {- e.preventDefault()-- $(this).addClass("hide")- $("#cancel").removeClass("hide")-- if (error_line) {- editor.getSession().removeGutterDecoration(error_line - 1, "error")- error_line = null- }-- $("#results").html('<p class="text-muted">Loading...</p>')-- var data = $.extend({}, params, {statement: getSQL(), data_source: $("#query_data_source").val()})-- cancelAllQueries()-- runQuery(data, function (data) {- queryDone()-- $("#results").html(data)-- error_line = getErrorLine()- if (error_line) {- editor.getSession().addGutterDecoration(error_line - 1, "error")- editor.scrollToLine(error_line, true, true, function () {})- editor.gotoLine(error_line, 0, true)- editor.focus()- }- }, function (data) {- // TODO show error- queryDone()- })-})--$(document).on("change", "#table_names", function () {- var val = $(this).val()- if (val.length > 0) {- var dataSource = $("#query_data_source").val()- editor.setValue(previewStatement[dataSource].replace("{table}", val), 1)- $("#run").click()- }-})-function showEditor() {editor = ace.edit("editor")editor.setTheme("ace/theme/twilight")@@ -205,8 +146,3 @@ function showEditor() {preventBackspaceNav()-function updatePreviewSelect() {- var dataSource = $("#query_data_source").val()- $("#tables").load(Routes.blazer_tables_queries_path({data_source: dataSource}))- $("#view-schema").attr("href", Routes.blazer_schema_queries_path({data_source: dataSource}))-}diff --git a/app/controllers/blazer/queries_controller.rb b/app/controllers/blazer/queries_controller.rbindex c977c881..682e240e 100644--- a/app/controllers/blazer/queries_controller.rb+++ b/app/controllers/blazer/queries_controller.rb@@ -176,8 +176,7 @@ module Blazerenddef tables- @tables = Blazer.data_sources[params[:data_source]].tables- render partial: "tables", layout: false+ render json: Blazer.data_sources[params[:data_source]].tablesenddef schemadiff --git a/app/views/blazer/queries/_form.html.erb b/app/views/blazer/queries/_form.html.erbindex e702ce35..d3f6abd1 100644--- a/app/views/blazer/queries/_form.html.erb+++ b/app/views/blazer/queries/_form.html.erb@@ -2,69 +2,154 @@<div class="alert alert-danger"><%= @query.errors.full_messages.first %></div><% end %>-<%= form_for @query, url: (@query.persisted? ? query_path(@query, variable_params) : queries_path(variable_params)), html: {class: "the_form", 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"><%= @query.statement %></div>+<div id="app" v-cloak>+ <%= form_for @query, url: (@query.persisted? ? query_path(@query, variable_params) : queries_path(variable_params)), html: {class: "the_form", 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"><%= @query.statement %></div>+ </div>+ </div>+ <div class="form-group text-right">+ <div class="pull-left" style="margin-top: 9px;">+ <%= link_to "Back", :back %>+ </div>+ <a :href="dataSourcePath" target="_blank" style="margin-right: 10px;">Schema</a>+ <%= f.select :data_source, Blazer.data_sources.values.select { |ds| q = @query.dup; q.data_source = ds.id; q.editable?(blazer_user) }.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></div></div>- <div class="form-group text-right">- <div class="pull-left" style="margin-top: 9px;">- <%= link_to "Back", :back %>+ <div class="col-xs-4">+ <div class="form-group">+ <%= f.label :name %>+ <%= f.text_field :name, class: "form-control" %></div>- <%= link_to "Schema", "#", target: "_blank", id: "view-schema", style: "margin-right: 10px;" %>- <%= f.select :data_source, Blazer.data_sources.values.select { |ds| q = @query.dup; q.data_source = ds.id; q.editable?(blazer_user) }.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;" class="hide">- <%= render partial: "tables" %>+ <div class="form-group">+ <%= f.label :description %>+ <%= f.text_area :description, placeholder: "Optional", style: "height: 80px;", class: "form-control" %>+ </div>+ <div class="text-right">+ <%= f.submit "For Enter Press", class: "hide" %>+ <% if @query.persisted? %>+ <%= link_to "Delete", query_path(@query), method: :delete, "data-confirm" => "Are you sure?", class: "btn btn-danger" %>+ <%= f.submit "Fork", class: "btn btn-info" %>+ <% end %>+ <%= f.submit @query.persisted? ? "Update" : "Create", class: "btn btn-success" %></div>- <script>- updatePreviewSelect();- $("#query_data_source").selectize().change(updatePreviewSelect);- </script>- <%= link_to "Run", "#", class: "btn btn-info", id: "run", style: "vertical-align: top; width: 70px;" %>- <%= link_to "Cancel", "#", class: "btn btn-danger hide", id: "cancel", style: "vertical-align: top; width: 70px;" %>- </div>- </div>- <div class="col-xs-4">- <div class="form-group">- <%= f.label :name %>- <%= f.text_field :name, class: "form-control" %>- </div>- <div class="form-group">- <%= f.label :description %>- <%= f.text_area :description, placeholder: "Optional", style: "height: 80px;", class: "form-control" %>- </div>- <div class="text-right">- <%= f.submit "For Enter Press", class: "hide" %><% if @query.persisted? %>- <%= link_to "Delete", query_path(@query), method: :delete, "data-confirm" => "Are you sure?", class: "btn btn-danger" %>- <%= f.submit "Fork", class: "btn btn-info" %>+ <% dashboards_count = @query.dashboards.count %>+ <% checks_count = @query.checks.count %>+ <% words = [] %>+ <% words << pluralize(dashboards_count, "dashboard") if dashboards_count > 0 %>+ <% words << pluralize(checks_count, "check") if checks_count > 0 %>+ <% if words.any? %>+ <div class="alert alert-info" style="margin-top: 10px; padding: 8px 12px;">+ Part of <%= words.to_sentence %>. Be careful when editing.+ </div>+ <% end %><% end %>- <%= f.submit @query.persisted? ? "Update" : "Create", class: "btn btn-success" %></div>- <% if @query.persisted? %>- <% dashboards_count = @query.dashboards.count %>- <% checks_count = @query.checks.count %>- <% words = [] %>- <% words << pluralize(dashboards_count, "dashboard") if dashboards_count > 0 %>- <% words << pluralize(checks_count, "check") if checks_count > 0 %>- <% if words.any? %>- <div class="alert alert-info" style="margin-top: 10px; padding: 8px 12px;">- Part of <%= words.to_sentence %>. Be careful when editing.- </div>- <% end %>- <% end %></div>- </div>-<% end %>+ <% end %>-<div id="results"></div>+ <div id="results">+ <p class="text-muted" v-if="running">Loading...</p>+ <div v-html="results" v-if="!running"></div>+ </div>+</div><script>var params = <%= raw blazer_json_escape(variable_params.to_json) %>;var previewStatement = <%= raw blazer_json_escape(Hash[Blazer.data_sources.map { |k, v| [k, v.preview_statement] }].to_json) %>;- showEditor();++ var app = new Vue({+ el: "#app",+ data: {+ running: false,+ results: "",+ dataSource: "",+ selectize: null+ },+ computed: {+ dataSourcePath: function() {+ return Routes.blazer_schema_queries_path({data_source: this.dataSource})+ }+ },+ methods: {+ run: function(e) {+ this.running = true+ this.results = ""+ cancelAllQueries()++ var data = $.extend({}, params, {statement: getSQL(), data_source: $("#query_data_source").val()})++ var _this = this++ runQuery(data, function (data) {+ _this.running = false+ _this.results = data++ error_line = getErrorLine()+ if (error_line) {+ editor.getSession().addGutterDecoration(error_line - 1, "error")+ editor.scrollToLine(error_line, true, true, function () {})+ editor.gotoLine(error_line, 0, true)+ editor.focus()+ }+ }, function (data) {+ _this.running = false+ _this.results = data+ })+ },+ cancel: function(e) {+ this.running = false+ cancelAllQueries()+ },+ updateDataSource: function(dataSource) {+ this.dataSource = dataSource+ _this = this++ $.getJSON(Routes.blazer_tables_queries_path({data_source: this.dataSource}), function(data) {+ var newOptions = []+ for (var i = 0; i < data.length; i++) {+ newOptions.push({text: data[i], value: data[i]})+ }+ var selectize = _this.selectize+ selectize.clearOptions()+ selectize.addOption(newOptions)+ selectize.refreshOptions(false)+ })+ }+ },+ mounted: function() {+ _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()+ })++ showEditor()+ }+ })</script>diff --git a/app/views/blazer/queries/_tables.html.erb b/app/views/blazer/queries/_tables.html.erbdeleted file mode 100644index c1912a04..00000000--- a/app/views/blazer/queries/_tables.html.erb+++ /dev/null@@ -1,5 +0,0 @@-<%= select_tag :table_names, options_for_select([["Preview table", nil]] + (@tables || [])), style: "width: 240px;" %>--<script>- $("#table_names").selectize({}).parent().removeClass("hide");-</script>