mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
wip
This commit is contained in:
@@ -1,144 +1,224 @@
|
||||
import * as util from '../util/util';
|
||||
import {Tap} from '../util/tap';
|
||||
|
||||
let registry = {};
|
||||
let platformRegistry = {};
|
||||
let defaultPlatform;
|
||||
let activePlatform;
|
||||
|
||||
class PlatformController {
|
||||
export class Platform {
|
||||
|
||||
constructor(platformQuerystring, userAgent) {
|
||||
this.pqs = platformQuerystring;
|
||||
this.ua = userAgent;
|
||||
load(platformName) {
|
||||
this._c = Platform.get(platformName);
|
||||
}
|
||||
|
||||
get() {
|
||||
if (util.isUndefined(activePlatform)) {
|
||||
this.set(this.detect());
|
||||
}
|
||||
return activePlatform || defaultPlatform;
|
||||
name() {
|
||||
return this._c.name;
|
||||
}
|
||||
|
||||
getName() {
|
||||
return this.get().name;
|
||||
settings() {
|
||||
return this._c.settings || {};
|
||||
}
|
||||
|
||||
getMode() {
|
||||
let plt = this.get();
|
||||
return plt.mode || plt.name;
|
||||
}
|
||||
|
||||
register(platform) {
|
||||
registry[platform.name] = platform;
|
||||
}
|
||||
|
||||
getPlatform(name) {
|
||||
return registry[name];
|
||||
}
|
||||
|
||||
set(platform) {
|
||||
activePlatform = platform;
|
||||
|
||||
this._applyBodyClasses();
|
||||
}
|
||||
|
||||
setDefault(platform) {
|
||||
defaultPlatform = platform;
|
||||
}
|
||||
|
||||
isRegistered(platformName) {
|
||||
return registry.some(platform => {
|
||||
return platform.name === platformName;
|
||||
})
|
||||
}
|
||||
|
||||
detect() {
|
||||
for (let name in registry) {
|
||||
if (registry[name].isMatch(this.pqs, this.ua)) {
|
||||
return registry[name];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
_applyBodyClasses() {
|
||||
if(!activePlatform) {
|
||||
return;
|
||||
}
|
||||
|
||||
document.body.classList.add('platform-' + activePlatform.name);
|
||||
subsets() {
|
||||
return this._c.subsets || [];
|
||||
}
|
||||
|
||||
run() {
|
||||
activePlatform && activePlatform.run();
|
||||
this._c.run && this._c.run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the platform matches the provided one.
|
||||
*/
|
||||
is(platform) {
|
||||
if(!activePlatform) { return false; }
|
||||
|
||||
return activePlatform.name === platform;
|
||||
parent(val) {
|
||||
if (arguments.length) {
|
||||
this._parent = val;
|
||||
}
|
||||
return this._parent;
|
||||
}
|
||||
/**
|
||||
* Check if the loaded device matches the provided one.
|
||||
*/
|
||||
isDevice(device) {
|
||||
if(!activePlatform) { return false; }
|
||||
|
||||
return activePlatform.getDevice() === device;
|
||||
child(val) {
|
||||
if (arguments.length) {
|
||||
this._child = val;
|
||||
}
|
||||
return this._child;
|
||||
}
|
||||
|
||||
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());
|
||||
|
||||
if (!parents.length) {
|
||||
platform = new Platform();
|
||||
platform.load(this.name());
|
||||
platform.child(childPlatform);
|
||||
return platform;
|
||||
}
|
||||
|
||||
let platform = null;
|
||||
let rootPlatform = null;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
static getActivePlatform(app) {
|
||||
let platform = new Platform();
|
||||
platform.load('tablet');
|
||||
|
||||
let root = platform.getRoot(app, null);
|
||||
console.log(root)
|
||||
}
|
||||
|
||||
static register(platform) {
|
||||
platformRegistry[platform.name] = platform;
|
||||
}
|
||||
|
||||
static get(platformName) {
|
||||
return platformRegistry[platformName] || {};
|
||||
}
|
||||
|
||||
static getSubsetParents(subsetPlatformName) {
|
||||
let parentPlatformNames = [];
|
||||
let platform = null;
|
||||
|
||||
for (let platformName in platformRegistry) {
|
||||
platform = platformRegistry[platformName];
|
||||
if (platform.subsets && platform.subsets.indexOf(subsetPlatformName) > -1) {
|
||||
parentPlatformNames.push(platformName);
|
||||
}
|
||||
}
|
||||
return parentPlatformNames;
|
||||
}
|
||||
}
|
||||
|
||||
export let Platform = new PlatformController((util.getQuerystring('ionicplatform')).toLowerCase(), window.navigator.userAgent);
|
||||
let rootPlatform = null;
|
||||
|
||||
|
||||
Platform.register({
|
||||
name: 'core',
|
||||
subsets: [
|
||||
'mobile'
|
||||
],
|
||||
settings: {
|
||||
mode: 'a'
|
||||
},
|
||||
run() {
|
||||
console.log('Core');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Platform.register({
|
||||
name: 'mobile',
|
||||
subsets: [
|
||||
'android',
|
||||
'ios'
|
||||
],
|
||||
settings: {
|
||||
mode: 'b'
|
||||
},
|
||||
run() {
|
||||
console.log('Mobile');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Platform.register({
|
||||
name: 'android',
|
||||
mode: 'md',
|
||||
isMatch(platformQuerystring, userAgent) {
|
||||
if (platformQuerystring) {
|
||||
return platformQuerystring == 'android';
|
||||
}
|
||||
return /android/i.test(userAgent);
|
||||
subsets: [
|
||||
'tablet'
|
||||
],
|
||||
settings: {
|
||||
mode: 'c'
|
||||
},
|
||||
getDevice: function() {
|
||||
return 'android';
|
||||
isMatch(app) {
|
||||
return app.matchesPlatform('android');
|
||||
},
|
||||
run() {
|
||||
console.log('Android');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Platform.register({
|
||||
name: 'ios',
|
||||
isMatch(platformQuerystring, userAgent) {
|
||||
if (platformQuerystring) {
|
||||
return platformQuerystring == 'ios';
|
||||
}
|
||||
return /ipad|iphone|ipod/i.test(userAgent);
|
||||
name: 'tablet',
|
||||
settings: {
|
||||
mode: 'd'
|
||||
},
|
||||
getDevice: function() {
|
||||
if(/ipad/i.test(userAgent)) {
|
||||
return 'ipad';
|
||||
}
|
||||
if(/iphone/i.test(userAgent)) {
|
||||
return 'iphone';
|
||||
}
|
||||
isMatch(app) {
|
||||
return app.height() >= 800 || app.width() >= 800;
|
||||
},
|
||||
run() {
|
||||
console.log('Tablet');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Platform.register({
|
||||
name: 'ios',
|
||||
subsets: [
|
||||
'ipad',
|
||||
'iphone'
|
||||
],
|
||||
settings: {
|
||||
mode: 'e'
|
||||
},
|
||||
isMatch(app) {
|
||||
return app.matchesPlatform('ios');
|
||||
},
|
||||
run() {
|
||||
console.log('iOS');
|
||||
Tap.run();
|
||||
}
|
||||
});
|
||||
|
||||
// Last case is a catch-all
|
||||
// TODO(mlynch): don't default to iOS, default to core,
|
||||
// also make sure to remove getPlatform and set to detect()
|
||||
Platform.setDefault({
|
||||
name: 'ios'
|
||||
});
|
||||
Platform.set( Platform.getPlatform('ios') );//Platform.detect() );
|
||||
|
||||
// If the platform needs to do some initialization (like load a custom
|
||||
// tap strategy), run it now
|
||||
Platform.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');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
155
ionic/platform/platform_TWO.js
Normal file
155
ionic/platform/platform_TWO.js
Normal file
@@ -0,0 +1,155 @@
|
||||
// import * as util from '../util/util';
|
||||
// import {IonicConfig} from '../config/config';
|
||||
|
||||
|
||||
// let platformRegistry = {};
|
||||
|
||||
// export class Platform extends IonicConfig {
|
||||
|
||||
// constructor(settings={}) {
|
||||
// super(settings);
|
||||
// this._chld = {};
|
||||
// this._parent = null;
|
||||
// }
|
||||
|
||||
// parent(val) {
|
||||
// if (arguments.length) {
|
||||
// this._parent = val;
|
||||
// }
|
||||
// return this._parent;
|
||||
// }
|
||||
|
||||
// app(val) {
|
||||
// if (arguments.length) {
|
||||
// this._app = val;
|
||||
// }
|
||||
// return this._app;
|
||||
// }
|
||||
|
||||
// name(val) {
|
||||
// if (arguments.length) {
|
||||
// this._name = val;
|
||||
// }
|
||||
// return this._name;
|
||||
// }
|
||||
|
||||
// is(platformName, climbToRoot) {
|
||||
// if (this._name == platformName) {
|
||||
// return true;
|
||||
// }
|
||||
|
||||
// let platform = null;
|
||||
|
||||
// if (climbToRoot !== false) {
|
||||
// platform = this._parent
|
||||
// while (platform) {
|
||||
// if (platform.name() == platformName) {
|
||||
// return true;
|
||||
// }
|
||||
// platform = platform._parent;
|
||||
// }
|
||||
// }
|
||||
|
||||
// for (let childPlatform in this._chld) {
|
||||
// platform = this._chld[childPlatform];
|
||||
// platform.app(this._app);
|
||||
// if (platform.is(platformName, false) == platform.isMatch()) {
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// matchesQuery(queryKey, queryValue) {
|
||||
// const qs = this._app.query()[queryKey];
|
||||
// return !!(qs && qs == queryValue);
|
||||
// }
|
||||
|
||||
// matchesUserAgent(userAgentExpression) {
|
||||
// const rx = new RegExp(userAgentExpression, 'i');
|
||||
// return rx.test( this._app.userAgent() );
|
||||
// }
|
||||
|
||||
// matchesPlatform(platformQueryValue, platformUserAgentExpression) {
|
||||
// return this.matchesQuery('ionicplatform', platformQueryValue) ||
|
||||
// this.matchesUserAgent(platformUserAgentExpression);
|
||||
// }
|
||||
|
||||
// matchesDevice(deviceQueryValue, deviceUserAgentExpression) {
|
||||
// return this.matchesQuery('ionicdevice', deviceQueryValue) ||
|
||||
// this.matchesUserAgent(deviceUserAgentExpression);
|
||||
// }
|
||||
|
||||
// registerChild(platformName, PlatformClass) {
|
||||
// let platform = new PlatformClass();
|
||||
// platform.name(platformName);
|
||||
// platform.parent(this);
|
||||
// this._chld[platformName] = platform;
|
||||
// }
|
||||
|
||||
// root() {
|
||||
// let rootPlatform = this;
|
||||
// while (rootPlatform._parent) {
|
||||
// rootPlatform = rootPlatform._parent;
|
||||
// }
|
||||
// return rootPlatform;
|
||||
// }
|
||||
|
||||
// runAll() {
|
||||
// let platform = null;
|
||||
|
||||
// if (this.isMatch()) {
|
||||
// this.run();
|
||||
|
||||
// for (let childPlatform in this._chld) {
|
||||
// this._chld[childPlatform].app(this._app);
|
||||
// this._chld[childPlatform].runAll();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// getActive() {
|
||||
// let platform = null;
|
||||
|
||||
// if (this.isMatch()) {
|
||||
// for (let childPlatform in this._chld) {
|
||||
// this._chld[childPlatform].app(this._app);
|
||||
// platform = this._chld[childPlatform].getActive();
|
||||
// if (platform) {
|
||||
// return platform;
|
||||
// }
|
||||
// }
|
||||
|
||||
// return this;
|
||||
// }
|
||||
|
||||
// return null;
|
||||
// }
|
||||
|
||||
|
||||
// /* Methods to Override */
|
||||
// isMatch() { return true; }
|
||||
// run() {}
|
||||
|
||||
|
||||
// /* Static Methods */
|
||||
// static register(platformName, PlatformClass) {
|
||||
// basePlatform.registerChild(platformName, PlatformClass);
|
||||
// }
|
||||
|
||||
// static getActivePlatform(app) {
|
||||
// basePlatform.app(app);
|
||||
// return basePlatform.getActive(app);
|
||||
// }
|
||||
|
||||
// static setBase(PlatformClass) {
|
||||
// basePlatform = new PlatformClass();
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
// let basePlatform = null;
|
||||
|
||||
|
||||
console.log('')
|
||||
Reference in New Issue
Block a user