debian-mirror-gitlab/app/assets/javascripts/lib/utils/icon_utils.js

43 lines
1,010 B
JavaScript
Raw Normal View History

2020-04-08 14:13:33 +05:30
import { memoize } from 'lodash';
2019-03-02 22:35:43 +05:30
import axios from '~/lib/utils/axios_utils';
/**
2020-04-08 14:13:33 +05:30
* Resolves to a DOM that contains GitLab icons
* in svg format. Memoized to avoid duplicate requests
2019-03-02 22:35:43 +05:30
*/
2020-04-08 14:13:33 +05:30
const getSvgDom = memoize(() =>
2019-03-02 22:35:43 +05:30
axios
.get(gon.sprite_icons)
2020-04-08 14:13:33 +05:30
.then(({ data: svgs }) => new DOMParser().parseFromString(svgs, 'text/xml'))
2021-03-08 18:12:59 +05:30
.catch((e) => {
2020-04-08 14:13:33 +05:30
getSvgDom.cache.clear();
2020-04-22 19:07:51 +05:30
throw e;
2020-04-08 14:13:33 +05:30
}),
);
/**
* 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
*/
2021-03-08 18:12:59 +05:30
export const getSvgIconPathContent = (name) =>
2020-04-08 14:13:33 +05:30
getSvgDom()
2021-03-08 18:12:59 +05:30
.then((doc) => {
2020-04-08 14:13:33 +05:30
return doc.querySelector(`#${name} path`).getAttribute('d');
})
2019-03-02 22:35:43 +05:30
.catch(() => null);