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