rule.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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: 'weigh',
  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},
  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. formatter: Table.api.formatter.toggle
  35. },
  36. {
  37. field: 'id',
  38. title: '<a href="javascript:;" class="btn btn-success btn-xs btn-toggle"><i class="fa fa-chevron-up"></i></a>',
  39. operate: false,
  40. formatter: Controller.api.formatter.subnode
  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. });
  55. // 为表格绑定事件
  56. Table.api.bindevent(table);
  57. //表格内容渲染前
  58. table.on('pre-body.bs.table', function (e, data) {
  59. var options = table.bootstrapTable("getOptions");
  60. options.escape = true;
  61. });
  62. //当内容渲染完成后
  63. table.on('post-body.bs.table', function (e, data) {
  64. var options = table.bootstrapTable("getOptions");
  65. options.escape = false;
  66. //默认隐藏所有子节点
  67. //$("a.btn[data-id][data-pid][data-pid!=0]").closest("tr").hide();
  68. $(".btn-node-sub.disabled").closest("tr").hide();
  69. //显示隐藏子节点
  70. $(".btn-node-sub").off("click").on("click", function (e) {
  71. var status = $(this).data("shown") ? true : false;
  72. $("a.btn[data-pid='" + $(this).data("id") + "']").each(function () {
  73. $(this).closest("tr").toggle(!status);
  74. });
  75. $(this).data("shown", !status);
  76. return false;
  77. });
  78. //点击切换/排序/删除操作后刷新左侧菜单
  79. $(".btn-change[data-id],.btn-delone,.btn-dragsort").data("success", function (data, ret) {
  80. Fast.api.refreshmenu();
  81. return false;
  82. });
  83. });
  84. //批量删除后的回调
  85. $(".toolbar > .btn-del,.toolbar .btn-more~ul>li>a").data("success", function (e) {
  86. Fast.api.refreshmenu();
  87. });
  88. //展开隐藏一级
  89. $(document.body).on("click", ".btn-toggle", function (e) {
  90. $("a.btn[data-id][data-pid][data-pid!=0].disabled").closest("tr").hide();
  91. var that = this;
  92. var show = $("i", that).hasClass("fa-chevron-down");
  93. $("i", that).toggleClass("fa-chevron-down", !show);
  94. $("i", that).toggleClass("fa-chevron-up", show);
  95. $("a.btn[data-id][data-pid][data-pid!=0]").not('.disabled').closest("tr").toggle(show);
  96. $(".btn-node-sub[data-pid=0]").data("shown", show);
  97. });
  98. //展开隐藏全部
  99. $(document.body).on("click", ".btn-toggle-all", function (e) {
  100. var that = this;
  101. var show = $("i", that).hasClass("fa-plus");
  102. $("i", that).toggleClass("fa-plus", !show);
  103. $("i", that).toggleClass("fa-minus", show);
  104. $(".btn-node-sub.disabled").closest("tr").toggle(show);
  105. $(".btn-node-sub").data("shown", show);
  106. });
  107. },
  108. add: function () {
  109. Controller.api.bindevent();
  110. },
  111. edit: function () {
  112. Controller.api.bindevent();
  113. },
  114. api: {
  115. formatter: {
  116. title: function (value, row, index) {
  117. value = value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
  118. return !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
  119. },
  120. name: function (value, row, index) {
  121. return !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
  122. },
  123. icon: function (value, row, index) {
  124. return '<span class="' + (!row.ismenu || row.status == 'hidden' ? 'text-muted' : '') + '"><i class="' + value + '"></i></span>';
  125. },
  126. subnode: function (value, row, index) {
  127. return '<a href="javascript:;" data-toggle="tooltip" title="' + __('Toggle sub menu') + '" data-id="' + row.id + '" data-pid="' + row.pid + '" class="btn btn-xs '
  128. + (row.haschild == 1 || row.ismenu == 1 ? 'btn-success' : 'btn-default disabled') + ' btn-node-sub"><i class="fa fa-sitemap"></i></a>';
  129. }
  130. },
  131. bindevent: function () {
  132. $(document).on('click', "input[name='row[ismenu]']", function () {
  133. var name = $("input[name='row[name]']");
  134. name.prop("placeholder", $(this).val() == 1 ? name.data("placeholder-menu") : name.data("placeholder-node"));
  135. });
  136. $("input[name='row[ismenu]']:checked").trigger("click");
  137. var iconlist = [];
  138. var iconfunc = function () {
  139. Layer.open({
  140. type: 1,
  141. area: ['99%', '98%'], //宽高
  142. content: Template('chooseicontpl', {iconlist: iconlist})
  143. });
  144. };
  145. Form.api.bindevent($("form[role=form]"), function (data) {
  146. Fast.api.refreshmenu();
  147. });
  148. $(document).on('click', ".btn-search-icon", function () {
  149. if (iconlist.length == 0) {
  150. $.get(Config.site.cdnurl + "/assets/libs/font-awesome/less/variables.less", function (ret) {
  151. var exp = /fa-var-(.*):/ig;
  152. var result;
  153. while ((result = exp.exec(ret)) != null) {
  154. iconlist.push(result[1]);
  155. }
  156. iconfunc();
  157. });
  158. } else {
  159. iconfunc();
  160. }
  161. });
  162. $(document).on('click', '#chooseicon ul li', function () {
  163. $("input[name='row[icon]']").val('fa fa-' + $(this).data("font"));
  164. Layer.closeAll();
  165. });
  166. $(document).on('keyup', 'input.js-icon-search', function () {
  167. $("#chooseicon ul li").show();
  168. if ($(this).val() != '') {
  169. $("#chooseicon ul li:not([data-font*='" + $(this).val() + "'])").hide();
  170. }
  171. });
  172. }
  173. }
  174. };
  175. return Controller;
  176. });