MenuWidget.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. <template>
  2. <div class="menu-side left_col scroll-view">
  3. <div class="navbar nav_title" style="border: 0">
  4. <a href="#/desktop/dashboard" class="site_title"><img src="/static/assets/client-base-v4/image/template-logo_42.png" /></a>
  5. </div>
  6. <div class="clearfix" />
  7. <div id="sidebar-menu" class="main_menu_side hidden-print main_menu">
  8. <div class="menu_section">
  9. <div class="menu-div">
  10. <input
  11. v-model="searchText"
  12. autocomplete="off"
  13. type="text"
  14. class="form-control menu-search"
  15. :placeholder="$t('lang.menuWidget.searchMenu')"
  16. />
  17. </div>
  18. <ul class="nav menu-nav side-menu">
  19. <template v-for="rootNode in rootNodes">
  20. <li
  21. v-if="rootNode.show"
  22. :key="rootNode.uuid"
  23. :name="rootNode.no"
  24. :model="rootNode"
  25. :class="{ active: selectedNode === rootNode }"
  26. >
  27. <a @click="selectMenuNode(rootNode)">
  28. <i :class="rootNode.icon" />
  29. {{ Language.getMenuNameTrl($i18n.locale, rootNode) }}
  30. <span class="fa fa-chevron-down" />
  31. </a>
  32. <ul
  33. class="nav menu-nav child_menu"
  34. :class="{ active: rootNode.expand == true }"
  35. >
  36. <template v-for="subMenuItem in rootNode.subMenus" :key="subMenuItem.uuid">
  37. <MenuNode
  38. :model="subMenuItem"
  39. @select-node="selectNode($event, rootNode.subMenus)"
  40. />
  41. </template>
  42. </ul>
  43. </li>
  44. </template>
  45. </ul>
  46. </div>
  47. </div>
  48. </div>
  49. </template>
  50. <script>
  51. import Common from '../common/Common.js';
  52. import Language from '../common/Language.js';
  53. import MenuNode from './MenuNode.vue';
  54. import { Uuid } from 'pc-component-v3';
  55. export default {
  56. components: {
  57. MenuNode,
  58. },
  59. data: function () {
  60. this.Language = Language;
  61. return {
  62. rootNodes: [],
  63. selectedNode: {}, // 选择的节点
  64. searchText: '',
  65. };
  66. },
  67. watch: {
  68. searchText: function (newVal, oldVal) {
  69. this.search(newVal);
  70. },
  71. },
  72. mounted: function () {
  73. var _self = this;
  74. this.loadMenuData();
  75. },
  76. methods: {
  77. /// 加载数据
  78. loadMenuData: function () {
  79. console.log('loadMenuData');
  80. var _self = this;
  81. $.ajax({
  82. url: Common.getApiURL('MenuResourceV2/listCanVist'),
  83. type: 'POST',
  84. dataType: 'json',
  85. beforeSend: function (request) {
  86. Common.addTokenToRequest(request);
  87. },
  88. success: function (data) {
  89. $.ajax({
  90. url: Common.getApiURL('UserMenuResource/queryAddMenus'),
  91. type: 'get',
  92. dataType: 'json',
  93. beforeSend: function (request) {
  94. Common.addTokenToRequest(request);
  95. },
  96. success: function (data1) {
  97. if(data1 != null){
  98. console.log(data1);
  99. _self.formateCollectionMenuData(data, data1);
  100. }else{
  101. _self.formateMenuData(data);
  102. }
  103. },
  104. error: function (XMLHttpRequest, textStatus, errorThrown) {
  105. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  106. },
  107. });
  108. },
  109. error: function (XMLHttpRequest, textStatus, errorThrown) {
  110. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  111. },
  112. });
  113. },
  114. // 格式化菜单数据
  115. formateMenuData: function (datas) {
  116. var _self = this;
  117. if (datas == null || datas.length == 0) {
  118. return;
  119. }
  120. // 对菜单进行排序和设置expand
  121. function initNode(nodes) {
  122. if (nodes instanceof Array) {
  123. nodes.forEach(function (node) {
  124. node.expand = false;
  125. node.show = true;
  126. node.uuid = Uuid.createUUID();
  127. if (node.subMenus instanceof Array) {
  128. initNode(node.subMenus);
  129. }
  130. });
  131. }
  132. }
  133. initNode(datas);
  134. this.rootNodes = datas;
  135. _self.$nextTick(function () {
  136. _self.initView();
  137. });
  138. },
  139. // 格式化菜单数据
  140. formateCollectionMenuData: function (datas, collections) {
  141. var _self = this;
  142. if ((datas == null || datas.length == 0) && (collections == null || collections.length == 0)) {
  143. return;
  144. }
  145. // 对菜单进行排序和设置expand
  146. function initNode(nodes, collectionDatas) {
  147. if (nodes instanceof Array) {
  148. nodes.forEach(function (node) {
  149. node.expand = false;
  150. node.show = true;
  151. node.uuid = Uuid.createUUID();
  152. collectionDatas.forEach(collectionData => {
  153. if(node.no == collectionData.no){
  154. node.favorite = true;
  155. }
  156. });
  157. if (node.subMenus instanceof Array) {
  158. initNode(node.subMenus, collectionDatas);
  159. }
  160. });
  161. }
  162. }
  163. initNode(datas,collections);
  164. this.rootNodes = datas;
  165. _self.$nextTick(function () {
  166. _self.initView();
  167. });
  168. },
  169. setContentHeight: function () {
  170. var $RIGHT_COL = $('.right_col');
  171. var $BODY = $('body');
  172. var $LEFT_COL = $('.left_col');
  173. var $NAV_MENU = $('.nav_menu');
  174. var bodyHeight = $(window).height();
  175. var leftColHeight = $LEFT_COL.eq(1).height();
  176. var contentHeight =
  177. (bodyHeight < leftColHeight ? leftColHeight : bodyHeight) -
  178. ($NAV_MENU.height() + 2);
  179. $RIGHT_COL.css('min-height', contentHeight);
  180. },
  181. // 初始化界面
  182. initView: function () {
  183. var _self = this;
  184. // 窗口大小改变的时候,重新计算高度
  185. $(window).resize(function () {
  186. _self.setContentHeight();
  187. });
  188. _self.setContentHeight();
  189. },
  190. // 选择菜单节点
  191. selectMenuNode: function (rootNode) {
  192. if (rootNode.expand) {
  193. rootNode.expand = false;
  194. } else {
  195. rootNode.expand = true;
  196. }
  197. },
  198. selectNode: function (node, subMenus) {
  199. if (node.expand) {
  200. subMenus.forEach(function (item) {
  201. if (item != node) {
  202. //这里控制菜单是否可以同时展开
  203. // item.expand = false;
  204. }
  205. });
  206. }
  207. },
  208. /**
  209. * 搜索菜单
  210. */
  211. search: function (inputText) {
  212. //判断一个节点及其子节点是否含有搜索的内容
  213. function filterNode(node) {
  214. var name = node.name;
  215. if (name.indexOf(inputText) != -1) {
  216. node.show = true;
  217. } else {
  218. node.show = false;
  219. }
  220. if (node.subMenus != undefined && node.subMenus instanceof Array) {
  221. node.subMenus.forEach(function (node2) {
  222. filterNode(node2);
  223. });
  224. }
  225. }
  226. /**
  227. * 递归修改属性为可见的节点的父节点为可见
  228. * @param {Object} node
  229. */
  230. function parseParent(node, level) {
  231. // 防止循环数据
  232. if (level > 5) {
  233. return;
  234. }
  235. if (node.subMenus != undefined && node.subMenus instanceof Array) {
  236. node.subMenus.forEach(function (node1) {
  237. parseParent(node1, level + 1);
  238. //递归结束时,设置可见节点的父节点为可见
  239. if (node1.show == true) {
  240. node.show = true;
  241. node.expand = true;
  242. node1.expand = true;
  243. }
  244. });
  245. }
  246. }
  247. var _self = this;
  248. _self.setNodeView();
  249. if (inputText) {
  250. //遍历所有根节点与传销内容无关的设置不可见
  251. this.rootNodes.forEach(function (node) {
  252. filterNode(node);
  253. });
  254. this.rootNodes.forEach(function (node) {
  255. parseParent(node, 0);
  256. });
  257. }
  258. },
  259. /**
  260. * 设置全部菜单可见
  261. */
  262. setNodeView: function () {
  263. var _self = this;
  264. function initNode(nodes) {
  265. if (nodes instanceof Array) {
  266. nodes.sort(function (a, b) {
  267. if (a.sortNo != undefined && b.sortNo != undefined) {
  268. return a.sortNo - b.sortNo;
  269. } else {
  270. return 0;
  271. }
  272. });
  273. nodes.forEach(function (node) {
  274. node.expand = false;
  275. node.show = true;
  276. if (node.subMenus instanceof Array) {
  277. initNode(node.subMenus);
  278. }
  279. });
  280. }
  281. }
  282. initNode(_self.rootNodes);
  283. },
  284. },
  285. };
  286. </script>
  287. <style scoped>
  288. .menu-div {
  289. padding-left: 10px;
  290. padding-right: 10px;
  291. }
  292. .call-div {
  293. color: white;
  294. padding-top: 10px;
  295. padding-left: 15px;
  296. padding-right: 10px;
  297. font-size: medium;
  298. cursor: pointer;
  299. }
  300. </style>
  301. <style>
  302. .menu-side {
  303. position: fixed;
  304. top: 0px;
  305. bottom: 0;
  306. left: 0;
  307. /* padding-top: 51px; */
  308. width: 230px;
  309. background-color: #293c55;
  310. overflow-y: auto;
  311. overflow-x: hidden;
  312. }
  313. .main_menu_side {
  314. padding: 0;
  315. }
  316. .main_menu .fa {
  317. width: 26px;
  318. opacity: 0.99;
  319. display: inline-block;
  320. font-family: FontAwesome;
  321. font-style: normal;
  322. font-weight: normal;
  323. font-size: 18px;
  324. -webkit-font-smoothing: antialiased;
  325. -moz-osx-font-smoothing: grayscale;
  326. }
  327. .main_menu .navbar-nav > li > a {
  328. color: #fff !important;
  329. }
  330. .main_menu .nav.side-menu > li {
  331. position: relative;
  332. display: block;
  333. cursor: pointer;
  334. }
  335. .main_menu .nav.side-menu > li > a {
  336. margin-bottom: 6px;
  337. }
  338. .main_menu .nav.side-menu > li > a:hover {
  339. color: #f2f5f7 !important;
  340. }
  341. .main_menu .nav.side-menu > li > a,
  342. .main_menu .nav.child_menu > li > a {
  343. color: #e7e7e7;
  344. font-weight: 500;
  345. }
  346. .main_menu .nav.side-menu > li.current-page,
  347. .main_menu .nav.side-menu > li.active {
  348. border-right: 5px solid #1abb9c;
  349. }
  350. .main_menu .nav.side-menu > li.active > a {
  351. text-shadow: rgba(0, 0, 0, 0.25) 0 -1px 0;
  352. background: linear-gradient(#334556, #2c4257), #2a3f54;
  353. box-shadow: rgba(0, 0, 0, 0.25) 0 1px 0,
  354. inset rgba(255, 255, 255, 0.16) 0 1px 0;
  355. }
  356. .main_menu .nav.child_menu {
  357. display: none;
  358. }
  359. .main_menu .nav.child_menu.active {
  360. display: block;
  361. }
  362. /*.nav.child_menu li:hover,
  363. .nav.child_menu li.active {
  364. background-color: #495d70;
  365. }
  366. .nav.child_menu li a:hover {
  367. background-color: #495d70;
  368. }*/
  369. .main_menu .nav.child_menu li {
  370. padding-left: 36px;
  371. }
  372. .main_menu .nav-md ul.nav.child_menu li:before {
  373. background: #425668;
  374. bottom: auto;
  375. content: "";
  376. height: 8px;
  377. left: 23px;
  378. margin-top: 15px;
  379. position: absolute;
  380. right: auto;
  381. width: 8px;
  382. z-index: 1;
  383. border-radius: 50%;
  384. }
  385. .main_menu .nav-md ul.nav.child_menu li:after {
  386. border-left: 1px solid #425668;
  387. bottom: 0;
  388. content: "";
  389. left: 27px;
  390. position: absolute;
  391. top: 0;
  392. }
  393. .main_menu .nav.child_menu li li:hover,
  394. .main_menu .nav.child_menu li li.active {
  395. background-color: #495d70;
  396. }
  397. .main_menu .nav.child_menu li li a:hover,
  398. .main_menu .nav.child_menu li li a.active {
  399. color: #fff;
  400. }
  401. .main_menu .nav > li > a {
  402. position: relative;
  403. display: block;
  404. padding: 13px 15px 12px;
  405. }
  406. /*.nav li.current-page {
  407. background: rgba(255, 255, 255, 0.05);
  408. }
  409. .nav li li li.current-page {
  410. background: none;
  411. }*/
  412. .main_menu .nav li li.current-page a {
  413. color: #fff;
  414. }
  415. .main_menu .nav.navbar-nav > li > a {
  416. color: #515356 !important;
  417. }
  418. .main_menu .nav.top_menu > li > a {
  419. position: relative;
  420. display: block;
  421. padding: 10px 15px;
  422. color: #34495e !important;
  423. }
  424. /* .nav > li > a:hover, .nav > li > a:focus {
  425. background-color: transparent;
  426. }*/
  427. .main_menu .menu-nav > li > a:focus,
  428. .main_menu .menu-nav > li > a:hover {
  429. text-decoration: none;
  430. background-color: transparent !important;
  431. }
  432. .main_menu .menu-search {
  433. background-color: #1c3e4b;
  434. color: #fff;
  435. }
  436. </style>