utils-2c56d1c8.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { g as getAssetPath } from './index-b72adede.js';
  2. let CACHED_MAP;
  3. const getIconMap = () => {
  4. if (typeof window === 'undefined') {
  5. return new Map();
  6. }
  7. else {
  8. if (!CACHED_MAP) {
  9. const win = window;
  10. win.Ionicons = win.Ionicons || {};
  11. CACHED_MAP = win.Ionicons.map = win.Ionicons.map || new Map();
  12. }
  13. return CACHED_MAP;
  14. }
  15. };
  16. const addIcons = (icons) => {
  17. Object.keys(icons).forEach(name => {
  18. addToIconMap(name, icons[name]);
  19. /**
  20. * Developers can also pass in the SVG object directly
  21. * and Ionicons can map the object to a kebab case name.
  22. * Example: addIcons({ addCircleOutline });
  23. * This will create an "addCircleOutline" entry and
  24. * an "add-circle-outline" entry.
  25. * Usage: <ion-icon name="add-circle-outline"></ion-icon>
  26. * Using name="addCircleOutline" is valid too, but the
  27. * kebab case naming is preferred.
  28. */
  29. const toKebabCase = name.replace(/([a-z0-9]|(?=[A-Z]))([A-Z0-9])/g, "$1-$2").toLowerCase();
  30. if (name !== toKebabCase) {
  31. addToIconMap(toKebabCase, icons[name]);
  32. }
  33. });
  34. };
  35. const addToIconMap = (name, data) => {
  36. const map = getIconMap();
  37. const existingIcon = map.get(name);
  38. if (existingIcon === undefined) {
  39. map.set(name, data);
  40. /**
  41. * Importing and defining the same icon reference
  42. * multiple times should not yield a warning.
  43. */
  44. }
  45. else if (existingIcon !== data) {
  46. console.warn(`[Ionicons Warning]: Multiple icons were mapped to name "${name}". Ensure that multiple icons are not mapped to the same icon name.`);
  47. }
  48. };
  49. const getUrl = (i) => {
  50. let url = getSrc(i.src);
  51. if (url) {
  52. return url;
  53. }
  54. url = getName(i.name, i.icon, i.mode, i.ios, i.md);
  55. if (url) {
  56. return getNamedUrl(url, i);
  57. }
  58. if (i.icon) {
  59. url = getSrc(i.icon);
  60. if (url) {
  61. return url;
  62. }
  63. url = getSrc(i.icon[i.mode]);
  64. if (url) {
  65. return url;
  66. }
  67. }
  68. return null;
  69. };
  70. const getNamedUrl = (iconName, iconEl) => {
  71. const url = getIconMap().get(iconName);
  72. if (url) {
  73. return url;
  74. }
  75. try {
  76. return getAssetPath(`svg/${iconName}.svg`);
  77. }
  78. catch (e) {
  79. /**
  80. * In the custom elements build version of ionicons, referencing an icon
  81. * by name will throw an invalid URL error because the asset path is not defined.
  82. * This catches that error and logs something that is more developer-friendly.
  83. * We also include a reference to the ion-icon element so developers can
  84. * figure out which instance of ion-icon needs to be updated.
  85. */
  86. console.warn(`[Ionicons Warning]: Could not load icon with name "${iconName}". Ensure that the icon is registered using addIcons or that the icon SVG data is passed directly to the icon component.`, iconEl);
  87. }
  88. };
  89. const getName = (iconName, icon, mode, ios, md) => {
  90. // default to "md" if somehow the mode wasn't set
  91. mode = (mode && toLower(mode)) === 'ios' ? 'ios' : 'md';
  92. // if an icon was passed in using the ios or md attributes
  93. // set the iconName to whatever was passed in
  94. if (ios && mode === 'ios') {
  95. iconName = toLower(ios);
  96. }
  97. else if (md && mode === 'md') {
  98. iconName = toLower(md);
  99. }
  100. else {
  101. if (!iconName && icon && !isSrc(icon)) {
  102. iconName = icon;
  103. }
  104. if (isStr(iconName)) {
  105. iconName = toLower(iconName);
  106. }
  107. }
  108. if (!isStr(iconName) || iconName.trim() === '') {
  109. return null;
  110. }
  111. // only allow alpha characters and dash
  112. const invalidChars = iconName.replace(/[a-z]|-|\d/gi, '');
  113. if (invalidChars !== '') {
  114. return null;
  115. }
  116. return iconName;
  117. };
  118. const getSrc = (src) => {
  119. if (isStr(src)) {
  120. src = src.trim();
  121. if (isSrc(src)) {
  122. return src;
  123. }
  124. }
  125. return null;
  126. };
  127. const isSrc = (str) => str.length > 0 && /(\/|\.)/.test(str);
  128. const isStr = (val) => typeof val === 'string';
  129. const toLower = (val) => val.toLowerCase();
  130. /**
  131. * Elements inside of web components sometimes need to inherit global attributes
  132. * set on the host. For example, the inner input in `ion-input` should inherit
  133. * the `title` attribute that developers set directly on `ion-input`. This
  134. * helper function should be called in componentWillLoad and assigned to a variable
  135. * that is later used in the render function.
  136. *
  137. * This does not need to be reactive as changing attributes on the host element
  138. * does not trigger a re-render.
  139. */
  140. const inheritAttributes = (el, attributes = []) => {
  141. const attributeObject = {};
  142. attributes.forEach(attr => {
  143. if (el.hasAttribute(attr)) {
  144. const value = el.getAttribute(attr);
  145. if (value !== null) {
  146. attributeObject[attr] = el.getAttribute(attr);
  147. }
  148. el.removeAttribute(attr);
  149. }
  150. });
  151. return attributeObject;
  152. };
  153. /**
  154. * Returns `true` if the document or host element
  155. * has a `dir` set to `rtl`. The host value will always
  156. * take priority over the root document value.
  157. */
  158. const isRTL = (hostEl) => {
  159. if (hostEl) {
  160. if (hostEl.dir !== '') {
  161. return hostEl.dir.toLowerCase() === 'rtl';
  162. }
  163. }
  164. return (document === null || document === void 0 ? void 0 : document.dir.toLowerCase()) === 'rtl';
  165. };
  166. export { addIcons as a, inheritAttributes as b, getName as c, isRTL as d, getUrl as g, isStr as i };