]> git.immae.eu Git - github/bastienwirtz/homer.git/blob - src/App.vue
Simplify the connectivity checker
[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 @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"
45 @search-focus="showMenu = true"
46 @search-open="navigateToFirstService"
47 @search-cancel="filterServices"
48 />
49 </Navbar>
50 </div>
51
52 <section id="main-section" class="section">
53 <div v-cloak class="container">
54 <ConnectivityChecker
55 v-if="config.connectivityCheck"
56 @network-status-update="offline = $event"
57 />
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">
66 <i v-if="group.icon" :class="['fa-fw', group.icon]"></i>
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>
72 {{ group.name }}
73 </h2>
74 <Service
75 v-for="(item, index) in group.items"
76 :key="index"
77 v-bind:item="item"
78 :class="['column', `is-${12 / config.columns}`]"
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
89 :class="['column', `is-${12 / config.columns}`]"
90 v-for="group in services"
91 :key="group.name"
92 >
93 <h2 v-if="group.name" class="group-title">
94 <i v-if="group.icon" :class="['fa-fw', group.icon]"></i>
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>
100 {{ group.name }}
101 </h2>
102 <Service
103 v-for="(item, index) in group.items"
104 :key="index"
105 v-bind:item="item"
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>
126 const jsyaml = require("js-yaml");
127 const merge = require("lodash.merge");
128
129 import Navbar from "./components/Navbar.vue";
130 import ConnectivityChecker from "./components/ConnectivityChecker.vue";
131 import Service from "./components/Service.vue";
132 import Message from "./components/Message.vue";
133 import SearchInput from "./components/SearchInput.vue";
134 import SettingToggle from "./components/SettingToggle.vue";
135 import DarkMode from "./components/DarkMode.vue";
136 import DynamicTheme from "./components/DynamicTheme.vue";
137
138 import defaultConfig from "./assets/defaults.yml";
139
140 export default {
141 name: "App",
142 components: {
143 Navbar,
144 ConnectivityChecker,
145 Service,
146 Message,
147 SearchInput,
148 SettingToggle,
149 DarkMode,
150 DynamicTheme,
151 },
152 data: function () {
153 return {
154 config: null,
155 services: null,
156 offline: false,
157 filter: "",
158 vlayout: true,
159 isDark: null,
160 showMenu: false,
161 };
162 },
163 created: async function () {
164 this.buildDashboard();
165 window.onhashchange = this.buildDashboard;
166 },
167 methods: {
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 },
199 getConfig: function (path = "assets/config.yml") {
200 return fetch(path).then((response) => {
201 if (response.redirected) {
202 // This allows to work with authentication proxies.
203 window.location.href = response.url;
204 return;
205 }
206 if (!response.ok) {
207 throw Error(`${response.statusText}: ${response.body}`);
208 }
209
210 const that = this;
211 return response
212 .text()
213 .then((body) => {
214 return jsyaml.load(body);
215 })
216 .then(function (config) {
217 if (config.externalConfig) {
218 return that.getConfig(config.externalConfig);
219 }
220 return config;
221 });
222 });
223 },
224 matchesFilter: function (item) {
225 return (
226 item.name.toLowerCase().includes(this.filter) ||
227 (item.subtitle && item.subtitle.toLowerCase().includes(this.filter)) ||
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",
260 items: searchResultItems,
261 },
262 ];
263 },
264 handleErrors: function (title, content) {
265 return {
266 message: {
267 title: title,
268 style: "is-danger",
269 content: content,
270 },
271 };
272 },
273 createStylesheet: function (css) {
274 let style = document.createElement("style");
275 style.appendChild(document.createTextNode(css));
276 document.head.appendChild(style);
277 },
278 },
279 };
280 </script>