UNPKG

6.8 kBJavaScriptView Raw
1'use strict';
2
3const index = require('./index-a0a08b2a.js');
4
5const getPlatforms = (win) => setupPlatforms(win);
6const isPlatform = (winOrPlatform, platform) => {
7 if (typeof winOrPlatform === 'string') {
8 platform = winOrPlatform;
9 winOrPlatform = undefined;
10 }
11 return getPlatforms(winOrPlatform).includes(platform);
12};
13const setupPlatforms = (win = window) => {
14 if (typeof win === 'undefined') {
15 return [];
16 }
17 win.Ionic = win.Ionic || {};
18 let platforms = win.Ionic.platforms;
19 if (platforms == null) {
20 platforms = win.Ionic.platforms = detectPlatforms(win);
21 platforms.forEach(p => win.document.documentElement.classList.add(`plt-${p}`));
22 }
23 return platforms;
24};
25const detectPlatforms = (win) => Object.keys(PLATFORMS_MAP).filter(p => PLATFORMS_MAP[p](win));
26const isMobileWeb = (win) => isMobile(win) && !isHybrid(win);
27const isIpad = (win) => {
28 // iOS 12 and below
29 if (testUserAgent(win, /iPad/i)) {
30 return true;
31 }
32 // iOS 13+
33 if (testUserAgent(win, /Macintosh/i) && isMobile(win)) {
34 return true;
35 }
36 return false;
37};
38const isIphone = (win) => testUserAgent(win, /iPhone/i);
39const isIOS = (win) => testUserAgent(win, /iPhone|iPod/i) || isIpad(win);
40const isAndroid = (win) => testUserAgent(win, /android|sink/i);
41const isAndroidTablet = (win) => {
42 return isAndroid(win) && !testUserAgent(win, /mobile/i);
43};
44const isPhablet = (win) => {
45 const width = win.innerWidth;
46 const height = win.innerHeight;
47 const smallest = Math.min(width, height);
48 const largest = Math.max(width, height);
49 return (smallest > 390 && smallest < 520) &&
50 (largest > 620 && largest < 800);
51};
52const isTablet = (win) => {
53 const width = win.innerWidth;
54 const height = win.innerHeight;
55 const smallest = Math.min(width, height);
56 const largest = Math.max(width, height);
57 return (isIpad(win) ||
58 isAndroidTablet(win) ||
59 ((smallest > 460 && smallest < 820) &&
60 (largest > 780 && largest < 1400)));
61};
62const isMobile = (win) => matchMedia(win, '(any-pointer:coarse)');
63const isDesktop = (win) => !isMobile(win);
64const isHybrid = (win) => isCordova(win) || isCapacitorNative(win);
65const isCordova = (win) => !!(win['cordova'] || win['phonegap'] || win['PhoneGap']);
66const isCapacitorNative = (win) => {
67 const capacitor = win['Capacitor'];
68 return !!(capacitor && capacitor.isNative);
69};
70const isElectron = (win) => testUserAgent(win, /electron/i);
71const isPWA = (win) => !!(win.matchMedia('(display-mode: standalone)').matches || win.navigator.standalone);
72const testUserAgent = (win, expr) => expr.test(win.navigator.userAgent);
73const matchMedia = (win, query) => win.matchMedia(query).matches;
74const PLATFORMS_MAP = {
75 'ipad': isIpad,
76 'iphone': isIphone,
77 'ios': isIOS,
78 'android': isAndroid,
79 'phablet': isPhablet,
80 'tablet': isTablet,
81 'cordova': isCordova,
82 'capacitor': isCapacitorNative,
83 'electron': isElectron,
84 'pwa': isPWA,
85 'mobile': isMobile,
86 'mobileweb': isMobileWeb,
87 'desktop': isDesktop,
88 'hybrid': isHybrid
89};
90
91class Config {
92 constructor() {
93 this.m = new Map();
94 }
95 reset(configObj) {
96 this.m = new Map(Object.entries(configObj));
97 }
98 get(key, fallback) {
99 const value = this.m.get(key);
100 return value !== undefined ? value : fallback;
101 }
102 getBoolean(key, fallback = false) {
103 const val = this.m.get(key);
104 if (val === undefined) {
105 return fallback;
106 }
107 if (typeof val === 'string') {
108 return val === 'true';
109 }
110 return !!val;
111 }
112 getNumber(key, fallback) {
113 const val = parseFloat(this.m.get(key));
114 return isNaN(val) ? (fallback !== undefined ? fallback : NaN) : val;
115 }
116 set(key, value) {
117 this.m.set(key, value);
118 }
119}
120const config = /*@__PURE__*/ new Config();
121const configFromSession = (win) => {
122 try {
123 const configStr = win.sessionStorage.getItem(IONIC_SESSION_KEY);
124 return configStr !== null ? JSON.parse(configStr) : {};
125 }
126 catch (e) {
127 return {};
128 }
129};
130const saveConfig = (win, c) => {
131 try {
132 win.sessionStorage.setItem(IONIC_SESSION_KEY, JSON.stringify(c));
133 }
134 catch (e) {
135 return;
136 }
137};
138const configFromURL = (win) => {
139 const configObj = {};
140 win.location.search
141 .slice(1)
142 .split('&')
143 .map(entry => entry.split('='))
144 .map(([key, value]) => [decodeURIComponent(key), decodeURIComponent(value)])
145 .filter(([key]) => startsWith(key, IONIC_PREFIX))
146 .map(([key, value]) => [key.slice(IONIC_PREFIX.length), value])
147 .forEach(([key, value]) => {
148 configObj[key] = value;
149 });
150 return configObj;
151};
152const startsWith = (input, search) => {
153 return input.substr(0, search.length) === search;
154};
155const IONIC_PREFIX = 'ionic:';
156const IONIC_SESSION_KEY = 'ionic-persist-config';
157
158let defaultMode;
159const getIonMode = (ref) => {
160 return (ref && index.getMode(ref)) || defaultMode;
161};
162const initialize = (userConfig = {}) => {
163 if (typeof window === 'undefined') {
164 return;
165 }
166 const doc = window.document;
167 const win = window;
168 const Ionic = win.Ionic = win.Ionic || {};
169 // Setup platforms
170 setupPlatforms(win);
171 // create the Ionic.config from raw config object (if it exists)
172 // and convert Ionic.config into a ConfigApi that has a get() fn
173 const configObj = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, configFromSession(win)), { persistConfig: false }), Ionic.config), configFromURL(win)), userConfig);
174 config.reset(configObj);
175 if (config.getBoolean('persistConfig')) {
176 saveConfig(win, configObj);
177 }
178 // first see if the mode was set as an attribute on <html>
179 // which could have been set by the user, or by pre-rendering
180 // otherwise get the mode via config settings, and fallback to md
181 Ionic.config = config;
182 Ionic.mode = defaultMode = config.get('mode', (doc.documentElement.getAttribute('mode')) || (isPlatform(win, 'ios') ? 'ios' : 'md'));
183 config.set('mode', defaultMode);
184 doc.documentElement.setAttribute('mode', defaultMode);
185 doc.documentElement.classList.add(defaultMode);
186 if (config.getBoolean('_testing')) {
187 config.set('animated', false);
188 }
189 const isIonicElement = (elm) => elm.tagName && elm.tagName.startsWith('ION-');
190 const isAllowedIonicModeValue = (elmMode) => ['ios', 'md'].includes(elmMode);
191 index.setMode((elm) => {
192 while (elm) {
193 const elmMode = elm.mode || elm.getAttribute('mode');
194 if (elmMode) {
195 if (isAllowedIonicModeValue(elmMode)) {
196 return elmMode;
197 }
198 else if (isIonicElement(elm)) {
199 console.warn('Invalid ionic mode: "' + elmMode + '", expected: "ios" or "md"');
200 }
201 }
202 elm = elm.parentElement;
203 }
204 return defaultMode;
205 });
206};
207
208exports.config = config;
209exports.getIonMode = getIonMode;
210exports.getPlatforms = getPlatforms;
211exports.initialize = initialize;
212exports.isPlatform = isPlatform;