UNPKG

vimo-dt

Version:

A Vue2.x UI Project For Mobile & HyBrid

600 lines (531 loc) 16.3 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.QueryParams = undefined; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); exports.setupPlatform = setupPlatform; var _util = require('../util/util'); var _platformDefaultConfigs = require('./platform-default-configs'); var _platformDefaultConfigs2 = _interopRequireDefault(_platformDefaultConfigs); var _getCss = require('../util/get-css'); var _getCss2 = _interopRequireDefault(_getCss); var _type = require('../util/type'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var Platform = function () { function Platform() { var _this = this; _classCallCheck(this, Platform); this._readyPromise = new Promise(function (resolve, reject) { _this._readyResolve = resolve; _this._readyReject = reject; }); this._versions = {}; this._lang = null; this._qp = null; this._bPlt = null; this._ua = null; this._default = null; this._platforms = []; this._registry = null; this._pW = 0; this._pH = 0; this._lW = 0; this._lH = 0; this._isPortrait = null; this._nt = null; this._networkChangeCallbacks = []; this.css = { transform: null, transition: null, transitionDuration: null, transitionDelay: null, transitionTimingFn: null, transitionStart: null, transitionEnd: null, transformOrigin: null, animationDelay: null }; } _createClass(Platform, [{ key: 'is', value: function is(platformName) { return this._platforms.indexOf(platformName) > -1; } }, { key: 'platforms', value: function platforms() { return this._platforms; } }, { key: 'versions', value: function versions() { return this._versions; } }, { key: 'version', value: function version() { for (var platformName in this._versions) { if (this._versions[platformName]) { return this._versions[platformName]; } } return {}; } }, { key: 'ready', value: function ready() { return this._readyPromise; } }, { key: 'triggerReady', value: function triggerReady(readySource) { this._readyResolve(readySource); } }, { key: 'triggerFail', value: function triggerFail(rejectSource) { this._readyReject(rejectSource); } }, { key: 'beforeReady', value: function beforeReady() { this.triggerReady('H5 Initialization Process!'); } }, { key: 'exitApp', value: function exitApp() { console.error('H5未实现此方法, 请检查调用!'); } }, { key: 'setNetworkType', value: function setNetworkType(networkType) { this._nt = networkType; } }, { key: 'networkType', value: function networkType() { return this._nt; } }, { key: 'onNetworkChange', value: function onNetworkChange(fn) { if ((0, _type.isPresent)(fn) && (0, _type.isFunction)(fn) && this._networkChangeCallbacks.indexOf(fn) === -1) { this._networkChangeCallbacks.push(fn); } } }, { key: 'setCssProps', value: function setCssProps() { this.css = _getCss2.default; return this.css; } }, { key: 'userAgent', value: function userAgent() { return this._ua || ''; } }, { key: 'setUserAgent', value: function setUserAgent(userAgent) { this._ua = userAgent; } }, { key: 'getQueryParam', value: function getQueryParam(key) { return this._qp.get(key); } }, { key: 'setQueryParams', value: function setQueryParams(queryParams) { this._qp = queryParams; } }, { key: 'navigatorPlatform', value: function navigatorPlatform() { return this._bPlt || ''; } }, { key: 'setNavigatorPlatform', value: function setNavigatorPlatform(navigatorPlatform) { this._bPlt = navigatorPlatform; } }, { key: 'setLang', value: function setLang(language, updateDocument) { this._lang = language; if (updateDocument) { document.documentElement.setAttribute('lang', language); } } }, { key: 'lang', value: function lang() { return this._lang; } }, { key: 'width', value: function width() { this._calcDim(); return this._isPortrait ? this._pW : this._lW; } }, { key: 'height', value: function height() { this._calcDim(); return this._isPortrait ? this._pH : this._lH; } }, { key: 'isPortrait', value: function isPortrait() { this._calcDim(); return this._isPortrait; } }, { key: 'isLandscape', value: function isLandscape() { return !this.isPortrait(); } }, { key: '_calcDim', value: function _calcDim() { if (window.screen.width > 0 && window.screen.height > 0) { if (window['innerWidth'] < window['innerHeight']) { if (this._pW <= window['innerWidth']) { this._isPortrait = true; this._pW = window['innerWidth']; } if (this._pH <= window['innerHeight']) { this._isPortrait = true; this._pH = window['innerHeight']; } } else { if (this._lW > window['innerWidth']) { this._isPortrait = true; } if (this._lW <= window['innerWidth']) { this._isPortrait = false; this._lW = window['innerWidth']; } if (this._lH <= window['innerHeight']) { this._isPortrait = false; this._lH = window['innerHeight']; } } } } }, { key: 'setPlatformConfigs', value: function setPlatformConfigs(platformConfigs) { this._registry = platformConfigs || {}; } }, { key: 'getPlatformConfig', value: function getPlatformConfig(platformName) { return this._registry[platformName] || {}; } }, { key: 'registry', value: function registry() { return this._registry; } }, { key: 'setDefault', value: function setDefault(platformName) { this._default = platformName; } }, { key: 'testQuery', value: function testQuery(queryValue, queryTestValue) { var valueSplit = queryValue.toLowerCase().split(';'); return valueSplit.indexOf(queryTestValue) > -1; } }, { key: 'testNavigatorPlatform', value: function testNavigatorPlatform(navigatorPlatformExpression) { var rgx = new RegExp(navigatorPlatformExpression, 'i'); return rgx.test(this._bPlt); } }, { key: 'matchUserAgentVersion', value: function matchUserAgentVersion(userAgentExpression) { if (this._ua && userAgentExpression) { var val = this._ua.match(userAgentExpression); if (val) { return { major: val[1], minor: val[2], patch: val[3] }; } } } }, { key: 'testUserAgent', value: function testUserAgent(expression) { if (this._ua) { return this._ua.indexOf(expression) >= 0; } return false; } }, { key: 'isPlatformMatch', value: function isPlatformMatch(queryStringName, userAgentAtLeastHas) { var userAgentMustNotHave = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; var queryValue = this._qp.get('platform'); if (queryValue) { return this.testQuery(queryValue, queryStringName); } userAgentAtLeastHas = userAgentAtLeastHas || [queryStringName]; var userAgent = this._ua.toLowerCase(); for (var i = 0; i < userAgentAtLeastHas.length; i++) { if (userAgent.indexOf(userAgentAtLeastHas[i]) > -1) { for (var j = 0; j < userAgentMustNotHave.length; j++) { if (userAgent.indexOf(userAgentMustNotHave[j]) > -1) { return false; } } return true; } } return false; } }, { key: 'init', value: function init() { this._calcDim(); this._platforms = []; var rootPlatformNode = void 0; var enginePlatformNode = void 0; var tmpPlatform = void 0; for (var platformName in this._registry) { tmpPlatform = this.matchPlatform(platformName); if (tmpPlatform) { if (tmpPlatform.isEngine) { enginePlatformNode = tmpPlatform; } else if (!rootPlatformNode || tmpPlatform.depth > rootPlatformNode.depth) { rootPlatformNode = tmpPlatform; } } } if (!rootPlatformNode) { rootPlatformNode = new PlatformNode(this._registry, this._default); } if (rootPlatformNode) { if (enginePlatformNode) { enginePlatformNode.child = rootPlatformNode; rootPlatformNode.parent = enginePlatformNode; rootPlatformNode = enginePlatformNode; } var platformNode = rootPlatformNode; while (platformNode) { insertSuperset(this._registry, platformNode); platformNode = platformNode.child; } platformNode = rootPlatformNode.parent; while (platformNode) { rootPlatformNode = platformNode; platformNode = platformNode.parent; } platformNode = rootPlatformNode; while (platformNode) { platformNode.beforeInitialize(this); platformNode.initialize(this); this._platforms.push(platformNode.name); this._versions[platformNode.name] = platformNode.version(this); platformNode = platformNode.child; } } } }, { key: 'matchPlatform', value: function matchPlatform(platformName) { var platformNode = new PlatformNode(this._registry, platformName); var rootNode = platformNode.getRoot(this); if (rootNode) { rootNode.depth = 0; var childPlatform = rootNode.child; while (childPlatform) { rootNode.depth++; childPlatform = childPlatform.child; } } return rootNode; } }]); return Platform; }(); var PlatformNode = function () { function PlatformNode(registry, platformName) { _classCallCheck(this, PlatformNode); this.parent = null; this.child = null; this.depth = null; this.registry = registry; this.c = registry[platformName]; this.name = platformName; this.isEngine = this.c && this.c.isEngine; } _createClass(PlatformNode, [{ key: 'settings', value: function settings() { return this.c.settings || {}; } }, { key: 'superset', value: function superset() { return this.c.superset; } }, { key: 'isMatch', value: function isMatch(p) { return this.c.isMatch && this.c.isMatch(p); } }, { key: 'beforeInitialize', value: function beforeInitialize(platform) { this.c.beforeInitialize && this.c.beforeInitialize(platform); } }, { key: 'initialize', value: function initialize(platform) { this.c.initialize && this.c.initialize(platform); } }, { key: 'version', value: function version(p) { if (this.c.versionParser) { var v = this.c.versionParser(p); if (v) { if (!v.major) v.major = '0'; if (!v.minor) v.minor = '0'; if (!v.patch) v.patch = '0'; var str = v.major + '.' + v.minor + (v.patch ? '.' + v.patch : ''); return { str: str, num: parseFloat(str), major: parseInt(v.major, 10), minor: parseInt(v.minor, 10), patch: parseInt(v.patch, 10) }; } } } }, { key: 'getRoot', value: function getRoot(p) { if (this.isMatch(p)) { var parents = this.getSubsetParents(this.name); if (!parents.length) { return this; } var platformNode = null; var rootPlatformNode = null; for (var i = 0; i < parents.length; i++) { platformNode = new PlatformNode(this.registry, parents[i]); platformNode.child = this; rootPlatformNode = platformNode.getRoot(p); if (rootPlatformNode) { this.parent = platformNode; return rootPlatformNode; } } } return null; } }, { key: 'getSubsetParents', value: function getSubsetParents(subsetPlatformName) { var parentPlatformNames = []; var platform = null; for (var platformName in this.registry) { platform = this.registry[platformName]; if (platform.subsets && platform.subsets.indexOf(subsetPlatformName) > -1) { parentPlatformNames.push(platformName); } } return parentPlatformNames; } }]); return PlatformNode; }(); function insertSuperset(registry, platformNode) { var supersetPlatformName = platformNode.superset(); if (supersetPlatformName) { var supersetPlatform = new PlatformNode(registry, supersetPlatformName); supersetPlatform.parent = platformNode.parent; supersetPlatform.child = platformNode; if (supersetPlatform.parent) { supersetPlatform.parent.child = supersetPlatform; } platformNode.parent = supersetPlatform; } } var QueryParams = exports.QueryParams = function () { function QueryParams() { var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window.location.href; _classCallCheck(this, QueryParams); this.data = {}; this.parseUrl(url); } _createClass(QueryParams, [{ key: 'get', value: function get(key) { return this.data[key.toLowerCase()]; } }, { key: 'parseUrl', value: function parseUrl(url) { if (url) { var startIndex = url.indexOf('?'); if (startIndex > -1) { var queries = url.slice(startIndex + 1).split('&'); for (var i = 0; i < queries.length; i++) { if (queries[i].indexOf('=') > 0) { var split = queries[i].split('='); if (split.length > 1) { this.data[split[0].toLowerCase()] = split[1].split('#')[0]; } } } } } return this.data; } }]); return QueryParams; }(); function setupPlatform() { var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var p = new Platform(); var _finalConf = _platformDefaultConfigs2.default; for (var outerKey in config) { if (_finalConf[outerKey] && (0, _type.isObject)(_finalConf[outerKey])) { var _cusConf = config[outerKey]; var _defConf = _finalConf[outerKey]; for (var innerKey in _cusConf) { var _tmp = {}; _tmp = (0, _util.defaults)(_cusConf[innerKey], _defConf[innerKey]); _defConf[innerKey] = _tmp; } } else { _finalConf[outerKey] = config[outerKey]; } } var keys = Object.keys(_finalConf).filter(function (item) { return item !== 'mobile' && item !== 'android' && item !== 'ios'; }); _finalConf.android.subsets = keys; _finalConf.ios.subsets = keys; p.setDefault('core'); p.setPlatformConfigs(_finalConf); p.setQueryParams(new QueryParams()); !p.navigatorPlatform() && p.setNavigatorPlatform(window.navigator.platform); !p.userAgent() && p.setUserAgent(window.navigator.userAgent); !p.lang() && p.setLang('zh-CN', true); p.setCssProps(); p.init(); p.beforeReady(); return p; }