forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
css.js
114 lines (114 loc) · 5.56 KB
/
css.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------
*---------------------------------------------------------------------------------------------
*---------------------------------------------------------------------------------------------
*---------------------------------------------------------------------------------------------
*---------------------------------------------------------------------------------------------
* Please make sure to make edits in the .ts file at https://github.com/Microsoft/vscode-loader/
*---------------------------------------------------------------------------------------------
*---------------------------------------------------------------------------------------------
*---------------------------------------------------------------------------------------------
*---------------------------------------------------------------------------------------------
*--------------------------------------------------------------------------------------------*/
'use strict';
var CSSLoaderPlugin;
(function (CSSLoaderPlugin) {
/**
* Known issue:
* - In IE there is no way to know if the CSS file loaded successfully or not.
*/
var BrowserCSSLoader = /** @class */ (function () {
function BrowserCSSLoader() {
this._pendingLoads = 0;
}
BrowserCSSLoader.prototype.attachListeners = function (name, linkNode, callback, errorback) {
var unbind = function () {
linkNode.removeEventListener('load', loadEventListener);
linkNode.removeEventListener('error', errorEventListener);
};
var loadEventListener = function (e) {
unbind();
callback();
};
var errorEventListener = function (e) {
unbind();
errorback(e);
};
linkNode.addEventListener('load', loadEventListener);
linkNode.addEventListener('error', errorEventListener);
};
BrowserCSSLoader.prototype._onLoad = function (name, callback) {
this._pendingLoads--;
callback();
};
BrowserCSSLoader.prototype._onLoadError = function (name, errorback, err) {
this._pendingLoads--;
errorback(err);
};
BrowserCSSLoader.prototype._insertLinkNode = function (linkNode) {
this._pendingLoads++;
var head = document.head || document.getElementsByTagName('head')[0];
var other = head.getElementsByTagName('link') || document.head.getElementsByTagName('script');
if (other.length > 0) {
head.insertBefore(linkNode, other[other.length - 1]);
}
else {
head.appendChild(linkNode);
}
};
BrowserCSSLoader.prototype.createLinkTag = function (name, cssUrl, externalCallback, externalErrorback) {
var _this = this;
var linkNode = document.createElement('link');
linkNode.setAttribute('rel', 'stylesheet');
linkNode.setAttribute('type', 'text/css');
linkNode.setAttribute('data-name', name);
var callback = function () { return _this._onLoad(name, externalCallback); };
var errorback = function (err) { return _this._onLoadError(name, externalErrorback, err); };
this.attachListeners(name, linkNode, callback, errorback);
linkNode.setAttribute('href', cssUrl);
return linkNode;
};
BrowserCSSLoader.prototype._linkTagExists = function (name, cssUrl) {
var i, len, nameAttr, hrefAttr, links = document.getElementsByTagName('link');
for (i = 0, len = links.length; i < len; i++) {
nameAttr = links[i].getAttribute('data-name');
hrefAttr = links[i].getAttribute('href');
if (nameAttr === name || hrefAttr === cssUrl) {
return true;
}
}
return false;
};
BrowserCSSLoader.prototype.load = function (name, cssUrl, externalCallback, externalErrorback) {
if (this._linkTagExists(name, cssUrl)) {
externalCallback();
return;
}
var linkNode = this.createLinkTag(name, cssUrl, externalCallback, externalErrorback);
this._insertLinkNode(linkNode);
};
return BrowserCSSLoader;
}());
// ------------------------------ Finally, the plugin
var CSSPlugin = /** @class */ (function () {
function CSSPlugin() {
this._cssLoader = new BrowserCSSLoader();
}
CSSPlugin.prototype.load = function (name, req, load) {
var cssUrl = req.toUrl(name + '.css');
this._cssLoader.load(name, cssUrl, function (contents) {
load({});
}, function (err) {
if (typeof load.error === 'function') {
load.error('Could not find ' + cssUrl + ' or it was empty');
}
});
};
return CSSPlugin;
}());
CSSLoaderPlugin.CSSPlugin = CSSPlugin;
define('vs/css', new CSSPlugin());
})(CSSLoaderPlugin || (CSSLoaderPlugin = {}));