Skip to content

Commit f37b196

Browse files
author
Adam Bradley
committed
refactor ionic.Platform methods
1 parent 27f3d56 commit f37b196

File tree

7 files changed

+176
-34
lines changed

7 files changed

+176
-34
lines changed

dist/js/ionic-angular.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,7 @@ angular.module('ionic.ui.checkbox', [])
12821282
(function() {
12831283
'use strict';
12841284

1285-
angular.module('ionic.ui.content', ['ionic.ui.service', 'ionic.service.platform'])
1285+
angular.module('ionic.ui.content', ['ionic.ui.service'])
12861286

12871287
/**
12881288
* Panel is a simple 100% width and height, fixed panel. It's meant for content to be
@@ -1299,7 +1299,7 @@ angular.module('ionic.ui.content', ['ionic.ui.service', 'ionic.service.platform'
12991299

13001300
// The content directive is a core scrollable content area
13011301
// that is part of many View hierarchies
1302-
.directive('content', ['$parse', '$timeout', '$ionicPlatform', '$ionicScrollDelegate', function($parse, $timeout, $ionicPlatform, $ionicScrollDelegate) {
1302+
.directive('content', ['$parse', '$timeout', '$ionicScrollDelegate', function($parse, $timeout, $ionicScrollDelegate) {
13031303
return {
13041304
restrict: 'E',
13051305
replace: true,
@@ -1364,7 +1364,7 @@ angular.module('ionic.ui.content', ['ionic.ui.service', 'ionic.service.platform'
13641364

13651365
// Otherwise, supercharge this baby!
13661366
var hasBouncing = $scope.$eval($scope.hasBouncing);
1367-
var enableBouncing = (!$ionicPlatform.is('Android') && hasBouncing !== false) || hasBouncing === true;
1367+
var enableBouncing = (!ionic.Platform.isAndroid() && hasBouncing !== false) || hasBouncing === true;
13681368
// No bouncing by default for Android users, lest they take up pitchforks
13691369
// to our bouncing goodness
13701370
sv = new ionic.views.Scroll({

dist/js/ionic-angular.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/ionic.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,29 +1798,31 @@ window.ionic = {
17981798
if(this.isCordova()) {
17991799
this.platforms.push('cordova');
18001800
}
1801-
if(this.isIOS7()) {
1802-
this.platforms.push('ios7');
1801+
if(this.isIOS()) {
1802+
this.platforms.push('ios');
1803+
this.platforms.push('ios' + parseInt(this.version(), 10));
18031804
}
18041805
if(this.isIPad()) {
18051806
this.platforms.push('ipad');
18061807
}
18071808
if(this.isAndroid()) {
18081809
this.platforms.push('android');
1810+
this.platforms.push('android' + parseInt(this.version(), 10));
18091811
}
18101812
},
18111813

18121814
// Check if we are running in Cordova
18131815
isCordova: function() {
1814-
return (window.cordova || window.PhoneGap || window.phonegap);
1816+
return !(!window.cordova && !window.PhoneGap && !window.phonegap);
18151817
},
18161818
isIPad: function() {
18171819
return navigator.userAgent.toLowerCase().indexOf('ipad') >= 0;
18181820
},
1819-
isIOS7: function() {
1820-
return this.platform() == 'ios' && this.version() >= 7.0;
1821+
isIOS: function() {
1822+
return this.is('ios');
18211823
},
18221824
isAndroid: function() {
1823-
return this.platform() === "android";
1825+
return this.is('android');
18241826
},
18251827

18261828
platform: function() {
@@ -1829,8 +1831,8 @@ window.ionic = {
18291831
return platformName;
18301832
},
18311833

1832-
setPlatform: function(name) {
1833-
if(name) platformName = name.toLowerCase();
1834+
setPlatform: function(n) {
1835+
platformName = n;
18341836
},
18351837

18361838
version: function() {
@@ -1840,14 +1842,19 @@ window.ionic = {
18401842
},
18411843

18421844
setVersion: function(v) {
1843-
if( !isNaN(v) ) version = parseFloat(v);
1845+
if(v) {
1846+
v = v.split('.');
1847+
platformVersion = parseFloat(v[0] + '.' + (v.length > 1 ? v[1] : 0));
1848+
} else {
1849+
platformVersion = 0;
1850+
}
18441851
},
18451852

18461853
// Check if the platform is the one detected by cordova
18471854
is: function(type) {
18481855
var pName = this.platform();
18491856
if(pName) {
1850-
return pName === type.toLowerCase();
1857+
return pName.toLowerCase() === type.toLowerCase();
18511858
}
18521859
// A quick hack for
18531860
return navigator.userAgent.toLowerCase().indexOf(type.toLowerCase()) >= 0;
@@ -1895,8 +1902,8 @@ window.ionic = {
18951902

18961903
};
18971904

1898-
var platformName,
1899-
platformVersion,
1905+
var platformName, // just the name, like iOS or Android
1906+
platformVersion, // a float of the major and minor, like 7.1
19001907
readyCallbacks = [];
19011908

19021909
// setup listeners to know when the device is ready to go

dist/js/ionic.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/ext/angular/src/directive/ionicContent.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(function() {
22
'use strict';
33

4-
angular.module('ionic.ui.content', ['ionic.ui.service', 'ionic.service.platform'])
4+
angular.module('ionic.ui.content', ['ionic.ui.service'])
55

66
/**
77
* Panel is a simple 100% width and height, fixed panel. It's meant for content to be
@@ -18,7 +18,7 @@ angular.module('ionic.ui.content', ['ionic.ui.service', 'ionic.service.platform'
1818

1919
// The content directive is a core scrollable content area
2020
// that is part of many View hierarchies
21-
.directive('content', ['$parse', '$timeout', '$ionicPlatform', '$ionicScrollDelegate', function($parse, $timeout, $ionicPlatform, $ionicScrollDelegate) {
21+
.directive('content', ['$parse', '$timeout', '$ionicScrollDelegate', function($parse, $timeout, $ionicScrollDelegate) {
2222
return {
2323
restrict: 'E',
2424
replace: true,
@@ -83,7 +83,7 @@ angular.module('ionic.ui.content', ['ionic.ui.service', 'ionic.service.platform'
8383

8484
// Otherwise, supercharge this baby!
8585
var hasBouncing = $scope.$eval($scope.hasBouncing);
86-
var enableBouncing = (!$ionicPlatform.is('Android') && hasBouncing !== false) || hasBouncing === true;
86+
var enableBouncing = (!ionic.Platform.isAndroid() && hasBouncing !== false) || hasBouncing === true;
8787
// No bouncing by default for Android users, lest they take up pitchforks
8888
// to our bouncing goodness
8989
sv = new ionic.views.Scroll({
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
describe('Ionic Platform Service', function() {
2+
var window;
3+
4+
beforeEach(inject(function($window) {
5+
window = $window;
6+
}));
7+
8+
it('should set platform name', function() {
9+
ionic.Platform.setPlatform('Android');
10+
expect(ionic.Platform.platform()).toEqual('Android');
11+
12+
ionic.Platform.setPlatform('iOS');
13+
expect(ionic.Platform.platform()).toEqual('iOS');
14+
15+
ionic.Platform.setPlatform('wInDoWs');
16+
expect(ionic.Platform.platform()).toEqual('wInDoWs');
17+
18+
ionic.Platform.setPlatform('');
19+
expect(ionic.Platform.platform()).toEqual(undefined);
20+
21+
ionic.Platform.setPlatform();
22+
expect(ionic.Platform.platform()).toEqual(undefined);
23+
});
24+
25+
it('set version', function() {
26+
ionic.Platform.setVersion('1.2.3');
27+
expect(ionic.Platform.version()).toEqual(1.2);
28+
29+
ionic.Platform.setVersion('1.2');
30+
expect(ionic.Platform.version()).toEqual(1.2);
31+
32+
ionic.Platform.setVersion('1');
33+
expect(ionic.Platform.version()).toEqual(1.0);
34+
35+
ionic.Platform.setVersion(' ');
36+
expect(ionic.Platform.version()).toEqual(0);
37+
38+
ionic.Platform.setVersion('');
39+
expect(ionic.Platform.version()).toEqual(0);
40+
41+
ionic.Platform.setVersion(null);
42+
expect(ionic.Platform.version()).toEqual(0);
43+
44+
ionic.Platform.setVersion();
45+
expect(ionic.Platform.version()).toEqual(0);
46+
});
47+
48+
it('is iOS', function() {
49+
ionic.Platform.setPlatform('iOS');
50+
expect(ionic.Platform.isIOS()).toEqual(true);
51+
52+
ionic.Platform.setPlatform('ios');
53+
expect(ionic.Platform.isIOS()).toEqual(true);
54+
55+
ionic.Platform.setPlatform('Android');
56+
expect(ionic.Platform.isIOS()).toEqual(false);
57+
});
58+
59+
it('is Android', function() {
60+
ionic.Platform.setPlatform('Android');
61+
expect(ionic.Platform.isAndroid()).toEqual(true);
62+
63+
ionic.Platform.setPlatform('android');
64+
expect(ionic.Platform.isAndroid()).toEqual(true);
65+
66+
ionic.Platform.setPlatform('ios');
67+
expect(ionic.Platform.isAndroid()).toEqual(false);
68+
});
69+
70+
it('is Cordova', function() {
71+
expect(ionic.Platform.isCordova()).toEqual(false);
72+
window.cordova = {};
73+
expect(ionic.Platform.isCordova()).toEqual(true);
74+
delete window.cordova;
75+
window.PhoneGap = {};
76+
expect(ionic.Platform.isCordova()).toEqual(true);
77+
delete window.phonegap;
78+
window.phonegap = {};
79+
expect(ionic.Platform.isCordova()).toEqual(true);
80+
});
81+
82+
it('sets ios platforms', function() {
83+
window.cordova = {};
84+
ionic.Platform.setPlatform('iOS');
85+
ionic.Platform.setVersion('7.9.3');
86+
87+
ionic.Platform._checkPlatforms()
88+
89+
expect(ionic.Platform.platforms[0]).toEqual('cordova');
90+
expect(ionic.Platform.platforms[1]).toEqual('ios');
91+
expect(ionic.Platform.platforms[2]).toEqual('ios7');
92+
});
93+
94+
it('sets android platforms', function() {
95+
window.cordova = {};
96+
ionic.Platform.setPlatform('android');
97+
ionic.Platform.setVersion('4.4.4');
98+
99+
ionic.Platform._checkPlatforms()
100+
101+
expect(ionic.Platform.platforms[0]).toEqual('cordova');
102+
expect(ionic.Platform.platforms[1]).toEqual('android');
103+
expect(ionic.Platform.platforms[2]).toEqual('android4');
104+
});
105+
106+
it('is android', function() {
107+
ionic.Platform.setPlatform('AnDrOiD');
108+
expect(ionic.Platform.is('android')).toEqual(true);
109+
ionic.Platform.setPlatform('ANDROID');
110+
expect(ionic.Platform.is('android')).toEqual(true);
111+
ionic.Platform.setPlatform('android');
112+
expect(ionic.Platform.is('android')).toEqual(true);
113+
ionic.Platform.setPlatform('ios');
114+
expect(ionic.Platform.is('android')).toEqual(false);
115+
});
116+
117+
it('is android', function() {
118+
ionic.Platform.setPlatform('iOs');
119+
expect(ionic.Platform.is('ios')).toEqual(true);
120+
ionic.Platform.setPlatform('iOs');
121+
expect(ionic.Platform.is('IOS')).toEqual(true);
122+
ionic.Platform.setPlatform('IOS');
123+
expect(ionic.Platform.is('ios')).toEqual(true);
124+
ionic.Platform.setPlatform('IOS');
125+
expect(ionic.Platform.is('android')).toEqual(false);
126+
});
127+
128+
});

js/utils/platform.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,31 @@
4242
if(this.isCordova()) {
4343
this.platforms.push('cordova');
4444
}
45-
if(this.isIOS7()) {
46-
this.platforms.push('ios7');
45+
if(this.isIOS()) {
46+
this.platforms.push('ios');
47+
this.platforms.push('ios' + parseInt(this.version(), 10));
4748
}
4849
if(this.isIPad()) {
4950
this.platforms.push('ipad');
5051
}
5152
if(this.isAndroid()) {
5253
this.platforms.push('android');
54+
this.platforms.push('android' + parseInt(this.version(), 10));
5355
}
5456
},
5557

5658
// Check if we are running in Cordova
5759
isCordova: function() {
58-
return (window.cordova || window.PhoneGap || window.phonegap);
60+
return !(!window.cordova && !window.PhoneGap && !window.phonegap);
5961
},
6062
isIPad: function() {
6163
return navigator.userAgent.toLowerCase().indexOf('ipad') >= 0;
6264
},
63-
isIOS7: function() {
64-
return this.platform() == 'ios' && this.version() >= 7.0;
65+
isIOS: function() {
66+
return this.is('ios');
6567
},
6668
isAndroid: function() {
67-
return this.platform() === "android";
69+
return this.is('android');
6870
},
6971

7072
platform: function() {
@@ -73,8 +75,8 @@
7375
return platformName;
7476
},
7577

76-
setPlatform: function(name) {
77-
if(name) platformName = name.toLowerCase();
78+
setPlatform: function(n) {
79+
platformName = n;
7880
},
7981

8082
version: function() {
@@ -84,14 +86,19 @@
8486
},
8587

8688
setVersion: function(v) {
87-
if( !isNaN(v) ) version = parseFloat(v);
89+
if(v) {
90+
v = v.split('.');
91+
platformVersion = parseFloat(v[0] + '.' + (v.length > 1 ? v[1] : 0));
92+
} else {
93+
platformVersion = 0;
94+
}
8895
},
8996

9097
// Check if the platform is the one detected by cordova
9198
is: function(type) {
9299
var pName = this.platform();
93100
if(pName) {
94-
return pName === type.toLowerCase();
101+
return pName.toLowerCase() === type.toLowerCase();
95102
}
96103
// A quick hack for
97104
return navigator.userAgent.toLowerCase().indexOf(type.toLowerCase()) >= 0;
@@ -139,8 +146,8 @@
139146

140147
};
141148

142-
var platformName,
143-
platformVersion,
149+
var platformName, // just the name, like iOS or Android
150+
platformVersion, // a float of the major and minor, like 7.1
144151
readyCallbacks = [];
145152

146153
// setup listeners to know when the device is ready to go

0 commit comments

Comments
 (0)