MenuWidget.vue 12 KB

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