platform version parsing

This commit is contained in:
Adam Bradley
2015-06-26 21:47:07 -05:00
parent e4070428ae
commit 8503e52c56
3 changed files with 77 additions and 39 deletions

View File

@ -95,9 +95,21 @@ export class IonicApp {
applyCss(bodyEle, platform, config) { applyCss(bodyEle, platform, config) {
let className = bodyEle.className; let className = bodyEle.className;
let versions = platform.versions();
platform.platforms().forEach(platformName => { 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'); className += ' mode-' + config.setting('mode');
bodyEle.className = className.trim(); bodyEle.className = className.trim();
} }

View File

@ -7,10 +7,9 @@ export class PlatformCtrl {
constructor() { constructor() {
this._settings = {}; this._settings = {};
this._platforms = []; this._platforms = [];
this._versions = {};
this._registry = {}; this._registry = {};
this._default = null; this._default = null;
this._vMajor = 0;
this._vMinor = 0;
this._readyPromise = new Promise(res => { this._readyResolve = res; } ); this._readyPromise = new Promise(res => { this._readyResolve = res; } );
} }
@ -29,16 +28,14 @@ export class PlatformCtrl {
return this._platforms; return this._platforms;
} }
version(asObject) { versions(platformName) {
let version = parseFloat(this._vMajor + '.' + this._vMinor); if (arguments.length) {
if (asObject) { // get a specific platform's version
return { return this._versions[platformName];
version: version,
major: this._vMajor,
minor: this._vMinor
}
} }
return version;
// get all the platforms that have a valid parsed version
return this._versions;
} }
ready() { ready() {
@ -109,26 +106,6 @@ export class PlatformCtrl {
return this._ua; 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) { width(val) {
if (arguments.length) { if (arguments.length) {
this._w = val; this._w = val;
@ -159,12 +136,40 @@ export class PlatformCtrl {
this._default = platformName; 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) { isPlatform(queryValue, userAgentExpression) {
if (!userAgentExpression) { if (!userAgentExpression) {
userAgentExpression = queryValue; userAgentExpression = queryValue;
} }
return (this.matchQuery(queryValue)) || return this.testQuery(queryValue) ||
(this.matchUserAgent(userAgentExpression) !== null); this.testUserAgent(userAgentExpression);
} }
load() { load() {
@ -244,12 +249,13 @@ export class PlatformCtrl {
// copy default platform settings into this platform settings obj // copy default platform settings into this platform settings obj
this._settings[platformNode.name()] = util.extend({}, platformNode.settings()); 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 // go to the next platform child
platformNode = platformNode.child(); platformNode = platformNode.child();
} }
} }
return this;
} }
settings(val) { settings(val) {
@ -347,6 +353,21 @@ class PlatformNode {
return this.c.isMatched; 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) { getRoot(p) {
if (this.isMatch(p)) { if (this.isMatch(p)) {

View File

@ -53,6 +53,9 @@ Platform.register({
// "silk" is kindle fire // "silk" is kindle fire
let re = 'android| silk'; let re = 'android| silk';
return p.isPlatform('android', re); return p.isPlatform('android', re);
},
versionParser(p) {
return p.matchUserAgentVersion(/Android (\d+).(\d+)?/);
} }
}); });
@ -71,13 +74,12 @@ Platform.register({
}, },
isMatch(p) { isMatch(p) {
// SLEDGEHAMMER OVERRIDE FOR NOW // SLEDGEHAMMER OVERRIDE FOR NOW
return true; //return true;
return p.isPlatform('ios', 'iphone|ipad|ipod'); return p.isPlatform('ios', 'iphone|ipad|ipod');
}, },
versionParser(p) { versionParser(p) {
let val = p.matchUserAgent('OS (\d+)_(\d+)?'); return p.matchUserAgentVersion(/OS (\d+)_(\d+)?/);
console.log(val);
} }
}); });
@ -114,6 +116,9 @@ Platform.register({
}, },
isMatch(p) { isMatch(p) {
return p.isPlatform('windowsphone', 'windows phone'); return p.isPlatform('windowsphone', 'windows phone');
},
versionParser(p) {
return p.matchUserAgentVersion(/Windows Phone (\d+).(\d+)?/);
} }
}); });