weex-nuke
Version:
基于 Rax 、Weex 的高性能组件体系 ~~
107 lines (100 loc) • 2.43 kB
JavaScript
import { createElement } from 'rax';
import { urlHelper, urlHandler } from 'nuke-util';
import { isWeex } from 'nuke-env';
const Navigator = {
setTitle(title) {
if (isWeex) {
const navigator = require('@weex-module/navigator');
try {
navigator.setTitle({ title }, () => {});
} catch (e) {
document.title = title;
}
}
},
convertURL(url) {
if (url.indexOf('qap://') > -1) {
const options = {
refer: window.location.href,
};
if (
typeof window.__QAP__ !== 'undefined' &&
__QAP__.package &&
__QAP__.package.config &&
__QAP__.package.config.WebRootPath
) {
options.webRootPath = __QAP__.package.config.WebRootPath;
}
url = urlHandler(url, options);
}
return url;
},
isEmpty(obj) {
for (const key in obj) {
if (hasOwnProperty.call(obj, key)) {
return false;
}
}
return true;
},
serialize(url, obj) {
if (this.isEmpty(obj)) {
return url;
}
let str = '';
for (const p in obj) {
str = `&${p}=${encodeURIComponent(obj[p])}`;
}
if (urlHelper.urlParse(url).search.length > 0) {
url = str ? `${url}${str}` : url;
} else {
url = str ? `${url}?${str.substring(1)}` : url;
}
return url;
},
push(url, title, animated = true, param = {}, callback) {
if (isWeex) {
const navigator = require('@weex-module/navigator');
try {
navigator.push(
{
url,
title,
...param,
},
(e) => {
callback && callback(e);
}
);
} catch (e) {
callback && callback(e);
}
} else {
url = this.convertURL(url);
if (url) {
if (param.target && param.target === '_blank') {
delete param.target;
window.open(this.serialize(url, param), '_blank');
} else {
window.location.href = this.serialize(url, param);
}
}
}
},
pop(options = { animation: 'true' }, callback) {
if (isWeex) {
const navigator = require('@weex-module/navigator');
try {
navigator.pop(options, (e) => {
callback && callback(e);
});
} catch (e) {
callback && callback(e);
}
} else {
window.history.back();
}
},
};
export default Navigator;
;