Skip to content

Commit

Permalink
Add preview to the json generator
Browse files Browse the repository at this point in the history
  • Loading branch information
x4base committed May 5, 2016
1 parent 95a2c1e commit a9d71ac
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
23 changes: 19 additions & 4 deletions caravel/assets/javascripts/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ var jsonGenerator = require("./modules/json_generator");

$(document).ready(function () {
var $modal = $('#json_gen_modal');
$('#convertButton').click(function () {

var getJsonOrWarn = function () {
var expression = $('#expression').val().trim();
var $alert = $modal.find('#errorMsg');
$alert
Expand All @@ -12,15 +13,29 @@ $(document).ready(function () {
.html("");
try {
var parse_tree = jsonGenerator.parse(expression);
$('#json').val(JSON.stringify(parse_tree));

$(this).closest('.modal').modal('hide');
return JSON.stringify(parse_tree);
} catch (e) {
$alert
.show()
.html(e.message);
}
return null;
};

$('#convertButton').click(function () {
var json = getJsonOrWarn();
if (json) {
$('#json').val(json);
$(this).closest('.modal').modal('hide');
}
});

$('#previewButton').click(function () {
var $preview = $modal.find('#preview');
var json = getJsonOrWarn();
$preview.html(json || "");
});

$modal.on('shown.bs.modal', function () {
$('#expression').focus();
});
Expand Down
3 changes: 3 additions & 0 deletions caravel/assets/javascripts/modules/json_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ var convertArithmetic = function (node) {

var parse = function (expression) {
expression = expression.trim().replace('\n', '');
if (!expression) {
throw new Error("Please enter the expression");
}
var parse_tree = jsep(expression);
var converterMap = {
CallExpression: convertFuncCall,
Expand Down
5 changes: 5 additions & 0 deletions caravel/templates/caravel/models/datasource/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ <h4 class="modal-title" id="myModalLabel">JSON Generator</h4>
</div>
<div class="modal-body">
<textarea id="expression" rows="5" cols="60"></textarea>
<p><strong>Preview:</strong></p>
<div class="" id="preview">(Please click the preview button)</div>
<div class="alert alert-danger hidden" id="errorMsg"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">
Close
</button>
<button type="button" class="btn btn-default" id="previewButton">
Preview
</button>
<button type="button" class="btn btn-default" id="convertButton">
Convert to Json
</button>
Expand Down
3 changes: 2 additions & 1 deletion caravel/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ class DruidMetricInlineView(CompactCRUDMixin, CaravelModelView): # noqa
"(http://druid.io/docs/latest/querying/post-aggregations.html)",
True),
'json': utils.markdown(
"You can also use the [generator](javascript:showJsonGenModal()) to generate json",
"You can also use the "
"[generator](javascript:showJsonGenModal()) to generate json",
True),
}
appbuilder.add_view_no_menu(DruidMetricInlineView)
Expand Down

0 comments on commit a9d71ac

Please sign in to comment.