/*md5:44164a0b41ee54b7c9cdb3deda130812*/ /** * 导航功能 * 设计规范:https://done.alibaba-inc.com/file/4fS9MVfQ8r3g/yGGGiUSmHB1SrTTl/preview?aid=B9C48BFB-D87D-412C-94AA-0B5154888713 */ import Magix, { Vframe } from 'magix'; import * as $ from '$'; import * as View from '../mx-util/view'; import * as Dialog from '../mx-dialog/index'; Magix.applyStyle('@index.less'); export default View.extend({ tmpl: '@index.html', mixins: [Dialog], init(ops) { let that = this; that.assign(ops); that.on('destroy', () => { ['@{dealy.show.timer}', '@{dealy.hide.timer}', '@{show.bottom.timer}'].forEach(timer => { if (that[timer]) { clearTimeout(that[timer]); } }) }); }, assign(ops) { let that = this; // 当前数据截快照 that.updater.snapshot(); // 容器内使用场景 let wrapperId = ops.wrapper || ''; let wrapper = wrapperId ? $('#' + wrapperId) : $(window); // 导航样式mode // common:白底色版本(对应老参数配置dark=false) // dark:深底色版本 // his:历史样式(对应老参数配置dark=true) let mode = 'common'; if (ops.hasOwnProperty('mode')) { mode = ops.mode; } else { if (ops.dark + '' !== 'false') { // 老版导航样式 历史配置参数 dark true/false // 为了兼容历史场景的使用,没有dark参数也是老版组件样式 mode = 'his'; } } // 设备信息:无线pc兼容 let devInfo = that['@{get.dev.info}'](); //是否需要顶部外链信息,pc默认是true,无线端不显示 let originLinks = ops.links + '' !== 'false'; let links = devInfo.pc ? originLinks : false; // 根据现实模式进行参数修正 let login = (ops.login + '' !== 'false'); //是否需要显示登录信息,默认是true let height, width = +ops.width, colorBg, colorText; switch (mode) { case 'his': // 历史使用方式兼容 // 黑底色版无论是否显示外链高度固定 links = true; login = false; height = 88; if (!width) { width = wrapper.outerWidth() - 120; } break; default: // common / dark // 无线端隐藏顶部产品线信息,收起到右侧抽屉 height = 100; if (!links) { height -= 50; } colorBg = ops.colorBg; colorText = ops.colorText; break; } // 色值计算 let color = ops.color || this['@{get.css.var}']('--app-brand', '#385ACC'); let result = this['@{color.to.rgb}'](color); let colorOpacity = `rgba(${result.r}, ${result.g}, ${result.b}, 0.1)`; // 默认不选中任何一个导航,表示选中的一级导航 // 如果默认为某个二级导航,订正选中态为一级的 let valueKey = ops.valueKey || 'value', textKey = ops.textKey || 'text', linkKey = ops.linkKey || 'link', outerKey = ops.outerKey || 'outer'; // 默认true let cur = ops.cur || ''; let parent = '', child = ''; let navs = ops.navs || []; navs.forEach(nav => { if (nav[valueKey] == cur) { // 选中的是一级菜单 parent = nav[valueKey]; child = ''; } let allLinks = nav.subs && nav.subs.length > 0; if (nav.subs && nav.subs.length > 0) { let indexArr = [], // 保持顺序 pId = `${that.id}_all`, groupMap = {}, hasTitle = false, // 是否有标题 hasInfo = false; // 分组信息 nav.subs.forEach(sub => { // 是否全部为链接类型 allLinks = allLinks && !!sub[linkKey]; // 选中的是二级菜单 if (sub[valueKey] == cur) { parent = nav[valueKey]; child = sub[valueKey]; } // 分组信息 let gId = (sub.group || pId) + ''; groupMap[gId] = groupMap[gId] || { title: sub.group || '', subs: [] }; groupMap[gId].subs.push(sub); if (indexArr.indexOf(gId) < 0) { // 保证顺序 indexArr.push(gId); } // 是否有分组标题 hasTitle = hasTitle || !!sub.group; // 右侧详情说明 hasInfo = hasInfo || !$.isEmptyObject(sub.info); }); nav.groups = []; for (let i = 0; i < indexArr.length; i++) { nav.groups.push(groupMap[indexArr[i]]); } // 有详情:全屏 // 无详情:有几列显示几列 24 + 100 Magix.mix(nav, { hasTitle, hasInfo, groupWidth: hasInfo ? Math.min(document.documentElement.clientWidth, window.innerWidth) : (124 * nav.groups.length + 2), }) } // 子选项全为外链时,点击一级菜单默认跳转第一个外链 nav[linkKey] = nav[linkKey] || (allLinks ? nav.subs[0][linkKey] : ''); // 移动端加icon,取后两个字 nav.bottomText = nav[textKey].slice(-2); nav.icon = nav.icon || ''; }) // 无线端底部菜单计算,最多显示6个 let bottomNavs = navs.slice(0, 6); that.updater.set({ wrapperId, width, height, logoNav: ops.logoNav || navs[0], navs, valueKey, textKey, linkKey, outerKey, linkFn: (item) => { let t = ` href=${item[linkKey]} `; if (item[outerKey] + '' !== 'false') { // 默认true 外链打开 t += ' target=_blank '; } return t; }, parent, child, mode, color, colorOpacity, colorBg, colorText, login, bizCode: ops.bizCode || '', //项目bizCode loginView: ops.loginView || '', //登录页面 user: ops.user || '', logoutUrl: ops.logoutUrl || '', //退出接口 links, originLinks, bottomNavs, styles: `top: ${(links ? 50 : 0)}px;`, logo: ops.logo, ceiling: (ops.ceiling + '' !== 'false'), //是否需要吸顶功能,默认是true, rightCeilingShow: (ops.rightCeilingShow + '' === 'true'), // 右侧view是否默认不显示,吸顶时显示 rightView: ops.rightView || '', //右侧自定义view rightViewData: ops.rightViewData || {}, devInfo }); // 关闭popover navs.forEach(nav => { let popNode = document.querySelector(`[data-pop="${that.id}_${nav[valueKey]}"]`); if (popNode && popNode.id) { let popVf = Vframe.get(popNode.id); if (popVf) { popVf.invoke('hide'); }; } }) that['@{wrapper}'] = wrapper; that['@{owner.node}'] = $('#' + that.id); // altered是否有变化 // true:有变化 let altered = that.updater.altered(); return altered; }, render() { let that = this; let renderFn = (data) => { that.updater.digest(Magix.mix({ fixed: false, bottomNavShow: true }, data)); let { wrapperId, links, ceiling, devInfo } = that.updater.get(); if (ceiling) { let wrapper = that['@{wrapper}']; let scrollFn = () => { let others = $(`#${that.id} .@index.less:others`); let otherHeight = 0; if (others.length > 0) { otherHeight = others.outerHeight() } let scrollTop = wrapper.scrollTop(); let styles = [ 'width: 100%', 'left: 0' ]; if (wrapperId) { // 容器内使用场景 styles.push( 'position: absolute', 'top: ' + scrollTop + 'px' ) } else { // 相对window定位 styles.push( 'position: fixed', 'top: 0' ) } if (scrollTop > otherHeight) { that.updater.digest({ fixed: true, styles: styles.join(';') }) } else { that.updater.digest({ fixed: false, styles: `top: ${(links ? 50 : 0)}px;` }) } } if (!that['@{init.header.scroll}']) { that['@{init.header.scroll}'] = true; wrapper.on('scroll.header', scrollFn); that.on('destroy', () => { wrapper.off('scroll.header', scrollFn); }); } scrollFn(); } if (!that['@{init.bottom.scroll}'] && (devInfo.phone || devInfo.pad)) { let wrapper = that['@{wrapper}']; let bottomScrollFn = () => { // 滚动时底部导航隐藏,滚动结束再显示 clearTimeout(that['@{show.bottom.timer}']); that['@{show.bottom.timer}'] = setTimeout(() => { that.updater.digest({ bottomNavShow: true }) }, 100) that.updater.digest({ bottomNavShow: false }) } that['@{init.bottom.scroll}'] = true; wrapper.on('scroll.bottom', bottomScrollFn); that.on('destroy', () => { wrapper.off('scroll.bottom', bottomScrollFn); }) } } $.getJSON('//g.alicdn.com/mm/bp-source/lib/products.json', ({ logos = {}, products }) => { let d = { list: products.map(item => { // popover的提示内容 item.tip = `${item.seconds.map(second => `