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.editor;
35
36 import info.magnolia.ui.vaadin.editor.CroppableImage.JCropReleaseEvent;
37 import info.magnolia.ui.vaadin.editor.CroppableImage.JCropSelectionEvent;
38 import info.magnolia.ui.vaadin.gwt.client.jcrop.JCropState;
39 import info.magnolia.ui.vaadin.gwt.shared.jcrop.SelectionArea;
40
41 import com.vaadin.annotations.JavaScript;
42 import com.vaadin.server.AbstractJavaScriptExtension;
43 import com.vaadin.ui.Component;
44 import com.vaadin.ui.Image;
45
46 import elemental.json.JsonObject;
47 import elemental.json.impl.JreJsonFactory;
48
49
50
51
52
53
54
55 @JavaScript({ "jquery.color.js", "jquery.Jcrop.min.js", "jcrop_connector.js" })
56 public class JCrop extends AbstractJavaScriptExtension {
57
58 private JreJsonFactory jsonFactory = new JreJsonFactory();
59
60 @Override
61 public CroppableImage getParent() {
62 return (CroppableImage)super.getParent();
63 }
64
65 public JCrop(JCropHandler handler) {
66 addFunction("doOnSelect", args -> {
67 SelectionArea area = AreaFromJSON(args.getObject(0));
68 getState(false).selection = area;
69 getParent().fireEvent(new JCropSelectionEvent(getParent(), area));
70 });
71
72 addFunction("doOnRelease", args -> {
73 getState().selection = null;
74 getParent().fireEvent(new JCropReleaseEvent(getParent()));
75 });
76
77 addFunction("onCreated", args -> getState(false).isValid = true);
78 }
79
80 protected SelectionArea AreaFromJSON(JsonObject json) {
81 return new SelectionArea(
82 (int)json.getNumber("x"),
83 (int)json.getNumber("y"),
84 (int)json.getNumber("w"),
85 (int)json.getNumber("h"));
86 }
87
88 @Override
89 public void attach() {
90 super.attach();
91 getParent().addStyleName("croppable" + getConnectorId());
92 if (getState().selectionStatusComponent != null) {
93 ((Component)getState().selectionStatusComponent).addStyleName("crop-status" + getConnectorId());
94 }
95 }
96
97 @Override
98 protected JCropState getState(boolean markAsDirty) {
99 return (JCropState)super.getState(markAsDirty);
100 }
101
102 @Override
103 public JCropState getState() {
104 return (JCropState) super.getState();
105 }
106
107 @Override
108 protected Class<Image> getSupportedParentType() {
109 return Image.class;
110 }
111
112 public void setAspectRatio(double aspectRatio) {
113 getState().aspectRatio = aspectRatio;
114 }
115
116 public void animateTo(SelectionArea area) {
117 callFunction("animateTo", jsonFactory.create(area.toString()));
118 }
119
120 public boolean isCropVisible() {
121 return getState().isVisible;
122 }
123
124 public void setCropVisible(boolean isVisible) {
125 getState().isVisible = isVisible;
126 }
127
128 public void enable() {
129 callFunction("enable");
130 }
131
132 public void disable() {
133 callFunction("disable");
134 }
135
136 public void setSelectionStatusComponent(Component c) {
137 if (getState().selectionStatusComponent != null) {
138 ((Component)getState().selectionStatusComponent).removeStyleName("");
139 }
140 getState().selectionStatusComponent = c;
141 if (getSession() != null) {
142 c.addStyleName("crop-status" + getConnectorId());
143 }
144 }
145
146 public void setBackgroundColor(String color) {
147 getState().backgroundColor = color;
148 }
149
150 public void setBackgroundOpacity(double opacity) {
151 getState().backgroundOpacity = opacity;
152 }
153
154 public void setMinHeight(int height) {
155 getState().minHeight = height;
156 }
157
158 public void setMaxHeight(int height) {
159 getState().maxHeight = height;
160 }
161
162 public void setMaxWidth(int width) {
163 getState().maxWidth = width;
164 }
165
166 public void setMinWidth(int width) {
167 getState().minWidth = width;
168 }
169
170 public void setEnabled(boolean enabled) {
171 getState().enabled = enabled;
172 }
173
174 public void invalidate() {
175 getState().isValid = false;
176 }
177
178 public void setTrueHeight(int height) {
179 getState().trueHeight = height;
180 }
181
182 public void setTrueWidth(int width) {
183 getState().trueWidth = width;
184 }
185
186 public void select(SelectionArea area) {
187 getState().selection = area;
188 }
189 }