leftMenu.jsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import React from "react";
  2. import { Menu, Icon, Layout } from "antd";
  3. import "@/account/leftTab.less";
  4. const SubMenu = Menu.SubMenu;
  5. import { Link } from "react-router";
  6. import { adminMenu } from "../menu";
  7. import logo from "img/acc-logo.png";
  8. const LeftTab = React.createClass({
  9. getInitialState() {
  10. return {
  11. openKeys: ["sub1"],
  12. current: "index",
  13. switch: false,
  14. rootSubmenuKeys: [
  15. "advertisementList",
  16. "proservlist",
  17. "buyList",
  18. "firmList",
  19. "talentsList",
  20. "activity"
  21. ]
  22. };
  23. },
  24. menuClick(e) {
  25. this.setState({
  26. current: e.key
  27. });
  28. window.location.hash = e.key;
  29. },
  30. iconFun() {
  31. let menuW = document.querySelector(".ant-layout-sider");
  32. if (this.state.switch) {
  33. menuW.style.width = "200px";
  34. menuW.style.flex = "0 0 200px";
  35. } else {
  36. menuW.style.width = "50px";
  37. menuW.style.flex = "0 0 50px";
  38. }
  39. this.setState({
  40. switch: !this.state.switch
  41. });
  42. },
  43. onOpenChange(openKeys) {
  44. const latestOpenKey = openKeys.find(
  45. key => this.state.openKeys.indexOf(key) === -1
  46. );
  47. if (this.state.rootSubmenuKeys.indexOf(latestOpenKey) === -1) {
  48. this.setState({ openKeys });
  49. } else {
  50. this.setState({
  51. openKeys: latestOpenKey ? [latestOpenKey] : []
  52. });
  53. }
  54. },
  55. componentWillMount() {
  56. if (window.location.hash) {
  57. let current = window.location.hash.substr(2),
  58. open = [];
  59. if (!current.length) {
  60. window.location.hash = "index";
  61. this.setState({
  62. openKeys: ["advertisementList"],
  63. current: "advertisementList"
  64. });
  65. } else {
  66. adminMenu.map(item => {
  67. if (item.children) {
  68. item.children.map(atem => {
  69. if (current == atem.url) {
  70. open.push(item.url);
  71. return false;
  72. }
  73. });
  74. }
  75. });
  76. this.setState({
  77. openKeys: open,
  78. current
  79. });
  80. }
  81. } else {
  82. window.location.hash = "index";
  83. this.setState({
  84. openKeys: ["advertisementList"],
  85. current: "advertisementList"
  86. });
  87. }
  88. },
  89. render() {
  90. return (
  91. <div>
  92. <div className="acc-index">
  93. {!this.state.switch && <img src={logo} alt="" />}
  94. {!this.state.switch && (
  95. <span>
  96. <a href={globalConfig.context + "/admin/index.html"}>
  97. 管理员中心
  98. </a>
  99. </span>
  100. )}
  101. {/* <Icon type={this.state.switch ? 'forward' : 'backward'} className="icon" onClick={this.iconFun} /> */}
  102. </div>
  103. <Menu
  104. onClick={this.menuClick}
  105. defaultOpenKeys={this.state.openKeys}
  106. selectedKeys={[this.state.current]}
  107. openKeys={this.state.openKeys}
  108. onOpenChange={this.onOpenChange}
  109. mode={this.state.switch ? "vertical" : "inline"}
  110. theme="dark"
  111. className="account-left"
  112. >
  113. {adminMenu.map(subMenu => {
  114. if (subMenu.children && subMenu.children.length) {
  115. return (
  116. <SubMenu
  117. key={subMenu.url}
  118. title={
  119. <span>
  120. <Icon type={subMenu.icon} />
  121. {!this.state.switch && <span>{subMenu.name}</span>}
  122. </span>
  123. }
  124. >
  125. {subMenu.children.map(menu => (
  126. <Menu.Item key={menu.url}>{menu.name}</Menu.Item>
  127. ))}
  128. </SubMenu>
  129. );
  130. }
  131. return (
  132. <Menu.Item key={subMenu.url}>
  133. <Icon type={subMenu.icon} />
  134. <span className="nav-text">{subMenu.name}</span>
  135. </Menu.Item>
  136. );
  137. })}
  138. </Menu>
  139. </div>
  140. );
  141. }
  142. });
  143. export default LeftTab;