1 | var COCKPIT_VERSION = require('cockpitversion.js'); |
||
2 | var logger; |
||
3 | |||
4 | |||
5 | var PREFERENCES = 'plugins:ui-manager'; |
||
6 | const path = require('path'); |
||
0 ignored issues
–
show
Backwards Compatibility
introduced
by
![]() |
|||
7 | const fs = require('fs'); |
||
0 ignored issues
–
show
|
|||
8 | function UIManager(name, deps) { |
||
9 | logger = deps.logger; |
||
10 | logger.info('UI Manager plugin started.'); |
||
11 | var preferences = getPreferences(deps.config); |
||
12 | this.UIs = []; |
||
13 | this.deps = deps; |
||
14 | this.plugin = { name: 'ui-manager' }; |
||
15 | } |
||
16 | function getPreferences(config) { |
||
17 | var preferences = config.preferences.get(PREFERENCES); |
||
18 | if (preferences === undefined) { |
||
19 | preferences = {}; |
||
20 | config.preferences.set(PREFERENCES, preferences); |
||
21 | } |
||
22 | logger.debug('Plugin Manager loaded preferences: ' + JSON.stringify(preferences)); |
||
23 | return preferences; |
||
24 | } |
||
25 | UIManager.prototype.start = function start() { |
||
26 | var self = this; |
||
27 | /* Crawl the plugins looking for those with settings definitions */ |
||
28 | this.deps.loadedPlugins.forEach(function (plugin) { |
||
29 | if (plugin !== undefined) { |
||
30 | if (plugin.plugin !== undefined && plugin.plugin.type === 'theme') { |
||
31 | self.UIs.push(plugin); |
||
32 | } |
||
33 | } |
||
34 | }); |
||
35 | var pathInfo = this.deps.pathInfo(); |
||
36 | this.deps.app.get('/sw-import.js', function (req, res) { |
||
37 | var data = 'if(\'function\' === typeof importScripts) {importScripts(\'components/platinum-sw/service-worker.js\');}'; |
||
38 | res.writeHead(200, { |
||
39 | 'Content-Type': 'application/javascript', |
||
40 | 'Content-Length': data.length |
||
41 | }); |
||
42 | res.write(data); |
||
43 | res.end(); |
||
44 | }); |
||
45 | this.deps.app.get('/', function (req, res) { |
||
46 | var theme = self.deps.config.preferences.get('plugins:ui-manager').selectedUI; |
||
47 | var ua = req.header('user-agent'); |
||
48 | // Check the user-agent string to identyfy the device. |
||
49 | if (/mobile|iphone|ipod|android|blackberry|opera|mini|windows\sce|palm|smartphone|iemobile|ipad|android|android 3.0|xoom|sch-i800|playbook|tablet|kindle/i.test(ua)) { |
||
50 | theme = 'mobile-ui'; |
||
51 | } |
||
52 | //You can override the theme by passing theme=<themename> in the query string |
||
53 | if (req.query && req.query.theme) { |
||
54 | theme = req.query.theme; |
||
55 | } |
||
56 | theme = theme === undefined ? 'new-ui' : theme; |
||
57 | var scriplets = self.getAppletsByTheme(self.getApplets(), theme); |
||
58 | //TODO: Add theme to the message so you can differentiate the applets by theme |
||
59 | //and ignore if it is not the theme you are using. |
||
60 | //TODO: Look for applet.ejs.disable in a theme to remove the applet option. |
||
61 | self.deps.cockpit.emit('ui-manager-applets', scriplets.filter(function (item) { |
||
62 | return [ |
||
63 | 'footer', |
||
64 | 'header', |
||
65 | 'head' |
||
66 | ].indexOf(item.name) == -1; |
||
67 | })); |
||
68 | res.render(__dirname + '/base.ejs', { |
||
69 | title: 'OpenROV ROV Cockpit', |
||
70 | scripts: pathInfo.scripts, |
||
71 | styles: pathInfo.styles, |
||
72 | sysscripts: pathInfo.sysscripts, |
||
73 | webcomponents: pathInfo.webcomponents.filter(function(wcInfo){ |
||
74 | return (wcInfo.path.indexOf('ui-manager/orov-widget-registry.html')==-1) |
||
0 ignored issues
–
show
There should be a semicolon.
Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers. Further Readings: ![]() |
|||
75 | }), |
||
76 | config: self.deps.config, |
||
77 | scriplets: scriplets, |
||
78 | theme: theme, |
||
79 | cacheMode: process.env.IGNORE_CACHE=='true'?'networkFirst':'fastest', |
||
80 | cockpit_version: COCKPIT_VERSION |
||
81 | }); |
||
82 | }); |
||
83 | |||
84 | |||
85 | this.deps.app.get('/popup', function (req, res) { |
||
86 | var theme = self.deps.config.preferences.get('plugins:ui-manager').selectedUI; |
||
87 | var ua = req.header('user-agent'); |
||
88 | // Check the user-agent string to identyfy the device. |
||
89 | if (/mobile|iphone|ipod|android|blackberry|opera|mini|windows\sce|palm|smartphone|iemobile|ipad|android|android 3.0|xoom|sch-i800|playbook|tablet|kindle/i.test(ua)) { |
||
90 | theme = 'mobile-ui'; |
||
91 | } |
||
92 | //You can override the theme by passing theme=<themename> in the query string |
||
93 | if (req.query && req.query.theme) { |
||
94 | theme = req.query.theme; |
||
95 | } |
||
96 | var applet; |
||
97 | if (req.query && req.query.app) { |
||
98 | applet = req.query.app; |
||
99 | } |
||
100 | theme = theme === undefined ? 'new-ui' : theme; |
||
101 | var scriplets = self.getAppletsByTheme(self.getApplets(), theme); |
||
102 | //TODO: Add theme to the message so you can differentiate the applets by theme |
||
103 | //and ignore if it is not the theme you are using. |
||
104 | //TODO: Look for applet.ejs.disable in a theme to remove the applet option. |
||
105 | self.deps.cockpit.emit('ui-manager-applets', scriplets.filter(function (item) { |
||
106 | return [ |
||
107 | 'footer', |
||
108 | 'header', |
||
109 | 'head' |
||
110 | ].indexOf(item.name) == -1; |
||
111 | })); |
||
112 | res.render(__dirname + '/popup.ejs', { |
||
113 | title: 'OpenROV ROV Cockpit', |
||
114 | scripts: pathInfo.scripts, |
||
115 | styles: pathInfo.styles, |
||
116 | sysscripts: pathInfo.sysscripts, |
||
117 | webcomponents: pathInfo.webcomponents.filter(function(wcInfo){ |
||
118 | return (wcInfo.path.indexOf('ui-manager/orov-widget-registry.html')==-1) |
||
0 ignored issues
–
show
There should be a semicolon.
Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers. Further Readings: ![]() |
|||
119 | }), |
||
120 | config: self.deps.config, |
||
121 | scriplet: scriplets.find(function (item) { |
||
122 | return item.name == applet; |
||
123 | }), |
||
124 | scriplets: scriplets, |
||
125 | theme: theme, |
||
126 | cockpit_version: COCKPIT_VERSION |
||
127 | }); |
||
128 | }); |
||
129 | |||
130 | this.deps.app.get('/all_imports.html', function (req, res) { |
||
131 | var theme = self.deps.config.preferences.get('plugins:ui-manager').selectedUI; |
||
132 | var ua = req.header('user-agent'); |
||
133 | // Check the user-agent string to identyfy the device. |
||
134 | if (/mobile|iphone|ipod|android|blackberry|opera|mini|windows\sce|palm|smartphone|iemobile|ipad|android|android 3.0|xoom|sch-i800|playbook|tablet|kindle/i.test(ua)) { |
||
135 | theme = 'mobile-ui'; |
||
136 | } |
||
137 | //You can override the theme by passing theme=<themename> in the query string |
||
138 | if (req.query && req.query.theme) { |
||
139 | theme = req.query.theme; |
||
140 | } |
||
141 | var applet; |
||
142 | if (req.query && req.query.app) { |
||
143 | applet = req.query.app; |
||
144 | } |
||
145 | theme = theme === undefined ? 'new-ui' : theme; |
||
146 | var scriplets = self.getAppletsByTheme(self.getApplets(), theme); |
||
147 | //TODO: Add theme to the message so you can differentiate the applets by theme |
||
148 | //and ignore if it is not the theme you are using. |
||
149 | //TODO: Look for applet.ejs.disable in a theme to remove the applet option. |
||
150 | self.deps.cockpit.emit('ui-manager-applets', scriplets.filter(function (item) { |
||
151 | return [ |
||
152 | 'footer', |
||
153 | 'header', |
||
154 | 'head' |
||
155 | ].indexOf(item.name) == -1; |
||
156 | })); |
||
157 | res.render(__dirname + '/all_imports.ejs', { |
||
158 | title: 'OpenROV ROV Cockpit', |
||
159 | scripts: pathInfo.scripts, |
||
160 | styles: pathInfo.styles, |
||
161 | sysscripts: pathInfo.sysscripts, |
||
162 | webcomponents: pathInfo.webcomponents.filter(function(wcInfo){ |
||
163 | return (wcInfo.path.indexOf('ui-manager/orov-widget-registry.html')==-1) |
||
0 ignored issues
–
show
There should be a semicolon.
Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers. Further Readings: ![]() |
|||
164 | }), |
||
165 | config: self.deps.config, |
||
166 | scriplet: scriplets.find(function (item) { |
||
167 | return item.name == applet; |
||
168 | }), |
||
169 | scriplets: scriplets, |
||
170 | theme: theme |
||
171 | }); |
||
172 | }); |
||
173 | |||
174 | this.deps.app.get('/all_scripts.html', function (req, res) { |
||
175 | var theme = self.deps.config.preferences.get('plugins:ui-manager').selectedUI; |
||
176 | var ua = req.header('user-agent'); |
||
177 | // Check the user-agent string to identyfy the device. |
||
178 | if (/mobile|iphone|ipod|android|blackberry|opera|mini|windows\sce|palm|smartphone|iemobile|ipad|android|android 3.0|xoom|sch-i800|playbook|tablet|kindle/i.test(ua)) { |
||
179 | theme = 'mobile-ui'; |
||
180 | } |
||
181 | //You can override the theme by passing theme=<themename> in the query string |
||
182 | if (req.query && req.query.theme) { |
||
183 | theme = req.query.theme; |
||
184 | } |
||
185 | var applet; |
||
186 | if (req.query && req.query.app) { |
||
187 | applet = req.query.app; |
||
188 | } |
||
189 | theme = theme === undefined ? 'new-ui' : theme; |
||
190 | var scriplets = self.getAppletsByTheme(self.getApplets(), theme); |
||
191 | //TODO: Add theme to the message so you can differentiate the applets by theme |
||
192 | //and ignore if it is not the theme you are using. |
||
193 | //TODO: Look for applet.ejs.disable in a theme to remove the applet option. |
||
194 | self.deps.cockpit.emit('ui-manager-applets', scriplets.filter(function (item) { |
||
195 | return [ |
||
196 | 'footer', |
||
197 | 'header', |
||
198 | 'head' |
||
199 | ].indexOf(item.name) == -1; |
||
200 | })); |
||
201 | res.render(__dirname + '/all_scripts.ejs', { |
||
202 | title: 'OpenROV ROV Cockpit', |
||
203 | scripts: pathInfo.scripts, |
||
204 | styles: pathInfo.styles, |
||
205 | sysscripts: pathInfo.sysscripts, |
||
206 | webcomponents: pathInfo.webcomponents.filter(function(wcInfo){ |
||
207 | return (wcInfo.path.indexOf('ui-manager/orov-widget-registry.html')==-1) |
||
0 ignored issues
–
show
There should be a semicolon.
Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers. Further Readings: ![]() |
|||
208 | }), |
||
209 | config: self.deps.config, |
||
210 | scriplet: scriplets.find(function (item) { |
||
211 | return item.name == applet; |
||
212 | }), |
||
213 | scriplets: scriplets, |
||
214 | theme: theme |
||
215 | }); |
||
216 | }); |
||
217 | |||
218 | |||
219 | }; |
||
220 | UIManager.prototype.getAppletsByTheme = function getAppletsByTheme(applets, theme) { |
||
221 | //This overrides any applet with those in the theme folder |
||
222 | var result = {}; |
||
223 | applets.base.forEach(function (item) { |
||
224 | result[item.name] = item; |
||
225 | }); |
||
226 | if (applets[theme] !== undefined) { |
||
227 | applets[theme].forEach(function (item) { |
||
228 | result[item.name] = item; |
||
229 | }); |
||
230 | } |
||
231 | return Object.keys(result).map(function (key) { |
||
232 | return result[key]; |
||
233 | }); |
||
234 | }; |
||
235 | UIManager.prototype.getApplets = function getApplets() { |
||
236 | var result = { base: [] }; |
||
237 | this.deps.loadedPlugins.forEach(function (plugin) { |
||
238 | if (plugin.plugin !== undefined && plugin.plugin.name == 'ui-manager') { |
||
239 | return; |
||
240 | } |
||
241 | if (plugin == undefined) { |
||
0 ignored issues
–
show
It is recommended to use
=== to compare with undefined .
Generally, it is recommended to use strict comparison whenever possible and not to rely on the weaker type-juggling comparison operator. ![]() |
|||
242 | return; |
||
243 | } |
||
244 | rpath = 'base'; |
||
245 | if (plugin.plugin !== undefined && plugin.plugin.type === 'theme') { |
||
246 | rpath = plugin.plugin.name; |
||
247 | if (result[rpath] == undefined) { |
||
0 ignored issues
–
show
It is recommended to use
=== to compare with undefined .
Generally, it is recommended to use strict comparison whenever possible and not to rely on the weaker type-juggling comparison operator. ![]() |
|||
248 | result[rpath] = []; |
||
249 | } |
||
250 | } |
||
251 | result[rpath] = result[rpath].concat(plugin._raw.applets.map(function (item) { |
||
252 | logger.debug('adding: ' + rpath + ': ' + item); |
||
253 | return { |
||
254 | name: path.basename(item.path, '.ejs'), |
||
255 | path: item.path, |
||
256 | iconMeta: item.icon === undefined ? null : fs.readFileSync(item.icon, 'utf8').replace(/(\r\n|\n|\r)/gm, '') |
||
257 | }; |
||
258 | })); |
||
259 | }); |
||
260 | logger.debug('getApplets'); |
||
261 | logger.debug( JSON.stringify( result ) ); |
||
262 | return result; |
||
263 | }; |
||
264 | UIManager.prototype.getSettingSchema = function getSettingSchema() { |
||
265 | var UIOptions = []; |
||
266 | this.deps.loadedPlugins.forEach(function (plugin) { |
||
267 | if (plugin !== undefined) { |
||
268 | if (plugin.plugin !== undefined && plugin.plugin.type === 'theme') { |
||
269 | UIOptions.push(plugin.plugin.name); |
||
270 | } |
||
271 | } |
||
272 | }); |
||
273 | return [{ |
||
274 | 'title': 'Themes', |
||
275 | 'category': 'ui', |
||
276 | 'type': 'object', |
||
277 | 'id': 'ui-manager', |
||
278 | 'properties': { |
||
279 | 'selectedUI': { |
||
280 | 'title' : 'Current theme', |
||
281 | 'type': 'string', |
||
282 | 'default': 'new-ui', |
||
283 | 'enum': UIOptions |
||
284 | } |
||
285 | }, |
||
286 | 'required': ['selectedUI'] |
||
287 | }]; |
||
288 | }; |
||
289 | module.exports = function (name, deps) { |
||
290 | return new UIManager(name, deps); |
||
291 | }; |