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