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