rule.js 11 KB

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