| 1 | /* |
|---|
| 2 | $Id$ |
|---|
| 3 | |
|---|
| 4 | Copyright Six Apart, Ltd. All rights reserved. |
|---|
| 5 | Redistribution and use in source and binary forms is |
|---|
| 6 | subject to the Six Apart JavaScript license: |
|---|
| 7 | |
|---|
| 8 | http://code.sixapart.com/svn/js/trunk/LICENSE.txt |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | with( TypeCore ) { |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | SpellChecker = new Class( Observable, { |
|---|
| 16 | SERVER_DELAY: 500, |
|---|
| 17 | MAX_PENDING_WORDS: 100, |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | init: function() { |
|---|
| 21 | if( defined( this.words ) ) |
|---|
| 22 | return; |
|---|
| 23 | arguments.callee.applySuper( this, arguments ); |
|---|
| 24 | this.words = {}; |
|---|
| 25 | this.pendingWords = []; |
|---|
| 26 | this.timer = null; |
|---|
| 27 | }, |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | destroy: function() { |
|---|
| 31 | if( this.timer ) |
|---|
| 32 | this.timer.stop(); |
|---|
| 33 | return arguments.callee.applySuper( this, arguments ); |
|---|
| 34 | }, |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | check: function( word ) { |
|---|
| 38 | this.init(); |
|---|
| 39 | if( !defined( word ) ) |
|---|
| 40 | return; |
|---|
| 41 | |
|---|
| 42 | if( this.words.hasOwnProperty( word ) ) |
|---|
| 43 | return (this.words[ word ] instanceof Array) |
|---|
| 44 | ? SpellChecker.INVALID |
|---|
| 45 | : this.words[ word ]; |
|---|
| 46 | |
|---|
| 47 | var ignored = this.checkIgnore( word ); |
|---|
| 48 | if( defined( ignored ) ) |
|---|
| 49 | return ignored; |
|---|
| 50 | |
|---|
| 51 | this.pendingWords.push( word ); |
|---|
| 52 | this.words[ word ] = SpellChecker.PENDING; |
|---|
| 53 | |
|---|
| 54 | if( this.pendingWords.length >= this.MAX_PENDING_WORDS ) |
|---|
| 55 | this.getWords(); |
|---|
| 56 | else if( !this.timer ) |
|---|
| 57 | this.timer = new Timer( this.getIndirectMethod( "getWords" ), this.SERVER_DELAY ); |
|---|
| 58 | else |
|---|
| 59 | this.timer.reset( this.SERVER_DELAY ); |
|---|
| 60 | |
|---|
| 61 | return SpellChecker.PENDING; |
|---|
| 62 | }, |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | checkIgnore: function( word ) { |
|---|
| 66 | this.init(); |
|---|
| 67 | if( !defined( word ) ) |
|---|
| 68 | return; |
|---|
| 69 | |
|---|
| 70 | if( word.match( /^[+-]?(\d+|((\d*\.\d+)|(\d+\.\d*))+)$/ ) ) // See Case 36394. |
|---|
| 71 | return this.ignore( word ); |
|---|
| 72 | }, |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | suggest: function( word ) { |
|---|
| 76 | this.init(); |
|---|
| 77 | if( !defined( word ) ) |
|---|
| 78 | return []; |
|---|
| 79 | |
|---|
| 80 | if( this.words.hasOwnProperty( word ) && this.words[ word ] instanceof Array ) |
|---|
| 81 | return this.words[ word ]; |
|---|
| 82 | return []; |
|---|
| 83 | }, |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | ignore: function( word ) { |
|---|
| 87 | this.init(); |
|---|
| 88 | if( !defined( word ) ) |
|---|
| 89 | return; |
|---|
| 90 | |
|---|
| 91 | this.pendingWords.remove( word ); |
|---|
| 92 | return this.words[ word ] = SpellChecker.IGNORED; |
|---|
| 93 | }, |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | /* xxx override this in your subclass */ |
|---|
| 97 | getWords: function() { |
|---|
| 98 | if( !this.pendingWords.length || this.request ) |
|---|
| 99 | return; |
|---|
| 100 | |
|---|
| 101 | if( !defined( this.i ) ) |
|---|
| 102 | this.i = 0; |
|---|
| 103 | |
|---|
| 104 | /* xxx marks every other word as invalid (for testing) */ |
|---|
| 105 | for( var i = 0; i < this.pendingWords.length; i++ ) |
|---|
| 106 | this.words[ this.pendingWords[ i ] ] = this.i++ % 2; |
|---|
| 107 | |
|---|
| 108 | this.pendingWords = []; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | }); |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | extend( SpellChecker, { |
|---|
| 115 | PENDING: -2, |
|---|
| 116 | IGNORED: -1, |
|---|
| 117 | VALID: 0, |
|---|
| 118 | INVALID: 1 |
|---|
| 119 | } ); |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | } /* with( TypeCore ) */ |
|---|