Skip to content

Commit

Permalink
将MIP1页面跳转逻辑融合到MIP2中 (#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
easonyq authored and PengXing committed Dec 13, 2018
1 parent a4f256c commit 89bf87d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
3 changes: 3 additions & 0 deletions packages/mip/src/page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ class Page {

// trigger show page custom event
this.emitEventInCurrentPage({name: CUSTOM_EVENT_SHOW_PAGE})

let mipServiceTmp = window.location.hash.match(/mipservice=(\d)/)
window.mipService = mipServiceTmp ? mipServiceTmp[1] : '1'
}

// ========================= Util functions for developers =========================
Expand Down
5 changes: 5 additions & 0 deletions packages/mip/src/page/util/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export function createIFrame ({fullpath, pageId}, {onLoad, onError} = {}) {
})
container.setAttribute('name', pageMeta)

if (fullpath.indexOf('#') === -1) {
fullpath += '#mipservice=' + window.mipService
} else {
fullpath += '&mipservice=' + window.mipService
}
container.setAttribute('src', fullpath)
container.setAttribute('class', MIP_IFRAME_CONTAINER)

Expand Down
2 changes: 1 addition & 1 deletion packages/mip/src/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class Runtime {
installMip1Polyfill(globalMip)
installMipComponentsPolyfill()
installSandbox(globalMip)
preregisteredExtensions.forEach(installExtension)
Array.isArray(preregisteredExtensions) && preregisteredExtensions.forEach(installExtension)
}
}

Expand Down
52 changes: 43 additions & 9 deletions packages/mip/src/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,18 +335,16 @@ let viewer = {
* otherwise let TOP jump
*/
event.delegate(document, 'a', 'click', function (event) {
let $a = this

/**
* browser will resolve fullpath, including path, query & hash
* eg. http://localhost:8080/examples/page/tree.html?a=b#hash
* don't use `$a.getAttribute('href')`
* don't use `this.getAttribute('href')`
*/
let to = $a.href
let isMipLink = $a.hasAttribute('mip-link') || $a.getAttribute('data-type') === 'mip'
let replace = $a.hasAttribute('replace')
let cacheFirst = $a.hasAttribute('cache-first')
let state = self._getMipLinkData.call($a)
let to = this.href
let isMipLink = this.hasAttribute('mip-link') || this.getAttribute('data-type') === 'mip'
let replace = this.hasAttribute('replace')
let cacheFirst = this.hasAttribute('cache-first')
let state = self._getMipLinkData.call(this)

/**
* For mail、phone、market、app ...
Expand All @@ -361,7 +359,21 @@ let viewer = {
return
}

self.open(to, {isMipLink, replace, state, cacheFirst})
// 以下情况使用 MIP 接管页面跳转
// 1. Standalone
// 2. New MIP Service
let useNewMIPService = window.MIP.standalone || window.mipService === '2'
if (useNewMIPService) {
self.open(to, {isMipLink, replace, state, cacheFirst})
} else {
if (isMipLink) {
let message = self._getMessageData.call(this);
self.sendMessage(message.messageKey, message.messageData);
} else {
// other jump through '_top'
top.location.href = this.href;
}
}

event.preventDefault()
}, false)
Expand All @@ -383,6 +395,28 @@ let viewer = {
}
},

/**
* get alink postMessage data
* @return {Object} messageData
*/
_getMessageData () {
let messageKey = 'loadiframe';
let messageData = {};
messageData.url = this.href;
if (this.hasAttribute('no-head')) {
messageData.nohead = true;
}
if (this.hasAttribute('mip-link')) {
let parent = this.parentNode;
messageData.title = parent.getAttribute('title') || parent.innerText.trim().split('\n')[0];
messageData.click = parent.getAttribute('data-click');
} else {
messageData.title = this.getAttribute('data-title') || this.innerText.trim().split('\n')[0];
messageData.click = this.getAttribute('data-click');
}
return {messageKey, messageData}
},

handleBrowserQuirks () {
// add normal scroll class to body. except ios in iframe.
// Patch for ios+iframe is default in mip.css
Expand Down

0 comments on commit 89bf87d

Please sign in to comment.