command.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {
  2. var Controller = {
  3. index: function () {
  4. // 初始化表格参数配置
  5. Table.api.init({
  6. extend: {
  7. index_url: 'command/index',
  8. add_url: 'command/add',
  9. edit_url: '',
  10. del_url: 'command/del',
  11. multi_url: 'command/multi',
  12. table: 'command',
  13. }
  14. });
  15. var table = $("#table");
  16. // 初始化表格
  17. table.bootstrapTable({
  18. url: $.fn.bootstrapTable.defaults.extend.index_url,
  19. pk: 'id',
  20. sortName: 'id',
  21. columns: [
  22. [
  23. {checkbox: true},
  24. {field: 'id', title: __('Id')},
  25. {field: 'type', title: __('Type'), formatter: Table.api.formatter.search},
  26. {field: 'type_text', title: __('Type')},
  27. {
  28. field: 'command', title: __('Command'), renderDefault: false, formatter: function (value, row, index) {
  29. return '<input type="text" class="form-control" value="' + value + '">';
  30. }
  31. },
  32. {
  33. field: 'executetime',
  34. title: __('Executetime'),
  35. operate: 'RANGE',
  36. addclass: 'datetimerange',
  37. formatter: Table.api.formatter.datetime
  38. },
  39. {
  40. field: 'createtime',
  41. title: __('Createtime'),
  42. operate: 'RANGE',
  43. addclass: 'datetimerange',
  44. formatter: Table.api.formatter.datetime
  45. },
  46. {
  47. field: 'updatetime',
  48. title: __('Updatetime'),
  49. operate: 'RANGE',
  50. addclass: 'datetimerange',
  51. formatter: Table.api.formatter.datetime
  52. },
  53. {
  54. field: 'status',
  55. title: __('Status'),
  56. table: table,
  57. custom: {"successed": 'success', "failured": 'danger'},
  58. searchList: {"successed": __('Successed'), "failured": __('Failured')},
  59. formatter: Table.api.formatter.status
  60. },
  61. {
  62. field: 'operate',
  63. title: __('Operate'),
  64. buttons: [
  65. {
  66. name: 'execute',
  67. title: __('Execute again'),
  68. text: __('Execute again'),
  69. url: 'command/execute',
  70. icon: 'fa fa-repeat',
  71. classname: 'btn btn-success btn-xs btn-execute btn-ajax',
  72. success: function (data) {
  73. Layer.alert("<textarea class='form-control' cols='60' rows='5'>" + data.result + "</textarea>", {
  74. title: __("执行结果"),
  75. shadeClose: true
  76. });
  77. table.bootstrapTable('refresh');
  78. return false;
  79. }
  80. },
  81. {
  82. name: 'execute',
  83. title: __('Detail'),
  84. text: __('Detail'),
  85. url: 'command/detail',
  86. icon: 'fa fa-list',
  87. classname: 'btn btn-info btn-xs btn-execute btn-dialog'
  88. }
  89. ],
  90. table: table,
  91. events: Table.api.events.operate,
  92. formatter: Table.api.formatter.operate
  93. }
  94. ]
  95. ]
  96. });
  97. // 为表格绑定事件
  98. Table.api.bindevent(table);
  99. },
  100. add: function () {
  101. require(['bootstrap-select', 'bootstrap-select-lang']);
  102. var mainfields = [];
  103. var relationfields = {};
  104. var maintable = [];
  105. var relationtable = [];
  106. var relationmode = ["belongsto", "hasone"];
  107. var renderselect = function (select, data) {
  108. var html = [];
  109. for (var i = 0; i < data.length; i++) {
  110. html.push("<option value='" + data[i] + "'>" + data[i] + "</option>");
  111. }
  112. $(select).html(html.join(""));
  113. select.trigger("change");
  114. if (select.data("selectpicker")) {
  115. select.selectpicker('refresh');
  116. }
  117. return select;
  118. };
  119. $("select[name=table] option").each(function () {
  120. maintable.push($(this).val());
  121. });
  122. $(document).on('change', "input[name='isrelation']", function () {
  123. $("#relation-zone").toggleClass("hide", !$(this).prop("checked"));
  124. });
  125. $(document).on('change', "select[name='table']", function () {
  126. var that = this;
  127. Fast.api.ajax({
  128. url: "command/get_field_list",
  129. data: {table: $(that).val()},
  130. }, function (data, ret) {
  131. mainfields = data.fieldlist;
  132. $("#relation-zone .relation-item").remove();
  133. renderselect($("#fields"), mainfields);
  134. return false;
  135. });
  136. return false;
  137. });
  138. $(document).on('click', "a.btn-newrelation", function () {
  139. var that = this;
  140. var index = parseInt($(that).data("index")) + 1;
  141. var content = Template("relationtpl", {index: index});
  142. content = $(content.replace(/\[index\]/, index));
  143. $(this).data("index", index);
  144. $(content).insertBefore($(that).closest(".row"));
  145. $('select', content).selectpicker();
  146. var exists = [$("select[name='table']").val()];
  147. $("select.relationtable").each(function () {
  148. exists.push($(this).val());
  149. });
  150. relationtable = [];
  151. $.each(maintable, function (i, j) {
  152. if ($.inArray(j, exists) < 0) {
  153. relationtable.push(j);
  154. }
  155. });
  156. renderselect($("select.relationtable", content), relationtable);
  157. $("select.relationtable", content).trigger("change");
  158. });
  159. $(document).on('click', "a.btn-removerelation", function () {
  160. $(this).closest(".row").remove();
  161. });
  162. $(document).on('change', "#relation-zone select.relationmode", function () {
  163. var table = $("select.relationtable", $(this).closest(".row")).val();
  164. var that = this;
  165. Fast.api.ajax({
  166. url: "command/get_field_list",
  167. data: {table: table},
  168. }, function (data, ret) {
  169. renderselect($(that).closest(".row").find("select.relationprimarykey"), $(that).val() == 'belongsto' ? data.fieldlist : mainfields);
  170. renderselect($(that).closest(".row").find("select.relationforeignkey"), $(that).val() == 'hasone' ? data.fieldlist : mainfields);
  171. return false;
  172. });
  173. });
  174. $(document).on('change', "#relation-zone select.relationtable", function () {
  175. var that = this;
  176. Fast.api.ajax({
  177. url: "command/get_field_list",
  178. data: {table: $(that).val()},
  179. }, function (data, ret) {
  180. renderselect($(that).closest(".row").find("select.relationmode"), relationmode);
  181. renderselect($(that).closest(".row").find("select.relationfields"), mainfields)
  182. renderselect($(that).closest(".row").find("select.relationforeignkey"), data.fieldlist)
  183. renderselect($(that).closest(".row").find("select.relationfields"), data.fieldlist)
  184. return false;
  185. });
  186. });
  187. $(document).on('click', ".btn-command", function () {
  188. var form = $(this).closest("form");
  189. var textarea = $("textarea[rel=command]", form);
  190. textarea.val('');
  191. Fast.api.ajax({
  192. url: "command/command/action/command",
  193. data: form.serialize(),
  194. }, function (data, ret) {
  195. textarea.val(data.command);
  196. return false;
  197. });
  198. });
  199. $(document).on('click', ".btn-execute", function () {
  200. var form = $(this).closest("form");
  201. var textarea = $("textarea[rel=result]", form);
  202. textarea.val('');
  203. Fast.api.ajax({
  204. url: "command/command/action/execute",
  205. data: form.serialize(),
  206. }, function (data, ret) {
  207. textarea.val(data.result);
  208. window.parent.$(".toolbar .btn-refresh").trigger('click');
  209. top.window.Fast.api.refreshmenu();
  210. return false;
  211. }, function () {
  212. window.parent.$(".toolbar .btn-refresh").trigger('click');
  213. });
  214. });
  215. $("select[name='table']").trigger("change");
  216. Controller.api.bindevent();
  217. },
  218. edit: function () {
  219. Controller.api.bindevent();
  220. },
  221. api: {
  222. bindevent: function () {
  223. Form.api.bindevent($("form[role=form]"));
  224. }
  225. }
  226. };
  227. return Controller;
  228. });