rule.js 9.9 KB

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