Skip to content

Commit 3d72148

Browse files
committed
Creation of patterns browser v2
1 parent 2f8abaf commit 3d72148

File tree

4 files changed

+244
-71
lines changed

4 files changed

+244
-71
lines changed

layouts/partials/menu-patterns-browser.html

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,7 @@ <h2 class="accordion-header" id="typeHeading">
2020
<div class="pf-c-accordion__expanded-content-body">
2121
<a href="/learn/about-pattern-tiers-types/">What do these tiers mean?</a>
2222
<div class="pf-c-select__menu-group">
23-
<fieldset class="pf-c-select__menu-fieldset" aria-labelledby="select-checkbox-expanded-selected-group-patterntype">
24-
<label class="pf-c-check pf-c-select__menu-item" for="type:maintained">
25-
<input class="pf-c-check__input" type="checkbox" onclick="filterSelection(this.id)" id="type:maintained" name="maintained"/>
26-
<span class="pf-c-check__label">Maintained</span>
27-
</label>
28-
<label class="pf-c-check pf-c-select__menu-item" for="type:tested">
29-
<input class="pf-c-check__input" type="checkbox" onclick="filterSelection(this.id)" id="type:tested" name="tested"/>
30-
<span class="pf-c-check__label">Tested</span>
31-
</label>
32-
<label class="pf-c-check pf-c-select__menu-item" for="type:sandbox">
33-
<input class="pf-c-check__input" type="checkbox" onclick="filterSelection(this.id)" id="type:sandbox" name="sandbox"/>
34-
<span class="pf-c-check__label">Sandbox</span>
35-
</label>
23+
<fieldset class="pf-c-select__menu-fieldset" aria-labelledby="select-checkbox-expanded-selected-group-patterntype" id="TiersItems">
3624
</fieldset>
3725
</div>
3826
</div>
@@ -56,13 +44,7 @@ <h2 class="accordion-header" id="industriesHeading">
5644
<div id="collapseIndustries" class="pf-c-accordion__expanded-content collapse" aria-labelledby="industriesHeading" data-bs-parent="#patternsAccordionNav">
5745
<div class="pf-c-accordion__expanded-content-body">
5846
<div class="pf-c-select__menu-group">
59-
<fieldset class="pf-c-select__menu-fieldset" aria-labelledby="select-checkbox-expanded-selected-group-industry">
60-
{{ range $name, $taxonomy := .Site.Taxonomies.industries }}
61-
<label class="pf-c-check pf-c-select__menu-item" for="industries:{{- $name | urlize -}}">
62-
<input class="pf-c-check__input" type="checkbox" id="industries:{{- $name | urlize -}}" onclick="filterSelection(this.id)" name="{{- $name | humanize | title -}}"/>
63-
<span class="pf-c-check__label">{{- $name | humanize | title -}}</span>
64-
</label>
65-
{{ end }}
47+
<fieldset class="pf-c-select__menu-fieldset" aria-labelledby="select-checkbox-expanded-selected-group-industry" id="IndustriesItems">
6648
</fieldset>
6749
</div>
6850
</div>
@@ -86,13 +68,7 @@ <h2 class="accordion-header" id="rhProductHeading">
8668
<div id="collapseRhProducts" class="pf-c-accordion__expanded-content collapse" aria-labelledby="productHeading" data-bs-parent="#patternsAccordionNav">
8769
<div class="pf-c-accordion__expanded-content-body">
8870
<div class="pf-c-select__menu-group">
89-
<fieldset class="pf-c-select__menu-fieldset" aria-labelledby="select-checkbox-expanded-selected-group-rh-products">
90-
{{ range $name, $taxonomy := .Site.Taxonomies.rh_products }}
91-
<label class="pf-c-check pf-c-select__menu-item" for="rh_products:{{- $name | urlize -}}">
92-
<input class="pf-c-check__input" type="checkbox" id="rh_products:{{- $name | urlize -}}" onclick="filterSelection(this.id)" name="{{ $taxonomy.Page.Title }}"/>
93-
<span class="pf-c-check__label wrappable">{{ $taxonomy.Page.Title }}</span>
94-
</label>
95-
{{ end }}
71+
<fieldset class="pf-c-select__menu-fieldset" aria-labelledby="select-checkbox-expanded-selected-group-rh-products" id="RhProductsItems">
9672
</fieldset>
9773
</div>
9874
</div>
@@ -116,13 +92,7 @@ <h2 class="accordion-header" id="otherProductHeading">
11692
<div id="collapseOtherProducts" class="pf-c-accordion__expanded-content collapse" aria-labelledby="otherProductHeading" data-bs-parent="#patternsAccordionNav">
11793
<div class="pf-c-accordion__expanded-content-body">
11894
<div class="pf-c-select__menu-group">
119-
<fieldset class="pf-c-select__menu-fieldset" aria-labelledby="select-checkbox-expanded-selected-group-RHproducts">
120-
{{ range $name, $taxonomy := .Site.Taxonomies.other_products }}
121-
<label class="pf-c-check pf-c-select__menu-item" for="other_products:{{- $name | urlize -}}">
122-
<input class="pf-c-check__input" type="checkbox" id="other_products:{{- $name | urlize -}}" onclick="filterSelection(this.id)" name="{{ $taxonomy.Page.Title }}"/>
123-
<span class="pf-c-check__label wrappable">{{ $taxonomy.Page.Title }}</span>
124-
</label>
125-
{{ end }}
95+
<fieldset class="pf-c-select__menu-fieldset" aria-labelledby="select-checkbox-expanded-selected-group-otherproducts" id="OtherProductsItems">
12696
</fieldset>
12797
</div>
12898
</div>

layouts/partials/patterns-browser.html

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,45 +25,34 @@ <h1 class="pf-c-title pf-m-4xl">
2525
{{ $patterns := where $.Pages "Section" "patterns"}}
2626
<div class="pf-l-stack pf-m-gutter">
2727
<div class="pf-l-stack__item pf-u-text-align-right pattern-count-style">
28-
<span id="pattern-count">{{- len $patterns -}}</span> of {{ len $patterns }} patterns displayed
28+
<label class="pf-c-form__label" for="form-vertical-name">
29+
<span class="pf-c-form__label-text">Sort:</span>
30+
</label>
31+
<select
32+
class
33+
id="select-pattern-sort"
34+
name="select-pattern-sort"
35+
aria-label="Selector for pattern sort"
36+
onchange="filterSelection()"
37+
>
38+
<option value="atoz" selected>A to Z</option>
39+
<option value="ztoa">Z to A</option>
40+
<option value="newest">Newest</option>
41+
<option value="oldest">Oldest</option>
42+
</select>
43+
</div>
44+
<div class="pf-l-stack__item pf-u-text-align-right pattern-count-style" id="pattern-counter">
2945
</div>
3046
<div class="pf-l-stack__item">
31-
<div class="pf-l-gallery pf-m-gutter" style="--pf-l-gallery--GridTemplateColumns--min: 300px;">
32-
{{ range $pattern := $patterns.ByTitle }}
33-
{{ $data := newScratch }}
34-
{{ $data.Set "tags" slice }}
35-
{{ $params := .Params }}
36-
{{ range $taxonomyname, $taxonomy := .Site.Taxonomies }}
37-
38-
{{ if isset $params $taxonomyname }}
39-
{{ range $term := $pattern.Param $taxonomyname }}
40-
{{ $taglist := $data.Get "tags" }}
41-
{{ $taglist := $taglist | append (string (printf "%s:%s" $taxonomyname ($term | urlize) | printf "%s" )) }}
42-
{{ $data.Set "tags" $taglist }}
43-
{{ end }}
47+
<div class="pf-l-gallery pf-m-gutter" style="--pf-l-gallery--GridTemplateColumns--min: 300px;" id="PatternCards">
4448

45-
{{ end }}
46-
{{ end }}
47-
{{ $taglist := $data.Get "tags" }}
48-
{{ $validated := .Param "tier" }}
49-
{{ if (eq $validated "maintained" ) }}
50-
{{ .Scratch.Set "patterntype" "type:maintained" }}
51-
{{ else if (eq $validated "tested" ) }}
52-
{{ .Scratch.Set "patterntype" "type:tested" }}
53-
{{ else }}
54-
{{ .Scratch.Set "patterntype" "type:sandbox" }}
55-
{{ end }}
56-
{{ $patterntype := .Scratch.Get "patterntype" }}
57-
<div class="pf-l-gallery__item filterDiv" tag-data="{{ delimit $taglist " " }} {{ $patterntype }}" style="display: grid;">
58-
{{ .Render "summary-with-image" }}
59-
</div>
60-
{{ end }}
6149
</div>
6250
</div>
6351
</div>
6452
</div>
6553
</div>
66-
<script src="{{ "js/filter.js" | relURL }}"></script>
54+
<div id="debug"></div>
55+
<script src="{{ "js/patterns-browser-v2.js" | relURL }}"></script>
6756
</section>
6857
{{ partial "footer.html" . }}
6958
</main>

layouts/patterns/list.json.json

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
11
{{- if (eq .RelPermalink "/patterns/") }}
2+
3+
{{/* Patterns */}}
24
{{- $patterns := where $.Pages "Section" "patterns"}}
3-
{{- $patterns_len := $patterns | len }}
4-
{{- $pattern_list := slice }}
5-
{{- range $index, $pattern := $patterns.ByTitle }}
6-
{{- $pattern_list = $pattern_list | append (dict $pattern.Title $pattern.Params) }}
7-
{{- end }}
8-
{{- $pattern_list | jsonify }}
5+
{{- $pattern_list := slice }}
6+
{{- range $index, $pattern := $patterns.ByTitle }}
7+
{{- $object := dict "Name" $pattern.LinkTitle "Link" $pattern.RelPermalink "Params" $pattern.Params -}}
8+
{{- $pattern_list = $pattern_list | append $object -}}
9+
{{- end }}
10+
11+
{{/* Red Hat Products */}}
12+
{{- $rh_products := .Site.Taxonomies.rh_products }}
13+
{{- $rh_products_list := slice }}
14+
{{- range $index, $rh_product := $rh_products.Alphabetical }}
15+
{{- $rh_products_list = $rh_products_list | append (dict "Name" $rh_product.Name "LinkTitle" $rh_product.Page.LinkTitle) }}
16+
{{- end }}
17+
18+
{{/* Other Products */}}
19+
{{- $other_products := .Site.Taxonomies.other_products }}
20+
{{- $other_products_list := slice }}
21+
{{- range $index, $other_product := $other_products.Alphabetical }}
22+
{{- $other_products_list = $other_products_list | append (dict "Name" $other_product.Name "LinkTitle" $other_product.Page.LinkTitle) }}
23+
{{- end }}
24+
25+
{{/* Industries */}}
26+
{{- $industries := .Site.Taxonomies.industries }}
27+
{{- $industries_list := slice }}
28+
{{- range $index, $industry := $industries.Alphabetical }}
29+
{{- $industries_list = $industries_list | append (dict "Name" $industry.Name "LinkTitle" $industry.Page.LinkTitle) }}
30+
{{- end }}
31+
32+
{{/* Tiers */}}
33+
{{- $tiers_list := slice -}}
34+
{{- $tiers_list = $tiers_list | append (dict "Name" "maintained" "LinkTitle" "Maintained") }}
35+
{{- $tiers_list = $tiers_list | append (dict "Name" "tested" "LinkTitle" "Tested") }}
36+
{{- $tiers_list = $tiers_list | append (dict "Name" "sandbox" "LinkTitle" "Sandbox") }}
37+
38+
{{- $filter_categories := dict "rh_products" $rh_products_list "other_products" $other_products_list "industries" $industries_list "tier" $tiers_list -}}
39+
{{- dict "patterns" $pattern_list "filter_categories" $filter_categories | jsonify }}
940
{{- end }}

static/js/patterns-browser-v2.js

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
async function getData() {
2+
const url = "/patterns/index.json";
3+
try {
4+
const response = await fetch(url);
5+
if (!response.ok) {
6+
throw new Error(`Response status: ${response.status}`);
7+
}
8+
const json = await response.json();
9+
return json;
10+
} catch (error) {
11+
console.error(error.message);
12+
}
13+
}
14+
15+
function cleanString(string) {
16+
return string.replace(/ /g, "-")
17+
}
18+
19+
function capitalizeFirstLetter(string) {
20+
return string[0].toUpperCase() + string.slice(1);
21+
}
22+
23+
function sortAtoZ(a, b){
24+
const nameA = a.Name.toUpperCase()
25+
const nameB = b.Name.toUpperCase()
26+
if (nameA < nameB) {
27+
return -1;
28+
}
29+
if (nameA > nameB) {
30+
return 1;
31+
}
32+
return 0;
33+
}
34+
35+
function sortDate(a, b){
36+
if (a.Params.date != undefined) {
37+
var dateA = new Date(a.Params.date)
38+
} else {
39+
var dateA = new Date("2000-01-01T00:00:00Z");
40+
};
41+
if (b.Params.date != undefined) {
42+
var dateB = new Date(b.Params.date)
43+
} else {
44+
var dateB = new Date("2000-01-01T00:00:00Z");
45+
};
46+
if (dateA < dateB) {
47+
return -1;
48+
}
49+
if (dateA > dateB) {
50+
return 1;
51+
}
52+
return 0;
53+
}
54+
55+
function renderFilterItem(type, name, linkTitle) {
56+
var filterItem = '<label class="pf-c-check pf-c-select__menu-item" for="' + type + ':' + cleanString(name) + '">' +
57+
'<input class="pf-c-check__input" type="checkbox" id="' + type + ':' + cleanString(name) + '"' + 'onclick="filterSelection()" name="' + linkTitle + '"/>' +
58+
'<span class="pf-c-check__label wrappable">' + linkTitle + '</span>'+
59+
'</label>';
60+
return filterItem;
61+
}
62+
63+
function renderFilter(elementId, filterType, filterData) {
64+
for (item = 0; item < filterData.length; item++) {
65+
const element = document.getElementById(elementId);
66+
element.innerHTML += renderFilterItem(filterType, filterData[item].Name, filterData[item].LinkTitle);
67+
};
68+
}
69+
70+
function renderLabel(tier) {
71+
if (tier != undefined) {
72+
if (tier == "maintained") { var color = "green" }
73+
else if (tier == "tested") { var color = "blue" }
74+
else if (tier == "sandbox") { var color = "orange" };
75+
var renderedLabel = '<span class="pf-c-label pf-m-' + color +'">' +
76+
'<span class="pf-c-label__content">' +
77+
'<img src="/images/pattern-tier-' + tier + '.png" alt="' + capitalizeFirstLetter(tier) + '" width="16" height="16" class="custom-pattern-icon"/>' +
78+
capitalizeFirstLetter(tier) +
79+
'</span>' +
80+
'</span>';
81+
return renderedLabel;
82+
} else {
83+
return "";
84+
};
85+
}
86+
87+
function renderCard(pattern) {
88+
var renderCard = '<div class="pf-l-gallery__item" style="display: grid;">' +
89+
'<div class="pf-c-card" style="text-align: left; --pf-c-card__title--FontSize: 1rem; --pf-c-card__body--FontSize: 0.95rem;">' +
90+
'<div class="pf-c-card__title">' +
91+
'<a href="' + pattern.Link +'">' +
92+
pattern.Name +
93+
'</a>' +
94+
'</div>' +
95+
'<div class="pf-c-card__body">' +
96+
pattern.Params.summary +
97+
'</div>' +
98+
'<div class="pf-c-card__footer">' +
99+
renderLabel(pattern.Params.tier) +
100+
'</div>' +
101+
'</div>' +
102+
'</div>';
103+
return renderCard;
104+
}
105+
106+
function renderFilteredCards(patterns, filter_categories) {
107+
const element = document.getElementById("PatternCards");
108+
element.innerHTML = "";
109+
var filter = new Object();
110+
var filteredPatterns = [];
111+
var sortValue = document.getElementById("select-pattern-sort");
112+
for (const [category, terms] of Object.entries(filter_categories)) {
113+
for (item = 0; item < terms.length; item++) {
114+
var checkboxId = category + ":" + cleanString(terms[item].Name);
115+
var checkbox = document.getElementById(checkboxId);
116+
if (checkbox.checked) {
117+
if (filter[category] == undefined) { filter[category] = [] };
118+
filter[category].push(terms[item].LinkTitle);
119+
};
120+
};
121+
};
122+
for (item = 0; item < patterns.length; item++) {
123+
var checksPassed = new Object();
124+
for (const [category, terms] of Object.entries(filter)) {
125+
if (typeof(patterns[item].Params[category]) == "string") {
126+
for (termId = 0; termId < terms.length; termId++) {
127+
if (terms[termId].toLowerCase() == patterns[item].Params[category].toLowerCase()) {
128+
checksPassed[category] = true;
129+
}
130+
}
131+
} else if (typeof(patterns[item].Params[category]) == "object" && patterns[item].Params[category] != null) {
132+
var patternTerms = patterns[item].Params[category].map(v => v.toLowerCase());
133+
var filterTerms = terms.map(v => v.toLowerCase());
134+
if(filterTerms.every(r => patternTerms.includes(r))) {
135+
checksPassed[category] = true;
136+
};
137+
};
138+
};
139+
var patternPassed = true;
140+
for (const [category, terms] of Object.entries(filter)) {
141+
if (checksPassed[category] != true) {
142+
patternPassed = false;
143+
};
144+
};
145+
if (patternPassed == true) { filteredPatterns.push(patterns[item]); };
146+
};
147+
if (sortValue.value == "atoz") {
148+
filteredPatterns.sort(sortAtoZ);
149+
} else if (sortValue.value == "ztoa") {
150+
filteredPatterns.sort(sortAtoZ);
151+
filteredPatterns.reverse();
152+
} else if (sortValue.value == "oldest") {
153+
filteredPatterns.sort(sortDate);
154+
} else if (sortValue.value == "newest") {
155+
filteredPatterns.sort(sortDate);
156+
filteredPatterns.reverse();
157+
};
158+
for (item = 0; item < filteredPatterns.length; item++) {
159+
const element = document.getElementById("PatternCards");
160+
element.innerHTML += renderCard(filteredPatterns[item]);
161+
};
162+
var totalPatternsCount = patterns.length
163+
var filteredPatternsCount = filteredPatterns.length
164+
const counter = document.getElementById("pattern-counter");
165+
counter.innerHTML = filteredPatternsCount + " of " + totalPatternsCount + " patterns displayed";
166+
}
167+
168+
function filterSelection(filter) {
169+
// Declaring variables
170+
const patternsData = getData()
171+
patternsData.then(output => {
172+
renderFilteredCards(output.patterns, output.filter_categories)
173+
});
174+
}
175+
176+
const patternsData = getData()
177+
patternsData.then(output => {
178+
renderFilter("TiersItems", "tier", output.filter_categories.tier);
179+
renderFilter("IndustriesItems", "industries", output.filter_categories.industries);
180+
renderFilter("RhProductsItems", "rh_products", output.filter_categories.rh_products);
181+
renderFilter("OtherProductsItems", "other_products", output.filter_categories.other_products);
182+
renderFilteredCards(output.patterns, output.filter_categories)
183+
});

0 commit comments

Comments
 (0)