forked from NathanaelA/nativescript-platform-css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatformcss.js
120 lines (104 loc) · 2.98 KB
/
platformcss.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**********************************************************************************
* (c) 2016-2019, Master Technology
* Licensed under the MIT license or contact me for a Support or Commercial License
*
* I do contract work in most languages, so let me solve your problems!
*
* Any questions please feel free to email me or put a issue up on the github repo
* Version 1.6.7 [email protected]
*********************************************************************************/
"use strict";
/* jshint camelcase: false */
/* global android, NSString, nsPlatform */
const Page = require('tns-core-modules/ui/page').Page;
require('nativescript-globalevents');
require('nativescript-platform');
/**
* Function that adds the proper class when we navigate to a new page
* @param args
*/
let deviceInfo, sizeGroupings = false;
let groupings = [1280,1024,800,600,540,480,400,360,320];
const setDevice = function(args) {
const currentPage = args.object;
let device;
if (!deviceInfo) {
switch (nsPlatform.platform) {
case nsPlatform.type.IOS:
device = 'ios ios';
break;
case nsPlatform.type.ANDROID:
device = 'android android';
break;
}
const screen = nsPlatform.screen;
if (sizeGroupings) {
let size = (screen.width < screen.height ? screen.width : screen.height);
let found = false;
for (let i=0;i<groupings.length;i++) {
if (size >= groupings[i]) {
device += groupings[i];
found = true;
break;
}
}
if (!found) {
device += size;
}
} else {
if (screen.width < screen.height) {
device += screen.width;
} else {
device += screen.height;
}
}
const deviceName = nsPlatform.device.name || '';
// Add device name; this is use
device += " " + deviceName.replace(/[^a-z0-9]/gmi,'').toLowerCase() + " " + nsPlatform.deviceType.toLowerCase();
if (nsPlatform.device.notch) {
device += " notch";
}
deviceInfo = device;
} else {
device = deviceInfo;
}
if (currentPage) {
if (nsPlatform.platform === nsPlatform.type.ANDROID && nsPlatform.hasSoftNav()) {
device += " softnav";
}
const data = currentPage.className || '';
if (data.length) {
currentPage.className = data + ' ' + device;
} else {
currentPage.className = device;
}
}
};
// Setup Events
Page.on(Page.navigatingToFirst, setDevice);
Page.on(Page.showingModallyFirst, setDevice);
exports.sizeGroupings = function(val) {
if (Array.isArray(val)) {
if (val.length === 0) {
sizeGroupings = false;
} else {
groupings = val.splice(0);
groupings.sort(function (x, y) {
if (x < y) {
return 1;
}
else if (x > y) {
return -1;
}
return 0;
});
sizeGroupings = true;
}
} else {
if (sizeGroupings === !!val) {
return;
}
sizeGroupings = !!val;
}
deviceInfo = null;
};