_form.html.erb 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <% if @query.errors.any? %>
  2. <div class="alert alert-danger"><%= @query.errors.full_messages.first %></div>
  3. <% end %>
  4. <%= form_for @query, url: (@query.persisted? ? query_path(@query, variable_params) : queries_path(variable_params)), html: {class: "the_form", autocomplete: "off"} do |f| %>
  5. <div class="row">
  6. <div class="col-xs-8">
  7. <div class= "form-group">
  8. <%= f.hidden_field :statement %>
  9. <div id="editor-container">
  10. <div id="editor"><%= @query.statement %></div>
  11. </div>
  12. </div>
  13. <div class="form-group text-right">
  14. <div class="pull-left" style="margin-top: 6px;">
  15. <%= link_to "Back", :back %>
  16. </div>
  17. <%= f.select :data_source, Blazer.data_sources.values.map { |ds| [ds.name, ds.id] }, {}, class: ("hide" if Blazer.data_sources.size == 1), style: "width: 140px;" %>
  18. <div id="tables" style="display: inline-block; width: 260px; margin-right: 10px;" class="hide">
  19. <%= render partial: "tables" %>
  20. </div>
  21. <script>
  22. function updatePreviewSelect() {
  23. $("#tables").load("<%= tables_queries_path %>?" + $.param({data_source: $("#query_data_source").val()}));
  24. }
  25. updatePreviewSelect();
  26. $("#query_data_source").selectize().change(updatePreviewSelect);
  27. </script>
  28. <%= link_to "Run", "#", class: "btn btn-info", id: "run", style: "vertical-align: top;" %>
  29. </div>
  30. </div>
  31. <div class="col-xs-4">
  32. <div class="form-group">
  33. <%= f.label :name %>
  34. <%= f.text_field :name, class: "form-control" %>
  35. </div>
  36. <div class="form-group">
  37. <%= f.label :description %>
  38. <%= f.text_area :description, placeholder: "Optional", style: "height: 80px;", class: "form-control" %>
  39. </div>
  40. <div class="text-right">
  41. <% if @query.persisted? %>
  42. <%= link_to "Delete", query_path(@query), method: :delete, "data-confirm" => "Are you sure?", class: "btn btn-danger" %>
  43. <%= f.submit "Fork", class: "btn btn-info" %>
  44. <% end %>
  45. <%= f.submit @query.persisted? ? "Update" : "Create", class: "btn btn-success" %>
  46. </div>
  47. <% if @query.persisted? %>
  48. <% dashboards_count = @query.dashboards.count %>
  49. <% checks_count = @query.checks.count %>
  50. <% words = [] %>
  51. <% words << pluralize(dashboards_count, "dashboard") if dashboards_count > 0 %>
  52. <% words << pluralize(checks_count, "check") if checks_count > 0 %>
  53. <% if words.any? %>
  54. <div class="alert alert-info" style="margin-top: 10px; padding: 8px 12px;">
  55. Part of <%= words.to_sentence %>. Be careful when editing.
  56. </div>
  57. <% end %>
  58. <% end %>
  59. </div>
  60. </div>
  61. <% end %>
  62. <div id="results"></div>
  63. <script>
  64. var editor = ace.edit("editor");
  65. editor.setTheme("ace/theme/twilight");
  66. editor.getSession().setMode("ace/mode/sql");
  67. editor.setOptions({
  68. enableBasicAutocompletion: false,
  69. enableSnippets: false,
  70. enableLiveAutocompletion: false,
  71. highlightActiveLine: false,
  72. fontSize: 12,
  73. minLines: 10
  74. });
  75. editor.renderer.setShowGutter(true);
  76. editor.renderer.setPrintMarginColumn(false);
  77. editor.renderer.setPadding(10);
  78. editor.getSession().setUseWrapMode(true);
  79. editor.commands.addCommand({
  80. name: 'run',
  81. bindKey: {win: 'Ctrl-Enter', mac: 'Command-Enter'},
  82. exec: function(editor) {
  83. $("#run").click();
  84. },
  85. readOnly: false // false if this command should not apply in readOnly mode
  86. });
  87. // http://stackoverflow.com/questions/11584061/
  88. function adjustHeight() {
  89. var lines = editor.getSession().getScreenLength();
  90. if (lines < 9) {
  91. lines = 9;
  92. }
  93. var newHeight = (lines + 1) * 16;
  94. $("#editor").height(newHeight.toString() + "px");
  95. editor.resize();
  96. };
  97. function getSQL() {
  98. var selectedText = editor.getSelectedText();
  99. var text = selectedText.length == 0 ? editor.getValue() : selectedText;
  100. return text.replace(/\n/g, "\r\n");
  101. }
  102. function getErrorLine() {
  103. var error_line = /LINE (\d+)/g.exec($("#results").find('.alert-danger').text());
  104. if (error_line) {
  105. error_line = parseInt(error_line[1], 10);
  106. if (editor.getSelectedText().length > 0) {
  107. error_line += editor.getSelectionRange().start.row;
  108. }
  109. return error_line;
  110. }
  111. }
  112. editor.getSession().on("change", adjustHeight);
  113. adjustHeight();
  114. $("#editor").show();
  115. editor.focus();
  116. var error_line = null;
  117. var xhr;
  118. var params = <%= raw blazer_json_escape(variable_params.to_json) %>;
  119. $("#run").click(function (e) {
  120. e.preventDefault();
  121. if (error_line) {
  122. editor.getSession().removeGutterDecoration(error_line - 1, "error");
  123. error_line = null;
  124. }
  125. $("#results").html('<p class="text-muted">Loading...</p>');
  126. if (xhr) {
  127. xhr.abort();
  128. }
  129. var data = $.extend({}, params, {statement: getSQL(), data_source: $("#query_data_source").val()});
  130. xhr = runQuery(data, function (data) {
  131. $("#results").html(data);
  132. error_line = getErrorLine();
  133. if (error_line) {
  134. editor.getSession().addGutterDecoration(error_line - 1, "error");
  135. editor.scrollToLine(error_line, true, true, function () {});
  136. editor.gotoLine(error_line, 0, true);
  137. editor.focus();
  138. }
  139. });
  140. });
  141. if ($("#query_statement").val() != "") {
  142. $("#run").click();
  143. }
  144. $(document).on("change", "#table_names", function () {
  145. var val = $(this).val();
  146. if (val.length > 0) {
  147. editor.setValue("SELECT * FROM " + val + " LIMIT 10");
  148. $("#run").click();
  149. }
  150. });
  151. $("form.the_form").on("submit", function() {
  152. $("#query_statement").val(editor.getValue());
  153. return true;
  154. });
  155. preventBackspaceNav();
  156. </script>