]> git.immae.eu Git - github/bastienwirtz/homer.git/blob - src/App.vue
Pass new forwarder config to services, give it a default value of empty
[github/bastienwirtz/homer.git] / src / App.vue
1 <template>
2 <div
3 id="app"
4 v-if="config"
5 :class="[
6 `theme-${config.theme}`,
7 isDark ? 'is-dark' : 'is-light',
8 !config.footer ? 'no-footer' : '',
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">
16 <a href="#">
17 <img v-if="config.logo" :src="config.logo" alt="dashboard logo" />
18 </a>
19 <i v-if="config.icon" :class="config.icon"></i>
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"
31 @navbar-toggle="showMenu = !showMenu"
32 >
33 <DarkMode
34 @updated="isDark = $event"
35 :defaultValue="this.config.defaults.colorTheme"
36 />
37
38 <SettingToggle
39 @updated="vlayout = $event"
40 name="vlayout"
41 icon="fa-list"
42 iconAlt="fa-columns"
43 :defaultValue="this.config.defaults.layout == 'columns'"
44 />
45
46 <SearchInput
47 class="navbar-item is-inline-block-mobile"
48 :hotkey="searchHotkey()"
49 @input="filterServices"
50 @search-focus="showMenu = true"
51 @search-open="navigateToFirstService"
52 @search-cancel="filterServices"
53 />
54 </Navbar>
55 </div>
56
57 <section id="main-section" class="section">
58 <div v-cloak class="container">
59 <ConnectivityChecker
60 v-if="config.connectivityCheck"
61 @network-status-update="offline = $event"
62 />
63
64 <GetStarted v-if="loaded && !services" />
65
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">
74 <i v-if="group.icon" :class="['fa-fw', group.icon]"></i>
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>
80 {{ group.name }}
81 </h2>
82 <Service
83 v-for="(item, index) in group.items"
84 :key="index"
85 :item="item"
86 :proxy="config.proxy"
87 :forwarder="config.forwarder"
88 :class="['column', `is-${12 / config.columns}`]"
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
99 :class="['column', `is-${12 / config.columns}`]"
100 v-for="group in services"
101 :key="group.name"
102 >
103 <h2 v-if="group.name" class="group-title">
104 <i v-if="group.icon" :class="['fa-fw', group.icon]"></i>
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>
110 {{ group.name }}
111 </h2>
112 <Service
113 v-for="(item, index) in group.items"
114 :key="index"
115 :item="item"
116 :proxy="config.proxy"
117 :forwarder="config.forwarder"
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>
138 const jsyaml = require("js-yaml");
139 const merge = require("lodash.merge");
140
141 import Navbar from "./components/Navbar.vue";
142 import GetStarted from "./components/GetStarted.vue";
143 import ConnectivityChecker from "./components/ConnectivityChecker.vue";
144 import Service from "./components/Service.vue";
145 import Message from "./components/Message.vue";
146 import SearchInput from "./components/SearchInput.vue";
147 import SettingToggle from "./components/SettingToggle.vue";
148 import DarkMode from "./components/DarkMode.vue";
149 import DynamicTheme from "./components/DynamicTheme.vue";
150
151 import defaultConfig from "./assets/defaults.yml";
152
153 export default {
154 name: "App",
155 components: {
156 Navbar,
157 GetStarted,
158 ConnectivityChecker,
159 Service,
160 Message,
161 SearchInput,
162 SettingToggle,
163 DarkMode,
164 DynamicTheme,
165 },
166 data: function () {
167 return {
168 loaded: false,
169 config: null,
170 services: null,
171 offline: false,
172 filter: "",
173 vlayout: true,
174 isDark: null,
175 showMenu: false,
176 };
177 },
178 created: async function () {
179 this.buildDashboard();
180 window.onhashchange = this.buildDashboard;
181 this.loaded = true;
182 },
183 methods: {
184 searchHotkey() {
185 if (this.config.hotkey && this.config.hotkey.search) {
186 return this.config.hotkey.search;
187 }
188 },
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;
209
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 },
221 getConfig: function (path = "assets/config.yml") {
222 return fetch(path).then((response) => {
223 if (response.redirected) {
224 // This allows to work with authentication proxies.
225 window.location.href = response.url;
226 return;
227 }
228
229 if (!response.ok) {
230 throw Error(`${response.statusText}: ${response.body}`);
231 }
232
233 const that = this;
234 return response
235 .text()
236 .then((body) => {
237 return jsyaml.load(body);
238 })
239 .then(function (config) {
240 if (config.externalConfig) {
241 return that.getConfig(config.externalConfig);
242 }
243 return config;
244 });
245 });
246 },
247 matchesFilter: function (item) {
248 return (
249 item.name.toLowerCase().includes(this.filter) ||
250 (item.subtitle && item.subtitle.toLowerCase().includes(this.filter)) ||
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",
283 items: searchResultItems,
284 },
285 ];
286 },
287 handleErrors: function (title, content) {
288 return {
289 message: {
290 title: title,
291 style: "is-danger",
292 content: content,
293 },
294 };
295 },
296 createStylesheet: function (css) {
297 let style = document.createElement("style");
298 style.appendChild(document.createTextNode(css));
299 document.head.appendChild(style);
300 },
301 },
302 };
303 </script>