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

43 lines
1,004 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'))
2020-04-22 19:07:51 +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
*/
export const getSvgIconPathContent = name =>
getSvgDom()
.then(doc => {
return doc.querySelector(`#${name} path`).getAttribute('d');
})
2019-03-02 22:35:43 +05:30
.catch(() => null);