/** * 全局通用页面新功能引导(带遮罩) * 功能说明: 页面级引导,一个页面里可包含多个引导,一步一步跳转 * 配置流程: * 1、当前view的模板里需要引导的区块加上 data-guide-id="xxxx" 全局唯一标识 2、同时配置alp:https://alp.alibaba-inc.com/#!/page/jsonp-editor/index?id=3515495 里的引导 id 与模板的 data-guide-id 一致。 */ import Magix from 'magix' import View from '../view' Magix.applyStyle('@./new-function-guide.less') // const GUIDE_LOCALSTORAGE_KEY = '_dmp_global_new_guide_mark' // 标识是否已读引导 const TIPS_WIDTH = 220 // 引导浮层的默认宽度 const ARROW_GAP = 20 // const { localStorage } = window const GUIDE_ID_KEY = 'data-guide-id' const GUIDE_NAV_KEY = 'data-nav' // 顶上菜单用了 mx-header 组件,需要特殊定制的key export default View.extend({ tmpl: '@new-function-guide.html', init(options) { this.options = options this.on('destroy', () => { // view 销毁时清除当前的节点轮询探测 this._stopPolling = true }) }, async render() { const loc = Magix.Router.parse() const { user, all } = this.updater.get() // 是否是新手引导,新手引导强制另一套创建人群的引导配置 const isNewbieGuide = user.USER_STATE == all.NEWBIE_GUIDE_STATE let aplConfigApi = 'dmp_new_guide_mask' if (loc.get('__isAfterCreateCrowd')) { // url有参数 __isAfterCreateCrowd 标识,则人群列表出另外的引导 aplConfigApi = 'dmp_new_guide_mask_after_create' } else if (isNewbieGuide) { aplConfigApi = 'dmp_new_guide_mask_newbie' } this.updater.set({ aplConfigApi, isNewbieGuide }) const viewGuides = await this.getViewGuides() // 当前页面没有引导 if (!viewGuides) { return this.updater.digest({ noMatch: true }) } // 非新手引导已看过引导的不显示 if ( !isNewbieGuide && localStorage.getItem( `${aplConfigApi}_${viewGuides.path}_${user.loginUser.userId}_${viewGuides.count}` ) ) { return this.updater.digest({ noMatch: true }) } // 新手引导如果关闭了,后续所有引导都不显示 if (isNewbieGuide && Magix.config('noShowNewbieGuide')) { return this.updater.digest({ noMatch: true }) } // 过滤掉有可能因为没有权限不展示的引导模块 // permissionKey 控制 viewGuides.guides = viewGuides.guides.filter(guide => { if ( !guide.permissionKey || (guide.permissionKey && user.permissions.includes(guide.permissionKey)) ) { return true } }) // 配置引导模块的宽度 for (const guide of viewGuides.guides) { guide.width = guide.width || TIPS_WIDTH } // 轮询等待当前页面对应的所有引导页面节点都加载完毕后 await this.waitAllGuideMounted(viewGuides?.guides) this.updater.digest({ viewGuides, beforeShow: true // 在引导显示前先渲染tips浮层,以获取tips的真实高度 }) // 过滤掉页面上不存在的引导点(非权限控制的) viewGuides.guides = viewGuides.guides.filter(guide => { const guideKey = guide.isNav ? GUIDE_NAV_KEY : GUIDE_ID_KEY const targetEl = $(`[${guideKey}="${guide[GUIDE_ID_KEY]}"]`) if (targetEl[0]) { return true } }) if (!viewGuides.guides.length) { return this.updater.digest({ noMatch: true }) } // 等待一段时间保证页面所有内容加载完毕 await this.awaitTime(500) const docWidth = $(document).width() const docHeight = $(document).height() // 计算引导浮层的大小方位,以及 border mask 的宽高 viewGuides.guides.forEach((guide, i) => { const guideKey = guide.isNav ? GUIDE_NAV_KEY : GUIDE_ID_KEY const targetEl = $(`[${guideKey}="${guide[GUIDE_ID_KEY]}"]`) const targetWidth = targetEl.outerWidth() const targetHeight = targetEl.outerHeight() const { left, top } = targetEl.offset() const tipsEl = $(`#${this.id}_${i}`) guide.position = guide.position || 'bottom' // 提示默认出现在下方 guide.targetWidth = targetWidth guide.targetHeight = targetHeight guide.left = left guide.top = top guide.borderWidthRight = docWidth - left - targetWidth guide.borderWidthBottom = docHeight - top - targetHeight guide.height = tipsEl.outerHeight() switch (guide.position) { case 'bottom': guide.tipsLeft = left - (guide.width - targetWidth) / 2 guide.tipsTop = top + targetHeight + ARROW_GAP break case 'top': guide.tipsLeft = left - (guide.width - targetWidth) / 2 guide.tipsTop = top - guide.height - ARROW_GAP break case 'left': guide.tipsLeft = left - guide.width - ARROW_GAP guide.tipsTop = top - (guide.height - targetHeight) / 2 break case 'right': guide.tipsLeft = left + targetWidth + ARROW_GAP guide.tipsTop = top - (guide.height - targetHeight) / 2 break } }) const index = 0 // 引导节点的索引 this.updater.digest({ viewGuides, index, beforeShow: false }) this.setGuideIntoView(index) }, // 等待X时间 async awaitTime(time) { return new Promise(resolve => { setTimeout(() => { resolve(null) }, time) }) }, 'next'() { const { index } = this.updater.get() const newIndex = index + 1 this.updater.digest({ index: newIndex }) this.setGuideIntoView(newIndex) }, 'jumpOver'(e) { const { isNewbieGuide } = this.updater.get() this.updater.digest({ index: -1 }) if (isNewbieGuide) { // 新手引导跳过的话标识在 Magix.config 配置里 Magix.config({ noShowNewbieGuide: true }) } else { // 其他的引导跳过记录在 localStorage 里 this.setNoShowGuide() } }, // 不再显示当前页的引导,状态写入localStorage setNoShowGuide() { const { viewGuides, aplConfigApi, user } = this.updater.get() localStorage.setItem( `${aplConfigApi}_${viewGuides.path}_${user.loginUser.userId}_${viewGuides.count}`, '1' ) }, // 滚屏到引导的位置 setGuideIntoView(index) { const { viewGuides } = this.updater.get() // if (viewGuides.noScroll) { // return // } const guide = viewGuides.guides[index] // fixed 的节点配置了不滚动,否则定位会有问题 if (guide.noScroll) { return } const guideKey = guide.isNav ? GUIDE_NAV_KEY : GUIDE_ID_KEY const guideEl = $(`[${guideKey}="${guide[GUIDE_ID_KEY]}"]`) const { top } = guideEl.offset() let scrollTop if (guide.position === 'top') { scrollTop = top - guide.height - 90 } else { scrollTop = top - 100 } $(document).scrollTop(scrollTop) }, async getViewGuides() { const { aplConfigApi } = this.updater.get() const loc = Magix.Router.parse() if (Magix.config(`allViewGuidesMap_${aplConfigApi}`)) { return Magix.config(`allViewGuidesMap_${aplConfigApi}`)[loc.path] } try { const models = [ { name: `alp_dmp_${aplConfigApi}_get` } ] const [model] = await this.fetch(models) const allViewGuides = model.get('data.list', []) const allViewGuidesMap = Magix.toMap(allViewGuides, 'path') Magix.config({ [`allViewGuidesMap_${aplConfigApi}`]: allViewGuidesMap }) return allViewGuidesMap[loc.path] } catch (error) { this.alert('错误提示', error.msg || error.message || error) } }, async waitAllGuideMounted(guides = []) { const allGuidesPromise = [] for (const guide of guides) { const guideKey = guide.isNav ? GUIDE_NAV_KEY : GUIDE_ID_KEY allGuidesPromise.push( this.waitSelector(`[${guideKey}="${guide[GUIDE_ID_KEY]}"]`) ) } await Promise.all(allGuidesPromise) }, waitSelector(selector, context) { context = context || document return new Promise(resolve => { let target const now = new Date().getTime() const check = () => { const newNow = new Date().getTime() // 页面销毁时中断 // 轮询时间超过 15 秒也中断,因为有可能配置的引导节点在当前用户下不存在(非权限原因) if (this._stopPolling || newNow - now > 1000 * 15) { return resolve(null) } // console.log('waitSelector', Math.random()) target = context.querySelector(selector) if (target) { resolve(target) } else { setTimeout(check, 500) } } setTimeout(check, 500) }) } })