View Javadoc
1   /**
2    * This file Copyright (c) 2012-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.gwt.client.richtext;
35  
36  import java.util.Arrays;
37  import java.util.List;
38  
39  import org.vaadin.openesignforms.ckeditor.widgetset.client.ui.CKEditor;
40  import org.vaadin.openesignforms.ckeditor.widgetset.client.ui.VCKEditorTextField;
41  
42  import com.google.gwt.user.client.Timer;
43  import com.vaadin.client.ApplicationConnection;
44  import com.vaadin.client.UIDL;
45  import com.vaadin.client.ValueMap;
46  
47  /**
48   * Magnolia rich text field adds an ability to custom plugins to communicate
49   * with the server. This was not possible with the add-on out of the box.
50   */
51  public class VMagnoliaRichTextField extends VCKEditorTextField implements VMagnoliaRichTextEditor.Listener {
52  
53      public static final String VAR_EVENTNAMES = "eventnames";
54      public static final String VAR_SERVERPLUGINS = "serverplugins";
55      public static final String VAR_EVENT_PREFIX = "pluginEvent:";
56      public static final String VAR_FIRE_PLUGIN_EVENT = "firePluginEvent";
57      public static final String VAR_FIRE_PLUGIN_EVENT_VALUE = "firePluginEventValue";
58  
59      private VMagnoliaRichTextEditor editor;
60      private List<String> pluginEvents;
61      private ValueMap customPlugins;
62      private boolean immediate;
63  
64      @Override
65      public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
66  
67          // Make sure external plugins are assigned before the loading command.
68          if (uidl.hasAttribute(VAR_SERVERPLUGINS)) {
69              customPlugins = uidl.getMapAttribute(VAR_SERVERPLUGINS);
70          }
71  
72          // list of plugin events that server is interested of handling.
73          if (uidl.hasAttribute(VAR_EVENTNAMES)) {
74              pluginEvents = Arrays.asList(uidl.getStringArrayAttribute(VAR_EVENTNAMES));
75          }
76  
77          if (uidl.hasAttribute(ATTR_IMMEDIATE)) {
78              immediate = uidl.getBooleanAttribute(ATTR_IMMEDIATE);
79          }
80  
81          super.updateFromUIDL(uidl, client);
82  
83          // Server wants to send an event to a plugin, we must do this after super value update.
84          if (uidl.hasAttribute(VAR_FIRE_PLUGIN_EVENT) && this.editor != null) {
85              this.editor.fire(
86                      uidl.getStringAttribute(VAR_FIRE_PLUGIN_EVENT),
87                      uidl.getStringAttribute(VAR_FIRE_PLUGIN_EVENT_VALUE)
88                      );
89          }
90      }
91  
92      @Override
93      protected CKEditor loadEditor(String inPageConfig) {
94          // Register external plugins
95          if (customPlugins != null && customPlugins.getKeySet() != null && !customPlugins.getKeySet().isEmpty()) {
96              for (String plugin : customPlugins.getKeySet()) {
97                  addExternalPlugin(plugin, customPlugins.getString(plugin));
98              }
99          }
100 
101         // Set convenience base path for registering external plugins in custom config.js
102         setVaadinDirUrl(clientToServer.getConfiguration().getVaadinDirUrl());
103 
104         // Load editor
105         editor = (VMagnoliaRichTextEditor) super.loadEditor(inPageConfig);
106         return editor;
107     }
108 
109     private native void setVaadinDirUrl(String vaadinDirUrl) /*-{
110         $wnd.CKEDITOR.vaadinDirUrl = vaadinDirUrl;
111     }-*/;
112 
113     private native void addExternalPlugin(String pluginName, String path) /*-{
114         $wnd.CKEDITOR.plugins.addExternal(pluginName, path, 'plugin.js');
115     }-*/;
116 
117     /**
118      * Add plugin listeners when instance is ready.
119      */
120     @Override
121     public void onInstanceReady() {
122         super.onInstanceReady();
123 
124         // Add plugin listeners
125         if (pluginEvents != null && !pluginEvents.isEmpty()) {
126             for (String eventName : pluginEvents) {
127                 editor.addPluginListener(eventName, this);
128             }
129         }
130     }
131 
132     @Override
133     public void onPluginEvent(String eventName, String data) {
134         if (pluginEvents.contains(eventName)) {
135             clientToServer.updateVariable(paintableId, VAR_EVENT_PREFIX + eventName, data == null ? "" : data, true);
136         }
137     }
138 
139     /**
140      * Override VCKEditorTextField's default behavior, defer update in case field is immediate.
141      */
142     @Override
143     public void onChange() {
144         if (editor != null && !editor.isReadOnly()) {
145             clientToServer.updateVariable(paintableId, VAR_TEXT, editor.getData(), false);
146             if (immediate) {
147                 valueUpdateTimer.schedule(200);
148             }
149         }
150     }
151 
152     private Timer valueUpdateTimer = new Timer() {
153         @Override
154         public void run() {
155             clientToServer.sendPendingVariableChanges();
156         }
157     };
158 
159     @Override
160     public void doResize() {
161         super.doResize();
162     }
163 }