]> git.immae.eu Git - github/bastienwirtz/homer.git/blame - src/App.vue
Changes relative paths to hash. Avoids problems with #8
[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">
c6267296 16 <a href="#"><img v-if="config.logo" :src="config.logo" alt="dashboard logo" /></a>
239ef168 17 <i v-if="config.icon" :class="config.icon"></i>
b9c5fcf0
BW
18 </div>
19 <div class="dashboard-title">
20 <span class="headline">{{ config.subtitle }}</span>
21 <h1>{{ config.title }}</h1>
22 </div>
23 </div>
24 </section>
25
26 <Navbar
27 :open="showMenu"
28 :links="config.links"
ed8b17e0 29 @navbar-toggle="showMenu = !showMenu"
b9c5fcf0
BW
30 >
31 <DarkMode @updated="isDark = $event" />
32
33 <SettingToggle
34 @updated="vlayout = $event"
35 name="vlayout"
36 icon="fa-list"
37 iconAlt="fa-columns"
38 />
39
40 <SearchInput
41 class="navbar-item is-inline-block-mobile"
42 @input="filterServices"
ed8b17e0
BW
43 @search-focus="showMenu = true"
44 @search-open="navigateToFirstService"
45 @search-cancel="filterServices"
b9c5fcf0
BW
46 />
47 </Navbar>
48 </div>
49
50 <section id="main-section" class="section">
51 <div v-cloak class="container">
e9113b48
BW
52 <ConnectivityChecker
53 v-if="config.connectivityCheck"
ed8b17e0 54 @network-status-update="offline = $event"
e9113b48 55 />
b9c5fcf0
BW
56 <div v-if="!offline">
57 <!-- Optional messages -->
58 <Message :item="config.message" />
59
60 <!-- Horizontal layout -->
61 <div v-if="!vlayout || filter" class="columns is-multiline">
62 <template v-for="group in services">
63 <h2 v-if="group.name" class="column is-full group-title">
da6e676d 64 <i v-if="group.icon" :class="['fa-fw', group.icon]"></i>
b9c5fcf0
BW
65 {{ group.name }}
66 </h2>
67 <Service
e1ecf86f 68 v-for="(item, index) in group.items"
69 :key="index"
b9c5fcf0 70 v-bind:item="item"
9e4fe0d2 71 :class="['column', `is-${12 / config.columns}`]"
b9c5fcf0
BW
72 />
73 </template>
74 </div>
75
76 <!-- Vertical layout -->
77 <div
78 v-if="!filter && vlayout"
79 class="columns is-multiline layout-vertical"
80 >
81 <div
9e4fe0d2 82 :class="['column', `is-${12 / config.columns}`]"
b9c5fcf0
BW
83 v-for="group in services"
84 :key="group.name"
85 >
86 <h2 v-if="group.name" class="group-title">
da6e676d 87 <i v-if="group.icon" :class="['fa-fw', group.icon]"></i>
b9c5fcf0
BW
88 {{ group.name }}
89 </h2>
90 <Service
e1ecf86f 91 v-for="(item, index) in group.items"
92 :key="index"
b9c5fcf0 93 v-bind:item="item"
b9c5fcf0
BW
94 />
95 </div>
96 </div>
97 </div>
98 </div>
99 </section>
100
101 <footer class="footer">
102 <div class="container">
103 <div
104 class="content has-text-centered"
105 v-if="config.footer"
106 v-html="config.footer"
107 ></div>
108 </div>
109 </footer>
110 </div>
111</template>
112
113<script>
114const jsyaml = require("js-yaml");
115const merge = require("lodash.merge");
116
117import Navbar from "./components/Navbar.vue";
118import ConnectivityChecker from "./components/ConnectivityChecker.vue";
119import Service from "./components/Service.vue";
120import Message from "./components/Message.vue";
121import SearchInput from "./components/SearchInput.vue";
122import SettingToggle from "./components/SettingToggle.vue";
123import DarkMode from "./components/DarkMode.vue";
124import DynamicTheme from "./components/DynamicTheme.vue";
125
126import defaultConfig from "./assets/defaults.yml";
127
128export default {
129 name: "App",
130 components: {
131 Navbar,
132 ConnectivityChecker,
133 Service,
134 Message,
135 SearchInput,
136 SettingToggle,
137 DarkMode,
9814a037 138 DynamicTheme,
b9c5fcf0
BW
139 },
140 data: function () {
141 return {
142 config: null,
143 services: null,
144 offline: false,
145 filter: "",
146 vlayout: true,
147 isDark: null,
9814a037 148 showMenu: false,
b9c5fcf0
BW
149 };
150 },
151 created: async function () {
bd910942 152 const defaults = jsyaml.load(defaultConfig);
0ae40f78 153 let config;
c6267296 154 window.onhashchange = function() { location.reload(); };
0ae40f78
BW
155 try {
156 config = await this.getConfig();
c6267296 157 const path = (window.location.hash.substring(1) != '') ? window.location.hash.substring(1) : null;
e3bd2ecc 158 if (path) {
c6267296 159 let pathConfig = await this.getConfig(`assets/${path}.yml`); // the slash (/) is included in the pathname
e3bd2ecc 160 for (const prop in pathConfig) config[prop] = pathConfig[prop];
161 }
162 // config = await this.getConfig(path ? `assets/${path}.yml` : null);
163 //config = await (path ? this.getConfig(`assets/${path}.yml`) : this.getConfig())
0ae40f78
BW
164 } catch (error) {
165 console.log(error);
166 config = this.handleErrors("⚠️ Error loading configuration", error);
167 }
bd910942
BW
168 this.config = merge(defaults, config);
169 this.services = this.config.services;
67fd101a
BW
170 document.title =
171 this.config.documentTitle ||
172 `${this.config.title} | ${this.config.subtitle}`;
6777bc34 173 if (this.config.stylesheet) {
ffe3404a 174 let stylesheet = "";
8e5ee54a
GC
175 for (const file of this.config.stylesheet) {
176 stylesheet += `@import "${file}";`;
177 }
178 this.createStylesheet(stylesheet);
6777bc34 179 }
b9c5fcf0
BW
180 },
181 methods: {
b102c9b2 182 getConfig: function (path = "assets/config.yml") {
1a42e30a 183 return fetch(path).then((response) => {
0ae40f78
BW
184 if (response.redirected) {
185 // This allows to work with authentication proxies.
186 window.location.href = response.url;
187 return;
188 }
1a42e30a 189 if (!response.ok) {
0ae40f78 190 throw Error(`${response.statusText}: ${response.body}`);
1a42e30a
BW
191 }
192
193 const that = this;
194 return response
195 .text()
196 .then((body) => {
bd910942 197 return jsyaml.load(body);
1a42e30a
BW
198 })
199 .then(function (config) {
200 if (config.externalConfig) {
201 return that.getConfig(config.externalConfig);
202 }
203 return config;
bd910942 204 });
1a42e30a 205 });
b9c5fcf0
BW
206 },
207 matchesFilter: function (item) {
208 return (
209 item.name.toLowerCase().includes(this.filter) ||
1c451e11 210 (item.subtitle && item.subtitle.toLowerCase().includes(this.filter)) ||
b9c5fcf0
BW
211 (item.tag && item.tag.toLowerCase().includes(this.filter))
212 );
213 },
214 navigateToFirstService: function (target) {
215 try {
216 const service = this.services[0].items[0];
217 window.open(service.url, target || service.target || "_self");
218 } catch (error) {
219 console.warning("fail to open service");
220 }
221 },
222 filterServices: function (filter) {
223 this.filter = filter;
224
225 if (!filter) {
226 this.services = this.config.services;
227 return;
228 }
229
230 const searchResultItems = [];
231 for (const group of this.config.services) {
232 for (const item of group.items) {
233 if (this.matchesFilter(item)) {
234 searchResultItems.push(item);
235 }
236 }
237 }
238
239 this.services = [
240 {
241 name: filter,
242 icon: "fas fa-search",
9814a037
BW
243 items: searchResultItems,
244 },
b9c5fcf0 245 ];
9814a037 246 },
bd910942
BW
247 handleErrors: function (title, content) {
248 return {
249 message: {
250 title: title,
251 style: "is-danger",
252 content: content,
253 },
254 };
255 },
ffe3404a
BW
256 createStylesheet: function (css) {
257 let style = document.createElement("style");
6777bc34
GC
258 style.appendChild(document.createTextNode(css));
259 document.head.appendChild(style);
260 },
9814a037 261 },
b9c5fcf0
BW
262};
263</script>