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