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