« 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: a997de0edf4a8368fb791f9b0967c3b5b2c4ac1f (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
export default {
  contentDependencies: [
    'generateAbsoluteDatetimestamp',
    'generateDatetimestampTemplate',
    'generateTooltip',
  ],

  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),

    tooltip:
      relation('generateTooltip'),
  }),

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

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

  generate(data, relations, slots, {language}) {
    if (data.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),

      tooltip:
        slots.tooltip &&
          relations.tooltip.slots({
            content:
              language.formatRelativeDate(data.currentDate, data.referenceDate, {
                considerRoundingDays: true,
                approximate: true,
                absolute: slots.style === 'year',
              }),
          }),

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