001 package org.gwtbootstrap3.extras.summernote.client.ui.base;
002
003 /*
004 * #%L
005 * GwtBootstrap3
006 * %%
007 * Copyright (C) 2013 - 2014 GwtBootstrap3
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 * http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023 import com.google.gwt.core.client.JavaScriptObject;
024 import com.google.gwt.dom.client.Element;
025 import com.google.gwt.user.client.Event;
026 import com.google.web.bindery.event.shared.HandlerRegistration;
027 import org.gwtbootstrap3.client.ui.TextArea;
028 import org.gwtbootstrap3.extras.summernote.client.event.*;
029
030 /**
031 * Wrapper for the Summernote WYSIWYG Editor
032 * <p/>
033 * See: http://hackerwins.github.io/summernote/
034 *
035 * @author godi
036 */
037 public class SummernoteBase extends TextArea {
038 /**
039 * Default settings
040 */
041 private int height = 100;
042 private boolean hasFocus = false;
043 private String code;
044 private Toolbar toolbar = buildDefaultToolbar();
045
046 public SummernoteBase() {
047 }
048
049 public void setHeight(final int height) {
050 this.height = height;
051 }
052
053 public void setHasFocus(final boolean hasFocus) {
054 this.hasFocus = hasFocus;
055 }
056
057 public void setToolbar(final Toolbar toolbar) {
058 this.toolbar = toolbar;
059 }
060
061 public void setCode(final String code) {
062 this.code = code;
063 }
064
065 public HandlerRegistration addInitializedHandler(final SummernoteInitializedHandler handler) {
066 return addHandler(handler, SummernoteInitializedEvent.getType());
067 }
068
069 public HandlerRegistration addBlurHandler(final SummernoteOnBlurHandler handler) {
070 return addHandler(handler, SummernoteOnBlurEvent.getType());
071 }
072
073 public HandlerRegistration addEnterHandler(final SummernoteOnEnterHandler handler) {
074 return addHandler(handler, SummernoteOnEnterEvent.getType());
075 }
076
077 public HandlerRegistration addFocusHandler(final SummernoteOnFocusHandler handler) {
078 return addHandler(handler, SummernoteOnFocusEvent.getType());
079 }
080
081 public HandlerRegistration addImageUploadHandler(final SummernoteOnImageUploadHandler handler) {
082 return addHandler(handler, SummernoteOnImageUploadEvent.getType());
083 }
084
085 public HandlerRegistration addKeyDownHandler(final SummernoteOnKeyDownHandler handler) {
086 return addHandler(handler, SummernoteOnKeyDownEvent.getType());
087 }
088
089 public HandlerRegistration addKeyUpHandler(final SummernoteOnKeyUpHandler handler) {
090 return addHandler(handler, SummernoteOnKeyUpEvent.getType());
091 }
092
093 public HandlerRegistration addPasteHandler(final SummernoteOnPasteHandler handler) {
094 return addHandler(handler, SummernoteOnPasteEvent.getType());
095 }
096
097 /**
098 * Gets the HTML code generated from the editor
099 *
100 * @return generated code
101 */
102 public String getCode() {
103 return getCode(getElement());
104 }
105
106 @Override
107 public String getText() {
108 return getCode(getElement());
109 }
110
111 @Override
112 public void setText(String text) {
113 setCode(getElement(), text);
114 }
115
116 /**
117 * Call this when updating settings to ensure everything is up to date
118 */
119 public void reconfigure() {
120 destroy(getElement());
121 initialize();
122 }
123
124 private Toolbar buildDefaultToolbar() {
125 return new Toolbar().toggleAll(true);
126 }
127
128 private void initialize() {
129 initialize(getElement(), height, hasFocus, toolbar.build());
130
131 if (code != null) {
132 setCode(getElement(), code);
133 }
134 }
135
136 @Override
137 protected void onLoad() {
138 super.onLoad();
139
140 // Initialize
141 initialize();
142 }
143
144 @Override
145 protected void onUnload() {
146 super.onUnload();
147
148 // Destroy
149 destroy(getElement());
150 }
151
152 protected void onInitialize(final Event evt) {
153 fireEvent(new SummernoteInitializedEvent(this, evt));
154 }
155
156 protected void onBlue(final Event evt) {
157 fireEvent(new SummernoteOnBlurEvent(this, evt));
158 }
159
160 protected void onEnter(final Event evt) {
161 fireEvent(new SummernoteOnEnterEvent(this, evt));
162 }
163
164 protected void onFocus(final Event evt) {
165 fireEvent(new SummernoteOnFocusEvent(this, evt));
166 }
167
168 protected void onImageUpload(final Event evt) {
169 fireEvent(new SummernoteOnImageUploadEvent(this, evt));
170 }
171
172 protected void onKeyUp(final Event evt) {
173 fireEvent(new SummernoteOnKeyUpEvent(this, evt));
174 }
175
176 protected void onKeyDown(final Event evt) {
177 fireEvent(new SummernoteOnKeyDownEvent(this, evt));
178 }
179
180 protected void onPaste(final Event evt) {
181 fireEvent(new SummernoteOnPasteEvent(this, evt));
182 }
183
184 private native void initialize(Element e, int height, boolean hasFocus, JavaScriptObject toolbar) /*-{
185 var target = this;
186
187 $wnd.jQuery(e).summernote({
188 height: height,
189 focus: hasFocus,
190 toolbar: toolbar,
191 oninit: function (evt) {
192 target.@org.gwtbootstrap3.extras.summernote.client.ui.base.SummernoteBase::onInitialize(Lcom/google/gwt/user/client/Event;)(evt);
193 },
194 onenter: function (evt) {
195 target.@org.gwtbootstrap3.extras.summernote.client.ui.base.SummernoteBase::onEnter(Lcom/google/gwt/user/client/Event;)(evt);
196 },
197 onfocus: function (evt) {
198 target.@org.gwtbootstrap3.extras.summernote.client.ui.base.SummernoteBase::onFocus(Lcom/google/gwt/user/client/Event;)(evt);
199 },
200 onblur: function (evt) {
201 target.@org.gwtbootstrap3.extras.summernote.client.ui.base.SummernoteBase::onBlue(Lcom/google/gwt/user/client/Event;)(evt);
202 },
203 onkeyup: function (evt) {
204 target.@org.gwtbootstrap3.extras.summernote.client.ui.base.SummernoteBase::onKeyUp(Lcom/google/gwt/user/client/Event;)(evt);
205 },
206 onkeydown: function (evt) {
207 target.@org.gwtbootstrap3.extras.summernote.client.ui.base.SummernoteBase::onKeyDown(Lcom/google/gwt/user/client/Event;)(evt);
208 },
209 onImageUpload: function (evt) {
210 target.@org.gwtbootstrap3.extras.summernote.client.ui.base.SummernoteBase::onImageUpload(Lcom/google/gwt/user/client/Event;)(evt);
211 },
212 onpaste: function (evt) {
213 target.@org.gwtbootstrap3.extras.summernote.client.ui.base.SummernoteBase::onPaste(Lcom/google/gwt/user/client/Event;)(evt);
214 }
215 });
216 }-*/;
217
218 private native void destroy(Element e) /*-{
219 $wnd.jQuery(e).destroy();
220 $wnd.jQuery(e).off('oninit');
221 $wnd.jQuery(e).off('onenter');
222 $wnd.jQuery(e).off('onfocus');
223 $wnd.jQuery(e).off('onkeydown');
224 $wnd.jQuery(e).off('onblur');
225 $wnd.jQuery(e).off('onkeyup');
226 $wnd.jQuery(e).off('ononkeydowninit');
227 $wnd.jQuery(e).off('onImageUpload');
228 $wnd.jQuery(e).off('onpaste');
229 }-*/;
230
231 private native void setCode(Element e, String code) /*-{
232 $wnd.jQuery(e).code(code);
233 }-*/;
234
235 private native String getCode(Element e)/*-{
236 return $wnd.jQuery(e).code();
237 }-*/;
238 }