« get me outta code hell

generateRelativeDatetimestamp.js « dependencies « content « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content/dependencies/generateRelativeDatetimestamp.js
blob: f0417594d76f1432a8c5ab178806736ab01da3df (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
export default {
  contentDependencies: [
    'generateAbsoluteDatetimestamp',
    'generateDatetimestampTemplate',
  ],

  extraDependencies: ['html', 'language'],

  data: (currentDate, referenceDate) =>
    (currentDate.getTime() === referenceDate.getTime()
      ? {equal: true, date: currentDate}
      : {equal: false, currentDate, referenceDate}),

  relations: (relation, currentDate) =>
    ({template: relation('generateDatetimestampTemplate'),
      fallback: relation('generateAbsoluteDatetimestamp', currentDate)}),

  slots: {
    style: {
      validate: v => v.is('full', 'year'),
      default: 'full',
    },

    tooltip: {
      type: 'boolean',
      default: false,
    },
  },

  generate(data, relations, slots, {language}) {
    if (data.comparison === 'equal') {
      return relations.fallback.slots({
        style: slots.style,
        tooltip: slots.tooltip,
      });
    }

    return relations.template.slots({
      mainContent:
        (slots.style === 'full'
          ? language.formatDate(data.currentDate)
       : slots.style === 'year'
          ? data.currentDate.getFullYear().toString()
          : null),

      tooltipContent:
        slots.tooltip &&
          language.formatRelativeDate(data.currentDate, data.referenceDate, {
            considerRoundingDays: true,
            approximate: true,
            absolute: true,
          }),

      datetime:
        data.currentDate.toISOString(),
    });
  },
};