<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>
<style>
#cqlin-div {
height: 200px;
width: 100%;
resize: vertical;
margin-bottom: 0px;
display: block;
padding-bottom:0px;
}
.parsed-container {
display: flex;
justify-content: space-between;
gap: 10px;
margin-top: 0;
padding-top:0;
}
.parsed-box {
flex: 1;
display: flex;
flex-direction: column;
}
.parsed-box textarea {
width: 100%;
height: 200px;
resize: vertical;
}
.select2-container {
max-width: 100%;
width: auto !important;
min-width: 200px;
}
.select2-container--default .select2-selection--single {
height: 34px !important;
font-size: 14px;
}
.select2-dropdown {
min-width: 100% !important;
max-width: 600px;
}
.select2-search__field {
font-size: 14px !important;
padding: 4px !important;
}
@media (max-width: 768px) {
.select2-container {
max-width: 90%;
}
}
@media (max-width: 480px) {
.select2-container {
max-width: 100%;
}
}
</style>
<script type="module">
import init, { CQL2 } from '../pkg/cql2_wasm.js';
await init();
window.CQL2 = CQL2;
$(document).ready(function(){
console.log("Ready");
console.log("window.cql2", window.CQL2);
function fetchexamples(){
fetch('../examples.json')
.then(response => response.json())
.then(data => {
window.examples = data;
console.log(window.examples);
set_example_options(data);
}
)
.catch(error => console.error('Error fetching examples json:', error));
}
function set_example_options(data){
$.each(data, function(key, value){
$('<option/>')
.val(key)
.text(value.name)
.prop('title', value.description)
.appendTo('#examples');
});
}
function check(from_select=false){
console.log('check', from_select);
if (from_select == false){
$("#examples").val(null).trigger('change');
$("#example-description").text("");
}
let valid = false;
let txt = "Invalid";
let jsn = "Invalid";
try {
let val = $("#cqlin").val();
console.log("cqlin val", val);
let e = new window.CQL2(val);
valid = e.is_valid();
txt = e.to_text();
jsn = e.to_json_pretty();
} catch(error) {
console.log(error);
}
console.log(valid, txt, jsn);
$("#cql2text").val(txt).css({"background-color": valid ? "#90EE90" : "pink"});
$("#cql2json").val(jsn).css({"background-color": valid ? "#90EE90" : "pink"});
}
$("#cqlin").on('input propertychange', function(){check(false);});
function example_change(){
let selectedOption = $('#examples').find(":selected");
let val = selectedOption.val();
if (val){
let textorjson = $("#exampletype").find(":selected");
let exampletype = textorjson.val();
let sel = window.examples[val][exampletype];
let description = selectedOption.attr("title");
if (sel.startsWith("{")) {
let j = JSON.parse(sel);
sel = JSON.stringify(j, null, 2);
}
$("#cqlin").val(sel);
$("#example-description").text("Current example description: " + description); check(true);
}
};
$("#examples").change(example_change);
$("#exampletype").change(example_change);
$('#examples').select2({
placeholder: "Search or select an example...",
allowClear: true,
width: '100%'
});
fetchexamples();
$("#examples").val('example32').trigger('change');
});
</script>
<p id="example-description" style="font-weight: margin-bottom: 5px;"></p>
<select id="exampletype" class="searchable-dropdown" >
<option value='cql2_text'>CQL2 Text</option>
<option value='cql2_json'>CQL2 JSON</option>
</select>
<select id="examples" class="searchable-dropdown" >
<option value=''>-</option>
</select>
<div id="cqlin-div">
<textarea id="cqlin" rows="20" cols="114"></textarea>
</div>
<br/>
<div class="parsed-container">
<div class="parsed-box">
<h3>Parsed CQL2 Text</h3>
<textarea id="cql2text" rows="15" readonly></textarea>
</div>
<div class="parsed-box">
<h3>Parsed CQL2 JSON</h3>
<textarea id="cql2json" rows="15" readonly></textarea>
</div>
</div>