]> git.immae.eu Git - github/bastienwirtz/homer.git/blame - src/App.vue
Pass new forwarder config to services, give it a default value of empty
[github/bastienwirtz/homer.git] / src / App.vue
CommitLineData
b9c5fcf0
BW
1<template>
2 <div
3 id="app"
4 v-if="config"
5 :class="[
6 `theme-${config.theme}`,
7 isDark ? 'is-dark' : 'is-light',
9814a037 8 !config.footer ? 'no-footer' : '',
b9c5fcf0
BW
9 ]"
10 >
11 <DynamicTheme :themes="config.colors" />
12 <div id="bighead">
13 <section v-if="config.header" class="first-line">
14 <div v-cloak class="container">
15 <div class="logo">
ba07da6b
BW
16 <a href="#">
17 <img v-if="config.logo" :src="config.logo" alt="dashboard logo" />
18 </a>
239ef168 19 <i v-if="config.icon" :class="config.icon"></i>
b9c5fcf0
BW
20 </div>
21 <div class="dashboard-title">
22 <span class="headline">{{ config.subtitle }}</span>
23 <h1>{{ config.title }}</h1>
24 </div>
25 </div>
26 </section>
27
28 <Navbar
29 :open="showMenu"
30 :links="config.links"
ed8b17e0 31 @navbar-toggle="showMenu = !showMenu"
b9c5fcf0 32 >
5db2414d
A
33 <DarkMode
34 @updated="isDark = $event"
35 :defaultValue="this.config.defaults.colorTheme"
36 />
b9c5fcf0
BW
37
38 <SettingToggle
39 @updated="vlayout = $event"
40 name="vlayout"
41 icon="fa-list"
42 iconAlt="fa-columns"
5db2414d 43 :defaultValue="this.config.defaults.layout == 'columns'"
b9c5fcf0
BW
44 />
45
46 <SearchInput
47 class="navbar-item is-inline-block-mobile"
b4a2db6e 48 :hotkey="searchHotkey()"
b9c5fcf0 49 @input="filterServices"
ed8b17e0
BW
50 @search-focus="showMenu = true"
51 @search-open="navigateToFirstService"
52 @search-cancel="filterServices"
b9c5fcf0
BW
53 />
54 </Navbar>
55 </div>
56
57 <section id="main-section" class="section">
58 <div v-cloak class="container">
e9113b48
BW
59 <ConnectivityChecker
60 v-if="config.connectivityCheck"
ed8b17e0 61 @network-status-update="offline = $event"
e9113b48 62 />
f3980069
BW
63
64 <GetStarted v-if="loaded && !services" />
65
b9c5fcf0
BW
66 <div v-if="!offline">
67 <!-- Optional messages -->
68 <Message :item="config.message" />
69
70 <!-- Horizontal layout -->
71 <div v-if="!vlayout || filter" class="columns is-multiline">
72 <template v-for="group in services">
73 <h2 v-if="group.name" class="column is-full group-title">
da6e676d 74 <i v-if="group.icon" :class="['fa-fw', group.icon]"></i>
42477020
GC
75 <div v-else-if="group.logo" class="group-logo media-left">
76 <figure class="image is-48x48">
77 <img :src="group.logo" :alt="`${group.name} logo`" />
78 </figure>
79 </div>
b9c5fcf0
BW
80 {{ group.name }}
81 </h2>
82 <Service
e1ecf86f 83 v-for="(item, index) in group.items"
84 :key="index"
0a3be103
BW
85 :item="item"
86 :proxy="config.proxy"
76d30be8 87 :forwarder="config.forwarder"
9e4fe0d2 88 :class="['column', `is-${12 / config.columns}`]"
b9c5fcf0
BW
89 />
90 </template>
91 </div>
92
93 <!-- Vertical layout -->
94 <div
95 v-if="!filter && vlayout"
96 class="columns is-multiline layout-vertical"
97 >
98 <div
9e4fe0d2 99 :class="['column', `is-${12 / config.columns}`]"
b9c5fcf0
BW
100 v-for="group in services"
101 :key="group.name"
102 >
103 <h2 v-if="group.name" class="group-title">
da6e676d 104 <i v-if="group.icon" :class="['fa-fw', group.icon]"></i>
42477020
GC
105 <div v-else-if="group.logo" class="group-logo media-left">
106 <figure class="image is-48x48">
107 <img :src="group.logo" :alt="`${group.name} logo`" />
108 </figure>
109 </div>
b9c5fcf0
BW
110 {{ group.name }}
111 </h2>
112 <Service
e1ecf86f 113 v-for="(item, index) in group.items"
114 :key="index"
0a3be103
BW
115 :item="item"
116 :proxy="config.proxy"
76d30be8 117 :forwarder="config.forwarder"
b9c5fcf0
BW
118 />
119 </div>
120 </div>
121 </div>
122 </div>
123 </section>
124
125 <footer class="footer">
126 <div class="container">
127 <div
128 class="content has-text-centered"
129 v-if="config.footer"
130 v-html="config.footer"
131 ></div>
132 </div>
133 </footer>
134 </div>
135</template>
136
137<script>
138const jsyaml = require("js-yaml");
139const merge = require("lodash.merge");
140
141import Navbar from "./components/Navbar.vue";
f3980069 142import GetStarted from "./components/GetStarted.vue";
b9c5fcf0
BW
143import ConnectivityChecker from "./components/ConnectivityChecker.vue";
144import Service from "./components/Service.vue";
145import Message from "./components/Message.vue";
146import SearchInput from "./components/SearchInput.vue";
147import SettingToggle from "./components/SettingToggle.vue";
148import DarkMode from "./components/DarkMode.vue";
149import DynamicTheme from "./components/DynamicTheme.vue";
150
151import defaultConfig from "./assets/defaults.yml";
152
153export default {
154 name: "App",
155 components: {
156 Navbar,
f3980069 157 GetStarted,
b9c5fcf0
BW
158 ConnectivityChecker,
159 Service,
160 Message,
161 SearchInput,
162 SettingToggle,
163 DarkMode,
9814a037 164 DynamicTheme,
b9c5fcf0
BW
165 },
166 data: function () {
167 return {
f3980069 168 loaded: false,
b9c5fcf0
BW
169 config: null,
170 services: null,
171 offline: false,
172 filter: "",
173 vlayout: true,
174 isDark: null,
9814a037 175 showMenu: false,
b9c5fcf0
BW
176 };
177 },
178 created: async function () {
ba07da6b
BW
179 this.buildDashboard();
180 window.onhashchange = this.buildDashboard;
f3980069 181 this.loaded = true;
b9c5fcf0
BW
182 },
183 methods: {
446e78d2
RS
184 searchHotkey() {
185 if (this.config.hotkey && this.config.hotkey.search) {
186 return this.config.hotkey.search;
187 }
188 },
ba07da6b
BW
189 buildDashboard: async function () {
190 const defaults = jsyaml.load(defaultConfig);
191 let config;
192 try {
193 config = await this.getConfig();
194 const path =
195 window.location.hash.substring(1) != ""
196 ? window.location.hash.substring(1)
197 : null;
198
199 if (path) {
200 let pathConfig = await this.getConfig(`assets/${path}.yml`); // the slash (/) is included in the pathname
201 config = Object.assign(config, pathConfig);
202 }
203 } catch (error) {
204 console.log(error);
205 config = this.handleErrors("⚠️ Error loading configuration", error);
206 }
207 this.config = merge(defaults, config);
208 this.services = this.config.services;
f3980069 209
ba07da6b
BW
210 document.title =
211 this.config.documentTitle ||
212 `${this.config.title} | ${this.config.subtitle}`;
213 if (this.config.stylesheet) {
214 let stylesheet = "";
215 for (const file of this.config.stylesheet) {
216 stylesheet += `@import "${file}";`;
217 }
218 this.createStylesheet(stylesheet);
219 }
220 },
b102c9b2 221 getConfig: function (path = "assets/config.yml") {
1a42e30a 222 return fetch(path).then((response) => {
0ae40f78
BW
223 if (response.redirected) {
224 // This allows to work with authentication proxies.
225 window.location.href = response.url;
226 return;
227 }
893690cf 228
1a42e30a 229 if (!response.ok) {
0ae40f78 230 throw Error(`${response.statusText}: ${response.body}`);
1a42e30a
BW
231 }
232
233 const that = this;
234 return response
235 .text()
236 .then((body) => {
bd910942 237 return jsyaml.load(body);
1a42e30a
BW
238 })
239 .then(function (config) {
240 if (config.externalConfig) {
241 return that.getConfig(config.externalConfig);
242 }
243 return config;
bd910942 244 });
1a42e30a 245 });
b9c5fcf0
BW
246 },
247 matchesFilter: function (item) {
248 return (
249 item.name.toLowerCase().includes(this.filter) ||
1c451e11 250 (item.subtitle && item.subtitle.toLowerCase().includes(this.filter)) ||
b9c5fcf0
BW
251 (item.tag && item.tag.toLowerCase().includes(this.filter))
252 );
253 },
254 navigateToFirstService: function (target) {
255 try {
256 const service = this.services[0].items[0];
257 window.open(service.url, target || service.target || "_self");
258 } catch (error) {
259 console.warning("fail to open service");
260 }
261 },
262 filterServices: function (filter) {
263 this.filter = filter;
264
265 if (!filter) {
266 this.services = this.config.services;
267 return;
268 }
269
270 const searchResultItems = [];
271 for (const group of this.config.services) {
272 for (const item of group.items) {
273 if (this.matchesFilter(item)) {
274 searchResultItems.push(item);
275 }
276 }
277 }
278
279 this.services = [
280 {
281 name: filter,
282 icon: "fas fa-search",
9814a037
BW
283 items: searchResultItems,
284 },
b9c5fcf0 285 ];
9814a037 286 },
bd910942
BW
287 handleErrors: function (title, content) {
288 return {
289 message: {
290 title: title,
291 style: "is-danger",
292 content: content,
293 },
294 };
295 },
ffe3404a
BW
296 createStylesheet: function (css) {
297 let style = document.createElement("style");
6777bc34
GC
298 style.appendChild(document.createTextNode(css));
299 document.head.appendChild(style);
300 },
9814a037 301 },
b9c5fcf0
BW
302};
303</script>