1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.ui.contentapp.browser;
35
36 import info.magnolia.ui.api.app.SubAppContext;
37 import info.magnolia.ui.contentapp.ContentBrowserSubApp;
38 import info.magnolia.ui.contentapp.browser.actions.ActionbarView;
39 import info.magnolia.ui.contentapp.browser.context.LocationContext;
40 import info.magnolia.ui.contentapp.browser.context.ValueContext;
41 import info.magnolia.ui.contentapp.configuration.BrowserDescriptor;
42 import info.magnolia.ui.contentapp.configuration.WorkbenchDefinition;
43 import info.magnolia.ui.framework.ContextProperty;
44 import info.magnolia.ui.framework.UiFrameworkView;
45 import info.magnolia.ui.framework.ViewDefinition;
46 import info.magnolia.ui.framework.datasource.components.ItemResolver;
47 import info.magnolia.ui.framework.datasource.definition.DatasourceDefinition;
48
49 import java.util.Collection;
50 import java.util.Objects;
51 import java.util.Optional;
52
53 import javax.inject.Inject;
54
55 import com.vaadin.ui.Composite;
56 import com.vaadin.ui.HorizontalLayout;
57 import com.vaadin.ui.VerticalLayout;
58
59
60
61
62
63
64
65 public class Browser<T, DS extends DatasourceDefinition> extends Composite implements UiFrameworkView {
66
67 private final ContextProperty<ContentBrowserSubApp.BrowserLocation> location;
68 private final SubAppContext subAppContext;
69 private final ItemResolver<T> itemResolver;
70
71 @Inject
72 public Browser(
73 ValueContext<T> valueContext,
74 LocationContext locationContext,
75 BrowserDescriptor<T, DS> descriptor,
76 SubAppContext subAppContext,
77 ItemResolver<T> itemResolver) {
78 this.subAppContext = subAppContext;
79 this.itemResolver = itemResolver;
80 this.location = locationContext.location();
81
82 valueContext.observe(selectedItems ->
83 location.mutate(location ->
84 pushItemPathIntoLocation(selectedItems, location)
85 )
86 );
87
88 location.observe(locationOptional ->
89 locationOptional.ifPresent(location -> {
90 Optional<String> currentPath = valueContext.getSingle().map(itemResolver::getPath);
91 if (currentPath.map(path -> !Objects.equals(path, location.getNodePath())).orElse(true)) {
92 itemResolver.getItemByPath(location.getNodePath()).ifPresent(valueContext::set);
93 }
94 }));
95
96 layout(descriptor);
97 }
98
99 private void pushItemPathIntoLocation(Collection<T> selectedItems, ContentBrowserSubApp.BrowserLocation location) {
100 location.updateNodePath(selectedItems.stream().findFirst().map(itemResolver::getPath).orElse(""));
101 updateLocation(location);
102 }
103
104 private void updateLocation(ContentBrowserSubApp.BrowserLocation location) {
105 subAppContext.getAppContext().updateSubAppLocation(subAppContext, location);
106 }
107
108 private void layout(BrowserDescriptor<T, DS> descriptor) {
109 final HorizontalLayout workbenchWrapper = new HorizontalLayout();
110 workbenchWrapper.setSizeFull();
111
112 WorkbenchDefinition<T> workbenchDefinition = descriptor.getWorkbench();
113 Workbench<T> workbench = create("workbench", workbenchDefinition);
114
115 workbench.onContentViewTypeChange(viewId -> location.mutate(location -> location.updateViewType(viewId)));
116 workbench.setTitle(descriptor.getLabel());
117
118 workbenchWrapper.addComponentsAndExpand(workbench);
119 workbenchWrapper.addComponents(create("actionbar", ActionbarView.class, descriptor.getActionbar()));
120
121 StatusBar<T> statusBar = create(
122 ViewDefinition.<StatusBar<T>>builder()
123 .withName("statusbar")
124 .withImplementationClass(StatusBar.class)
125 .build());
126
127 VerticalLayout browser = new VerticalLayout(workbenchWrapper, statusBar.asVaadinComponent());
128 browser.setSizeFull();
129 browser.setSpacing(false);
130 browser.setMargin(false);
131 browser.setExpandRatio(workbenchWrapper, 1f);
132 browser.setExpandRatio(statusBar.asVaadinComponent(), 0);
133 browser.addStyleName("browser");
134
135 setCompositionRoot(browser);
136 setSizeFull();
137 }
138
139 public void setLocation(ContentBrowserSubApp.BrowserLocation location) {
140 this.location.set(location);
141 }
142 }