edit.js 7.2 KB

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