config.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2. var Controller = {
  3. index: function () {
  4. $("form.edit-form").data("validator-options", {
  5. display: function (elem) {
  6. return $(elem).closest('tr').find("td:first").text();
  7. }
  8. });
  9. Form.api.bindevent($("form.edit-form"));
  10. //不可见的元素不验证
  11. $("form#add-form").data("validator-options", {
  12. ignore: ':hidden',
  13. rules: {
  14. content: function () {
  15. return ['radio', 'checkbox', 'select', 'selects'].indexOf($("#add-form select[name='row[type]']").val()) > -1;
  16. },
  17. extend: function () {
  18. return $("#add-form select[name='row[type]']").val() == 'custom';
  19. }
  20. }
  21. });
  22. Form.api.bindevent($("form#add-form"), function (ret) {
  23. setTimeout(function () {
  24. location.reload();
  25. }, 1500);
  26. });
  27. //渲染关联显示字段和存储字段
  28. var renderselect = function (id, data, defaultvalue) {
  29. var html = [];
  30. for (var i = 0; i < data.length; i++) {
  31. html.push("<option value='" + data[i].name + "' " + (defaultvalue == data[i].name ? "selected" : "") + " data-subtext='" + data[i].title + "'>" + data[i].name + "</option>");
  32. }
  33. var select = $(id);
  34. $(select).html(html.join(""));
  35. select.trigger("change");
  36. if (select.data("selectpicker")) {
  37. select.selectpicker('refresh');
  38. }
  39. };
  40. //关联表切换
  41. $(document).on('change', "#c-selectpage-table", function (e, first) {
  42. var that = this;
  43. Fast.api.ajax({
  44. url: "general/config/get_fields_list",
  45. data: {table: $(that).val()},
  46. }, function (data, ret) {
  47. renderselect("#c-selectpage-primarykey", data.fieldList, first ? $("#c-selectpage-primarykey").data("value") : '');
  48. renderselect("#c-selectpage-field", data.fieldList, first ? $("#c-selectpage-field").data("value") : '');
  49. return false;
  50. });
  51. return false;
  52. });
  53. //如果编辑模式则渲染已知数据
  54. if (['selectpage', 'selectpages'].indexOf($("#c-type").val()) > -1) {
  55. $("#c-selectpage-table").trigger("change", true);
  56. }
  57. //切换类型时
  58. $(document).on("change", "#c-type", function () {
  59. var value = $(this).val();
  60. $(".tf").addClass("hidden");
  61. $(".tf.tf-" + value).removeClass("hidden");
  62. if (["selectpage", "selectpages"].indexOf(value) > -1 && $("#c-selectpage-table option").size() == 1) {
  63. //异步加载表列表
  64. Fast.api.ajax({
  65. url: "general/config/get_table_list",
  66. }, function (data, ret) {
  67. renderselect("#c-selectpage-table", data.tableList);
  68. return false;
  69. });
  70. }
  71. });
  72. //切换显示隐藏变量字典列表
  73. $(document).on("change", "form#add-form select[name='row[type]']", function (e) {
  74. $("#add-content-container").toggleClass("hide", ['select', 'selects', 'checkbox', 'radio'].indexOf($(this).val()) > -1 ? false : true);
  75. });
  76. //选择规则
  77. $(document).on("click", ".rulelist > li > a", function () {
  78. var ruleArr = $("#rule").val() == '' ? [] : $("#rule").val().split(";");
  79. var rule = $(this).data("value");
  80. var index = ruleArr.indexOf(rule);
  81. if (index > -1) {
  82. ruleArr.splice(index, 1);
  83. } else {
  84. ruleArr.push(rule);
  85. }
  86. $("#rule").val(ruleArr.join(";"));
  87. $(this).parent().toggleClass("active");
  88. });
  89. //添加向发件人发送测试邮件按钮和方法
  90. $('input[name="row[mail_from]"]').parent().next().append('<a class="btn btn-info testmail">' + __('Send a test message') + '</a>');
  91. $(document).on("click", ".testmail", function () {
  92. var that = this;
  93. Layer.prompt({title: __('Please input your email'), formType: 0}, function (value, index) {
  94. Backend.api.ajax({
  95. url: "general/config/emailtest",
  96. data: $(that).closest("form").serialize() + "&receiver=" + value
  97. });
  98. });
  99. });
  100. //删除配置
  101. $(document).on("click", ".btn-delcfg", function () {
  102. var that = this;
  103. Layer.confirm(__('Are you sure you want to delete this item?'), {
  104. icon: 3,
  105. title: '提示'
  106. }, function (index) {
  107. Backend.api.ajax({
  108. url: "general/config/del",
  109. data: {name: $(that).data("name")}
  110. }, function () {
  111. $(that).closest("tr").remove();
  112. Layer.close(index);
  113. });
  114. });
  115. });
  116. },
  117. add: function () {
  118. Controller.api.bindevent();
  119. },
  120. edit: function () {
  121. Controller.api.bindevent();
  122. },
  123. api: {
  124. bindevent: function () {
  125. Form.api.bindevent($("form[role=form]"));
  126. }
  127. }
  128. };
  129. return Controller;
  130. });