View Javadoc
1   /**
2    * This file Copyright (c) 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.ckeditor;
35  
36  
37  import static info.magnolia.ui.vaadin.gwt.client.ckeditor.VMagnoliaCKEditorTextEvents.*;
38  
39  import java.net.URI;
40  import java.util.ArrayList;
41  import java.util.Arrays;
42  import java.util.List;
43  import java.util.Map;
44  import java.util.Objects;
45  import java.util.Optional;
46  
47  import org.apache.commons.lang3.StringUtils;
48  import org.vaadin.alump.ckeditor.CKEditorTextField;
49  
50  import com.vaadin.server.Page;
51  import com.vaadin.server.PaintException;
52  import com.vaadin.server.PaintTarget;
53  import com.vaadin.server.VaadinService;
54  
55  
56  /**
57   * Magnolia 6 - Vaadin 8 extended CKEditorTextField for custom made Magnolia plugins.
58   * By default CKEditor wrapper for Vaadin does not allow
59   * custom events between CKEditor plugins and server.
60   */
61  public class MagnoliaCKEditorTextField extends CKEditorTextField implements MagnoliaCKEditorTextFieldEvents {
62  
63      private String fireEvent;
64      private String fireEventValue;
65      private String[] customEvents;
66      private Map<String, String> serverPlugins;
67      private List<PluginListener> listeners = new ArrayList<>();
68      private MagnoliaCKEditorConfig config;
69  
70      public void addListener(PluginListener listener) {
71          listeners.add(listener);
72      }
73  
74      public MagnoliaCKEditorTextField(MagnoliaCKEditorConfig config) {
75          super(config);
76          this.config = config;
77          serverPlugins = config.getExternalPlugins();
78          customEvents = config.getListenedEvents();
79          config.setContentsCss(getPathToVaadin() + "/themes/resurface/richtextfield-contents/styles.css");
80      }
81  
82      @Override
83      public void changeVariables(Object source, Map<String, Object> variables) {
84          super.changeVariables(source, variables);
85  
86          if (isEmpty()) {
87              this.setValue(null, true);
88          }
89  
90          Optional.ofNullable(config).ifPresent(c -> {
91              // See if client sends events
92              Arrays.stream(c.getListenedEvents())
93                      .filter(Objects::nonNull)
94                      .forEach(eventName -> {
95                          final String eventNameResolved = VAR_EVENT_PREFIX + eventName;
96                          if (variables.containsKey(eventNameResolved)) {
97                              listeners.forEach(l -> l.onPluginEvent(eventName, variables.get(eventNameResolved).toString()));
98                          }
99                      });
100         });
101     }
102     @Override
103     public void firePluginEvent(String event, String value) {
104         fireEvent = event;
105         fireEventValue = value;
106         markAsDirty();
107     }
108 
109     @Override
110     public void paintContent(PaintTarget target) throws PaintException {
111         super.paintContent(target);
112 
113         if (serverPlugins != null) {
114             target.addAttribute(VAR_SERVERPLUGINS, serverPlugins);
115         }
116 
117         // tell client that server is interested of these events
118         if (customEvents != null) {
119             target.addAttribute(VAR_EVENTNAMES, customEvents);
120         }
121 
122         // send event to plugin
123         if (fireEvent != null && fireEventValue != null) {
124             target.addAttribute(VAR_FIRE_PLUGIN_EVENT, fireEvent);
125             target.addAttribute(VAR_FIRE_PLUGIN_EVENT_VALUE, fireEventValue);
126             fireEvent = null;
127             fireEventValue = null;
128         }
129     }
130 
131     @Override
132     public void setHeight(float height, Unit unit) {
133         super.setHeight(height, unit);
134     }
135 
136     @Override
137     public boolean isEmpty() {
138         return StringUtils.isEmpty(getValue());
139     }
140 
141     private String getPathToVaadin() {
142         final URI location = Page.getCurrent().getLocation();
143         return location.getScheme()  + "://" +location.getAuthority() + VaadinService.getCurrentRequest().getContextPath() + "/VAADIN";
144     }
145 }