2020-03-17 05:09:20 -04:00
|
|
|
import { memoize } from 'lodash';
|
2019-01-23 19:06:02 -05:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
|
|
|
|
/**
|
2020-03-17 05:09:20 -04:00
|
|
|
* Resolves to a DOM that contains GitLab icons
|
|
|
|
* in svg format. Memoized to avoid duplicate requests
|
2019-01-23 19:06:02 -05:00
|
|
|
*/
|
2020-03-17 05:09:20 -04:00
|
|
|
const getSvgDom = memoize(() =>
|
2019-01-23 19:06:02 -05:00
|
|
|
axios
|
|
|
|
.get(gon.sprite_icons)
|
2020-03-17 05:09:20 -04:00
|
|
|
.then(({ data: svgs }) => new DOMParser().parseFromString(svgs, 'text/xml'))
|
2020-12-23 19:10:25 -05:00
|
|
|
.catch((e) => {
|
2020-03-17 05:09:20 -04:00
|
|
|
getSvgDom.cache.clear();
|
2020-04-01 11:07:45 -04:00
|
|
|
|
|
|
|
throw e;
|
2020-03-17 05:09:20 -04:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears the memoized SVG content.
|
|
|
|
*
|
|
|
|
* You probably don't need to invoke this function unless
|
|
|
|
* sprite_icons are updated.
|
|
|
|
*/
|
|
|
|
export const clearSvgIconPathContentCache = () => {
|
|
|
|
getSvgDom.cache.clear();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve SVG icon path content from gitlab/svg sprite icons.
|
|
|
|
*
|
|
|
|
* Content loaded is cached.
|
|
|
|
*
|
|
|
|
* @param {String} name - Icon name
|
|
|
|
* @returns A promise that resolves to the svg path
|
|
|
|
*/
|
2020-12-23 19:10:25 -05:00
|
|
|
export const getSvgIconPathContent = (name) =>
|
2020-03-17 05:09:20 -04:00
|
|
|
getSvgDom()
|
2020-12-23 19:10:25 -05:00
|
|
|
.then((doc) => {
|
2020-03-17 05:09:20 -04:00
|
|
|
return doc.querySelector(`#${name} path`).getAttribute('d');
|
|
|
|
})
|
2019-01-23 19:06:02 -05:00
|
|
|
.catch(() => null);
|