rule.js 11 KB

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