zentaobiz.ui.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. window.initPromptSelectDataSourceForm = function()
  2. {
  3. selectCategory();
  4. selectField();
  5. removeField();
  6. sortSelectedFields();
  7. $('#submit').prop('disabled', !selectedSources.filter(Boolean).length);
  8. };
  9. // 状态管理对象
  10. const DataSourceManager = {
  11. state: {
  12. selectedCategory: activeDataSource || 'product',
  13. selectedFields: selectedSources || []
  14. },
  15. setCategory(category) {
  16. this.state.selectedCategory = category;
  17. $('#datagroup').val(category);
  18. },
  19. setFields(fields) {
  20. this.state.selectedFields = fields;
  21. $('#datasource').val(fields.join(','));
  22. },
  23. getCategory() {
  24. return this.state.selectedCategory;
  25. },
  26. getFields() {
  27. return this.state.selectedFields;
  28. }
  29. };
  30. // 渲染字段列表
  31. function renderFieldList(category) {
  32. if (!dataSource || !dataSourceLang || !category) {
  33. console.warn('Missing required data for renderFieldList');
  34. return;
  35. }
  36. const categoryDataSources = dataSource[category];
  37. const categoryLang = dataSourceLang[category];
  38. if (!categoryDataSources || !categoryLang) {
  39. console.warn(`No data found for category: ${category}`);
  40. return;
  41. }
  42. $('.category-groups')
  43. .attr('data-object-group', category)
  44. .empty();
  45. let html = '';
  46. Object.keys(categoryDataSources).forEach(sourceKey => {
  47. const fields = categoryDataSources[sourceKey];
  48. const sourceLang = categoryLang[sourceKey];
  49. const headerHtml = `
  50. <div class="category-group-header">
  51. <label class="checkbox group-item">
  52. <input type="checkbox" data-prop="${sourceKey}">
  53. <span>${sourceLang.common}</span>
  54. </label>
  55. </div>
  56. `;
  57. const fieldsHtml = fields.map(field => `
  58. <label class="checkbox field-item">
  59. <input type="checkbox" data-prop="${sourceKey}.${field}">
  60. <span>${sourceLang[field]}</span>
  61. </label>
  62. `).join('');
  63. const groupHtml = `
  64. <div class="category-group">
  65. ${headerHtml}
  66. <div class="category-group-body check-list-inline">
  67. ${fieldsHtml}
  68. </div>
  69. </div>
  70. `;
  71. html += groupHtml;
  72. });
  73. $('.category-groups').html(html);
  74. }
  75. // 渲染已选择数据的标题
  76. function renderSelectedTitle(category) {
  77. if (!dataSourceLang || !category) {
  78. return;
  79. }
  80. const categoryLang = dataSourceLang[category];
  81. if (!categoryLang) {
  82. return;
  83. }
  84. const categoryName = categoryLang.common;
  85. const titleElement = $('#selected-title-text');
  86. if (titleElement.length) {
  87. const format = titleElement.data('format');
  88. if (format) {
  89. const formattedText = format
  90. .replace('{0}', categoryName)
  91. .replace('{1}', DataSourceManager.getFields().length);
  92. titleElement.text(formattedText);
  93. } else {
  94. titleElement.text(categoryName);
  95. }
  96. }
  97. }
  98. // 渲染已选择的数据列表
  99. function renderSelectedList() {
  100. const fields = DataSourceManager.getFields().filter(Boolean);
  101. if (!fields.length) {
  102. $('#selected-data-sorter').html('');
  103. $('#submit').prop('disabled', true);
  104. return;
  105. }
  106. $('#submit').prop('disabled', false);
  107. const listItems = fields.map((field, index) => {
  108. const [source, fieldName] = field.split('.');
  109. const sourceLang = dataSourceLang[DataSourceManager.getCategory()][source];
  110. const fieldLang = sourceLang[fieldName];
  111. return `
  112. <li class="list-group-item" data-prop="${field}" data-order="${index}">
  113. <i class="icon icon-move text-gray drag-icon"></i>
  114. <span>${sourceLang.common} / ${fieldLang}</span>
  115. <i class="icon icon-close text-gray remove-icon"></i>
  116. </li>
  117. `;
  118. }).join('');
  119. const html = `
  120. <ol class="list-group">
  121. ${listItems}
  122. </ol>
  123. `;
  124. $('#selected-data-sorter').html(html);
  125. sortSelectedFields();
  126. }
  127. // 选择分类
  128. function selectCategory() {
  129. $('.category-list li')
  130. .on('click', function() {
  131. if($(this).hasClass('active')) {
  132. return;
  133. }
  134. DataSourceManager.setFields([]);
  135. $(this).addClass('active')
  136. .siblings().removeClass('active');
  137. const category = $(this).data('category');
  138. DataSourceManager.setCategory(category);
  139. renderFieldList(category);
  140. renderSelectedTitle(category);
  141. renderSelectedList();
  142. });
  143. }
  144. // 切换字段选中状态
  145. function toggleFieldSelect(fieldName, checked) {
  146. const fields = DataSourceManager.getFields();
  147. if (checked) {
  148. if (fields.indexOf(fieldName) < 0) {
  149. fields.push(fieldName);
  150. }
  151. } else {
  152. const index = fields.indexOf(fieldName);
  153. if (index >= 0) {
  154. fields.splice(index, 1);
  155. }
  156. }
  157. DataSourceManager.setFields(fields);
  158. renderSelectedTitle(DataSourceManager.getCategory());
  159. renderSelectedList();
  160. }
  161. // 分组选择
  162. function toggleGroupSelection(groupName, checked) {
  163. const fields = dataSource[DataSourceManager.getCategory()][groupName];
  164. fields.forEach(field => {
  165. const key = `${groupName}.${field}`;
  166. $(`input[type="checkbox"][data-prop="${key}"]`)
  167. .prop('checked', checked);
  168. toggleFieldSelect(key, checked);
  169. });
  170. }
  171. // 分组选择变化
  172. function groupSelectionOnChange(checkbox) {
  173. const checked = $(checkbox).prop('checked');
  174. const groupName = $(checkbox).data('prop');
  175. toggleGroupSelection(groupName, checked);
  176. }
  177. // 更新分组选择状态
  178. function updateGroupSelection(groupName) {
  179. const groupFields = dataSource[DataSourceManager.getCategory()][groupName];
  180. const selectedFields = DataSourceManager.getFields();
  181. const allChecked = groupFields
  182. .every(field => {
  183. const key = `${groupName}.${field}`;
  184. return selectedFields.indexOf(key) >= 0;
  185. });
  186. $(`input[type="checkbox"][data-prop="${groupName}"]`)
  187. .prop('checked', allChecked);
  188. }
  189. // 更新字段选择状态
  190. function updateFieldSelection(fieldName, checked) {
  191. $(`input[type="checkbox"][data-prop="${fieldName}"]`)
  192. .prop('checked', checked);
  193. }
  194. // 字段选择变化
  195. function fieldSelectionOnChange(checkbox) {
  196. const checked = $(checkbox).prop('checked');
  197. const fieldName = $(checkbox).data('prop');
  198. toggleFieldSelect(fieldName, checked);
  199. const groupName = fieldName.split('.')[0];
  200. updateGroupSelection(groupName);
  201. }
  202. // 选择字段
  203. function selectField() {
  204. $('.category-groups')
  205. .on('change', 'input[type="checkbox"]', function() {
  206. const prop = $(this).data('prop');
  207. // 检查是否为分组
  208. if(prop && prop.indexOf('.') === -1) {
  209. groupSelectionOnChange(this);
  210. } else {
  211. fieldSelectionOnChange(this);
  212. }
  213. });
  214. }
  215. // 删除字段
  216. function removeField() {
  217. $('#selected-data-sorter')
  218. .on('click', '.remove-icon', function() {
  219. const field = $(this).closest('li').data('prop');
  220. const fields = DataSourceManager.getFields();
  221. const index = fields.indexOf(field);
  222. if (index >= 0) {
  223. fields.splice(index, 1);
  224. DataSourceManager.setFields(fields);
  225. updateFieldSelection(field, false);
  226. const groupName = field.split('.')[0];
  227. updateGroupSelection(groupName);
  228. renderSelectedList();
  229. }
  230. });
  231. }
  232. // 拖动排序
  233. function sortSelectedFields() {
  234. if ($('#selected-data-sorter .list-group').data('sortable-initialized')) {
  235. return;
  236. }
  237. try {
  238. new zui.Sortable('#selected-data-sorter .list-group', {
  239. handle: '.list-group-item',
  240. animation: 150,
  241. ghostClass: 'bg-primary-pale',
  242. onSort: function(event) {
  243. const newOrder = [];
  244. $('#selected-data-sorter .list-group-item')
  245. .each(function() {
  246. newOrder.push($(this).data('prop'));
  247. });
  248. DataSourceManager.setFields(newOrder);
  249. updateListOrder();
  250. }
  251. });
  252. $('#selected-data-sorter .list-group').data('sortable-initialized', true);
  253. } catch (error) {
  254. console.error('Failed to initialize sortable for selected data sorter');
  255. }
  256. }
  257. // 只更新排序索引,不重新渲染
  258. function updateListOrder() {
  259. $('#selected-data-sorter .list-group-item').each(function(index) {
  260. $(this).attr('data-order', index);
  261. });
  262. }