Index: trunk/parsers/wikidom/lib/synth/bases/es.ControllerList.js |
— | — | @@ -0,0 +1,98 @@ |
| 2 | +/** |
| 3 | + * Creates an es.ControllerList object. |
| 4 | + * |
| 5 | + * Controller lists follow the structural changes of a list of views, keeping a list of controllers |
| 6 | + * in sync at all times. |
| 7 | + * |
| 8 | + * @class |
| 9 | + * @constructor |
| 10 | + * @extends {es.EventEmitter} |
| 11 | + * @param listView {es.ViewList} List of views to mimic |
| 12 | + * @property items {Array} List of controllers, correlating to the connected list of views |
| 13 | + */ |
| 14 | +es.ControllerList = function( listView ) { |
| 15 | + es.EventEmitter.call( this ); |
| 16 | + this.listView = listView; |
| 17 | + if ( !this.listView ) { |
| 18 | + return; |
| 19 | + } |
| 20 | + this.items = new es.AggregateArray(); |
| 21 | + var list = this; |
| 22 | + this.relayUpdate = function() { |
| 23 | + list.emit( 'update' ); |
| 24 | + }; |
| 25 | + function recycleItemController( itemView, autoCreate ) { |
| 26 | + var itemController = list.lookupItemController( itemView ); |
| 27 | + if ( itemController ) { |
| 28 | + list.items.splice( list.items.indexOf( itemController ), 1 ); |
| 29 | + } |
| 30 | + if ( autoCreate && itemController === null ) { |
| 31 | + itemController = itemView.createController(); |
| 32 | + } |
| 33 | + return itemController; |
| 34 | + } |
| 35 | + this.listView.on( 'prepend', function( itemView ) { |
| 36 | + var itemController = recycleItemController( itemView, true ); |
| 37 | + itemController.on( 'update', list.relayUpdate ); |
| 38 | + list.items.unshift( itemController ); |
| 39 | + list.emit( 'prepend', itemController ); |
| 40 | + list.emit( 'update' ); |
| 41 | + } ); |
| 42 | + this.listView.on( 'append', function( itemView ) { |
| 43 | + var itemController = recycleItemController( itemView, true ); |
| 44 | + itemController.on( 'update', list.relayUpdate ); |
| 45 | + list.items.push( itemController ); |
| 46 | + list.emit( 'append', itemController ); |
| 47 | + list.emit( 'update' ); |
| 48 | + } ); |
| 49 | + this.listView.on( 'insertBefore', function( itemView, beforeView ) { |
| 50 | + var beforeController = list.lookupItemController( beforeView ), |
| 51 | + itemController = recycleItemController( itemView, true ); |
| 52 | + itemController.on( 'update', list.relayUpdate ); |
| 53 | + if ( beforeController ) { |
| 54 | + list.items.splice( list.items.indexOf( beforeController ), 0, itemController ); |
| 55 | + } else { |
| 56 | + list.items.unshift( itemController ); |
| 57 | + } |
| 58 | + list.emit( 'insertBefore', itemController, beforeController ); |
| 59 | + list.emit( 'update' ); |
| 60 | + } ); |
| 61 | + this.listView.on( 'insertAfter', function( itemView, afterView ) { |
| 62 | + var afterController = list.lookupItemController( afterView ), |
| 63 | + itemController = recycleItemController( itemView, true ); |
| 64 | + itemController.on( 'update', list.relayUpdate ); |
| 65 | + if ( afterController ) { |
| 66 | + list.items.splice( list.items.indexOf( afterController ) + 1, 0, itemController ); |
| 67 | + } else { |
| 68 | + list.items.push( itemController ); |
| 69 | + } |
| 70 | + list.emit( 'insertAfter', itemController, afterController ); |
| 71 | + list.emit( 'update' ); |
| 72 | + } ); |
| 73 | + this.listView.on( 'remove', function( itemView ) { |
| 74 | + var itemController = recycleItemController( itemView ); |
| 75 | + itemController.removeListener( 'update', list.relayUpdate ); |
| 76 | + list.emit( 'remove', itemController ); |
| 77 | + list.emit( 'update' ); |
| 78 | + } ); |
| 79 | + // Auto-add items for existing items |
| 80 | + var itemViews = this.listView.all(); |
| 81 | + for ( var i = 0; i < itemViews.length; i++ ) { |
| 82 | + var itemController = itemViews[i].createController(); |
| 83 | + itemController.on( 'update', list.relayUpdate ); |
| 84 | + this.items.push( itemController ); |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +es.ControllerList.prototype.lookupItemController = function( itemView ) { |
| 89 | + for ( var i = 0; i < this.items.length; i++ ) { |
| 90 | + if ( this.items[i].getView() === itemView ) { |
| 91 | + return this.items[i]; |
| 92 | + } |
| 93 | + } |
| 94 | + return null; |
| 95 | +}; |
| 96 | + |
| 97 | +/* Inheritance */ |
| 98 | + |
| 99 | +es.extend( es.ControllerList, es.EventEmitter ); |
Index: trunk/parsers/wikidom/lib/synth/bases/es.ControllerItem.js |
— | — | @@ -0,0 +1,50 @@ |
| 2 | +/** |
| 3 | + * Creates an es.ControllerItem object. |
| 4 | + * |
| 5 | + * Controller item objects are controllers for individual models in a model list. |
| 6 | + * |
| 7 | + * @class |
| 8 | + * @constructor |
| 9 | + * @extends {es.EventEmitter} |
| 10 | + * @property itemView {es.ModelItem} Model this controller is connected to |
| 11 | + */ |
| 12 | +es.ViewContainerItem = function( itemView ) { |
| 13 | + es.EventEmitter.call( this ); |
| 14 | + this.itemView = itemView; |
| 15 | +}; |
| 16 | + |
| 17 | +/** |
| 18 | + * Gets the model this controller is connected to. |
| 19 | + * |
| 20 | + * @method |
| 21 | + * @returns {es.ModelItem} Model this controller is connected to |
| 22 | + */ |
| 23 | +es.ControllerItem.prototype.getModel = function() { |
| 24 | + return this.itemView.getModel(); |
| 25 | +}; |
| 26 | + |
| 27 | +/** |
| 28 | + * Gets the view this controller is connected to. |
| 29 | + * |
| 30 | + * @method |
| 31 | + * @returns {es.ViewItem} View this controller is connected to |
| 32 | + */ |
| 33 | +es.ControllerItem.prototype.getView = function() { |
| 34 | + return this.itemView; |
| 35 | +}; |
| 36 | + |
| 37 | +/** |
| 38 | + * Gets the index of this item within it's container. |
| 39 | + * |
| 40 | + * This method simply delegates to the model. |
| 41 | + * |
| 42 | + * @method |
| 43 | + * @returns {Integer} Index of item in it's container |
| 44 | + */ |
| 45 | +es.ControllerItem.prototype.getIndex = function() { |
| 46 | + return this.itemView.getModel().getIndex(); |
| 47 | +}; |
| 48 | + |
| 49 | +/* Inheritance */ |
| 50 | + |
| 51 | +es.extend( es.ControllerItem, es.EventEmitter ); |