Index: trunk/parsers/wikidom/lib/es/es.Document.js |
— | — | @@ -5,120 +5,11 @@ |
6 | 6 | * @returns {es.Document} |
7 | 7 | */ |
8 | 8 | es.Document = function( blocks ) { |
9 | | - es.EventEmitter.call( this ); |
10 | | - this.blocks = []; |
11 | | - var i; |
12 | | - for( i = 0; i < blocks.length; i++ ) { |
13 | | - this.appendBlock(blocks[i]); |
14 | | - } |
| 9 | + es.Container.call( this, blocks, 'document', 'blocks' ); |
15 | 10 | this.width = null; |
16 | | - this.$ = $( '<div class="editSurface-document"></div>' ) |
17 | | - .data( 'document', this ); |
18 | 11 | } |
19 | 12 | |
20 | | -/** |
21 | | - * Gets the first block in the document. |
22 | | - * |
23 | | - * @returns {es.Block} |
24 | | - */ |
25 | | -es.Document.prototype.firstBlock = function() { |
26 | | - return this.blocks.length ? this.blocks[0] : null; |
27 | | -}; |
28 | | - |
29 | | -/** |
30 | | - * Gets the last block in the document. |
31 | | - * |
32 | | - * @returns {es.Block} |
33 | | - */ |
34 | | -es.Document.prototype.lastBlock = function() { |
35 | | - return this.blocks.length ? this.blocks[this.blocks.length - 1] : null; |
36 | | -}; |
37 | | - |
38 | | -/** |
39 | | - * Adds a block to the end of the document. |
40 | | - * |
41 | | - * @param {es.Block} Block to append |
42 | | - */ |
43 | | -es.Document.prototype.appendBlock = function( block ) { |
44 | | - block.document = this; |
45 | | - block.on( 'update', function() { |
46 | | - block.document.emit( 'update' ); |
47 | | - } ); |
48 | | - this.blocks.push( block ); |
49 | | -}; |
50 | | - |
51 | | -/** |
52 | | - * Adds a block to the beginning of the document. |
53 | | - * |
54 | | - * @param {es.Block} Block to prepend |
55 | | - */ |
56 | | -es.Document.prototype.prependBlock = function( block ) { |
57 | | - block.document = this; |
58 | | - block.on( 'update', function() { |
59 | | - block.document.emit( 'update' ); |
60 | | - } ); |
61 | | - this.blocks.unshift( block ); |
62 | | -}; |
63 | | - |
64 | | -/** |
65 | | - * Adds a block to the document after an existing block. |
66 | | - * |
67 | | - * @param block {es.Block} Block to insert |
68 | | - * @param before {es.Block} Block to insert before, if null then block will be inserted at the end |
69 | | - */ |
70 | | -es.Document.prototype.insertBlockBefore = function( block, before ) { |
71 | | - block.document = this; |
72 | | - block.on( 'update', function() { |
73 | | - block.document.emit( 'update' ); |
74 | | - } ); |
75 | | - if ( before ) { |
76 | | - this.blocks.splice( before.getIndex(), 0, block ); |
77 | | - } else { |
78 | | - this.blocks.push( block ); |
79 | | - } |
80 | | -}; |
81 | | -/** |
82 | | - * Adds a block to the document after an existing block. |
83 | | - * @param block {es.Block} Block to insert |
84 | | - * @param after {es.Block} Block to insert after, if null then block will be inserted at the end |
85 | | - */ |
86 | | -es.Document.prototype.insertBlockAfter = function( block, after ) { |
87 | | - block.document = this; |
88 | | - block.on( 'update', function() { |
89 | | - block.document.emit( 'update' ); |
90 | | - } ); |
91 | | - if ( after ) { |
92 | | - this.blocks.splice( after.getIndex() + 1, 0, block ); |
93 | | - } else { |
94 | | - this.blocks.push( block ); |
95 | | - } |
96 | | -}; |
97 | | - |
98 | | -/** |
99 | | - * Removes a block from the document. |
100 | | - * |
101 | | - * @param {es.Block} Block to remove |
102 | | - */ |
103 | | -es.Document.prototype.removeBlock = function( block ) { |
104 | | - block.removeAllListeners( 'update' ); |
105 | | - this.blocks.splice( block.getIndex(), 1 ); |
106 | | - block.document = null; |
107 | | - block.$.detach(); |
108 | | - this.emit( 'update' ); |
109 | | -}; |
110 | | - |
111 | | -es.Document.prototype.renderBlocks = function( offset ) { |
112 | | - // Remember width, to avoid updates when without width changes |
113 | | - this.width = this.$.innerWidth(); |
114 | | - // Render blocks |
115 | | - var i; |
116 | | - for ( i = 0; i < this.blocks.length; i++ ) { |
117 | | - this.$.append( this.blocks[i].$ ); |
118 | | - this.blocks[i].renderContent( offset ); |
119 | | - } |
120 | | -}; |
121 | | - |
122 | | -es.Document.prototype.updateBlocks = function( offset ) { |
| 13 | +es.Document.prototype.renderBlocks = function() { |
123 | 14 | // Bypass rendering when width has not changed |
124 | 15 | var width = this.$.innerWidth(); |
125 | 16 | if ( this.width === width ) { |
— | — | @@ -126,10 +17,9 @@ |
127 | 18 | } |
128 | 19 | this.width = width; |
129 | 20 | // Render blocks |
130 | | - var doc; |
131 | | - this.$.children( '.editSurface-block' ).each( function( i ) { |
132 | | - $(this).data( 'block' ).renderContent( offset ); |
133 | | - } ); |
| 21 | + for ( var i = 0; i < this.blocks.length; i++ ) { |
| 22 | + this.blocks[i].renderContent(); |
| 23 | + } |
134 | 24 | }; |
135 | 25 | |
136 | | -es.extend( es.Document, es.EventEmitter ); |
| 26 | +es.extend( es.Document, es.Container ); |
Index: trunk/parsers/wikidom/lib/es/es.Container.js |
— | — | @@ -0,0 +1,135 @@ |
| 2 | +/** |
| 3 | + * |
| 4 | + * @extends {es.EventEmitter} |
| 5 | + * @param items {Array} List of items |
| 6 | + * @returns {es.Container} |
| 7 | + */ |
| 8 | +es.Container = function( items, typeName, listName ) { |
| 9 | + es.EventEmitter.call( this ); |
| 10 | + if ( typeof typeName !== 'string' ) { |
| 11 | + typeName = 'container'; |
| 12 | + } |
| 13 | + if ( typeof listName !== 'string' ) { |
| 14 | + listName = 'items'; |
| 15 | + } |
| 16 | + this._typeName = typeName; |
| 17 | + this._listName = listName; |
| 18 | + this._list = this[listName] = []; |
| 19 | + this.$ = $( '<div></div>' ) |
| 20 | + .addClass( 'editSurface-' + typeName ) |
| 21 | + .data( typeName, this ); |
| 22 | + // Auto-append |
| 23 | + if ( $.isArray( items ) ) { |
| 24 | + for ( var i = 0; i < items.length; i++ ) { |
| 25 | + this.append( items[i] ); |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Gets the first item in the container. |
| 32 | + * |
| 33 | + * @returns {Object} |
| 34 | + */ |
| 35 | +es.Container.prototype.first = function() { |
| 36 | + return this._list.length ? this._list[0] : null; |
| 37 | +}; |
| 38 | + |
| 39 | +/** |
| 40 | + * Gets the last item in the container. |
| 41 | + * |
| 42 | + * @returns {Object} |
| 43 | + */ |
| 44 | +es.Container.prototype.last = function() { |
| 45 | + return this._list.length |
| 46 | + ? this._list[this._list.length - 1] : null; |
| 47 | +}; |
| 48 | + |
| 49 | +/** |
| 50 | + * Adds an item to the end of the container. |
| 51 | + * |
| 52 | + * @param {Object} Item to append |
| 53 | + */ |
| 54 | +es.Container.prototype.append = function( item ) { |
| 55 | + item[this._typeName] = this; |
| 56 | + var container = this; |
| 57 | + item.on( 'update', function() { |
| 58 | + container.emit( 'update' ); |
| 59 | + } ); |
| 60 | + this._list.push( item ); |
| 61 | + this.$.append( item.$ ); |
| 62 | + this.emit( 'update' ); |
| 63 | +}; |
| 64 | + |
| 65 | +/** |
| 66 | + * Adds an item to the beginning of the container. |
| 67 | + * |
| 68 | + * @param {Object} Item to prepend |
| 69 | + */ |
| 70 | +es.Container.prototype.prepend = function( item ) { |
| 71 | + item[this._typeName] = this; |
| 72 | + var container = this; |
| 73 | + item.on( 'update', function() { |
| 74 | + container.emit( 'update' ); |
| 75 | + } ); |
| 76 | + this._list.unshift( item ); |
| 77 | + this.$.prepend( item.$ ); |
| 78 | + this.emit( 'update' ); |
| 79 | +}; |
| 80 | + |
| 81 | +/** |
| 82 | + * Adds an item to the container after an existing item. |
| 83 | + * |
| 84 | + * @param item {Object} Item to insert |
| 85 | + * @param before {Object} Item to insert before, if null then item will be inserted at the end |
| 86 | + */ |
| 87 | +es.Container.prototype.insertBefore = function( item, before ) { |
| 88 | + item[this._typeName] = this; |
| 89 | + var container = this; |
| 90 | + item.on( 'update', function() { |
| 91 | + container.emit( 'update' ); |
| 92 | + } ); |
| 93 | + if ( before ) { |
| 94 | + this._list.splice( before.getIndex(), 0, item ); |
| 95 | + item.$.insertBefore( before.$ ); |
| 96 | + } else { |
| 97 | + this._list.push( item ); |
| 98 | + this.$.append( item.$ ); |
| 99 | + } |
| 100 | + this.emit( 'update' ); |
| 101 | +}; |
| 102 | +/** |
| 103 | + * Adds an item to the container after an existing item. |
| 104 | + * @param item {Object} Item to insert |
| 105 | + * @param after {Object} Item to insert after, if null then item will be inserted at the end |
| 106 | + */ |
| 107 | +es.Container.prototype.insertAfter = function( item, after ) { |
| 108 | + item[this._typeName] = this; |
| 109 | + var container = this; |
| 110 | + item.on( 'update', function() { |
| 111 | + container.emit( 'update' ); |
| 112 | + } ); |
| 113 | + if ( after ) { |
| 114 | + this._list.splice( after.getIndex() + 1, 0, item ); |
| 115 | + item.$.insertAfter( after.$ ); |
| 116 | + } else { |
| 117 | + this._list.push( item ); |
| 118 | + this.$.append( item.$ ); |
| 119 | + } |
| 120 | + this.emit( 'update' ); |
| 121 | +}; |
| 122 | + |
| 123 | +/** |
| 124 | + * Removes an item from the container. |
| 125 | + * |
| 126 | + * @param {Object} Item to remove |
| 127 | + */ |
| 128 | +es.Container.prototype.remove = function( item ) { |
| 129 | + item.removeAllListeners( 'update' ); |
| 130 | + this._list.splice( item.getIndex(), 1 ); |
| 131 | + item[this._typeName] = null; |
| 132 | + item.$.detach(); |
| 133 | + this.emit( 'update' ); |
| 134 | +}; |
| 135 | + |
| 136 | +es.extend( es.Container, es.EventEmitter ); |
Property changes on: trunk/parsers/wikidom/lib/es/es.Container.js |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 137 | + native |
Added: svn:mime-type |
2 | 138 | + text/plain |
Index: trunk/parsers/wikidom/lib/es/es.Surface.js |
— | — | @@ -71,7 +71,7 @@ |
72 | 72 | |
73 | 73 | $(window).resize( function() { |
74 | 74 | surface.cursor.hide(); |
75 | | - surface.doc.updateBlocks(); |
| 75 | + surface.doc.renderBlocks(); |
76 | 76 | } ); |
77 | 77 | |
78 | 78 | this.doc.on( 'update', function() { |
Index: trunk/parsers/wikidom/demos/es/index.html |
— | — | @@ -54,6 +54,7 @@ |
55 | 55 | <script type="text/javascript" src="../../lib/es/es.js"></script> |
56 | 56 | <script type="text/javascript" src="../../lib/es/es.EventEmitter.js"></script> |
57 | 57 | <script type="text/javascript" src="../../lib/es/es.Content.js"></script> |
| 58 | + <script type="text/javascript" src="../../lib/es/es.Container.js"></script> |
58 | 59 | <script type="text/javascript" src="../../lib/es/es.Block.js"></script> |
59 | 60 | <script type="text/javascript" src="../../lib/es/es.Document.js"></script> |
60 | 61 | <script type="text/javascript" src="../../lib/es/es.Surface.js"></script> |