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 sc_require('views/table_row'); 9 10 SC.TableCellView = SC.View.extend({ 11 12 classNames: ['sc-table-cell'], 13 14 column: null, 15 escapeHTMLBinding: SC.Binding.oneWay('.column.escapeHTML'), 16 formatter: SC.Binding.oneWay('.column.formatter'), 17 18 displayValue: function() { 19 var value = this.get('content') ; 20 21 // 1. apply the formatter 22 var formatter = this.get('column').get('formatter'); 23 if (formatter) { 24 var formattedValue = (SC.typeOf(formatter) === SC.T_FUNCTION) ? formatter(value, this) : formatter.fieldValueForObject(value, this) ; 25 if (!SC.none(formattedValue)) value = formattedValue ; 26 } 27 28 if (SC.typeOf(value) === SC.T_ARRAY) { 29 var ary = []; 30 for(var idx=0;idx<value.get('length');idx++) { 31 var x = value.objectAt(idx) ; 32 if (!SC.none(x) && x.toString) x = x.toString() ; 33 ary.push(x) ; 34 } 35 value = ary.join(',') ; 36 } 37 38 if (!SC.none(value) && value.toString) value = value.toString() ; 39 40 if (this.get('escapeHTML')) value = SC.RenderContext.escapeHTML(value); 41 42 return value ; 43 }.property('content', 'escapeHTML', 'formatter').cacheable(), 44 45 render: function(context, firstTime) { 46 context.push(this.get('displayValue')); 47 }, 48 49 init: function() { 50 sc_super(); 51 52 var column = this.get('column'); 53 54 column.addObserver('width', this, '_sctcv_layoutDidChange'); 55 column.addObserver('maxWidth', this, '_sctcv_layoutDidChange'); 56 column.addObserver('minWidth', this, '_sctcv_layoutDidChange'); 57 }, 58 59 _sctcv_layoutDidChange: function(sender, key, value, rev) { 60 var pv = this.get('parentView'); 61 SC.run( function() { pv.layoutChildViews(); }); 62 } 63 }); 64