var fieldSettings = {}; var fields = []; var rows = []; var modal; var allTypeOptions = {}; function query(callback) { $.post(createLink('dataset', 'ajaxQuery'), {sql: $('#sql').val()}, function(resp) { resp = JSON.parse(resp); if(resp.result !== 'success') { $('.error').removeClass('hidden'); $('.error td').html(resp.message); } else { fields = resp.fields; rows = resp.rows; vars = resp.vars; drawTable(resp.fields, resp.rows); if(typeof callback !== 'undefined') callback(); } }); } $(document).ready(function() { $('#sql').on('input', function() { $('.error').addClass('hidden'); }) $('.query').click(function() { query(); }) $('.fieldSettings').click(function() { query(function() { modal = new $.zui.ModalTrigger({title: lang.fieldSettings, custom: function(el) { var html = '
'; html += ''; html += ''; for(let index in fields) { var field = fields[index]; var typeOptions = genTypeOptions(index); var name = typeof fieldSettings[field] == 'undefined' ? '' : fieldSettings[field].name; html += ''; } html += ''; html += ''; html += '
' + lang.field + '' + lang.fieldName + '' + lang.fieldType + '
' + field + '' + typeOptions + '
'; html += '
'; return html; }}); modal.show({onShow: function() { setTimeout(function() { $('.chosen').chosen(); $('select[name^="object"]').change(function(e) { var object = $(this).val(); var objectid = $(this).attr('id'); var $that = $(this); if(!object) { return false; } $.get(createLink('dataset', 'ajaxGetTypeOptions', 'object=' + object), function(res) { var index = objectid.substring(6); var id = 'type' + index; var res = JSON.parse(res); var options = ''; for(let key in res.options) { options += ''; } allTypeOptions[object] = res.options; var html = ''; $that.closest('.td-row').find('.typebox').html(html); $('.types').chosen(); }); }); }, 20) return false; }}) modal.show(); }) }) $('.save').click(function() { query(function() { if(JSON.stringify(fieldSettings) == '{}') { for(let index in fields) { fieldSettings[fields[index]] = { name: '', object: '', field: '', type: 'string', }; } } $.post(createLink('dataset', 'create'), {name: $('#name').val(), sql: $('#sql').val(), fields: JSON.stringify(fieldSettings), objects: JSON.stringify(allTypeOptions)}, function(res) { res = JSON.parse(res); if(res.result == 'fail') { $('.error').removeClass('hidden'); $('.error td').html(res.message.name); return; } window.location.href = createLink('dataset', 'browse', 'type=custom'); }) }) }) }); function saveSettings() { for(let index in fields) { var object = $('#object' + index).val(); var type = object ? allTypeOptions[object][$('#type' + index).val()].type : 'string'; fieldSettings[fields[index]] = { name: $('#name' + index).val(), object: object, field: $('#type' + index).val(), type: type, } } modal.close(); this.drawTable(fields, rows); return false; } function genTypeOptions(index) { var object = typeof fieldSettings[fields[index]] == 'undefined' ? '' : fieldSettings[fields[index]].object; var field = typeof fieldSettings[fields[index]] == 'undefined' ? '' : fieldSettings[fields[index]].field; var objectOptions = ''; for(let value in lang.objects) { objectOptions += ''; } var typeOptions = ''; for(let value in allTypeOptions[object]) { typeOptions += ''; } return '
'; } function drawTable(fields, rows) { var head = ''; for(let field of fields) { head += '' + (typeof fieldSettings[field] !== 'undefined' && fieldSettings[field].name ? fieldSettings[field].name : field) + ''; } head += ''; var html = '' + head + ''; var body = ''; for(let row of rows) { body += ''; for(let index in row) body += '' + row[index] + ''; body += ''; } html += '' + body + ''; $('table.result').html(html); }