create.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. var fieldSettings = {};
  2. var fields = [];
  3. var rows = [];
  4. var modal;
  5. var allTypeOptions = {};
  6. function query(callback) {
  7. $.post(createLink('dataset', 'ajaxQuery'), {sql: $('#sql').val()}, function(resp)
  8. {
  9. resp = JSON.parse(resp);
  10. if(resp.result !== 'success')
  11. {
  12. $('.error').removeClass('hidden');
  13. $('.error td').html(resp.message);
  14. }
  15. else
  16. {
  17. fields = resp.fields;
  18. rows = resp.rows;
  19. vars = resp.vars;
  20. drawTable(resp.fields, resp.rows);
  21. if(typeof callback !== 'undefined') callback();
  22. }
  23. });
  24. }
  25. $(document).ready(function()
  26. {
  27. $('#sql').on('input', function() { $('.error').addClass('hidden'); })
  28. $('.query').click(function() {
  29. query();
  30. })
  31. $('.fieldSettings').click(function() {
  32. query(function() {
  33. modal = new $.zui.ModalTrigger({title: lang.fieldSettings, custom: function(el) {
  34. var html = '<form id="edit" onsubmit="javascript:saveSettings();return false;">';
  35. html += '<table class="table table-form">';
  36. html += '<thead><tr><th class="w-100px">' + lang.field + '</th><th class="w-250px">' + lang.fieldName + '</th><th class="w-300px">' + lang.fieldType + '</th></tr></thead><tbody>';
  37. for(let index in fields)
  38. {
  39. var field = fields[index];
  40. var typeOptions = genTypeOptions(index);
  41. var name = typeof fieldSettings[field] == 'undefined' ? '' : fieldSettings[field].name;
  42. html += '<tr><td>' + field + '</td><td><input name="name" id="name' + index + '" class="form-control" value="' + name + '" /></td><td class="td-row">' + typeOptions + '</td></tr>';
  43. }
  44. html += '</tbody>';
  45. html += '<tfoot><tr><td colspan="3" class="text-center"><button type="submit" id="submit" class="btn btn-wide btn-primary">' + lang.save + '</button></td></tr></tfoot>';
  46. html += '</table>';
  47. html += '</form>';
  48. return html;
  49. }});
  50. modal.show({onShow: function()
  51. {
  52. setTimeout(function() {
  53. $('.chosen').chosen();
  54. $('select[name^="object"]').change(function(e)
  55. {
  56. var object = $(this).val();
  57. var objectid = $(this).attr('id');
  58. var $that = $(this);
  59. if(!object)
  60. {
  61. return false;
  62. }
  63. $.get(createLink('dataset', 'ajaxGetTypeOptions', 'object=' + object), function(res)
  64. {
  65. var index = objectid.substring(6);
  66. var id = 'type' + index;
  67. var res = JSON.parse(res);
  68. var options = '';
  69. for(let key in res.options)
  70. {
  71. options += '<option value="' + key + '">' + res.options[key].name + '</option>';
  72. }
  73. allTypeOptions[object] = res.options;
  74. var html = '<select class="form-control chosen" name="type[]" id="' + id + '">'+ options + '</select>';
  75. $that.closest('.td-row').find('.typebox').html(html);
  76. $('.types').chosen();
  77. });
  78. });
  79. }, 20)
  80. return false;
  81. }})
  82. modal.show();
  83. })
  84. })
  85. $('.save').click(function() {
  86. query(function() {
  87. if(JSON.stringify(fieldSettings) == '{}')
  88. {
  89. for(let field in fields)
  90. {
  91. fieldSettings[field] = {
  92. name: fields[field],
  93. object: '',
  94. field: '',
  95. type: 'string',
  96. };
  97. }
  98. }
  99. $.post(createLink('dataset', 'create'), {name: $('#name').val(), sql: $('#sql').val(), fields: JSON.stringify(fieldSettings), objects: JSON.stringify(allTypeOptions)}, function(res) {
  100. res = JSON.parse(res);
  101. if(res.result == 'fail')
  102. {
  103. $('.error').removeClass('hidden');
  104. $('.error td').html(res.message.name);
  105. return;
  106. }
  107. window.location.href = createLink('dataset', 'browse', 'type=custom');
  108. })
  109. })
  110. })
  111. });
  112. function saveSettings()
  113. {
  114. for(let index in fields)
  115. {
  116. var object = $('#object' + index).val();
  117. var type = object ? allTypeOptions[object][$('#type' + index).val()].type : 'string';
  118. fieldSettings[fields[index]] = {
  119. name: $('#name' + index).val(),
  120. object: object,
  121. field: $('#type' + index).val(),
  122. type: type,
  123. }
  124. }
  125. modal.close();
  126. this.drawTable(fields, rows);
  127. return false;
  128. }
  129. function genTypeOptions(index)
  130. {
  131. var object = typeof fieldSettings[fields[index]] == 'undefined' ? '' : fieldSettings[fields[index]].object;
  132. var field = typeof fieldSettings[fields[index]] == 'undefined' ? '' : fieldSettings[fields[index]].field;
  133. var objectOptions = '<option value=""></option>';
  134. for(let value in lang.objects)
  135. {
  136. objectOptions += '<option value="' + value + '" ' + (value === object ? 'selected' : '') + '>' + lang.objects[value] + '</option>';
  137. }
  138. var typeOptions = '<option value=""></option>';
  139. for(let value in allTypeOptions[object])
  140. {
  141. typeOptions += '<option value="' + value + '" ' + (value === field ? 'selected' : '') + '>' + allTypeOptions[object][value].name + '</option>';
  142. }
  143. return '<div class="table-col"><select name="object[]" id="object' + index + '" class="form-control chosen">' + objectOptions + '</select></div><div class="table-col typebox"><select class="form-control types chosen" name="type[]" id="type' + index + '">' + typeOptions + '</select></div>';
  144. }
  145. function drawTable(fields, rows)
  146. {
  147. var head = '<tr>';
  148. for(let field in fields)
  149. {
  150. var fieldName = fields[field];
  151. head += '<th>' + (typeof fieldSettings[field] !== 'undefined' && fieldSettings[field].name ? fieldSettings[field].name : fieldName) + '</th>';
  152. }
  153. head += '</tr>';
  154. var html = '<thead>' + head + '</thead>';
  155. var body = '';
  156. for(let row of rows)
  157. {
  158. body += '<tr>';
  159. for(let index in row) body += '<td>' + row[index] + '</td>';
  160. body += '</tr>';
  161. }
  162. html += '<tbody>' + body + '</tbody>';
  163. $('table.result').html(html);
  164. }