ionic-global.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. import { setMode, getMode } from '@stencil/core/internal/client';
  5. import { c as config, b as configFromSession, d as configFromURL, s as saveConfig, a as printIonWarning } from './index4.js';
  6. const getPlatforms = (win) => setupPlatforms(win);
  7. const isPlatform = (winOrPlatform, platform) => {
  8. if (typeof winOrPlatform === 'string') {
  9. platform = winOrPlatform;
  10. winOrPlatform = undefined;
  11. }
  12. return getPlatforms(winOrPlatform).includes(platform);
  13. };
  14. const setupPlatforms = (win = window) => {
  15. if (typeof win === 'undefined') {
  16. return [];
  17. }
  18. win.Ionic = win.Ionic || {};
  19. let platforms = win.Ionic.platforms;
  20. if (platforms == null) {
  21. platforms = win.Ionic.platforms = detectPlatforms(win);
  22. platforms.forEach((p) => win.document.documentElement.classList.add(`plt-${p}`));
  23. }
  24. return platforms;
  25. };
  26. const detectPlatforms = (win) => {
  27. const customPlatformMethods = config.get('platform');
  28. return Object.keys(PLATFORMS_MAP).filter((p) => {
  29. const customMethod = customPlatformMethods === null || customPlatformMethods === void 0 ? void 0 : customPlatformMethods[p];
  30. return typeof customMethod === 'function' ? customMethod(win) : PLATFORMS_MAP[p](win);
  31. });
  32. };
  33. const isMobileWeb = (win) => isMobile(win) && !isHybrid(win);
  34. const isIpad = (win) => {
  35. // iOS 12 and below
  36. if (testUserAgent(win, /iPad/i)) {
  37. return true;
  38. }
  39. // iOS 13+
  40. if (testUserAgent(win, /Macintosh/i) && isMobile(win)) {
  41. return true;
  42. }
  43. return false;
  44. };
  45. const isIphone = (win) => testUserAgent(win, /iPhone/i);
  46. const isIOS = (win) => testUserAgent(win, /iPhone|iPod/i) || isIpad(win);
  47. const isAndroid = (win) => testUserAgent(win, /android|sink/i);
  48. const isAndroidTablet = (win) => {
  49. return isAndroid(win) && !testUserAgent(win, /mobile/i);
  50. };
  51. const isPhablet = (win) => {
  52. const width = win.innerWidth;
  53. const height = win.innerHeight;
  54. const smallest = Math.min(width, height);
  55. const largest = Math.max(width, height);
  56. return smallest > 390 && smallest < 520 && largest > 620 && largest < 800;
  57. };
  58. const isTablet = (win) => {
  59. const width = win.innerWidth;
  60. const height = win.innerHeight;
  61. const smallest = Math.min(width, height);
  62. const largest = Math.max(width, height);
  63. return isIpad(win) || isAndroidTablet(win) || (smallest > 460 && smallest < 820 && largest > 780 && largest < 1400);
  64. };
  65. const isMobile = (win) => matchMedia(win, '(any-pointer:coarse)');
  66. const isDesktop = (win) => !isMobile(win);
  67. const isHybrid = (win) => isCordova(win) || isCapacitorNative(win);
  68. const isCordova = (win) => !!(win['cordova'] || win['phonegap'] || win['PhoneGap']);
  69. const isCapacitorNative = (win) => {
  70. const capacitor = win['Capacitor'];
  71. // TODO(ROU-11693): Remove when we no longer support Capacitor 2, which does not have isNativePlatform
  72. return !!((capacitor === null || capacitor === void 0 ? void 0 : capacitor.isNative) || ((capacitor === null || capacitor === void 0 ? void 0 : capacitor.isNativePlatform) && !!capacitor.isNativePlatform()));
  73. };
  74. const isElectron = (win) => testUserAgent(win, /electron/i);
  75. const isPWA = (win) => { var _a; return !!(((_a = win.matchMedia) === null || _a === void 0 ? void 0 : _a.call(win, '(display-mode: standalone)').matches) || win.navigator.standalone); };
  76. const testUserAgent = (win, expr) => expr.test(win.navigator.userAgent);
  77. const matchMedia = (win, query) => { var _a; return (_a = win.matchMedia) === null || _a === void 0 ? void 0 : _a.call(win, query).matches; };
  78. const PLATFORMS_MAP = {
  79. ipad: isIpad,
  80. iphone: isIphone,
  81. ios: isIOS,
  82. android: isAndroid,
  83. phablet: isPhablet,
  84. tablet: isTablet,
  85. cordova: isCordova,
  86. capacitor: isCapacitorNative,
  87. electron: isElectron,
  88. pwa: isPWA,
  89. mobile: isMobile,
  90. mobileweb: isMobileWeb,
  91. desktop: isDesktop,
  92. hybrid: isHybrid,
  93. };
  94. // TODO(FW-2832): types
  95. let defaultMode;
  96. const getIonMode = (ref) => {
  97. return (ref && getMode(ref)) || defaultMode;
  98. };
  99. const initialize = (userConfig = {}) => {
  100. if (typeof window === 'undefined') {
  101. return;
  102. }
  103. const doc = window.document;
  104. const win = window;
  105. const Ionic = (win.Ionic = win.Ionic || {});
  106. // create the Ionic.config from raw config object (if it exists)
  107. // and convert Ionic.config into a ConfigApi that has a get() fn
  108. const configObj = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, configFromSession(win)), { persistConfig: false }), Ionic.config), configFromURL(win)), userConfig);
  109. config.reset(configObj);
  110. if (config.getBoolean('persistConfig')) {
  111. saveConfig(win, configObj);
  112. }
  113. // Setup platforms
  114. setupPlatforms(win);
  115. // first see if the mode was set as an attribute on <html>
  116. // which could have been set by the user, or by pre-rendering
  117. // otherwise get the mode via config settings, and fallback to md
  118. Ionic.config = config;
  119. Ionic.mode = defaultMode = config.get('mode', doc.documentElement.getAttribute('mode') || (isPlatform(win, 'ios') ? 'ios' : 'md'));
  120. config.set('mode', defaultMode);
  121. doc.documentElement.setAttribute('mode', defaultMode);
  122. doc.documentElement.classList.add(defaultMode);
  123. if (config.getBoolean('_testing')) {
  124. config.set('animated', false);
  125. }
  126. const isIonicElement = (elm) => { var _a; return (_a = elm.tagName) === null || _a === void 0 ? void 0 : _a.startsWith('ION-'); };
  127. const isAllowedIonicModeValue = (elmMode) => ['ios', 'md'].includes(elmMode);
  128. setMode((elm) => {
  129. while (elm) {
  130. const elmMode = elm.mode || elm.getAttribute('mode');
  131. if (elmMode) {
  132. if (isAllowedIonicModeValue(elmMode)) {
  133. return elmMode;
  134. }
  135. else if (isIonicElement(elm)) {
  136. printIonWarning('Invalid ionic mode: "' + elmMode + '", expected: "ios" or "md"');
  137. }
  138. }
  139. elm = elm.parentElement;
  140. }
  141. return defaultMode;
  142. });
  143. };
  144. export { isPlatform as a, getIonMode as b, getPlatforms as g, initialize as i };