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.vaadin.gwt.client.editor.dom;
35
36 import info.magnolia.cms.security.operations.OperationPermissionDefinition;
37 import info.magnolia.jcr.util.NodeTypes;
38 import info.magnolia.rendering.template.AreaDefinition;
39 import info.magnolia.ui.vaadin.gwt.client.editor.event.EditAreaEvent;
40 import info.magnolia.ui.vaadin.gwt.client.editor.event.NewAreaEvent;
41 import info.magnolia.ui.vaadin.gwt.client.editor.event.NewComponentEvent;
42 import info.magnolia.ui.vaadin.gwt.client.shared.AreaElement;
43 import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.AreaEndBar;
44 import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.ComponentPlaceHolder;
45 import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.listener.AreaListener;
46
47 import java.util.Map;
48
49 import com.google.gwt.dom.client.Element;
50 import com.google.gwt.event.shared.EventBus;
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public class MgnlArea extends MgnlElement implements AreaListener {
68 public static final String EDITOR_INIT_CLASS_NAME = "init";
69 private AreaEndBar areaEndBar;
70 private ComponentPlaceHolder componentPlaceHolder;
71 private Element componentMarkerElement;
72 private EventBus eventBus;
73
74
75
76
77 public MgnlArea(MgnlElement parent, EventBus eventBus) {
78 super(parent);
79 this.eventBus = eventBus;
80 }
81
82 private AreaEndBar getAreaEndBar() {
83 return areaEndBar;
84 }
85
86 public void setAreaEndBar(AreaEndBar areaEndBar) {
87 this.areaEndBar = areaEndBar;
88 }
89
90 private ComponentPlaceHolder getComponentPlaceHolder() {
91 return componentPlaceHolder;
92 }
93
94 public void setComponentPlaceHolder(ComponentPlaceHolder componentPlaceHolder) {
95 this.componentPlaceHolder = componentPlaceHolder;
96 }
97
98 public void setComponentMarkerElement(Element componentElement) {
99 this.componentMarkerElement = componentElement;
100 }
101
102 public Element getComponentMarkerElement() {
103 return componentMarkerElement;
104 }
105
106 @Override
107 public AreaElement getTypedElement() {
108 String availableComponents = getAttribute("availableComponents");
109 AreaElement area = new AreaElement(getAttribute("workspace"), getAttribute("path"), getAttribute("dialog"), availableComponents);
110
111 boolean areaHasChildComponents = getComponents().size() > 0;
112 boolean optional = Boolean.parseBoolean(getAttribute("optional"));
113 boolean created = Boolean.parseBoolean(getAttribute("created"));
114 boolean hasAvailableComponents = availableComponents != null && !availableComponents.isEmpty();
115 boolean addible = true;
116 if (getAttributes().containsKey(OperationPermissionDefinition.ADDIBLE)) {
117 addible = Boolean.parseBoolean(getAttribute(OperationPermissionDefinition.ADDIBLE));
118 }
119
120 area.setOptional(optional);
121 area.setCreated(created);
122 area.setAddible(addible && hasAvailableComponents && !(optional && !created) && !(isTypeSingle() && areaHasChildComponents) && !isMaxComponentsReached());
123
124 return area;
125 }
126
127 public boolean isTypeSingle() {
128 return "single".equals(getAttribute("type"));
129 }
130
131 public boolean isMaxComponentsReached() {
132 boolean showAddButton = Boolean.parseBoolean(getAttribute("showAddButton"));
133 boolean showNewComponentArea = Boolean.parseBoolean(getAttribute("showNewComponentArea"));
134
135 return showNewComponentArea && !showAddButton;
136 }
137
138 @Override
139 public void createOptionalArea() {
140 eventBus.fireEvent(new NewAreaEvent(getTypedElement()));
141 }
142
143 @Override
144 public void edit() {
145 eventBus.fireEvent(new EditAreaEvent(getTypedElement()));
146 }
147
148 @Override
149 public void createNewComponent() {
150 eventBus.fireEvent(new NewComponentEvent(getTypedElement()));
151 }
152
153 public boolean hasAddButton() {
154 boolean optional = Boolean.parseBoolean(getAttribute("optional"));
155 boolean created = Boolean.parseBoolean(getAttribute("created"));
156
157 return optional && !created;
158 }
159
160 public boolean hasEditButton() {
161 boolean optional = Boolean.parseBoolean(getAttribute("optional"));
162 boolean created = Boolean.parseBoolean(getAttribute("created"));
163 boolean dialog = null != getAttribute("dialog");
164
165 if (dialog) {
166
167 if (!optional || (optional && created)) {
168 return true;
169 }
170 }
171 return false;
172 }
173
174 public boolean hasAddComponentButton() {
175 return Boolean.parseBoolean(getAttribute("showAddButton"));
176 }
177
178 public String getLabel() {
179 String label = getAttribute("label");
180 boolean optional = Boolean.parseBoolean(getAttribute("optional"));
181 return label + ((optional) ? " (optional)" : "");
182 }
183
184 public boolean isBoxPlaceHolder() {
185 Element marker = getComponentMarkerElement();
186 boolean onlyBar = (marker != null && marker.getAttribute(AreaDefinition.CMS_ADD).equals("bar"));
187 return !onlyBar;
188 }
189
190 public String getPlaceHolderLabel(Map<String, String> i18nKeys) {
191 String label = getAttribute("label");
192
193
194 if (isMaxComponentsReached()) {
195 return i18nKeys.get("pages.areas.maxComponentsReached");
196 }
197
198 if (label != null && !label.isEmpty()) {
199 return i18nKeys.get("pages.areas.newLabelComponent").replace("{0}", label);
200 }
201
202
203 return i18nKeys.get("pages.areas.newComponent");
204 }
205
206 public int getActivationStatus() {
207 boolean isOptionalArea = Boolean.parseBoolean(getAttribute("optional"));
208 boolean isCreatedArea = Boolean.parseBoolean(getAttribute("created"));
209
210 if (isOptionalArea && !isCreatedArea) {
211 return -1;
212 }else if (containsAttribute(ACTIVATION_STATUS_KEY)) {
213 return Integer.parseInt(getAttribute(ACTIVATION_STATUS_KEY));
214 }
215 return NodeTypes.Activatable.ACTIVATION_STATUS_NOT_ACTIVATED;
216 }
217
218 public void removeFocus() {
219 if (getControlBar() != null) {
220 getControlBar().removeFocus();
221 }
222
223 if (getAreaEndBar() != null) {
224 getAreaEndBar().removeFocus();
225 }
226 }
227
228 public void setFocus(boolean child) {
229 if (getControlBar() != null) {
230 getControlBar().setFocus(child);
231 }
232 if (getAreaEndBar() != null) {
233 getAreaEndBar().setFocus(child);
234 }
235 }
236
237 public void setVisible(boolean visible) {
238 if (getControlBar() != null) {
239 getControlBar().setVisible(visible);
240 }
241 if (getAreaEndBar() != null) {
242 getAreaEndBar().setVisible(visible);
243 }
244 }
245
246 public void setPlaceHolderVisible(boolean visible) {
247 if (getComponentPlaceHolder() != null) {
248 getComponentPlaceHolder().setVisible(visible);
249 }
250 }
251
252 public void toggleInitFocus(boolean visible) {
253 if (visible) {
254 getControlBar().addStyleName(EDITOR_INIT_CLASS_NAME);
255 getAreaEndBar().addStyleName(EDITOR_INIT_CLASS_NAME);
256 getAreaEndBar().addStyleName(EDITOR_INIT_CLASS_NAME);
257 } else {
258 getControlBar().removeStyleName(EDITOR_INIT_CLASS_NAME);
259 getAreaEndBar().removeStyleName(EDITOR_INIT_CLASS_NAME);
260 }
261 }
262
263 public void onDragStart(boolean isDrag) {
264 if (getComponentPlaceHolder() != null) {
265 getComponentPlaceHolder().setStyleName("moveOngoing", isDrag);
266 }
267 }
268 }