mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-19 03:32:21 +08:00
checking in
This commit is contained in:
@ -1,90 +1,97 @@
|
||||
import * as util from '../util/util';
|
||||
import {Tap} from '../util/tap';
|
||||
|
||||
let platformRegistry = {};
|
||||
let defaultPlatform;
|
||||
let activePlatform;
|
||||
|
||||
export class Platform {
|
||||
|
||||
load(platformName) {
|
||||
this._c = Platform.get(platformName);
|
||||
constructor() {
|
||||
this._settings = {};
|
||||
this._platforms = [];
|
||||
}
|
||||
|
||||
name() {
|
||||
return this._c.name;
|
||||
is(platformName) {
|
||||
return (this._platforms.indexOf(platformName) > -1);
|
||||
}
|
||||
|
||||
settings() {
|
||||
return this._c.settings || {};
|
||||
}
|
||||
|
||||
subsets() {
|
||||
return this._c.subsets || [];
|
||||
settings(val) {
|
||||
if (arguments.length) {
|
||||
this._settings = val;
|
||||
}
|
||||
return this._settings;
|
||||
}
|
||||
|
||||
run() {
|
||||
this._c.run && this._c.run();
|
||||
}
|
||||
let config = null;
|
||||
|
||||
parent(val) {
|
||||
if (arguments.length) {
|
||||
this._parent = val;
|
||||
for (var i = 0; i < this._platforms.length; i++) {
|
||||
config = Platform.get(this._platforms[i]);
|
||||
config.run && config.run();
|
||||
}
|
||||
return this._parent;
|
||||
}
|
||||
|
||||
child(val) {
|
||||
if (arguments.length) {
|
||||
this._child = val;
|
||||
}
|
||||
return this._child;
|
||||
add(platformName) {
|
||||
this._platforms.push(platformName);
|
||||
}
|
||||
|
||||
isMatch(app) {
|
||||
if (!this._c.isMatch) {
|
||||
return true;
|
||||
}
|
||||
return this._c.isMatch(app);
|
||||
}
|
||||
|
||||
getRoot(app, childPlatform) {
|
||||
if (this.isMatch(app)) {
|
||||
let parents = Platform.getSubsetParents(this.name());
|
||||
/* Static Methods */
|
||||
static create(app) {
|
||||
let rootNode = null;
|
||||
|
||||
if (!parents.length) {
|
||||
platform = new Platform();
|
||||
platform.load(this.name());
|
||||
platform.child(childPlatform);
|
||||
return platform;
|
||||
}
|
||||
function matchPlatform(platformConfig) {
|
||||
let platformNode = new PlatformNode();
|
||||
platformNode.config(platformConfig);
|
||||
let tmpPlatform = platformNode.getRoot(app, 0);
|
||||
|
||||
let platform = null;
|
||||
let rootPlatform = null;
|
||||
if (tmpPlatform) {
|
||||
tmpPlatform.depth = 0;
|
||||
let childPlatform = tmpPlatform.child();
|
||||
while(childPlatform) {
|
||||
tmpPlatform.depth++
|
||||
childPlatform = childPlatform.child();
|
||||
}
|
||||
|
||||
for (let i = 0; i < parents.length; i++) {
|
||||
platform = new Platform();
|
||||
platform.load(parents[i]);
|
||||
platform.child(this);
|
||||
|
||||
rootPlatform = platform.getRoot(app, this);
|
||||
if (rootPlatform) {
|
||||
this.parent(platform);
|
||||
return rootPlatform;
|
||||
if (!rootNode || tmpPlatform.depth > rootNode.depth) {
|
||||
rootNode = tmpPlatform;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
function insertSuperset(platformNode) {
|
||||
let supersetPlaformName = platformNode.superset();
|
||||
if (supersetPlaformName) {
|
||||
let supersetPlatform = new PlatformNode();
|
||||
supersetPlatform.load(supersetPlaformName);
|
||||
supersetPlatform.parent(platformNode.parent());
|
||||
supersetPlatform.child(platformNode);
|
||||
supersetPlatform.parent().child(supersetPlatform);
|
||||
platformNode.parent(supersetPlatform);
|
||||
}
|
||||
}
|
||||
|
||||
for (let platformName in platformRegistry) {
|
||||
matchPlatform( platformRegistry[platformName] );
|
||||
}
|
||||
|
||||
static getActivePlatform(app) {
|
||||
let platform = new Platform();
|
||||
platform.load('tablet');
|
||||
if (rootNode) {
|
||||
let platformNode = rootNode.child();
|
||||
while (platformNode) {
|
||||
insertSuperset(platformNode);
|
||||
platformNode = platformNode.child();
|
||||
}
|
||||
|
||||
let root = platform.getRoot(app, null);
|
||||
console.log(root)
|
||||
platformNode = rootNode.child();
|
||||
let settings = {};
|
||||
while (platformNode) {
|
||||
platform.add(platformNode.name());
|
||||
util.extend(settings, platformNode.settings());
|
||||
platformNode = platformNode.child();
|
||||
}
|
||||
|
||||
platform.settings(settings);
|
||||
}
|
||||
|
||||
return platform;
|
||||
}
|
||||
|
||||
static register(platform) {
|
||||
@ -109,116 +116,93 @@ export class Platform {
|
||||
}
|
||||
}
|
||||
|
||||
let rootPlatform = null;
|
||||
|
||||
class PlatformNode {
|
||||
|
||||
Platform.register({
|
||||
name: 'core',
|
||||
subsets: [
|
||||
'mobile'
|
||||
],
|
||||
settings: {
|
||||
mode: 'a'
|
||||
},
|
||||
run() {
|
||||
console.log('Core');
|
||||
load(platformName) {
|
||||
this._c = Platform.get(platformName);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Platform.register({
|
||||
name: 'mobile',
|
||||
subsets: [
|
||||
'android',
|
||||
'ios'
|
||||
],
|
||||
settings: {
|
||||
mode: 'b'
|
||||
},
|
||||
run() {
|
||||
console.log('Mobile');
|
||||
config(val) {
|
||||
this._c = val;
|
||||
}
|
||||
});
|
||||
|
||||
settings() {
|
||||
return this._c.settings || {};
|
||||
}
|
||||
|
||||
name() {
|
||||
return this._c.name;
|
||||
}
|
||||
|
||||
superset() {
|
||||
return this._c.superset;
|
||||
}
|
||||
|
||||
runAll() {
|
||||
let platform = this;
|
||||
while (platform) {
|
||||
platform.run();
|
||||
platform = platform.child();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
parent(val) {
|
||||
if (arguments.length) {
|
||||
this._parent = val;
|
||||
}
|
||||
return this._parent;
|
||||
}
|
||||
|
||||
child(val) {
|
||||
if (arguments.length) {
|
||||
this._child = val;
|
||||
}
|
||||
return this._child;
|
||||
}
|
||||
|
||||
Platform.register({
|
||||
name: 'android',
|
||||
subsets: [
|
||||
'tablet'
|
||||
],
|
||||
settings: {
|
||||
mode: 'c'
|
||||
},
|
||||
isMatch(app) {
|
||||
return app.matchesPlatform('android');
|
||||
},
|
||||
run() {
|
||||
console.log('Android');
|
||||
if (typeof this._c._isMatched !== 'boolean') {
|
||||
// only do the actual check once
|
||||
if (!this._c.isMatch) {
|
||||
this._c._isMatched = true;
|
||||
} else {
|
||||
this._c._isMatched = this._c.isMatch(app);
|
||||
}
|
||||
}
|
||||
return this._c._isMatched;
|
||||
}
|
||||
});
|
||||
|
||||
getRoot(app) {
|
||||
if (this.isMatch(app)) {
|
||||
|
||||
Platform.register({
|
||||
name: 'tablet',
|
||||
settings: {
|
||||
mode: 'd'
|
||||
},
|
||||
isMatch(app) {
|
||||
return app.height() >= 800 || app.width() >= 800;
|
||||
},
|
||||
run() {
|
||||
console.log('Tablet');
|
||||
let parents = Platform.getSubsetParents(this.name());
|
||||
|
||||
if (!parents.length) {
|
||||
return this;
|
||||
}
|
||||
|
||||
let platform = null;
|
||||
let rootPlatform = null;
|
||||
|
||||
for (let i = 0; i < parents.length; i++) {
|
||||
platform = new PlatformNode();
|
||||
platform.load(parents[i]);
|
||||
platform.child(this);
|
||||
|
||||
rootPlatform = platform.getRoot(app);
|
||||
if (rootPlatform) {
|
||||
this.parent(platform);
|
||||
return rootPlatform;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
Platform.register({
|
||||
name: 'ios',
|
||||
subsets: [
|
||||
'ipad',
|
||||
'iphone'
|
||||
],
|
||||
settings: {
|
||||
mode: 'e'
|
||||
},
|
||||
isMatch(app) {
|
||||
return app.matchesPlatform('ios');
|
||||
},
|
||||
run() {
|
||||
console.log('iOS');
|
||||
Tap.run();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Platform.register({
|
||||
name: 'ipad',
|
||||
subsets: [
|
||||
'tablet'
|
||||
],
|
||||
settings: {
|
||||
mode: 'f'
|
||||
},
|
||||
isMatch(app) {
|
||||
return app.matchesDevice('ipad');
|
||||
},
|
||||
run() {
|
||||
console.log('iPad');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Platform.register({
|
||||
name: 'iphone',
|
||||
settings: {
|
||||
mode: 'g'
|
||||
},
|
||||
isMatch(app) {
|
||||
return app.matchesDevice('iphone');
|
||||
},
|
||||
run() {
|
||||
console.log('iPhone');
|
||||
}
|
||||
});
|
||||
|
||||
let platformRegistry = {};
|
||||
|
||||
|
Reference in New Issue
Block a user