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  
46  import elemental.json.JsonObject;
47  import elemental.json.impl.JreJsonFactory;
48  
49  
50  /**
51   * An {@link Image} extension that operates JCrop JQuery plugin.
52   *
53   * @see <a href="http://deepliquid.com/content/Jcrop.html">JCrop plugin</a>
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 }