Skip to content

Commit 7374793

Browse files
authored
DEV: Migrate custom header links to new objects setting type (#53)
Why this change? Since discourse/discourse@a440e15, we have started to support objects typed theme setting so we are switching this theme component to use it instead as it provides a much better UX for configuring the settings required for the theme component.
1 parent 80f31b9 commit 7374793

File tree

8 files changed

+204
-32
lines changed

8 files changed

+204
-32
lines changed

.discourse-compatibility

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
< 3.3.0.beta2-dev: 0a837d7c044798417b1a7389cc37a67859c25dbe
12
< 3.3.0.beta1-dev: 50061254831a658eba821238a8a1ae0b7029ab09
23
< 3.2.0.beta2: 061adfe5ae20abbb82be711d373894c30987ec75
34
< 3.2.0.beta1-dev: c344d0f519bbb3660e306c50c0d1aa1a776c5e13

javascripts/discourse/components/custom-header-links.hbs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
>
1313
{{#each this.links as |link|}}
1414
<li
15-
class="headerLink
16-
{{link.device}}
17-
{{link.keepOnScrollClass}}
18-
{{link.locale}}
19-
{{link.linkClass}}
20-
{{link.keepOnScroll}}"
15+
class={{concat-class
16+
"headerLink"
17+
link.device
18+
link.locale
19+
link.linkClass
20+
link.hideOnScroll
21+
}}
2122
>
2223
<a
2324
title={{link.anchorAttributes.title}}

javascripts/discourse/components/custom-header-links.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@ export default class CustomHeaderLinks extends Component {
77
}
88

99
get links() {
10-
return settings.custom_header_links.split("|").reduce((result, item) => {
11-
let [
12-
linkText,
13-
linkTitle,
14-
linkHref,
15-
device,
16-
target = "",
17-
keepOnScroll,
18-
locale,
19-
] = item.split(",").map((s) => s.trim());
10+
return settings.custom_header_links.reduce((result, link) => {
11+
const linkText = link.text;
12+
const linkTitle = link.title;
13+
const linkHref = link.url;
14+
const target = link.target;
15+
const hideOnScroll = link.hide_on_scroll;
16+
const locale = link.locale;
17+
const device = link.view;
2018

2119
if (!linkText || (locale && document.documentElement.lang !== locale)) {
2220
return result;
@@ -32,8 +30,8 @@ export default class CustomHeaderLinks extends Component {
3230

3331
result.push({
3432
device: `headerLink--${device}`,
35-
keepOnScroll: `headerLink--${keepOnScroll}`,
36-
locale: `headerLink--${locale}`,
33+
hideOnScroll: `headerLink--${hideOnScroll}`,
34+
locale: locale ? `headerLink--${locale}` : null,
3735
linkClass,
3836
anchorAttributes,
3937
linkText,

locales/en.yml

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
11
en:
22
theme_metadata:
33
settings:
4-
custom_header_links: "Comma delimited in this order: link text, link title, URL, view, target, hide on scroll<br><b>Link text:</b> The text for the link<br><b>Link title:</b> the text that shows when the link is hovered<br><b>URL:</b> The path for the link (can be relative)<br><b>View:</b> vdm = desktop and mobile, vdo = desktop only, vmo = mobile only<br><b>Target:</b> blank = opens in a new tab, self = opens in the same tab<br><b>Hide on scroll:</b> remove = hides the link when the title is expanded on topic pages keep = keeps the link visible even when the title is visible on topic pages<br><b>Language:</b> blank = no locale assoaciated to the link, else insert a locale code (en, fr, de, ...)"
54
links_position: "Note that when links are displayed on the left, they're automatically hidden while scrolling within topics to make room for the title"
5+
custom_header_links:
6+
description: Custom links to be displayed in the header
7+
schema:
8+
properties:
9+
text:
10+
label: Text
11+
description: The text for the link
12+
title:
13+
label: Title
14+
description: The title attribute for the link
15+
url:
16+
label: URL
17+
description: The URL for the link
18+
view:
19+
label: View
20+
description: |
21+
vdm = desktop and mobile
22+
vdo = desktop only
23+
vmo = mobile only
24+
target:
25+
label: Target
26+
description: |
27+
blank = opens in a new tab
28+
self = opens in the same tab
29+
hide_on_scroll:
30+
label: Hide on scroll
31+
description: |
32+
remove = hides the link when the title is expanded on topic pages
33+
keep = keeps the link visible even when the title is visible on topic pages
34+
locale:
35+
label: Locale
36+
description: The locale in which the link should be displayed on. The link will be displayed on all locales if left blank.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export default function migrate(settings) {
2+
const oldSetting = settings.get("custom_header_links");
3+
4+
if (oldSetting) {
5+
const newSetting = oldSetting.split("|").map((link) => {
6+
const [text, title, url, view, target, hide_on_scroll, locale] = link
7+
.split(",")
8+
.map((s) => s.trim());
9+
10+
const newLink = {
11+
text,
12+
title,
13+
url,
14+
view,
15+
target,
16+
hide_on_scroll,
17+
locale,
18+
};
19+
20+
Object.keys(newLink).forEach((key) => {
21+
if (newLink[key] === undefined) {
22+
delete newLink[key];
23+
}
24+
});
25+
26+
return newLink;
27+
});
28+
29+
settings.set("custom_header_links", newSetting);
30+
}
31+
32+
return settings;
33+
}

settings.yml

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,65 @@
11
custom_header_links:
2-
type: list
3-
list_type: simple
4-
default: "External link, this link will open in a new tab, https://meta.discourse.org, vdo, blank, remove|Most Liked, Posts with the most amount of likes, /latest/?order=op_likes, vdo, self, keep|Privacy, Our Privacy Policy, /privacy, vdm, self, keep"
2+
type: objects
3+
default:
4+
- text: "External link"
5+
title: "This link will open in a new tab"
6+
url: "https://meta.discourse.org"
7+
view: "vdo"
8+
target: "blank"
9+
hide_on_scroll: "remove"
10+
- text: "Most Liked"
11+
title: "Posts with the most amount of likes"
12+
url: "/latest/?order=op_likes"
13+
view: "vdo"
14+
target: "self"
15+
hide_on_scroll: "keep"
16+
- text: "Privacy"
17+
title: "Our Privacy Policy"
18+
url: "/privacy"
19+
view: "vdm"
20+
target: "self"
21+
hide_on_scroll: "keep"
22+
schema:
23+
name: "link"
24+
properties:
25+
text:
26+
type: string
27+
required: true
28+
validations:
29+
min_length: 1
30+
max_length: 100
31+
title:
32+
type: string
33+
required: true
34+
validations:
35+
min_length: 1
36+
max_length: 1000
37+
url:
38+
type: string
39+
required: true
40+
validations:
41+
min_length: 1
42+
max_length: 2048
43+
url: true
44+
view:
45+
type: enum
46+
choices:
47+
- vdm
48+
- vdo
49+
- vmo
50+
target:
51+
type: enum
52+
choices:
53+
- blank
54+
- self
55+
hide_on_scroll:
56+
type: enum
57+
choices:
58+
- remove
59+
- keep
60+
default: keep
61+
locale:
62+
type: string
563

664
links_position:
765
default: right

spec/system/viewing_custom_header_links_spec.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
context "when glimmer headers are enabled" do
1010
before do
1111
if SiteSetting.respond_to?(:experimental_glimmer_header_groups)
12-
SiteSetting.experimental_glimmer_header_groups =
13-
Group::AUTO_GROUPS[:everyone]
12+
SiteSetting.experimental_glimmer_header_groups = Group::AUTO_GROUPS[:everyone]
1413
else
1514
SiteSetting.glimmer_header_mode = "enabled"
1615
end
@@ -24,28 +23,27 @@
2423
expect(custom_header_link).to have_custom_header_link(
2524
"External link",
2625
href: "https://meta.discourse.org",
27-
title: "this link will open in a new tab"
26+
title: "This link will open in a new tab",
2827
)
2928

3029
expect(custom_header_link).to have_custom_header_link(
3130
"Most Liked",
3231
href: "/latest/?order=op_likes",
33-
title: "Posts with the most amount of likes"
32+
title: "Posts with the most amount of likes",
3433
)
3534

3635
expect(custom_header_link).to have_custom_header_link(
3736
"Privacy",
3837
href: "/privacy",
39-
title: "Our Privacy Policy"
38+
title: "Our Privacy Policy",
4039
)
4140
end
4241
end
4342

4443
context "when glimmer headers are disabled" do
4544
before do
4645
if SiteSetting.respond_to?(:experimental_glimmer_header_groups)
47-
SiteSetting.experimental_glimmer_header_groups =
48-
nil
46+
SiteSetting.experimental_glimmer_header_groups = nil
4947
else
5048
SiteSetting.glimmer_header_mode = "disabled"
5149
end
@@ -59,19 +57,19 @@
5957
expect(custom_header_link).to have_custom_header_link(
6058
"External link",
6159
href: "https://meta.discourse.org",
62-
title: "this link will open in a new tab"
60+
title: "This link will open in a new tab",
6361
)
6462

6563
expect(custom_header_link).to have_custom_header_link(
6664
"Most Liked",
6765
href: "/latest/?order=op_likes",
68-
title: "Posts with the most amount of likes"
66+
title: "Posts with the most amount of likes",
6967
)
7068

7169
expect(custom_header_link).to have_custom_header_link(
7270
"Privacy",
7371
href: "/privacy",
74-
title: "Our Privacy Policy"
72+
title: "Our Privacy Policy",
7573
)
7674
end
7775
end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { module, test } from "qunit";
2+
import migrate from "../../../../migrations/settings/0002-migrate-custom-header-links";
3+
4+
module(
5+
"Unit | Migrations | Settings | 0002-migrate-custom-header-links",
6+
function () {
7+
test("migrate", function (assert) {
8+
const settings = new Map(
9+
Object.entries({
10+
custom_header_links:
11+
"External link, this link will open in a new tab, https://meta.discourse.org, vdo, blank, remove|Most Liked, Posts with the most amount of likes, /latest/?order=op_likes, vdo, self, keep|Privacy, Our Privacy Policy, /privacy, vdm, self, keep, en",
12+
})
13+
);
14+
15+
const result = migrate(settings);
16+
17+
const expectedResult = new Map(
18+
Object.entries({
19+
custom_header_links: [
20+
{
21+
text: "External link",
22+
title: "this link will open in a new tab",
23+
url: "https://meta.discourse.org",
24+
view: "vdo",
25+
target: "blank",
26+
hide_on_scroll: "remove",
27+
},
28+
{
29+
text: "Most Liked",
30+
title: "Posts with the most amount of likes",
31+
url: "/latest/?order=op_likes",
32+
view: "vdo",
33+
target: "self",
34+
hide_on_scroll: "keep",
35+
},
36+
{
37+
text: "Privacy",
38+
title: "Our Privacy Policy",
39+
url: "/privacy",
40+
view: "vdm",
41+
target: "self",
42+
hide_on_scroll: "keep",
43+
locale: "en",
44+
},
45+
],
46+
})
47+
);
48+
49+
assert.deepEqual(Array.from(result), Array.from(expectedResult));
50+
});
51+
}
52+
);

0 commit comments

Comments
 (0)