You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

158 lines
6.7 KiB

  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function ($, undefined, Backend, Table, Form, undefined) {
  2. //读取选中的条目
  3. $.jstree.core.prototype.get_all_checked = function (full) {
  4. var obj = this.get_selected(), i, j;
  5. for (i = 0, j = obj.length; i < j; i++) {
  6. obj = obj.concat(this.get_node(obj[i]).parents);
  7. }
  8. obj = $.grep(obj, function (v, i, a) {
  9. return v != '#';
  10. });
  11. obj = obj.filter(function (itm, i, a) {
  12. return i == a.indexOf(itm);
  13. });
  14. return full ? $.map(obj, $.proxy(function (i) {
  15. return this.get_node(i);
  16. }, this)) : obj;
  17. };
  18. var Controller = {
  19. index: function () {
  20. // 初始化表格参数配置
  21. Table.api.init({
  22. extend: {
  23. "index_url": "auth/group/index",
  24. "add_url": "auth/group/add",
  25. "edit_url": "auth/group/edit",
  26. "del_url": "auth/group/del",
  27. "multi_url": "auth/group/multi",
  28. }
  29. });
  30. var table = $("#table");
  31. //在表格内容渲染完成后回调的事件
  32. table.on('post-body.bs.table', function (e, json) {
  33. $("tbody tr[data-index]", this).each(function () {
  34. if (Config.admin.group_ids.indexOf(parseInt(parseInt($("td:eq(1)", this).text()))) > -1) {
  35. $("input[type=checkbox]", this).prop("disabled", true);
  36. }
  37. });
  38. });
  39. // 初始化表格
  40. table.bootstrapTable({
  41. url: $.fn.bootstrapTable.defaults.extend.index_url,
  42. escape: false,
  43. columns: [
  44. [
  45. {field: 'state', checkbox: true,},
  46. {field: 'id', title: 'ID'},
  47. {field: 'pid', title: __('Parent')},
  48. {field: 'name', title: __('Name'), align: 'left'},
  49. {field: 'status', title: __('Status'), formatter: Table.api.formatter.status},
  50. {
  51. field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (value, row, index) {
  52. if (Config.admin.group_ids.indexOf(parseInt(row.id)) > -1) {
  53. return '';
  54. }
  55. return Table.api.formatter.operate.call(this, value, row, index);
  56. }
  57. }
  58. ]
  59. ],
  60. pagination: false,
  61. search: false,
  62. commonSearch: false,
  63. });
  64. // 为表格绑定事件
  65. Table.api.bindevent(table);//当内容渲染完成后
  66. },
  67. add: function () {
  68. Controller.api.bindevent();
  69. },
  70. edit: function () {
  71. Controller.api.bindevent();
  72. },
  73. api: {
  74. bindevent: function () {
  75. Form.api.bindevent($("form[role=form]"), null, null, function () {
  76. if ($("#treeview").size() > 0) {
  77. var r = $("#treeview").jstree("get_all_checked");
  78. $("input[name='row[rules]']").val(r.join(','));
  79. }
  80. return true;
  81. });
  82. //渲染权限节点树
  83. //变更级别后需要重建节点树
  84. $(document).on("change", "select[name='row[pid]']", function () {
  85. var pid = $(this).data("pid");
  86. var id = $(this).data("id");
  87. if ($(this).val() == id) {
  88. $("option[value='" + pid + "']", this).prop("selected", true).change();
  89. Backend.api.toastr.error(__('Can not change the parent to self'));
  90. return false;
  91. }
  92. $.ajax({
  93. url: "auth/group/roletree",
  94. type: 'post',
  95. dataType: 'json',
  96. data: {id: id, pid: $(this).val()},
  97. success: function (ret) {
  98. if (ret.hasOwnProperty("code")) {
  99. var data = ret.hasOwnProperty("data") && ret.data != "" ? ret.data : "";
  100. if (ret.code === 1) {
  101. //销毁已有的节点树
  102. $("#treeview").jstree("destroy");
  103. Controller.api.rendertree(data);
  104. } else {
  105. Backend.api.toastr.error(ret.msg);
  106. }
  107. }
  108. }, error: function (e) {
  109. Backend.api.toastr.error(e.message);
  110. }
  111. });
  112. });
  113. //全选和展开
  114. $(document).on("click", "#checkall", function () {
  115. $("#treeview").jstree($(this).prop("checked") ? "check_all" : "uncheck_all");
  116. });
  117. $(document).on("click", "#expandall", function () {
  118. $("#treeview").jstree($(this).prop("checked") ? "open_all" : "close_all");
  119. });
  120. $("select[name='row[pid]']").trigger("change");
  121. },
  122. rendertree: function (content) {
  123. $("#treeview")
  124. .on('redraw.jstree', function (e) {
  125. $(".layer-footer").attr("domrefresh", Math.random());
  126. })
  127. .jstree({
  128. "themes": {"stripes": true},
  129. "checkbox": {
  130. "keep_selected_style": false,
  131. },
  132. "types": {
  133. "root": {
  134. "icon": "fa fa-folder-open",
  135. },
  136. "menu": {
  137. "icon": "fa fa-folder-open",
  138. },
  139. "file": {
  140. "icon": "fa fa-file-o",
  141. }
  142. },
  143. "plugins": ["checkbox", "types"],
  144. "core": {
  145. 'check_callback': true,
  146. "data": content
  147. }
  148. });
  149. }
  150. }
  151. };
  152. return Controller;
  153. });