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 import com.vaadin.ui.JavaScriptFunction;
46
47 import elemental.json.JsonArray;
48 import elemental.json.JsonObject;
49 import elemental.json.impl.JreJsonFactory;
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", new JavaScriptFunction() {
67 @Override
68 public void call(JsonArray args) {
69 SelectionArea area = AreaFromJSON(args.getObject(0));
70 getState(false).selection = area;
71 getParent().fireEvent(new JCropSelectionEvent(getParent(), area));
72 }
73 });
74
75 addFunction("doOnRelease", new JavaScriptFunction() {
76 @Override
77 public void call(JsonArray args) {
78 getState().selection = null;
79 getParent().fireEvent(new JCropReleaseEvent(getParent()));
80 }
81 });
82
83 addFunction("onCreated", new JavaScriptFunction() {
84 @Override
85 public void call(JsonArray args) {
86 getState(false).isValid = true;
87 }
88 });
89 }
90
91 protected SelectionArea AreaFromJSON(JsonObject json) {
92 return new SelectionArea(
93 (int)json.getNumber("x"),
94 (int)json.getNumber("y"),
95 (int)json.getNumber("w"),
96 (int)json.getNumber("h"));
97 }
98
99 @Override
100 public void attach() {
101 super.attach();
102 getParent().addStyleName("croppable" + getConnectorId());
103 if (getState().selectionStatusComponent != null) {
104 ((Component)getState().selectionStatusComponent).addStyleName("crop-status" + getConnectorId());
105 }
106 }
107
108 @Override
109 protected JCropState getState(boolean markAsDirty) {
110 return (JCropState)super.getState(markAsDirty);
111 }
112
113 @Override
114 public JCropState getState() {
115 return (JCropState) super.getState();
116 }
117
118 @Override
119 protected Class<Image> getSupportedParentType() {
120 return Image.class;
121 }
122
123 public void setAspectRatio(double aspectRatio) {
124 getState().aspectRatio = aspectRatio;
125 }
126
127 public void animateTo(SelectionArea area) {
128 callFunction("animateTo", jsonFactory.create(area.toString()));
129 }
130
131 public boolean isCropVisible() {
132 return getState().isVisible;
133 }
134
135 public void setCropVisible(boolean isVisible) {
136 getState().isVisible = isVisible;
137 }
138
139 public void enable() {
140 callFunction("enable");
141 }
142
143 public void disable() {
144 callFunction("disable");
145 }
146
147 public void setSelectionStatusComponent(Component c) {
148 if (getState().selectionStatusComponent != null) {
149 ((Component)getState().selectionStatusComponent).removeStyleName("");
150 }
151 getState().selectionStatusComponent = c;
152 if (getSession() != null) {
153 c.addStyleName("crop-status" + getConnectorId());
154 }
155 }
156
157 public void setBackgroundColor(String color) {
158 getState().backgroundColor = color;
159 }
160
161 public void setBackgroundOpacity(double opacity) {
162 getState().backgroundOpacity = opacity;
163 }
164
165 public void setMinHeight(int height) {
166 getState().minHeight = height;
167 }
168
169 public void setMaxHeight(int height) {
170 getState().maxHeight = height;
171 }
172
173 public void setMaxWidth(int width) {
174 getState().maxWidth = width;
175 }
176
177 public void setMinWidth(int width) {
178 getState().minWidth = width;
179 }
180
181 public void setEnabled(boolean enabled) {
182 getState().enabled = enabled;
183 }
184
185 public void invalidate() {
186 getState().isValid = false;
187 }
188
189 public void setTrueHeight(int height) {
190 getState().trueHeight = height;
191 }
192
193 public void setTrueWidth(int width) {
194 getState().trueWidth = width;
195 }
196
197 public void select(SelectionArea area) {
198 getState().selection = area;
199 }
200 }