View Javadoc
1   /**
2    * This file Copyright (c) 2010-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * An {@link Image} extension that operates JCrop JQuery plugin ({@link http://deepliquid.com/content/Jcrop.html}).
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 }