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