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