| 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 |
|
| 15 |
/** |
| 16 |
* @fileoverview Class to encapsulate an editable field that blends in with |
| 17 |
* the style of the page. The field can be fixed height, grow with its |
| 18 |
* contents, or have a min height after which it grows to its contents. |
| 19 |
* This is a goog.editor.Field, but with blending and sizing capabilities, |
| 20 |
* and avoids using an iframe whenever possible. |
| 21 |
* |
| 22 |
* @author [email protected] (Nick Santos) |
| 23 |
* @see ../demos/editor/seamlessfield.html |
| 24 |
*/ |
| 25 |
|
| 26 |
|
| 27 |
goog.provide('goog.editor.SeamlessField'); |
| 28 |
|
| 29 |
goog.require('goog.cssom.iframe.style'); |
| 30 |
goog.require('goog.dom'); |
| 31 |
goog.require('goog.dom.Range'); |
| 32 |
goog.require('goog.dom.TagName'); |
| 33 |
goog.require('goog.dom.safe'); |
| 34 |
goog.require('goog.editor.BrowserFeature'); |
| 35 |
goog.require('goog.editor.Field'); |
| 36 |
goog.require('goog.editor.icontent'); |
| 37 |
goog.require('goog.editor.icontent.FieldFormatInfo'); |
| 38 |
goog.require('goog.editor.icontent.FieldStyleInfo'); |
| 39 |
goog.require('goog.editor.node'); |
| 40 |
goog.require('goog.events'); |
| 41 |
goog.require('goog.events.EventType'); |
| 42 |
goog.require('goog.html.SafeHtml'); |
| 43 |
goog.require('goog.log'); |
| 44 |
goog.require('goog.style'); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
/** |
| 49 |
* This class encapsulates an editable field that blends in with the |
| 50 |
* surrounding page. |
| 51 |
* To see events fired by this object, please see the base class. |
| 52 |
* |
| 53 |
* @param {string} id An identifer for the field. This is used to find the |
| 54 |
* field and the element associated with this field. |
| 55 |
* @param {Document=} opt_doc The document that the element with the given |
| 56 |
* id can be found it. |
| 57 |
* @constructor |
| 58 |
* @extends {goog.editor.Field} |
| 59 |
*/ |
| 60 |
goog.editor.SeamlessField = function(id, opt_doc) { |
| 61 |
goog.editor.Field.call(this, id, opt_doc); |
| 62 |
}; |
| 63 |
goog.inherits(goog.editor.SeamlessField, goog.editor.Field); |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* @override |
| 68 |
*/ |
| 69 |
goog.editor.SeamlessField.prototype.logger = |
| 70 |
goog.log.getLogger('goog.editor.SeamlessField'); |
| 71 |
|
| 72 |
// Functions dealing with field sizing. |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* The key used for listening for the "dragover" event. |
| 77 |
* @type {goog.events.Key} |
| 78 |
* @private |
| 79 |
*/ |
| 80 |
goog.editor.SeamlessField.prototype.listenForDragOverEventKey_; |
| 81 |
|
| 82 |
|
| 83 |
/** |
| 84 |
* The key used for listening for the iframe "load" event. |
| 85 |
* @type {goog.events.Key} |
| 86 |
* @private |
| 87 |
*/ |
| 88 |
goog.editor.SeamlessField.prototype.listenForIframeLoadEventKey_; |
| 89 |
|
| 90 |
|
| 91 |
/** |
| 92 |
* Sets the min height of this editable field's iframe. Only used in growing |
| 93 |
* mode when an iframe is used. This will cause an immediate field sizing to |
| 94 |
* update the field if necessary based on the new min height. |
| 95 |
* @param {number} height The min height specified as a number of pixels, |
| 96 |
* e.g., 75. |
| 97 |
*/ |
| 98 |
goog.editor.SeamlessField.prototype.setMinHeight = function(height) { |
| 99 |
if (height == this.minHeight_) { |
| 100 |
// Do nothing if the min height isn't changing. |
| 101 |
return; |
| 102 |
} |
| 103 |
this.minHeight_ = height; |
| 104 |
if (this.usesIframe()) { |
| 105 |
this.doFieldSizingGecko(); |
| 106 |
} |
| 107 |
}; |
| 108 |
|
| 109 |
|
| 110 |
/** |
| 111 |
* Whether the field should be rendered with a fixed height, or should expand |
| 112 |
* to fit its contents. |
| 113 |
* @type {boolean} |
| 114 |
* @private |
| 115 |
*/ |
| 116 |
goog.editor.SeamlessField.prototype.isFixedHeight_ = false; |
| 117 |
|
| 118 |
|
| 119 |
/** |
| 120 |
* Whether the fixed-height handling has been overridden manually. |
| 121 |
* @type {boolean} |
| 122 |
* @private |
| 123 |
*/ |
| 124 |
goog.editor.SeamlessField.prototype.isFixedHeightOverridden_ = false; |
| 125 |
|
| 126 |
|
| 127 |
/** |
| 128 |
* @return {boolean} Whether the field should be rendered with a fixed |
| 129 |
* height, or should expand to fit its contents. |
| 130 |
* @override |
| 131 |
*/ |
| 132 |
goog.editor.SeamlessField.prototype.isFixedHeight = function() { |
| 133 |
return this.isFixedHeight_; |
| 134 |
}; |
| 135 |
|
| 136 |
|
| 137 |
/** |
| 138 |
* @param {boolean} newVal Explicitly set whether the field should be |
| 139 |
* of a fixed-height. This overrides auto-detection. |
| 140 |
*/ |
| 141 |
goog.editor.SeamlessField.prototype.overrideFixedHeight = function(newVal) { |
| 142 |
this.isFixedHeight_ = newVal; |
| 143 |
this.isFixedHeightOverridden_ = true; |
| 144 |
}; |
| 145 |
|
| 146 |
|
| 147 |
/** |
| 148 |
* Auto-detect whether the current field should have a fixed height. |
| 149 |
* @private |
| 150 |
*/ |
| 151 |
goog.editor.SeamlessField.prototype.autoDetectFixedHeight_ = function() { |
| 152 |
if (!this.isFixedHeightOverridden_) { |
| 153 |
var originalElement = this.getOriginalElement(); |
| 154 |
if (originalElement) { |
| 155 |
this.isFixedHeight_ = |
| 156 |
goog.style.getComputedOverflowY(originalElement) == 'auto'; |
| 157 |
} |
| 158 |
} |
| 159 |
}; |
| 160 |
|
| 161 |
|
| 162 |
/** |
| 163 |
* Resize the iframe in response to the wrapper div changing size. |
| 164 |
* @private |
| 165 |
*/ |
| 166 |
goog.editor.SeamlessField.prototype.handleOuterDocChange_ = function() { |
| 167 |
if (this.isEventStopped(goog.editor.Field.EventType.CHANGE)) { |
| 168 |
return; |
| 169 |
} |
| 170 |
this.sizeIframeToWrapperGecko_(); |
| 171 |
}; |
| 172 |
|
| 173 |
|
| 174 |
/** |
| 175 |
* Sizes the iframe to its body's height. |
| 176 |
* @private |
| 177 |
*/ |
| 178 |
goog.editor.SeamlessField.prototype.sizeIframeToBodyHeightGecko_ = function() { |
| 179 |
if (this.acquireSizeIframeLockGecko_()) { |
| 180 |
var resized = false; |
| 181 |
var ifr = this.getEditableIframe(); |
| 182 |
if (ifr) { |
| 183 |
var fieldHeight = this.getIframeBodyHeightGecko_(); |
| 184 |
|
| 185 |
if (this.minHeight_) { |
| 186 |
fieldHeight = Math.max(fieldHeight, this.minHeight_); |
| 187 |
} |
| 188 |
if (parseInt(goog.style.getStyle(ifr, 'height'), 10) != fieldHeight) { |
| 189 |
ifr.style.height = fieldHeight + 'px'; |
| 190 |
resized = true; |
| 191 |
} |
| 192 |
} |
| 193 |
this.releaseSizeIframeLockGecko_(); |
| 194 |
if (resized) { |
| 195 |
this.dispatchEvent(goog.editor.Field.EventType.IFRAME_RESIZED); |
| 196 |
} |
| 197 |
} |
| 198 |
}; |
| 199 |
|
| 200 |
|
| 201 |
/** |
| 202 |
* @return {number} The height of the editable iframe's body. |
| 203 |
* @private |
| 204 |
*/ |
| 205 |
goog.editor.SeamlessField.prototype.getIframeBodyHeightGecko_ = function() { |
| 206 |
var ifr = this.getEditableIframe(); |
| 207 |
var body = ifr.contentDocument.body; |
| 208 |
var htmlElement = /** @type {!HTMLElement} */ (body.parentNode); |
| 209 |
|
| 210 |
|
| 211 |
// If the iframe's height is 0, then the offsetHeight/scrollHeight of the |
| 212 |
// HTML element in the iframe can be totally wack (i.e. too large |
| 213 |
// by 50-500px). Also, in standard's mode the clientHeight is 0. |
| 214 |
if (parseInt(goog.style.getStyle(ifr, 'height'), 10) === 0) { |
| 215 |
goog.style.setStyle(ifr, 'height', 1 + 'px'); |
| 216 |
} |
| 217 |
|
| 218 |
var fieldHeight; |
| 219 |
if (goog.editor.node.isStandardsMode(body)) { |
| 220 |
// If in standards-mode, |
| 221 |
// grab the HTML element as it will contain all the field's |
| 222 |
// contents. The body's height, for example, will not include that of |
| 223 |
// floated images at the bottom in standards mode. |
| 224 |
// Note that this value include all scrollbars *except* for scrollbars |
| 225 |
// on the HTML element itself. |
| 226 |
fieldHeight = htmlElement.offsetHeight; |
| 227 |
} else { |
| 228 |
// In quirks-mode, the body-element always seems |
| 229 |
// to size to the containing window. The html-element however, |
| 230 |
// sizes to the content, and can thus end up with a value smaller |
| 231 |
// than its child body-element if the content is shrinking. |
| 232 |
// We want to make the iframe shrink too when the content shrinks, |
| 233 |
// so rather than size the iframe to the body-element, size it to |
| 234 |
// the html-element. |
| 235 |
fieldHeight = htmlElement.scrollHeight; |
| 236 |
|
| 237 |
// If there is a horizontal scroll, add in the thickness of the |
| 238 |
// scrollbar. |
| 239 |
if (htmlElement.clientHeight != htmlElement.offsetHeight) { |
| 240 |
fieldHeight += goog.editor.SeamlessField.getScrollbarWidth_(); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
return fieldHeight; |
| 245 |
}; |
| 246 |
|
| 247 |
|
| 248 |
/** |
| 249 |
* Grabs the width of a scrollbar from the browser and caches the result. |
| 250 |
* @return {number} The scrollbar width in pixels. |
| 251 |
* @private |
| 252 |
*/ |
| 253 |
goog.editor.SeamlessField.getScrollbarWidth_ = function() { |
| 254 |
return goog.editor.SeamlessField.scrollbarWidth_ || |
| 255 |
(goog.editor.SeamlessField.scrollbarWidth_ = |
| 256 |
goog.style.getScrollbarWidth()); |
| 257 |
}; |
| 258 |
|
| 259 |
|
| 260 |
/** |
| 261 |
* Sizes the iframe to its container div's width. The width of the div |
| 262 |
* is controlled by its containing context, not by its contents. |
| 263 |
* if it extends outside of it's contents, then it gets a horizontal scroll. |
| 264 |
* @private |
| 265 |
*/ |
| 266 |
goog.editor.SeamlessField.prototype.sizeIframeToWrapperGecko_ = function() { |
| 267 |
if (this.acquireSizeIframeLockGecko_()) { |
| 268 |
var ifr = this.getEditableIframe(); |
| 269 |
var field = this.getElement(); |
| 270 |
var resized = false; |
| 271 |
if (ifr && field) { |
| 272 |
var fieldPaddingBox; |
| 273 |
var widthDiv = /** @type {!HTMLElement} */ (ifr.parentNode); |
| 274 |
|
| 275 |
var width = widthDiv.offsetWidth; |
| 276 |
if (parseInt(goog.style.getStyle(ifr, 'width'), 10) != width) { |
| 277 |
fieldPaddingBox = goog.style.getPaddingBox(field); |
| 278 |
ifr.style.width = width + 'px'; |
| 279 |
field.style.width = |
| 280 |
width - fieldPaddingBox.left - fieldPaddingBox.right + 'px'; |
| 281 |
resized = true; |
| 282 |
} |
| 283 |
|
| 284 |
var height = widthDiv.offsetHeight; |
| 285 |
if (this.isFixedHeight() && |
| 286 |
parseInt(goog.style.getStyle(ifr, 'height'), 10) != height) { |
| 287 |
if (!fieldPaddingBox) { |
| 288 |
fieldPaddingBox = goog.style.getPaddingBox(field); |
| 289 |
} |
| 290 |
ifr.style.height = height + 'px'; |
| 291 |
field.style.height = |
| 292 |
height - fieldPaddingBox.top - fieldPaddingBox.bottom + 'px'; |
| 293 |
resized = true; |
| 294 |
} |
| 295 |
} |
| 296 |
this.releaseSizeIframeLockGecko_(); |
| 297 |
if (resized) { |
| 298 |
this.dispatchEvent(goog.editor.Field.EventType.IFRAME_RESIZED); |
| 299 |
} |
| 300 |
} |
| 301 |
}; |
| 302 |
|
| 303 |
|
| 304 |
/** |
| 305 |
* Perform all the sizing immediately. |
| 306 |
*/ |
| 307 |
goog.editor.SeamlessField.prototype.doFieldSizingGecko = function() { |
| 308 |
// Because doFieldSizingGecko can be called after a setTimeout |
| 309 |
// it is possible that the field has been destroyed before this call |
| 310 |
// to do the sizing is executed. Check for field existence and do nothing |
| 311 |
// if it has already been destroyed. |
| 312 |
if (this.getElement()) { |
| 313 |
// The order of operations is important here. Sizing the iframe to the |
| 314 |
// wrapper could cause the width to change, which could change the line |
| 315 |
// wrapping, which could change the body height. So we need to do that |
| 316 |
// first, then size the iframe to fit the body height. |
| 317 |
this.sizeIframeToWrapperGecko_(); |
| 318 |
if (!this.isFixedHeight()) { |
| 319 |
this.sizeIframeToBodyHeightGecko_(); |
| 320 |
} |
| 321 |
} |
| 322 |
}; |
| 323 |
|
| 324 |
|
| 325 |
/** |
| 326 |
* Acquires a lock on resizing the field iframe. This is used to ensure that |
| 327 |
* modifications we make while in a mutation event handler don't cause |
| 328 |
* infinite loops. |
| 329 |
* @return {boolean} False if the lock is already acquired. |
| 330 |
* @private |
| 331 |
*/ |
| 332 |
goog.editor.SeamlessField.prototype.acquireSizeIframeLockGecko_ = function() { |
| 333 |
if (this.sizeIframeLock_) { |
| 334 |
return false; |
| 335 |
} |
| 336 |
return this.sizeIframeLock_ = true; |
| 337 |
}; |
| 338 |
|
| 339 |
|
| 340 |
/** |
| 341 |
* Releases a lock on resizing the field iframe. This is used to ensure that |
| 342 |
* modifications we make while in a mutation event handler don't cause |
| 343 |
* infinite loops. |
| 344 |
* @private |
| 345 |
*/ |
| 346 |
goog.editor.SeamlessField.prototype.releaseSizeIframeLockGecko_ = function() { |
| 347 |
this.sizeIframeLock_ = false; |
| 348 |
}; |
| 349 |
|
| 350 |
|
| 351 |
// Functions dealing with blending in with the surrounding page. |
| 352 |
|
| 353 |
|
| 354 |
/** |
| 355 |
* String containing the css rules that, if applied to a document's body, |
| 356 |
* would style that body as if it were the original element we made editable. |
| 357 |
* See goog.cssom.iframe.style.getElementContext for more details. |
| 358 |
* @type {string} |
| 359 |
* @private |
| 360 |
*/ |
| 361 |
goog.editor.SeamlessField.prototype.iframeableCss_ = ''; |
| 362 |
|
| 363 |
|
| 364 |
/** |
| 365 |
* Gets the css rules that should be used to style an iframe's body as if it |
| 366 |
* were the original element that we made editable. |
| 367 |
* @param {boolean=} opt_forceRegeneration Set to true to not read the cached |
| 368 |
* copy and instead completely regenerate the css rules. |
| 369 |
* @return {string} The string containing the css rules to use. |
| 370 |
*/ |
| 371 |
goog.editor.SeamlessField.prototype.getIframeableCss = function( |
| 372 |
opt_forceRegeneration) { |
| 373 |
if (!this.iframeableCss_ || opt_forceRegeneration) { |
| 374 |
var originalElement = this.getOriginalElement(); |
| 375 |
if (originalElement) { |
| 376 |
this.iframeableCss_ = goog.cssom.iframe.style.getElementContext( |
| 377 |
originalElement, opt_forceRegeneration); |
| 378 |
} |
| 379 |
} |
| 380 |
return this.iframeableCss_; |
| 381 |
}; |
| 382 |
|
| 383 |
|
| 384 |
/** |
| 385 |
* Sets the css rules that should be used inside the editable iframe. |
| 386 |
* Note: to clear the css cache between makeNotEditable/makeEditable, |
| 387 |
* call this with "" as iframeableCss. |
| 388 |
* TODO(user): Unify all these css setting methods + Nick's open |
| 389 |
* CL. This is getting ridiculous. |
| 390 |
* @param {string} iframeableCss String containing the css rules to use. |
| 391 |
*/ |
| 392 |
goog.editor.SeamlessField.prototype.setIframeableCss = function(iframeableCss) { |
| 393 |
this.iframeableCss_ = iframeableCss; |
| 394 |
}; |
| 395 |
|
| 396 |
|
| 397 |
/** |
| 398 |
* Used to ensure that CSS stylings are only installed once for none |
| 399 |
* iframe seamless mode. |
| 400 |
* TODO(user): Make it a formal part of the API that you can only |
| 401 |
* set one set of styles globally. |
| 402 |
* In seamless, non-iframe mode, all the stylings would go in the |
| 403 |
* same document and conflict. |
| 404 |
* @type {boolean} |
| 405 |
* @private |
| 406 |
*/ |
| 407 |
goog.editor.SeamlessField.haveInstalledCss_ = false; |
| 408 |
|
| 409 |
|
| 410 |
// Overridden methods. |
| 411 |
|
| 412 |
|
| 413 |
/** @override */ |
| 414 |
goog.editor.SeamlessField.prototype.usesIframe = function() { |
| 415 |
// TODO(user): Switch Firefox to using contentEditable |
| 416 |
// rather than designMode iframe once contentEditable support |
| 417 |
// is less buggy. |
| 418 |
return !goog.editor.BrowserFeature.HAS_CONTENT_EDITABLE; |
| 419 |
}; |
| 420 |
|
| 421 |
|
| 422 |
/** @override */ |
| 423 |
goog.editor.SeamlessField.prototype.setupMutationEventHandlersGecko = |
| 424 |
function() { |
| 425 |
goog.editor.SeamlessField.superClass_.setupMutationEventHandlersGecko.call( |
| 426 |
this); |
| 427 |
|
| 428 |
if (this.usesIframe()) { |
| 429 |
var iframe = this.getEditableIframe(); |
| 430 |
var outerDoc = iframe.ownerDocument; |
| 431 |
this.eventRegister.listen( |
| 432 |
outerDoc, goog.editor.Field.MUTATION_EVENTS_GECKO, |
| 433 |
this.handleOuterDocChange_, true); |
| 434 |
|
| 435 |
// If the images load after we do the initial sizing, then this will |
| 436 |
// force a field resize. |
| 437 |
this.listenForIframeLoadEventKey_ = goog.events.listenOnce( |
| 438 |
this.getEditableDomHelper().getWindow(), goog.events.EventType.LOAD, |
| 439 |
this.sizeIframeToBodyHeightGecko_, true, this); |
| 440 |
|
| 441 |
this.eventRegister.listen( |
| 442 |
outerDoc, 'DOMAttrModified', |
| 443 |
goog.bind(this.handleDomAttrChange, this, this.handleOuterDocChange_), |
| 444 |
true); |
| 445 |
} |
| 446 |
}; |
| 447 |
|
| 448 |
|
| 449 |
/** @override */ |
| 450 |
goog.editor.SeamlessField.prototype.handleChange = function() { |
| 451 |
if (this.isEventStopped(goog.editor.Field.EventType.CHANGE)) { |
| 452 |
return; |
| 453 |
} |
| 454 |
|
| 455 |
goog.editor.SeamlessField.superClass_.handleChange.call(this); |
| 456 |
|
| 457 |
if (this.usesIframe()) { |
| 458 |
this.sizeIframeToBodyHeightGecko_(); |
| 459 |
} |
| 460 |
}; |
| 461 |
|
| 462 |
|
| 463 |
/** @override */ |
| 464 |
goog.editor.SeamlessField.prototype.dispatchBlur = function() { |
| 465 |
if (this.isEventStopped(goog.editor.Field.EventType.BLUR)) { |
| 466 |
return; |
| 467 |
} |
| 468 |
|
| 469 |
goog.editor.SeamlessField.superClass_.dispatchBlur.call(this); |
| 470 |
|
| 471 |
// Clear the selection and restore the current range back after collapsing |
| 472 |
// it. The ideal solution would have been to just leave the range intact; but |
| 473 |
// when there are multiple fields present on the page, its important that |
| 474 |
// the selection isn't retained when we switch between the fields. We also |
| 475 |
// have to make sure that the cursor position is retained when we tab in and |
| 476 |
// out of a field and our approach addresses both these issues. |
| 477 |
// Another point to note is that we do it on a setTimeout to allow for |
| 478 |
// DOM modifications on blur. Otherwise, something like setLoremIpsum will |
| 479 |
// leave a blinking cursor in the field even though it's blurred. |
| 480 |
if (!goog.editor.BrowserFeature.HAS_CONTENT_EDITABLE && |
| 481 |
!goog.editor.BrowserFeature.CLEARS_SELECTION_WHEN_FOCUS_LEAVES) { |
| 482 |
var win = this.getEditableDomHelper().getWindow(); |
| 483 |
var dragging = false; |
| 484 |
goog.events.unlistenByKey(this.listenForDragOverEventKey_); |
| 485 |
this.listenForDragOverEventKey_ = goog.events.listenOnce( |
| 486 |
win.document.body, 'dragover', function() { dragging = true; }); |
| 487 |
goog.global.setTimeout(goog.bind(function() { |
| 488 |
// Do not clear the selection if we're only dragging text. |
| 489 |
// This addresses a bug on FF1.5/linux where dragging fires a blur, |
| 490 |
// but clearing the selection confuses Firefox's drag-and-drop |
| 491 |
// implementation. For more info, see http://b/1061064 |
| 492 |
if (!dragging) { |
| 493 |
if (this.editableDomHelper) { |
| 494 |
var rng = this.getRange(); |
| 495 |
|
| 496 |
// If there are multiple fields on a page, we need to make sure that |
| 497 |
// the selection isn't retained when we switch between fields. We |
| 498 |
// could have collapsed the range but there is a bug in GECKO where |
| 499 |
// the selection stays highlighted even though its backing range is |
| 500 |
// collapsed (http://b/1390115). To get around this, we clear the |
| 501 |
// selection and restore the collapsed range back in. Restoring the |
| 502 |
// range is important so that the cursor stays intact when we tab out |
| 503 |
// and into a field (See http://b/1790301 for additional details on |
| 504 |
// this). |
| 505 |
var iframeWindow = this.editableDomHelper.getWindow(); |
| 506 |
goog.dom.Range.clearSelection(iframeWindow); |
| 507 |
|
| 508 |
if (rng) { |
| 509 |
rng.collapse(true); |
| 510 |
rng.select(); |
| 511 |
} |
| 512 |
} |
| 513 |
} |
| 514 |
}, this), 0); |
| 515 |
} |
| 516 |
}; |
| 517 |
|
| 518 |
|
| 519 |
/** @override */ |
| 520 |
goog.editor.SeamlessField.prototype.turnOnDesignModeGecko = function() { |
| 521 |
goog.editor.SeamlessField.superClass_.turnOnDesignModeGecko.call(this); |
| 522 |
var doc = this.getEditableDomHelper().getDocument(); |
| 523 |
|
| 524 |
doc.execCommand('enableInlineTableEditing', false, 'false'); |
| 525 |
doc.execCommand('enableObjectResizing', false, 'false'); |
| 526 |
}; |
| 527 |
|
| 528 |
|
| 529 |
/** @override */ |
| 530 |
goog.editor.SeamlessField.prototype.installStyles = function() { |
| 531 |
if (!this.usesIframe()) { |
| 532 |
if (!goog.editor.SeamlessField.haveInstalledCss_) { |
| 533 |
if (this.cssStyles.getTypedStringValue()) { |
| 534 |
goog.style.installSafeStyleSheet(this.cssStyles, this.getElement()); |
| 535 |
} |
| 536 |
|
| 537 |
// TODO(user): this should be reset to false when the editor is quit. |
| 538 |
// In non-iframe mode, CSS styles should only be instaled once. |
| 539 |
goog.editor.SeamlessField.haveInstalledCss_ = true; |
| 540 |
} |
| 541 |
} |
| 542 |
}; |
| 543 |
|
| 544 |
|
| 545 |
/** @override */ |
| 546 |
goog.editor.SeamlessField.prototype.makeEditableInternal = function( |
| 547 |
opt_iframeSrc) { |
| 548 |
if (this.usesIframe()) { |
| 549 |
goog.editor.SeamlessField.superClass_.makeEditableInternal.call( |
| 550 |
this, opt_iframeSrc); |
| 551 |
} else { |
| 552 |
var field = this.getOriginalElement(); |
| 553 |
if (field) { |
| 554 |
this.setupFieldObject(field); |
| 555 |
field.contentEditable = true; |
| 556 |
|
| 557 |
this.injectContents(field.innerHTML, field); |
| 558 |
|
| 559 |
this.handleFieldLoad(); |
| 560 |
} |
| 561 |
} |
| 562 |
}; |
| 563 |
|
| 564 |
|
| 565 |
/** @override */ |
| 566 |
goog.editor.SeamlessField.prototype.handleFieldLoad = function() { |
| 567 |
if (this.usesIframe()) { |
| 568 |
// If the CSS inheriting code screws up (e.g. makes fonts too large) and |
| 569 |
// the field is sized off in goog.editor.Field.makeIframeField, then we need |
| 570 |
// to size it correctly, but it needs to be visible for the browser |
| 571 |
// to have fully rendered it. We need to put this on a timeout to give |
| 572 |
// the browser time to render. |
| 573 |
var self = this; |
| 574 |
goog.global.setTimeout(function() { self.doFieldSizingGecko(); }, 0); |
| 575 |
} |
| 576 |
goog.editor.SeamlessField.superClass_.handleFieldLoad.call(this); |
| 577 |
}; |
| 578 |
|
| 579 |
|
| 580 |
/** @override */ |
| 581 |
goog.editor.SeamlessField.prototype.getIframeAttributes = function() { |
| 582 |
return {'frameBorder': 0, 'style': 'padding:0;'}; |
| 583 |
}; |
| 584 |
|
| 585 |
|
| 586 |
/** @override */ |
| 587 |
goog.editor.SeamlessField.prototype.attachIframe = function(iframe) { |
| 588 |
this.autoDetectFixedHeight_(); |
| 589 |
var field = this.getOriginalElement(); |
| 590 |
var dh = goog.dom.getDomHelper(field); |
| 591 |
|
| 592 |
// Grab the width/height values of the field before modifying any CSS |
| 593 |
// as some of the modifications affect its size (e.g. innerHTML='') |
| 594 |
// Here, we set the size of the field to fixed so there's not too much |
| 595 |
// jiggling when we set the innerHTML of the field. |
| 596 |
var oldWidth = field.style.width; |
| 597 |
var oldHeight = field.style.height; |
| 598 |
goog.style.setStyle(field, 'visibility', 'hidden'); |
| 599 |
|
| 600 |
// If there is a floated element at the bottom of the field, |
| 601 |
// then it needs a clearing div at the end to cause the clientHeight |
| 602 |
// to contain the entire field. |
| 603 |
// Also, with css re-writing, the margins of the first/last |
| 604 |
// paragraph don't seem to get included in the clientHeight. Specifically, |
| 605 |
// the extra divs below force the field's clientHeight to include the |
| 606 |
// margins on the first and last elements contained within it. |
| 607 |
var startDiv = dh.createDom( |
| 608 |
goog.dom.TagName.DIV, |
| 609 |
{'style': 'height:0;clear:both', 'innerHTML': ' '}); |
| 610 |
var endDiv = startDiv.cloneNode(true); |
| 611 |
field.insertBefore(startDiv, field.firstChild); |
| 612 |
goog.dom.appendChild(field, endDiv); |
| 613 |
|
| 614 |
var contentBox = goog.style.getContentBoxSize(field); |
| 615 |
var width = contentBox.width; |
| 616 |
var height = contentBox.height; |
| 617 |
|
| 618 |
var html = ''; |
| 619 |
if (this.isFixedHeight()) { |
| 620 |
html = ' '; |
| 621 |
|
| 622 |
goog.style.setStyle(field, 'position', 'relative'); |
| 623 |
goog.style.setStyle(field, 'overflow', 'visible'); |
| 624 |
|
| 625 |
goog.style.setStyle(iframe, 'position', 'absolute'); |
| 626 |
goog.style.setStyle(iframe, 'top', '0'); |
| 627 |
goog.style.setStyle(iframe, 'left', '0'); |
| 628 |
} |
| 629 |
goog.style.setSize(field, width, height); |
| 630 |
|
| 631 |
// In strict mode, browsers put blank space at the bottom and right |
| 632 |
// if a field when it has an iframe child, to fill up the remaining line |
| 633 |
// height. So make the line height = 0. |
| 634 |
if (goog.editor.node.isStandardsMode(field)) { |
| 635 |
this.originalFieldLineHeight_ = field.style.lineHeight; |
| 636 |
goog.style.setStyle(field, 'lineHeight', '0'); |
| 637 |
} |
| 638 |
|
| 639 |
goog.editor.node.replaceInnerHtml(field, html); |
| 640 |
// Set the initial size |
| 641 |
goog.style.setSize(iframe, width, height); |
| 642 |
goog.style.setSize(field, oldWidth, oldHeight); |
| 643 |
goog.style.setStyle(field, 'visibility', ''); |
| 644 |
goog.dom.appendChild(field, iframe); |
| 645 |
|
| 646 |
// Only write if its not IE HTTPS in which case we're waiting for load. |
| 647 |
if (!this.shouldLoadAsynchronously()) { |
| 648 |
var doc = iframe.contentWindow.document; |
| 649 |
if (goog.editor.node.isStandardsMode(iframe.ownerDocument)) { |
| 650 |
doc.open(); |
| 651 |
var emptyHtml = goog.html.SafeHtml.concat( |
| 652 |
goog.html.SafeHtml.DOCTYPE_HTML, goog.html.SafeHtml.create('html')); |
| 653 |
goog.dom.safe.documentWrite(doc, emptyHtml); |
| 654 |
doc.close(); |
| 655 |
} |
| 656 |
} |
| 657 |
}; |
| 658 |
|
| 659 |
|
| 660 |
/** @override */ |
| 661 |
goog.editor.SeamlessField.prototype.getFieldFormatInfo = function(extraStyles) { |
| 662 |
var originalElement = this.getOriginalElement(); |
| 663 |
if (originalElement) { |
| 664 |
return new goog.editor.icontent.FieldFormatInfo( |
| 665 |
this.id, goog.editor.node.isStandardsMode(originalElement), true, |
| 666 |
this.isFixedHeight(), extraStyles); |
| 667 |
} |
| 668 |
throw new Error('no field'); |
| 669 |
}; |
| 670 |
|
| 671 |
|
| 672 |
/** @override */ |
| 673 |
goog.editor.SeamlessField.prototype.writeIframeContent = function( |
| 674 |
iframe, innerHtml, extraStyles) { |
| 675 |
// For seamless iframes, hide the iframe while we're laying it out to |
| 676 |
// prevent the flicker. |
| 677 |
goog.style.setStyle(iframe, 'visibility', 'hidden'); |
| 678 |
var formatInfo = this.getFieldFormatInfo(extraStyles); |
| 679 |
var styleInfo = new goog.editor.icontent.FieldStyleInfo( |
| 680 |
this.getOriginalElement(), |
| 681 |
this.cssStyles.getTypedStringValue() + this.getIframeableCss()); |
| 682 |
goog.editor.icontent.writeNormalInitialBlendedIframe( |
| 683 |
formatInfo, innerHtml, styleInfo, iframe); |
| 684 |
this.doFieldSizingGecko(); |
| 685 |
goog.style.setStyle(iframe, 'visibility', 'visible'); |
| 686 |
}; |
| 687 |
|
| 688 |
|
| 689 |
/** @override */ |
| 690 |
goog.editor.SeamlessField.prototype.restoreDom = function() { |
| 691 |
// TODO(user): Consider only removing the iframe if we are |
| 692 |
// restoring the original node. |
| 693 |
if (this.usesIframe()) { |
| 694 |
goog.dom.removeNode(this.getEditableIframe()); |
| 695 |
} |
| 696 |
}; |
| 697 |
|
| 698 |
|
| 699 |
/** @override */ |
| 700 |
goog.editor.SeamlessField.prototype.clearListeners = function() { |
| 701 |
goog.events.unlistenByKey(this.listenForDragOverEventKey_); |
| 702 |
goog.events.unlistenByKey(this.listenForIframeLoadEventKey_); |
| 703 |
|
| 704 |
goog.editor.SeamlessField.base(this, 'clearListeners'); |
| 705 |
}; |