index-cc858e97.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. 'use strict';
  5. // TODO(FW-2832): types
  6. class Config {
  7. constructor() {
  8. this.m = new Map();
  9. }
  10. reset(configObj) {
  11. this.m = new Map(Object.entries(configObj));
  12. }
  13. get(key, fallback) {
  14. const value = this.m.get(key);
  15. return value !== undefined ? value : fallback;
  16. }
  17. getBoolean(key, fallback = false) {
  18. const val = this.m.get(key);
  19. if (val === undefined) {
  20. return fallback;
  21. }
  22. if (typeof val === 'string') {
  23. return val === 'true';
  24. }
  25. return !!val;
  26. }
  27. getNumber(key, fallback) {
  28. const val = parseFloat(this.m.get(key));
  29. return isNaN(val) ? (fallback !== undefined ? fallback : NaN) : val;
  30. }
  31. set(key, value) {
  32. this.m.set(key, value);
  33. }
  34. }
  35. const config = /*@__PURE__*/ new Config();
  36. const configFromSession = (win) => {
  37. try {
  38. const configStr = win.sessionStorage.getItem(IONIC_SESSION_KEY);
  39. return configStr !== null ? JSON.parse(configStr) : {};
  40. }
  41. catch (e) {
  42. return {};
  43. }
  44. };
  45. const saveConfig = (win, c) => {
  46. try {
  47. win.sessionStorage.setItem(IONIC_SESSION_KEY, JSON.stringify(c));
  48. }
  49. catch (e) {
  50. return;
  51. }
  52. };
  53. const configFromURL = (win) => {
  54. const configObj = {};
  55. win.location.search
  56. .slice(1)
  57. .split('&')
  58. .map((entry) => entry.split('='))
  59. .map(([key, value]) => {
  60. try {
  61. return [decodeURIComponent(key), decodeURIComponent(value)];
  62. }
  63. catch (e) {
  64. return ['', ''];
  65. }
  66. })
  67. .filter(([key]) => startsWith(key, IONIC_PREFIX))
  68. .map(([key, value]) => [key.slice(IONIC_PREFIX.length), value])
  69. .forEach(([key, value]) => {
  70. configObj[key] = value;
  71. });
  72. return configObj;
  73. };
  74. const startsWith = (input, search) => {
  75. return input.substr(0, search.length) === search;
  76. };
  77. const IONIC_PREFIX = 'ionic:';
  78. const IONIC_SESSION_KEY = 'ionic-persist-config';
  79. exports.LogLevel = void 0;
  80. (function (LogLevel) {
  81. LogLevel["OFF"] = "OFF";
  82. LogLevel["ERROR"] = "ERROR";
  83. LogLevel["WARN"] = "WARN";
  84. })(exports.LogLevel || (exports.LogLevel = {}));
  85. /**
  86. * Logs a warning to the console with an Ionic prefix
  87. * to indicate the library that is warning the developer.
  88. *
  89. * @param message - The string message to be logged to the console.
  90. */
  91. const printIonWarning = (message, ...params) => {
  92. const logLevel = config.get('logLevel', exports.LogLevel.WARN);
  93. if ([exports.LogLevel.WARN].includes(logLevel)) {
  94. return console.warn(`[Ionic Warning]: ${message}`, ...params);
  95. }
  96. };
  97. /**
  98. * Logs an error to the console with an Ionic prefix
  99. * to indicate the library that is warning the developer.
  100. *
  101. * @param message - The string message to be logged to the console.
  102. * @param params - Additional arguments to supply to the console.error.
  103. */
  104. const printIonError = (message, ...params) => {
  105. const logLevel = config.get('logLevel', exports.LogLevel.ERROR);
  106. if ([exports.LogLevel.ERROR, exports.LogLevel.WARN].includes(logLevel)) {
  107. return console.error(`[Ionic Error]: ${message}`, ...params);
  108. }
  109. };
  110. /**
  111. * Prints an error informing developers that an implementation requires an element to be used
  112. * within a specific selector.
  113. *
  114. * @param el The web component element this is requiring the element.
  115. * @param targetSelectors The selector or selectors that were not found.
  116. */
  117. const printRequiredElementError = (el, ...targetSelectors) => {
  118. return console.error(`<${el.tagName.toLowerCase()}> must be used inside ${targetSelectors.join(' or ')}.`);
  119. };
  120. exports.config = config;
  121. exports.configFromSession = configFromSession;
  122. exports.configFromURL = configFromURL;
  123. exports.printIonError = printIonError;
  124. exports.printIonWarning = printIonWarning;
  125. exports.printRequiredElementError = printRequiredElementError;
  126. exports.saveConfig = saveConfig;