group.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. search: false,
  23. advancedSearch: false,
  24. pagination: false,
  25. extend: {
  26. "index_url": "auth/group/index",
  27. "add_url": "auth/group/add",
  28. "edit_url": "auth/group/edit",
  29. "del_url": "auth/group/del",
  30. "multi_url": "auth/group/multi",
  31. }
  32. });
  33. var table = $("#table");
  34. // 初始化表格
  35. table.bootstrapTable({
  36. url: $.fn.bootstrapTable.defaults.extend.index_url,
  37. columns: [
  38. [
  39. {field: 'state', checkbox: true, },
  40. {field: 'id', title: 'ID'},
  41. {field: 'pid', title: __('Parent')},
  42. {field: 'name', title: __('Name'), align: 'left'},
  43. {field: 'status', title: __('Status'), formatter: Table.api.formatter.status},
  44. {field: 'operate', title: __('Operate'), events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  45. ]
  46. ]
  47. });
  48. // 为表格绑定事件
  49. Table.api.bindevent(table);//当内容渲染完成后
  50. },
  51. add: function () {
  52. Form.api.bindevent($("form[role=form]"));
  53. Controller.api.bindevent();
  54. },
  55. edit: function () {
  56. Form.api.bindevent($("form[role=form]"));
  57. Controller.api.bindevent();
  58. },
  59. api: {
  60. refreshrules: function () {
  61. if ($("#treeview").size() > 0) {
  62. var r = $("#treeview").jstree("get_all_checked");
  63. $("input[name='row[rules]']").val(r.join(','));
  64. }
  65. return true;
  66. },
  67. bindevent: function () {
  68. Form.api.custom.refreshrules = Controller.api.refreshrules;
  69. //渲染权限节点树
  70. //变更级别后需要重建节点树
  71. $(document).on("change", "select[name='row[pid]']", function () {
  72. var pid = $(this).data("pid");
  73. var id = $(this).data("id");
  74. if ($(this).val() == id) {
  75. $("option[value='" + pid + "']", this).prop("selected", true).change();
  76. Backend.api.toastr.error(__('Can not change the parent to self'));
  77. return false;
  78. }
  79. $.ajax({
  80. url: "ajax/roletree",
  81. type: 'post',
  82. dataType: 'json',
  83. data: {id: id, pid: $(this).val()},
  84. success: function (ret) {
  85. if (ret.hasOwnProperty("code")) {
  86. var data = ret.hasOwnProperty("data") && ret.data != "" ? ret.data : "";
  87. if (ret.code === 1) {
  88. //销毁已有的节点树
  89. $("#treeview").jstree("destroy");
  90. Controller.api.rendertree(data);
  91. } else {
  92. Backend.api.toastr.error(ret.data);
  93. }
  94. }
  95. }, error: function (e) {
  96. Backend.api.toastr.error(e.message);
  97. }
  98. });
  99. });
  100. //全选和展开
  101. $(document).on("click", "#checkall", function () {
  102. $("#treeview").jstree($(this).prop("checked") ? "check_all" : "uncheck_all");
  103. });
  104. $(document).on("click", "#expandall", function () {
  105. $("#treeview").jstree($(this).prop("checked") ? "open_all" : "close_all");
  106. });
  107. $("select[name='row[pid]']").trigger("change");
  108. },
  109. rendertree: function (content) {
  110. $("#treeview")
  111. .on('redraw.jstree', function (e) {
  112. $(".layer-footer").attr("domrefresh", Math.random());
  113. })
  114. .jstree({
  115. "themes": {"stripes": true},
  116. "checkbox": {
  117. "keep_selected_style": false,
  118. },
  119. "types": {
  120. "root": {
  121. "icon": "fa fa-folder-open",
  122. },
  123. "menu": {
  124. "icon": "fa fa-folder-open",
  125. },
  126. "file": {
  127. "icon": "fa fa-file-o",
  128. }
  129. },
  130. "plugins": ["checkbox", "types"],
  131. "core": {
  132. 'check_callback': true,
  133. "data": content
  134. }
  135. });
  136. }
  137. }
  138. };
  139. return Controller;
  140. });