UNPKG

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