rule.js 9.9 KB

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