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