response.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2. var Controller = {
  3. index: function () {
  4. // 初始化表格参数配置
  5. Table.api.init({
  6. extend: {
  7. index_url: 'wechat/response/index',
  8. add_url: 'wechat/response/add',
  9. edit_url: 'wechat/response/edit',
  10. del_url: 'wechat/response/del',
  11. multi_url: 'wechat/response/multi',
  12. }
  13. });
  14. var table = $("#table");
  15. // 初始化表格
  16. table.bootstrapTable({
  17. url: $.fn.bootstrapTable.defaults.extend.index_url,
  18. sortName: 'id',
  19. columns: [
  20. [
  21. {field: 'state', checkbox: true, },
  22. {field: 'id', title: 'ID'},
  23. {field: 'type', title: __('Type')},
  24. {field: 'title', title: __('Resource title')},
  25. {field: 'eventkey', title: __('Event key')},
  26. {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, operate:false},
  27. {field: 'operate', title: __('Operate'), events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  28. ]
  29. ]
  30. });
  31. // 为表格绑定事件
  32. Table.api.bindevent(table);
  33. },
  34. select: function () {
  35. // 初始化表格参数配置
  36. Table.api.init({
  37. extend: {
  38. index_url: 'wechat/response/index',
  39. }
  40. });
  41. var table = $("#table");
  42. // 初始化表格
  43. table.bootstrapTable({
  44. url: $.fn.bootstrapTable.defaults.extend.index_url,
  45. sortName: 'id',
  46. columns: [
  47. [
  48. {field: 'state', checkbox: true, },
  49. {field: 'id', title: 'ID'},
  50. {field: 'type', title: __('Type')},
  51. {field: 'title', title: __('Title')},
  52. {field: 'event', title: __('Event')},
  53. {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, operate:false},
  54. {field: 'operate', title: __('Operate'), events: {
  55. 'click .btn-chooseone': function (e, value, row, index) {
  56. var callback = Backend.api.query('callback');
  57. if (callback) {
  58. parent.window[callback](row);
  59. }
  60. },
  61. }, formatter: function () {
  62. return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
  63. }}
  64. ]
  65. ]
  66. });
  67. // 为表格绑定事件
  68. Table.api.bindevent(table);
  69. },
  70. add: function () {
  71. Form.api.bindevent($("form[role=form]"), null, function (ret) {
  72. var callback = Backend.api.query('callback');
  73. if (callback) {
  74. parent.window[callback](ret);
  75. } else {
  76. parent.$(".btn-refresh").trigger("click");
  77. var index = parent.Layer.getFrameIndex(window.name);
  78. parent.Layer.close(index);
  79. }
  80. });
  81. Controller.api.bindevent();
  82. },
  83. edit: function () {
  84. Form.api.bindevent($("form[role=form]"));
  85. Controller.api.bindevent();
  86. },
  87. api: {
  88. bindevent: function () {
  89. var getAppFileds = function (app) {
  90. var app = apps[app];
  91. var appConfig = app['config'];
  92. var str = '';
  93. for (i in appConfig) {
  94. if (appConfig[i]['type'] == 'text' || appConfig[i]['type'] == 'textarea') {
  95. var pattern_str = 'pattern ="required" ';
  96. var alt = '';
  97. if (undefined != appConfig[i]['alt'])
  98. alt = appConfig[i]['alt'];
  99. if (undefined != appConfig[i]['pattern'])
  100. pattern_str = 'pattern ="' + appConfig[i]['pattern'] + '" ';
  101. if (appConfig[i]['type'] == 'textarea') {
  102. str += '<div class="form-group"><label for="content" class="control-label col-xs-12 col-sm-2">' + appConfig[i]['caption'] + ':</label><div class="col-xs-12 col-sm-8"><textarea class="form-control" name="row[content][' + appConfig[i]['field'] + ']" ' + pattern_str + ' alt="' + alt + '" data-rule="required"></textarea> </div> </div>';
  103. } else {
  104. str += '<div class="form-group"><label for="content" class="control-label col-xs-12 col-sm-2">' + appConfig[i]['caption'] + ':</label><div class="col-xs-12 col-sm-8"><input class="form-control" name="row[content][' + appConfig[i]['field'] + ']" type="text" ' + pattern_str + ' alt="' + alt + '" data-rule="required"> </div> </div>';
  105. }
  106. } else {
  107. var options = appConfig[i]['options'];
  108. options = options.split(',');
  109. var option_str = '';
  110. if (appConfig[i]['type'] == 'select') {
  111. for (o in options) {
  112. var option = options[o];
  113. var item = option.split(':');
  114. option_str += '<option value="' + item[0] + '">' + item[1] + '</option>';
  115. }
  116. option_str = '<select class="form-control" name="row[content][' + appConfig[i]['field'] + ']">' + option_str + '</select>';
  117. } else if (appConfig[i]['type'] == 'checkbox') {
  118. for (o in options) {
  119. var option = options[o];
  120. var item = option.split(':');
  121. option_str += '<input type="checkbox" name="row[content][' + appConfig[i]['field'] + '][]" value="' + item[0] + '"> <label>' + item[1] + '</label> ';
  122. }
  123. } else if (appConfig[i]['type'] == 'radio') {
  124. for (o in options) {
  125. var option = options[o];
  126. var item = option.split(':');
  127. option_str += '<input type="radio" name="row[content][' + appConfig[i]['field'] + ']" value="' + item[0] + '"> <label>' + item[1] + '</label> ';
  128. }
  129. }
  130. str += '<div class="form-group"><label for="content" class="control-label col-xs-12 col-sm-2">' + appConfig[i]['caption'] + ':</label><div class="col-xs-12 col-sm-8">' + option_str + ' </div> </div>';
  131. }
  132. }
  133. return str;
  134. };
  135. $(document).on('change', "#app", function () {
  136. var app = $(this).val();
  137. $("#appfields").html(getAppFileds(app));
  138. if (datas.app == app) {
  139. delete(datas.app);
  140. var form = $("form.form-ajax");
  141. $.each(datas, function (i, j) {
  142. form.field("row[content][" + i + "]" + ($("input[name='row[content][" + i + "][]']", form).size() > 0 ? '[]' : ''), j);
  143. });
  144. }
  145. });
  146. $(document).on('click', "input[name='row[type]']", function () {
  147. var type = $(this).val();
  148. if (type == 'text') {
  149. $("#expand").html('<div class="form-group"><label for="content" class="control-label col-xs-12 col-sm-2">文本内容:</label><div class="col-xs-12 col-sm-8"><textarea class="form-control" name="row[content][content]" data-rule="required"></textarea> <a href="javascript:;" class="btn-insertlink">插入链接</a></div></div>');
  150. $("form.form-ajax").field("row[content][content]", datas.content);
  151. } else if (type == 'app') {
  152. $("#expand").html('<div class="form-group"><label for="content" class="control-label col-xs-12 col-sm-2">应用:</label><div class="col-xs-12 col-sm-8"><select class="form-control" name="row[content][app]" id="app">' + $("select[name=applist]").html() + '</select></div></div><div id="appfields"><div>');
  153. $("form.form-ajax").field("row[content][app]", datas.app);
  154. $("#app").trigger('change');
  155. }
  156. });
  157. $(document).on('click', ".btn-insertlink", function () {
  158. var textarea = $("textarea[name='row[content][content]']");
  159. var cursorPos = textarea.prop('selectionStart');
  160. var v = textarea.val();
  161. var textBefore = v.substring(0, cursorPos);
  162. var textAfter = v.substring(cursorPos, v.length);
  163. Layer.prompt({title: '请输入显示的文字', formType: 3}, function (text, index) {
  164. Layer.close(index);
  165. Layer.prompt({title: '请输入跳转的链接URL(包含http)', formType: 3}, function (link, index) {
  166. text = text == '' ? link : text;
  167. textarea.val(textBefore + '<a href="' + link + '">' + text + '</a>' + textAfter);
  168. Layer.close(index);
  169. });
  170. });
  171. });
  172. $("input[name='row[type]']:checked").trigger("click");
  173. }
  174. }
  175. };
  176. return Controller;
  177. });