rule.js 11 KB

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