Index: trunk/parsers/wikidom/tests/annotations/index.html |
— | — | @@ -10,6 +10,8 @@ |
11 | 11 | <h2 id="qunit-banner"></h2> |
12 | 12 | <h2 id="qunit-userAgent"></h2> |
13 | 13 | <ol id="qunit-tests"></ol> |
| 14 | + <script src="../../lib/es/es.js" type="text/javascript"></script> |
| 15 | + <script src="../../lib/es/es.EventEmitter.js" type="text/javascript"></script> |
14 | 16 | <script src="../../lib/es/es.Content.js" type="text/javascript"></script> |
15 | 17 | <script src="../../lib/jquery.js" type="text/javascript"></script> |
16 | 18 | <script src="../../lib/qunit.js" type="text/javascript"></script> |
Index: trunk/parsers/wikidom/tests/annotations/test.js |
— | — | @@ -55,9 +55,47 @@ |
56 | 56 | |
57 | 57 | /* Tests */ |
58 | 58 | |
59 | | -test( 'Content.substring', function() { |
60 | | - expect( 5 ); |
61 | | - |
| 59 | +test( 'Content modification', 17, function() { |
| 60 | + content.on( 'change', function( args ) { |
| 61 | + ok( true, 'Change events get triggered after ' + args.type + ' events' ); |
| 62 | + } ); |
| 63 | + content.on( 'insert', function( args ) { |
| 64 | + ok( true, 'Insert events get triggered' ); |
| 65 | + equal( args.offset, 5, 'Insert events have correct offsets' ); |
| 66 | + deepEqual( args.content, ['a', 'b', 'c'], 'Insert events have correct content' ); |
| 67 | + deepEqual( content.data.slice( 5, 8 ), ['a', 'b', 'c'], 'Content is inserted correctly' ); |
| 68 | + // +1 change event |
| 69 | + } ); |
| 70 | + content.on( 'annotate', function( args ) { |
| 71 | + ok( true, 'Annotate events get triggered' ); |
| 72 | + equal( args.method, 'add', 'Annotate events have correct method' ); |
| 73 | + deepEqual( args.annotation, { 'type': 'italic' }, 'Annotate events have correct annotation' ); |
| 74 | + equal( args.start, 5, 'Annotate events have correct start points' ); |
| 75 | + equal( args.end, 6, 'Annotate events have correct end points' ); |
| 76 | + deepEqual( |
| 77 | + content.data.slice( 4, 8 ), |
| 78 | + [' ', ['a', { 'type': 'italic' }], 'b', 'c'], |
| 79 | + 'Content is annotated correctly' |
| 80 | + ); |
| 81 | + // +1 change event |
| 82 | + } ); |
| 83 | + content.on( 'remove', function( args ) { |
| 84 | + ok( true, 'Remove events get triggered' ); |
| 85 | + equal( args.start, 5, 'Remove events have correct start points' ); |
| 86 | + equal( args.end, 8, 'Remove events have correct end points' ); |
| 87 | + deepEqual( |
| 88 | + content.data.slice( 4, 8 ), |
| 89 | + [' ', 'i', 's', ' '], |
| 90 | + 'Content is removed correctly' |
| 91 | + ); |
| 92 | + // +1 change event |
| 93 | + } ); |
| 94 | + content.insert( 5, ['a', 'b', 'c'] ); |
| 95 | + content.annotate( 'add', { 'type': 'italic' }, 5, 6 ); |
| 96 | + content.remove( 5, 8 ); |
| 97 | +} ); |
| 98 | + |
| 99 | +test( 'Content.substring', 5, function() { |
62 | 100 | equal( |
63 | 101 | content.substring( 3, 39 ), |
64 | 102 | 's is a test paragraph!\nParagraphs ca', |
— | — | @@ -85,11 +123,11 @@ |
86 | 124 | ); |
87 | 125 | } ); |
88 | 126 | |
89 | | -test( 'Content.getLength', function() { |
90 | | - equals( content.getLength(), 65, 'Returns correct length' ); |
91 | | -} ) |
| 127 | +test( 'Content.getLength', 1, function() { |
| 128 | + equal( content.getLength(), 65, 'Returns correct length' ); |
| 129 | +} ); |
92 | 130 | |
93 | | -test( 'Content.slice', function() { |
| 131 | +test( 'Content.slice', 2, function() { |
94 | 132 | deepEqual( |
95 | 133 | content.slice().data, |
96 | 134 | [ |
Index: trunk/parsers/wikidom/lib/es/es.Content.js |
— | — | @@ -10,6 +10,7 @@ |
11 | 11 | * @returns {Content} |
12 | 12 | */ |
13 | 13 | function Content( content ) { |
| 14 | + EventEmitter.call( this ); |
14 | 15 | this.data = content || []; |
15 | 16 | }; |
16 | 17 | |
— | — | @@ -296,23 +297,28 @@ |
297 | 298 | * |
298 | 299 | * Inserted content will inherit annotations from neighboring content. |
299 | 300 | * |
300 | | - * @param start {Integer} Position to insert content at |
301 | | - * @param insert {Array} Content data to insert |
| 301 | + * @param offset {Integer} Position to insert content at |
| 302 | + * @param content {Array} Content data to insert |
302 | 303 | */ |
303 | | -Content.prototype.insert = function( start, insert ) { |
| 304 | +Content.prototype.insert = function( offset, content ) { |
304 | 305 | // TODO: Prefer to not take annotations from a neighbor that's a space character |
305 | | - var neighbor = this.data[Math.max( start - 1, 0 )]; |
| 306 | + var neighbor = this.data[Math.max( offset - 1, 0 )]; |
306 | 307 | if ( $.isArray( neighbor ) ) { |
307 | 308 | var annotations = neighbor.slice( 1 ); |
308 | 309 | var i; |
309 | | - for ( i = 0; i < insert.length; i++ ) { |
310 | | - if ( typeof insert[i] === 'string' ) { |
311 | | - insert[i] = [insert[i]]; |
| 310 | + for ( i = 0; i < content.length; i++ ) { |
| 311 | + if ( typeof content[i] === 'string' ) { |
| 312 | + content[i] = [content[i]]; |
312 | 313 | } |
313 | | - insert[i] = insert[i].concat( annotations ); |
| 314 | + content[i] = content[i].concat( annotations ); |
314 | 315 | } |
315 | 316 | } |
316 | | - Array.prototype.splice.apply( this.data, [start, 0].concat( insert ) ); |
| 317 | + Array.prototype.splice.apply( this.data, [offset, 0].concat( content ) ); |
| 318 | + this.emit( 'insert', { |
| 319 | + 'offset': offset, |
| 320 | + 'content': content |
| 321 | + } ); |
| 322 | + this.emit( 'change', { 'type': 'insert' } ); |
317 | 323 | }; |
318 | 324 | |
319 | 325 | /** |
— | — | @@ -323,6 +329,11 @@ |
324 | 330 | */ |
325 | 331 | Content.prototype.remove = function( start, end ) { |
326 | 332 | this.data.splice( start, end - start ); |
| 333 | + this.emit( 'remove', { |
| 334 | + 'start': start, |
| 335 | + 'end': end |
| 336 | + } ); |
| 337 | + this.emit( 'change', { 'type': 'remove' } ); |
327 | 338 | }; |
328 | 339 | |
329 | 340 | /** |
— | — | @@ -461,6 +472,13 @@ |
462 | 473 | } |
463 | 474 | } |
464 | 475 | } |
| 476 | + this.emit( 'annotate', { |
| 477 | + 'method': method, |
| 478 | + 'annotation': annotation, |
| 479 | + 'start': start, |
| 480 | + 'end': end |
| 481 | + } ); |
| 482 | + this.emit( 'change', { 'type': 'annotate' } ); |
465 | 483 | }; |
466 | 484 | |
467 | 485 | /** |
— | — | @@ -513,3 +531,5 @@ |
514 | 532 | } |
515 | 533 | return out; |
516 | 534 | }; |
| 535 | + |
| 536 | +extend( Content, EventEmitter ); |
Index: trunk/parsers/wikidom/lib/es/es.EventEmitter.js |
— | — | @@ -0,0 +1,77 @@ |
| 2 | +function EventEmitter() { |
| 3 | + this.events = {}; |
| 4 | +} |
| 5 | + |
| 6 | +EventEmitter.prototype.emit = function( type ) { |
| 7 | + if ( type === 'error' && !( 'error' in this.events ) ) { |
| 8 | + throw 'Missing error handler error.'; |
| 9 | + } |
| 10 | + if ( !( type in this.events ) ) { |
| 11 | + return false; |
| 12 | + } |
| 13 | + var listeners = this.events[type].slice(); |
| 14 | + var args = Array.prototype.slice.call( arguments, 1 ); |
| 15 | + for ( var i = 0; i < listeners.length; i++ ) { |
| 16 | + listeners[i].apply( this, args ); |
| 17 | + } |
| 18 | + return true; |
| 19 | +}; |
| 20 | + |
| 21 | +EventEmitter.prototype.addListener = function( type, listener ) { |
| 22 | + if ( typeof listener !== 'function' ) { |
| 23 | + throw 'Invalid listener error. Function expected.'; |
| 24 | + } |
| 25 | + this.emit( 'newListener', type, listener ); |
| 26 | + if ( type in this.events ) { |
| 27 | + this.events[type].push( listener ); |
| 28 | + } else { |
| 29 | + this.events[type] = [listener]; |
| 30 | + } |
| 31 | + return this; |
| 32 | +}; |
| 33 | + |
| 34 | +EventEmitter.prototype.on = function( type, listener ) { |
| 35 | + this.addListener( type, listener ); |
| 36 | +}; |
| 37 | + |
| 38 | +EventEmitter.prototype.once = function( type, listener ) { |
| 39 | + var that = this; |
| 40 | + this.addListener( type, function g() { |
| 41 | + that.removeListener( type, g ); |
| 42 | + listener.apply( that, arguments ); |
| 43 | + } ); |
| 44 | +}; |
| 45 | + |
| 46 | +EventEmitter.prototype.removeListener = function( type, listener ) { |
| 47 | + if ( typeof listener !== 'function' ) { |
| 48 | + throw 'Invalid listener error. Function expected.'; |
| 49 | + } |
| 50 | + if ( !( type in this.events ) || !this.events[type].length ) { |
| 51 | + return this; |
| 52 | + } |
| 53 | + var handlers = this.events[type]; |
| 54 | + if ( handlers.length == 1 && handlers[0] === listener ) { |
| 55 | + delete this.events[type]; |
| 56 | + } else { |
| 57 | + var i = handlers.indexOf( listener ); |
| 58 | + if ( i < 0 ) { |
| 59 | + return this; |
| 60 | + } |
| 61 | + handlers.splice( i, 1 ); |
| 62 | + if ( handlers.length == 0 ) { |
| 63 | + delete this.events[type]; |
| 64 | + } |
| 65 | + } |
| 66 | + return this; |
| 67 | +}; |
| 68 | + |
| 69 | +EventEmitter.prototype.removeAllListeners = function( type ) { |
| 70 | + if ( type in this.events ) { |
| 71 | + delete this.events[type]; |
| 72 | + } |
| 73 | + return this; |
| 74 | +}; |
| 75 | + |
| 76 | +EventEmitter.prototype.listeners = function( type ) { |
| 77 | + return type in this.events ? this.events[type] : []; |
| 78 | +}; |
Property changes on: trunk/parsers/wikidom/lib/es/es.EventEmitter.js |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 79 | + native |
Added: svn:mime-type |
2 | 80 | + text/plain |
Index: trunk/parsers/wikidom/demos/es/index.html |
— | — | @@ -52,6 +52,7 @@ |
53 | 53 | <!-- EditSurface --> |
54 | 54 | <script type="text/javascript" src="../../lib/jquery.js"></script> |
55 | 55 | <script type="text/javascript" src="../../lib/es/es.js"></script> |
| 56 | + <script type="text/javascript" src="../../lib/es/es.EventEmitter.js"></script> |
56 | 57 | <script type="text/javascript" src="../../lib/es/es.Content.js"></script> |
57 | 58 | <script type="text/javascript" src="../../lib/es/es.Block.js"></script> |
58 | 59 | <script type="text/javascript" src="../../lib/es/es.Document.js"></script> |