1 // ==========================================================================
  2 // Project:   SproutCore - JavaScript Application Framework
  3 // Copyright: ©2006-2011 Strobe Inc. and contributors.
  4 //            Portions ©2008-2011 Apple, Inc. All rights reserved.
  5 // License:   Licensed under MIT license (see license.js)
  6 // ==========================================================================
  7 
  8 
  9 
 10 SC.TableRowView = SC.View.extend({
 11 /** @scope SC.TableRowView.prototype */
 12 
 13   //layout: { height: 18, left: 0, right: 0, top: 0 },
 14 
 15   // ..........................................................
 16   // PROPERTIES
 17   //
 18 
 19   classNames: ['sc-table-row'],
 20 
 21   cells: [],
 22 
 23   acceptsFirstResponder: YES,
 24 
 25   /**
 26     A reference to the row's encompassing TableView.
 27 
 28     @property
 29     @type SC.TableView
 30   */
 31   tableView: null,
 32 
 33   // ..........................................................
 34   // METHODS
 35   //
 36 
 37   init: function() {
 38     sc_super();
 39     this._sctrv_handleChildren();
 40   },
 41 
 42   /**
 43     A collection of `SC.TableColumn` objects.
 44 
 45     @property
 46     @type Array
 47   */
 48   columns: function() {
 49     return this.get('tableView').get('columns');
 50   }.property(),
 51 
 52   render: function(context, firstTime) {
 53     sc_super();
 54     context.setClass('sel', this.get('isSelected'));
 55   },
 56 
 57   render: function(context, firstTime) {
 58     var classArray = [];
 59 
 60     classArray.push((this.get('contentIndex')%2 === 0) ? 'even' : 'odd');
 61     context.addClass(classArray);
 62 
 63     sc_super();
 64   },
 65 
 66   renderChildViews: function(context, firstTime) {
 67     var cells = this.get('cells'), cell, idx;
 68     for (idx = 0; idx < cells.get('length'); idx++) {
 69       cell = cells.objectAt(idx);
 70       context = context.begin(cell.get('tagName'));
 71       cell.render(context, firstTime);
 72       context = context.end();
 73     }
 74     return context;
 75   },
 76 
 77   layoutChildViews: function() {
 78     var cells = this.get('cells'), columns = this.get('columns'),
 79         cell, column, idx;
 80     var left = 0, width, rowHeight = this.get('tableView').get('rowHeight');
 81 
 82     for (idx = 0; idx < cells.get('length'); idx++) {
 83       cell = cells.objectAt(idx);
 84       column = columns.objectAt(idx);
 85       width = column.get('width');
 86 
 87       cell.adjust({
 88         left: left,
 89         width: width,
 90         height: rowHeight
 91       });
 92 
 93       left += width;
 94       cell.updateLayout();
 95     }
 96   },
 97 
 98   // ..........................................................
 99   // INTERNAL SUPPORT
100   //
101 
102   _sctrv_layoutForChildAtColumnIndex: function(index) {
103     var columns = this.get('columns'),
104         rowHeight = this.get('tableView').get('rowHeight'),
105         layout = {},
106         left = 0, idx;
107 
108     for (idx = 0; idx < index; idx++) {
109       left += columns.objectAt(idx).get('width');
110     }
111 
112     return {
113       left:   left,
114       width:  columns.objectAt(index).get('width'),
115       height: rowHeight
116     };
117   },
118 
119   _sctrv_createTableCell: function(column, value) {
120     var cell = SC.TableCellView.create({
121       column:  column,
122       content: value
123     });
124     return cell;
125   },
126 
127   // The row needs to redraw when the selection state changes.
128   _sctrv_handleSelection: function() {
129     this.displayDidChange();
130   }.observes('isSelected'),
131 
132   _sctrv_handleChildren: function() {
133     var content = this.get('content'), columns = this.get('columns');
134 
135     this.removeAllChildren();
136     var column, key, value, cells = [], cell, idx;
137     for (idx = 0; idx < columns.get('length'); idx++) {
138       column = columns.objectAt(idx);
139       key    = column.get('key');
140       value  = content ? content.getPath(key) : "";
141       cell   = this._sctrv_createTableCell(column, value);
142       cells.push(cell);
143       this.appendChild(cell);
144     }
145 
146     this.set('cells', cells);
147   }
148 });
149 
150