1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview An abstract superclass for TrogEdit dialog plugins. Each
* Trogedit dialog has its own plugin.
*
* @author nicksantos@google.com (Nick Santos)
*/
goog.provide('goog.editor.plugins.AbstractDialogPlugin');
goog.provide('goog.editor.plugins.AbstractDialogPlugin.EventType');
goog.require('goog.dom');
goog.require('goog.dom.Range');
goog.require('goog.editor.Field');
goog.require('goog.editor.Plugin');
goog.require('goog.editor.range');
goog.require('goog.events');
goog.require('goog.ui.editor.AbstractDialog');
// *** Public interface ***************************************************** //
/**
* An abstract superclass for a Trogedit plugin that creates exactly one
* dialog. By default dialogs are not reused -- each time execCommand is called,
* a new instance of the dialog object is created (and the old one disposed of).
* To enable reusing of the dialog object, subclasses should call
* setReuseDialog() after calling the superclass constructor.
* @param {string} command The command that this plugin handles.
* @constructor
* @extends {goog.editor.Plugin}
*/
goog.editor.plugins.AbstractDialogPlugin = function(command) {
goog.editor.plugins.AbstractDialogPlugin.base(this, 'constructor');
/**
* The command that this plugin handles.
* @private {string}
*/
this.command_ = command;
/** @private {function()} */
this.restoreScrollPosition_ = function() {};
/**
* The current dialog that was created and opened by this plugin.
* @private {?goog.ui.editor.AbstractDialog}
*/
this.dialog_ = null;
/**
* Whether this plugin should reuse the same instance of the dialog each time
* execCommand is called or create a new one.
* @private {boolean}
*/
this.reuseDialog_ = false;
/**
* Mutex to prevent recursive calls to disposeDialog_.
* @private {boolean}
*/
this.isDisposingDialog_ = false;
/**
* SavedRange representing the selection before the dialog was opened.
* @private {?goog.dom.SavedRange}
*/
this.savedRange_ = null;
};
goog.inherits(goog.editor.plugins.AbstractDialogPlugin, goog.editor.Plugin);
/** @override */
goog.editor.plugins.AbstractDialogPlugin.prototype.isSupportedCommand =
function(command) {
return command == this.command_;
};
/**
* Handles execCommand. Dialog plugins don't make any changes when they open a
* dialog, just when the dialog closes (because only modal dialogs are
* supported). Hence this method does not dispatch the change events that the
* superclass method does.
* @param {string} command The command to execute.
* @param {...*} var_args Any additional parameters needed to
* execute the command.
* @return {*} The result of the execCommand, if any.
* @override
*/
goog.editor.plugins.AbstractDialogPlugin.prototype.execCommand = function(
command, var_args) {
return this.execCommandInternal.apply(this, arguments);
};
// *** Events *************************************************************** //
/**
* Event type constants for events the dialog plugins fire.
* @enum {string}
*/
goog.editor.plugins.AbstractDialogPlugin.EventType = {
// This event is fired when a dialog has been opened.
OPENED: 'dialogOpened',
// This event is fired when a dialog has been closed.
CLOSED: 'dialogClosed'
};
// *** Protected interface ************************************************** //
/**
* Creates a new instance of this plugin's dialog. Must be overridden by
* subclasses.
* Implementations should expect that the editor is inactive and cannot be
* focused, nor will its caret position (or selection) be determinable until
* after the dialogs goog.ui.PopupBase.EventType.HIDE event has been handled.
* @param {!goog.dom.DomHelper} dialogDomHelper The dom helper to be used to
* create the dialog.
* @param {*=} opt_arg The dialog specific argument. Concrete subclasses should
* declare a specific type.
* @return {goog.ui.editor.AbstractDialog} The newly created dialog.
* @protected
*/
goog.editor.plugins.AbstractDialogPlugin.prototype.createDialog =
goog.abstractMethod;
/**
* Returns the current dialog that was created and opened by this plugin.
* @return {goog.ui.editor.AbstractDialog} The current dialog that was created
* and opened by this plugin.
* @protected
*/
goog.editor.plugins.AbstractDialogPlugin.prototype.getDialog = function() {
return this.dialog_;
};
/**
* Sets whether this plugin should reuse the same instance of the dialog each
* time execCommand is called or create a new one. This is intended for use by
* subclasses only, hence protected.
* @param {boolean} reuse Whether to reuse the dialog.
* @protected
*/
goog.editor.plugins.AbstractDialogPlugin.prototype.setReuseDialog = function(
reuse) {
this.reuseDialog_ = reuse;
};
/**
* Handles execCommand by opening the dialog. Dispatches
* {@link goog.editor.plugins.AbstractDialogPlugin.EventType.OPENED} after the
* dialog is shown.
* @param {string} command The command to execute.
* @param {*=} opt_arg The dialog specific argument. Should be the same as
* {@link createDialog}.
* @return {*} Always returns true, indicating the dialog was shown.
* @protected
* @override
*/
goog.editor.plugins.AbstractDialogPlugin.prototype.execCommandInternal =
function(command, opt_arg) {
// If this plugin should not reuse dialog instances, first dispose of the
// previous dialog.
if (!this.reuseDialog_) {
this.disposeDialog_();
}
// If there is no dialog yet (or we aren't reusing the previous one), create
// one.
if (!this.dialog_) {
this.dialog_ = this.createDialog(
// TODO(user): Add Field.getAppDomHelper. (Note dom helper will
// need to be updated if setAppWindow is called by clients.)
goog.dom.getDomHelper(this.getFieldObject().getAppWindow()), opt_arg);
}
// Since we're opening a dialog, we need to clear the selection because the
// focus will be going to the dialog, and if we leave an selection in the
// editor while another selection is active in the dialog as the user is
// typing, some browsers will screw up the original selection. But first we
// save it so we can restore it when the dialog closes.
// getRange may return null if there is no selection in the field.
var tempRange = this.getFieldObject().getRange();
// saveUsingDom() did not work as well as saveUsingNormalizedCarets(),
// not sure why.
this.restoreScrollPosition_ = this.saveScrollPosition();
this.savedRange_ =
tempRange && goog.editor.range.saveUsingNormalizedCarets(tempRange);
goog.dom.Range.clearSelection(
this.getFieldObject().getEditableDomHelper().getWindow());
// Listen for the dialog closing so we can clean up.
goog.events.listenOnce(
this.dialog_, goog.ui.editor.AbstractDialog.EventType.AFTER_HIDE,
this.handleAfterHide, false, this);
this.getFieldObject().setModalMode(true);
this.dialog_.show();
this.dispatchEvent(goog.editor.plugins.AbstractDialogPlugin.EventType.OPENED);
// Since the selection has left the document, dispatch a selection
// change event.
this.getFieldObject().dispatchSelectionChangeEvent();
return true;
};
/**
* Cleans up after the dialog has closed, including restoring the selection to
* what it was before the dialog was opened. If a subclass modifies the editable
* field's content such that the original selection is no longer valid (usually
* the case when the user clicks OK, and sometimes also on Cancel), it is that
* subclass' responsibility to place the selection in the desired place during
* the OK or Cancel (or other) handler. In that case, this method will leave the
* selection in place.
* @param {goog.events.Event} e The AFTER_HIDE event object.
* @protected
*/
goog.editor.plugins.AbstractDialogPlugin.prototype.handleAfterHide = function(
e) {
this.getFieldObject().setModalMode(false);
this.restoreOriginalSelection();
this.restoreScrollPosition_();
if (!this.reuseDialog_) {
this.disposeDialog_();
}
this.dispatchEvent(goog.editor.plugins.AbstractDialogPlugin.EventType.CLOSED);
// Since the selection has returned to the document, dispatch a selection
// change event.
this.getFieldObject().dispatchSelectionChangeEvent();
// When the dialog closes due to pressing enter or escape, that happens on the
// keydown event. But the browser will still fire a keyup event after that,
// which is caught by the editable field and causes it to try to fire a
// selection change event. To avoid that, we "debounce" the selection change
// event, meaning the editable field will not fire that event if the keyup
// that caused it immediately after this dialog was hidden ("immediately"
// means a small number of milliseconds defined by the editable field).
this.getFieldObject().debounceEvent(
goog.editor.Field.EventType.SELECTIONCHANGE);
};
/**
* Restores the selection in the editable field to what it was before the dialog
* was opened. This is not guaranteed to work if the contents of the field
* have changed.
* @protected
*/
goog.editor.plugins.AbstractDialogPlugin.prototype.restoreOriginalSelection =
function() {
this.getFieldObject().restoreSavedRange(this.savedRange_);
this.savedRange_ = null;
};
/**
* Cleans up the structure used to save the original selection before the dialog
* was opened. Should be used by subclasses that don't restore the original
* selection via restoreOriginalSelection.
* @protected
*/
goog.editor.plugins.AbstractDialogPlugin.prototype.disposeOriginalSelection =
function() {
if (this.savedRange_) {
this.savedRange_.dispose();
this.savedRange_ = null;
}
};
/** @override */
goog.editor.plugins.AbstractDialogPlugin.prototype.disposeInternal =
function() {
this.disposeDialog_();
goog.editor.plugins.AbstractDialogPlugin.base(this, 'disposeInternal');
};
// *** Private implementation *********************************************** //
/**
* Disposes of the dialog if needed. It is this abstract class' responsibility
* to dispose of the dialog. The "if needed" refers to the fact this method
* might be called twice (nested calls, not sequential) in the dispose flow, so
* if the dialog was already disposed once it should not be disposed again.
* @private
*/
goog.editor.plugins.AbstractDialogPlugin.prototype.disposeDialog_ = function() {
// Wrap disposing the dialog in a mutex. Otherwise disposing it would cause it
// to get hidden (if it is still open) and fire AFTER_HIDE, which in
// turn would cause the dialog to be disposed again (closure only flags an
// object as disposed after the dispose call chain completes, so it doesn't
// prevent recursive dispose calls).
if (this.dialog_ && !this.isDisposingDialog_) {
this.isDisposingDialog_ = true;
this.dialog_.dispose();
this.dialog_ = null;
this.isDisposingDialog_ = false;
}
};
|