jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/require/requirecss.js

73 lines
1.8 KiB
JavaScript
Raw Normal View History

2015-12-14 08:43:03 -07:00
define(function () {
var requireCss = {};
requireCss.normalize = function (name, normalize) {
if (name.substr(name.length - 4, 4) == '.css')
name = name.substr(0, name.length - 4);
return normalize(name);
}
var importedCss = [];
function isLoaded(url) {
return importedCss.indexOf(url) != -1;
}
function removeFromLoadHistory(url) {
url = url.toLowerCase();
importedCss = importedCss.filter(function (c) {
return url.indexOf(c.toLowerCase()) == -1;
});
}
requireCss.load = function (cssId, req, load, config) {
// Somehow if the url starts with /css, require will get all screwed up since this extension is also called css
2016-05-12 19:32:12 -07:00
var srch = '/emby-webcomponents/require/requirecss';
2015-12-26 11:35:53 -07:00
var index = cssId.indexOf(srch);
if (index != -1) {
cssId = 'css' + cssId.substring(index + srch.length);
}
2015-12-14 08:43:03 -07:00
var url = cssId + '.css';
if (url.indexOf('http') != 0 && url.indexOf('file:') != 0) {
url = config.baseUrl + url;
}
if (!isLoaded(url)) {
importedCss.push(url);
var link = document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.setAttribute('type', 'text/css');
link.onload = load;
2016-04-03 10:36:47 -07:00
var linkUrl = url;
if (config.urlArgs) {
linkUrl += config.urlArgs(cssId, url);
}
link.setAttribute('href', linkUrl);
2015-12-14 08:43:03 -07:00
document.head.appendChild(link);
} else {
load();
}
}
window.requireCss = {
2016-01-25 13:28:29 -07:00
removeStylesheet: function (stylesheet) {
2015-12-14 08:43:03 -07:00
2016-01-25 13:28:29 -07:00
stylesheet.parentNode.removeChild(stylesheet);
removeFromLoadHistory(stylesheet.href);
2015-12-14 08:43:03 -07:00
}
};
return requireCss;
});