diff --git a/addon/services/ember-cordova/platform.js b/addon/services/ember-cordova/platform.js new file mode 100644 index 0000000..02548a3 --- /dev/null +++ b/addon/services/ember-cordova/platform.js @@ -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); + } +}); diff --git a/app/services/ember-cordova/platform.js b/app/services/ember-cordova/platform.js new file mode 100644 index 0000000..bbd8766 --- /dev/null +++ b/app/services/ember-cordova/platform.js @@ -0,0 +1 @@ +export { default } from 'ember-cordova-platform/services/ember-cordova/platform'; diff --git a/package.json b/package.json index 9f8c244..78bc53b 100644 --- a/package.json +++ b/package.json @@ -1,20 +1,36 @@ { "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": "alex@isleofcode.com", + "url": "https://isleofcode.com" + }, + + "contributors": [ + { + "name": "Aidan Nulman", + "email": "aidan@isleofcode.com", + "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": { @@ -22,7 +38,6 @@ }, "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",