v1.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. window.builderUpdate = function(action, type = 'page')
  2. {
  3. let {url, data, selectors} = getSqlBuilderPost(action, type);
  4. data = {sqlbuilder: data};
  5. const formData = zui.createFormData({action, data: JSON.stringify(data)});
  6. const defaultSelectors = ['#sqlBuilder', 'pageJS/.zin-page-js', 'pageCSS/.zin-page-css>*', '#configJS'];
  7. if(selectors?.length) defaultSelectors.push(selectors);
  8. postAndLoadPage(url, formData, defaultSelectors.join(','));
  9. }
  10. window.getSqlBuilderPost = function(action, type = 'page')
  11. {
  12. const sqlBuilder = $('#sqlBuilder').data('sqlbuilder');
  13. const url = $('#builderPanel').data('url');
  14. const selectors = type == 'page' ? '#builderPanel' : '';
  15. return {url, data: sqlBuilder, selectors};
  16. }
  17. window.switchBuilderStep = function(event, selectedClass, defaultClass)
  18. {
  19. const step = $(event.currentTarget).data('step');
  20. const currentStep = getSqlBuilder('step');
  21. if(currentStep == step) return;
  22. setSqlBuilder('step', step);
  23. builderUpdate('sqlBuilder-step');
  24. }
  25. window.changeBuilderTable = function(event)
  26. {
  27. const $target = $(event.target);
  28. const name = $target.attr('name');
  29. const value = $target.val();
  30. const from = getSqlBuilder('from');
  31. const joins = getSqlBuilder('joins');
  32. if(name == 'from')
  33. {
  34. from.table = value;
  35. from.select = [];
  36. }
  37. if(name.startsWith('left'))
  38. {
  39. const [key, alias] = name.split('_');
  40. const index = joins.findIndex(join => join.alias == alias);
  41. if(index < 0) return;
  42. joins[index].table = value;
  43. joins[index].select = [];
  44. }
  45. if(name.startsWith('join'))
  46. {
  47. const [key, alias, onKey] = name.split('_');
  48. const index = joins.findIndex(join => join.alias == alias);
  49. if(index < 0) return;
  50. if(onKey == 'table') joins[index].on[0] = value;
  51. if(onKey == 'fieldA') joins[index].on[1] = value;
  52. if(onKey == 'fieldB') joins[index].on[4] = value;
  53. }
  54. setSqlBuilder('from', from);
  55. setSqlBuilder('joins', joins);
  56. builderUpdate('sqlBuilder-table');
  57. }
  58. window.changeBuilderFunc = function(event)
  59. {
  60. const $target = $(event.target);
  61. const name = $target.attr('name');
  62. const value = $target.val();
  63. const funcs = getSqlBuilder('funcs');
  64. const [key, index] = name.split('_');
  65. funcs[index][key] = value;
  66. setSqlBuilder('funcs', funcs);
  67. builderUpdate('sqlBuilder-func');
  68. }
  69. window.changeBuilderWhere = function(event)
  70. {
  71. const $target = $(event.target);
  72. const name = $target.attr('name');
  73. const value = $target.val();
  74. const wheres = getSqlBuilder('wheres');
  75. const [key, groupIndex, itemIndex] = name.split('_');
  76. if(key == 'operator')
  77. {
  78. wheres[groupIndex].operator = value;
  79. }
  80. else
  81. {
  82. wheres[groupIndex].items[itemIndex][key] = value;
  83. }
  84. setSqlBuilder('wheres', wheres);
  85. builderUpdate('sqlBuilder-where', key == 4 ? 'ajax' : 'page');
  86. }
  87. window.changeBuilderQuery = function(event)
  88. {
  89. const $target = $(event.target);
  90. const name = $target.attr('name');
  91. const value = $target.val();
  92. const querys = getSqlBuilder('querys');
  93. let [key, index] = name.split('_');
  94. if(index.indexOf("[") !== -1) index = index.substring(0, index.indexOf("["));
  95. querys[index][key] = value;
  96. setSqlBuilder('querys', querys);
  97. builderUpdate('sqlBuilder-query');
  98. }
  99. window.addJoinTable = function(event)
  100. {
  101. const $target = $(event.target);
  102. let index = $(event.currentTarget).data('index');
  103. const joins = getSqlBuilder('joins');
  104. if(index == -1) index = joins.length - 1;
  105. joins.splice(index + 1, 0, 'add');
  106. setSqlBuilder('joins', joins);
  107. builderUpdate('sqlBuilder-table');
  108. }
  109. window.removeJoinTable = function(event)
  110. {
  111. let index = $(event.currentTarget).data('index');
  112. const joins = getSqlBuilder('joins');
  113. joins.splice(index, 1);
  114. setSqlBuilder('joins', joins);
  115. builderUpdate('sqlBuilder-table');
  116. }
  117. window.handleSelectFieldChange = function(event)
  118. {
  119. const $target = $(event.target);
  120. const alias = $target.data('alias');
  121. const value = $target.val();
  122. const checked = $target.is(':checked');
  123. const from = getSqlBuilder('from');
  124. const joins = getSqlBuilder('joins');
  125. if(alias == 't1')
  126. {
  127. const fieldIndex = from.select.findIndex(field => value == field);
  128. if(checked) from.select.splice(-1, 0, value);
  129. else from.select.splice(fieldIndex, 1);
  130. }
  131. else
  132. {
  133. const joinIndex = joins.findIndex(join => join.alias == alias);
  134. const fieldIndex = joins[joinIndex].select.findIndex(field => value == field);
  135. if(checked) joins[joinIndex].select.splice(-1, 0, value);
  136. else joins[joinIndex].select.splice(fieldIndex, 1);
  137. }
  138. setSqlBuilder('from', from);
  139. setSqlBuilder('joins', joins);
  140. builderUpdate('sqlBuilder-field', 'ajax');
  141. }
  142. window.checkAllField = function(event)
  143. {
  144. const alias = $(event.currentTarget).data('alias');
  145. const isCheckedAll = $(event.currentTarget).data('checked');
  146. const from = getSqlBuilder('from');
  147. const joins = getSqlBuilder('joins');
  148. if(alias == 't1')
  149. {
  150. from.select = isCheckedAll ? [] : '*';
  151. }
  152. else
  153. {
  154. const joinIndex = joins.findIndex(join => join.alias == alias);
  155. joins[joinIndex].select = isCheckedAll ? [] : '*';
  156. }
  157. setSqlBuilder('from', from);
  158. setSqlBuilder('joins', joins);
  159. builderUpdate('sqlBuilder-field');
  160. }
  161. window.addFunction = function(event)
  162. {
  163. const $target = $(event.target);
  164. let index = $(event.currentTarget).data('index');
  165. const funcs = getSqlBuilder('funcs');
  166. if(index == -1) index = funcs.length - 1;
  167. funcs.splice(index + 1, 0, 'add');
  168. setSqlBuilder('funcs', funcs);
  169. builderUpdate('sqlBuilder-func');
  170. }
  171. window.removeFunction = function(event)
  172. {
  173. let index = $(event.currentTarget).data('index');
  174. const funcs = getSqlBuilder('funcs');
  175. funcs.splice(index, 1);
  176. setSqlBuilder('funcs', funcs);
  177. builderUpdate('sqlBuilder-func');
  178. }
  179. window.addWhereGroup = function(event)
  180. {
  181. const $target = $(event.target);
  182. let index = $(event.currentTarget).data('index');
  183. const wheres = getSqlBuilder('wheres');
  184. console.log(index);
  185. if(index == -1) index = wheres.length - 1;
  186. wheres.splice(index + 1, 0, 'add');
  187. setSqlBuilder('wheres', wheres);
  188. builderUpdate('sqlBuilder-group');
  189. }
  190. window.removeWhereGroup = function(event)
  191. {
  192. let index = $(event.currentTarget).data('index');
  193. const wheres = getSqlBuilder('wheres');
  194. wheres.splice(index, 1);
  195. setSqlBuilder('wheres', wheres);
  196. builderUpdate('sqlBuilder-where');
  197. }
  198. window.addWhereItem = function(event)
  199. {
  200. const $target = $(event.target);
  201. const index = $(event.currentTarget).data('index');
  202. const wheres = getSqlBuilder('wheres');
  203. const [groupIndex, itemIndex] = index.split('_');
  204. wheres[groupIndex].items.splice(itemIndex + 1, 0, 'add');
  205. setSqlBuilder('wheres', wheres);
  206. builderUpdate('sqlBuilder-where');
  207. }
  208. window.removeWhereItem = function(event)
  209. {
  210. const index = $(event.currentTarget).data('index');
  211. const wheres = getSqlBuilder('wheres');
  212. const [groupIndex, itemIndex] = index.split('_');
  213. wheres[groupIndex].items.splice(itemIndex, 1);
  214. if(!wheres[groupIndex].items.length) wheres.splice(groupIndex, 1);
  215. setSqlBuilder('wheres', wheres);
  216. builderUpdate('sqlBuilder-where');
  217. }
  218. window.addBuilderQueryFilter = function(event)
  219. {
  220. const $target = $(event.target);
  221. let index = $(event.currentTarget).data('index');
  222. const querys = getSqlBuilder('querys');
  223. if(index == -1) index = querys.length - 1;
  224. querys.splice(index + 1, 0, 'add');
  225. setSqlBuilder('querys', querys);
  226. builderUpdate('sqlBuilder-query');
  227. }
  228. window.removeBuilderQueryFilter = function(event)
  229. {
  230. const index = $(event.currentTarget).data('index');
  231. const querys = getSqlBuilder('querys');
  232. querys.splice(index, 1);
  233. setSqlBuilder('querys', querys);
  234. builderUpdate('sqlBuilder-query');
  235. }
  236. window.changeGroupBy = function(event)
  237. {
  238. const $target = $(event.target);
  239. const checked = $target.is(':checked');
  240. let groups = getSqlBuilder('groups');
  241. groups = checked;
  242. setSqlBuilder('groups', groups);
  243. builderUpdate('sqlBuilder-group');
  244. }
  245. window.changeAgg = function(event)
  246. {
  247. const $target = $(event.target);
  248. const name = $target.attr('name');
  249. const value = $target.val();
  250. let groups = getSqlBuilder('groups');
  251. const [table, field] = name.split('_');
  252. groups = groups.map(group => {
  253. const [groupTable, groupField] = group.select;
  254. if(table == groupTable && field == groupField) group.select[3] = value;
  255. return group;
  256. });
  257. setSqlBuilder('groups', groups);
  258. builderUpdate('sqlBuilder-group');
  259. }
  260. window.sortGroupBy = function(event)
  261. {
  262. const $sortableList = $(event.target).closest('.group-by-sort').zui('SortableList');
  263. let groups = getSqlBuilder('groups');
  264. const sortOrders = $sortableList.$.getOrders();
  265. const groupFields = groups.filter(group => group.type == 'group');
  266. const oldOrders = groupFields.map(group => group.order);
  267. const originOrders = oldOrders;
  268. originOrders.sort();
  269. const newOrders = sortOrders.map(sortOrder => oldOrders[sortOrder]);
  270. groups = groups.map((group, index) => {
  271. if(group.type != 'group') return group;
  272. const newIndex = newOrders.findIndex(order => group.order == order);
  273. group.order = originOrders[newIndex];
  274. return group;
  275. });
  276. setSqlBuilder('groups', groups);
  277. builderUpdate('sqlBuilder-group', 'ajax');
  278. }
  279. window.switchGroupFieldType = function(event)
  280. {
  281. const $target = $(event.target);
  282. const index = $(event.currentTarget).data('index');
  283. const type = $(event.currentTarget).data('type');
  284. let groups = getSqlBuilder('groups');
  285. if(type == groups[index].type) return;
  286. groups[index].type = type;
  287. setSqlBuilder('groups', groups);
  288. builderUpdate('sqlBuilder-group');
  289. }
  290. window.setSqlBuilder = function(key, value)
  291. {
  292. const sqlBuilder = $('#sqlBuilder').data('sqlbuilder');
  293. sqlBuilder[key] = value;
  294. $('#sqlBuilder').attr('data-sqlbuilder', JSON.stringify(sqlBuilder));
  295. }
  296. window.getSqlBuilder = function(key)
  297. {
  298. const onUpdate = $('#builderPanel').data('onupdate');
  299. if(onUpdate?.length) window.builderUpdate = window[onUpdate];
  300. const sqlBuilder = $('#sqlBuilder').data('sqlbuilder');
  301. return sqlBuilder[key];
  302. }