aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/app.js
blob: df6eff4a1b8851549014eef9261f6329a8a0ddf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import projects from './config/projects';
import {
  translate,
  setLocale,
  currentLocale,
  supportedLanguages,
} from './i18n/i18n';
import { createAckeeInstance, setAckeeRecord } from './utilities/ackee';
import {
  hideToBottom,
  hideToLeft,
  showFromBottom,
  showFromLeft,
} from './utilities/animations';

/**
 * Check the size of the current viewport.
 * @returns {Boolean} True if viewport lower than 1200px; false otherwise.
 */
function isSmallVw() {
  return window.innerWidth < 1200;
}

/**
 * Show/hide header and footer with slide animation (left).
 */
function toggleHeaderFooter() {
  const header = document.querySelector('header');
  const footer = document.querySelector('footer');
  const elements = [header, footer];

  elements.forEach((el) => {
    if (el.classList.contains('hide')) {
      showFromLeft(el);
    } else {
      hideToLeft(el);
    }
  });
}

/**
 * Show/hide project details with slide animation (bottom).
 */
function toggleProjectDetails() {
  const details = document.querySelector('.project-details');

  if (details.classList.contains('hide')) {
    showFromBottom(details);
  } else {
    hideToBottom(details);
  }
}

/**
 * Add an event listener to show or hide header and footer.
 */
function listenMenuBtn() {
  const menuBtn = document.querySelector('.btn--menu');
  menuBtn.addEventListener('click', toggleHeaderFooter);
}

/**
 * Show or hide the project details button depending on current location.
 */
function toggleProjectDetailsBtn() {
  const button = document.querySelector('.btn--details');
  const currentPath = window.location.hash;

  if (currentPath) {
    button.style.display = '';
  } else {
    button.style.display = 'none';
  }
}

/**
 * Update the visibility of some DOM elements depending on viewport.
 */
function updateView() {
  const header = document.querySelector('header');
  const footer = document.querySelector('footer');
  const toolbar = document.querySelector('.toolbar');
  const details = document.querySelector('.project-details');

  if (isSmallVw()) {
    header.classList.add('hide');
    footer.classList.add('hide');
    toolbar.classList.remove('hide');
    details?.classList.add('hide');
    details?.classList.remove('fade-in');
  } else {
    showFromLeft(header);
    showFromLeft(footer);
    toolbar.classList.add('hide');
    details?.classList.remove('hide');
    details?.classList.add('fade-in');
  }

  toggleProjectDetailsBtn();
}

/**
 * Update view when the window size changes.
 */
function listenWindowSize() {
  window.addEventListener('resize', updateView);
}

/**
 * Retrieve a project by id.
 * @param {Integer} id - The project id.
 * @returns {Object} The current project.
 */
function getCurrentProject(id) {
  return projects.find((project) => project.id === id);
}

/**
 * Get a list item for the given repo.
 * @param {String} name - The repository name.
 * @param {String} url - The repository URL.
 * @returns {HTMLElement} A list item.
 */
function getRepoItem(name, url) {
  const item = document.createElement('li');
  const link = document.createElement('a');
  const span = document.createElement('span');
  span.classList.add('screen-reader-text');
  span.textContent = name;
  link.classList.add('list__link', `list__link--${name.toLocaleLowerCase()}`);
  link.href = url;
  link.appendChild(span);
  item.classList.add('list__item');
  item.appendChild(link);
  return item;
}

/**
 * Get the repos list wrapped inside ul element and the title.
 * @param {Object[]} repos - An array of repo with name and URL.
 * @returns {[title, list]} An array of HTMLElements for title and list.
 */
function getRepos(repos) {
  if (repos.length === 0) return [];

  const wrapper = document.createElement('div');
  const title = document.createElement('h3');
  const list = document.createElement('ul');
  const items = repos.map((repo) => getRepoItem(repo.name, repo.url));
  title.classList.add('project-details__title');
  title.textContent = translate('main.project.details.repo', {
    count: repos.length,
  });
  list.classList.add('list', 'list--repos');
  list.append(...items);
  wrapper.append(title, list);
  return [title, list];
}

/**
 * Get the technologies list wrapped inside ul element and the title.
 * @param {String[]} technologies - An array of technology names.
 * @returns {[title, list]} An array of HTMLElements for title and list.
 */
function getTechs(technologies) {
  if (technologies.length === 0) return [];

  const title = document.createElement('h3');
  title.classList.add('project-details__title');
  title.textContent = translate('main.project.details.tech', {
    count: technologies.length,
  });
  const list = document.createElement('ul');
  const items = technologies.map((technology) => {
    const item = document.createElement('li');
    item.textContent = technology;
    return item;
  });
  list.classList.add('list', 'list--tech');
  list.append(...items);
  return [title, list];
}

/**
 * Retrieve the project details.
 * @param {Object} project - The project.
 * @returns {HTMLElement} The project details wrapped in a div.
 */
function getProjectDetails(project) {
  const details = document.createElement('div');
  const title = document.createElement('h2');
  const techList = project?.technologies ? getTechs(project.technologies) : [];
  const reposList = getRepos(project.repo);
  const locale = currentLocale();
  let description;

  if (project.description) {
    description = document.createElement('div');
    description.classList.add('project-details__description');
    description.textContent = project.description[locale] || '';
  } else {
    description = '';
  }

  title.classList.add('project-details__title');
  title.textContent = translate('main.project.details.about', {
    name: project.name,
  });
  details.classList.add('project-details');
  if (!isSmallVw()) details.classList.add('fade-in');
  details.replaceChildren(title, description, ...techList, ...reposList);

  return details;
}

/**
 * Get an iframe for the given path/url.
 * @param {String} src - The path/url to use as source.
 * @returns {HTMLElement} The iframe.
 */
function getIframe(src) {
  const iframe = document.createElement('iframe');
  iframe.src = src;
  return iframe;
}

/**
 * Retrieve the project preview.
 * @param {String} projectPath - The project path.
 * @returns {HTMLElement} The project preview wrapped in a div.
 */
function getProjectPreview(projectPath) {
  const preview = document.createElement('div');
  const iframe = getIframe(projectPath);
  preview.classList.add('project-preview', 'fade-in');
  preview.replaceChildren(iframe);
  return preview;
}

/**
 * Display the targeted project.
 * @param {String} id - The project id.
 */
function showProject(id) {
  const currentProject = getCurrentProject(id);
  const main = document.querySelector('.main');
  const details = getProjectDetails(currentProject);
  const preview = getProjectPreview(currentProject.path);
  const detailsBtn = document.querySelector('.btn--details');

  if (isSmallVw()) details.classList.add('hide');

  detailsBtn.textContent = translate('main.project.details.about', {
    name: currentProject.name,
  });
  detailsBtn.addEventListener('click', toggleProjectDetails);
  window.history.pushState({}, currentProject.name, `/#${id}`);
  document.title = `${currentProject.name} | Demo | Armand Philippot`;
  main.replaceChildren(preview, details);
}

/**
 * Add a CSS class to the current project in projects nav.
 * @param {String} id - The project id.
 */
function setSelectedProject(id) {
  const links = document.querySelectorAll('.nav__link');
  links.forEach((link) => {
    if (link.id === id) {
      link.classList.add('nav__link--selected');
    } else {
      link.classList.remove('nav__link--selected');
    }
  });
}

/**
 * Create a list item for a project.
 * @param {String} id - The project id.
 * @param {String} name - The project name.
 * @returns {HTMLElement} The list item.
 */
function getProjectsNavItem(id, name) {
  const item = document.createElement('li');
  const link = document.createElement('a');
  link.classList.add('nav__link');
  link.href = `/#${id}`;
  link.id = id;
  link.textContent = name;
  link.addEventListener('click', (e) => {
    e.preventDefault();
    showProject(id);
    setSelectedProject(id);
    toggleProjectDetailsBtn();
    if (isSmallVw()) toggleHeaderFooter();
  });
  item.classList.add('nav__item');
  item.appendChild(link);
  return item;
}

/**
 * Print the list of available projects.
 */
function printProjectsNav() {
  const ul = document.querySelector('.nav .nav__list');

  projects.forEach((project) => {
    const item = getProjectsNavItem(project.id, project.name);
    ul.appendChild(item);
  });
}

/**
 * Load corresponding project if the requested page contains a hash.
 */
function printRequestedPage() {
  const currentPath = window.location.hash;

  if (currentPath) {
    const id = currentPath.replace('#', '');
    showProject(id);
    setSelectedProject(id);
  }
}

/**
 * Replace the legal notice link and text.
 */
function replaceLegalNoticeLink() {
  const link = document.querySelector('.nav__link--legal');
  link.href = translate('footer.legalNotice.link');
  link.textContent = translate('footer.legalNotice.txt');
}

/**
 * Translate all text available in HTML templates.
 */
function translateHTMLContent() {
  const brandingDesc = document.querySelector('.branding__description');
  const navLabel = document.querySelector('.nav__label');
  const license = document.querySelector('.copyright__license');
  const instructions = document.querySelector('.instructions');
  brandingDesc.textContent = translate('branding.description');
  navLabel.textContent = translate('nav.title');
  license.title = translate('footer.license');
  if (instructions) instructions.textContent = translate('main.instructions');
}

/**
 * Translate the website according to the user preferred language.
 */
function setAppLocale() {
  const preferredLanguage = navigator.language;
  const supportedLanguage = supportedLanguages.find(
    (lang) => preferredLanguage.startsWith(lang.code)
    // eslint-disable-next-line function-paren-newline -- Conflict with Prettier
  );
  const locale = supportedLanguage?.code || 'en';
  setLocale(locale);
}

/**
 * Initialize the website with the projects list and Ackee.
 */
function init() {
  const ackee = createAckeeInstance();

  setAckeeRecord(ackee);
  setAppLocale();
  translateHTMLContent();
  replaceLegalNoticeLink();
  printProjectsNav();
  updateView();
  listenWindowSize();
  listenMenuBtn();
  printRequestedPage();
}

init();