rule.js 11 KB

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