diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-17 12:09:20 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-17 12:09:20 +0300 |
commit | fc1df8c8307fc5022f9e8aae04164c089d8fdf2e (patch) | |
tree | a759f58abf9e41200c48a60de73c84cab47a250d /app/assets/javascripts/lib/utils/icon_utils.js | |
parent | c8df22c555ab707a705e57c4257fd3ed1ce7c3b0 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/assets/javascripts/lib/utils/icon_utils.js')
-rw-r--r-- | app/assets/javascripts/lib/utils/icon_utils.js | 44 |
1 files changed, 33 insertions, 11 deletions
diff --git a/app/assets/javascripts/lib/utils/icon_utils.js b/app/assets/javascripts/lib/utils/icon_utils.js index 7b8dd9bbef7..97ee773358d 100644 --- a/app/assets/javascripts/lib/utils/icon_utils.js +++ b/app/assets/javascripts/lib/utils/icon_utils.js @@ -1,18 +1,40 @@ -/* eslint-disable import/prefer-default-export */ - +import { memoize } from 'lodash'; import axios from '~/lib/utils/axios_utils'; /** - * Retrieve SVG icon path content from gitlab/svg sprite icons - * @param {String} name + * Resolves to a DOM that contains GitLab icons + * in svg format. Memoized to avoid duplicate requests */ -export const getSvgIconPathContent = name => +const getSvgDom = memoize(() => axios .get(gon.sprite_icons) - .then(({ data: svgs }) => - new DOMParser() - .parseFromString(svgs, 'text/xml') - .querySelector(`#${name} path`) - .getAttribute('d'), - ) + .then(({ data: svgs }) => new DOMParser().parseFromString(svgs, 'text/xml')) + .catch(() => { + getSvgDom.cache.clear(); + }), +); + +/** + * 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'); + }) .catch(() => null); |