| 1 |
// Copyright 2006 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
// All Rights Reserved. |
| 15 |
|
| 16 |
/** |
| 17 |
* @fileoverview Class to encapsulate an editable field. Always uses an |
| 18 |
* iframe to contain the editable area, never inherits the style of the |
| 19 |
* surrounding page, and is always a fixed height. |
| 20 |
* |
| 21 |
* @author [email protected] (Nick Santos) |
| 22 |
* @see ../demos/editor/editor.html |
| 23 |
* @see ../demos/editor/field_basic.html |
| 24 |
*/ |
| 25 |
|
| 26 |
goog.provide('goog.editor.Field'); |
| 27 |
goog.provide('goog.editor.Field.EventType'); |
| 28 |
|
| 29 |
goog.require('goog.a11y.aria'); |
| 30 |
goog.require('goog.a11y.aria.Role'); |
| 31 |
goog.require('goog.array'); |
| 32 |
goog.require('goog.asserts'); |
| 33 |
goog.require('goog.async.Delay'); |
| 34 |
goog.require('goog.dom'); |
| 35 |
goog.require('goog.dom.Range'); |
| 36 |
goog.require('goog.dom.TagName'); |
| 37 |
goog.require('goog.dom.classlist'); |
| 38 |
goog.require('goog.dom.safe'); |
| 39 |
goog.require('goog.editor.BrowserFeature'); |
| 40 |
goog.require('goog.editor.Command'); |
| 41 |
goog.require('goog.editor.Plugin'); |
| 42 |
goog.require('goog.editor.icontent'); |
| 43 |
goog.require('goog.editor.icontent.FieldFormatInfo'); |
| 44 |
goog.require('goog.editor.icontent.FieldStyleInfo'); |
| 45 |
goog.require('goog.editor.node'); |
| 46 |
goog.require('goog.editor.range'); |
| 47 |
goog.require('goog.events'); |
| 48 |
goog.require('goog.events.EventHandler'); |
| 49 |
goog.require('goog.events.EventTarget'); |
| 50 |
goog.require('goog.events.EventType'); |
| 51 |
goog.require('goog.events.KeyCodes'); |
| 52 |
goog.require('goog.functions'); |
| 53 |
goog.require('goog.html.SafeHtml'); |
| 54 |
goog.require('goog.html.SafeStyleSheet'); |
| 55 |
goog.require('goog.log'); |
| 56 |
goog.require('goog.log.Level'); |
| 57 |
goog.require('goog.string'); |
| 58 |
goog.require('goog.string.Unicode'); |
| 59 |
goog.require('goog.style'); |
| 60 |
goog.require('goog.userAgent'); |
| 61 |
goog.require('goog.userAgent.product'); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
/** |
| 66 |
* This class encapsulates an editable field. |
| 67 |
* |
| 68 |
* event: load Fires when the field is loaded |
| 69 |
* event: unload Fires when the field is unloaded (made not editable) |
| 70 |
* |
| 71 |
* event: beforechange Fires before the content of the field might change |
| 72 |
* |
| 73 |
* event: delayedchange Fires a short time after field has changed. If multiple |
| 74 |
* change events happen really close to each other only |
| 75 |
* the last one will trigger the delayedchange event. |
| 76 |
* |
| 77 |
* event: beforefocus Fires before the field becomes active |
| 78 |
* event: focus Fires when the field becomes active. Fires after the blur event |
| 79 |
* event: blur Fires when the field becomes inactive |
| 80 |
* |
| 81 |
* TODO: figure out if blur or beforefocus fires first in IE and make FF match |
| 82 |
* |
| 83 |
* @param {string} id An identifer for the field. This is used to find the |
| 84 |
* field and the element associated with this field. |
| 85 |
* @param {Document=} opt_doc The document that the element with the given |
| 86 |
* id can be found in. If not provided, the default document is used. |
| 87 |
* @constructor |
| 88 |
* @extends {goog.events.EventTarget} |
| 89 |
*/ |
| 90 |
goog.editor.Field = function(id, opt_doc) { |
| 91 |
goog.events.EventTarget.call(this); |
| 92 |
|
| 93 |
/** |
| 94 |
* The id for this editable field, which must match the id of the element |
| 95 |
* associated with this field. |
| 96 |
* @type {string} |
| 97 |
*/ |
| 98 |
this.id = id; |
| 99 |
|
| 100 |
/** |
| 101 |
* The hash code for this field. Should be equal to the id. |
| 102 |
* @type {string} |
| 103 |
* @private |
| 104 |
*/ |
| 105 |
this.hashCode_ = id; |
| 106 |
|
| 107 |
/** |
| 108 |
* Dom helper for the editable node. |
| 109 |
* @type {goog.dom.DomHelper} |
| 110 |
* @protected |
| 111 |
*/ |
| 112 |
this.editableDomHelper = null; |
| 113 |
|
| 114 |
/** |
| 115 |
* Map of class id to registered plugin. |
| 116 |
* @type {Object} |
| 117 |
* @private |
| 118 |
*/ |
| 119 |
this.plugins_ = {}; |
| 120 |
|
| 121 |
|
| 122 |
/** |
| 123 |
* Plugins registered on this field, indexed by the goog.editor.Plugin.Op |
| 124 |
* that they support. |
| 125 |
* @type {Object<Array<goog.editor.Plugin>>} |
| 126 |
* @private |
| 127 |
*/ |
| 128 |
this.indexedPlugins_ = {}; |
| 129 |
|
| 130 |
for (var op in goog.editor.Plugin.OPCODE) { |
| 131 |
this.indexedPlugins_[op] = []; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
/** |
| 136 |
* Additional styles to install for the editable field. |
| 137 |
* @type {!goog.html.SafeStyleSheet} |
| 138 |
* @protected |
| 139 |
*/ |
| 140 |
this.cssStyles = goog.html.SafeStyleSheet.EMPTY; |
| 141 |
|
| 142 |
// The field will not listen to change events until it has finished loading |
| 143 |
/** @private */ |
| 144 |
this.stoppedEvents_ = {}; |
| 145 |
this.stopEvent(goog.editor.Field.EventType.CHANGE); |
| 146 |
this.stopEvent(goog.editor.Field.EventType.DELAYEDCHANGE); |
| 147 |
/** @private */ |
| 148 |
this.isModified_ = false; |
| 149 |
/** @private */ |
| 150 |
this.isEverModified_ = false; |
| 151 |
/** @private */ |
| 152 |
this.delayedChangeTimer_ = new goog.async.Delay( |
| 153 |
this.dispatchDelayedChange_, goog.editor.Field.DELAYED_CHANGE_FREQUENCY, |
| 154 |
this); |
| 155 |
|
| 156 |
/** @private */ |
| 157 |
this.debouncedEvents_ = {}; |
| 158 |
for (var key in goog.editor.Field.EventType) { |
| 159 |
this.debouncedEvents_[goog.editor.Field.EventType[key]] = 0; |
| 160 |
} |
| 161 |
|
| 162 |
if (goog.editor.BrowserFeature.USE_MUTATION_EVENTS) { |
| 163 |
/** @private */ |
| 164 |
this.changeTimerGecko_ = new goog.async.Delay( |
| 165 |
this.handleChange, goog.editor.Field.CHANGE_FREQUENCY, this); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* @type {goog.events.EventHandler<!goog.editor.Field>} |
| 170 |
* @protected |
| 171 |
*/ |
| 172 |
this.eventRegister = new goog.events.EventHandler(this); |
| 173 |
|
| 174 |
// Wrappers around this field, to be disposed when the field is disposed. |
| 175 |
/** @private */ |
| 176 |
this.wrappers_ = []; |
| 177 |
|
| 178 |
/** @private */ |
| 179 |
this.loadState_ = goog.editor.Field.LoadState_.UNEDITABLE; |
| 180 |
|
| 181 |
var doc = opt_doc || document; |
| 182 |
|
| 183 |
/** |
| 184 |
* The dom helper for the node to be made editable. |
| 185 |
* @type {goog.dom.DomHelper} |
| 186 |
* @protected |
| 187 |
*/ |
| 188 |
this.originalDomHelper = goog.dom.getDomHelper(doc); |
| 189 |
|
| 190 |
/** |
| 191 |
* The original node that is being made editable, or null if it has |
| 192 |
* not yet been found. |
| 193 |
* @type {Element} |
| 194 |
* @protected |
| 195 |
*/ |
| 196 |
this.originalElement = this.originalDomHelper.getElement(this.id); |
| 197 |
|
| 198 |
/** |
| 199 |
* @private {boolean} |
| 200 |
*/ |
| 201 |
this.followLinkInNewWindow_ = |
| 202 |
goog.editor.BrowserFeature.FOLLOWS_EDITABLE_LINKS; |
| 203 |
|
| 204 |
// Default to the same window as the field is in. |
| 205 |
/** @private */ |
| 206 |
this.appWindow_ = this.originalDomHelper.getWindow(); |
| 207 |
}; |
| 208 |
goog.inherits(goog.editor.Field, goog.events.EventTarget); |
| 209 |
|
| 210 |
|
| 211 |
/** |
| 212 |
* The editable dom node. |
| 213 |
* @type {Element} |
| 214 |
* TODO(user): Make this private! |
| 215 |
*/ |
| 216 |
goog.editor.Field.prototype.field = null; |
| 217 |
|
| 218 |
|
| 219 |
/** |
| 220 |
* Logging object. |
| 221 |
* @type {goog.log.Logger} |
| 222 |
* @protected |
| 223 |
*/ |
| 224 |
goog.editor.Field.prototype.logger = goog.log.getLogger('goog.editor.Field'); |
| 225 |
|
| 226 |
|
| 227 |
/** |
| 228 |
* Event types that can be stopped/started. |
| 229 |
* @enum {string} |
| 230 |
*/ |
| 231 |
goog.editor.Field.EventType = { |
| 232 |
/** |
| 233 |
* Dispatched when the command state of the selection may have changed. This |
| 234 |
* event should be listened to for updating toolbar state. |
| 235 |
*/ |
| 236 |
COMMAND_VALUE_CHANGE: 'cvc', |
| 237 |
/** |
| 238 |
* Dispatched when the field is loaded and ready to use. |
| 239 |
*/ |
| 240 |
LOAD: 'load', |
| 241 |
/** |
| 242 |
* Dispatched when the field is fully unloaded and uneditable. |
| 243 |
*/ |
| 244 |
UNLOAD: 'unload', |
| 245 |
/** |
| 246 |
* Dispatched before the field contents are changed. |
| 247 |
*/ |
| 248 |
BEFORECHANGE: 'beforechange', |
| 249 |
/** |
| 250 |
* Dispatched when the field contents change, in FF only. |
| 251 |
* Used for internal resizing, please do not use. |
| 252 |
*/ |
| 253 |
CHANGE: 'change', |
| 254 |
/** |
| 255 |
* Dispatched on a slight delay after changes are made. |
| 256 |
* Use for autosave, or other times your app needs to know |
| 257 |
* that the field contents changed. |
| 258 |
*/ |
| 259 |
DELAYEDCHANGE: 'delayedchange', |
| 260 |
/** |
| 261 |
* Dispatched before focus in moved into the field. |
| 262 |
*/ |
| 263 |
BEFOREFOCUS: 'beforefocus', |
| 264 |
/** |
| 265 |
* Dispatched when focus is moved into the field. |
| 266 |
*/ |
| 267 |
FOCUS: 'focus', |
| 268 |
/** |
| 269 |
* Dispatched when the field is blurred. |
| 270 |
*/ |
| 271 |
BLUR: 'blur', |
| 272 |
/** |
| 273 |
* Dispatched before tab is handled by the field. This is a legacy way |
| 274 |
* of controlling tab behavior. Use trog.plugins.AbstractTabHandler now. |
| 275 |
*/ |
| 276 |
BEFORETAB: 'beforetab', |
| 277 |
/** |
| 278 |
* Dispatched after the iframe containing the field is resized, so that UI |
| 279 |
* components which contain it can respond. |
| 280 |
*/ |
| 281 |
IFRAME_RESIZED: 'ifrsz', |
| 282 |
/** |
| 283 |
* Dispatched after a user action that will eventually fire a SELECTIONCHANGE |
| 284 |
* event. For mouseups, this is fired immediately before SELECTIONCHANGE, |
| 285 |
* since {@link #handleMouseUp_} fires SELECTIONCHANGE immediately. May be |
| 286 |
* fired up to {@link #SELECTION_CHANGE_FREQUENCY_} ms before SELECTIONCHANGE |
| 287 |
* is fired in the case of keyup events, since they use |
| 288 |
* {@link #selectionChangeTimer_}. |
| 289 |
*/ |
| 290 |
BEFORESELECTIONCHANGE: 'beforeselectionchange', |
| 291 |
/** |
| 292 |
* Dispatched when the selection changes. |
| 293 |
* Use handleSelectionChange from plugin API instead of listening |
| 294 |
* directly to this event. |
| 295 |
*/ |
| 296 |
SELECTIONCHANGE: 'selectionchange' |
| 297 |
}; |
| 298 |
|
| 299 |
|
| 300 |
/** |
| 301 |
* The load state of the field. |
| 302 |
* @enum {number} |
| 303 |
* @private |
| 304 |
*/ |
| 305 |
goog.editor.Field.LoadState_ = { |
| 306 |
UNEDITABLE: 0, |
| 307 |
LOADING: 1, |
| 308 |
EDITABLE: 2 |
| 309 |
}; |
| 310 |
|
| 311 |
|
| 312 |
/** |
| 313 |
* The amount of time that a debounce blocks an event. |
| 314 |
* TODO(nicksantos): As of 9/30/07, this is only used for blocking |
| 315 |
* a keyup event after a keydown. We might need to tweak this for other |
| 316 |
* types of events. Maybe have a per-event debounce time? |
| 317 |
* @type {number} |
| 318 |
* @private |
| 319 |
*/ |
| 320 |
goog.editor.Field.DEBOUNCE_TIME_MS_ = 500; |
| 321 |
|
| 322 |
|
| 323 |
/** |
| 324 |
* There is at most one "active" field at a time. By "active" field, we mean |
| 325 |
* a field that has focus and is being used. |
| 326 |
* @type {?string} |
| 327 |
* @private |
| 328 |
*/ |
| 329 |
goog.editor.Field.activeFieldId_ = null; |
| 330 |
|
| 331 |
|
| 332 |
/** |
| 333 |
* Whether this field is in "modal interaction" mode. This usually |
| 334 |
* means that it's being edited by a dialog. |
| 335 |
* @type {boolean} |
| 336 |
* @private |
| 337 |
*/ |
| 338 |
goog.editor.Field.prototype.inModalMode_ = false; |
| 339 |
|
| 340 |
|
| 341 |
/** |
| 342 |
* The window where dialogs and bubbles should be rendered. |
| 343 |
* @type {!Window} |
| 344 |
* @private |
| 345 |
*/ |
| 346 |
goog.editor.Field.prototype.appWindow_; |
| 347 |
|
| 348 |
|
| 349 |
/** |
| 350 |
* Target node to be used when dispatching SELECTIONCHANGE asynchronously on |
| 351 |
* mouseup (to avoid IE quirk). Should be set just before starting the timer and |
| 352 |
* nulled right after consuming. |
| 353 |
* @type {Node} |
| 354 |
* @private |
| 355 |
*/ |
| 356 |
goog.editor.Field.prototype.selectionChangeTarget_; |
| 357 |
|
| 358 |
|
| 359 |
/** |
| 360 |
* Flag controlling wether to capture mouse up events on the window or not. |
| 361 |
* @type {boolean} |
| 362 |
* @private |
| 363 |
*/ |
| 364 |
goog.editor.Field.prototype.useWindowMouseUp_ = false; |
| 365 |
|
| 366 |
|
| 367 |
/** |
| 368 |
* FLag indicating the handling of a mouse event sequence. |
| 369 |
* @type {boolean} |
| 370 |
* @private |
| 371 |
*/ |
| 372 |
goog.editor.Field.prototype.waitingForMouseUp_ = false; |
| 373 |
|
| 374 |
|
| 375 |
/** |
| 376 |
* Sets the active field id. |
| 377 |
* @param {?string} fieldId The active field id. |
| 378 |
*/ |
| 379 |
goog.editor.Field.setActiveFieldId = function(fieldId) { |
| 380 |
goog.editor.Field.activeFieldId_ = fieldId; |
| 381 |
}; |
| 382 |
|
| 383 |
|
| 384 |
/** |
| 385 |
* @return {?string} The id of the active field. |
| 386 |
*/ |
| 387 |
goog.editor.Field.getActiveFieldId = function() { |
| 388 |
return goog.editor.Field.activeFieldId_; |
| 389 |
}; |
| 390 |
|
| 391 |
|
| 392 |
/** |
| 393 |
* Sets flag to control whether to use window mouse up after seeing |
| 394 |
* a mouse down operation on the field. |
| 395 |
* @param {boolean} flag True to track window mouse up. |
| 396 |
*/ |
| 397 |
goog.editor.Field.prototype.setUseWindowMouseUp = function(flag) { |
| 398 |
goog.asserts.assert( |
| 399 |
!flag || !this.usesIframe(), |
| 400 |
'procssing window mouse up should only be enabled when not using iframe'); |
| 401 |
this.useWindowMouseUp_ = flag; |
| 402 |
}; |
| 403 |
|
| 404 |
|
| 405 |
/** |
| 406 |
* @return {boolean} Whether we're in modal interaction mode. When this |
| 407 |
* returns true, another plugin is interacting with the field contents |
| 408 |
* in a synchronous way, and expects you not to make changes to |
| 409 |
* the field's DOM structure or selection. |
| 410 |
*/ |
| 411 |
goog.editor.Field.prototype.inModalMode = function() { |
| 412 |
return this.inModalMode_; |
| 413 |
}; |
| 414 |
|
| 415 |
|
| 416 |
/** |
| 417 |
* @param {boolean} inModalMode Sets whether we're in modal interaction mode. |
| 418 |
*/ |
| 419 |
goog.editor.Field.prototype.setModalMode = function(inModalMode) { |
| 420 |
this.inModalMode_ = inModalMode; |
| 421 |
}; |
| 422 |
|
| 423 |
|
| 424 |
/** |
| 425 |
* Returns a string usable as a hash code for this field. For field's |
| 426 |
* that were created with an id, the hash code is guaranteed to be the id. |
| 427 |
* TODO(user): I think we can get rid of this. Seems only used from editor. |
| 428 |
* @return {string} The hash code for this editable field. |
| 429 |
*/ |
| 430 |
goog.editor.Field.prototype.getHashCode = function() { |
| 431 |
return this.hashCode_; |
| 432 |
}; |
| 433 |
|
| 434 |
|
| 435 |
/** |
| 436 |
* Returns the editable DOM element or null if this field |
| 437 |
* is not editable. |
| 438 |
* <p>On IE or Safari this is the element with contentEditable=true |
| 439 |
* (in whitebox mode, the iFrame body). |
| 440 |
* <p>On Gecko this is the iFrame body |
| 441 |
* TODO(user): How do we word this for subclass version? |
| 442 |
* @return {Element} The editable DOM element, defined as above. |
| 443 |
*/ |
| 444 |
goog.editor.Field.prototype.getElement = function() { |
| 445 |
return this.field; |
| 446 |
}; |
| 447 |
|
| 448 |
|
| 449 |
/** |
| 450 |
* Returns original DOM element that is being made editable by Trogedit or |
| 451 |
* null if that element has not yet been found in the appropriate document. |
| 452 |
* @return {Element} The original element. |
| 453 |
*/ |
| 454 |
goog.editor.Field.prototype.getOriginalElement = function() { |
| 455 |
return this.originalElement; |
| 456 |
}; |
| 457 |
|
| 458 |
|
| 459 |
/** |
| 460 |
* Registers a keyboard event listener on the field. This is necessary for |
| 461 |
* Gecko since the fields are contained in an iFrame and there is no way to |
| 462 |
* auto-propagate key events up to the main window. |
| 463 |
* @param {string|Array<string>} type Event type to listen for or array of |
| 464 |
* event types, for example goog.events.EventType.KEYDOWN. |
| 465 |
* @param {Function} listener Function to be used as the listener. |
| 466 |
* @param {boolean=} opt_capture Whether to use capture phase (optional, |
| 467 |
* defaults to false). |
| 468 |
* @param {Object=} opt_handler Object in whose scope to call the listener. |
| 469 |
*/ |
| 470 |
goog.editor.Field.prototype.addListener = function( |
| 471 |
type, listener, opt_capture, opt_handler) { |
| 472 |
var elem = this.getElement(); |
| 473 |
// On Gecko, keyboard events only reliably fire on the document element when |
| 474 |
// using an iframe. |
| 475 |
if (goog.editor.BrowserFeature.USE_DOCUMENT_FOR_KEY_EVENTS && elem && |
| 476 |
this.usesIframe()) { |
| 477 |
elem = elem.ownerDocument; |
| 478 |
} |
| 479 |
if (opt_handler) { |
| 480 |
this.eventRegister.listenWithScope( |
| 481 |
elem, type, listener, opt_capture, opt_handler); |
| 482 |
} else { |
| 483 |
this.eventRegister.listen(elem, type, listener, opt_capture); |
| 484 |
} |
| 485 |
}; |
| 486 |
|
| 487 |
|
| 488 |
/** |
| 489 |
* Returns the registered plugin with the given classId. |
| 490 |
* @param {string} classId classId of the plugin. |
| 491 |
* @return {goog.editor.Plugin} Registered plugin with the given classId. |
| 492 |
*/ |
| 493 |
goog.editor.Field.prototype.getPluginByClassId = function(classId) { |
| 494 |
return this.plugins_[classId]; |
| 495 |
}; |
| 496 |
|
| 497 |
|
| 498 |
/** |
| 499 |
* Registers the plugin with the editable field. |
| 500 |
* @param {goog.editor.Plugin} plugin The plugin to register. |
| 501 |
*/ |
| 502 |
goog.editor.Field.prototype.registerPlugin = function(plugin) { |
| 503 |
var classId = plugin.getTrogClassId(); |
| 504 |
if (this.plugins_[classId]) { |
| 505 |
goog.log.error( |
| 506 |
this.logger, 'Cannot register the same class of plugin twice.'); |
| 507 |
} |
| 508 |
this.plugins_[classId] = plugin; |
| 509 |
|
| 510 |
// Only key events and execute should have these has* functions with a custom |
| 511 |
// handler array since they need to be very careful about performance. |
| 512 |
// The rest of the plugin hooks should be event-based. |
| 513 |
for (var op in goog.editor.Plugin.OPCODE) { |
| 514 |
var opcode = goog.editor.Plugin.OPCODE[op]; |
| 515 |
if (plugin[opcode]) { |
| 516 |
this.indexedPlugins_[op].push(plugin); |
| 517 |
} |
| 518 |
} |
| 519 |
plugin.registerFieldObject(this); |
| 520 |
|
| 521 |
// By default we enable all plugins for fields that are currently loaded. |
| 522 |
if (this.isLoaded()) { |
| 523 |
plugin.enable(this); |
| 524 |
} |
| 525 |
}; |
| 526 |
|
| 527 |
|
| 528 |
/** |
| 529 |
* Unregisters the plugin with this field. |
| 530 |
* @param {goog.editor.Plugin} plugin The plugin to unregister. |
| 531 |
*/ |
| 532 |
goog.editor.Field.prototype.unregisterPlugin = function(plugin) { |
| 533 |
var classId = plugin.getTrogClassId(); |
| 534 |
if (!this.plugins_[classId]) { |
| 535 |
goog.log.error( |
| 536 |
this.logger, 'Cannot unregister a plugin that isn\'t registered.'); |
| 537 |
} |
| 538 |
delete this.plugins_[classId]; |
| 539 |
|
| 540 |
for (var op in goog.editor.Plugin.OPCODE) { |
| 541 |
var opcode = goog.editor.Plugin.OPCODE[op]; |
| 542 |
if (plugin[opcode]) { |
| 543 |
goog.array.remove(this.indexedPlugins_[op], plugin); |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
plugin.unregisterFieldObject(this); |
| 548 |
}; |
| 549 |
|
| 550 |
|
| 551 |
/** |
| 552 |
* Sets the value that will replace the style attribute of this field's |
| 553 |
* element when the field is made non-editable. This method is called with the |
| 554 |
* current value of the style attribute when the field is made editable. |
| 555 |
* @param {string} cssText The value of the style attribute. |
| 556 |
*/ |
| 557 |
goog.editor.Field.prototype.setInitialStyle = function(cssText) { |
| 558 |
this.cssText = cssText; |
| 559 |
}; |
| 560 |
|
| 561 |
|
| 562 |
/** |
| 563 |
* Reset the properties on the original field element to how it was before |
| 564 |
* it was made editable. |
| 565 |
*/ |
| 566 |
goog.editor.Field.prototype.resetOriginalElemProperties = function() { |
| 567 |
var field = this.getOriginalElement(); |
| 568 |
field.removeAttribute('contentEditable'); |
| 569 |
field.removeAttribute('g_editable'); |
| 570 |
field.removeAttribute('role'); |
| 571 |
|
| 572 |
if (!this.id) { |
| 573 |
field.removeAttribute('id'); |
| 574 |
} else { |
| 575 |
field.id = this.id; |
| 576 |
} |
| 577 |
|
| 578 |
field.className = this.savedClassName_ || ''; |
| 579 |
|
| 580 |
var cssText = this.cssText; |
| 581 |
if (!cssText) { |
| 582 |
field.removeAttribute('style'); |
| 583 |
} else { |
| 584 |
goog.dom.setProperties(field, {'style': cssText}); |
| 585 |
} |
| 586 |
|
| 587 |
if (goog.isString(this.originalFieldLineHeight_)) { |
| 588 |
goog.style.setStyle(field, 'lineHeight', this.originalFieldLineHeight_); |
| 589 |
this.originalFieldLineHeight_ = null; |
| 590 |
} |
| 591 |
}; |
| 592 |
|
| 593 |
|
| 594 |
/** |
| 595 |
* Checks the modified state of the field. |
| 596 |
* Note: Changes that take place while the goog.editor.Field.EventType.CHANGE |
| 597 |
* event is stopped do not effect the modified state. |
| 598 |
* @param {boolean=} opt_useIsEverModified Set to true to check if the field |
| 599 |
* has ever been modified since it was created, otherwise checks if the field |
| 600 |
* has been modified since the last goog.editor.Field.EventType.DELAYEDCHANGE |
| 601 |
* event was dispatched. |
| 602 |
* @return {boolean} Whether the field has been modified. |
| 603 |
*/ |
| 604 |
goog.editor.Field.prototype.isModified = function(opt_useIsEverModified) { |
| 605 |
return opt_useIsEverModified ? this.isEverModified_ : this.isModified_; |
| 606 |
}; |
| 607 |
|
| 608 |
|
| 609 |
/** |
| 610 |
* Number of milliseconds after a change when the change event should be fired. |
| 611 |
* @type {number} |
| 612 |
*/ |
| 613 |
goog.editor.Field.CHANGE_FREQUENCY = 15; |
| 614 |
|
| 615 |
|
| 616 |
/** |
| 617 |
* Number of milliseconds between delayed change events. |
| 618 |
* @type {number} |
| 619 |
*/ |
| 620 |
goog.editor.Field.DELAYED_CHANGE_FREQUENCY = 250; |
| 621 |
|
| 622 |
|
| 623 |
/** |
| 624 |
* @return {boolean} Whether the field is implemented as an iframe. |
| 625 |
*/ |
| 626 |
goog.editor.Field.prototype.usesIframe = goog.functions.TRUE; |
| 627 |
|
| 628 |
|
| 629 |
/** |
| 630 |
* @return {boolean} Whether the field should be rendered with a fixed |
| 631 |
* height, or should expand to fit its contents. |
| 632 |
*/ |
| 633 |
goog.editor.Field.prototype.isFixedHeight = goog.functions.TRUE; |
| 634 |
|
| 635 |
|
| 636 |
/** |
| 637 |
* @return {boolean} Whether the field should be refocused on input. |
| 638 |
* This is a workaround for the iOS bug that text input doesn't work |
| 639 |
* when the main window listens touch events. |
| 640 |
*/ |
| 641 |
goog.editor.Field.prototype.shouldRefocusOnInputMobileSafari = |
| 642 |
goog.functions.FALSE; |
| 643 |
|
| 644 |
|
| 645 |
/** |
| 646 |
* Map of keyCodes (not charCodes) that cause changes in the field contents. |
| 647 |
* @type {Object} |
| 648 |
* @private |
| 649 |
*/ |
| 650 |
goog.editor.Field.KEYS_CAUSING_CHANGES_ = { |
| 651 |
46: true, // DEL |
| 652 |
8: true // BACKSPACE |
| 653 |
}; |
| 654 |
|
| 655 |
if (!goog.userAgent.IE) { |
| 656 |
// Only IE doesn't change the field by default upon tab. |
| 657 |
// TODO(user): This really isn't right now that we have tab plugins. |
| 658 |
goog.editor.Field.KEYS_CAUSING_CHANGES_[9] = true; // TAB |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
/** |
| 663 |
* Map of keyCodes (not charCodes) that when used in conjunction with the |
| 664 |
* Ctrl key cause changes in the field contents. These are the keys that are |
| 665 |
* not handled by basic formatting trogedit plugins. |
| 666 |
* @type {Object} |
| 667 |
* @private |
| 668 |
*/ |
| 669 |
goog.editor.Field.CTRL_KEYS_CAUSING_CHANGES_ = { |
| 670 |
86: true, // V |
| 671 |
88: true // X |
| 672 |
}; |
| 673 |
|
| 674 |
if (goog.userAgent.WINDOWS && !goog.userAgent.GECKO) { |
| 675 |
// In IE and Webkit, input from IME (Input Method Editor) does not generate a |
| 676 |
// keypress event so we have to rely on the keydown event. This way we have |
| 677 |
// false positives while the user is using keyboard to select the |
| 678 |
// character to input, but it is still better than the false negatives |
| 679 |
// that ignores user's final input at all. |
| 680 |
goog.editor.Field.KEYS_CAUSING_CHANGES_[229] = true; // from IME; |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
/** |
| 685 |
* Returns true if the keypress generates a change in contents. |
| 686 |
* @param {goog.events.BrowserEvent} e The event. |
| 687 |
* @param {boolean} testAllKeys True to test for all types of generating keys. |
| 688 |
* False to test for only the keys found in |
| 689 |
* goog.editor.Field.KEYS_CAUSING_CHANGES_. |
| 690 |
* @return {boolean} Whether the keypress generates a change in contents. |
| 691 |
* @private |
| 692 |
*/ |
| 693 |
goog.editor.Field.isGeneratingKey_ = function(e, testAllKeys) { |
| 694 |
if (goog.editor.Field.isSpecialGeneratingKey_(e)) { |
| 695 |
return true; |
| 696 |
} |
| 697 |
|
| 698 |
return !!( |
| 699 |
testAllKeys && !(e.ctrlKey || e.metaKey) && |
| 700 |
(!goog.userAgent.GECKO || e.charCode)); |
| 701 |
}; |
| 702 |
|
| 703 |
|
| 704 |
/** |
| 705 |
* Returns true if the keypress generates a change in the contents. |
| 706 |
* due to a special key listed in goog.editor.Field.KEYS_CAUSING_CHANGES_ |
| 707 |
* @param {goog.events.BrowserEvent} e The event. |
| 708 |
* @return {boolean} Whether the keypress generated a change in the contents. |
| 709 |
* @private |
| 710 |
*/ |
| 711 |
goog.editor.Field.isSpecialGeneratingKey_ = function(e) { |
| 712 |
var testCtrlKeys = (e.ctrlKey || e.metaKey) && |
| 713 |
e.keyCode in goog.editor.Field.CTRL_KEYS_CAUSING_CHANGES_; |
| 714 |
var testRegularKeys = !(e.ctrlKey || e.metaKey) && |
| 715 |
e.keyCode in goog.editor.Field.KEYS_CAUSING_CHANGES_; |
| 716 |
|
| 717 |
return testCtrlKeys || testRegularKeys; |
| 718 |
}; |
| 719 |
|
| 720 |
|
| 721 |
/** |
| 722 |
* Sets the application window. |
| 723 |
* @param {!Window} appWindow The window where dialogs and bubbles should be |
| 724 |
* rendered. |
| 725 |
*/ |
| 726 |
goog.editor.Field.prototype.setAppWindow = function(appWindow) { |
| 727 |
this.appWindow_ = appWindow; |
| 728 |
}; |
| 729 |
|
| 730 |
|
| 731 |
/** |
| 732 |
* Returns the "application" window, where dialogs and bubbles |
| 733 |
* should be rendered. |
| 734 |
* @return {!Window} The window. |
| 735 |
*/ |
| 736 |
goog.editor.Field.prototype.getAppWindow = function() { |
| 737 |
return this.appWindow_; |
| 738 |
}; |
| 739 |
|
| 740 |
|
| 741 |
/** |
| 742 |
* Sets the zIndex that the field should be based off of. |
| 743 |
* TODO(user): Get rid of this completely. Here for Sites. |
| 744 |
* Should this be set directly on UI plugins? |
| 745 |
* |
| 746 |
* @param {number} zindex The base zIndex of the editor. |
| 747 |
*/ |
| 748 |
goog.editor.Field.prototype.setBaseZindex = function(zindex) { |
| 749 |
this.baseZindex_ = zindex; |
| 750 |
}; |
| 751 |
|
| 752 |
|
| 753 |
/** |
| 754 |
* Returns the zindex of the base level of the field. |
| 755 |
* |
| 756 |
* @return {number} The base zindex of the editor. |
| 757 |
*/ |
| 758 |
goog.editor.Field.prototype.getBaseZindex = function() { |
| 759 |
return this.baseZindex_ || 0; |
| 760 |
}; |
| 761 |
|
| 762 |
|
| 763 |
/** |
| 764 |
* Sets up the field object and window util of this field, and enables this |
| 765 |
* editable field with all registered plugins. |
| 766 |
* This is essential to the initialization of the field. |
| 767 |
* It must be called when the field becomes fully loaded and editable. |
| 768 |
* @param {Element} field The field property. |
| 769 |
* @protected |
| 770 |
*/ |
| 771 |
goog.editor.Field.prototype.setupFieldObject = function(field) { |
| 772 |
this.loadState_ = goog.editor.Field.LoadState_.EDITABLE; |
| 773 |
this.field = field; |
| 774 |
this.editableDomHelper = goog.dom.getDomHelper(field); |
| 775 |
this.isModified_ = false; |
| 776 |
this.isEverModified_ = false; |
| 777 |
field.setAttribute('g_editable', 'true'); |
| 778 |
goog.a11y.aria.setRole(field, goog.a11y.aria.Role.TEXTBOX); |
| 779 |
}; |
| 780 |
|
| 781 |
|
| 782 |
/** |
| 783 |
* Help make the field not editable by setting internal data structures to null, |
| 784 |
* and disabling this field with all registered plugins. |
| 785 |
* @private |
| 786 |
*/ |
| 787 |
goog.editor.Field.prototype.tearDownFieldObject_ = function() { |
| 788 |
this.loadState_ = goog.editor.Field.LoadState_.UNEDITABLE; |
| 789 |
|
| 790 |
for (var classId in this.plugins_) { |
| 791 |
var plugin = this.plugins_[classId]; |
| 792 |
if (!plugin.activeOnUneditableFields()) { |
| 793 |
plugin.disable(this); |
| 794 |
} |
| 795 |
} |
| 796 |
|
| 797 |
this.field = null; |
| 798 |
this.editableDomHelper = null; |
| 799 |
}; |
| 800 |
|
| 801 |
|
| 802 |
/** |
| 803 |
* Initialize listeners on the field. |
| 804 |
* @private |
| 805 |
*/ |
| 806 |
goog.editor.Field.prototype.setupChangeListeners_ = function() { |
| 807 |
if ((goog.userAgent.product.IPHONE || goog.userAgent.product.IPAD) && |
| 808 |
this.usesIframe() && this.shouldRefocusOnInputMobileSafari()) { |
| 809 |
// This is a workaround for the iOS bug that text input doesn't work |
| 810 |
// when the main window listens touch events. |
| 811 |
var editWindow = this.getEditableDomHelper().getWindow(); |
| 812 |
this.boundRefocusListenerMobileSafari_ = |
| 813 |
goog.bind(editWindow.focus, editWindow); |
| 814 |
editWindow.addEventListener( |
| 815 |
goog.events.EventType.KEYDOWN, this.boundRefocusListenerMobileSafari_, |
| 816 |
false); |
| 817 |
editWindow.addEventListener( |
| 818 |
goog.events.EventType.TOUCHEND, this.boundRefocusListenerMobileSafari_, |
| 819 |
false); |
| 820 |
} |
| 821 |
if (goog.userAgent.OPERA && this.usesIframe()) { |
| 822 |
// We can't use addListener here because we need to listen on the window, |
| 823 |
// and removing listeners on window objects from the event register throws |
| 824 |
// an exception if the window is closed. |
| 825 |
this.boundFocusListenerOpera_ = |
| 826 |
goog.bind(this.dispatchFocusAndBeforeFocus_, this); |
| 827 |
this.boundBlurListenerOpera_ = goog.bind(this.dispatchBlur, this); |
| 828 |
var editWindow = this.getEditableDomHelper().getWindow(); |
| 829 |
editWindow.addEventListener( |
| 830 |
goog.events.EventType.FOCUS, this.boundFocusListenerOpera_, false); |
| 831 |
editWindow.addEventListener( |
| 832 |
goog.events.EventType.BLUR, this.boundBlurListenerOpera_, false); |
| 833 |
} else { |
| 834 |
if (goog.editor.BrowserFeature.SUPPORTS_FOCUSIN) { |
| 835 |
this.addListener(goog.events.EventType.FOCUS, this.dispatchFocus_); |
| 836 |
this.addListener( |
| 837 |
goog.events.EventType.FOCUSIN, this.dispatchBeforeFocus_); |
| 838 |
} else { |
| 839 |
this.addListener( |
| 840 |
goog.events.EventType.FOCUS, this.dispatchFocusAndBeforeFocus_); |
| 841 |
} |
| 842 |
this.addListener( |
| 843 |
goog.events.EventType.BLUR, this.dispatchBlur, |
| 844 |
goog.editor.BrowserFeature.USE_MUTATION_EVENTS); |
| 845 |
} |
| 846 |
|
| 847 |
if (goog.editor.BrowserFeature.USE_MUTATION_EVENTS) { |
| 848 |
// Ways to detect changes in Mozilla: |
| 849 |
// |
| 850 |
// keypress - check event.charCode (only typable characters has a |
| 851 |
// charCode), but also keyboard commands lile Ctrl+C will |
| 852 |
// return a charCode. |
| 853 |
// dragdrop - fires when the user drops something. This does not necessary |
| 854 |
// lead to a change but we cannot detect if it will or not |
| 855 |
// |
| 856 |
// Known Issues: We cannot detect cut and paste using menus |
| 857 |
// We cannot detect when someone moves something out of the |
| 858 |
// field using drag and drop. |
| 859 |
// |
| 860 |
this.setupMutationEventHandlersGecko(); |
| 861 |
} else { |
| 862 |
// Ways to detect that a change is about to happen in other browsers. |
| 863 |
// (IE and Safari have these events. Opera appears to work, but we haven't |
| 864 |
// researched it.) |
| 865 |
// |
| 866 |
// onbeforepaste |
| 867 |
// onbeforecut |
| 868 |
// ondrop - happens when the user drops something on the editable text |
| 869 |
// field the value at this time does not contain the dropped text |
| 870 |
// ondragleave - when the user drags something from the current document. |
| 871 |
// This might not cause a change if the action was copy |
| 872 |
// instead of move |
| 873 |
// onkeypress - IE only fires keypress events if the key will generate |
| 874 |
// output. It will not trigger for delete and backspace |
| 875 |
// onkeydown - For delete and backspace |
| 876 |
// |
| 877 |
// known issues: IE triggers beforepaste just by opening the edit menu |
| 878 |
// delete at the end should not cause beforechange |
| 879 |
// backspace at the beginning should not cause beforechange |
| 880 |
// see above in ondragleave |
| 881 |
// TODO(user): Why don't we dispatchBeforeChange from the |
| 882 |
// handleDrop event for all browsers? |
| 883 |
this.addListener( |
| 884 |
['beforecut', 'beforepaste', 'drop', 'dragend'], |
| 885 |
this.dispatchBeforeChange); |
| 886 |
this.addListener( |
| 887 |
['cut', 'paste'], goog.functions.lock(this.dispatchChange)); |
| 888 |
this.addListener('drop', this.handleDrop_); |
| 889 |
} |
| 890 |
|
| 891 |
// TODO(user): Figure out why we use dragend vs dragdrop and |
| 892 |
// document this better. |
| 893 |
var dropEventName = goog.userAgent.WEBKIT ? 'dragend' : 'dragdrop'; |
| 894 |
this.addListener(dropEventName, this.handleDrop_); |
| 895 |
|
| 896 |
this.addListener(goog.events.EventType.KEYDOWN, this.handleKeyDown_); |
| 897 |
this.addListener(goog.events.EventType.KEYPRESS, this.handleKeyPress_); |
| 898 |
this.addListener(goog.events.EventType.KEYUP, this.handleKeyUp_); |
| 899 |
|
| 900 |
this.selectionChangeTimer_ = new goog.async.Delay( |
| 901 |
this.handleSelectionChangeTimer_, |
| 902 |
goog.editor.Field.SELECTION_CHANGE_FREQUENCY_, this); |
| 903 |
|
| 904 |
if (this.followLinkInNewWindow_) { |
| 905 |
this.addListener( |
| 906 |
goog.events.EventType.CLICK, goog.editor.Field.cancelLinkClick_); |
| 907 |
} |
| 908 |
|
| 909 |
this.addListener(goog.events.EventType.MOUSEDOWN, this.handleMouseDown_); |
| 910 |
if (this.useWindowMouseUp_) { |
| 911 |
this.eventRegister.listen( |
| 912 |
this.editableDomHelper.getDocument(), goog.events.EventType.MOUSEUP, |
| 913 |
this.handleMouseUp_); |
| 914 |
this.addListener(goog.events.EventType.DRAGSTART, this.handleDragStart_); |
| 915 |
} else { |
| 916 |
this.addListener(goog.events.EventType.MOUSEUP, this.handleMouseUp_); |
| 917 |
} |
| 918 |
}; |
| 919 |
|
| 920 |
|
| 921 |
/** |
| 922 |
* Frequency to check for selection changes. |
| 923 |
* @type {number} |
| 924 |
* @private |
| 925 |
*/ |
| 926 |
goog.editor.Field.SELECTION_CHANGE_FREQUENCY_ = 250; |
| 927 |
|
| 928 |
|
| 929 |
/** |
| 930 |
* Stops all listeners and timers. |
| 931 |
* @protected |
| 932 |
*/ |
| 933 |
goog.editor.Field.prototype.clearListeners = function() { |
| 934 |
if (this.eventRegister) { |
| 935 |
this.eventRegister.removeAll(); |
| 936 |
} |
| 937 |
|
| 938 |
if ((goog.userAgent.product.IPHONE || goog.userAgent.product.IPAD) && |
| 939 |
this.usesIframe() && this.shouldRefocusOnInputMobileSafari()) { |
| 940 |
try { |
| 941 |
var editWindow = this.getEditableDomHelper().getWindow(); |
| 942 |
editWindow.removeEventListener( |
| 943 |
goog.events.EventType.KEYDOWN, this.boundRefocusListenerMobileSafari_, |
| 944 |
false); |
| 945 |
editWindow.removeEventListener( |
| 946 |
goog.events.EventType.TOUCHEND, |
| 947 |
this.boundRefocusListenerMobileSafari_, false); |
| 948 |
} catch (e) { |
| 949 |
// The editWindow no longer exists, or has been navigated to a different- |
| 950 |
// origin URL. Either way, the event listeners have already been removed |
| 951 |
// for us. |
| 952 |
} |
| 953 |
delete this.boundRefocusListenerMobileSafari_; |
| 954 |
} |
| 955 |
if (goog.userAgent.OPERA && this.usesIframe()) { |
| 956 |
try { |
| 957 |
var editWindow = this.getEditableDomHelper().getWindow(); |
| 958 |
editWindow.removeEventListener( |
| 959 |
goog.events.EventType.FOCUS, this.boundFocusListenerOpera_, false); |
| 960 |
editWindow.removeEventListener( |
| 961 |
goog.events.EventType.BLUR, this.boundBlurListenerOpera_, false); |
| 962 |
} catch (e) { |
| 963 |
// The editWindow no longer exists, or has been navigated to a different- |
| 964 |
// origin URL. Either way, the event listeners have already been removed |
| 965 |
// for us. |
| 966 |
} |
| 967 |
delete this.boundFocusListenerOpera_; |
| 968 |
delete this.boundBlurListenerOpera_; |
| 969 |
} |
| 970 |
|
| 971 |
if (this.changeTimerGecko_) { |
| 972 |
this.changeTimerGecko_.stop(); |
| 973 |
} |
| 974 |
this.delayedChangeTimer_.stop(); |
| 975 |
}; |
| 976 |
|
| 977 |
|
| 978 |
/** @override */ |
| 979 |
goog.editor.Field.prototype.disposeInternal = function() { |
| 980 |
if (this.isLoading() || this.isLoaded()) { |
| 981 |
goog.log.warning(this.logger, 'Disposing a field that is in use.'); |
| 982 |
} |
| 983 |
|
| 984 |
if (this.getOriginalElement()) { |
| 985 |
this.execCommand(goog.editor.Command.CLEAR_LOREM); |
| 986 |
} |
| 987 |
|
| 988 |
this.tearDownFieldObject_(); |
| 989 |
this.clearListeners(); |
| 990 |
this.clearFieldLoadListener_(); |
| 991 |
this.originalDomHelper = null; |
| 992 |
|
| 993 |
if (this.eventRegister) { |
| 994 |
this.eventRegister.dispose(); |
| 995 |
this.eventRegister = null; |
| 996 |
} |
| 997 |
|
| 998 |
this.removeAllWrappers(); |
| 999 |
|
| 1000 |
if (goog.editor.Field.getActiveFieldId() == this.id) { |
| 1001 |
goog.editor.Field.setActiveFieldId(null); |
| 1002 |
} |
| 1003 |
|
| 1004 |
for (var classId in this.plugins_) { |
| 1005 |
var plugin = this.plugins_[classId]; |
| 1006 |
if (plugin.isAutoDispose()) { |
| 1007 |
plugin.dispose(); |
| 1008 |
} |
| 1009 |
} |
| 1010 |
delete (this.plugins_); |
| 1011 |
|
| 1012 |
goog.editor.Field.superClass_.disposeInternal.call(this); |
| 1013 |
}; |
| 1014 |
|
| 1015 |
|
| 1016 |
/** |
| 1017 |
* Attach an wrapper to this field, to be thrown out when the field |
| 1018 |
* is disposed. |
| 1019 |
* @param {goog.Disposable} wrapper The wrapper to attach. |
| 1020 |
*/ |
| 1021 |
goog.editor.Field.prototype.attachWrapper = function(wrapper) { |
| 1022 |
this.wrappers_.push(wrapper); |
| 1023 |
}; |
| 1024 |
|
| 1025 |
|
| 1026 |
/** |
| 1027 |
* Removes all wrappers and destroys them. |
| 1028 |
*/ |
| 1029 |
goog.editor.Field.prototype.removeAllWrappers = function() { |
| 1030 |
var wrapper; |
| 1031 |
while (wrapper = this.wrappers_.pop()) { |
| 1032 |
wrapper.dispose(); |
| 1033 |
} |
| 1034 |
}; |
| 1035 |
|
| 1036 |
|
| 1037 |
/** |
| 1038 |
* Sets whether activating a hyperlink in this editable field will open a new |
| 1039 |
* window or not. |
| 1040 |
* @param {boolean} followLinkInNewWindow |
| 1041 |
*/ |
| 1042 |
goog.editor.Field.prototype.setFollowLinkInNewWindow = function( |
| 1043 |
followLinkInNewWindow) { |
| 1044 |
this.followLinkInNewWindow_ = followLinkInNewWindow; |
| 1045 |
}; |
| 1046 |
|
| 1047 |
|
| 1048 |
/** |
| 1049 |
* List of mutation events in Gecko browsers. |
| 1050 |
* @type {Array<string>} |
| 1051 |
* @protected |
| 1052 |
*/ |
| 1053 |
goog.editor.Field.MUTATION_EVENTS_GECKO = [ |
| 1054 |
'DOMNodeInserted', 'DOMNodeRemoved', 'DOMNodeRemovedFromDocument', |
| 1055 |
'DOMNodeInsertedIntoDocument', 'DOMCharacterDataModified' |
| 1056 |
]; |
| 1057 |
|
| 1058 |
|
| 1059 |
/** |
| 1060 |
* Mutation events tell us when something has changed for mozilla. |
| 1061 |
* @protected |
| 1062 |
*/ |
| 1063 |
goog.editor.Field.prototype.setupMutationEventHandlersGecko = function() { |
| 1064 |
// Always use DOMSubtreeModified on Gecko when not using an iframe so that |
| 1065 |
// DOM mutations outside the Field do not trigger handleMutationEventGecko_. |
| 1066 |
if (goog.editor.BrowserFeature.HAS_DOM_SUBTREE_MODIFIED_EVENT || |
| 1067 |
!this.usesIframe()) { |
| 1068 |
this.eventRegister.listen( |
| 1069 |
this.getElement(), 'DOMSubtreeModified', |
| 1070 |
this.handleMutationEventGecko_); |
| 1071 |
} else { |
| 1072 |
var doc = this.getEditableDomHelper().getDocument(); |
| 1073 |
this.eventRegister.listen( |
| 1074 |
doc, goog.editor.Field.MUTATION_EVENTS_GECKO, |
| 1075 |
this.handleMutationEventGecko_, true); |
| 1076 |
|
| 1077 |
// DOMAttrModified fires for a lot of events we want to ignore. This goes |
| 1078 |
// through a different handler so that we can ignore many of these. |
| 1079 |
this.eventRegister.listen( |
| 1080 |
doc, 'DOMAttrModified', |
| 1081 |
goog.bind( |
| 1082 |
this.handleDomAttrChange, this, this.handleMutationEventGecko_), |
| 1083 |
true); |
| 1084 |
} |
| 1085 |
}; |
| 1086 |
|
| 1087 |
|
| 1088 |
/** |
| 1089 |
* Handle before change key events and fire the beforetab event if appropriate. |
| 1090 |
* This needs to happen on keydown in IE and keypress in FF. |
| 1091 |
* @param {goog.events.BrowserEvent} e The browser event. |
| 1092 |
* @return {boolean} Whether to still perform the default key action. Only set |
| 1093 |
* to true if the actual event has already been canceled. |
| 1094 |
* @private |
| 1095 |
*/ |
| 1096 |
goog.editor.Field.prototype.handleBeforeChangeKeyEvent_ = function(e) { |
| 1097 |
// There are two reasons to block a key: |
| 1098 |
var block = |
| 1099 |
// #1: to intercept a tab |
| 1100 |
// TODO: possibly don't allow clients to intercept tabs outside of LIs and |
| 1101 |
// maybe tables as well? |
| 1102 |
(e.keyCode == goog.events.KeyCodes.TAB && !this.dispatchBeforeTab_(e)) || |
| 1103 |
// #2: to block a Firefox-specific bug where Macs try to navigate |
| 1104 |
// back a page when you hit command+left arrow or comamnd-right arrow. |
| 1105 |
// See https://bugzilla.mozilla.org/show_bug.cgi?id=341886 |
| 1106 |
// This was fixed in Firefox 29, but still exists in older versions. |
| 1107 |
(goog.userAgent.GECKO && e.metaKey && |
| 1108 |
!goog.userAgent.isVersionOrHigher(29) && |
| 1109 |
(e.keyCode == goog.events.KeyCodes.LEFT || |
| 1110 |
e.keyCode == goog.events.KeyCodes.RIGHT)); |
| 1111 |
|
| 1112 |
if (block) { |
| 1113 |
e.preventDefault(); |
| 1114 |
return false; |
| 1115 |
} else { |
| 1116 |
// In Gecko we have both keyCode and charCode. charCode is for human |
| 1117 |
// readable characters like a, b and c. However pressing ctrl+c and so on |
| 1118 |
// also causes charCode to be set. |
| 1119 |
|
| 1120 |
// TODO(arv): Del at end of field or backspace at beginning should be |
| 1121 |
// ignored. |
| 1122 |
this.gotGeneratingKey_ = e.charCode || |
| 1123 |
goog.editor.Field.isGeneratingKey_(e, goog.userAgent.GECKO); |
| 1124 |
if (this.gotGeneratingKey_) { |
| 1125 |
this.dispatchBeforeChange(); |
| 1126 |
// TODO(robbyw): Should we return the value of the above? |
| 1127 |
} |
| 1128 |
} |
| 1129 |
|
| 1130 |
return true; |
| 1131 |
}; |
| 1132 |
|
| 1133 |
|
| 1134 |
/** |
| 1135 |
* Keycodes that result in a selectionchange event (e.g. the cursor moving). |
| 1136 |
* @type {!Object<number, number>} |
| 1137 |
*/ |
| 1138 |
goog.editor.Field.SELECTION_CHANGE_KEYCODES = { |
| 1139 |
8: 1, // backspace |
| 1140 |
9: 1, // tab |
| 1141 |
13: 1, // enter |
| 1142 |
33: 1, // page up |
| 1143 |
34: 1, // page down |
| 1144 |
35: 1, // end |
| 1145 |
36: 1, // home |
| 1146 |
37: 1, // left |
| 1147 |
38: 1, // up |
| 1148 |
39: 1, // right |
| 1149 |
40: 1, // down |
| 1150 |
46: 1 // delete |
| 1151 |
}; |
| 1152 |
|
| 1153 |
|
| 1154 |
/** |
| 1155 |
* Map of keyCodes (not charCodes) that when used in conjunction with the |
| 1156 |
* Ctrl key cause selection changes in the field contents. These are the keys |
| 1157 |
* that are not handled by the basic formatting trogedit plugins. Note that |
| 1158 |
* combinations like Ctrl-left etc are already handled in |
| 1159 |
* SELECTION_CHANGE_KEYCODES |
| 1160 |
* @type {Object} |
| 1161 |
* @private |
| 1162 |
*/ |
| 1163 |
goog.editor.Field.CTRL_KEYS_CAUSING_SELECTION_CHANGES_ = { |
| 1164 |
65: true, // A |
| 1165 |
86: true, // V |
| 1166 |
88: true // X |
| 1167 |
}; |
| 1168 |
|
| 1169 |
|
| 1170 |
/** |
| 1171 |
* Map of keyCodes (not charCodes) that might need to be handled as a keyboard |
| 1172 |
* shortcut (even when ctrl/meta key is not pressed) by some plugin. Currently |
| 1173 |
* it is a small list. If it grows too big we can optimize it by using ranges |
| 1174 |
* or extending it from SELECTION_CHANGE_KEYCODES |
| 1175 |
* @type {Object} |
| 1176 |
* @private |
| 1177 |
*/ |
| 1178 |
goog.editor.Field.POTENTIAL_SHORTCUT_KEYCODES_ = { |
| 1179 |
8: 1, // backspace |
| 1180 |
9: 1, // tab |
| 1181 |
13: 1, // enter |
| 1182 |
27: 1, // esc |
| 1183 |
33: 1, // page up |
| 1184 |
34: 1, // page down |
| 1185 |
37: 1, // left |
| 1186 |
38: 1, // up |
| 1187 |
39: 1, // right |
| 1188 |
40: 1 // down |
| 1189 |
}; |
| 1190 |
|
| 1191 |
|
| 1192 |
/** |
| 1193 |
* Calls all the plugins of the given operation, in sequence, with the |
| 1194 |
* given arguments. This is short-circuiting: once one plugin cancels |
| 1195 |
* the event, no more plugins will be invoked. |
| 1196 |
* @param {goog.editor.Plugin.Op} op A plugin op. |
| 1197 |
* @param {...*} var_args The arguments to the plugin. |
| 1198 |
* @return {boolean} True if one of the plugins cancel the event, false |
| 1199 |
* otherwise. |
| 1200 |
* @private |
| 1201 |
*/ |
| 1202 |
goog.editor.Field.prototype.invokeShortCircuitingOp_ = function(op, var_args) { |
| 1203 |
var plugins = this.indexedPlugins_[op]; |
| 1204 |
var argList = goog.array.slice(arguments, 1); |
| 1205 |
for (var i = 0; i < plugins.length; ++i) { |
| 1206 |
// If the plugin returns true, that means it handled the event and |
| 1207 |
// we shouldn't propagate to the other plugins. |
| 1208 |
var plugin = plugins[i]; |
| 1209 |
if ((plugin.isEnabled(this) || goog.editor.Plugin.IRREPRESSIBLE_OPS[op]) && |
| 1210 |
plugin[goog.editor.Plugin.OPCODE[op]].apply(plugin, argList)) { |
| 1211 |
// Only one plugin is allowed to handle the event. If for some reason |
| 1212 |
// a plugin wants to handle it and still allow other plugins to handle |
| 1213 |
// it, it shouldn't return true. |
| 1214 |
return true; |
| 1215 |
} |
| 1216 |
} |
| 1217 |
|
| 1218 |
return false; |
| 1219 |
}; |
| 1220 |
|
| 1221 |
|
| 1222 |
/** |
| 1223 |
* Invoke this operation on all plugins with the given arguments. |
| 1224 |
* @param {goog.editor.Plugin.Op} op A plugin op. |
| 1225 |
* @param {...*} var_args The arguments to the plugin. |
| 1226 |
* @private |
| 1227 |
*/ |
| 1228 |
goog.editor.Field.prototype.invokeOp_ = function(op, var_args) { |
| 1229 |
var plugins = this.indexedPlugins_[op]; |
| 1230 |
var argList = goog.array.slice(arguments, 1); |
| 1231 |
for (var i = 0; i < plugins.length; ++i) { |
| 1232 |
var plugin = plugins[i]; |
| 1233 |
if (plugin.isEnabled(this) || goog.editor.Plugin.IRREPRESSIBLE_OPS[op]) { |
| 1234 |
plugin[goog.editor.Plugin.OPCODE[op]].apply(plugin, argList); |
| 1235 |
} |
| 1236 |
} |
| 1237 |
}; |
| 1238 |
|
| 1239 |
|
| 1240 |
/** |
| 1241 |
* Reduce this argument over all plugins. The result of each plugin invocation |
| 1242 |
* will be passed to the next plugin invocation. See goog.array.reduce. |
| 1243 |
* @param {goog.editor.Plugin.Op} op A plugin op. |
| 1244 |
* @param {string} arg The argument to reduce. For now, we assume it's a |
| 1245 |
* string, but we should widen this later if there are reducing |
| 1246 |
* plugins that don't operate on strings. |
| 1247 |
* @param {...*} var_args Any extra arguments to pass to the plugin. These args |
| 1248 |
* will not be reduced. |
| 1249 |
* @return {string} The reduced argument. |
| 1250 |
* @private |
| 1251 |
*/ |
| 1252 |
goog.editor.Field.prototype.reduceOp_ = function(op, arg, var_args) { |
| 1253 |
var plugins = this.indexedPlugins_[op]; |
| 1254 |
var argList = goog.array.slice(arguments, 1); |
| 1255 |
for (var i = 0; i < plugins.length; ++i) { |
| 1256 |
var plugin = plugins[i]; |
| 1257 |
if (plugin.isEnabled(this) || goog.editor.Plugin.IRREPRESSIBLE_OPS[op]) { |
| 1258 |
argList[0] = plugin[goog.editor.Plugin.OPCODE[op]].apply(plugin, argList); |
| 1259 |
} |
| 1260 |
} |
| 1261 |
return argList[0]; |
| 1262 |
}; |
| 1263 |
|
| 1264 |
|
| 1265 |
/** |
| 1266 |
* Prepare the given contents, then inject them into the editable field. |
| 1267 |
* @param {?string} contents The contents to prepare. |
| 1268 |
* @param {Element} field The field element. |
| 1269 |
* @protected |
| 1270 |
*/ |
| 1271 |
goog.editor.Field.prototype.injectContents = function(contents, field) { |
| 1272 |
var styles = {}; |
| 1273 |
var newHtml = this.getInjectableContents(contents, styles); |
| 1274 |
goog.style.setStyle(field, styles); |
| 1275 |
goog.editor.node.replaceInnerHtml(field, newHtml); |
| 1276 |
}; |
| 1277 |
|
| 1278 |
|
| 1279 |
/** |
| 1280 |
* Returns prepared contents that can be injected into the editable field. |
| 1281 |
* @param {?string} contents The contents to prepare. |
| 1282 |
* @param {Object} styles A map that will be populated with styles that should |
| 1283 |
* be applied to the field element together with the contents. |
| 1284 |
* @return {string} The prepared contents. |
| 1285 |
*/ |
| 1286 |
goog.editor.Field.prototype.getInjectableContents = function(contents, styles) { |
| 1287 |
return this.reduceOp_( |
| 1288 |
goog.editor.Plugin.Op.PREPARE_CONTENTS_HTML, contents || '', styles); |
| 1289 |
}; |
| 1290 |
|
| 1291 |
|
| 1292 |
/** |
| 1293 |
* Handles keydown on the field. |
| 1294 |
* @param {goog.events.BrowserEvent} e The browser event. |
| 1295 |
* @private |
| 1296 |
*/ |
| 1297 |
goog.editor.Field.prototype.handleKeyDown_ = function(e) { |
| 1298 |
// Mac only fires Cmd+A for keydown, not keyup: b/22407515. |
| 1299 |
if (goog.userAgent.MAC && e.keyCode == goog.events.KeyCodes.A) { |
| 1300 |
this.maybeStartSelectionChangeTimer_(e); |
| 1301 |
} |
| 1302 |
|
| 1303 |
if (!goog.editor.BrowserFeature.USE_MUTATION_EVENTS) { |
| 1304 |
if (!this.handleBeforeChangeKeyEvent_(e)) { |
| 1305 |
return; |
| 1306 |
} |
| 1307 |
} |
| 1308 |
|
| 1309 |
if (!this.invokeShortCircuitingOp_(goog.editor.Plugin.Op.KEYDOWN, e) && |
| 1310 |
goog.editor.BrowserFeature.USES_KEYDOWN) { |
| 1311 |
this.handleKeyboardShortcut_(e); |
| 1312 |
} |
| 1313 |
}; |
| 1314 |
|
| 1315 |
|
| 1316 |
/** |
| 1317 |
* Handles keypress on the field. |
| 1318 |
* @param {goog.events.BrowserEvent} e The browser event. |
| 1319 |
* @private |
| 1320 |
*/ |
| 1321 |
goog.editor.Field.prototype.handleKeyPress_ = function(e) { |
| 1322 |
if (goog.editor.BrowserFeature.USE_MUTATION_EVENTS) { |
| 1323 |
if (!this.handleBeforeChangeKeyEvent_(e)) { |
| 1324 |
return; |
| 1325 |
} |
| 1326 |
} else { |
| 1327 |
// In IE only keys that generate output trigger keypress |
| 1328 |
// In Mozilla charCode is set for keys generating content. |
| 1329 |
this.gotGeneratingKey_ = true; |
| 1330 |
this.dispatchBeforeChange(); |
| 1331 |
} |
| 1332 |
|
| 1333 |
if (!this.invokeShortCircuitingOp_(goog.editor.Plugin.Op.KEYPRESS, e) && |
| 1334 |
!goog.editor.BrowserFeature.USES_KEYDOWN) { |
| 1335 |
this.handleKeyboardShortcut_(e); |
| 1336 |
} |
| 1337 |
}; |
| 1338 |
|
| 1339 |
|
| 1340 |
/** |
| 1341 |
* Handles keyup on the field. |
| 1342 |
* @param {!goog.events.BrowserEvent} e The browser event. |
| 1343 |
* @private |
| 1344 |
*/ |
| 1345 |
goog.editor.Field.prototype.handleKeyUp_ = function(e) { |
| 1346 |
if (!goog.editor.BrowserFeature.USE_MUTATION_EVENTS && |
| 1347 |
(this.gotGeneratingKey_ || |
| 1348 |
goog.editor.Field.isSpecialGeneratingKey_(e))) { |
| 1349 |
// The special keys won't have set the gotGeneratingKey flag, so we check |
| 1350 |
// for them explicitly |
| 1351 |
this.handleChange(); |
| 1352 |
} |
| 1353 |
|
| 1354 |
this.invokeShortCircuitingOp_(goog.editor.Plugin.Op.KEYUP, e); |
| 1355 |
this.maybeStartSelectionChangeTimer_(e); |
| 1356 |
}; |
| 1357 |
|
| 1358 |
|
| 1359 |
/** |
| 1360 |
* Fires {@code BEFORESELECTIONCHANGE} and starts the selection change timer |
| 1361 |
* (which will fire {@code SELECTIONCHANGE}) if the given event is a key event |
| 1362 |
* that causes a selection change. |
| 1363 |
* @param {!goog.events.BrowserEvent} e The browser event. |
| 1364 |
* @private |
| 1365 |
*/ |
| 1366 |
goog.editor.Field.prototype.maybeStartSelectionChangeTimer_ = function(e) { |
| 1367 |
if (this.isEventStopped(goog.editor.Field.EventType.SELECTIONCHANGE)) { |
| 1368 |
return; |
| 1369 |
} |
| 1370 |
|
| 1371 |
if (goog.editor.Field.SELECTION_CHANGE_KEYCODES[e.keyCode] || |
| 1372 |
((e.ctrlKey || e.metaKey) && |
| 1373 |
goog.editor.Field.CTRL_KEYS_CAUSING_SELECTION_CHANGES_[e.keyCode])) { |
| 1374 |
this.dispatchEvent(goog.editor.Field.EventType.BEFORESELECTIONCHANGE); |
| 1375 |
this.selectionChangeTimer_.start(); |
| 1376 |
} |
| 1377 |
}; |
| 1378 |
|
| 1379 |
|
| 1380 |
/** |
| 1381 |
* Handles keyboard shortcuts on the field. Note that we bake this into our |
| 1382 |
* handleKeyPress/handleKeyDown rather than using goog.events.KeyHandler or |
| 1383 |
* goog.ui.KeyboardShortcutHandler for performance reasons. Since these |
| 1384 |
* are handled on every key stroke, we do not want to be going out to the |
| 1385 |
* event system every time. |
| 1386 |
* @param {goog.events.BrowserEvent} e The browser event. |
| 1387 |
* @private |
| 1388 |
*/ |
| 1389 |
goog.editor.Field.prototype.handleKeyboardShortcut_ = function(e) { |
| 1390 |
// Alt key is used for i18n languages to enter certain characters. like |
| 1391 |
// control + alt + z (used for IMEs) and control + alt + s for Polish. |
| 1392 |
// So we don't invoke handleKeyboardShortcut at all for alt keys. |
| 1393 |
if (e.altKey) { |
| 1394 |
return; |
| 1395 |
} |
| 1396 |
|
| 1397 |
var isModifierPressed = goog.userAgent.MAC ? e.metaKey : e.ctrlKey; |
| 1398 |
if (isModifierPressed || |
| 1399 |
goog.editor.Field.POTENTIAL_SHORTCUT_KEYCODES_[e.keyCode]) { |
| 1400 |
// TODO(user): goog.events.KeyHandler uses much more complicated logic |
| 1401 |
// to determine key. Consider changing to what they do. |
| 1402 |
var key = e.charCode || e.keyCode; |
| 1403 |
|
| 1404 |
if (key == 17) { // Ctrl key |
| 1405 |
// In IE and Webkit pressing Ctrl key itself results in this event. |
| 1406 |
return; |
| 1407 |
} |
| 1408 |
|
| 1409 |
var stringKey = String.fromCharCode(key).toLowerCase(); |
| 1410 |
// Ctrl+Cmd+Space generates a charCode for a backtick on Mac Firefox, but |
| 1411 |
// has the correct string key in the browser event. |
| 1412 |
if (goog.userAgent.MAC && goog.userAgent.GECKO && stringKey == '`' && |
| 1413 |
e.getBrowserEvent().key == ' ') { |
| 1414 |
stringKey = ' '; |
| 1415 |
} |
| 1416 |
// Converting the keyCode for "\" using fromCharCode creates "u", so we need |
| 1417 |
// to look out for it specifically. |
| 1418 |
if (e.keyCode == goog.events.KeyCodes.BACKSLASH) { |
| 1419 |
stringKey = '\\'; |
| 1420 |
} |
| 1421 |
|
| 1422 |
if (this.invokeShortCircuitingOp_( |
| 1423 |
goog.editor.Plugin.Op.SHORTCUT, e, stringKey, isModifierPressed)) { |
| 1424 |
e.preventDefault(); |
| 1425 |
// We don't call stopPropagation as some other handler outside of |
| 1426 |
// trogedit might need it. |
| 1427 |
} |
| 1428 |
} |
| 1429 |
}; |
| 1430 |
|
| 1431 |
|
| 1432 |
/** |
| 1433 |
* Executes an editing command as per the registered plugins. |
| 1434 |
* @param {string} command The command to execute. |
| 1435 |
* @param {...*} var_args Any additional parameters needed to execute the |
| 1436 |
* command. |
| 1437 |
* @return {*} False if the command wasn't handled, otherwise, the result of |
| 1438 |
* the command. |
| 1439 |
*/ |
| 1440 |
goog.editor.Field.prototype.execCommand = function(command, var_args) { |
| 1441 |
var args = arguments; |
| 1442 |
var result; |
| 1443 |
|
| 1444 |
var plugins = this.indexedPlugins_[goog.editor.Plugin.Op.EXEC_COMMAND]; |
| 1445 |
for (var i = 0; i < plugins.length; ++i) { |
| 1446 |
// If the plugin supports the command, that means it handled the |
| 1447 |
// event and we shouldn't propagate to the other plugins. |
| 1448 |
var plugin = plugins[i]; |
| 1449 |
if (plugin.isEnabled(this) && plugin.isSupportedCommand(command)) { |
| 1450 |
result = plugin.execCommand.apply(plugin, args); |
| 1451 |
break; |
| 1452 |
} |
| 1453 |
} |
| 1454 |
|
| 1455 |
return result; |
| 1456 |
}; |
| 1457 |
|
| 1458 |
|
| 1459 |
/** |
| 1460 |
* Gets the value of command(s). |
| 1461 |
* @param {string|Array<string>} commands String name(s) of the command. |
| 1462 |
* @return {*} Value of each command. Returns false (or array of falses) |
| 1463 |
* if designMode is off or the field is otherwise uneditable, and |
| 1464 |
* there are no activeOnUneditable plugins for the command. |
| 1465 |
*/ |
| 1466 |
goog.editor.Field.prototype.queryCommandValue = function(commands) { |
| 1467 |
var isEditable = this.isLoaded() && this.isSelectionEditable(); |
| 1468 |
if (goog.isString(commands)) { |
| 1469 |
return this.queryCommandValueInternal_(commands, isEditable); |
| 1470 |
} else { |
| 1471 |
var state = {}; |
| 1472 |
for (var i = 0; i < commands.length; i++) { |
| 1473 |
state[commands[i]] = |
| 1474 |
this.queryCommandValueInternal_(commands[i], isEditable); |
| 1475 |
} |
| 1476 |
return state; |
| 1477 |
} |
| 1478 |
}; |
| 1479 |
|
| 1480 |
|
| 1481 |
/** |
| 1482 |
* Gets the value of this command. |
| 1483 |
* @param {string} command The command to check. |
| 1484 |
* @param {boolean} isEditable Whether the field is currently editable. |
| 1485 |
* @return {*} The state of this command. Null if not handled. |
| 1486 |
* False if the field is uneditable and there are no handlers for |
| 1487 |
* uneditable commands. |
| 1488 |
* @private |
| 1489 |
*/ |
| 1490 |
goog.editor.Field.prototype.queryCommandValueInternal_ = function( |
| 1491 |
command, isEditable) { |
| 1492 |
var plugins = this.indexedPlugins_[goog.editor.Plugin.Op.QUERY_COMMAND]; |
| 1493 |
for (var i = 0; i < plugins.length; ++i) { |
| 1494 |
var plugin = plugins[i]; |
| 1495 |
if (plugin.isEnabled(this) && plugin.isSupportedCommand(command) && |
| 1496 |
(isEditable || plugin.activeOnUneditableFields())) { |
| 1497 |
return plugin.queryCommandValue(command); |
| 1498 |
} |
| 1499 |
} |
| 1500 |
return isEditable ? null : false; |
| 1501 |
}; |
| 1502 |
|
| 1503 |
|
| 1504 |
/** |
| 1505 |
* Fires a change event only if the attribute change effects the editiable |
| 1506 |
* field. We ignore events that are internal browser events (ie scrollbar |
| 1507 |
* state change) |
| 1508 |
* @param {Function} handler The function to call if this is not an internal |
| 1509 |
* browser event. |
| 1510 |
* @param {goog.events.BrowserEvent} browserEvent The browser event. |
| 1511 |
* @protected |
| 1512 |
*/ |
| 1513 |
goog.editor.Field.prototype.handleDomAttrChange = function( |
| 1514 |
handler, browserEvent) { |
| 1515 |
if (this.isEventStopped(goog.editor.Field.EventType.CHANGE)) { |
| 1516 |
return; |
| 1517 |
} |
| 1518 |
|
| 1519 |
var e = browserEvent.getBrowserEvent(); |
| 1520 |
|
| 1521 |
// For XUL elements, since we don't care what they are doing |
| 1522 |
try { |
| 1523 |
if (e.originalTarget.prefix || |
| 1524 |
/** @type {!Element} */ (e.originalTarget).nodeName == 'scrollbar') { |
| 1525 |
return; |
| 1526 |
} |
| 1527 |
} catch (ex1) { |
| 1528 |
// Some XUL nodes don't like you reading their properties. If we got |
| 1529 |
// the exception, this implies a XUL node so we can return. |
| 1530 |
return; |
| 1531 |
} |
| 1532 |
|
| 1533 |
// Check if prev and new values are different, sometimes this fires when |
| 1534 |
// nothing has really changed. |
| 1535 |
if (e.prevValue == e.newValue) { |
| 1536 |
return; |
| 1537 |
} |
| 1538 |
handler.call(this, e); |
| 1539 |
}; |
| 1540 |
|
| 1541 |
|
| 1542 |
/** |
| 1543 |
* Handle a mutation event. |
| 1544 |
* @param {goog.events.BrowserEvent|Event} e The browser event. |
| 1545 |
* @private |
| 1546 |
*/ |
| 1547 |
goog.editor.Field.prototype.handleMutationEventGecko_ = function(e) { |
| 1548 |
if (this.isEventStopped(goog.editor.Field.EventType.CHANGE)) { |
| 1549 |
return; |
| 1550 |
} |
| 1551 |
|
| 1552 |
e = e.getBrowserEvent ? e.getBrowserEvent() : e; |
| 1553 |
// For people with firebug, firebug sets this property on elements it is |
| 1554 |
// inserting into the dom. |
| 1555 |
if (e.target.firebugIgnore) { |
| 1556 |
return; |
| 1557 |
} |
| 1558 |
|
| 1559 |
this.isModified_ = true; |
| 1560 |
this.isEverModified_ = true; |
| 1561 |
this.changeTimerGecko_.start(); |
| 1562 |
}; |
| 1563 |
|
| 1564 |
|
| 1565 |
/** |
| 1566 |
* Handle drop events. Deal with focus/selection issues and set the document |
| 1567 |
* as changed. |
| 1568 |
* @param {goog.events.BrowserEvent} e The browser event. |
| 1569 |
* @private |
| 1570 |
*/ |
| 1571 |
goog.editor.Field.prototype.handleDrop_ = function(e) { |
| 1572 |
if (goog.userAgent.IE) { |
| 1573 |
// TODO(user): This should really be done in the loremipsum plugin. |
| 1574 |
this.execCommand(goog.editor.Command.CLEAR_LOREM, true); |
| 1575 |
} |
| 1576 |
|
| 1577 |
// TODO(user): I just moved this code to this location, but I wonder why |
| 1578 |
// it is only done for this case. Investigate. |
| 1579 |
if (goog.editor.BrowserFeature.USE_MUTATION_EVENTS) { |
| 1580 |
this.dispatchFocusAndBeforeFocus_(); |
| 1581 |
} |
| 1582 |
|
| 1583 |
this.dispatchChange(); |
| 1584 |
}; |
| 1585 |
|
| 1586 |
|
| 1587 |
/** |
| 1588 |
* @return {HTMLIFrameElement} The iframe that's body is editable. |
| 1589 |
* @protected |
| 1590 |
*/ |
| 1591 |
goog.editor.Field.prototype.getEditableIframe = function() { |
| 1592 |
var dh; |
| 1593 |
if (this.usesIframe() && (dh = this.getEditableDomHelper())) { |
| 1594 |
// If the iframe has been destroyed, the dh could still exist since the |
| 1595 |
// node may not be gc'ed, but fetching the window can fail. |
| 1596 |
var win = dh.getWindow(); |
| 1597 |
return /** @type {HTMLIFrameElement} */ (win && win.frameElement); |
| 1598 |
} |
| 1599 |
return null; |
| 1600 |
}; |
| 1601 |
|
| 1602 |
|
| 1603 |
/** |
| 1604 |
* @return {goog.dom.DomHelper?} The dom helper for the editable node. |
| 1605 |
*/ |
| 1606 |
goog.editor.Field.prototype.getEditableDomHelper = function() { |
| 1607 |
return this.editableDomHelper; |
| 1608 |
}; |
| 1609 |
|
| 1610 |
|
| 1611 |
/** |
| 1612 |
* @return {goog.dom.AbstractRange?} Closure range object wrapping the selection |
| 1613 |
* in this field or null if this field is not currently editable. |
| 1614 |
*/ |
| 1615 |
goog.editor.Field.prototype.getRange = function() { |
| 1616 |
var win = this.editableDomHelper && this.editableDomHelper.getWindow(); |
| 1617 |
return win && goog.dom.Range.createFromWindow(win); |
| 1618 |
}; |
| 1619 |
|
| 1620 |
|
| 1621 |
/** |
| 1622 |
* Dispatch a selection change event, optionally caused by the given browser |
| 1623 |
* event or selecting the given target. |
| 1624 |
* @param {goog.events.BrowserEvent=} opt_e Optional browser event causing this |
| 1625 |
* event. |
| 1626 |
* @param {Node=} opt_target The node the selection changed to. |
| 1627 |
*/ |
| 1628 |
goog.editor.Field.prototype.dispatchSelectionChangeEvent = function( |
| 1629 |
opt_e, opt_target) { |
| 1630 |
if (this.isEventStopped(goog.editor.Field.EventType.SELECTIONCHANGE)) { |
| 1631 |
return; |
| 1632 |
} |
| 1633 |
|
| 1634 |
// The selection is editable only if the selection is inside the |
| 1635 |
// editable field. |
| 1636 |
var range = this.getRange(); |
| 1637 |
var rangeContainer = range && range.getContainerElement(); |
| 1638 |
this.isSelectionEditable_ = |
| 1639 |
!!rangeContainer && goog.dom.contains(this.getElement(), rangeContainer); |
| 1640 |
|
| 1641 |
this.dispatchCommandValueChange(); |
| 1642 |
this.dispatchEvent({ |
| 1643 |
type: goog.editor.Field.EventType.SELECTIONCHANGE, |
| 1644 |
originalType: opt_e && opt_e.type |
| 1645 |
}); |
| 1646 |
|
| 1647 |
this.invokeShortCircuitingOp_( |
| 1648 |
goog.editor.Plugin.Op.SELECTION, opt_e, opt_target); |
| 1649 |
}; |
| 1650 |
|
| 1651 |
|
| 1652 |
/** |
| 1653 |
* Dispatch a selection change event using a browser event that was |
| 1654 |
* asynchronously saved earlier. |
| 1655 |
* @private |
| 1656 |
*/ |
| 1657 |
goog.editor.Field.prototype.handleSelectionChangeTimer_ = function() { |
| 1658 |
var t = this.selectionChangeTarget_; |
| 1659 |
this.selectionChangeTarget_ = null; |
| 1660 |
this.dispatchSelectionChangeEvent(undefined, t); |
| 1661 |
}; |
| 1662 |
|
| 1663 |
|
| 1664 |
/** |
| 1665 |
* This dispatches the beforechange event on the editable field |
| 1666 |
*/ |
| 1667 |
goog.editor.Field.prototype.dispatchBeforeChange = function() { |
| 1668 |
if (this.isEventStopped(goog.editor.Field.EventType.BEFORECHANGE)) { |
| 1669 |
return; |
| 1670 |
} |
| 1671 |
|
| 1672 |
this.dispatchEvent(goog.editor.Field.EventType.BEFORECHANGE); |
| 1673 |
}; |
| 1674 |
|
| 1675 |
|
| 1676 |
/** |
| 1677 |
* This dispatches the beforetab event on the editable field. If this event is |
| 1678 |
* cancelled, then the default tab behavior is prevented. |
| 1679 |
* @param {goog.events.BrowserEvent} e The tab event. |
| 1680 |
* @private |
| 1681 |
* @return {boolean} The result of dispatchEvent. |
| 1682 |
*/ |
| 1683 |
goog.editor.Field.prototype.dispatchBeforeTab_ = function(e) { |
| 1684 |
return this.dispatchEvent({ |
| 1685 |
type: goog.editor.Field.EventType.BEFORETAB, |
| 1686 |
shiftKey: e.shiftKey, |
| 1687 |
altKey: e.altKey, |
| 1688 |
ctrlKey: e.ctrlKey |
| 1689 |
}); |
| 1690 |
}; |
| 1691 |
|
| 1692 |
|
| 1693 |
/** |
| 1694 |
* Temporarily ignore change events. If the time has already been set, it will |
| 1695 |
* fire immediately now. Further setting of the timer is stopped and |
| 1696 |
* dispatching of events is stopped until startChangeEvents is called. |
| 1697 |
* @param {boolean=} opt_stopChange Whether to ignore base change events. |
| 1698 |
* @param {boolean=} opt_stopDelayedChange Whether to ignore delayed change |
| 1699 |
* events. |
| 1700 |
*/ |
| 1701 |
goog.editor.Field.prototype.stopChangeEvents = function( |
| 1702 |
opt_stopChange, opt_stopDelayedChange) { |
| 1703 |
if (opt_stopChange) { |
| 1704 |
if (this.changeTimerGecko_) { |
| 1705 |
this.changeTimerGecko_.fireIfActive(); |
| 1706 |
} |
| 1707 |
|
| 1708 |
this.stopEvent(goog.editor.Field.EventType.CHANGE); |
| 1709 |
} |
| 1710 |
if (opt_stopDelayedChange) { |
| 1711 |
this.clearDelayedChange(); |
| 1712 |
this.stopEvent(goog.editor.Field.EventType.DELAYEDCHANGE); |
| 1713 |
} |
| 1714 |
}; |
| 1715 |
|
| 1716 |
|
| 1717 |
/** |
| 1718 |
* Start change events again and fire once if desired. |
| 1719 |
* @param {boolean=} opt_fireChange Whether to fire the change event |
| 1720 |
* immediately. |
| 1721 |
* @param {boolean=} opt_fireDelayedChange Whether to fire the delayed change |
| 1722 |
* event immediately. |
| 1723 |
*/ |
| 1724 |
goog.editor.Field.prototype.startChangeEvents = function( |
| 1725 |
opt_fireChange, opt_fireDelayedChange) { |
| 1726 |
|
| 1727 |
if (!opt_fireChange && this.changeTimerGecko_) { |
| 1728 |
// In the case where change events were stopped and we're not firing |
| 1729 |
// them on start, the user was trying to suppress all change or delayed |
| 1730 |
// change events. Clear the change timer now while the events are still |
| 1731 |
// stopped so that its firing doesn't fire a stopped change event, or |
| 1732 |
// queue up a delayed change event that we were trying to stop. |
| 1733 |
this.changeTimerGecko_.fireIfActive(); |
| 1734 |
} |
| 1735 |
|
| 1736 |
this.startEvent(goog.editor.Field.EventType.CHANGE); |
| 1737 |
this.startEvent(goog.editor.Field.EventType.DELAYEDCHANGE); |
| 1738 |
if (opt_fireChange) { |
| 1739 |
this.handleChange(); |
| 1740 |
} |
| 1741 |
|
| 1742 |
if (opt_fireDelayedChange) { |
| 1743 |
this.dispatchDelayedChange_(); |
| 1744 |
} |
| 1745 |
}; |
| 1746 |
|
| 1747 |
|
| 1748 |
/** |
| 1749 |
* Stops the event of the given type from being dispatched. |
| 1750 |
* @param {goog.editor.Field.EventType} eventType type of event to stop. |
| 1751 |
*/ |
| 1752 |
goog.editor.Field.prototype.stopEvent = function(eventType) { |
| 1753 |
this.stoppedEvents_[eventType] = 1; |
| 1754 |
}; |
| 1755 |
|
| 1756 |
|
| 1757 |
/** |
| 1758 |
* Re-starts the event of the given type being dispatched, if it had |
| 1759 |
* previously been stopped with stopEvent(). |
| 1760 |
* @param {goog.editor.Field.EventType} eventType type of event to start. |
| 1761 |
*/ |
| 1762 |
goog.editor.Field.prototype.startEvent = function(eventType) { |
| 1763 |
// Toggling this bit on/off instead of deleting it/re-adding it |
| 1764 |
// saves array allocations. |
| 1765 |
this.stoppedEvents_[eventType] = 0; |
| 1766 |
}; |
| 1767 |
|
| 1768 |
|
| 1769 |
/** |
| 1770 |
* Block an event for a short amount of time. Intended |
| 1771 |
* for the situation where an event pair fires in quick succession |
| 1772 |
* (e.g., mousedown/mouseup, keydown/keyup, focus/blur), |
| 1773 |
* and we want the second event in the pair to get "debounced." |
| 1774 |
* |
| 1775 |
* WARNING: This should never be used to solve race conditions or for |
| 1776 |
* mission-critical actions. It should only be used for UI improvements, |
| 1777 |
* where it's okay if the behavior is non-deterministic. |
| 1778 |
* |
| 1779 |
* @param {goog.editor.Field.EventType} eventType type of event to debounce. |
| 1780 |
*/ |
| 1781 |
goog.editor.Field.prototype.debounceEvent = function(eventType) { |
| 1782 |
this.debouncedEvents_[eventType] = goog.now(); |
| 1783 |
}; |
| 1784 |
|
| 1785 |
|
| 1786 |
/** |
| 1787 |
* Checks if the event of the given type has stopped being dispatched |
| 1788 |
* @param {goog.editor.Field.EventType} eventType type of event to check. |
| 1789 |
* @return {boolean} true if the event has been stopped with stopEvent(). |
| 1790 |
* @protected |
| 1791 |
*/ |
| 1792 |
goog.editor.Field.prototype.isEventStopped = function(eventType) { |
| 1793 |
return !!this.stoppedEvents_[eventType] || |
| 1794 |
(this.debouncedEvents_[eventType] && |
| 1795 |
(goog.now() - this.debouncedEvents_[eventType] <= |
| 1796 |
goog.editor.Field.DEBOUNCE_TIME_MS_)); |
| 1797 |
}; |
| 1798 |
|
| 1799 |
|
| 1800 |
/** |
| 1801 |
* Calls a function to manipulate the dom of this field. This method should be |
| 1802 |
* used whenever Trogedit clients need to modify the dom of the field, so that |
| 1803 |
* delayed change events are handled appropriately. Extra delayed change events |
| 1804 |
* will cause undesired states to be added to the undo-redo stack. This method |
| 1805 |
* will always fire at most one delayed change event, depending on the value of |
| 1806 |
* {@code opt_preventDelayedChange}. |
| 1807 |
* |
| 1808 |
* @param {function()} func The function to call that will manipulate the dom. |
| 1809 |
* @param {boolean=} opt_preventDelayedChange Whether delayed change should be |
| 1810 |
* prevented after calling {@code func}. Defaults to always firing |
| 1811 |
* delayed change. |
| 1812 |
* @param {Object=} opt_handler Object in whose scope to call the listener. |
| 1813 |
*/ |
| 1814 |
goog.editor.Field.prototype.manipulateDom = function( |
| 1815 |
func, opt_preventDelayedChange, opt_handler) { |
| 1816 |
|
| 1817 |
this.stopChangeEvents(true, true); |
| 1818 |
// We don't want any problems with the passed in function permanently |
| 1819 |
// stopping change events. That would break Trogedit. |
| 1820 |
try { |
| 1821 |
func.call(opt_handler); |
| 1822 |
} finally { |
| 1823 |
// If the field isn't loaded then change and delayed change events will be |
| 1824 |
// started as part of the onload behavior. |
| 1825 |
if (this.isLoaded()) { |
| 1826 |
// We assume that func always modified the dom and so fire a single change |
| 1827 |
// event. Delayed change is only fired if not prevented by the user. |
| 1828 |
if (opt_preventDelayedChange) { |
| 1829 |
this.startEvent(goog.editor.Field.EventType.CHANGE); |
| 1830 |
this.handleChange(); |
| 1831 |
this.startEvent(goog.editor.Field.EventType.DELAYEDCHANGE); |
| 1832 |
} else { |
| 1833 |
this.dispatchChange(); |
| 1834 |
} |
| 1835 |
} |
| 1836 |
} |
| 1837 |
}; |
| 1838 |
|
| 1839 |
|
| 1840 |
/** |
| 1841 |
* Dispatches a command value change event. |
| 1842 |
* @param {Array<string>=} opt_commands Commands whose state has |
| 1843 |
* changed. |
| 1844 |
*/ |
| 1845 |
goog.editor.Field.prototype.dispatchCommandValueChange = function( |
| 1846 |
opt_commands) { |
| 1847 |
if (opt_commands) { |
| 1848 |
this.dispatchEvent({ |
| 1849 |
type: goog.editor.Field.EventType.COMMAND_VALUE_CHANGE, |
| 1850 |
commands: opt_commands |
| 1851 |
}); |
| 1852 |
} else { |
| 1853 |
this.dispatchEvent(goog.editor.Field.EventType.COMMAND_VALUE_CHANGE); |
| 1854 |
} |
| 1855 |
}; |
| 1856 |
|
| 1857 |
|
| 1858 |
/** |
| 1859 |
* Dispatches the appropriate set of change events. This only fires |
| 1860 |
* synchronous change events in blended-mode, iframe-using mozilla. It just |
| 1861 |
* starts the appropriate timer for goog.editor.Field.EventType.DELAYEDCHANGE. |
| 1862 |
* This also starts up change events again if they were stopped. |
| 1863 |
* |
| 1864 |
* @param {boolean=} opt_noDelay True if |
| 1865 |
* goog.editor.Field.EventType.DELAYEDCHANGE should be fired syncronously. |
| 1866 |
*/ |
| 1867 |
goog.editor.Field.prototype.dispatchChange = function(opt_noDelay) { |
| 1868 |
this.startChangeEvents(true, opt_noDelay); |
| 1869 |
}; |
| 1870 |
|
| 1871 |
|
| 1872 |
/** |
| 1873 |
* Handle a change in the Editable Field. Marks the field has modified, |
| 1874 |
* dispatches the change event on the editable field (moz only), starts the |
| 1875 |
* timer for the delayed change event. Note that these actions only occur if |
| 1876 |
* the proper events are not stopped. |
| 1877 |
*/ |
| 1878 |
goog.editor.Field.prototype.handleChange = function() { |
| 1879 |
if (this.isEventStopped(goog.editor.Field.EventType.CHANGE)) { |
| 1880 |
return; |
| 1881 |
} |
| 1882 |
|
| 1883 |
// Clear the changeTimerGecko_ if it's active, since any manual call to |
| 1884 |
// handle change is equiavlent to changeTimerGecko_.fire(). |
| 1885 |
if (this.changeTimerGecko_) { |
| 1886 |
this.changeTimerGecko_.stop(); |
| 1887 |
} |
| 1888 |
|
| 1889 |
this.isModified_ = true; |
| 1890 |
this.isEverModified_ = true; |
| 1891 |
|
| 1892 |
if (this.isEventStopped(goog.editor.Field.EventType.DELAYEDCHANGE)) { |
| 1893 |
return; |
| 1894 |
} |
| 1895 |
|
| 1896 |
this.delayedChangeTimer_.start(); |
| 1897 |
}; |
| 1898 |
|
| 1899 |
|
| 1900 |
/** |
| 1901 |
* Dispatch a delayed change event. |
| 1902 |
* @private |
| 1903 |
*/ |
| 1904 |
goog.editor.Field.prototype.dispatchDelayedChange_ = function() { |
| 1905 |
if (this.isEventStopped(goog.editor.Field.EventType.DELAYEDCHANGE)) { |
| 1906 |
return; |
| 1907 |
} |
| 1908 |
// Clear the delayedChangeTimer_ if it's active, since any manual call to |
| 1909 |
// dispatchDelayedChange_ is equivalent to delayedChangeTimer_.fire(). |
| 1910 |
this.delayedChangeTimer_.stop(); |
| 1911 |
this.isModified_ = false; |
| 1912 |
this.dispatchEvent(goog.editor.Field.EventType.DELAYEDCHANGE); |
| 1913 |
}; |
| 1914 |
|
| 1915 |
|
| 1916 |
/** |
| 1917 |
* Don't wait for the timer and just fire the delayed change event if it's |
| 1918 |
* pending. |
| 1919 |
*/ |
| 1920 |
goog.editor.Field.prototype.clearDelayedChange = function() { |
| 1921 |
// The changeTimerGecko_ will queue up a delayed change so to fully clear |
| 1922 |
// delayed change we must also clear this timer. |
| 1923 |
if (this.changeTimerGecko_) { |
| 1924 |
this.changeTimerGecko_.fireIfActive(); |
| 1925 |
} |
| 1926 |
this.delayedChangeTimer_.fireIfActive(); |
| 1927 |
}; |
| 1928 |
|
| 1929 |
|
| 1930 |
/** |
| 1931 |
* Dispatch beforefocus and focus for FF. Note that both of these actually |
| 1932 |
* happen in the document's "focus" event. Unfortunately, we don't actually |
| 1933 |
* have a way of getting in before the focus event in FF (boo! hiss!). |
| 1934 |
* In IE, we use onfocusin for before focus and onfocus for focus. |
| 1935 |
* @private |
| 1936 |
*/ |
| 1937 |
goog.editor.Field.prototype.dispatchFocusAndBeforeFocus_ = function() { |
| 1938 |
this.dispatchBeforeFocus_(); |
| 1939 |
this.dispatchFocus_(); |
| 1940 |
}; |
| 1941 |
|
| 1942 |
|
| 1943 |
/** |
| 1944 |
* Dispatches a before focus event. |
| 1945 |
* @private |
| 1946 |
*/ |
| 1947 |
goog.editor.Field.prototype.dispatchBeforeFocus_ = function() { |
| 1948 |
if (this.isEventStopped(goog.editor.Field.EventType.BEFOREFOCUS)) { |
| 1949 |
return; |
| 1950 |
} |
| 1951 |
|
| 1952 |
this.execCommand(goog.editor.Command.CLEAR_LOREM, true); |
| 1953 |
this.dispatchEvent(goog.editor.Field.EventType.BEFOREFOCUS); |
| 1954 |
}; |
| 1955 |
|
| 1956 |
|
| 1957 |
/** |
| 1958 |
* Dispatches a focus event. |
| 1959 |
* @private |
| 1960 |
*/ |
| 1961 |
goog.editor.Field.prototype.dispatchFocus_ = function() { |
| 1962 |
if (this.isEventStopped(goog.editor.Field.EventType.FOCUS)) { |
| 1963 |
return; |
| 1964 |
} |
| 1965 |
goog.editor.Field.setActiveFieldId(this.id); |
| 1966 |
|
| 1967 |
this.isSelectionEditable_ = true; |
| 1968 |
|
| 1969 |
this.dispatchEvent(goog.editor.Field.EventType.FOCUS); |
| 1970 |
|
| 1971 |
if (goog.editor.BrowserFeature |
| 1972 |
.PUTS_CURSOR_BEFORE_FIRST_BLOCK_ELEMENT_ON_FOCUS) { |
| 1973 |
// If the cursor is at the beginning of the field, make sure that it is |
| 1974 |
// in the first user-visible line break, e.g., |
| 1975 |
// no selection: <div><p>...</p></div> --> <div><p>|cursor|...</p></div> |
| 1976 |
// <div>|cursor|<p>...</p></div> --> <div><p>|cursor|...</p></div> |
| 1977 |
// <body>|cursor|<p>...</p></body> --> <body><p>|cursor|...</p></body> |
| 1978 |
var field = this.getElement(); |
| 1979 |
var range = this.getRange(); |
| 1980 |
|
| 1981 |
if (range) { |
| 1982 |
var focusNode = /** @type {!Element} */ (range.getFocusNode()); |
| 1983 |
if (range.getFocusOffset() == 0 && |
| 1984 |
(!focusNode || focusNode == field || |
| 1985 |
focusNode.tagName == goog.dom.TagName.BODY)) { |
| 1986 |
goog.editor.range.selectNodeStart(field); |
| 1987 |
} |
| 1988 |
} |
| 1989 |
} |
| 1990 |
|
| 1991 |
if (!goog.editor.BrowserFeature.CLEARS_SELECTION_WHEN_FOCUS_LEAVES && |
| 1992 |
this.usesIframe()) { |
| 1993 |
var parent = this.getEditableDomHelper().getWindow().parent; |
| 1994 |
parent.getSelection().removeAllRanges(); |
| 1995 |
} |
| 1996 |
}; |
| 1997 |
|
| 1998 |
|
| 1999 |
/** |
| 2000 |
* Dispatches a blur event. |
| 2001 |
* @protected |
| 2002 |
*/ |
| 2003 |
goog.editor.Field.prototype.dispatchBlur = function() { |
| 2004 |
if (this.isEventStopped(goog.editor.Field.EventType.BLUR)) { |
| 2005 |
return; |
| 2006 |
} |
| 2007 |
|
| 2008 |
// Another field may have already been registered as active, so only |
| 2009 |
// clear out the active field id if we still think this field is active. |
| 2010 |
if (goog.editor.Field.getActiveFieldId() == this.id) { |
| 2011 |
goog.editor.Field.setActiveFieldId(null); |
| 2012 |
} |
| 2013 |
|
| 2014 |
this.isSelectionEditable_ = false; |
| 2015 |
this.dispatchEvent(goog.editor.Field.EventType.BLUR); |
| 2016 |
}; |
| 2017 |
|
| 2018 |
|
| 2019 |
/** |
| 2020 |
* @return {boolean} Whether the selection is editable. |
| 2021 |
*/ |
| 2022 |
goog.editor.Field.prototype.isSelectionEditable = function() { |
| 2023 |
return this.isSelectionEditable_; |
| 2024 |
}; |
| 2025 |
|
| 2026 |
|
| 2027 |
/** |
| 2028 |
* Event handler for clicks in browsers that will follow a link when the user |
| 2029 |
* clicks, even if it's editable. We stop the click manually |
| 2030 |
* @param {goog.events.BrowserEvent} e The event. |
| 2031 |
* @private |
| 2032 |
*/ |
| 2033 |
goog.editor.Field.cancelLinkClick_ = function(e) { |
| 2034 |
if (goog.dom.getAncestorByTagNameAndClass( |
| 2035 |
/** @type {Node} */ (e.target), goog.dom.TagName.A)) { |
| 2036 |
e.preventDefault(); |
| 2037 |
} |
| 2038 |
}; |
| 2039 |
|
| 2040 |
|
| 2041 |
/** |
| 2042 |
* Handle mouse down inside the editable field. |
| 2043 |
* @param {goog.events.BrowserEvent} e The event. |
| 2044 |
* @private |
| 2045 |
*/ |
| 2046 |
goog.editor.Field.prototype.handleMouseDown_ = function(e) { |
| 2047 |
goog.editor.Field.setActiveFieldId(this.id); |
| 2048 |
|
| 2049 |
// Open links in a new window if the user control + clicks. |
| 2050 |
if (goog.userAgent.IE) { |
| 2051 |
var targetElement = e.target; |
| 2052 |
if (targetElement && |
| 2053 |
/** @type {!Element} */ (targetElement).tagName == goog.dom.TagName.A && |
| 2054 |
e.ctrlKey) { |
| 2055 |
this.originalDomHelper.getWindow().open(targetElement.href); |
| 2056 |
} |
| 2057 |
} |
| 2058 |
this.waitingForMouseUp_ = true; |
| 2059 |
}; |
| 2060 |
|
| 2061 |
|
| 2062 |
/** |
| 2063 |
* Handle drag start. Needs to cancel listening for the mouse up event on the |
| 2064 |
* window. |
| 2065 |
* @param {goog.events.BrowserEvent} e The event. |
| 2066 |
* @private |
| 2067 |
*/ |
| 2068 |
goog.editor.Field.prototype.handleDragStart_ = function(e) { |
| 2069 |
this.waitingForMouseUp_ = false; |
| 2070 |
}; |
| 2071 |
|
| 2072 |
|
| 2073 |
/** |
| 2074 |
* Handle mouse up inside the editable field. |
| 2075 |
* @param {goog.events.BrowserEvent} e The event. |
| 2076 |
* @private |
| 2077 |
*/ |
| 2078 |
goog.editor.Field.prototype.handleMouseUp_ = function(e) { |
| 2079 |
if (this.useWindowMouseUp_ && !this.waitingForMouseUp_) { |
| 2080 |
return; |
| 2081 |
} |
| 2082 |
this.waitingForMouseUp_ = false; |
| 2083 |
|
| 2084 |
/* |
| 2085 |
* We fire a selection change event immediately for listeners that depend on |
| 2086 |
* the native browser event object (e). On IE, a listener that tries to |
| 2087 |
* retrieve the selection with goog.dom.Range may see an out-of-date |
| 2088 |
* selection range. |
| 2089 |
*/ |
| 2090 |
this.dispatchEvent(goog.editor.Field.EventType.BEFORESELECTIONCHANGE); |
| 2091 |
this.dispatchSelectionChangeEvent(e); |
| 2092 |
if (goog.userAgent.IE) { |
| 2093 |
/* |
| 2094 |
* Fire a second selection change event for listeners that need an |
| 2095 |
* up-to-date selection range. Save the event's target to be sent with it |
| 2096 |
* (it's safer than saving a copy of the event itself). |
| 2097 |
*/ |
| 2098 |
this.selectionChangeTarget_ = /** @type {Node} */ (e.target); |
| 2099 |
this.selectionChangeTimer_.start(); |
| 2100 |
} |
| 2101 |
}; |
| 2102 |
|
| 2103 |
|
| 2104 |
/** |
| 2105 |
* Retrieve the HTML contents of a field. |
| 2106 |
* |
| 2107 |
* Do NOT just get the innerHTML of a field directly--there's a lot of |
| 2108 |
* processing that needs to happen. |
| 2109 |
* @return {string} The scrubbed contents of the field. |
| 2110 |
*/ |
| 2111 |
goog.editor.Field.prototype.getCleanContents = function() { |
| 2112 |
if (this.queryCommandValue(goog.editor.Command.USING_LOREM)) { |
| 2113 |
return goog.string.Unicode.NBSP; |
| 2114 |
} |
| 2115 |
|
| 2116 |
if (!this.isLoaded()) { |
| 2117 |
// The field is uneditable, so it's ok to read contents directly. |
| 2118 |
var elem = this.getOriginalElement(); |
| 2119 |
if (!elem) { |
| 2120 |
goog.log.log( |
| 2121 |
this.logger, goog.log.Level.SHOUT, |
| 2122 |
"Couldn't get the field element to read the contents"); |
| 2123 |
} |
| 2124 |
return elem.innerHTML; |
| 2125 |
} |
| 2126 |
|
| 2127 |
var fieldCopy = this.getFieldCopy(); |
| 2128 |
|
| 2129 |
// Allow the plugins to handle their cleanup. |
| 2130 |
this.invokeOp_(goog.editor.Plugin.Op.CLEAN_CONTENTS_DOM, fieldCopy); |
| 2131 |
return this.reduceOp_( |
| 2132 |
goog.editor.Plugin.Op.CLEAN_CONTENTS_HTML, fieldCopy.innerHTML); |
| 2133 |
}; |
| 2134 |
|
| 2135 |
|
| 2136 |
/** |
| 2137 |
* Get the copy of the editable field element, which has the innerHTML set |
| 2138 |
* correctly. |
| 2139 |
* @return {!Element} The copy of the editable field. |
| 2140 |
* @protected |
| 2141 |
*/ |
| 2142 |
goog.editor.Field.prototype.getFieldCopy = function() { |
| 2143 |
var field = this.getElement(); |
| 2144 |
// Deep cloneNode strips some script tag contents in IE, so we do this. |
| 2145 |
var fieldCopy = /** @type {Element} */ (field.cloneNode(false)); |
| 2146 |
|
| 2147 |
// For some reason, when IE sets innerHtml of the cloned node, it strips |
| 2148 |
// script tags that fall at the beginning of an element. Appending a |
| 2149 |
// non-breaking space prevents this. |
| 2150 |
var html = field.innerHTML; |
| 2151 |
if (goog.userAgent.IE && html.match(/^\s*<script/i)) { |
| 2152 |
html = goog.string.Unicode.NBSP + html; |
| 2153 |
} |
| 2154 |
fieldCopy.innerHTML = html; |
| 2155 |
return fieldCopy; |
| 2156 |
}; |
| 2157 |
|
| 2158 |
|
| 2159 |
/** |
| 2160 |
* Sets the contents of the field. |
| 2161 |
* @param {boolean} addParas Boolean to specify whether to add paragraphs |
| 2162 |
* to long fields. |
| 2163 |
* @param {?goog.html.SafeHtml} html html to insert. If html=null, then this |
| 2164 |
* defaults to a nsbp for mozilla and an empty string for IE. |
| 2165 |
* @param {boolean=} opt_dontFireDelayedChange True to make this content change |
| 2166 |
* not fire a delayed change event. |
| 2167 |
* @param {boolean=} opt_applyLorem Whether to apply lorem ipsum styles. |
| 2168 |
*/ |
| 2169 |
goog.editor.Field.prototype.setSafeHtml = function( |
| 2170 |
addParas, html, opt_dontFireDelayedChange, opt_applyLorem) { |
| 2171 |
if (this.isLoading()) { |
| 2172 |
goog.log.error(this.logger, "Can't set html while loading Trogedit"); |
| 2173 |
return; |
| 2174 |
} |
| 2175 |
|
| 2176 |
// Clear the lorem ipsum style, always. |
| 2177 |
if (opt_applyLorem) { |
| 2178 |
this.execCommand(goog.editor.Command.CLEAR_LOREM); |
| 2179 |
} |
| 2180 |
|
| 2181 |
if (html && addParas) { |
| 2182 |
html = goog.html.SafeHtml.create('p', {}, html); |
| 2183 |
} |
| 2184 |
|
| 2185 |
// If we don't want change events to fire, we have to turn off change events |
| 2186 |
// before setting the field contents, since that causes mutation events. |
| 2187 |
if (opt_dontFireDelayedChange) { |
| 2188 |
this.stopChangeEvents(false, true); |
| 2189 |
} |
| 2190 |
|
| 2191 |
this.setInnerHtml_(html); |
| 2192 |
|
| 2193 |
// Set the lorem ipsum style, if the element is empty. |
| 2194 |
if (opt_applyLorem) { |
| 2195 |
this.execCommand(goog.editor.Command.UPDATE_LOREM); |
| 2196 |
} |
| 2197 |
|
| 2198 |
// TODO(user): This check should probably be moved to isEventStopped and |
| 2199 |
// startEvent. |
| 2200 |
if (this.isLoaded()) { |
| 2201 |
if (opt_dontFireDelayedChange) { // Turn back on change events |
| 2202 |
// We must fire change timer if necessary before restarting change events! |
| 2203 |
// Otherwise, the change timer firing after we restart events will cause |
| 2204 |
// the delayed change we were trying to stop. Flow: |
| 2205 |
// Stop delayed change |
| 2206 |
// setInnerHtml_, this starts the change timer |
| 2207 |
// start delayed change |
| 2208 |
// change timer fires |
| 2209 |
// starts delayed change timer since event was not stopped |
| 2210 |
// delayed change fires for the delayed change we tried to stop. |
| 2211 |
if (goog.editor.BrowserFeature.USE_MUTATION_EVENTS) { |
| 2212 |
this.changeTimerGecko_.fireIfActive(); |
| 2213 |
} |
| 2214 |
this.startChangeEvents(); |
| 2215 |
} else { // Mark the document as changed and fire change events. |
| 2216 |
this.dispatchChange(); |
| 2217 |
} |
| 2218 |
} |
| 2219 |
}; |
| 2220 |
|
| 2221 |
|
| 2222 |
/** |
| 2223 |
* Sets the inner HTML of the field. Works on both editable and |
| 2224 |
* uneditable fields. |
| 2225 |
* @param {?goog.html.SafeHtml} html The new inner HTML of the field. |
| 2226 |
* @private |
| 2227 |
*/ |
| 2228 |
goog.editor.Field.prototype.setInnerHtml_ = function(html) { |
| 2229 |
var field = this.getElement(); |
| 2230 |
if (field) { |
| 2231 |
// Safari will put <style> tags into *new* <head> elements. When setting |
| 2232 |
// HTML, we need to remove these spare <head>s to make sure there's a |
| 2233 |
// clean slate, but keep the first <head>. |
| 2234 |
// Note: We punt on this issue for the non iframe case since |
| 2235 |
// we don't want to screw with the main document. |
| 2236 |
if (this.usesIframe() && goog.editor.BrowserFeature.MOVES_STYLE_TO_HEAD) { |
| 2237 |
var heads = goog.dom.getElementsByTagName( |
| 2238 |
goog.dom.TagName.HEAD, goog.asserts.assert(field.ownerDocument)); |
| 2239 |
for (var i = heads.length - 1; i >= 1; --i) { |
| 2240 |
heads[i].parentNode.removeChild(heads[i]); |
| 2241 |
} |
| 2242 |
} |
| 2243 |
} else { |
| 2244 |
field = this.getOriginalElement(); |
| 2245 |
} |
| 2246 |
|
| 2247 |
if (field) { |
| 2248 |
this.injectContents(html && goog.html.SafeHtml.unwrap(html), field); |
| 2249 |
} |
| 2250 |
}; |
| 2251 |
|
| 2252 |
|
| 2253 |
/** |
| 2254 |
* Attemps to turn on designMode for a document. This function can fail under |
| 2255 |
* certain circumstances related to the load event, and will throw an exception. |
| 2256 |
* @protected |
| 2257 |
*/ |
| 2258 |
goog.editor.Field.prototype.turnOnDesignModeGecko = function() { |
| 2259 |
var doc = this.getEditableDomHelper().getDocument(); |
| 2260 |
|
| 2261 |
// NOTE(nicksantos): This will fail under certain conditions, like |
| 2262 |
// when the node has display: none. It's up to clients to ensure that |
| 2263 |
// their fields are valid when they try to make them editable. |
| 2264 |
doc.designMode = 'on'; |
| 2265 |
|
| 2266 |
if (goog.editor.BrowserFeature.HAS_STYLE_WITH_CSS) { |
| 2267 |
doc.execCommand('styleWithCSS', false, false); |
| 2268 |
} |
| 2269 |
}; |
| 2270 |
|
| 2271 |
|
| 2272 |
/** |
| 2273 |
* Installs styles if needed. Only writes styles when they can't be written |
| 2274 |
* inline directly into the field. |
| 2275 |
* @protected |
| 2276 |
*/ |
| 2277 |
goog.editor.Field.prototype.installStyles = function() { |
| 2278 |
if (this.cssStyles.getTypedStringValue() && this.shouldLoadAsynchronously()) { |
| 2279 |
goog.style.installSafeStyleSheet(this.cssStyles, this.getElement()); |
| 2280 |
} |
| 2281 |
}; |
| 2282 |
|
| 2283 |
|
| 2284 |
/** |
| 2285 |
* Signal that the field is loaded and ready to use. Change events now are |
| 2286 |
* in effect. |
| 2287 |
* @private |
| 2288 |
*/ |
| 2289 |
goog.editor.Field.prototype.dispatchLoadEvent_ = function() { |
| 2290 |
this.getElement(); |
| 2291 |
this.installStyles(); |
| 2292 |
this.startChangeEvents(); |
| 2293 |
goog.log.info(this.logger, 'Dispatching load ' + this.id); |
| 2294 |
this.dispatchEvent(goog.editor.Field.EventType.LOAD); |
| 2295 |
}; |
| 2296 |
|
| 2297 |
|
| 2298 |
/** |
| 2299 |
* @return {boolean} Whether the field is uneditable. |
| 2300 |
*/ |
| 2301 |
goog.editor.Field.prototype.isUneditable = function() { |
| 2302 |
return this.loadState_ == goog.editor.Field.LoadState_.UNEDITABLE; |
| 2303 |
}; |
| 2304 |
|
| 2305 |
|
| 2306 |
/** |
| 2307 |
* @return {boolean} Whether the field has finished loading. |
| 2308 |
*/ |
| 2309 |
goog.editor.Field.prototype.isLoaded = function() { |
| 2310 |
return this.loadState_ == goog.editor.Field.LoadState_.EDITABLE; |
| 2311 |
}; |
| 2312 |
|
| 2313 |
|
| 2314 |
/** |
| 2315 |
* @return {boolean} Whether the field is in the process of loading. |
| 2316 |
*/ |
| 2317 |
goog.editor.Field.prototype.isLoading = function() { |
| 2318 |
return this.loadState_ == goog.editor.Field.LoadState_.LOADING; |
| 2319 |
}; |
| 2320 |
|
| 2321 |
|
| 2322 |
/** |
| 2323 |
* Gives the field focus. |
| 2324 |
*/ |
| 2325 |
goog.editor.Field.prototype.focus = function() { |
| 2326 |
if (!goog.editor.BrowserFeature.HAS_CONTENT_EDITABLE && this.usesIframe()) { |
| 2327 |
// In designMode, only the window itself can be focused; not the element. |
| 2328 |
this.getEditableDomHelper().getWindow().focus(); |
| 2329 |
} else { |
| 2330 |
if (goog.userAgent.OPERA) { |
| 2331 |
// Opera will scroll to the bottom of the focused document, even |
| 2332 |
// if it is contained in an iframe that is scrolled to the top and |
| 2333 |
// the bottom flows past the end of it. To prevent this, |
| 2334 |
// save the scroll position of the document containing the editor |
| 2335 |
// iframe, then restore it after the focus. |
| 2336 |
var scrollX = this.appWindow_.pageXOffset; |
| 2337 |
var scrollY = this.appWindow_.pageYOffset; |
| 2338 |
} |
| 2339 |
this.getElement().focus(); |
| 2340 |
if (goog.userAgent.OPERA) { |
| 2341 |
this.appWindow_.scrollTo( |
| 2342 |
/** @type {number} */ (scrollX), /** @type {number} */ (scrollY)); |
| 2343 |
} |
| 2344 |
} |
| 2345 |
}; |
| 2346 |
|
| 2347 |
|
| 2348 |
/** |
| 2349 |
* Gives the field focus and places the cursor at the start of the field. |
| 2350 |
*/ |
| 2351 |
goog.editor.Field.prototype.focusAndPlaceCursorAtStart = function() { |
| 2352 |
// NOTE(user): Excluding Gecko to maintain existing behavior post refactoring |
| 2353 |
// placeCursorAtStart into its own method. In Gecko browsers that currently |
| 2354 |
// have a selection the existing selection will be restored, otherwise it |
| 2355 |
// will go to the start. |
| 2356 |
// TODO(user): Refactor the code using this and related methods. We should |
| 2357 |
// only mess with the selection in the case where there is not an existing |
| 2358 |
// selection in the field. |
| 2359 |
if (goog.editor.BrowserFeature.HAS_IE_RANGES || !goog.userAgent.GECKO) { |
| 2360 |
this.placeCursorAtStart(); |
| 2361 |
} |
| 2362 |
this.focus(); |
| 2363 |
}; |
| 2364 |
|
| 2365 |
|
| 2366 |
/** |
| 2367 |
* Place the cursor at the start of this field. It's recommended that you only |
| 2368 |
* use this method (and manipulate the selection in general) when there is not |
| 2369 |
* an existing selection in the field. |
| 2370 |
*/ |
| 2371 |
goog.editor.Field.prototype.placeCursorAtStart = function() { |
| 2372 |
this.placeCursorAtStartOrEnd_(true); |
| 2373 |
}; |
| 2374 |
|
| 2375 |
|
| 2376 |
/** |
| 2377 |
* Place the cursor at the start of this field. It's recommended that you only |
| 2378 |
* use this method (and manipulate the selection in general) when there is not |
| 2379 |
* an existing selection in the field. |
| 2380 |
*/ |
| 2381 |
goog.editor.Field.prototype.placeCursorAtEnd = function() { |
| 2382 |
this.placeCursorAtStartOrEnd_(false); |
| 2383 |
}; |
| 2384 |
|
| 2385 |
|
| 2386 |
/** |
| 2387 |
* Helper method to place the cursor at the start or end of this field. |
| 2388 |
* @param {boolean} isStart True for start, false for end. |
| 2389 |
* @private |
| 2390 |
*/ |
| 2391 |
goog.editor.Field.prototype.placeCursorAtStartOrEnd_ = function(isStart) { |
| 2392 |
var field = this.getElement(); |
| 2393 |
if (field) { |
| 2394 |
var cursorPosition = isStart ? goog.editor.node.getLeftMostLeaf(field) : |
| 2395 |
goog.editor.node.getRightMostLeaf(field); |
| 2396 |
if (field == cursorPosition) { |
| 2397 |
// The rightmost leaf we found was the field element itself (which likely |
| 2398 |
// means the field element is empty). We can't place the cursor next to |
| 2399 |
// the field element, so just place it at the beginning. |
| 2400 |
goog.dom.Range.createCaret(field, 0).select(); |
| 2401 |
} else { |
| 2402 |
goog.editor.range.placeCursorNextTo(cursorPosition, isStart); |
| 2403 |
} |
| 2404 |
this.dispatchSelectionChangeEvent(); |
| 2405 |
} |
| 2406 |
}; |
| 2407 |
|
| 2408 |
|
| 2409 |
/** |
| 2410 |
* Restore a saved range, and set the focus on the field. |
| 2411 |
* If no range is specified, we simply set the focus. |
| 2412 |
* @param {goog.dom.SavedRange=} opt_range A previously saved selected range. |
| 2413 |
*/ |
| 2414 |
goog.editor.Field.prototype.restoreSavedRange = function(opt_range) { |
| 2415 |
if (opt_range) { |
| 2416 |
opt_range.restore(); |
| 2417 |
} |
| 2418 |
this.focus(); |
| 2419 |
}; |
| 2420 |
|
| 2421 |
|
| 2422 |
/** |
| 2423 |
* Makes a field editable. |
| 2424 |
* |
| 2425 |
* @param {!goog.html.TrustedResourceUrl=} opt_iframeSrc URL to set the iframe |
| 2426 |
* src to if necessary. |
| 2427 |
*/ |
| 2428 |
goog.editor.Field.prototype.makeEditable = function(opt_iframeSrc) { |
| 2429 |
this.loadState_ = goog.editor.Field.LoadState_.LOADING; |
| 2430 |
|
| 2431 |
var field = this.getOriginalElement(); |
| 2432 |
|
| 2433 |
// TODO: In the fieldObj, save the field's id, className, cssText |
| 2434 |
// in order to reset it on closeField. That way, we can muck with the field's |
| 2435 |
// css, id, class and restore to how it was at the end. |
| 2436 |
this.nodeName = field.nodeName; |
| 2437 |
this.savedClassName_ = field.className; |
| 2438 |
this.setInitialStyle(field.style.cssText); |
| 2439 |
|
| 2440 |
goog.dom.classlist.add(field, 'editable'); |
| 2441 |
|
| 2442 |
this.makeEditableInternal(opt_iframeSrc); |
| 2443 |
}; |
| 2444 |
|
| 2445 |
|
| 2446 |
/** |
| 2447 |
* Handles actually making something editable - creating necessary nodes, |
| 2448 |
* injecting content, etc. |
| 2449 |
* @param {!goog.html.TrustedResourceUrl=} opt_iframeSrc URL to set the iframe |
| 2450 |
* src to if necessary. |
| 2451 |
* @protected |
| 2452 |
*/ |
| 2453 |
goog.editor.Field.prototype.makeEditableInternal = function(opt_iframeSrc) { |
| 2454 |
this.makeIframeField_(opt_iframeSrc); |
| 2455 |
}; |
| 2456 |
|
| 2457 |
|
| 2458 |
/** |
| 2459 |
* Handle the loading of the field (e.g. once the field is ready to setup). |
| 2460 |
* TODO(user): this should probably just be moved into dispatchLoadEvent_. |
| 2461 |
* @protected |
| 2462 |
*/ |
| 2463 |
goog.editor.Field.prototype.handleFieldLoad = function() { |
| 2464 |
if (goog.userAgent.IE) { |
| 2465 |
// This sometimes fails if the selection is invalid. This can happen, for |
| 2466 |
// example, if you attach a CLICK handler to the field that causes the |
| 2467 |
// field to be removed from the DOM and replaced with an editor |
| 2468 |
// -- however, listening to another event like MOUSEDOWN does not have this |
| 2469 |
// issue since no mouse selection has happened at that time. |
| 2470 |
goog.dom.Range.clearSelection(this.editableDomHelper.getWindow()); |
| 2471 |
} |
| 2472 |
|
| 2473 |
if (goog.editor.Field.getActiveFieldId() != this.id) { |
| 2474 |
this.execCommand(goog.editor.Command.UPDATE_LOREM); |
| 2475 |
} |
| 2476 |
|
| 2477 |
this.setupChangeListeners_(); |
| 2478 |
this.dispatchLoadEvent_(); |
| 2479 |
|
| 2480 |
// Enabling plugins after we fire the load event so that clients have a |
| 2481 |
// chance to set initial field contents before we start mucking with |
| 2482 |
// everything. |
| 2483 |
for (var classId in this.plugins_) { |
| 2484 |
this.plugins_[classId].enable(this); |
| 2485 |
} |
| 2486 |
}; |
| 2487 |
|
| 2488 |
|
| 2489 |
/** |
| 2490 |
* Closes the field and cancels all pending change timers. Note that this |
| 2491 |
* means that if a change event has not fired yet, it will not fire. Clients |
| 2492 |
* should check fieldOj.isModified() if they depend on the final change event. |
| 2493 |
* Throws an error if the field is already uneditable. |
| 2494 |
* |
| 2495 |
* @param {boolean=} opt_skipRestore True to prevent copying of editable field |
| 2496 |
* contents back into the original node. |
| 2497 |
*/ |
| 2498 |
goog.editor.Field.prototype.makeUneditable = function(opt_skipRestore) { |
| 2499 |
if (this.isUneditable()) { |
| 2500 |
throw new Error('makeUneditable: Field is already uneditable'); |
| 2501 |
} |
| 2502 |
|
| 2503 |
// Fire any events waiting on a timeout. |
| 2504 |
// Clearing delayed change also clears changeTimerGecko_. |
| 2505 |
this.clearDelayedChange(); |
| 2506 |
this.selectionChangeTimer_.fireIfActive(); |
| 2507 |
this.execCommand(goog.editor.Command.CLEAR_LOREM); |
| 2508 |
|
| 2509 |
var html = null; |
| 2510 |
if (!opt_skipRestore && this.getElement()) { |
| 2511 |
// Rest of cleanup is simpler if field was never initialized. |
| 2512 |
html = this.getCleanContents(); |
| 2513 |
} |
| 2514 |
|
| 2515 |
// First clean up anything that happens in makeFieldEditable |
| 2516 |
// (i.e. anything that needs cleanup even if field has not loaded). |
| 2517 |
this.clearFieldLoadListener_(); |
| 2518 |
|
| 2519 |
var field = this.getOriginalElement(); |
| 2520 |
if (goog.editor.Field.getActiveFieldId() == field.id) { |
| 2521 |
goog.editor.Field.setActiveFieldId(null); |
| 2522 |
} |
| 2523 |
|
| 2524 |
// Clear all listeners before removing the nodes from the dom - if |
| 2525 |
// there are listeners on the iframe window, Firefox throws errors trying |
| 2526 |
// to unlisten once the iframe is no longer in the dom. |
| 2527 |
this.clearListeners(); |
| 2528 |
|
| 2529 |
// For fields that have loaded, clean up anything that happened in |
| 2530 |
// handleFieldOpen or later. |
| 2531 |
// If html is provided, copy it back and reset the properties on the field |
| 2532 |
// so that the original node will have the same properties as it did before |
| 2533 |
// it was made editable. |
| 2534 |
if (goog.isString(html)) { |
| 2535 |
goog.editor.node.replaceInnerHtml(field, html); |
| 2536 |
this.resetOriginalElemProperties(); |
| 2537 |
} |
| 2538 |
|
| 2539 |
this.restoreDom(); |
| 2540 |
this.tearDownFieldObject_(); |
| 2541 |
|
| 2542 |
// On Safari, make sure to un-focus the field so that the |
| 2543 |
// native "current field" highlight style gets removed. |
| 2544 |
if (goog.userAgent.WEBKIT) { |
| 2545 |
field.blur(); |
| 2546 |
} |
| 2547 |
|
| 2548 |
this.execCommand(goog.editor.Command.UPDATE_LOREM); |
| 2549 |
this.dispatchEvent(goog.editor.Field.EventType.UNLOAD); |
| 2550 |
}; |
| 2551 |
|
| 2552 |
|
| 2553 |
/** |
| 2554 |
* Restores the dom to how it was before being made editable. |
| 2555 |
* @protected |
| 2556 |
*/ |
| 2557 |
goog.editor.Field.prototype.restoreDom = function() { |
| 2558 |
// TODO(user): Consider only removing the iframe if we are |
| 2559 |
// restoring the original node, aka, if opt_html. |
| 2560 |
var field = this.getOriginalElement(); |
| 2561 |
// TODO(robbyw): Consider throwing an error if !field. |
| 2562 |
if (field) { |
| 2563 |
// If the field is in the process of loading when it starts getting torn |
| 2564 |
// up, the iframe will not exist. |
| 2565 |
var iframe = this.getEditableIframe(); |
| 2566 |
if (iframe) { |
| 2567 |
goog.dom.replaceNode(field, iframe); |
| 2568 |
} |
| 2569 |
} |
| 2570 |
}; |
| 2571 |
|
| 2572 |
|
| 2573 |
/** |
| 2574 |
* Returns true if the field needs to be loaded asynchrnously. |
| 2575 |
* @return {boolean} True if loads are async. |
| 2576 |
* @protected |
| 2577 |
*/ |
| 2578 |
goog.editor.Field.prototype.shouldLoadAsynchronously = function() { |
| 2579 |
if (!goog.isDef(this.isHttps_)) { |
| 2580 |
this.isHttps_ = false; |
| 2581 |
|
| 2582 |
if (goog.userAgent.IE && this.usesIframe()) { |
| 2583 |
// IE iframes need to load asynchronously if they are in https as we need |
| 2584 |
// to set an actual src on the iframe and wait for it to load. |
| 2585 |
|
| 2586 |
// Find the top-most window we have access to and see if it's https. |
| 2587 |
// Technically this could fail if we have an http frame in an https frame |
| 2588 |
// on the same domain (or vice versa), but walking up the window hierarchy |
| 2589 |
// to find the first window that has an http* protocol seems like |
| 2590 |
// overkill. |
| 2591 |
var win = this.originalDomHelper.getWindow(); |
| 2592 |
while (win != win.parent) { |
| 2593 |
try { |
| 2594 |
win = win.parent; |
| 2595 |
} catch (e) { |
| 2596 |
break; |
| 2597 |
} |
| 2598 |
} |
| 2599 |
var loc = win.location; |
| 2600 |
this.isHttps_ = |
| 2601 |
loc.protocol == 'https:' && loc.search.indexOf('nocheckhttps') == -1; |
| 2602 |
} |
| 2603 |
} |
| 2604 |
return this.isHttps_; |
| 2605 |
}; |
| 2606 |
|
| 2607 |
|
| 2608 |
/** |
| 2609 |
* Start the editable iframe creation process for Mozilla or IE whitebox. |
| 2610 |
* The iframes load asynchronously. |
| 2611 |
* |
| 2612 |
* @param {!goog.html.TrustedResourceUrl=} opt_iframeSrc URL to set the iframe |
| 2613 |
* src to if necessary. |
| 2614 |
* @private |
| 2615 |
*/ |
| 2616 |
goog.editor.Field.prototype.makeIframeField_ = function(opt_iframeSrc) { |
| 2617 |
var field = this.getOriginalElement(); |
| 2618 |
// TODO(robbyw): Consider throwing an error if !field. |
| 2619 |
if (field) { |
| 2620 |
var html = field.innerHTML; |
| 2621 |
|
| 2622 |
// Invoke prepareContentsHtml on all plugins to prepare html for editing. |
| 2623 |
// Make sure this is done before calling this.attachFrame which removes the |
| 2624 |
// original element from DOM tree. Plugins may assume that the original |
| 2625 |
// element is still in its original position in DOM. |
| 2626 |
var styles = {}; |
| 2627 |
html = this.reduceOp_( |
| 2628 |
goog.editor.Plugin.Op.PREPARE_CONTENTS_HTML, html, styles); |
| 2629 |
|
| 2630 |
var iframe = this.originalDomHelper.createDom( |
| 2631 |
goog.dom.TagName.IFRAME, this.getIframeAttributes()); |
| 2632 |
|
| 2633 |
// TODO(nicksantos): Figure out if this is ever needed in SAFARI? |
| 2634 |
// In IE over HTTPS we need to wait for a load event before we set up the |
| 2635 |
// iframe, this is to prevent a security prompt or access is denied |
| 2636 |
// errors. |
| 2637 |
// NOTE(user): This hasn't been confirmed. isHttps_ allows a query |
| 2638 |
// param, nocheckhttps, which we can use to ascertain if this is actually |
| 2639 |
// needed. It was originally thought to be needed for IE6 SP1, but |
| 2640 |
// errors have been seen in IE7 as well. |
| 2641 |
if (this.shouldLoadAsynchronously()) { |
| 2642 |
// onLoad is the function to call once the iframe is ready to continue |
| 2643 |
// loading. |
| 2644 |
var onLoad = |
| 2645 |
goog.bind(this.iframeFieldLoadHandler, this, iframe, html, styles); |
| 2646 |
|
| 2647 |
this.fieldLoadListenerKey_ = |
| 2648 |
goog.events.listen(iframe, goog.events.EventType.LOAD, onLoad, true); |
| 2649 |
|
| 2650 |
if (opt_iframeSrc) { |
| 2651 |
goog.dom.safe.setIframeSrc(iframe, opt_iframeSrc); |
| 2652 |
} |
| 2653 |
} |
| 2654 |
|
| 2655 |
this.attachIframe(iframe); |
| 2656 |
|
| 2657 |
// Only continue if its not IE HTTPS in which case we're waiting for load. |
| 2658 |
if (!this.shouldLoadAsynchronously()) { |
| 2659 |
this.iframeFieldLoadHandler(iframe, html, styles); |
| 2660 |
} |
| 2661 |
} |
| 2662 |
}; |
| 2663 |
|
| 2664 |
|
| 2665 |
/** |
| 2666 |
* Given the original field element, and the iframe that is destined to |
| 2667 |
* become the editable field, styles them appropriately and add the iframe |
| 2668 |
* to the dom. |
| 2669 |
* |
| 2670 |
* @param {HTMLIFrameElement} iframe The iframe element. |
| 2671 |
* @protected |
| 2672 |
*/ |
| 2673 |
goog.editor.Field.prototype.attachIframe = function(iframe) { |
| 2674 |
var field = this.getOriginalElement(); |
| 2675 |
// TODO(user): Why do we do these two lines .. and why whitebox only? |
| 2676 |
iframe.className = field.className; |
| 2677 |
iframe.id = field.id; |
| 2678 |
goog.dom.replaceNode(iframe, field); |
| 2679 |
}; |
| 2680 |
|
| 2681 |
|
| 2682 |
/** |
| 2683 |
* @param {Object} extraStyles A map of extra styles. |
| 2684 |
* @return {!goog.editor.icontent.FieldFormatInfo} The FieldFormatInfo |
| 2685 |
* object for this field's configuration. |
| 2686 |
* @protected |
| 2687 |
*/ |
| 2688 |
goog.editor.Field.prototype.getFieldFormatInfo = function(extraStyles) { |
| 2689 |
var originalElement = this.getOriginalElement(); |
| 2690 |
var isStandardsMode = goog.editor.node.isStandardsMode(originalElement); |
| 2691 |
|
| 2692 |
return new goog.editor.icontent.FieldFormatInfo( |
| 2693 |
this.id, isStandardsMode, false, false, extraStyles); |
| 2694 |
}; |
| 2695 |
|
| 2696 |
|
| 2697 |
/** |
| 2698 |
* Writes the html content into the iframe. Handles writing any aditional |
| 2699 |
* styling as well. |
| 2700 |
* @param {HTMLIFrameElement} iframe Iframe to write contents into. |
| 2701 |
* @param {string} innerHtml The html content to write into the iframe. |
| 2702 |
* @param {Object} extraStyles A map of extra style attributes. |
| 2703 |
* @protected |
| 2704 |
*/ |
| 2705 |
goog.editor.Field.prototype.writeIframeContent = function( |
| 2706 |
iframe, innerHtml, extraStyles) { |
| 2707 |
var formatInfo = this.getFieldFormatInfo(extraStyles); |
| 2708 |
|
| 2709 |
if (this.shouldLoadAsynchronously()) { |
| 2710 |
var doc = goog.dom.getFrameContentDocument(iframe); |
| 2711 |
goog.editor.icontent.writeHttpsInitialIframe(formatInfo, doc, innerHtml); |
| 2712 |
} else { |
| 2713 |
var styleInfo = new goog.editor.icontent.FieldStyleInfo( |
| 2714 |
this.getElement(), this.cssStyles.getTypedStringValue()); |
| 2715 |
goog.editor.icontent.writeNormalInitialIframe( |
| 2716 |
formatInfo, innerHtml, styleInfo, iframe); |
| 2717 |
} |
| 2718 |
}; |
| 2719 |
|
| 2720 |
|
| 2721 |
/** |
| 2722 |
* The function to call when the editable iframe loads. |
| 2723 |
* |
| 2724 |
* @param {HTMLIFrameElement} iframe Iframe that just loaded. |
| 2725 |
* @param {string} innerHtml Html to put inside the body of the iframe. |
| 2726 |
* @param {Object} styles Property-value map of CSS styles to install on |
| 2727 |
* editable field. |
| 2728 |
* @protected |
| 2729 |
*/ |
| 2730 |
goog.editor.Field.prototype.iframeFieldLoadHandler = function( |
| 2731 |
iframe, innerHtml, styles) { |
| 2732 |
this.clearFieldLoadListener_(); |
| 2733 |
|
| 2734 |
iframe.allowTransparency = 'true'; |
| 2735 |
this.writeIframeContent(iframe, innerHtml, styles); |
| 2736 |
var doc = goog.dom.getFrameContentDocument(iframe); |
| 2737 |
|
| 2738 |
// Make sure to get this pointer after the doc.write as the doc.write |
| 2739 |
// clobbers all the document contents. |
| 2740 |
var body = doc.body; |
| 2741 |
this.setupFieldObject(body); |
| 2742 |
|
| 2743 |
if (!goog.editor.BrowserFeature.HAS_CONTENT_EDITABLE && this.usesIframe()) { |
| 2744 |
this.turnOnDesignModeGecko(); |
| 2745 |
} |
| 2746 |
|
| 2747 |
this.handleFieldLoad(); |
| 2748 |
}; |
| 2749 |
|
| 2750 |
|
| 2751 |
/** |
| 2752 |
* Clears fieldLoadListener for a field. Must be called even (especially?) if |
| 2753 |
* the field is not yet loaded and therefore not in this.fieldMap_ |
| 2754 |
* @private |
| 2755 |
*/ |
| 2756 |
goog.editor.Field.prototype.clearFieldLoadListener_ = function() { |
| 2757 |
if (this.fieldLoadListenerKey_) { |
| 2758 |
goog.events.unlistenByKey(this.fieldLoadListenerKey_); |
| 2759 |
this.fieldLoadListenerKey_ = null; |
| 2760 |
} |
| 2761 |
}; |
| 2762 |
|
| 2763 |
|
| 2764 |
/** |
| 2765 |
* @return {!Object} Get the HTML attributes for this field's iframe. |
| 2766 |
* @protected |
| 2767 |
*/ |
| 2768 |
goog.editor.Field.prototype.getIframeAttributes = function() { |
| 2769 |
var iframeStyle = 'padding:0;' + this.getOriginalElement().style.cssText; |
| 2770 |
|
| 2771 |
if (!goog.string.endsWith(iframeStyle, ';')) { |
| 2772 |
iframeStyle += ';'; |
| 2773 |
} |
| 2774 |
|
| 2775 |
iframeStyle += 'background-color:white;'; |
| 2776 |
|
| 2777 |
// Ensure that the iframe has default overflow styling. If overflow is |
| 2778 |
// set to auto, an IE rendering bug can occur when it tries to render a |
| 2779 |
// table at the very bottom of the field, such that the table would cause |
| 2780 |
// a scrollbar, that makes the entire field go blank. |
| 2781 |
if (goog.userAgent.IE) { |
| 2782 |
iframeStyle += 'overflow:visible;'; |
| 2783 |
} |
| 2784 |
|
| 2785 |
return {'frameBorder': 0, 'style': iframeStyle}; |
| 2786 |
}; |