From 8503e52c563bf94207d45c53baf0e061fbd77494 Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Fri, 26 Jun 2015 21:47:07 -0500 Subject: [PATCH] platform version parsing --- ionic/components/app/app.js | 14 +++++- ionic/platform/platform.js | 91 +++++++++++++++++++++++-------------- ionic/platform/registry.js | 11 +++-- 3 files changed, 77 insertions(+), 39 deletions(-) diff --git a/ionic/components/app/app.js b/ionic/components/app/app.js index 8bcfc56bc5..56183932f0 100644 --- a/ionic/components/app/app.js +++ b/ionic/components/app/app.js @@ -95,9 +95,21 @@ export class IonicApp { applyCss(bodyEle, platform, config) { let className = bodyEle.className; + + let versions = platform.versions(); platform.platforms().forEach(platformName => { - className += ' platform-' + platformName; + // platform-ios platform-ios_8 platform-ios_8_3 + let platformClass = ' platform-' + platformName; + className += platformClass; + + let platformVersion = versions[platformName]; + if (platformVersion) { + platformClass += '_' + platformVersion.major; + className += platformClass; + className += platformClass + '_' + platformVersion.minor; + } }); + className += ' mode-' + config.setting('mode'); bodyEle.className = className.trim(); } diff --git a/ionic/platform/platform.js b/ionic/platform/platform.js index 1a2d6b8f44..82bba2d9fc 100644 --- a/ionic/platform/platform.js +++ b/ionic/platform/platform.js @@ -7,10 +7,9 @@ export class PlatformCtrl { constructor() { this._settings = {}; this._platforms = []; + this._versions = {}; this._registry = {}; this._default = null; - this._vMajor = 0; - this._vMinor = 0; this._readyPromise = new Promise(res => { this._readyResolve = res; } ); } @@ -29,16 +28,14 @@ export class PlatformCtrl { return this._platforms; } - version(asObject) { - let version = parseFloat(this._vMajor + '.' + this._vMinor); - if (asObject) { - return { - version: version, - major: this._vMajor, - minor: this._vMinor - } + versions(platformName) { + if (arguments.length) { + // get a specific platform's version + return this._versions[platformName]; } - return version; + + // get all the platforms that have a valid parsed version + return this._versions; } ready() { @@ -109,26 +106,6 @@ export class PlatformCtrl { return this._ua; } - matchQuery(queryValue) { - let val = this.query('ionicplatform'); - if (val) { - let valueSplit = val.toLowerCase().split(';'); - for (let i = 0; i < valueSplit.length; i++) { - if (valueSplit[i] == queryValue) { - return true; - } - } - } - return false; - } - - matchUserAgent(userAgentExpression) { - if (this._ua) { - let rx = new RegExp(userAgentExpression, 'i'); - return rx.exec(this._ua); - } - } - width(val) { if (arguments.length) { this._w = val; @@ -159,12 +136,40 @@ export class PlatformCtrl { this._default = platformName; } + testQuery(queryValue) { + let val = this.query('ionicplatform'); + if (val) { + let valueSplit = val.toLowerCase().split(';'); + for (let i = 0; i < valueSplit.length; i++) { + if (valueSplit[i] == queryValue) { + return true; + } + } + } + return false; + } + + testUserAgent(userAgentExpression) { + let rx = new RegExp(userAgentExpression, 'i'); + return rx.test(this._ua); + } + + matchUserAgentVersion(userAgentExpression) { + let val = this._ua.match(userAgentExpression); + if (val) { + return { + major: val[1], + minor: val[2] + } + } + } + isPlatform(queryValue, userAgentExpression) { if (!userAgentExpression) { userAgentExpression = queryValue; } - return (this.matchQuery(queryValue)) || - (this.matchUserAgent(userAgentExpression) !== null); + return this.testQuery(queryValue) || + this.testUserAgent(userAgentExpression); } load() { @@ -244,12 +249,13 @@ export class PlatformCtrl { // copy default platform settings into this platform settings obj this._settings[platformNode.name()] = util.extend({}, platformNode.settings()); + // get the platforms version if a version parser was provided + this._versions[platformNode.name()] = platformNode.version(this); + // go to the next platform child platformNode = platformNode.child(); } } - - return this; } settings(val) { @@ -347,6 +353,21 @@ class PlatformNode { return this.c.isMatched; } + version(p) { + if (this.c.versionParser) { + let v = this.c.versionParser(p); + if (v) { + let str = v.major + '.' + v.minor; + return { + str: str, + num: parseFloat(str), + major: parseInt(v.major, 10), + minor: parseInt(v.minor, 10) + }; + } + } + } + getRoot(p) { if (this.isMatch(p)) { diff --git a/ionic/platform/registry.js b/ionic/platform/registry.js index f79091f261..69bea40602 100644 --- a/ionic/platform/registry.js +++ b/ionic/platform/registry.js @@ -53,6 +53,9 @@ Platform.register({ // "silk" is kindle fire let re = 'android| silk'; return p.isPlatform('android', re); + }, + versionParser(p) { + return p.matchUserAgentVersion(/Android (\d+).(\d+)?/); } }); @@ -71,13 +74,12 @@ Platform.register({ }, isMatch(p) { // SLEDGEHAMMER OVERRIDE FOR NOW - return true; + //return true; return p.isPlatform('ios', 'iphone|ipad|ipod'); }, versionParser(p) { - let val = p.matchUserAgent('OS (\d+)_(\d+)?'); - console.log(val); + return p.matchUserAgentVersion(/OS (\d+)_(\d+)?/); } }); @@ -114,6 +116,9 @@ Platform.register({ }, isMatch(p) { return p.isPlatform('windowsphone', 'windows phone'); + }, + versionParser(p) { + return p.matchUserAgentVersion(/Windows Phone (\d+).(\d+)?/); } });