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