1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
8 |
|
9 | var _jsModules = require('@jenkins-cd/js-modules');
|
10 |
|
11 | var _jsModules2 = _interopRequireDefault(_jsModules);
|
12 |
|
13 | var _ModuleSpec = require('@jenkins-cd/js-modules/js/ModuleSpec');
|
14 |
|
15 | var _ModuleSpec2 = _interopRequireDefault(_ModuleSpec);
|
16 |
|
17 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
18 |
|
19 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | var ResourceLoadTracker = function () {
|
28 | function ResourceLoadTracker() {
|
29 | _classCallCheck(this, ResourceLoadTracker);
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | this.pointCSSs = {};
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | this.activeCSSs = {};
|
42 | }
|
43 |
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | _createClass(ResourceLoadTracker, [{
|
53 | key: 'setExtensionPointMetadata',
|
54 | value: function setExtensionPointMetadata(extensionPointList) {
|
55 |
|
56 | this.pointCSSs = {};
|
57 | this.activeCSSs = {};
|
58 |
|
59 |
|
60 | for (var i1 = 0; i1 < extensionPointList.length; i1++) {
|
61 | var pluginMetadata = extensionPointList[i1];
|
62 | var extensions = pluginMetadata.extensions;
|
63 | var pluginCSS = pluginMetadata.extensionCSS;
|
64 |
|
65 |
|
66 | for (var i2 = 0; i2 < extensions.length; i2++) {
|
67 | var extensionPoint = extensions[i2].extensionPoint;
|
68 | var pointCSS = this.pointCSSs[extensionPoint];
|
69 |
|
70 | if (!pointCSS) {
|
71 | pointCSS = [];
|
72 | this.pointCSSs[extensionPoint] = pointCSS;
|
73 | }
|
74 |
|
75 |
|
76 | if (pointCSS.filter(function (pluginCSSEntry) {
|
77 | return pluginCSSEntry.url === pluginCSS;
|
78 | }).length === 0) {
|
79 | pointCSS.push({
|
80 | url: pluginCSS,
|
81 | hpiPluginId: pluginMetadata.hpiPluginId
|
82 | });
|
83 | }
|
84 | }
|
85 | }
|
86 | }
|
87 |
|
88 | |
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | }, {
|
99 | key: 'onMount',
|
100 | value: function onMount(extensionPointName) {
|
101 | var pointCSS = this.pointCSSs[extensionPointName];
|
102 | if (pointCSS) {
|
103 | for (var i = 0; i < pointCSS.length; i++) {
|
104 | this._requireCSS(pointCSS[i]);
|
105 | }
|
106 | }
|
107 | }
|
108 |
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | }, {
|
120 | key: 'onUnmount',
|
121 | value: function onUnmount(extensionPointName) {
|
122 | var pointCSS = this.pointCSSs[extensionPointName];
|
123 | if (pointCSS) {
|
124 | for (var i = 0; i < pointCSS.length; i++) {
|
125 | this._unrequireCSS(pointCSS[i]);
|
126 | }
|
127 | }
|
128 | }
|
129 | }, {
|
130 | key: '_requireCSS',
|
131 | value: function _requireCSS(pluginCSS) {
|
132 | if (!this.activeCSSs[pluginCSS.url]) {
|
133 | this._addCSS(pluginCSS);
|
134 | this.activeCSSs[pluginCSS.url] = true;
|
135 | }
|
136 | }
|
137 | }, {
|
138 | key: '_unrequireCSS',
|
139 | value: function _unrequireCSS(pluginCSS) {
|
140 | var activeCount = this.activeCSSs[pluginCSS.url];
|
141 |
|
142 | if (!activeCount) {
|
143 |
|
144 | console.warn('Unexpected call to deactivate an inactive Jenkins Extension Point CSS: ' + url);
|
145 |
|
146 |
|
147 | } else {
|
148 | activeCount--;
|
149 | if (activeCount === 0) {
|
150 |
|
151 | delete this.activeCSSs[pluginCSS.url];
|
152 | this._removeCSS(pluginCSS);
|
153 | } else {
|
154 | this.activeCSSs[pluginCSS.url] = activeCount;
|
155 | }
|
156 | }
|
157 | }
|
158 | }, {
|
159 | key: '_addCSS',
|
160 | value: function _addCSS(pluginCSS) {
|
161 | var cssURL = getPluginCSSURL(pluginCSS);
|
162 | _jsModules2.default.addCSSToPage(cssURL);
|
163 | }
|
164 | }, {
|
165 | key: '_removeCSS',
|
166 | value: function _removeCSS(pluginCSS) {
|
167 | var cssURL = getPluginCSSURL(pluginCSS);
|
168 | var linkElId = _jsModules2.default.toCSSId(cssURL);
|
169 | var linkEl = document.getElementById(linkElId);
|
170 |
|
171 | if (linkEl) {
|
172 | linkEl.parentNode.removeChild(linkEl);
|
173 | }
|
174 | }
|
175 | }]);
|
176 |
|
177 | return ResourceLoadTracker;
|
178 | }();
|
179 |
|
180 | exports.default = ResourceLoadTracker;
|
181 |
|
182 |
|
183 | function getPluginCSSURL(pluginCSS) {
|
184 | var moduleSpec = new _ModuleSpec2.default(pluginCSS.hpiPluginId + ':jenkins-js-extension');
|
185 | var resolver = void 0;
|
186 |
|
187 |
|
188 | if (typeof _jsModules2.default.getResourceLocationResolver === 'function') {
|
189 | resolver = _jsModules2.default.getResourceLocationResolver(moduleSpec);
|
190 | }
|
191 |
|
192 | if (resolver) {
|
193 | return resolver(pluginCSS.url);
|
194 | } else {
|
195 | var adjunctUrl = _jsModules2.default.getAdjunctURL();
|
196 | return adjunctUrl + '/' + pluginCSS.url;
|
197 | }
|
198 | } |
\ | No newline at end of file |