{
 "hidden": false,
 "children": [{
  "configs": {"itemId": "module"},
  "expanded": true,
  "children": [{
   "configs": {
    "layout": "border",
    "itemId": "viewport1"
   },
   "expanded": true,
   "children": [
    {
     "configs": {
      "itemId": "roles",
      "split": "true",
      "width": "200",
      "region": "west",
      "title": "角色列表",
      "collapsible": "true",
      "rootVisible": "false"
     },
     "expanded": true,
     "children": [{
      "configs": {
       "itemId": "store",
       "url": "m?xwl=admin/perm/role-list"
      },
      "expanded": false,
      "children": [],
      "type": "treestore"
     }],
     "type": "tree",
     "events": {"selectionchange": "var role = selected[0];\
if (role) {\
  app.modules.setDisabled(false);\
  app.currentRole = role.data.ROLE_ID;\
  app.setModules(app.modules.getRootNode());\
//   app.modules.store.load({\
//     params:{roleId:role.data.ROLE_ID}\
//   });\
  app.setModulesChecked(role.data.ROLE_ID);\
  \
  app.role_id.setValue(role.data.ROLE_ID);\
  app.gridMenuRes.store.load();\
}"}
    },
    {
     "configs": {
      "layout": "border",
      "border": "true",
      "itemId": "paneltoal",
      "region": "center"
     },
     "expanded": true,
     "children": [
      {
       "configs": {
        "layout": "fit",
        "border": "false",
        "itemId": "panelMenu",
        "region": "north",
        "height": "350",
        "frame": "true"
       },
       "expanded": true,
       "children": [{
        "configs": {
         "border": "true",
         "itemId": "modules",
         "root": "{\
  fileName: 'Root'\
}",
         "disabled": "true",
         "region": "center",
         "rootVisible": "false",
         "multiSelect": "true"
        },
        "expanded": true,
        "children": [
         {
          "configs": {
           "itemId": "store",
           "fields": "['text', 'path','MENU_ID','fileName']",
           "url": "m?xwl=admin/perm/module-list"
          },
          "expanded": false,
          "children": [],
          "type": "treestore",
          "events": {"beforeload": "operation.params.path = operation.node.data.path;"}
         },
         {
          "configs": {
           "itemId": "tbar",
           "enableOverflow": "true"
          },
          "expanded": true,
          "children": [
           {
            "configs": {
             "itemId": "permitBatchBtn",
             "tooltip": "允许访问选择的所有模块 (Ctrl/Shift 多选)",
             "text": "批量允许",
             "iconCls": "application_add_icon"
            },
            "expanded": true,
            "children": [],
            "type": "item",
            "events": {"click": "app.setPerms(app.modules.getSelection(), true);"}
           },
           {
            "configs": {
             "itemId": "prohibitBatchBtn",
             "tooltip": "禁止访问选择的所有模块 (Ctrl/Shift 多选)",
             "text": "批量禁止",
             "iconCls": "application_delete_icon"
            },
            "expanded": true,
            "children": [],
            "type": "item",
            "events": {"click": "app.setPerms(app.modules.getSelection(), false);"}
           },
           {
            "configs": {
             "itemId": "expandAllBtn",
             "tooltip": "展开选择模块的全部子模块",
             "text": "全部展开",
             "iconCls": "plus_icon"
            },
            "expanded": false,
            "children": [],
            "type": "item",
            "events": {"click": "Wb.mask(null, null, 1);\
setTimeout(function() {\
  Wb.expand(app.modules);\
  Wb.unmask();\
}, 50);"}
           },
           {
            "configs": {
             "itemId": "collapseAllBtn",
             "tooltip": "收缩选择模块的全部子模块",
             "text": "全部收缩",
             "iconCls": "minus_icon"
            },
            "expanded": false,
            "children": [],
            "type": "item",
            "events": {"click": "Wb.collapse(app.modules);"}
           },
           {
            "configs": {
             "itemId": "combo",
             "labelAlign": "right",
             "bindField": "COD",
             "fieldLabel": "模块",
             "displayField": "NAME",
             "labelWidth": "40",
             "valueField": "COD",
             "height": "22"
            },
            "expanded": true,
            "children": [{
             "configs": {
              "itemId": "store",
              "url": "m?xwl=admin/perm/selectWbMenu"
             },
             "expanded": false,
             "children": [],
             "type": "store"
            }],
            "type": "combo",
            "events": {
             "beforequery": "// if(app.combo.getValue()== null ){\
//   app.combo.setValue(-1);\
// }\
\
",
             "select": "this.collapse();\
//选择任意一个节点Select any node\
var postion = app.modules.getRootNode();\
\
\
Wb.request({\
  url: 'm?xwl=admin/perm/check',\
  params: {\
    menuId: app.combo.getValue()\
  },\
  success: function(resp) {\
    var s = Wb.decode(resp.responseText);\
    var postion = app.modules.getRootNode();\
\
    //设置默认只筛选PC端菜单\
    postion = postion.childNodes[0];\
\
    for (var i = 0; i < s.length; i++) {\
      postion = postion.childNodes[s[i]];\
    }\
\
    Wb.selectAnyNode(app.modules, postion);\
  }\
});"
            }
           },
           {
            "configs": {
             "itemId": "nowRole",
             "text": "立即生效",
             "iconCls": "approval_icon"
            },
            "expanded": false,
            "children": [],
            "type": "item",
            "events": {"click": "\
Wb.requestAg({\
  params: {\
    bean: \"roleByRedisService\",\
    method: \"putRole\" //业务方法名 \
  },\
  success: function(resp) {\
    Wb.warn('权限更新完成!');\
    //app.findBtn.fireEvent('click');\
  }\
});"}
           }
          ],
          "type": "toolbar"
         }
        ],
        "type": "tree",
        "events": {
         "selectionchange": "var module = selected[0];\
//console.log(module);\
app.allows.setDisabled(!module);\
if (module) {\
  //app.allows.setDisabled(false);\
  app.currentModule = module.data.path;\
  Wb.setTitle(app.allows, module.data.text);\
  //app.setRoles(module);\
//   app.allows.store.load({\
//      params:{menuId:module.data.MENU_ID}\
//   });\
  app.setAllowsChecked(module.data.MENU_ID);\
  app.parent_menu_id.setValue(module.data.MENU_ID);\
  \
  app.gridMenuRes.store.load({\
    params:{parentMenuId:module.data.MENU_ID,roleId:app.role_id.getValue()}\
});\
}\
",
         "checkchange": "Wb.setChecked(node, !checked, app.modules);\
app.setPerms([node], checked);\
",
         "itemexpand": "if (app.currentRole)\
  app.setModules(node); //必须每次展现后进行设置，因为收缩时跳过设置\
"
        }
       }],
       "type": "panel"
      },
      {
       "configs": {
        "layout": "fit",
        "border": "false",
        "itemId": "panelMenuRes",
        "region": "center",
        "frame": "true"
       },
       "expanded": true,
       "children": [{
        "configs": {
         "border": "true",
         "itemId": "gridMenuRes",
         "selType": "checkboxmodel",
         "editable": "true",
         "pagingBar": "false"
        },
        "expanded": true,
        "children": [
         {
          "configs": {
           "itemId": "store",
           "url": "m?xwl=admin/perm/selectWbMenuRes"
          },
          "expanded": false,
          "children": [],
          "type": "store"
         },
         {
          "configs": {
           "itemId": "tbar",
           "region": "north"
          },
          "expanded": true,
          "children": [
           {
            "configs": {
             "itemId": "menu_name_res",
             "labelAlign": "right",
             "fieldLabel": "名称",
             "labelWidth": "40"
            },
            "expanded": false,
            "children": [],
            "type": "text"
           },
           {
            "configs": {
             "itemId": "findBtn",
             "text": "查询",
             "iconCls": "search_icon"
            },
            "expanded": false,
            "children": [],
            "type": "item",
            "events": {"click": "app.gridMenuRes.store.load({\
  params: {\
    parentMenuId: app.parent_menu_id.getValue(),\
    menuNameRes: app.menu_name_res.getValue(),\
    roleId: app.role_id.getValue()\
  }\
});"}
           },
           {
            "configs": {
             "itemId": "addBtn",
             "text": "授权",
             "iconCls": "user_add_icon"
            },
            "expanded": false,
            "children": [],
            "type": "item",
            "events": {"click": "\
var rows = app.gridMenuRes.getSelection();\
if (rows.length<1) {\
  Wb.warn('请选择至少1条数据');\
  return;\
}\
var menuIds = '';\
Wb.each(rows, function(row) {\
  menuIds += row.data.MENU_RES_ID +\",\";\
});\
\
Wb.requestAg({\
  params: {\
    roleId:app.role_id.getValue(),\
    menuIds: menuIds,\
    bean: \"permissionService\",\
    method: \"saveWbRoleResourceMethod\" //业务方法名 \
  },\
  success: function(resp) {\
    Wb.warn('授权成功!');\
    app.findBtn.fireEvent('click');\
  }\
});"}
           },
           {
            "configs": {
             "itemId": "delBtn",
             "text": "取消",
             "iconCls": "user_delete_icon"
            },
            "expanded": false,
            "children": [],
            "type": "item",
            "events": {"click": "var rows = app.gridMenuRes.getSelection();\
if (rows.length<1) {\
  Wb.warn('请选择至少1条数据');\
  return;\
}\
var menuIds = '';\
Wb.each(rows, function(row) {\
  menuIds += row.data.MENU_RES_ID +\",\";\
});\
\
Wb.requestAg({\
  params: {\
    roleId:app.role_id.getValue(),\
    menuIds: menuIds,\
    bean: \"permissionService\",\
    method: \"delWbRoleResourceMethod\" //业务方法名 \
  },\
  success: function(resp) {\
    Wb.warn('取消授权成功!');\
    app.findBtn.fireEvent('click');\
  }\
});"}
           },
           {
            "configs": {
             "itemId": "parent_menu_id",
             "hidden": "true"
            },
            "expanded": false,
            "children": [],
            "type": "text"
           },
           {
            "configs": {
             "itemId": "role_id",
             "hidden": "true"
            },
            "expanded": false,
            "children": [],
            "type": "text"
           },
           {
            "configs": {
             "itemId": "IDS",
             "hidden": "true"
            },
            "expanded": false,
            "children": [],
            "type": "text"
           }
          ],
          "type": "toolbar"
         },
         {
          "configs": {"itemId": "columns"},
          "expanded": true,
          "children": [
           {
            "configs": {
             "itemId": "id",
             "hidden": "true",
             "dataIndex": "MENU_RES_ID",
             "align": "center",
             "titleAlign": "center"
            },
            "expanded": false,
            "children": [],
            "type": "column"
           },
           {
            "configs": {
             "itemId": "menuResEname",
             "dataIndex": "MENU_RES_ENAME",
             "width": "300",
             "text": "英文名称",
             "align": "center",
             "titleAlign": "center"
            },
            "expanded": true,
            "children": [],
            "type": "column"
           },
           {
            "configs": {
             "itemId": "menuResCname",
             "dataIndex": "MENU_RES_CNAME",
             "width": "200",
             "text": "中文名称",
             "align": "center",
             "titleAlign": "center"
            },
            "expanded": true,
            "children": [],
            "type": "column"
           },
           {
            "configs": {
             "itemId": "menuResType",
             "renderer": "if(value=='tbr'){\
  return '按钮';\
}\
if(value=='panel'){\
  return '面板';\
}\
",
             "dataIndex": "MENU_RES_TYPE",
             "text": "类型",
             "align": "center",
             "titleAlign": "center"
            },
            "expanded": true,
            "children": [],
            "type": "column"
           },
           {
            "configs": {
             "itemId": "menuResFlag",
             "xtype": "checkcolumn",
             "dataIndex": "MENU_RES_FLAG",
             "disabled": "true",
             "text": "状态",
             "align": "center",
             "titleAlign": "center"
            },
            "expanded": true,
            "children": [],
            "type": "column"
           }
          ],
          "type": "array"
         }
        ],
        "type": "grid"
       }],
       "type": "panel"
      }
     ],
     "type": "panel"
    },
    {
     "configs": {
      "itemId": "allows",
      "split": "true",
      "width": "260",
      "disabled": "true",
      "region": "east",
      "title": "可访问",
      "collapsible": "true",
      "rootVisible": "false"
     },
     "expanded": false,
     "children": [{
      "configs": {
       "itemId": "store",
       "url": "m?xwl=admin/perm/module-role-list"
      },
      "expanded": false,
      "children": [],
      "type": "treestore"
     }],
     "type": "tree",
     "events": {"checkchange": "Wb.setChecked(node, !checked, app.allows);\
app.setAllowsPerms([app.modules.getSelection()[0]], checked, node.data.ROLE_ID);"}
    }
   ],
   "type": "viewport"
  }],
  "type": "module",
  "events": {"initialize": "Wb.apply(app, {\
  /**\
   * 获取所有模块的权限数据。\
   */\
  getPerms: function() {\
    Wb.request({\
      url: 'm?xwl=admin/perm/get-perm',\
      success: function(resp) {\
        app.rolesList = Wb.decode(resp.responseText);\
      }\
    });\
  },\
  /** \
   * 设置批量模块权限，并立即设置写入后台。\
   * @param {NodeInterface[]} choice 选择的节点列表。\
   * @param {Boolean} checked 选择状态。\
   * @param {String [setRole] 设置的角色，默认为角色列表选择的角色。\
   */\
  setPerms: function(choice, checked, setRole) {\
    var sels = app.modules.getSelection();\
    if (checked) {\
      var s = \"\";\
      for (var i = 0; i < sels.length; i++) {\
        s = sels[i];\
        //更新是否选中\
        // Wb.update(s,{checked:true});\
        s.set('checked', true);\
        Wb.request({\
          url: 'm?xwl=admin/perm/insert-perms',\
          params: {\
            path: Wb.pluck(sels, 'path'),\
            role: app.currentRole,\
            checked: checked,\
            ROLE_ID: app.currentRole,\
            MENU_ID: sels[i].data.MENU_ID\
          },\
          success: function() {\
            //               var roles, path;\
            //               Ext.suspendLayouts();\
            //               Wb.each(sels, function(node) {\
            //                 if (!node.isLeaf())\
            //                   return;\
            //                 path = node.data.path;\
            //                 roles = app.rolesList[path];\
            //                 if (checked) {\
            //                   if (!roles) {\
            //                     roles = {};\
            //                     app.rolesList[path] = roles;\
            //                   }\
            //                   roles[app.currentRole] = 1;\
            //                 } else {\
            //                   if (roles)\
            //                     delete roles[app.currentRole];\
            //                 }\
            //                 Wb.setChecked(node, checked);\
            //                 app.setRoles(node);\
            //               });\
            //               Ext.resumeLayouts(true);\
          }\
        });\
      }\
    }\
    if (!checked) {\
      var sj = \"\";\
      for (var j = 0; j < sels.length; j++) {\
        sj = sels[j];\
        //更新是否选中\
        //Wb.update(sj,{checked:false});\
        sj.set('checked', false);\
        Wb.request({\
          url: 'm?xwl=admin/perm/delete-perms',\
          params: {\
            path: Wb.pluck(sels, 'path'),\
            role: app.currentRole,\
            checked: checked,\
            ROLE_ID: app.currentRole,\
            MENU_ID: sels[j].data.MENU_ID\
          },\
          success: function() {\
\
            //               app.modules.store.load({\
            //                 params:{roleId:app.currentRole}\
            //               });\
\
            //               var roles, path;\
            //               Ext.suspendLayouts();\
            //               Wb.each(sels, function(node) {\
            //                 if (!node.isLeaf())\
            //                   return;\
            //                 path = node.data.path;\
            //                 roles = app.rolesList[path];\
            //                 if (checked) {\
            //                   if (!roles) {\
            //                     roles = {};\
            //                     app.rolesList[path] = roles;\
            //                   }\
            //                   roles[app.currentRole] = 1;\
            //                 } else {\
            //                   if (roles)\
            //                     delete roles[app.currentRole];\
            //                 }\
            //                 Wb.setChecked(node, checked);\
            //                 app.setRoles(node);\
            //               });\
            //               Ext.resumeLayouts(true);\
          }\
        });\
      }\
    }\
  },\
\
  /**\
   * 设置批量角色模块权限，并立即设置写入后台。\
   * @param {NodeInterface[]} choice 选择的节点列表。\
   * @param {Boolean} checked 选择状态。\
   * @param {String [setRole] 设置的角色，默认为角色列表选择的角色。\
   */\
  setAllowsPerms: function(choice, checked, setRole) {\
    //  console.log(choice[0].data.MENU_ID+'=='+choice[0].data.checked);\
    if (choice[0].data.checked) {\
      var sel = app.allows.getSelection();\
      if (!sel[0].data.checked) {\
        sel[0].set('checked', true);\
        Wb.request({\
          url: 'm?xwl=admin/perm/insert-perms',\
          params: {\
            path: Wb.pluck(sel, 'path'),\
            role: app.currentRole,\
            checked: checked,\
            ROLE_ID: sel[0].data.ROLE_ID,\
            MENU_ID: choice[0].data.MENU_ID\
          },\
          success: function() {}\
        });\
      } else {\
        sel[0].set('checked', false);\
        Wb.request({\
          url: 'm?xwl=admin/perm/delete-perms',\
          params: {\
            path: Wb.pluck(sel, 'path'),\
            role: app.currentRole,\
            checked: checked,\
            ROLE_ID: sel[0].data.ROLE_ID,\
            MENU_ID: choice[0].data.MENU_ID\
          },\
          success: function() {}\
        });\
      }\
    }\
  },\
  /**\
   * 设置指定节点及其所有子节点的模块权限视图。\
   * @param {NodeInterface} [base] 指定设置该节点及其所有子节点。如果为空设置所有节点。\
   * @return {Boolean} 节点及其所有子节点是否被选中。如果节点同时存在选中和非选中节点返回null。\
   */\
  setModules: function(base) {\
    var hasPerm, roles,\
      rolesList = app.rolesList;\
    Ext.suspendLayouts();\
    (base || app.modules.getRootNode()).cascadeBy(function(node) {\
      if (!node.data.depth || !node.isLeaf())\
        return;\
      roles = rolesList[node.data.path];\
      hasPerm = roles && roles[app.currentRole] == 1;\
      Wb.setChecked(node, hasPerm);\
    });\
    Ext.resumeLayouts(true);\
  },\
  /**\
   * 设置指定模块可访问角色的权限视图。\
   * @param {NodeInterface} node 模块节点。\
   */\
  setRoles: function(node) {\
    function isChecked(role) {\
      var roles, hasPerm, checked,\
        allUnchecked = true,\
        allChecked = true,\
        rolesList = app.rolesList;\
      node.cascadeBy(function(child) {\
        if (child.isLeaf()) {\
          roles = rolesList[child.data.path];\
          hasPerm = roles && roles[role] == 1;\
          if (hasPerm)\
            allUnchecked = false;\
          else\
            allChecked = false;\
        }\
      });\
      if (allChecked)\
        checked = true;\
      else if (allUnchecked)\
        checked = false;\
      else\
        checked = null;\
      return checked;\
    }\
    Ext.suspendLayouts();\
    app.allows.getRootNode().eachChild(function(child) {\
      Wb.setChecked(child, isChecked(child.data.ROLE_ID), app.allows);\
    });\
    Ext.resumeLayouts(true);\
  },\
\
  /**\
   *清理所有的菜单、角色状态\
   *node传入的id\
   */\
  clearCheck: function(node) {\
    if (node.data.checked)\
      Wb.setChecked(node, false,app.modules);\
    for (var n in node.childNodes) {\
      app.clearCheck(node.childNodes[n]);\
    }\
\
  },\
\
  /**\
   *根据角色设置功能\
   */\
  setModulesChecked: function(roleId) {\
    Wb.request({\
      url: 'm?xwl=admin/perm/module-list-data',\
      params: {\
        roleId: roleId\
      },\
      success: function(resp) {\
        var root = app.modules.getRootNode();\
        app.clearCheck(root);\
        var s = Wb.decode(resp.responseText);\
        var ids = s.rows.map(function(d) {\
          return d.MENU_ID;\
        });\
        function checkit(node) {\
          var idx = ids.findIndex(function(id) {\
            //console.log(node.data.MENU_ID == id);\
            return node.data.MENU_ID && node.data.MENU_ID == id;\
          });\
          if (idx >= 0) {\
            Wb.setChecked(node,true,app.modules);//node.set('checked', true);\
            ids.splice(idx, 1);\
          }\
          node.childNodes.every(function(n) {\
            checkit(n);\
            return true;\
          });\
        }\
        checkit(root);\
      }\
    });\
  },\
  /**\
   *根据功能查询角色\
   */\
  setAllowsChecked: function(menuId) {\
    //app.clearCheck(app.allows.getRootNode());\
    Wb.request({\
      url: 'm?xwl=admin/perm/module-role-list',\
      params: {\
        menuId: menuId\
      },\
      success: function(resp) {\
        var s = Wb.decode(resp.responseText);\
        var len = s.total;\
        for (var i = 0; i < len; i++) {\
          if (app.allows.getRootNode().childNodes[i].data.ROLE_ID == s.children[i].ROLE_ID) {\
            app.allows.getRootNode().childNodes[i].set('checked', s.children[i].checked);\
          }\
          //console.log(app.allows.getRootNode().childNodes[i].data.ROLE_ID);\
          //console.log(s.children[i].ROLE_ID+'==='+s.children[i].checked);\
        }\
      }\
    });\
  }\
});\
//app.getPerms();"}
 }],
 "roles": {"demo": 1},
 "title": "权限设置",
 "iconCls": "option_icon",
 "inframe": false,
 "pageLink": ""
}