| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | |||
| 6 | 6 | ||
| 7 | 7 | ## [Unreleased] | |
| 8 | 8 | ||
| 9 | + ### Added | ||
| 10 | + | ||
| 11 | + - Add [`Ordered`](https://patternfly-java.github.io/patternfly-java/org/patternfly/component/Ordered.html) interface and implementations for `List`, and `DataList`. | ||
| 12 | + | ||
| 9 | 13 | ## [0.4.16] - 2026-02-05 | |
| 10 | 14 | ||
| 11 | 15 | ### Added | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,99 @@ | |||
| 1 | + /* | ||
| 2 | + * Copyright 2023 Red Hat | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | + package org.patternfly.component; | ||
| 17 | + | ||
| 18 | + import java.util.Comparator; | ||
| 19 | + import java.util.TreeSet; | ||
| 20 | + | ||
| 21 | + import org.jboss.elemento.ElementContainerMethods; | ||
| 22 | + import org.jboss.elemento.IsElement; | ||
| 23 | + import org.jboss.elemento.TypedBuilder; | ||
| 24 | + import elemental2.dom.Element; | ||
| 25 | + import elemental2.dom.HTMLElement; | ||
| 26 | + | ||
| 27 | + import static java.util.Comparator.comparing; | ||
| 28 | + import static org.jboss.elemento.Elements.insertBefore; | ||
| 29 | + | ||
| 30 | + /** | ||
| 31 | + * The Ordered interface can be implemented by components that maintain its elements in a specific order. Typically, the | ||
| 32 | + * components also implements {@link HasItems}. By default, the order is determined by the {@link #defaultOrder()} method which | ||
| 33 | + * in turn determines the order based on the elements data {@value #DATA_ORDER} attribute. The order is maintained as elements | ||
| 34 | + * are added to the component. | ||
| 35 | + * | ||
| 36 | + * @param <E> the type of the main element | ||
| 37 | + * @param <B> the type of the builder for chaining methods | ||
| 38 | + * @param <T> the type of components being ordered | ||
| 39 | + */ | ||
| 40 | + public interface Ordered<E extends Element, B extends TypedBuilder<E, B>, T extends IsElement<? extends HTMLElement>> | ||
| 41 | + extends TypedBuilder<E, B>, IsElement<E> { | ||
| 42 | + | ||
| 43 | + String DATA_ORDER = "order"; | ||
| 44 | + | ||
| 45 | + /** | ||
| 46 | + * Adds an item to a container in a specific order determined by the provided comparator. The item's position is calculated | ||
| 47 | + * relative to the existing items in the container. | ||
| 48 | + * | ||
| 49 | + * @param hasItems the source of the current items to determine the insertion position | ||
| 50 | + * @param container the container to which the item will be added | ||
| 51 | + * @param item the item to be added to the container | ||
| 52 | + * @param comparator the comparator used to determine the order of the items | ||
| 53 | + */ | ||
| 54 | + default void addOrdered(HasItems<E, B, T> hasItems, ElementContainerMethods<E, B> container, T item, | ||
| 55 | + Comparator<T> comparator) { | ||
| 56 | + HTMLElement elementBefore = null; | ||
| 57 | + TreeSet<T> orderedItems = new TreeSet<>(comparator); | ||
| 58 | + orderedItems.addAll(hasItems.items()); | ||
| 59 | + for (T existing : orderedItems) { | ||
| 60 | + if (comparator.compare(item, existing) < 0) { | ||
| 61 | + elementBefore = existing.element(); | ||
| 62 | + break; | ||
| 63 | + } | ||
| 64 | + } | ||
| 65 | + if (elementBefore != null) { | ||
| 66 | + insertBefore(item.element(), elementBefore); | ||
| 67 | + } else { | ||
| 68 | + container.add(item.element()); | ||
| 69 | + } | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + /** | ||
| 73 | + * Provides a default comparator for ordering elements based on their {@value #DATA_ORDER} dataset value. | ||
| 74 | + * | ||
| 75 | + * @return a comparator that compares elements using the {@value #DATA_ORDER} attribute from their dataset | ||
| 76 | + */ | ||
| 77 | + default Comparator<T> defaultOrder() { | ||
| 78 | + return comparing(o -> o.element().dataset.get(DATA_ORDER)); | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + /** | ||
| 82 | + * Orders the elements contained in the component using the default comparator. The default comparator is based on the | ||
| 83 | + * {@value #DATA_ORDER} dataset attribute of the elements. | ||
| 84 | + * | ||
| 85 | + * @return the builder instance with the elements ordered | ||
| 86 | + */ | ||
| 87 | + default B ordered() { | ||
| 88 | + return ordered(defaultOrder()); | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + /** | ||
| 92 | + * Orders the elements contained in the component using the specified comparator. The comparator determines the order in | ||
| 93 | + * which the elements are arranged. | ||
| 94 | + * | ||
| 95 | + * @param comparator the comparator used to define the ordering of the elements | ||
| 96 | + * @return the builder instance with the elements ordered | ||
| 97 | + */ | ||
| 98 | + B ordered(Comparator<T> comparator); | ||
| 99 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,6 +16,7 @@ | |||
| 16 | 16 | package org.patternfly.component.list; | |
| 17 | 17 | ||
| 18 | 18 | import java.util.ArrayList; | |
| 19 | + import java.util.Comparator; | ||
| 19 | 20 | import java.util.Iterator; | |
| 20 | 21 | import java.util.LinkedHashMap; | |
| 21 | 22 | import java.util.List; | |
@@ -25,6 +26,7 @@ | |||
| 25 | 26 | import org.patternfly.component.BaseComponent; | |
| 26 | 27 | import org.patternfly.component.ComponentType; | |
| 27 | 28 | import org.patternfly.component.HasItems; | |
| 29 | + import org.patternfly.component.Ordered; | ||
| 28 | 30 | import org.patternfly.component.table.Wrap; | |
| 29 | 31 | import org.patternfly.style.GridBreakpoint; | |
| 30 | 32 | import org.patternfly.style.Modifiers.Compact; | |
@@ -59,7 +61,8 @@ | |||
| 59 | 61 | */ | |
| 60 | 62 | public class DataList extends BaseComponent<HTMLUListElement, DataList> implements | |
| 61 | 63 | Compact<HTMLUListElement, DataList>, | |
| 62 | - HasItems<HTMLUListElement, DataList, DataListItem> { | ||
| 64 | + HasItems<HTMLUListElement, DataList, DataListItem>, | ||
| 65 | + Ordered<HTMLUListElement, DataList, DataListItem> { | ||
| 63 | 66 | ||
| 64 | 67 | // ------------------------------------------------------ factory | |
| 65 | 68 | ||
@@ -72,6 +75,7 @@ public static DataList dataList() { | |||
| 72 | 75 | private final Map<String, DataListItem> items; | |
| 73 | 76 | private final List<BiConsumer<DataList, DataListItem>> onAdd; | |
| 74 | 77 | private final List<BiConsumer<DataList, DataListItem>> onRemove; | |
| 78 | + private Comparator<DataListItem> comparator; | ||
| 75 | 79 | ||
| 76 | 80 | DataList() { | |
| 77 | 81 | super(ComponentType.DataList, ul().css(component(dataList)) | |
@@ -87,10 +91,14 @@ public static DataList dataList() { | |||
| 87 | 91 | ||
| 88 | 92 | @Override | |
| 89 | 93 | public DataList add(DataListItem item) { | |
| 94 | + if (comparator != null) { | ||
| 95 | + addOrdered(this, this, item, comparator); | ||
| 96 | + } else { | ||
| 97 | + add(item.element()); | ||
| 98 | + } | ||
| 90 | 99 | items.put(item.identifier(), item); | |
| 91 | - DataList result = add(item.element()); | ||
| 92 | 100 | onAdd.forEach(bc -> bc.accept(this, item)); | |
| 93 | - return result; | ||
| 101 | + return this; | ||
| 94 | 102 | } | |
| 95 | 103 | ||
| 96 | 104 | // ------------------------------------------------------ builder | |
@@ -103,6 +111,12 @@ public DataList gridBreakpoint(GridBreakpoint breakpoint) { | |||
| 103 | 111 | return this; | |
| 104 | 112 | } | |
| 105 | 113 | ||
| 114 | + @Override | ||
| 115 | + public DataList ordered(Comparator<DataListItem> comparator) { | ||
| 116 | + this.comparator = comparator; | ||
| 117 | + return this; | ||
| 118 | + } | ||
| 119 | + | ||
| 106 | 120 | /** Determines which wrapping modifier to apply to the data list */ | |
| 107 | 121 | public DataList wrap(Wrap wrap) { | |
| 108 | 122 | if (verifyEnum(element(), "wrap", wrap, nowrap, truncate, breakWord)) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,16 +15,17 @@ | |||
| 15 | 15 | */ | |
| 16 | 16 | package org.patternfly.component.list; | |
| 17 | 17 | ||
| 18 | + import java.util.Comparator; | ||
| 18 | 19 | import java.util.Iterator; | |
| 19 | 20 | import java.util.LinkedHashMap; | |
| 20 | 21 | import java.util.Map; | |
| 21 | 22 | import java.util.function.BiConsumer; | |
| 22 | - import java.util.function.Function; | ||
| 23 | 23 | ||
| 24 | 24 | import org.jboss.elemento.HTMLContainerBuilder; | |
| 25 | 25 | import org.patternfly.component.BaseComponent; | |
| 26 | 26 | import org.patternfly.component.ComponentType; | |
| 27 | 27 | import org.patternfly.component.HasItems; | |
| 28 | + import org.patternfly.component.Ordered; | ||
| 28 | 29 | import org.patternfly.core.Roles; | |
| 29 | 30 | import org.patternfly.style.Classes; | |
| 30 | 31 | import org.patternfly.style.Modifiers.Bordered; | |
@@ -53,6 +54,7 @@ public class List extends BaseComponent<HTMLElement, List> implements | |||
| 53 | 54 | Bordered<HTMLElement, List>, | |
| 54 | 55 | HasItems<HTMLElement, List, ListItem>, | |
| 55 | 56 | Inline<HTMLElement, List>, | |
| 57 | + Ordered<HTMLElement, List, ListItem>, | ||
| 56 | 58 | Plain<HTMLElement, List> { | |
| 57 | 59 | ||
| 58 | 60 | // ------------------------------------------------------ factory | |
@@ -71,6 +73,7 @@ public static <E extends HTMLElement> List list(HTMLContainerBuilder<E> builder) | |||
| 71 | 73 | private final Map<String, ListItem> items; | |
| 72 | 74 | private final java.util.List<BiConsumer<List, ListItem>> onAdd; | |
| 73 | 75 | private final java.util.List<BiConsumer<List, ListItem>> onRemove; | |
| 76 | + private Comparator<ListItem> comparator; | ||
| 74 | 77 | ||
| 75 | 78 | <E extends HTMLElement> List(HTMLContainerBuilder<E> builder) { | |
| 76 | 79 | super(ComponentType.List, builder.css(Classes.component(list)) | |
@@ -83,22 +86,14 @@ <E extends HTMLElement> List(HTMLContainerBuilder<E> builder) { | |||
| 83 | 86 | ||
| 84 | 87 | // ------------------------------------------------------ add | |
| 85 | 88 | ||
| 86 | - public <T> List addItems(Iterable<T> items, Function<T, ListItem> display) { | ||
| 87 | - for (T item : items) { | ||
| 88 | - ListItem li = display.apply(item); | ||
| 89 | - addItem(li); | ||
| 90 | - } | ||
| 91 | - return this; | ||
| 92 | - } | ||
| 93 | - | ||
| 94 | - public List addItem(ListItem item) { | ||
| 95 | - return add(item); | ||
| 96 | - } | ||
| 97 | - | ||
| 98 | 89 | @Override | |
| 99 | 90 | public List add(ListItem item) { | |
| 91 | + if (comparator != null) { | ||
| 92 | + addOrdered(this, this, item, comparator); | ||
| 93 | + } else { | ||
| 94 | + add(item.element()); | ||
| 95 | + } | ||
| 100 | 96 | items.put(item.identifier(), item); | |
| 101 | - add(item.element()); | ||
| 102 | 97 | onAdd.forEach(bc -> bc.accept(this, item)); | |
| 103 | 98 | return this; | |
| 104 | 99 | } | |
@@ -107,12 +102,18 @@ public List addDivider() { | |||
| 107 | 102 | return add(divider(li)); | |
| 108 | 103 | } | |
| 109 | 104 | ||
| 110 | - | ||
| 111 | 105 | // ------------------------------------------------------ builder | |
| 106 | + | ||
| 112 | 107 | public List largeIcons() { | |
| 113 | 108 | return css(modifier(icon, lg)); | |
| 114 | 109 | } | |
| 115 | 110 | ||
| 111 | + @Override | ||
| 112 | + public List ordered(Comparator<ListItem> comparator) { | ||
| 113 | + this.comparator = comparator; | ||
| 114 | + return this; | ||
| 115 | + } | ||
| 116 | + | ||
| 116 | 117 | @Override | |
| 117 | 118 | public List that() { | |
| 118 | 119 | return this; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,18 @@ | |||
| 1 | + /* | ||
| 2 | + * Copyright 2023 Red Hat | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 1 | 16 | import * as fs from 'fs'; | |
| 2 | 17 | import * as path from 'path'; | |
| 3 | 18 | import { fileURLToPath } from 'url'; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments