@gouvfr/dsfr-roller 1.0.67 → 1.0.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gouvfr/dsfr-roller",
3
- "version": "1.0.67",
3
+ "version": "1.0.70",
4
4
  "description": "Le module `dsfr-roller` permet de publier le site de documentation du Système de Design de l’État - DSFR",
5
5
  "keywords": [
6
6
  "Système de Design de l'État",
@@ -56,7 +56,7 @@
56
56
  ],
57
57
  "main": "./index.js",
58
58
  "dependencies": {
59
- "@gouvfr/dsfr-forge": "=1.0.67",
59
+ "@gouvfr/dsfr-forge": "=1.0.70",
60
60
  "@gouvfr/dsfr-publisher": "npm:@gouvfr/dsfr@1.14.2",
61
61
  "deepmerge": "^4.3.1",
62
62
  "ejs": "^3.1.10",
@@ -0,0 +1,28 @@
1
+ import { Component } from '../component.js';
2
+
3
+ class Badge extends Component {
4
+ constructor (data) {
5
+ super(data, 'badge');
6
+ }
7
+
8
+ get ejsPath () {
9
+ return 'src/dsfr/component/badge/template/ejs/badge.ejs';
10
+ }
11
+
12
+ async format () {
13
+ const classes = ['fr-mb-6v'];
14
+ classes.push(...(this.data.classes || []));
15
+ return {
16
+ markup: this.data.markup || 'span',
17
+ size: this.data.size || 'sm',
18
+ label: this.data.label ?? this.data.text,
19
+ id: this.data.id,
20
+ classes: classes,
21
+ attributes: this.data.attributes,
22
+ type: this.data.type || 'info',
23
+ icon: this.data.icon,
24
+ }
25
+ }
26
+ }
27
+
28
+ export { Badge };
@@ -1,23 +1,7 @@
1
1
  import { Component } from '../component.js';
2
2
  import { formatLink } from '../../core/format-link.js';
3
3
  import { log } from '@gouvfr/dsfr-forge';
4
-
5
- const Badges = {
6
- BETA: {
7
- id: 'beta',
8
- type: 'info',
9
- icon: false,
10
- size: 'sm',
11
- },
12
- NEW: {
13
- id: 'new',
14
- type: 'new',
15
- icon: true,
16
- size: 'sm',
17
- }
18
- }
19
-
20
- const BadgesMap = new Map(Object.values(Badges).map(badge => [badge.id, badge]));
4
+ import { BadgesMap } from '../../page/body/badges-map.js';
21
5
 
22
6
  class Sidemenu extends Component {
23
7
  constructor (data, badgeLabels) {
@@ -0,0 +1,18 @@
1
+ import { Node } from '../../../node.js';
2
+ import { Badge } from '../../../../component/components/badge.js';
3
+
4
+ class BadgeLeafDirective extends Node {
5
+ constructor (data) {
6
+ super(data);
7
+ this.badge = new Badge({...this.data.properties, ...this.data.attributes});
8
+ }
9
+
10
+ async render () {
11
+ const data = { label: await this.renderChildren() };
12
+ return this.badge.render(data);
13
+ }
14
+ }
15
+
16
+ BadgeLeafDirective.NAME = 'fr-badge';
17
+
18
+ export { BadgeLeafDirective };
@@ -1,4 +1,5 @@
1
1
  import { Node } from '../../../node.js'
2
+ import { Badges } from '../../../../page/body/badges.js';
2
3
 
3
4
  class CardContainerDirective extends Node {
4
5
  structure (data) {
@@ -30,6 +31,7 @@ class CardContainerDirective extends Node {
30
31
  const contentDescription = contentChildren.find(child => child.type === 'paragraph');
31
32
  const hintStart = this.structureHintStart;
32
33
  const hintEnd = this.structureHintEnd;
34
+ const badges = this.structureBadges;
33
35
  const classes = ['fr-card'];
34
36
  switch (true) {
35
37
  case data.properties.enlargeLink === true:
@@ -87,7 +89,8 @@ class CardContainerDirective extends Node {
87
89
  ...image
88
90
  }
89
91
  ]
90
- }
92
+ },
93
+ badges
91
94
  ]
92
95
  } : {}
93
96
  ]
@@ -142,6 +145,13 @@ class CardContainerDirective extends Node {
142
145
  return structureHintStart;
143
146
  }
144
147
 
148
+ get structureBadges() {
149
+ let structureBadges = {};
150
+ if (this._data.badge) {
151
+ structureBadges = new Badges({ids: this._data.badge, resource: this._data.fragments.badge}).node;
152
+ }
153
+ return structureBadges;
154
+ }
145
155
  }
146
156
 
147
157
  CardContainerDirective.NAME = 'fr-card';
@@ -40,7 +40,8 @@ import { HpFaqContainerDirective } from './directive/home/hp-faq-container-direc
40
40
  import { HpAnalyticsContainerDirective } from './directive/home/hp-analytics-container-directive.js';
41
41
  import { AccordionContainerDirective } from './directive/components/accordion/accordion-container-directive.js';
42
42
  import { AccordionsGroupContainerDirective } from './directive/components/accordion/accordions-group-container-directive.js';
43
- import { ButtonLeafDirective } from './directive/components/button/button-leaf-directive.js'
43
+ import { ButtonLeafDirective } from './directive/components/button/button-leaf-directive.js';
44
+ import { BadgeLeafDirective } from './directive/components/badge/badge-leaf-directive.js';
44
45
  import { CardContainerDirective } from './directive/components/card/card-container-directive.js';
45
46
  import { TableContainerDirective } from './directive/components/table/table-container-directive.js';
46
47
  import { TabContainerDirective } from './directive/components/tabs/tab-container-directive.js'
@@ -123,6 +124,7 @@ const DIRECTIVE_CONTAINERS = [
123
124
  PreventPermalinksContainerDirective
124
125
  ];
125
126
  const DIRECTIVE_LEAFS = [
127
+ BadgeLeafDirective,
126
128
  ButtonLeafDirective,
127
129
  StorybookLeafDirective,
128
130
  FigmaLeafDirective,
@@ -0,0 +1,18 @@
1
+ const BadgeVariants = {
2
+ BETA: {
3
+ id: 'beta',
4
+ type: 'info',
5
+ icon: false,
6
+ size: 'sm',
7
+ },
8
+ NEW: {
9
+ id: 'new',
10
+ type: 'new',
11
+ icon: true,
12
+ size: 'sm',
13
+ }
14
+ };
15
+
16
+ const BadgesMap = new Map(Object.values(BadgeVariants).map(badge => [badge.id, badge]));
17
+
18
+ export { BadgesMap };
@@ -0,0 +1,70 @@
1
+ import { log } from '@gouvfr/dsfr-forge';
2
+ import { BadgesMap } from './badges-map.js';
3
+
4
+ class Badges {
5
+ constructor(data) {
6
+ let {ids, resource} = data;
7
+ if (typeof ids === 'string') ids = ids.split(',');
8
+ if (!Array.isArray(ids)) {
9
+ log.error(`Badge must be a string or an array of strings, received: ${typeof ids}`);
10
+ this._node = {};
11
+ return;
12
+ }
13
+ const badges = ids.map(id => this._formatBadge(id, resource));
14
+ if (badges.every(badge => Object.keys(badge).length === 0)) {
15
+ this._node = {};
16
+ return;
17
+ }
18
+ this._node = {
19
+ type: 'htmlContainer',
20
+ tagName: 'ul',
21
+ classes: ['fr-badges-group', 'fr-mb-2v'],
22
+ children: badges
23
+ };
24
+ }
25
+
26
+ _getIconClasses (icon) {
27
+ if (icon === false) {
28
+ return ['fr-badge--no-icon'];
29
+ }
30
+ if (icon === true) {
31
+ return [];
32
+ }
33
+ return [`fr-icon-${icon}`];
34
+ }
35
+
36
+ _formatBadge (id, resource) {
37
+ const badgeData = BadgesMap.get(id);
38
+ if (!badgeData) {
39
+ log.error(`Unknown badge type: ${id}`);
40
+ return {};
41
+ }
42
+ return {
43
+ type: 'htmlContainer',
44
+ tagName: 'li',
45
+ children: [
46
+ {
47
+ type: 'paragraph',
48
+ classes: [
49
+ 'fr-badge',
50
+ `fr-badge--${badgeData.type}`,
51
+ `fr-badge--${badgeData.size}`,
52
+ ...this._getIconClasses(badgeData.icon)
53
+ ],
54
+ children: [
55
+ {
56
+ type: 'text',
57
+ value: resource[id],
58
+ }
59
+ ]
60
+ }
61
+ ]
62
+ };
63
+ }
64
+
65
+ get node () {
66
+ return this._node;
67
+ }
68
+ }
69
+
70
+ export { Badges };
@@ -6,7 +6,8 @@ class Resource extends Renderable {
6
6
  meta: this._data.resource.meta,
7
7
  search: this._data.resource.search,
8
8
  pagination: this._data.resource.pagination,
9
- consent: this._data.resource.consent
9
+ consent: this._data.resource.consent,
10
+ badge: this._data.resource.badge
10
11
  };
11
12
  return `<script>
12
13
  window.resource = ${JSON.stringify(resource)};
package/src/page/page.js CHANGED
@@ -31,6 +31,7 @@ class Page extends Renderable {
31
31
  boost: this.data.boost,
32
32
  text: this._html.text,
33
33
  section: this.data.section,
34
+ badge: this.data.badge,
34
35
  }
35
36
  }
36
37
 
@@ -1,46 +1,74 @@
1
+ import { BadgesMap } from '../../../page/body/badges-map.js';
2
+
1
3
  class ResultCard {
2
- constructor(data) {
3
- this._title = data.title;
4
- this._url = data.url;
5
- this._desc = data.excerpt ?? data.summary;
6
- this._cover = data.cover || '/static/img/placeholder.16x9.png';
7
- this._section = data.section;
8
- }
9
-
10
- getTags() {
11
- if (!this._section) return '';
12
- return `<div class="fr-card__start">
13
- <ul class="fr-tags-group">
14
- <li>
15
- <p class="fr-tag">${this._section}</p>
16
- </li>
17
- </ul>
18
- </div>`;
19
- }
20
-
21
- getCover() {
22
- if (!this._cover) return '';
23
- return `<div class="fr-card__header">
24
- <div class="fr-card__img">
25
- <img src="${this._cover}" alt="" class="fr-responsive-img">
26
- </div>
27
- </div>`;
28
- }
29
-
30
- render() {
31
- return `<div class="fr-card fr-enlarge-link fr-card--horizontal fr-mb-8v">
32
- <div class="fr-card__body">
33
- <div class="fr-card__content">
34
- <h3 class="fr-card__title">
35
- <a href="${this._url}">${this._title}</a>
36
- </h3>
37
- ${this._desc ? `<p class="fr-card__desc">${this._desc}</p>` : ''}
38
- ${this.getTags()}
39
- </div>
40
- </div>
41
- ${this.getCover()}
42
- </div>`;
43
- }
4
+ constructor (data) {
5
+ this._title = data.title;
6
+ this._url = data.url;
7
+ this._desc = data.excerpt ?? data.summary;
8
+ this._cover = data.cover || '/static/img/placeholder.16x9.png';
9
+ this._section = data.section;
10
+ this._badge = data.badge || [];
11
+ }
12
+
13
+ getTags () {
14
+ if (!this._section) return '';
15
+ return `<div class="fr-card__start">
16
+ <ul class="fr-tags-group">
17
+ <li>
18
+ <p class="fr-tag">${this._section}</p>
19
+ </li>
20
+ </ul>
21
+ </div>`;
22
+ }
23
+
24
+ getBadges () {
25
+ if (!this._badge?.length) return '';
26
+
27
+ const badges = Array.isArray(this._badge) ? this._badge : this._badge.split(',').map(b => b.trim());
28
+ const badgeItems = badges
29
+ .map(badge => {
30
+ const badgeData = BadgesMap.get(badge);
31
+ if (!badgeData) return null;
32
+
33
+ const badgeLabel = window.resource?.badge?.[badge] || badge;
34
+ let iconClass = '';
35
+ if (badgeData.icon === false) {
36
+ iconClass = ' fr-badge--no-icon';
37
+ } else {
38
+ iconClass = ` fr-badge--icon-${badgeData.icon}`;
39
+ }
40
+ return `<li><span class="fr-badge fr-badge--${badgeData.type} fr-badge--${badgeData.size}${iconClass}">${badgeLabel}</span></li>`;
41
+ })
42
+ .filter(Boolean)
43
+ .join('');
44
+
45
+ return `<ul class="fr-badges-group fr-mb-2v">${badgeItems}</ul>`;
46
+ }
47
+
48
+ getCover () {
49
+ if (!this._cover) return '';
50
+ return `<div class="fr-card__header">
51
+ <div class="fr-card__img">
52
+ <img src="${this._cover}" alt="" class="fr-responsive-img">
53
+ </div>
54
+ ${this.getBadges()}
55
+ </div>`;
56
+ }
57
+
58
+ render () {
59
+ return `<div class="fr-card fr-enlarge-link fr-card--horizontal fr-mb-8v">
60
+ <div class="fr-card__body">
61
+ <div class="fr-card__content">
62
+ <h3 class="fr-card__title">
63
+ <a href="${this._url}">${this._title}</a>
64
+ </h3>
65
+ ${this._desc ? `<p class="fr-card__desc">${this._desc}</p>` : ''}
66
+ ${this.getTags()}
67
+ </div>
68
+ </div>
69
+ ${this.getCover()}
70
+ </div>`;
71
+ }
44
72
  }
45
73
 
46
74
  export { ResultCard };
@@ -7,39 +7,39 @@ import { Pagination } from '../../main/elements/pagination/index.js';
7
7
  const RESULTS_PER_PAGES = 10;
8
8
 
9
9
  class SearchPage extends Element {
10
- constructor(element) {
11
- super(element, 'searchPage');
12
-
13
- this._query = getQuery();
14
- this._searchBarInput = document.querySelector('#search-input');
15
- this._resultsCount = document.querySelector('#results-count');
16
- this._resultsList = document.querySelector('#results-cards');
17
- this._container = element.querySelector('#results-page--container');
18
- }
10
+ constructor(element) {
11
+ super(element, 'searchPage');
12
+
13
+ this._query = getQuery();
14
+ this._searchBarInput = document.querySelector('#search-input');
15
+ this._resultsCount = document.querySelector('#results-count');
16
+ this._resultsList = document.querySelector('#results-cards');
17
+ this._container = element.querySelector('#results-page--container');
18
+ }
19
19
 
20
- async init() {
21
- await window.searchEngine.init('searchPage');
20
+ async init() {
21
+ await window.searchEngine.init('searchPage');
22
22
 
23
- if (this._query) {
24
- this._searchBarInput.value = this._query;
25
- const results = window.searchEngine.search(this._query);
23
+ if (this._query) {
24
+ this._searchBarInput.value = this._query;
25
+ const results = window.searchEngine.search(this._query);
26
26
 
27
- const count = new ResultsCount(results.length);
28
- const resultsList = new ResultsList(results, RESULTS_PER_PAGES);
27
+ const count = new ResultsCount(results.length);
28
+ const resultsList = new ResultsList(results, RESULTS_PER_PAGES);
29
29
 
30
- this._resultsCount.innerHTML = count.render();
31
- document.title = `${document.title} - ${count.render()}`;
30
+ this._resultsCount.innerHTML = count.render();
31
+ document.title = `${document.title} - ${count.render()}`;
32
32
 
33
- this._container.appendChild(resultsList.element);
34
- resultsList.init();
33
+ this._container.appendChild(resultsList.element);
34
+ resultsList.init();
35
35
 
36
- if (results.length > RESULTS_PER_PAGES) {
37
- const pagination = new Pagination(results, RESULTS_PER_PAGES);
38
- this._container.appendChild(pagination.element);
39
- pagination.init();
40
- }
41
- }
36
+ if (results.length > RESULTS_PER_PAGES) {
37
+ const pagination = new Pagination(results, RESULTS_PER_PAGES);
38
+ this._container.appendChild(pagination.element);
39
+ pagination.init();
40
+ }
42
41
  }
42
+ }
43
43
  }
44
44
 
45
45
  export { SearchPage };
@@ -4,6 +4,7 @@ import { Breadcrumb } from '../../component/components/breadcrumb.js';
4
4
  import { TOC } from '../../page/body/toc.js';
5
5
  import { Edit } from '../../page/body/edit.js';
6
6
  import { Mesh } from '../../page/body/mesh.js';
7
+ import { Badges } from '../../page/body/badges.js';
7
8
 
8
9
  class EditorialTemplate extends Template {
9
10
  constructor (data) {
@@ -20,6 +21,12 @@ class EditorialTemplate extends Template {
20
21
  structure (data) {
21
22
  this._hasEditUrl = data?.editUrl != null;
22
23
  this._hasMesh = data?.mesh?.items?.length;
24
+ this._hasBadge = data?.badge != null;
25
+
26
+ if (this._hasBadge) {
27
+ const badges = new Badges({ids: data.badge, resource: data.resource.badge});
28
+ data.nodes.unshift(badges.node);
29
+ }
23
30
 
24
31
  if (this._hasEditUrl) {
25
32
  const edit = new Edit({...data.resource.edit, editUrl: data.editUrl, blankLabel: data.fragments.blank});