Skip to content

Commit

Permalink
init / migrate from ember-cordova/platform
Browse files Browse the repository at this point in the history
  • Loading branch information
alexblom committed Aug 22, 2017
1 parent 5cec03f commit 058a319
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 5 deletions.
175 changes: 175 additions & 0 deletions addon/services/ember-cordova/platform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import Ember from 'ember';

const IOS = 'ios';
const ANDROID = 'android';
const WINDOWS_PHONE = 'windowsphone';
const EDGE = 'edge';
const CROSSWALK = 'crosswalk';

const {
Service,
computed
} = Ember;

/*
Heavily borrowed from the Ionic Platform implementation,
and re-purposed for ember-cordova applications.
Source: https://github.com/driftyco/ionic/blob/master/js/utils/platform.js
*/

export default Ember.Service.extend({
navigator: window.navigator,
ua: navigator.userAgent,

platforms: [],

init() {
this._super(...arguments);
this._setPlatforms();
},

isHybrid: computed(function() {
return !(
!window.cordova &&
!window.PhoneGap &&
!window.phonegap &&
window.forge !== 'object'
);
}),

isCordova: computed(function() {
return window.cordova !== undefined;
}),

isIPad: computed(function() {
if (/iPad/i.test(window.navigator.platform)) {
return true;
}
return /iPad/i.test(window.ua);
}),

isIOS: computed(function() {
return this.is(IOS);
}),

isAndroid: computed(function() {
return this.is(ANDROID);
}),

isWindowsPhone: computed(function() {
return this.is(WINDOWS_PHONE);
}),

isEdge: computed(function() {
return this.is(EDGE);
}),

isCrosswalk: computed(function() {
return this.is(CROSSWALK);
}),

platform: Ember.computed(function() {
const ua = this.get('ua');
let platformName;

if (ua.indexOf('Edge') > -1) {
platformName = EDGE;
} else if (ua.indexOf('Windows Phone') > -1) {
platformName = WINDOWS_PHONE;
} else if (ua.indexOf('Android') > 0) {
platformName = ANDROID;
} else if (/iPhone|iPad|iPod/.test(ua)) {
platformName = IOS;
} else {
let navPlatform = navigator.platform;
if (navPlatform) {
platformName = navigator.platform.toLowerCase().split(' ')[0];
} else {
platformName = '';
}
}

return platformName;
}),

version: computed(function() {
let v = this.get('device.version');

if (!v) {
return undefined;
}

v = parseFloat(v[0] + '.' + (v.length > 1 ? v[1] : 0));
if (!isNaN(v)) {
return v;
}
}),

device: computed(function() {
return window.device || {};
}),

is: function(type) {
type = type.toLowerCase();
// check if it has an array of platforms
let platforms = this.get('platforms');
if (platforms) {
for (let x = 0; x < platforms.length; x++) {
if (platforms[x] === type) {
return true;
}
}
}
// exact match
const pName = this.get('platform');
if (pName) {
return pName === type.toLowerCase();
}

// A quick hack for to check userAgent
return this.get('ua').toLowerCase().indexOf(type) >= 0;
},

_setPlatforms() {
let _this = this;
let platforms = [];

if (_this.get('isWebView')) {
platforms.push('webview');
if (!(!window.cordova && !window.PhoneGap && !window.phonegap)) {
platforms.push('cordova');
} else if (typeof window.forge === 'object') {
platforms.push('trigger');
}
} else {
platforms.push('browser');
}

if (_this.get('isIPad')) {
platforms.push('ipad');
};

const platform = _this.get('platform');

if (platform) {
platforms.push(platform);

const version = _this.get('version');
if (version) {
let v = version.toString();
if (v.indexOf('.') > 0) {
v = v.replace('.', '_');
} else {
v += '_0';
}
platforms.push(platform + v.split('_')[0]);
platforms.push(platform + v);
}
}

_this.set('platforms', platforms);
}
});
1 change: 1 addition & 0 deletions app/services/ember-cordova/platform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-cordova-platform/services/ember-cordova/platform';
25 changes: 20 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
{
"name": "ember-cordova-platform",
"version": "0.0.0",
"version": "0.1.0",
"description": "The default blueprint for ember-cli addons.",
"keywords": [
"ember-addon"
],
"license": "MIT",
"author": "",
"author": {
"name": "Alex Blom",
"email": "[email protected]",
"url": "https://isleofcode.com"
},

"contributors": [
{
"name": "Aidan Nulman",
"email": "[email protected]",
"url": "https://isleofcode.com"
}
],
"keywords": [
"ember-addon",
"cordova",
"ember-cordova",
"ember-cordova-platform"
],
"directories": {
"doc": "doc",
"test": "tests"
},
"repository": "",
"scripts": {
"build": "ember build",
"start": "ember server",
"test": "ember try:each"
},
"dependencies": {
"ember-cli-babel": "^6.0.0"
},
"devDependencies": {
"broccoli-asset-rev": "^2.4.5",
"ember-ajax": "^3.0.0",
"ember-cli": "~2.14.0-beta.1",
"ember-cli-dependency-checker": "^1.3.0",
"ember-cli-eslint": "^3.0.0",
Expand Down

0 comments on commit 058a319

Please sign in to comment.