weex-lrui
Version:
A Flyme Style UI library based on Weex for Creator.
238 lines (233 loc) • 6.96 kB
JavaScript
/**
* Created by Yanjiie on 18/03/08. Fork from https://github.com/alibaba/weex-ui
*/
const Utils = {
_typeof (obj) {
return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
},
isPlainObject (obj) {
return Utils._typeof(obj) === 'object';
},
isString (obj) {
return typeof (obj) === 'string';
},
isNonEmptyArray (obj = []) {
return obj && obj.length > 0 && Array.isArray(obj) && typeof obj !== 'undefined';
},
isObject (item) {
return (item && typeof item === 'object' && !Array.isArray(item));
},
isEmptyObject (obj) {
return Object.keys(obj).length === 0 && obj.constructor === Object;
},
decodeIconFont (text) {
// 正则匹配 图标和文字混排 eg: 我去上学校,天天不迟到
const regExp = /&#x[a-z|0-9]{4,5};?/g;
if (regExp.test(text)) {
return text.replace(new RegExp(regExp, 'g'), function (iconText) {
const replace = iconText.replace(/&#x/, '0x').replace(/;$/, '');
return String.fromCharCode(replace);
});
} else {
return text;
}
},
mergeDeep (target, ...sources) {
if (!sources.length) return target;
const source = sources.shift();
if (Utils.isObject(target) && Utils.isObject(source)) {
for (const key in source) {
if (Utils.isObject(source[key])) {
if (!target[key]) {
Object.assign(target, {
[key]: {}
});
}
Utils.mergeDeep(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}
return Utils.mergeDeep(target, ...sources);
},
appendProtocol (url) {
if (/^\/\//.test(url)) {
const {
bundleUrl
} = weex.config;
return `http${/^https:/.test(bundleUrl) ? 's' : ''}:${url}`;
}
return url;
},
encodeURLParams (url) {
const parsedUrl = new UrlParser(url, true);
return parsedUrl.toString();
},
goToH5Page (jumpUrl, animated = false, callback = null) {
const Navigator = weex.requireModule('navigator');
const jumpUrlObj = new Utils.UrlParser(jumpUrl, true);
const url = Utils.appendProtocol(jumpUrlObj.toString());
Navigator.push({
url: Utils.encodeURLParams(url),
animated: animated.toString()
}, callback);
},
env: {
isWeb () {
const { platform } = weex.config.env;
return typeof (window) === 'object' && platform.toLowerCase() === 'web';
},
isIOS () {
const { platform } = weex.config.env;
return platform.toLowerCase() === 'ios';
},
/**
* 是否为 iPhone X
* @returns {boolean}
*/
isIPhoneX () {
const { deviceHeight } = weex.config.env;
if (Utils.env.isWeb()) {
return typeof window !== undefined && window.screen && window.screen.width && window.screen.height && (parseInt(window.screen.width, 10) === 375) && (parseInt(window.screen.height, 10) === 812);
}
return Utils.env.isIOS() && deviceHeight === 2436;
},
isAndroid () {
const { platform } = weex.config.env;
return platform.toLowerCase() === 'android';
},
/**
* 获取weex屏幕真实的设置高度,需要减去导航栏高度
* @returns {Number}
*/
getPageHeight () {
const { env } = weex.config;
const navHeight = Utils.env.isWeb() ? 0 : (Utils.env.isIPhoneX() ? 176 : 132);
return env.deviceHeight / env.deviceWidth * 750 - navHeight;
},
/**
* 获取weex屏幕真实的设置高度
* @returns {Number}
*/
getScreenHeight () {
const { env } = weex.config;
return env.deviceHeight / env.deviceWidth * 750;
}
},
/**
* 版本号比较
* @memberOf Utils
* @param currVer {string}
* @param promoteVer {string}
* @returns {boolean}
* @example
*
* const { Utils } = require('@ali/wx-bridge');
* const { compareVersion } = Utils;
* console.log(compareVersion('0.1.100', '0.1.11')); // 'true'
*/
compareVersion (currVer = '0.0.0', promoteVer = '0.0.0') {
if (currVer === promoteVer) return true;
const currVerArr = currVer.split('.');
const promoteVerArr = promoteVer.split('.');
const len = Math.max(currVerArr.length, promoteVerArr.length);
for (let i = 0; i < len; i++) {
const proVal = ~~promoteVerArr[i];
const curVal = ~~currVerArr[i];
if (proVal < curVal) {
return true;
} else if (proVal > curVal) {
return false;
}
}
return false;
},
/**
* 分割数组
* @param arr 被分割数组
* @param size 分割数组的长度
* @returns {Array}
*/
arrayChunk (arr = [], size = 4) {
let groups = [];
if (arr && arr.length > 0) {
groups = arr.map((e, i) => {
return i % size === 0 ? arr.slice(i, i + size) : null;
}).filter(e => {
return e;
});
}
return groups;
},
truncateString (str, len, hasDot = true) {
let newLength = 0;
let newStr = '';
let singleChar = '';
const chineseRegex = /[^\x00-\xff]/g;
const strLength = str.replace(chineseRegex, '**').length;
for (let i = 0; i < strLength; i++) {
singleChar = str.charAt(i).toString();
if (singleChar.match(chineseRegex) !== null) {
newLength += 2;
} else {
newLength++;
}
if (newLength > len) {
break;
}
newStr += singleChar;
}
if (hasDot && strLength > len) {
newStr += '...';
}
return newStr;
},
animation: {
/**
* 返回定义页面转场动画起初的位置
* @param ref
* @param transform 运动类型
* @param status
* @param callback 回调函数
*/
pageTransitionAnimation (ref, transform, status, callback) {
const animation = weex.requireModule('animation');
animation.transition(ref, {
styles: {
transform: transform
},
duration: status ? 250 : 300, // ms
timingFunction: status ? 'ease-in' : 'ease-out',
delay: 0 // ms
}, function () {
callback && callback();
});
}
},
uiStyle: {
/**
* 返回定义页面转场动画起初的位置
* @param animationType 页面转场动画的类型 push,model
* @param size 分割数组的长度
* @returns {}
*/
pageTransitionAnimationStyle (animationType) {
if (animationType === 'push') {
return {
left: '750px',
top: '0px',
height: (weex.config.env.deviceHeight / weex.config.env.deviceWidth * 750) + 'px'
};
} else if (animationType === 'model') {
return {
top: (weex.config.env.deviceHeight / weex.config.env.deviceWidth * 750) + 'px',
left: '0px',
height: (weex.config.env.deviceHeight / weex.config.env.deviceWidth * 750) + 'px'
};
}
return {};
}
}
};
export default Utils;