utils-b413c9c0.js 5.2 KB

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