2
0
mirror of https://github.com/iconify/iconify.git synced 2024-10-23 17:12:03 +00:00
iconify/packages/vue/src/iconify.ts

415 lines
8.9 KiB
TypeScript
Raw Normal View History

2021-05-06 08:21:39 +00:00
import { defineComponent } from 'vue';
import type {
2021-05-06 08:21:39 +00:00
DefineComponent,
ComponentOptionsMixin,
EmitsOptions,
VNodeProps,
AllowedComponentProps,
ComponentCustomProps,
} from 'vue';
import type { IconifyJSON, IconifyIcon } from '@iconify/types';
2021-05-06 08:21:39 +00:00
// Core
import type { IconifyIconName } from '@iconify/utils/lib/icon/name';
import { stringToIcon } from '@iconify/utils/lib/icon/name';
import type {
2021-05-06 08:21:39 +00:00
IconifyIconSize,
IconifyHorizontalIconAlignment,
IconifyVerticalIconAlignment,
} from '@iconify/utils/lib/customisations';
import type { IconifyStorageFunctions } from '@iconify/core/lib/storage/functions';
2021-05-06 08:21:39 +00:00
import {
iconExists,
getIcon,
addIcon,
addCollection,
2021-05-06 08:21:39 +00:00
getIconData,
allowSimpleNames,
} from '@iconify/core/lib/storage/functions';
import { listIcons, shareStorage } from '@iconify/core/lib/storage/storage';
import type { IconifyBuilderFunctions } from '@iconify/core/lib/builder/functions';
import { buildIcon } from '@iconify/core/lib/builder/functions';
import { replaceIDs } from '@iconify/utils/lib/svg/id';
import { calculateSize } from '@iconify/utils/lib/svg/size';
import type { IconifyIconBuildResult } from '@iconify/utils/lib/svg/build';
import { fullIcon } from '@iconify/utils/lib/icon';
2021-05-06 08:21:39 +00:00
// API
import type {
2021-05-06 08:21:39 +00:00
IconifyAPIFunctions,
IconifyAPIInternalFunctions,
IconifyAPIQueryParams,
IconifyAPICustomQueryParams,
2021-05-06 08:21:39 +00:00
} from '@iconify/core/lib/api/functions';
import type {
2021-05-06 08:21:39 +00:00
IconifyAPIModule,
IconifyAPISendQuery,
IconifyAPIPrepareIconsQuery,
2021-05-06 08:21:39 +00:00
} from '@iconify/core/lib/api/modules';
import { setAPIModule } from '@iconify/core/lib/api/modules';
import type {
2021-05-06 08:21:39 +00:00
PartialIconifyAPIConfig,
IconifyAPIConfig,
GetAPIConfig,
} from '@iconify/core/lib/api/config';
import {
addAPIProvider,
getAPIConfig,
listAPIProviders,
} from '@iconify/core/lib/api/config';
import {
fetchAPIModule,
setFetch,
getFetch,
} from '@iconify/core/lib/api/modules/fetch';
import type {
2021-05-06 08:21:39 +00:00
IconifyIconLoaderCallback,
IconifyIconLoaderAbort,
} from '@iconify/core/lib/api/icons';
2022-01-13 20:19:49 +00:00
import { loadIcons, loadIcon } from '@iconify/core/lib/api/icons';
import { sendAPIQuery } from '@iconify/core/lib/api/query';
2021-05-06 08:21:39 +00:00
// Cache
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 type {
2021-05-06 08:21:39 +00:00
IconifyBrowserCacheType,
IconifyBrowserCacheFunctions,
} from '@iconify/core/lib/browser-storage/functions';
// Properties
import type {
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 };
// Customisations and icon props
2021-05-06 08:21:39 +00:00
export {
IconifyIconCustomisations,
IconifyIconSize,
IconifyHorizontalIconAlignment,
IconifyVerticalIconAlignment,
IconifyIconProps,
IconProps,
IconifyIconOnLoad,
2021-05-06 08:21:39 +00:00
};
// API
export {
IconifyAPIConfig,
IconifyIconLoaderCallback,
IconifyIconLoaderAbort,
IconifyAPIModule,
GetAPIConfig,
IconifyAPIPrepareIconsQuery,
2021-05-06 08:21:39 +00:00
IconifyAPISendQuery,
PartialIconifyAPIConfig,
IconifyAPIQueryParams,
IconifyAPICustomQueryParams,
2021-05-06 08:21:39 +00:00
};
// Builder functions
export { RawIconCustomisations, IconifyIconBuildResult };
2021-05-06 08:21:39 +00:00
/* Browser cache */
export { IconifyBrowserCacheType };
/**
* Enable cache
2021-05-06 08:21:39 +00:00
*/
function enableCache(storage: IconifyBrowserCacheType): void {
2021-05-06 08:21:39 +00:00
toggleBrowserCache(storage, true);
}
2021-05-06 08:21:39 +00:00
/**
* Disable cache
2021-05-06 08:21:39 +00:00
*/
function disableCache(storage: IconifyBrowserCacheType): void {
toggleBrowserCache(storage, false);
}
2021-05-06 08:21:39 +00:00
/**
* Initialise stuff
*/
// Enable short names
allowSimpleNames(true);
// Set API module
setAPIModule('', fetchAPIModule);
2021-05-06 08:21:39 +00:00
/**
* Browser stuff
*/
if (typeof document !== 'undefined' && typeof window !== 'undefined') {
// Set cache and load existing cache
cache.store = storeCache;
2021-05-06 08:21:39 +00:00
loadCache();
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"
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) => {
2021-05-06 08:21:39 +00:00
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"
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 (!addAPIProvider(key, value)) {
2021-05-06 08:21:39 +00:00
console.error(err);
}
} catch (e) {
console.error(err);
}
}
}
}
}
/**
* Empty icon data, rendered when icon is not available
*/
const emptyIcon = fullIcon({
body: '',
});
2021-05-06 08:21:39 +00:00
/**
* Component
*/
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
iconMounted: false,
2021-05-06 08:21:39 +00:00
// Callback counter to trigger re-render
counter: 0,
};
},
mounted() {
2021-05-06 08:21:39 +00:00
// Current icon name
this._name = '';
// Loading
this._loadingIcon = null;
// Mark as mounted
this.iconMounted = true;
2021-05-06 08:21:39 +00:00
},
unmounted() {
this.abortLoading();
2021-05-06 08:21:39 +00:00
},
methods: {
abortLoading() {
if (this._loadingIcon) {
this._loadingIcon.abort();
this._loadingIcon = null;
}
},
2021-05-06 08:21:39 +00:00
// Get data for icon to render or null
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();
return {
data: fullIcon(icon),
};
2021-05-06 08:21:39 +00:00
}
// Invalid icon?
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
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,
abort: loadIcons([iconName], () => {
2021-05-06 08:21:39 +00:00
this.counter++;
}),
};
}
return null;
}
// Icon data is available
this.abortLoading();
if (this._name !== icon) {
this._name = icon;
if (onload) {
onload(icon);
}
}
// 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() {
// Re-render when counter changes
this.counter;
const props = this.$attrs;
// Get icon data
const icon: IconComponentData | null = this.iconMounted
? this.getIcon(props.icon, props.onLoad)
: null;
2021-05-06 08:21:39 +00:00
// Validate icon object
if (!icon) {
return render(emptyIcon, props);
2021-05-06 08:21:39 +00:00
}
// Add classes
let newProps = props;
if (icon.classes) {
newProps = {
...props,
class:
(typeof props['class'] === 'string'
? props['class'] + ' '
: '') + icon.classes.join(' '),
};
}
// Render icon
return render(icon.data, newProps);
2021-05-06 08:21:39 +00:00
},
});
/**
* Internal API
*/
const _api: IconifyAPIInternalFunctions = {
getAPIConfig,
setAPIModule,
sendAPIQuery,
setFetch,
getFetch,
listAPIProviders,
};
/**
* Export functions
*/
// IconifyAPIInternalFunctions
export { _api };
// IconifyAPIFunctions
2022-01-13 20:19:49 +00:00
export { addAPIProvider, loadIcons, loadIcon };
// IconifyStorageFunctions
export { iconExists, getIcon, listIcons, addIcon, addCollection, shareStorage };
// IconifyBuilderFunctions
export { replaceIDs, calculateSize, buildIcon };
// IconifyBrowserCacheFunctions
export { enableCache, disableCache };