rule.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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": "auth/rule/index",
  8. "add_url": "auth/rule/add",
  9. "edit_url": "auth/rule/edit",
  10. "del_url": "auth/rule/del",
  11. "multi_url": "auth/rule/multi",
  12. "table": "auth_rule"
  13. }
  14. });
  15. var table = $("#table");
  16. // 初始化表格
  17. table.bootstrapTable({
  18. url: $.fn.bootstrapTable.defaults.extend.index_url,
  19. sortName: '',
  20. escape: false,
  21. columns: [
  22. [
  23. {field: 'state', checkbox: true,},
  24. {field: 'id', title: 'ID'},
  25. {field: 'title', title: __('Title'), align: 'left', formatter: Controller.api.formatter.title, clickToSelect: !false},
  26. {field: 'icon', title: __('Icon'), formatter: Controller.api.formatter.icon},
  27. {field: 'name', title: __('Name'), align: 'left', formatter: Controller.api.formatter.name},
  28. {field: 'weigh', title: __('Weigh')},
  29. {field: 'status', title: __('Status'), formatter: Table.api.formatter.status},
  30. {
  31. field: 'ismenu',
  32. title: __('Ismenu'),
  33. align: 'center',
  34. table: table,
  35. formatter: Table.api.formatter.toggle
  36. },
  37. {
  38. field: 'operate',
  39. title: __('Operate'),
  40. table: table,
  41. events: Table.api.events.operate,
  42. formatter: Table.api.formatter.operate
  43. }
  44. ]
  45. ],
  46. pagination: false,
  47. search: false,
  48. commonSearch: false,
  49. rowAttributes: function (row, index) {
  50. return row.pid == 0 ? {} : {style: "display:none"};
  51. }
  52. });
  53. // 为表格绑定事件
  54. Table.api.bindevent(table);
  55. var btnSuccessEvent = function (data, ret) {
  56. if ($(this).hasClass("btn-change")) {
  57. var index = $(this).data("index");
  58. var row = Table.api.getrowbyindex(table, index);
  59. row.ismenu = $("i.fa.text-gray", this).length > 0 ? 1 : 0;
  60. table.bootstrapTable("updateRow", {index: index, row: row});
  61. } else if ($(this).hasClass("btn-delone")) {
  62. if ($(this).closest("tr[data-index]").find("a.btn-node-sub.disabled").length > 0) {
  63. $(this).closest("tr[data-index]").remove();
  64. } else {
  65. table.bootstrapTable('refresh');
  66. }
  67. } else if ($(this).hasClass("btn-dragsort")) {
  68. table.bootstrapTable('refresh');
  69. }
  70. Fast.api.refreshmenu();
  71. return false;
  72. };
  73. //表格内容渲染前
  74. table.on('pre-body.bs.table', function (e, data) {
  75. var options = table.bootstrapTable("getOptions");
  76. options.escape = true;
  77. });
  78. //当内容渲染完成后
  79. table.on('post-body.bs.table', function (e, data) {
  80. var options = table.bootstrapTable("getOptions");
  81. options.escape = false;
  82. //点击切换/排序/删除操作后刷新左侧菜单
  83. $(".btn-change[data-id],.btn-delone,.btn-dragsort").data("success", btnSuccessEvent);
  84. });
  85. table.on('post-body.bs.table', function (e, settings, json, xhr) {
  86. //显示隐藏子节点
  87. $(">tbody>tr[data-index] > td", this).on('click', "a.btn-node-sub", function () {
  88. var status = $(this).data("shown") ? true : false;
  89. $("a[data-pid='" + $(this).data("id") + "']").each(function () {
  90. $(this).closest("tr").toggle(!status);
  91. });
  92. if (status) {
  93. $("a[data-pid='" + $(this).data("id") + "']").trigger("collapse");
  94. }
  95. $(this).data("shown", !status);
  96. $("i", this).toggleClass("fa-caret-down").toggleClass("fa-caret-right");
  97. return false;
  98. });
  99. });
  100. //隐藏子节点
  101. $(document).on("collapse", ".btn-node-sub", function () {
  102. if ($("i", this).length > 0) {
  103. $("a[data-pid='" + $(this).data("id") + "']").trigger("collapse");
  104. }
  105. $("i", this).removeClass("fa-caret-down").addClass("fa-caret-right");
  106. $(this).data("shown", false);
  107. $(this).closest("tr").toggle(false);
  108. });
  109. //批量删除后的回调
  110. $(".toolbar > .btn-del,.toolbar .btn-more~ul>li>a").data("success", function (e) {
  111. Fast.api.refreshmenu();
  112. });
  113. //展开隐藏一级
  114. $(document.body).on("click", ".btn-toggle", function (e) {
  115. $("a[data-id][data-pid][data-pid!=0].disabled").closest("tr").hide();
  116. var that = this;
  117. var show = $("i", that).hasClass("fa-chevron-down");
  118. $("i", that).toggleClass("fa-chevron-down", !show).toggleClass("fa-chevron-up", show);
  119. $("a[data-id][data-pid][data-pid!=0]").not('.disabled').closest("tr").toggle(show);
  120. $(".btn-node-sub[data-pid=0]").data("shown", show);
  121. });
  122. //展开隐藏全部
  123. $(document.body).on("click", ".btn-toggle-all", function (e) {
  124. var that = this;
  125. var show = $("i", that).hasClass("fa-plus");
  126. $("i", that).toggleClass("fa-plus", !show).toggleClass("fa-minus", show);
  127. $(".btn-node-sub:not([data-pid=0])").closest("tr").toggle(show);
  128. $(".btn-node-sub").data("shown", show);
  129. $(".btn-node-sub > i").toggleClass("fa-caret-down", show).toggleClass("fa-caret-right", !show);
  130. });
  131. },
  132. add: function () {
  133. Controller.api.bindevent();
  134. },
  135. edit: function () {
  136. Controller.api.bindevent();
  137. },
  138. api: {
  139. formatter: {
  140. title: function (value, row, index) {
  141. value = value.toString().replace(/(&|&)nbsp;/g, ' ');
  142. var caret = row.haschild == 1 || row.ismenu == 1 ? '<i class="fa fa-caret-right"></i>' : '';
  143. value = value.indexOf("&nbsp;") > -1 ? value.replace(/(.*)&nbsp;/, "$1" + caret) : caret + value;
  144. value = !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
  145. return '<a href="javascript:;" data-id="' + row.id + '" data-pid="' + row.pid + '" class="'
  146. + (row.haschild == 1 || row.ismenu == 1 ? 'text-primary' : 'disabled') + ' btn-node-sub">' + value + '</a>';
  147. },
  148. name: function (value, row, index) {
  149. return !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
  150. },
  151. icon: function (value, row, index) {
  152. return '<span class="' + (!row.ismenu || row.status == 'hidden' ? 'text-muted' : '') + '"><i class="' + value + '"></i></span>';
  153. }
  154. },
  155. bindevent: function () {
  156. $(document).on('click', "input[name='row[ismenu]']", function () {
  157. var name = $("input[name='row[name]']");
  158. var ismenu = $(this).val() == 1;
  159. name.prop("placeholder", ismenu ? name.data("placeholder-menu") : name.data("placeholder-node"));
  160. $('div[data-type="menu"]').toggleClass("hidden", !ismenu);
  161. });
  162. $("input[name='row[ismenu]']:checked").trigger("click");
  163. var iconlist = [];
  164. var iconfunc = function () {
  165. Layer.open({
  166. type: 1,
  167. area: ['99%', '98%'], //宽高
  168. content: Template('chooseicontpl', {iconlist: iconlist})
  169. });
  170. };
  171. Form.api.bindevent($("form[role=form]"), function (data) {
  172. Fast.api.refreshmenu();
  173. });
  174. $(document).on('change keyup', "#icon", function () {
  175. $(this).prev().find("i").prop("class", $(this).val());
  176. });
  177. $(document).on('click', ".btn-search-icon", function () {
  178. if (iconlist.length == 0) {
  179. $.get(Config.site.cdnurl + "/assets/libs/font-awesome/less/variables.less", function (ret) {
  180. var exp = /fa-var-(.*):/ig;
  181. var result;
  182. while ((result = exp.exec(ret)) != null) {
  183. iconlist.push(result[1]);
  184. }
  185. iconfunc();
  186. });
  187. } else {
  188. iconfunc();
  189. }
  190. });
  191. $(document).on('click', '#chooseicon ul li', function () {
  192. $("input[name='row[icon]']").val('fa fa-' + $(this).data("font")).trigger("change");
  193. Layer.closeAll();
  194. });
  195. $(document).on('keyup', 'input.js-icon-search', function () {
  196. $("#chooseicon ul li").show();
  197. if ($(this).val() != '') {
  198. $("#chooseicon ul li:not([data-font*='" + $(this).val() + "'])").hide();
  199. }
  200. });
  201. }
  202. }
  203. };
  204. return Controller;
  205. });