group.js 6.7 KB

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