Search in sources :

Example 1 with HierarchicalUpdate

use of com.vaadin.flow.data.provider.hierarchy.HierarchicalArrayUpdater.HierarchicalUpdate in project flow by vaadin.

the class HierarchicalDataCommunicator method doExpand.

private Collection<T> doExpand(Collection<T> items, boolean syncClient) {
    List<T> expandedItems = new ArrayList<>();
    items.forEach(item -> {
        if (mapper.expand(item)) {
            expandedItems.add(item);
        }
    });
    if (syncClient && !expandedItems.isEmpty()) {
        HierarchicalUpdate update = arrayUpdater.startUpdate(getHierarchyMapper().getRootSize());
        update.enqueue("$connector.expandItems", expandedItems.stream().map(this::generateJsonForExpandedOrCollapsedItem).collect(JsonUtils.asArray()));
        requestFlush(update);
    }
    return expandedItems;
}
Also used : HierarchicalUpdate(com.vaadin.flow.data.provider.hierarchy.HierarchicalArrayUpdater.HierarchicalUpdate) ArrayList(java.util.ArrayList)

Example 2 with HierarchicalUpdate

use of com.vaadin.flow.data.provider.hierarchy.HierarchicalArrayUpdater.HierarchicalUpdate in project flow by vaadin.

the class HierarchicalDataCommunicator method doCollapse.

private Collection<T> doCollapse(Collection<T> items, boolean syncClient) {
    List<T> collapsedItems = new ArrayList<>();
    items.forEach(item -> {
        if (mapper.collapse(item)) {
            collapsedItems.add(item);
            HierarchicalCommunicationController<T> controller = dataControllers.remove(getKeyMapper().key(item));
            if (controller != null) {
                controller.unregisterPassivatedKeys();
            }
        }
    });
    if (syncClient && !collapsedItems.isEmpty()) {
        HierarchicalUpdate update = arrayUpdater.startUpdate(getHierarchyMapper().getRootSize());
        update.enqueue("$connector.collapseItems", collapsedItems.stream().map(this::generateJsonForExpandedOrCollapsedItem).collect(JsonUtils.asArray()));
        requestFlush(update);
    }
    return collapsedItems;
}
Also used : HierarchicalUpdate(com.vaadin.flow.data.provider.hierarchy.HierarchicalArrayUpdater.HierarchicalUpdate) ArrayList(java.util.ArrayList)

Example 3 with HierarchicalUpdate

use of com.vaadin.flow.data.provider.hierarchy.HierarchicalArrayUpdater.HierarchicalUpdate in project flow by vaadin.

the class HierarchicalCommunicationController method flush.

public void flush() {
    Set<String> oldActive = new HashSet<>(activeKeyOrder);
    assumedSize = mapper.countChildItems(keyMapper.get(parentKey));
    final Range previousActive = Range.withLength(activeStart, activeKeyOrder.size());
    final Range effectiveRequested = requestedRange.restrictTo(Range.withLength(0, assumedSize));
    resendEntireRange |= !(previousActive.intersects(effectiveRequested) || (previousActive.isEmpty() && effectiveRequested.isEmpty()));
    // Phase 1: Find all items that the client should have
    List<String> newActiveKeyOrder = collectKeysToFlush(previousActive, effectiveRequested);
    activeKeyOrder = newActiveKeyOrder;
    activeStart = effectiveRequested.getStart();
    // Phase 2: Collect changes to send
    HierarchicalUpdate update = startUpdate.apply(assumedSize);
    boolean updated = collectChangesToSend(previousActive, effectiveRequested, update);
    resendEntireRange = false;
    assumeEmptyClient = false;
    // Phase 3: passivate anything that isn't longer active
    passivateInactiveKeys(oldActive, newActiveKeyOrder, update, updated);
    // Phase 4: unregister passivated and updated items
    unregisterPassivatedKeys();
}
Also used : HierarchicalUpdate(com.vaadin.flow.data.provider.hierarchy.HierarchicalArrayUpdater.HierarchicalUpdate) Range(com.vaadin.flow.internal.Range) HashSet(java.util.HashSet)

Example 4 with HierarchicalUpdate

use of com.vaadin.flow.data.provider.hierarchy.HierarchicalArrayUpdater.HierarchicalUpdate in project flow by vaadin.

the class HierarchicalDataCommunicator method reset.

/**
 * Resets all the data.
 * <p>
 * It effectively resends all available data.
 */
@Override
public void reset() {
    super.reset();
    if (!dataControllers.isEmpty()) {
        dataControllers.values().forEach(HierarchicalCommunicationController::unregisterPassivatedKeys);
        dataControllers.clear();
    }
    if (getHierarchyMapper() != null) {
        HierarchicalUpdate update = arrayUpdater.startUpdate(getHierarchyMapper().getRootSize());
        update.enqueue("$connector.ensureHierarchy");
        Collection<T> expandedItems = getHierarchyMapper().getExpandedItems();
        if (!expandedItems.isEmpty()) {
            update.enqueue("$connector.expandItems", expandedItems.stream().map(getKeyMapper()::key).map(key -> {
                JsonObject json = Json.createObject();
                json.put("key", key);
                return json;
            }).collect(JsonUtils.asArray()));
        }
        requestFlush(update);
    }
}
Also used : HierarchicalUpdate(com.vaadin.flow.data.provider.hierarchy.HierarchicalArrayUpdater.HierarchicalUpdate) JsonObject(elemental.json.JsonObject)

Aggregations

HierarchicalUpdate (com.vaadin.flow.data.provider.hierarchy.HierarchicalArrayUpdater.HierarchicalUpdate)4 ArrayList (java.util.ArrayList)2 Range (com.vaadin.flow.internal.Range)1 JsonObject (elemental.json.JsonObject)1 HashSet (java.util.HashSet)1