UNPKG

falcon-ui

Version:

ui components for falcon

284 lines (277 loc) 7.4 kB
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import UrlParser from 'url-parse'; const $env = $falcon.env; const Utils = { UrlParser: UrlParser, _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: 我去上学校&#xe600;,天天不&#xe600;迟到 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); }, env: { isTaobao () { return false; }, isTrip () { return false; }, isBoat () { return false; }, isWeb () { return $env.platform === 'Web'; }, isIOS () { return false; }, isAndroid () { return false; }, isDevice() { // on device or on emulator return $env.platform === 'AliOSThings' }, /** * 获取weex屏幕真实的设置高度,需要减去导航栏高度 * @returns {Number} */ getPageHeight () { return ($env.deviceHeight / $env.deviceWidth) * 750; }, /** * 获取weex屏幕真实的设置高度 * @returns {Number} */ getScreenHeight () { 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; }, /* * 截断字符串 * @param str 传入字符串 * @param len 截断长度 * @param hasDot 末尾是否... * @returns {String} */ 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; }, /* * 转换 obj 为 url params参数 * @param obj 传入字符串 * @returns {String} */ objToParams (obj) { let str = ''; for (const key in obj) { if (str !== '') { str += '&'; } str += key + '=' + encodeURIComponent(obj[key]); } return str; }, /* * 转换 url params参数为obj * @param str 传入url参数字符串 * @returns {Object} */ paramsToObj (str) { let obj = {}; try { obj = JSON.parse( '{"' + decodeURI(str) .replace(/"/g, '\\"') .replace(/&/g, '","') .replace(/=/g, '":"') + '"}' ); } catch (e) { console.log(e); } return obj; }, animation: { /** * 返回定义页面转场动画起初的位置 * @param ref * @param transform 运动类型 * @param status * @param callback 回调函数 */ pageTransitionAnimation (ref, transform, status, callback) { const animation = $falcon.getPage(this).$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: ($env.deviceHeight / $env.deviceWidth) * 750 + 'px' }; } else if (animationType === 'model') { return { top: ($env.deviceHeight / $env.deviceWidth) * 750 + 'px', left: '0px', height: ($env.deviceHeight / $env.deviceWidth) * 750 + 'px' }; } return {}; } } }; export default Utils;