| 1 | /* |
|---|
| 2 | Spell Checking Library |
|---|
| 3 | $Id$ |
|---|
| 4 | |
|---|
| 5 | Copyright (c) 2006, Six Apart, Ltd. |
|---|
| 6 | All rights reserved. |
|---|
| 7 | |
|---|
| 8 | Redistribution and use in source and binary forms, with or without |
|---|
| 9 | modification, are permitted provided that the following conditions are |
|---|
| 10 | met: |
|---|
| 11 | |
|---|
| 12 | * Redistributions of source code must retain the above copyright |
|---|
| 13 | notice, this list of conditions and the following disclaimer. |
|---|
| 14 | |
|---|
| 15 | * Redistributions in binary form must reproduce the above |
|---|
| 16 | copyright notice, this list of conditions and the following disclaimer |
|---|
| 17 | in the documentation and/or other materials provided with the |
|---|
| 18 | distribution. |
|---|
| 19 | |
|---|
| 20 | * Neither the name of "Six Apart" nor the names of its |
|---|
| 21 | contributors may be used to endorse or promote products derived from |
|---|
| 22 | this software without specific prior written permission. |
|---|
| 23 | |
|---|
| 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 25 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 26 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 27 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 28 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 29 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 30 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 31 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 32 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 33 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 34 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 35 | */ |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | SpellChecker = new Class( Observable, { |
|---|
| 39 | SERVER_DELAY: 500, |
|---|
| 40 | MAX_PENDING_WORDS: 100, |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | init: function() { |
|---|
| 44 | if( defined( this.words ) ) |
|---|
| 45 | return; |
|---|
| 46 | arguments.callee.applySuper( this, arguments ); |
|---|
| 47 | this.words = {}; |
|---|
| 48 | this.pendingWords = []; |
|---|
| 49 | this.timer = null; |
|---|
| 50 | }, |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | destroy: function() { |
|---|
| 54 | if( this.timer ) |
|---|
| 55 | this.timer.stop(); |
|---|
| 56 | return arguments.callee.applySuper( this, arguments ); |
|---|
| 57 | }, |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | check: function( word ) { |
|---|
| 61 | this.init(); |
|---|
| 62 | if( !defined( word ) ) |
|---|
| 63 | return; |
|---|
| 64 | |
|---|
| 65 | if( this.words.hasOwnProperty( word ) ) |
|---|
| 66 | return (this.words[ word ] instanceof Array) |
|---|
| 67 | ? SpellChecker.INVALID |
|---|
| 68 | : this.words[ word ]; |
|---|
| 69 | |
|---|
| 70 | var ignored = this.checkIgnore( word ); |
|---|
| 71 | if( defined( ignored ) ) |
|---|
| 72 | return ignored; |
|---|
| 73 | |
|---|
| 74 | this.pendingWords.push( word ); |
|---|
| 75 | this.words[ word ] = SpellChecker.PENDING; |
|---|
| 76 | |
|---|
| 77 | if( this.pendingWords.length >= this.MAX_PENDING_WORDS ) |
|---|
| 78 | this.getWords(); |
|---|
| 79 | else if( !this.timer ) |
|---|
| 80 | this.timer = new Timer( this.getIndirectMethod( "getWords" ), this.SERVER_DELAY ); |
|---|
| 81 | else |
|---|
| 82 | this.timer.reset( this.SERVER_DELAY ); |
|---|
| 83 | |
|---|
| 84 | return SpellChecker.PENDING; |
|---|
| 85 | }, |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | checkIgnore: function( word ) { |
|---|
| 89 | this.init(); |
|---|
| 90 | if( !defined( word ) ) |
|---|
| 91 | return; |
|---|
| 92 | |
|---|
| 93 | if( word.match( /^[+-]?(\d+|((\d*\.\d+)|(\d+\.\d*))+)$/ ) ) // See Case 36394. |
|---|
| 94 | return this.ignore( word ); |
|---|
| 95 | }, |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | suggest: function( word ) { |
|---|
| 99 | this.init(); |
|---|
| 100 | if( !defined( word ) ) |
|---|
| 101 | return []; |
|---|
| 102 | |
|---|
| 103 | if( this.words.hasOwnProperty( word ) && this.words[ word ] instanceof Array ) |
|---|
| 104 | return this.words[ word ]; |
|---|
| 105 | return []; |
|---|
| 106 | }, |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | ignore: function( word ) { |
|---|
| 110 | this.init(); |
|---|
| 111 | if( !defined( word ) ) |
|---|
| 112 | return; |
|---|
| 113 | |
|---|
| 114 | this.pendingWords.remove( word ); |
|---|
| 115 | return this.words[ word ] = SpellChecker.IGNORED; |
|---|
| 116 | }, |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | /* xxx override this in your subclass */ |
|---|
| 120 | getWords: function() { |
|---|
| 121 | if( !this.pendingWords.length || this.request ) |
|---|
| 122 | return; |
|---|
| 123 | |
|---|
| 124 | if( !defined( this.i ) ) |
|---|
| 125 | this.i = 0; |
|---|
| 126 | |
|---|
| 127 | /* xxx marks every other word as invalid (for testing) */ |
|---|
| 128 | for( var i = 0; i < this.pendingWords.length; i++ ) |
|---|
| 129 | this.words[ this.pendingWords[ i ] ] = this.i++ % 2; |
|---|
| 130 | |
|---|
| 131 | this.pendingWords = []; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | }); |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | extend( SpellChecker, { |
|---|
| 138 | PENDING: -2, |
|---|
| 139 | IGNORED: -1, |
|---|
| 140 | VALID: 0, |
|---|
| 141 | INVALID: 1 |
|---|
| 142 | } ); |
|---|