index4.js 3.7 KB

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