2021-05-06 08:21:39 +00:00
|
|
|
import { defineComponent } from 'vue';
|
|
|
|
import {
|
|
|
|
DefineComponent,
|
|
|
|
ComponentOptionsMixin,
|
|
|
|
EmitsOptions,
|
|
|
|
VNodeProps,
|
|
|
|
AllowedComponentProps,
|
|
|
|
ComponentCustomProps,
|
|
|
|
} from 'vue';
|
|
|
|
|
|
|
|
import { IconifyJSON } from '@iconify/types';
|
|
|
|
|
|
|
|
// Core
|
2021-05-24 14:02:00 +00:00
|
|
|
import { IconifyIconName, stringToIcon } from '@iconify/utils/lib/icon/name';
|
2021-05-06 08:21:39 +00:00
|
|
|
import {
|
|
|
|
IconifyIconSize,
|
|
|
|
IconifyHorizontalIconAlignment,
|
|
|
|
IconifyVerticalIconAlignment,
|
2021-05-24 10:25:02 +00:00
|
|
|
} from '@iconify/utils/lib/customisations';
|
2021-05-06 08:21:39 +00:00
|
|
|
import {
|
|
|
|
IconifyStorageFunctions,
|
|
|
|
storageFunctions,
|
|
|
|
getIconData,
|
|
|
|
allowSimpleNames,
|
|
|
|
} from '@iconify/core/lib/storage/functions';
|
|
|
|
import {
|
|
|
|
IconifyBuilderFunctions,
|
|
|
|
builderFunctions,
|
|
|
|
} from '@iconify/core/lib/builder/functions';
|
2021-05-24 14:02:00 +00:00
|
|
|
import { IconifyIconBuildResult } from '@iconify/utils/lib/svg/build';
|
2021-05-24 10:25:02 +00:00
|
|
|
import { fullIcon, IconifyIcon } from '@iconify/utils/lib/icon';
|
2021-05-06 08:21:39 +00:00
|
|
|
|
|
|
|
// API
|
|
|
|
import {
|
|
|
|
IconifyAPIFunctions,
|
|
|
|
IconifyAPIInternalFunctions,
|
|
|
|
APIFunctions,
|
|
|
|
APIInternalFunctions,
|
2021-09-01 08:34:58 +00:00
|
|
|
IconifyAPIQueryParams,
|
|
|
|
IconifyAPICustomQueryParams,
|
|
|
|
IconifyAPIMergeQueryParams,
|
2021-05-06 08:21:39 +00:00
|
|
|
} from '@iconify/core/lib/api/functions';
|
|
|
|
import {
|
|
|
|
setAPIModule,
|
|
|
|
IconifyAPIModule,
|
|
|
|
IconifyAPISendQuery,
|
2021-09-01 08:34:58 +00:00
|
|
|
IconifyAPIPrepareIconsQuery,
|
2021-05-06 08:21:39 +00:00
|
|
|
} from '@iconify/core/lib/api/modules';
|
2021-09-05 16:09:25 +00:00
|
|
|
import { jsonpAPIModule } from '@iconify/core/lib/api/modules/jsonp';
|
2021-05-06 08:21:39 +00:00
|
|
|
import {
|
2021-09-05 16:09:25 +00:00
|
|
|
fetchAPIModule,
|
|
|
|
getFetch,
|
2021-05-06 08:21:39 +00:00
|
|
|
setFetch,
|
|
|
|
} from '@iconify/core/lib/api/modules/fetch';
|
|
|
|
import {
|
|
|
|
setAPIConfig,
|
|
|
|
PartialIconifyAPIConfig,
|
|
|
|
IconifyAPIConfig,
|
|
|
|
GetAPIConfig,
|
|
|
|
} from '@iconify/core/lib/api/config';
|
|
|
|
import {
|
|
|
|
IconifyIconLoaderCallback,
|
|
|
|
IconifyIconLoaderAbort,
|
2021-09-05 16:09:25 +00:00
|
|
|
} from '@iconify/core/lib/api/icons';
|
2021-05-06 08:21:39 +00:00
|
|
|
|
|
|
|
// Cache
|
2021-09-05 16:09:25 +00:00
|
|
|
import { cache } from '@iconify/core/lib/cache';
|
2021-05-06 08:21:39 +00:00
|
|
|
import { storeCache, loadCache } from '@iconify/core/lib/browser-storage';
|
|
|
|
import { toggleBrowserCache } from '@iconify/core/lib/browser-storage/functions';
|
|
|
|
import {
|
|
|
|
IconifyBrowserCacheType,
|
|
|
|
IconifyBrowserCacheFunctions,
|
|
|
|
} from '@iconify/core/lib/browser-storage/functions';
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
import {
|
2021-05-11 20:27:13 +00:00
|
|
|
RawIconCustomisations,
|
|
|
|
IconifyIconOnLoad,
|
2021-05-06 08:21:39 +00:00
|
|
|
IconProps,
|
|
|
|
IconifyIconCustomisations,
|
|
|
|
IconifyIconProps,
|
|
|
|
} from './props';
|
|
|
|
|
|
|
|
// Render SVG
|
|
|
|
import { render } from './render';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export required types
|
|
|
|
*/
|
|
|
|
// Function sets
|
|
|
|
export {
|
|
|
|
IconifyStorageFunctions,
|
|
|
|
IconifyBuilderFunctions,
|
|
|
|
IconifyBrowserCacheFunctions,
|
|
|
|
IconifyAPIFunctions,
|
|
|
|
IconifyAPIInternalFunctions,
|
|
|
|
};
|
|
|
|
|
|
|
|
// JSON stuff
|
|
|
|
export { IconifyIcon, IconifyJSON, IconifyIconName };
|
|
|
|
|
2021-05-11 20:27:13 +00:00
|
|
|
// Customisations and icon props
|
2021-05-06 08:21:39 +00:00
|
|
|
export {
|
|
|
|
IconifyIconCustomisations,
|
|
|
|
IconifyIconSize,
|
|
|
|
IconifyHorizontalIconAlignment,
|
|
|
|
IconifyVerticalIconAlignment,
|
|
|
|
IconifyIconProps,
|
|
|
|
IconProps,
|
2021-05-11 20:27:13 +00:00
|
|
|
IconifyIconOnLoad,
|
2021-05-06 08:21:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// API
|
|
|
|
export {
|
|
|
|
IconifyAPIConfig,
|
|
|
|
IconifyIconLoaderCallback,
|
|
|
|
IconifyIconLoaderAbort,
|
|
|
|
IconifyAPIModule,
|
|
|
|
GetAPIConfig,
|
2021-09-01 08:34:58 +00:00
|
|
|
IconifyAPIPrepareIconsQuery,
|
2021-05-06 08:21:39 +00:00
|
|
|
IconifyAPISendQuery,
|
|
|
|
PartialIconifyAPIConfig,
|
2021-09-01 08:34:58 +00:00
|
|
|
IconifyAPIQueryParams,
|
|
|
|
IconifyAPICustomQueryParams,
|
|
|
|
IconifyAPIMergeQueryParams,
|
2021-05-06 08:21:39 +00:00
|
|
|
};
|
|
|
|
|
2021-05-11 20:27:13 +00:00
|
|
|
// Builder functions
|
|
|
|
export { RawIconCustomisations, IconifyIconBuildResult };
|
|
|
|
|
2021-05-06 08:21:39 +00:00
|
|
|
/* Browser cache */
|
|
|
|
export { IconifyBrowserCacheType };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable and disable browser cache
|
|
|
|
*/
|
|
|
|
export const enableCache = (storage: IconifyBrowserCacheType) =>
|
|
|
|
toggleBrowserCache(storage, true);
|
|
|
|
|
|
|
|
export const disableCache = (storage: IconifyBrowserCacheType) =>
|
|
|
|
toggleBrowserCache(storage, false);
|
|
|
|
|
|
|
|
/* Storage functions */
|
|
|
|
/**
|
|
|
|
* Check if icon exists
|
|
|
|
*/
|
|
|
|
export const iconExists = storageFunctions.iconExists;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get icon data
|
|
|
|
*/
|
|
|
|
export const getIcon = storageFunctions.getIcon;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List available icons
|
|
|
|
*/
|
|
|
|
export const listIcons = storageFunctions.listIcons;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add one icon
|
|
|
|
*/
|
|
|
|
export const addIcon = storageFunctions.addIcon;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add icon set
|
|
|
|
*/
|
|
|
|
export const addCollection = storageFunctions.addCollection;
|
|
|
|
|
|
|
|
/* Builder functions */
|
|
|
|
/**
|
|
|
|
* Calculate icon size
|
|
|
|
*/
|
|
|
|
export const calculateSize = builderFunctions.calculateSize;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace unique ids in content
|
|
|
|
*/
|
|
|
|
export const replaceIDs = builderFunctions.replaceIDs;
|
|
|
|
|
2021-05-11 20:27:13 +00:00
|
|
|
/**
|
|
|
|
* Build SVG
|
|
|
|
*/
|
|
|
|
export const buildIcon = builderFunctions.buildIcon;
|
|
|
|
|
2021-05-06 08:21:39 +00:00
|
|
|
/* API functions */
|
|
|
|
/**
|
|
|
|
* Load icons
|
|
|
|
*/
|
|
|
|
export const loadIcons = APIFunctions.loadIcons;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add API provider
|
|
|
|
*/
|
|
|
|
export const addAPIProvider = APIFunctions.addAPIProvider;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export internal functions that can be used by third party implementations
|
|
|
|
*/
|
|
|
|
export const _api = APIInternalFunctions;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise stuff
|
|
|
|
*/
|
|
|
|
// Enable short names
|
|
|
|
allowSimpleNames(true);
|
|
|
|
|
2021-09-05 16:09:25 +00:00
|
|
|
// Set API module
|
|
|
|
setAPIModule('', getFetch() ? fetchAPIModule : jsonpAPIModule);
|
2021-05-06 08:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to enable node-fetch for getting icons on server side
|
|
|
|
*/
|
2021-05-07 18:19:48 +00:00
|
|
|
_api.setFetch = (nodeFetch: typeof fetch) => {
|
2021-05-06 08:21:39 +00:00
|
|
|
setFetch(nodeFetch);
|
2021-09-05 16:09:25 +00:00
|
|
|
setAPIModule('', fetchAPIModule);
|
2021-05-07 18:19:48 +00:00
|
|
|
};
|
2021-05-06 08:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Browser stuff
|
|
|
|
*/
|
|
|
|
if (typeof document !== 'undefined' && typeof window !== 'undefined') {
|
|
|
|
// Set cache and load existing cache
|
2021-09-05 16:09:25 +00:00
|
|
|
cache.store = storeCache;
|
2021-05-06 08:21:39 +00:00
|
|
|
loadCache();
|
|
|
|
|
2021-05-13 19:33:33 +00:00
|
|
|
interface WindowWithIconifyStuff {
|
|
|
|
IconifyPreload?: IconifyJSON[] | IconifyJSON;
|
|
|
|
IconifyProviders?: Record<string, PartialIconifyAPIConfig>;
|
|
|
|
}
|
|
|
|
const _window = window as WindowWithIconifyStuff;
|
2021-05-06 08:21:39 +00:00
|
|
|
|
|
|
|
// Load icons from global "IconifyPreload"
|
2021-05-13 19:33:33 +00:00
|
|
|
if (_window.IconifyPreload !== void 0) {
|
|
|
|
const preload = _window.IconifyPreload;
|
2021-05-06 08:21:39 +00:00
|
|
|
const err = 'Invalid IconifyPreload syntax.';
|
|
|
|
if (typeof preload === 'object' && preload !== null) {
|
|
|
|
(preload instanceof Array ? preload : [preload]).forEach(item => {
|
|
|
|
try {
|
|
|
|
if (
|
|
|
|
// Check if item is an object and not null/array
|
|
|
|
typeof item !== 'object' ||
|
|
|
|
item === null ||
|
|
|
|
item instanceof Array ||
|
|
|
|
// Check for 'icons' and 'prefix'
|
|
|
|
typeof item.icons !== 'object' ||
|
|
|
|
typeof item.prefix !== 'string' ||
|
|
|
|
// Add icon set
|
|
|
|
!addCollection(item)
|
|
|
|
) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set API from global "IconifyProviders"
|
2021-05-13 19:33:33 +00:00
|
|
|
if (_window.IconifyProviders !== void 0) {
|
|
|
|
const providers = _window.IconifyProviders;
|
2021-05-06 08:21:39 +00:00
|
|
|
if (typeof providers === 'object' && providers !== null) {
|
|
|
|
for (let key in providers) {
|
|
|
|
const err = 'IconifyProviders[' + key + '] is invalid.';
|
|
|
|
try {
|
|
|
|
const value = providers[key];
|
|
|
|
if (
|
|
|
|
typeof value !== 'object' ||
|
|
|
|
!value ||
|
|
|
|
value.resources === void 0
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!setAPIConfig(key, value)) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component
|
|
|
|
*/
|
2021-05-13 12:46:50 +00:00
|
|
|
interface IconComponentData {
|
|
|
|
data: Required<IconifyIcon>;
|
|
|
|
classes?: string[];
|
|
|
|
}
|
|
|
|
|
2021-05-06 08:21:39 +00:00
|
|
|
export const Icon = defineComponent({
|
|
|
|
// Do not inherit other attributes: it is handled by render()
|
|
|
|
inheritAttrs: false,
|
|
|
|
|
|
|
|
// Set initial data
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
// Mounted status
|
|
|
|
mounted: false,
|
|
|
|
|
|
|
|
// Callback counter to trigger re-render
|
|
|
|
counter: 0,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
beforeMount() {
|
|
|
|
// Current icon name
|
|
|
|
this._name = '';
|
|
|
|
|
|
|
|
// Loading
|
|
|
|
this._loadingIcon = null;
|
|
|
|
|
|
|
|
// Mark as mounted
|
|
|
|
this.mounted = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
unmounted() {
|
2021-05-07 18:19:48 +00:00
|
|
|
this.abortLoading();
|
2021-05-06 08:21:39 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
abortLoading() {
|
|
|
|
if (this._loadingIcon) {
|
|
|
|
this._loadingIcon.abort();
|
|
|
|
this._loadingIcon = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// Get data for icon to render or null
|
2021-05-13 12:46:50 +00:00
|
|
|
getIcon(
|
|
|
|
icon: IconifyIcon | string,
|
|
|
|
onload?: IconifyIconOnLoad
|
|
|
|
): IconComponentData | null {
|
2021-05-06 08:21:39 +00:00
|
|
|
// Icon is an object
|
|
|
|
if (
|
|
|
|
typeof icon === 'object' &&
|
|
|
|
icon !== null &&
|
|
|
|
typeof icon.body === 'string'
|
|
|
|
) {
|
|
|
|
// Stop loading
|
|
|
|
this._name = '';
|
|
|
|
this.abortLoading();
|
2021-05-13 12:46:50 +00:00
|
|
|
return {
|
|
|
|
data: fullIcon(icon),
|
|
|
|
};
|
2021-05-06 08:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invalid icon?
|
2021-05-13 12:46:50 +00:00
|
|
|
let iconName: IconifyIconName | null;
|
|
|
|
if (
|
|
|
|
typeof icon !== 'string' ||
|
|
|
|
(iconName = stringToIcon(icon, false, true)) === null
|
|
|
|
) {
|
2021-05-06 08:21:39 +00:00
|
|
|
this.abortLoading();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load icon
|
2021-05-13 12:46:50 +00:00
|
|
|
const data = getIconData(iconName);
|
2021-05-06 08:21:39 +00:00
|
|
|
if (data === null) {
|
|
|
|
// Icon needs to be loaded
|
|
|
|
if (!this._loadingIcon || this._loadingIcon.name !== icon) {
|
|
|
|
// New icon to load
|
|
|
|
this.abortLoading();
|
|
|
|
this._name = '';
|
|
|
|
this._loadingIcon = {
|
|
|
|
name: icon,
|
2021-09-05 16:09:25 +00:00
|
|
|
abort: loadIcons([iconName], () => {
|
2021-05-06 08:21:39 +00:00
|
|
|
this.counter++;
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Icon data is available
|
|
|
|
this.abortLoading();
|
2021-05-11 20:27:13 +00:00
|
|
|
if (this._name !== icon) {
|
|
|
|
this._name = icon;
|
|
|
|
if (onload) {
|
|
|
|
onload(icon);
|
|
|
|
}
|
|
|
|
}
|
2021-05-13 12:46:50 +00:00
|
|
|
|
|
|
|
// Add classes
|
|
|
|
const classes: string[] = ['iconify'];
|
|
|
|
if (iconName.prefix !== '') {
|
|
|
|
classes.push('iconify--' + iconName.prefix);
|
|
|
|
}
|
|
|
|
if (iconName.provider !== '') {
|
|
|
|
classes.push('iconify--' + iconName.provider);
|
|
|
|
}
|
|
|
|
|
|
|
|
return { data, classes };
|
2021-05-06 08:21:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Render icon
|
|
|
|
render() {
|
|
|
|
if (!this.mounted) {
|
|
|
|
return this.$slots.default ? this.$slots.default() : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-render when counter changes
|
|
|
|
this.counter;
|
|
|
|
|
|
|
|
// Get icon data
|
|
|
|
const props = this.$attrs;
|
2021-05-13 12:46:50 +00:00
|
|
|
const icon: IconComponentData | null = this.getIcon(
|
|
|
|
props.icon,
|
|
|
|
props.onLoad
|
|
|
|
);
|
2021-05-06 08:21:39 +00:00
|
|
|
|
|
|
|
// Validate icon object
|
|
|
|
if (!icon) {
|
|
|
|
return this.$slots.default ? this.$slots.default() : null;
|
|
|
|
}
|
|
|
|
|
2021-05-13 12:46:50 +00:00
|
|
|
// Add classes
|
|
|
|
let newProps = props;
|
|
|
|
if (icon.classes) {
|
2021-05-24 10:25:02 +00:00
|
|
|
newProps = {
|
|
|
|
...props,
|
2021-05-13 12:46:50 +00:00
|
|
|
class:
|
|
|
|
(typeof props['class'] === 'string'
|
|
|
|
? props['class'] + ' '
|
|
|
|
: '') + icon.classes.join(' '),
|
2021-05-24 10:25:02 +00:00
|
|
|
};
|
2021-05-13 12:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Render icon
|
|
|
|
return render(icon.data, newProps);
|
2021-05-06 08:21:39 +00:00
|
|
|
},
|
|
|
|
});
|