]> git.immae.eu Git - github/bastienwirtz/homer.git/blob - src/App.vue
Merge pull request #120 from tpansino/feature/custom-document-title
[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 <img v-if="config.logo" :src="config.logo" alt="dashboard logo" />
17 <i v-if="config.icon" :class="config.icon"></i>
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"
29 @navbar:toggle="showMenu = !showMenu"
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"
43 @search:focus="showMenu = true"
44 @search:open="navigateToFirstService"
45 @search:cancel="filterServices"
46 />
47 </Navbar>
48 </div>
49
50 <section id="main-section" class="section">
51 <div v-cloak class="container">
52 <ConnectivityChecker
53 v-if="config.connectivityCheck"
54 @network:status-update="offline = $event"
55 />
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">
64 <i v-if="group.icon" :class="['fa-fw', group.icon]"></i>
65 {{ group.name }}
66 </h2>
67 <Service
68 v-for="item in group.items"
69 :key="item.name"
70 v-bind:item="item"
71 :class="['column', `is-${12 / config.columns}`]"
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
82 :class="['column', `is-${12 / config.columns}`]"
83 v-for="group in services"
84 :key="group.name"
85 >
86 <h2 v-if="group.name" class="group-title">
87 <i v-if="group.icon" :class="['fa-fw', group.icon]"></i>
88 {{ group.name }}
89 </h2>
90 <Service
91 v-for="item in group.items"
92 v-bind:item="item"
93 :key="item.url"
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>
114 const jsyaml = require("js-yaml");
115 const merge = require("lodash.merge");
116
117 import Navbar from "./components/Navbar.vue";
118 import ConnectivityChecker from "./components/ConnectivityChecker.vue";
119 import Service from "./components/Service.vue";
120 import Message from "./components/Message.vue";
121 import SearchInput from "./components/SearchInput.vue";
122 import SettingToggle from "./components/SettingToggle.vue";
123 import DarkMode from "./components/DarkMode.vue";
124 import DynamicTheme from "./components/DynamicTheme.vue";
125
126 import defaultConfig from "./assets/defaults.yml";
127
128 export default {
129 name: "App",
130 components: {
131 Navbar,
132 ConnectivityChecker,
133 Service,
134 Message,
135 SearchInput,
136 SettingToggle,
137 DarkMode,
138 DynamicTheme,
139 },
140 data: function () {
141 return {
142 config: null,
143 services: null,
144 offline: false,
145 filter: "",
146 vlayout: true,
147 isDark: null,
148 showMenu: false,
149 };
150 },
151 created: async function () {
152 const defaults = jsyaml.load(defaultConfig);
153 let config;
154 try {
155 config = await this.getConfig();
156 } catch (error) {
157 console.log(error);
158 config = this.handleErrors("⚠️ Error loading configuration", error);
159 }
160 this.config = merge(defaults, config);
161 this.services = this.config.services;
162 document.title = this.config.documentTitle || `${this.config.title} | ${this.config.subtitle}`;
163 if (this.config.stylesheet) {
164 let stylesheet = "";
165 for (const file of this.config.stylesheet) {
166 stylesheet += `@import "${file}";`;
167 }
168 this.createStylesheet(stylesheet);
169 }
170 },
171 methods: {
172 getConfig: function (path = "assets/config.yml") {
173 return fetch(path).then((response) => {
174 if (response.redirected) {
175 // This allows to work with authentication proxies.
176 window.location.href = response.url;
177 return;
178 }
179 if (!response.ok) {
180 throw Error(`${response.statusText}: ${response.body}`);
181 }
182
183 const that = this;
184 return response
185 .text()
186 .then((body) => {
187 return jsyaml.load(body);
188 })
189 .then(function (config) {
190 if (config.externalConfig) {
191 return that.getConfig(config.externalConfig);
192 }
193 return config;
194 });
195 });
196 },
197 matchesFilter: function (item) {
198 return (
199 item.name.toLowerCase().includes(this.filter) ||
200 (item.tag && item.tag.toLowerCase().includes(this.filter))
201 );
202 },
203 navigateToFirstService: function (target) {
204 try {
205 const service = this.services[0].items[0];
206 window.open(service.url, target || service.target || "_self");
207 } catch (error) {
208 console.warning("fail to open service");
209 }
210 },
211 filterServices: function (filter) {
212 this.filter = filter;
213
214 if (!filter) {
215 this.services = this.config.services;
216 return;
217 }
218
219 const searchResultItems = [];
220 for (const group of this.config.services) {
221 for (const item of group.items) {
222 if (this.matchesFilter(item)) {
223 searchResultItems.push(item);
224 }
225 }
226 }
227
228 this.services = [
229 {
230 name: filter,
231 icon: "fas fa-search",
232 items: searchResultItems,
233 },
234 ];
235 },
236 handleErrors: function (title, content) {
237 return {
238 message: {
239 title: title,
240 style: "is-danger",
241 content: content,
242 },
243 };
244 },
245 createStylesheet: function (css) {
246 let style = document.createElement("style");
247 style.appendChild(document.createTextNode(css));
248 document.head.appendChild(style);
249 },
250 },
251 };
252 </script>