| 1 |
// Copyright 2007 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 Plain text spell checker implementation. |
| 17 |
* |
| 18 |
* @author [email protected] (Emil A Eklund) |
| 19 |
* @see ../demos/plaintextspellchecker.html |
| 20 |
*/ |
| 21 |
|
| 22 |
goog.provide('goog.ui.PlainTextSpellChecker'); |
| 23 |
|
| 24 |
goog.require('goog.Timer'); |
| 25 |
goog.require('goog.a11y.aria'); |
| 26 |
goog.require('goog.asserts'); |
| 27 |
goog.require('goog.dom'); |
| 28 |
goog.require('goog.dom.TagName'); |
| 29 |
goog.require('goog.events.EventHandler'); |
| 30 |
goog.require('goog.events.EventType'); |
| 31 |
goog.require('goog.events.KeyCodes'); |
| 32 |
goog.require('goog.events.KeyHandler'); |
| 33 |
goog.require('goog.spell.SpellCheck'); |
| 34 |
goog.require('goog.style'); |
| 35 |
goog.require('goog.ui.AbstractSpellChecker'); |
| 36 |
goog.require('goog.ui.Component'); |
| 37 |
goog.require('goog.userAgent'); |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* Plain text spell checker implementation. |
| 43 |
* |
| 44 |
* @param {goog.spell.SpellCheck} handler Instance of the SpellCheckHandler |
| 45 |
* support object to use. A single instance can be shared by multiple |
| 46 |
* editor components. |
| 47 |
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper. |
| 48 |
* @constructor |
| 49 |
* @extends {goog.ui.AbstractSpellChecker} |
| 50 |
* @final |
| 51 |
*/ |
| 52 |
goog.ui.PlainTextSpellChecker = function(handler, opt_domHelper) { |
| 53 |
goog.ui.AbstractSpellChecker.call(this, handler, opt_domHelper); |
| 54 |
|
| 55 |
/** |
| 56 |
* Correction UI container. |
| 57 |
* @private {!HTMLDivElement} |
| 58 |
*/ |
| 59 |
this.overlay_ = this.getDomHelper().createDom(goog.dom.TagName.DIV); |
| 60 |
goog.style.setPreWrap(this.overlay_); |
| 61 |
|
| 62 |
/** |
| 63 |
* Bound async function (to avoid rebinding it on every call). |
| 64 |
* @type {Function} |
| 65 |
* @private |
| 66 |
*/ |
| 67 |
this.boundContinueAsyncFn_ = goog.bind(this.continueAsync_, this); |
| 68 |
|
| 69 |
/** |
| 70 |
* Regular expression for matching line breaks. |
| 71 |
* @type {RegExp} |
| 72 |
* @private |
| 73 |
*/ |
| 74 |
this.endOfLineMatcher_ = new RegExp('(.*)(\n|\r\n){0,1}', 'g'); |
| 75 |
}; |
| 76 |
goog.inherits(goog.ui.PlainTextSpellChecker, goog.ui.AbstractSpellChecker); |
| 77 |
|
| 78 |
|
| 79 |
/** |
| 80 |
* Class name for invalid words. |
| 81 |
* @type {string} |
| 82 |
*/ |
| 83 |
goog.ui.PlainTextSpellChecker.prototype.invalidWordClassName = |
| 84 |
goog.getCssName('goog-spellcheck-invalidword'); |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* Class name for corrected words. |
| 89 |
* @type {string} |
| 90 |
*/ |
| 91 |
goog.ui.PlainTextSpellChecker.prototype.correctedWordClassName = |
| 92 |
goog.getCssName('goog-spellcheck-correctedword'); |
| 93 |
|
| 94 |
|
| 95 |
/** |
| 96 |
* Class name for correction pane. |
| 97 |
* @type {string} |
| 98 |
*/ |
| 99 |
goog.ui.PlainTextSpellChecker.prototype.correctionPaneClassName = |
| 100 |
goog.getCssName('goog-spellcheck-correctionpane'); |
| 101 |
|
| 102 |
|
| 103 |
/** |
| 104 |
* Number of words to scan to precharge the dictionary. |
| 105 |
* @type {number} |
| 106 |
* @private |
| 107 |
*/ |
| 108 |
goog.ui.PlainTextSpellChecker.prototype.dictionaryPreScanSize_ = 1000; |
| 109 |
|
| 110 |
|
| 111 |
/** |
| 112 |
* Size of window. Used to check if a resize operation actually changed the size |
| 113 |
* of the window. |
| 114 |
* @type {goog.math.Size|undefined} |
| 115 |
* @private |
| 116 |
*/ |
| 117 |
goog.ui.PlainTextSpellChecker.prototype.winSize_; |
| 118 |
|
| 119 |
|
| 120 |
/** |
| 121 |
* Event handler for listening to events without leaking. |
| 122 |
* @type {goog.events.EventHandler|undefined} |
| 123 |
* @private |
| 124 |
*/ |
| 125 |
goog.ui.PlainTextSpellChecker.prototype.eventHandler_; |
| 126 |
|
| 127 |
|
| 128 |
/** |
| 129 |
* The object handling keyboard events. |
| 130 |
* @type {goog.events.KeyHandler|undefined} |
| 131 |
* @private |
| 132 |
*/ |
| 133 |
goog.ui.PlainTextSpellChecker.prototype.keyHandler_; |
| 134 |
|
| 135 |
|
| 136 |
/** @private {number} */ |
| 137 |
goog.ui.PlainTextSpellChecker.prototype.textArrayIndex_; |
| 138 |
|
| 139 |
|
| 140 |
/** @private {!Array<string>} */ |
| 141 |
goog.ui.PlainTextSpellChecker.prototype.textArray_; |
| 142 |
|
| 143 |
|
| 144 |
/** @private {!Array<boolean>} */ |
| 145 |
goog.ui.PlainTextSpellChecker.prototype.textArrayProcess_; |
| 146 |
|
| 147 |
|
| 148 |
/** |
| 149 |
* Creates the initial DOM representation for the component. |
| 150 |
* @override |
| 151 |
*/ |
| 152 |
goog.ui.PlainTextSpellChecker.prototype.createDom = function() { |
| 153 |
this.setElementInternal( |
| 154 |
this.getDomHelper().createElement(goog.dom.TagName.TEXTAREA)); |
| 155 |
}; |
| 156 |
|
| 157 |
|
| 158 |
/** @override */ |
| 159 |
goog.ui.PlainTextSpellChecker.prototype.enterDocument = function() { |
| 160 |
goog.ui.PlainTextSpellChecker.superClass_.enterDocument.call(this); |
| 161 |
|
| 162 |
this.eventHandler_ = new goog.events.EventHandler(this); |
| 163 |
this.keyHandler_ = new goog.events.KeyHandler(this.overlay_); |
| 164 |
|
| 165 |
this.initSuggestionsMenu(); |
| 166 |
this.initAccessibility_(); |
| 167 |
}; |
| 168 |
|
| 169 |
|
| 170 |
/** @override */ |
| 171 |
goog.ui.PlainTextSpellChecker.prototype.exitDocument = function() { |
| 172 |
goog.ui.PlainTextSpellChecker.superClass_.exitDocument.call(this); |
| 173 |
|
| 174 |
if (this.eventHandler_) { |
| 175 |
this.eventHandler_.dispose(); |
| 176 |
this.eventHandler_ = undefined; |
| 177 |
} |
| 178 |
if (this.keyHandler_) { |
| 179 |
this.keyHandler_.dispose(); |
| 180 |
this.keyHandler_ = undefined; |
| 181 |
} |
| 182 |
}; |
| 183 |
|
| 184 |
|
| 185 |
/** |
| 186 |
* Initializes suggestions menu. Populates menu with separator and ignore option |
| 187 |
* that are always valid. Suggestions are later added above the separator. |
| 188 |
* @override |
| 189 |
*/ |
| 190 |
goog.ui.PlainTextSpellChecker.prototype.initSuggestionsMenu = function() { |
| 191 |
goog.ui.PlainTextSpellChecker.superClass_.initSuggestionsMenu.call(this); |
| 192 |
this.eventHandler_.listen( |
| 193 |
/** @type {goog.ui.PopupMenu} */ (this.getMenu()), |
| 194 |
goog.ui.Component.EventType.HIDE, this.onCorrectionHide_); |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
/** |
| 199 |
* Checks spelling for all text and displays correction UI. |
| 200 |
* @override |
| 201 |
*/ |
| 202 |
goog.ui.PlainTextSpellChecker.prototype.check = function() { |
| 203 |
var text = this.getElement().value; |
| 204 |
this.getElement().readOnly = true; |
| 205 |
|
| 206 |
// Prepare and position correction UI. |
| 207 |
goog.dom.removeChildren(this.overlay_); |
| 208 |
this.overlay_.className = this.correctionPaneClassName; |
| 209 |
if (this.getElement().parentNode != this.overlay_.parentNode) { |
| 210 |
this.getElement().parentNode.appendChild(this.overlay_); |
| 211 |
} |
| 212 |
goog.style.setElementShown(this.overlay_, false); |
| 213 |
|
| 214 |
this.preChargeDictionary_(text); |
| 215 |
}; |
| 216 |
|
| 217 |
|
| 218 |
/** |
| 219 |
* Final stage of spell checking - displays the correction UI. |
| 220 |
* @private |
| 221 |
*/ |
| 222 |
goog.ui.PlainTextSpellChecker.prototype.finishCheck_ = function() { |
| 223 |
// Show correction UI. |
| 224 |
this.positionOverlay_(); |
| 225 |
goog.style.setElementShown(this.getElement(), false); |
| 226 |
goog.style.setElementShown(this.overlay_, true); |
| 227 |
|
| 228 |
var eh = this.eventHandler_; |
| 229 |
eh.listen(this.overlay_, goog.events.EventType.CLICK, this.onWordClick_); |
| 230 |
eh.listen( |
| 231 |
/** @type {goog.events.KeyHandler} */ (this.keyHandler_), |
| 232 |
goog.events.KeyHandler.EventType.KEY, this.handleOverlayKeyEvent); |
| 233 |
|
| 234 |
// The position and size of the overlay element needs to be recalculated if |
| 235 |
// the browser window is resized. |
| 236 |
var win = goog.dom.getWindow(this.getDomHelper().getDocument()) || window; |
| 237 |
this.winSize_ = goog.dom.getViewportSize(win); |
| 238 |
eh.listen(win, goog.events.EventType.RESIZE, this.onWindowResize_); |
| 239 |
|
| 240 |
goog.ui.PlainTextSpellChecker.superClass_.check.call(this); |
| 241 |
}; |
| 242 |
|
| 243 |
|
| 244 |
/** |
| 245 |
* Start the scan after the dictionary was loaded. |
| 246 |
* |
| 247 |
* @param {string} text text to process. |
| 248 |
* @private |
| 249 |
*/ |
| 250 |
goog.ui.PlainTextSpellChecker.prototype.preChargeDictionary_ = function(text) { |
| 251 |
this.eventHandler_.listen( |
| 252 |
this.spellCheck, goog.spell.SpellCheck.EventType.READY, |
| 253 |
this.onDictionaryCharged_, true); |
| 254 |
|
| 255 |
this.populateDictionary(text, this.dictionaryPreScanSize_); |
| 256 |
}; |
| 257 |
|
| 258 |
|
| 259 |
/** |
| 260 |
* Loads few initial dictionary words into the cache. |
| 261 |
* |
| 262 |
* @param {goog.events.Event} e goog.spell.SpellCheck.EventType.READY event. |
| 263 |
* @private |
| 264 |
*/ |
| 265 |
goog.ui.PlainTextSpellChecker.prototype.onDictionaryCharged_ = function(e) { |
| 266 |
e.stopPropagation(); |
| 267 |
this.eventHandler_.unlisten( |
| 268 |
this.spellCheck, goog.spell.SpellCheck.EventType.READY, |
| 269 |
this.onDictionaryCharged_, true); |
| 270 |
this.checkAsync_(this.getElement().value); |
| 271 |
}; |
| 272 |
|
| 273 |
|
| 274 |
/** |
| 275 |
* Processes the included and skips the excluded text ranges. |
| 276 |
* @return {goog.ui.AbstractSpellChecker.AsyncResult} Whether the spell |
| 277 |
* checking is pending or done. |
| 278 |
* @private |
| 279 |
*/ |
| 280 |
goog.ui.PlainTextSpellChecker.prototype.spellCheckLoop_ = function() { |
| 281 |
for (var i = this.textArrayIndex_; i < this.textArray_.length; ++i) { |
| 282 |
var text = this.textArray_[i]; |
| 283 |
if (this.textArrayProcess_[i]) { |
| 284 |
var result = this.processTextAsync(this.overlay_, text); |
| 285 |
if (result == goog.ui.AbstractSpellChecker.AsyncResult.PENDING) { |
| 286 |
this.textArrayIndex_ = i + 1; |
| 287 |
goog.Timer.callOnce(this.boundContinueAsyncFn_); |
| 288 |
return result; |
| 289 |
} |
| 290 |
} else { |
| 291 |
this.processRange(this.overlay_, text); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
this.textArray_ = []; |
| 296 |
this.textArrayProcess_ = []; |
| 297 |
|
| 298 |
return goog.ui.AbstractSpellChecker.AsyncResult.DONE; |
| 299 |
}; |
| 300 |
|
| 301 |
|
| 302 |
/** |
| 303 |
* Breaks text into included and excluded ranges using the marker RegExp |
| 304 |
* supplied by the caller. |
| 305 |
* |
| 306 |
* @param {string} text text to process. |
| 307 |
* @private |
| 308 |
*/ |
| 309 |
goog.ui.PlainTextSpellChecker.prototype.initTextArray_ = function(text) { |
| 310 |
if (!this.excludeMarker) { |
| 311 |
this.textArray_ = [text]; |
| 312 |
this.textArrayProcess_ = [true]; |
| 313 |
return; |
| 314 |
} |
| 315 |
|
| 316 |
this.textArray_ = []; |
| 317 |
this.textArrayProcess_ = []; |
| 318 |
this.excludeMarker.lastIndex = 0; |
| 319 |
var stringSegmentStart = 0; |
| 320 |
var result; |
| 321 |
while (result = this.excludeMarker.exec(text)) { |
| 322 |
if (result[0].length == 0) { |
| 323 |
break; |
| 324 |
} |
| 325 |
var excludedRange = result[0]; |
| 326 |
var includedRange = |
| 327 |
text.substr(stringSegmentStart, result.index - stringSegmentStart); |
| 328 |
if (includedRange) { |
| 329 |
this.textArray_.push(includedRange); |
| 330 |
this.textArrayProcess_.push(true); |
| 331 |
} |
| 332 |
this.textArray_.push(excludedRange); |
| 333 |
this.textArrayProcess_.push(false); |
| 334 |
stringSegmentStart = this.excludeMarker.lastIndex; |
| 335 |
} |
| 336 |
|
| 337 |
var leftoverText = text.substr(stringSegmentStart); |
| 338 |
if (leftoverText) { |
| 339 |
this.textArray_.push(leftoverText); |
| 340 |
this.textArrayProcess_.push(true); |
| 341 |
} |
| 342 |
}; |
| 343 |
|
| 344 |
|
| 345 |
/** |
| 346 |
* Starts asynchrnonous spell checking. |
| 347 |
* |
| 348 |
* @param {string} text text to process. |
| 349 |
* @private |
| 350 |
*/ |
| 351 |
goog.ui.PlainTextSpellChecker.prototype.checkAsync_ = function(text) { |
| 352 |
this.initializeAsyncMode(); |
| 353 |
this.initTextArray_(text); |
| 354 |
this.textArrayIndex_ = 0; |
| 355 |
if (this.spellCheckLoop_() == |
| 356 |
goog.ui.AbstractSpellChecker.AsyncResult.PENDING) { |
| 357 |
return; |
| 358 |
} |
| 359 |
this.finishAsyncProcessing(); |
| 360 |
this.finishCheck_(); |
| 361 |
}; |
| 362 |
|
| 363 |
|
| 364 |
/** |
| 365 |
* Continues asynchrnonous spell checking. |
| 366 |
* @private |
| 367 |
*/ |
| 368 |
goog.ui.PlainTextSpellChecker.prototype.continueAsync_ = function() { |
| 369 |
// First finish with the current segment. |
| 370 |
var result = this.continueAsyncProcessing(); |
| 371 |
if (result == goog.ui.AbstractSpellChecker.AsyncResult.PENDING) { |
| 372 |
goog.Timer.callOnce(this.boundContinueAsyncFn_); |
| 373 |
return; |
| 374 |
} |
| 375 |
if (this.spellCheckLoop_() == |
| 376 |
goog.ui.AbstractSpellChecker.AsyncResult.PENDING) { |
| 377 |
return; |
| 378 |
} |
| 379 |
this.finishAsyncProcessing(); |
| 380 |
this.finishCheck_(); |
| 381 |
}; |
| 382 |
|
| 383 |
|
| 384 |
/** |
| 385 |
* Processes word. |
| 386 |
* |
| 387 |
* @param {Node} node Node containing word. |
| 388 |
* @param {string} word Word to process. |
| 389 |
* @param {goog.spell.SpellCheck.WordStatus} status Status of word. |
| 390 |
* @override |
| 391 |
*/ |
| 392 |
goog.ui.PlainTextSpellChecker.prototype.processWord = function( |
| 393 |
node, word, status) { |
| 394 |
node.appendChild(this.createWordElement(word, status)); |
| 395 |
}; |
| 396 |
|
| 397 |
|
| 398 |
/** |
| 399 |
* Processes range of text - recognized words and separators. |
| 400 |
* |
| 401 |
* @param {Node} node Node containing separator. |
| 402 |
* @param {string} text text to process. |
| 403 |
* @override |
| 404 |
*/ |
| 405 |
goog.ui.PlainTextSpellChecker.prototype.processRange = function(node, text) { |
| 406 |
this.endOfLineMatcher_.lastIndex = 0; |
| 407 |
var result; |
| 408 |
while (result = this.endOfLineMatcher_.exec(text)) { |
| 409 |
if (result[0].length == 0) { |
| 410 |
break; |
| 411 |
} |
| 412 |
node.appendChild(this.getDomHelper().createTextNode(result[1])); |
| 413 |
if (result[2]) { |
| 414 |
node.appendChild(this.getDomHelper().createElement(goog.dom.TagName.BR)); |
| 415 |
} |
| 416 |
} |
| 417 |
}; |
| 418 |
|
| 419 |
|
| 420 |
/** |
| 421 |
* Hides correction UI. |
| 422 |
* @override |
| 423 |
*/ |
| 424 |
goog.ui.PlainTextSpellChecker.prototype.resume = function() { |
| 425 |
var wasVisible = this.isVisible(); |
| 426 |
|
| 427 |
goog.ui.PlainTextSpellChecker.superClass_.resume.call(this); |
| 428 |
|
| 429 |
goog.style.setElementShown(this.overlay_, false); |
| 430 |
goog.style.setElementShown(this.getElement(), true); |
| 431 |
this.getElement().readOnly = false; |
| 432 |
|
| 433 |
if (wasVisible) { |
| 434 |
this.getElement().value = goog.dom.getRawTextContent(this.overlay_); |
| 435 |
goog.dom.removeChildren(this.overlay_); |
| 436 |
|
| 437 |
var eh = this.eventHandler_; |
| 438 |
eh.unlisten(this.overlay_, goog.events.EventType.CLICK, this.onWordClick_); |
| 439 |
eh.unlisten( |
| 440 |
/** @type {goog.events.KeyHandler} */ (this.keyHandler_), |
| 441 |
goog.events.KeyHandler.EventType.KEY, this.handleOverlayKeyEvent); |
| 442 |
|
| 443 |
var win = goog.dom.getWindow(this.getDomHelper().getDocument()) || window; |
| 444 |
eh.unlisten(win, goog.events.EventType.RESIZE, this.onWindowResize_); |
| 445 |
} |
| 446 |
}; |
| 447 |
|
| 448 |
|
| 449 |
/** |
| 450 |
* Returns desired element properties for the specified status. |
| 451 |
* |
| 452 |
* @param {goog.spell.SpellCheck.WordStatus} status Status of word. |
| 453 |
* @return {!Object} Properties to apply to word element. |
| 454 |
* @override |
| 455 |
*/ |
| 456 |
goog.ui.PlainTextSpellChecker.prototype.getElementProperties = function( |
| 457 |
status) { |
| 458 |
if (status == goog.spell.SpellCheck.WordStatus.INVALID) { |
| 459 |
return {'class': this.invalidWordClassName}; |
| 460 |
} else if (status == goog.spell.SpellCheck.WordStatus.CORRECTED) { |
| 461 |
return {'class': this.correctedWordClassName}; |
| 462 |
} |
| 463 |
return {'class': ''}; |
| 464 |
}; |
| 465 |
|
| 466 |
|
| 467 |
/** |
| 468 |
* Handles the click events. |
| 469 |
* |
| 470 |
* @param {goog.events.BrowserEvent} event Event object. |
| 471 |
* @private |
| 472 |
*/ |
| 473 |
goog.ui.PlainTextSpellChecker.prototype.onWordClick_ = function(event) { |
| 474 |
if (event.target.className == this.invalidWordClassName || |
| 475 |
event.target.className == this.correctedWordClassName) { |
| 476 |
this.showSuggestionsMenu(/** @type {!Element} */ (event.target), event); |
| 477 |
|
| 478 |
// Prevent document click handler from closing the menu. |
| 479 |
event.stopPropagation(); |
| 480 |
} |
| 481 |
}; |
| 482 |
|
| 483 |
|
| 484 |
/** |
| 485 |
* Handles window resize events. |
| 486 |
* |
| 487 |
* @param {goog.events.BrowserEvent} event Event object. |
| 488 |
* @private |
| 489 |
*/ |
| 490 |
goog.ui.PlainTextSpellChecker.prototype.onWindowResize_ = function(event) { |
| 491 |
var win = goog.dom.getWindow(this.getDomHelper().getDocument()) || window; |
| 492 |
var size = goog.dom.getViewportSize(win); |
| 493 |
|
| 494 |
if (size.width != this.winSize_.width || |
| 495 |
size.height != this.winSize_.height) { |
| 496 |
goog.style.setElementShown(this.overlay_, false); |
| 497 |
goog.style.setElementShown(this.getElement(), true); |
| 498 |
|
| 499 |
// IE requires a slight delay, allowing the resize operation to take effect. |
| 500 |
if (goog.userAgent.IE) { |
| 501 |
goog.Timer.callOnce(this.resizeOverlay_, 100, this); |
| 502 |
} else { |
| 503 |
this.resizeOverlay_(); |
| 504 |
} |
| 505 |
this.winSize_ = size; |
| 506 |
} |
| 507 |
}; |
| 508 |
|
| 509 |
|
| 510 |
/** |
| 511 |
* Resizes overlay to match the size of the bound element then displays the |
| 512 |
* overlay. Helper for {@link #onWindowResize_}. |
| 513 |
* |
| 514 |
* @private |
| 515 |
*/ |
| 516 |
goog.ui.PlainTextSpellChecker.prototype.resizeOverlay_ = function() { |
| 517 |
this.positionOverlay_(); |
| 518 |
goog.style.setElementShown(this.getElement(), false); |
| 519 |
goog.style.setElementShown(this.overlay_, true); |
| 520 |
}; |
| 521 |
|
| 522 |
|
| 523 |
/** |
| 524 |
* Updates the position and size of the overlay to match the original element. |
| 525 |
* |
| 526 |
* @private |
| 527 |
*/ |
| 528 |
goog.ui.PlainTextSpellChecker.prototype.positionOverlay_ = function() { |
| 529 |
goog.style.setPosition( |
| 530 |
this.overlay_, goog.style.getPosition(this.getElement())); |
| 531 |
goog.style.setSize(this.overlay_, goog.style.getSize(this.getElement())); |
| 532 |
}; |
| 533 |
|
| 534 |
|
| 535 |
/** @override */ |
| 536 |
goog.ui.PlainTextSpellChecker.prototype.disposeInternal = function() { |
| 537 |
this.getDomHelper().removeNode(this.overlay_); |
| 538 |
delete this.overlay_; |
| 539 |
delete this.boundContinueAsyncFn_; |
| 540 |
delete this.endOfLineMatcher_; |
| 541 |
goog.ui.PlainTextSpellChecker.superClass_.disposeInternal.call(this); |
| 542 |
}; |
| 543 |
|
| 544 |
|
| 545 |
/** |
| 546 |
* Specify ARIA roles and states as appropriate. |
| 547 |
* @private |
| 548 |
*/ |
| 549 |
goog.ui.PlainTextSpellChecker.prototype.initAccessibility_ = function() { |
| 550 |
goog.asserts.assert( |
| 551 |
this.overlay_, |
| 552 |
'The plain text spell checker DOM element cannot be null.'); |
| 553 |
goog.a11y.aria.setRole(this.overlay_, 'region'); |
| 554 |
goog.a11y.aria.setState(this.overlay_, 'live', 'assertive'); |
| 555 |
this.overlay_.tabIndex = 0; |
| 556 |
|
| 557 |
/** @desc Title for Spell Checker's overlay.*/ |
| 558 |
var MSG_SPELLCHECKER_OVERLAY_TITLE = goog.getMsg('Spell Checker'); |
| 559 |
this.overlay_.title = MSG_SPELLCHECKER_OVERLAY_TITLE; |
| 560 |
}; |
| 561 |
|
| 562 |
|
| 563 |
/** |
| 564 |
* Handles key down for overlay. |
| 565 |
* @param {goog.events.BrowserEvent} e The browser event. |
| 566 |
* @return {boolean} The handled value. |
| 567 |
*/ |
| 568 |
goog.ui.PlainTextSpellChecker.prototype.handleOverlayKeyEvent = function(e) { |
| 569 |
var handled = false; |
| 570 |
switch (e.keyCode) { |
| 571 |
case goog.events.KeyCodes.RIGHT: |
| 572 |
if (e.ctrlKey) { |
| 573 |
handled = this.navigate(goog.ui.AbstractSpellChecker.Direction.NEXT); |
| 574 |
} |
| 575 |
break; |
| 576 |
|
| 577 |
case goog.events.KeyCodes.LEFT: |
| 578 |
if (e.ctrlKey) { |
| 579 |
handled = |
| 580 |
this.navigate(goog.ui.AbstractSpellChecker.Direction.PREVIOUS); |
| 581 |
} |
| 582 |
break; |
| 583 |
|
| 584 |
case goog.events.KeyCodes.DOWN: |
| 585 |
if (this.getFocusedElementIndex()) { |
| 586 |
var el = this.getDomHelper().getElement( |
| 587 |
this.makeElementId(this.getFocusedElementIndex())); |
| 588 |
if (el) { |
| 589 |
var position = goog.style.getPosition(el); |
| 590 |
var size = goog.style.getSize(el); |
| 591 |
position.x += size.width / 2; |
| 592 |
position.y += size.height / 2; |
| 593 |
this.showSuggestionsMenu(el, position); |
| 594 |
handled = true; |
| 595 |
} |
| 596 |
} |
| 597 |
break; |
| 598 |
} |
| 599 |
|
| 600 |
if (handled) { |
| 601 |
e.preventDefault(); |
| 602 |
} |
| 603 |
|
| 604 |
return handled; |
| 605 |
}; |
| 606 |
|
| 607 |
|
| 608 |
/** |
| 609 |
* Handles correction menu actions. |
| 610 |
* |
| 611 |
* @param {goog.events.Event} event Action event. |
| 612 |
* @override |
| 613 |
*/ |
| 614 |
goog.ui.PlainTextSpellChecker.prototype.onCorrectionAction = function(event) { |
| 615 |
goog.ui.PlainTextSpellChecker.superClass_.onCorrectionAction.call( |
| 616 |
this, event); |
| 617 |
|
| 618 |
// In case of editWord base class has already set the focus (on the input), |
| 619 |
// otherwise set the focus back on the word. |
| 620 |
if (event.target != this.getMenuEdit()) { |
| 621 |
this.reFocus_(); |
| 622 |
} |
| 623 |
}; |
| 624 |
|
| 625 |
|
| 626 |
/** |
| 627 |
* Restores focus when the suggestion menu is hidden. |
| 628 |
* |
| 629 |
* @param {goog.events.BrowserEvent} event Blur event. |
| 630 |
* @private |
| 631 |
*/ |
| 632 |
goog.ui.PlainTextSpellChecker.prototype.onCorrectionHide_ = function(event) { |
| 633 |
this.reFocus_(); |
| 634 |
}; |
| 635 |
|
| 636 |
|
| 637 |
/** |
| 638 |
* Sets the focus back on the previously focused word element. |
| 639 |
* @private |
| 640 |
*/ |
| 641 |
goog.ui.PlainTextSpellChecker.prototype.reFocus_ = function() { |
| 642 |
var el = this.getElementByIndex(this.getFocusedElementIndex()); |
| 643 |
if (el) { |
| 644 |
el.focus(); |
| 645 |
} else { |
| 646 |
this.overlay_.focus(); |
| 647 |
} |
| 648 |
}; |