
var NodeBuilder = require('../../vn/node-builder');

/**
 * Represents a grid column. This is an abstract class and should not be
 * instantiated directly.
 */
module.exports = new Class({
	Extends: NodeBuilder
	,Tag: 'htk-column'
	,Properties: {
		value: {
			type: String
			,value: null
		},
		column: {
			type: String
			,value: null
		},
		columnIndex: {
			type: Number
			,value: -1
		},
		title: {
			 type: String
			 ,value: null
		},
		editable: {
			type: Boolean
			,value: false
		},
		renderer: {
			type: Function
			,value: false
		},
		class: {
			type: String
			,set(x) {
				this._userCssClass = x;
				
				if (this._cssClass)
					this._cssClass = x +' '+ this._cssClass;
				else
					this._cssClass = x;
			}
			,get() {
				return this._userCssClass;
			}
		}
	}
	
	,_cssClass: null
	,_userCssClass: null

	/**
	 * Initializes the column.
	 */
	,initialize(props) {
		NodeBuilder.prototype.initialize.call(this, props);
		this.td = this.createElement('td');
	}
	
	,renderHeader() {
		var th = this.createElement('th');
		
		if (this.title)
			th.appendChild(this.createTextNode(this.title));
			
		return th;
	}

	/**
	 * Draws the cell and returns its associated td.
	 *
	 * @return {HTMLTableData} the rendered cell
	 */
	,render() {
		var td = this.td.cloneNode(true);

		if (this._cssClass)
			td.className = this._cssClass;
			
		return td;
	}

	,updateColumnName(model) {
		if (this.columnIndex !== -1)
			this.column = model.getColumnName(this.columnIndex);
	}

	,changed(tr, newValue) {
		this.emit('changed', tr.rowIndex - 1, newValue);
	}
});

