enum_editor.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //表达式的选项值域选择器
  2. (function () {
  3. //备选的四个条件
  4. var ConditionEnum = {
  5. Equal: 0,
  6. NotEqual: 1,
  7. Includes: 2,
  8. NotIncludes: 3,
  9. };
  10. //当前选中的条件
  11. var _selectedCondition = 0;
  12. //对应的外部表达式环境属性ID
  13. var propertyId;
  14. //外部传入的当前设置
  15. var _curSetting;
  16. $(function () {
  17. //条件被选择时
  18. $(document).on("change", "input[name='condition']", function () {
  19. _selectedCondition = parseInt($(this).val());
  20. if (_selectedCondition < 2) {
  21. $('input[type="checkbox"][name="option"]:checked').each(function (index) {
  22. if (index > 0) {
  23. $(this).prop('checked', false);
  24. }
  25. });
  26. }
  27. });
  28. //选项被改变的时候
  29. $(document).on("change", "input[name='option']", function () {
  30. if (_selectedCondition < 2) {
  31. clearAll();
  32. $(this).prop("checked", true);
  33. if (_curSetting.showMode == "选项值域选择") $("#name").val(this.nextElementSibling.innerText)
  34. }
  35. });
  36. $("#search_text").on("keydown", function (e) {
  37. if (e.keyCode == 13) {
  38. $("#search_btn").click();
  39. }
  40. });
  41. $("input[name='option_type']").on("click", function () {
  42. let optionType = $(this).val();
  43. //动态选项
  44. if (optionType == "1") {
  45. $("#search_info").show();
  46. $('#clear_options').hide();
  47. $("#search_result")[0].innerText = "已选内容:" + (_curSetting.items?.join(",") ?? "无");
  48. $(".options_selector").empty();
  49. $("#search_text").focus();
  50. //根据输入内容查询后加载选项数据
  51. } else {
  52. //静态选项
  53. $("#search_info").hide();
  54. $('#clear_options').show();
  55. let optionData = _curSetting.options(_curSetting.propertyId);
  56. LoadOptionData(optionData, _curSetting.items);
  57. }
  58. });
  59. $("#search_btn").on("click", function () {
  60. let searchKey = $("#search_text").val();
  61. if (searchKey == null || searchKey == "") {
  62. layer.msg('请输入搜索关键字', { time: 1000 });
  63. $("#search_text").focus();
  64. return;
  65. }
  66. let optionData = _curSetting.options(_curSetting.propertyId, searchKey);
  67. LoadOptionData(optionData, _curSetting.items);
  68. });
  69. $("#clear_options").on("click", function () {
  70. $("input[name='option']").prop("checked", false);
  71. });
  72. });
  73. /**
  74. * 清楚所有选项内容
  75. */
  76. function clearAll() {
  77. $("input[name='option']").prop("checked", false);
  78. }
  79. /**
  80. 获取表达式代码
  81. */
  82. GetEnumSetting = function () {
  83. let name = $("#name").val();
  84. if (name == null || name == "") {
  85. layer.msg('名称不能为空', { time: 1000 });
  86. return null;
  87. }
  88. //这里不取id,因为解析执行是按照文本来的,取下一个元素的文本
  89. let selItems = $("input[name='option']:checked").map(function (e) {
  90. let ret = this.nextElementSibling.innerText;
  91. //如果是返回名称,则要加上引号,以便存储为字符型
  92. if (_curSetting.selItemMode == "value") ret = this.value;
  93. return ret;
  94. }).get();
  95. let result = {
  96. name: name,
  97. propertyId: _curSetting.propertyId,
  98. selectedCondition: _selectedCondition,
  99. items: selItems
  100. }
  101. return result;
  102. }
  103. /**
  104. * 初始化
  105. */
  106. Init = function (curSetting, allSettings) {
  107. _curSetting = curSetting;
  108. _selectedCondition = curSetting.selectedCondition ?? 0;
  109. if (curSetting != null) {
  110. $("#name").val(curSetting.name);
  111. //设置选项对比条件
  112. if (curSetting.selectedCondition) {
  113. $(`input[name='condition'][value=${curSetting.selectedCondition}]`).prop('checked', true);
  114. }
  115. //设置选项
  116. if (curSetting.options) {
  117. if (typeof curSetting.options !== "function") {
  118. $("#option_type").hide();
  119. LoadOptionData(curSetting.options, curSetting.items);
  120. }
  121. }
  122. }
  123. if (_curSetting.showMode == "选项值域选择") {
  124. $("fieldset[name='symbol']")[0].style.display = "none";
  125. $(".condition_selector")[0].style.display = "none";
  126. }
  127. }
  128. //加载选项数据
  129. function LoadOptionData(optionList, curSelItems) {
  130. if (optionList.length > 10000) {
  131. layer.msg('选项数据过多,只显示了前1万行,如需查找需要的选项,请输入关键字查找', { time: 5000 });
  132. optionList = optionList.slice(0, 10000);
  133. }
  134. $(".options_selector").empty();
  135. for (var option of optionList) {
  136. $(".options_selector").append($(`<label><input type="checkbox" name="option"
  137. value="${option.value}" ${curSelItems?.includes(_curSetting.selItemMode == "value" ? option.value : option.display) ? 'checked' : ''}/>
  138. <span>${option.display}</span></label>`));
  139. }
  140. //先改了运算符再执行选项加载
  141. if (_selectedCondition < 2) {
  142. $('input[type="checkbox"][name="option"]:checked').each(function (index) {
  143. if (index > 0) {
  144. $(this).prop('checked', false);
  145. }
  146. });
  147. }
  148. }
  149. })();