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