View Javadoc

1   /**
2    * This file Copyright (c) 2012-2013 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.ArrayList;
37  import java.util.Arrays;
38  import java.util.List;
39  
40  import org.vaadin.openesignforms.ckeditor.widgetset.client.ui.CKEditorService;
41  import org.vaadin.openesignforms.ckeditor.widgetset.client.ui.VCKEditorTextField;
42  
43  import com.google.gwt.core.client.JavaScriptObject;
44  import com.google.gwt.core.client.Scheduler.ScheduledCommand;
45  import com.vaadin.client.ApplicationConnection;
46  import com.vaadin.client.UIDL;
47  import com.vaadin.client.ValueMap;
48  
49  /**
50   * Magnolia rich text field adds an ability to custom plugins to communicate
51   * with the server. This was not possible with the add-on out of the box.
52   */
53  public class VMagnoliaRichTextField extends VCKEditorTextField implements VMagnoliaRichTextEditor.Listener {
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      protected VMagnoliaRichTextEditor editor;
60      public List<String> pluginEvents;
61      ValueMap customPlugins = null;
62  
63      public VMagnoliaRichTextField() {
64          super();
65          pluginEvents = new ArrayList<String>();
66          loadCKEditor();
67      }
68  
69      @Override
70      public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
71          super.updateFromUIDL(uidl, client);
72  
73          /*
74           * External plugins has to be loaded after CKEDITOR instance
75           * have been created but before editor instance is created.
76           * Store external plugins into member and let CKEditor loader
77           * to pick up them. Server side component has the responsibility
78           * to send this attribute early enough.
79           */
80          if (uidl.hasAttribute(VAR_SERVERPLUGINS)) {
81              customPlugins = uidl.getMapAttribute(VAR_SERVERPLUGINS);
82          }
83  
84          // list of plugin events that server is interested of handling.
85          if (uidl.hasAttribute(VAR_EVENTNAMES) && this.editor != null) {
86              pluginEvents = Arrays.asList(uidl.getStringArrayAttribute(VAR_EVENTNAMES));
87  
88              for (String eventName : pluginEvents) {
89                  this.editor.addListener(this, eventName);
90              }
91          }
92  
93          // Server wants to send an event to a plugin.
94          if (uidl.hasAttribute(VAR_FIRE_PLUGIN_EVENT) && this.editor != null) {
95              this.editor.fire(
96                      uidl.getStringAttribute(VAR_FIRE_PLUGIN_EVENT),
97                      uidl.getStringAttribute(VAR_FIRE_PLUGIN_EVENT_VALUE)
98              );
99          }
100     }
101 
102     private static native void loadExternalPlugin(String pluginName, String path) /*-{    
103                                                                                   $wnd.CKEDITOR.plugins.addExternal( pluginName, path, 'plugin.js' );
104                                                                                   }-*/;
105 
106     /**
107      * Will be invoked from CK plugins.
108      */
109     @Override
110     public void onPluginEvent(String eventName, String data) {
111         if (pluginEvents.contains(eventName)) {
112             clientToServer.updateVariable(
113                     paintableId,
114                     VAR_EVENT_PREFIX + eventName,
115                     data == null ? "" : data,
116                     true);
117         }
118     }
119 
120     /**
121      * Initializes CKEditor if not done already and get editor instance
122      * injected.
123      */
124     private void loadCKEditor() {
125         if (!CKEditorService.libraryReady()) {
126             CKEditorService.loadLibrary(new ScheduledCommand() {
127                 @Override
128                 public void execute() {
129                     loadPlugins();
130                     injectEditorTo(VMagnoliaRichTextField.this);
131                 }
132             });
133         } else {
134             loadPlugins();
135             injectEditorTo(this);
136         }
137     }
138 
139     /**
140      * Load plugins from custom path.
141      */
142     private void loadPlugins() {
143         if (customPlugins != null) {
144             for (String key : customPlugins.getKeySet()) {
145                 loadExternalPlugin(key, customPlugins.getString(key));
146             }
147         }
148     }
149 
150     protected void setEditor(JavaScriptObject editor) {
151         this.editor = (VMagnoliaRichTextEditor) editor;
152     }
153 
154     /*
155      * This method hides a hack. Base class owns privately an instance of
156      * CKEditor editor field. Editor object needs to be accessed or else it
157      * would not be possible to handle events from custom made CKEditor
158      * plugins. As a workaround this method adds a listener for creation of
159      * editor objects and if an editor is created inside DIV element with id
160      * matching paintableId member of this class, then this editor is the one
161      * from the base class => we got access to the private member we need.
162      */
163     private static native void injectEditorTo(final VMagnoliaRichTextField listener)
164     /*-{        
165         var createdEvent = function(e) {            
166             var listenerInstanceId = listener.@info.magnolia.ui.vaadin.gwt.client.richtext.VMagnoliaRichTextField::getPaintableId()();
167             var editorInstanceId = e.editor.element.getId();
168             if(listenerInstanceId == editorInstanceId) {
169                 listener.@info.magnolia.ui.vaadin.gwt.client.richtext.VMagnoliaRichTextField::setEditor(Lcom/google/gwt/core/client/JavaScriptObject;)(e.editor);
170                 
171                 e.editor.on('destroy', function(e) {
172                     listener.@info.magnolia.ui.vaadin.gwt.client.richtext.VMagnoliaRichTextField::setEditor(Lcom/google/gwt/core/client/JavaScriptObject;)(null);
173                 });
174             }
175         };
176 
177         $wnd.CKEDITOR.on('instanceCreated', createdEvent);
178      }-*/;
179 
180     /*
181      * Needed by the hack above
182      */
183     private String getPaintableId() {
184         return paintableId;
185     }
186 }