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