Skip to content

Commit 848b3f8

Browse files
authored
fix: errors with dom manipulation on tags page (#156)
* Replace "knowledge base" references * Safer way to remove blog tags from tag overview page * Add `maxDepth` to recursive function
1 parent e09de19 commit 848b3f8

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

js/src/admin/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ app.initializers.add('v17development-flarum-blog', () => {
3939

4040
// Add addPermissions
4141
extend(PermissionGrid.prototype, 'permissionItems', function (items) {
42-
// Add knowledge base permissions
42+
// Add blog permissions
4343
items.add(
4444
'blog',
4545
{

js/src/forum/pages/BlogComposer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,12 @@ export default class BlogComposer extends Page {
256256
return;
257257
}
258258

259-
// Find knowledge base tags
259+
// Find blog tags
260260
const findblogTags = this.tags.filter((tag) => {
261261
return blogTags.indexOf(tag.id()) >= 0;
262262
});
263263

264-
// No knowledge base tags selected
264+
// No blog tags selected
265265
if (findblogTags.length === 0) {
266266
alert(app.translator.trans('v17development-flarum-blog.forum.composer.no_blog_tags_selected'));
267267
return;

js/src/forum/utils/extendTagOverview.js

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ export default function extendTagOverview() {
1111

1212
if (app.forum.attribute('blogHideTags') == false) return markup;
1313

14-
// Get knowledge base tag ID's
15-
const knowledgeBaseTags = app.forum.attribute('blogTags') || [];
14+
// Get blog tag ID's
15+
const blogTags = app.forum.attribute('blogTags') || [];
16+
17+
const tag_tiles_parent = findChild(markup, 'TagsPage-content', true);
18+
const tag_tiles = tag_tiles_parent?.children[0];
1619

17-
// Get tiles
18-
let tag_tiles = markup.children[1].children[1].children[0].children;
20+
if (!tag_tiles_parent || !tag_tiles) return markup;
1921

20-
// Map through the tiles and remove tiles that are part of the knowledge base
21-
markup.children[1].children[1].children[0].children = tag_tiles.map((tile, i) => {
22-
return knowledgeBaseTags.indexOf(this.tags[i].id()) >= 0 ? null : tile;
22+
// Map through the tiles and remove tiles that are part of the blog
23+
tag_tiles.children = tag_tiles.children.map((tile, i) => {
24+
return blogTags.indexOf(this.tags[i].id()) >= 0 ? null : tile;
2325
});
2426

2527
return markup;
@@ -37,3 +39,39 @@ export default function extendTagOverview() {
3739
return items;
3840
});
3941
}
42+
43+
function findChild(parent, childClass, recursive = false, maxDepth = 50, depth = 0) {
44+
const children = getChildren(parent);
45+
let child = null;
46+
47+
for (let i = 0; i < children.length; i++) {
48+
const childClassName = children[i]?.attrs?.className || '';
49+
if (childClassName.includes(childClass)) {
50+
child = children[i];
51+
break;
52+
}
53+
}
54+
55+
// Recursive search
56+
if (recursive && !child && depth < maxDepth) {
57+
for (let subParent of children) {
58+
const subChild = findChild(subParent, childClass, true, maxDepth, depth + 1);
59+
if (subChild) {
60+
return subChild;
61+
}
62+
}
63+
}
64+
65+
return child;
66+
}
67+
68+
function getChildren(parent) {
69+
if (Array.isArray(parent)) {
70+
return parent;
71+
}
72+
const children = parent?.children || [];
73+
if (!Array.isArray(children)) {
74+
return [];
75+
}
76+
return children;
77+
}

0 commit comments

Comments
 (0)