jsm

net/sf/jsm/widgets/+jsmSelectField.js

Summary

No overview generated for 'net/sf/jsm/widgets/+jsmSelectField.js'


Class Summary
JsmSelectField Represents a select box

/**
 * Creates a new JsmSelectField
 * @extends JsmWidget
 * @class Represents a select box
  * @param {Array} options Array of options (value-label pairs), eg. [{'value':0, 'label':"All"},{'value':1, 'label':"UK"},{'value':2, 'label':"Ireland"}]
  * @param {Array} selectedValueOrLabel Array of selected values or labels
  * @param {boolean} multi Indicates if this is a multiselectbox
  * @param {int} size Amount of visible options
 * @constructor
 */     
  function JsmSelectField(options, selectedValueOrLabel, multi, size) {
    /** @ignore */
  	this.id=new Date().getTime() + "" + Math.random(); 
  	logger.info("Created JsmSelectField with id: " + this.id);   	  	
  	/** The selected option's values or labels */
  	this.selected = selectedValueOrLabel;
  	/** The options of this select box
  	* @private 
  	* @type Array
  	*/
  	this._options=jsm.noud(options)?new Array():options;
  	/** Flag if this is a multi select box */
  	this._multi=jsm.noud(multi)?false:multi;
  	/** How many options will be shown */
  	this._size=jsm.noud(size)?1:size;  	
//TODO other delimiter  	
  	this._delimiter=",";
  }
  
  JsmSelectField.extendClass(JsmWidget);
  
  /** @ignore */
  JsmSelectField.prototype.className="JsmSelectField";
  
  /** @ignore */
  JsmSelectField.prototype.toString = function() {
  	return "[JsmSelectField object]\n"; 	
  }
  
  /** Get the selected options values or labels 
  * @return {Array}
  */
  JsmSelectField.prototype.getSelected = function() {
  	return this.selected;
  }    
  
  /** Set the selected options values or labels 
  * @param {Array} selectedValueOrLabel Array of selected values or labels
  * @return this
  */  
  JsmSelectField.prototype.setSelected = function(selectedValueOrLabel) {
  	this.selected = selected;
  	return this;
  } 

  /** Set the flag if this is a multiselectbox
  * @param {boolean} multi
  * @return this
  */  
  JsmSelectField.prototype.setMulti = function(multi) {
  	this._multi = multi;
  	return this;
  }  
  
  /** Is this a multiselectbox
  * @return {boolean}
  */  
  JsmSelectField.prototype.isMulti = function() {
  	return this._multi;
  } 
  
  /** Set the number of visible options
  * @param {int} size
  * @return this
  */  
  JsmSelectField.prototype.setSize = function(size) {
  	this._size = size;
  	return this;
  }  
  
  /** Get the number of visible options
  * @return {int} size
  */  
  JsmSelectField.prototype.getSize = function() {
  	return this._size;
  }          

  /** 
  * @return {Array} The options
  */  
  JsmSelectField.prototype.getOptions = function() {
  	return this._options;
  }    

  /** Set the options
  * @param {Array} options Array of options (value-label pairs), eg. [{'value':0, 'label':"All"},{'value':1, 'label':"UK"},{'value':2, 'label':"Ireland"}]
  * @return this
  */    
  JsmSelectField.prototype.setOptions = function(options) {
  	this._options = options;
  	return this;
  }
  
  /** Add a single option to this select box
  * @param {String} value
  * @param {String} label  
  */
  JsmSelectField.prototype.addOption = function(value, label) {
  	this._options.push({'value': value, 'label': label});
  	return this;
  }           

  /** Init this select box; set selected to the cells value
  * @param {JsmTd} theTd The cell in which this widget is displayed
  */  
  JsmSelectField.prototype.init = function(theTd) {  
  	var value = theTd.getValue();
  	logger.debug("JsmSelectField: init(): " + theTd.getValue());
  	this.selected = theTd.getValue().split(this._delimiter);
  	return this;
  }  
  
  /** Update the tabel model if all validators are passed; will set the value and the meta data 
   * {@see JsmObject.META_DATA_NAME_VALUE} on the hosting {@JsmTd} object. For multi select boxes
   * the meta data will be set to a JSON-Array-String of current values, eg. ['1', '2'] otherwise
   * simply to a string value
   * @param {JsmTable} table The table this widget is attached to
   * @param {int} rowIdx The index of the row
   * @param {int} columnIdx The index of the column
   * @param theViewComponent   
   */  
  JsmSelectField.prototype.updateModel = function(table, rowIdx, columnIdx, theViewComponent) {
  	if (jsm.noud(theViewComponent)) {return;}
	if (!this.validate(table, rowIdx, columnIdx, theViewComponent)) {
		return;
	}  
	//get currently selected labels and values
	var currentLabel=""; //comma separated list of labels
	var currentSelectedLabels=new Array();//Array of currently selected labels
	var currentValue="";
	if (this.isMulti()) currentValue="["; //JSON-Array-String of current values, eg. ['1', '2']
	var options = theViewComponent.options;
	for (var i=0;i<options.length;i++) {
		var option = options[i];
		logger.trace("JsmSelectField: option: " + option + option.text + option.value);				
		if (option.selected) {
			currentSelectedLabels.push(option.text);
			currentLabel+=option.text + this._delimiter + " ";
			if (this.isMulti()) {
			 	currentValue+= "'" + option.value + "', ";
			} else {
				currentValue+= option.value;
			}	
		}		
	}
	currentLabel = currentLabel.substr(0, currentLabel.length-2);
	if (this.isMulti() && currentValue.length>2) currentValue = currentValue.substr(0, currentValue.length-2);	
	if (this.isMulti()) currentValue+="]";			
  	logger.debug("JsmSelectField: currentLabel: " + currentLabel);	
  	logger.debug("JsmSelectField: currentValue: " + currentValue);	  	
  	td = table.getTd(rowIdx, columnIdx);
  	//set the dirty flag if an options is selected and the model value is different from the selected options label
  	if (!jsm.arrayOfSameValues(this.selected, currentSelectedLabels)) {
		logger.debug("JsmSelectField: Set the td to dirty");  		
  		td.setDirty(true);
 	}

  	//set the label as the cells value
	td.setValue(currentLabel);
	//set the value as a meta data
	td.setMetaData(this.META_DATA_NAME_VALUE, currentValue);
  }    
  
  /** Returns the node of the view component; the model (this) will be attached to the node as property 'model'
  * @return theViewComponent The node of the view component (for example to append to a table cell)
  */  
  JsmSelectField.prototype.getNode = function() {
  	var selectField = document.createElement("select"); 
	selectField.model=this;  	
  	selectField.setAttribute("id", this.id);
  	selectField.setAttribute("name", this.id);  
  	if (this.isMulti()) {
  		selectField.multiple=true;
  		selectField.size=this.getSize();
  	}  		
  	 
  	this.addAttributesToViewComponent(selectField);  	
  	
  	//add Option tags to the select field
	for (var optionIdx=0;optionIdx<this.getOptions().length;optionIdx++) {	
		var optionObject = document.createElement("option");
		option = this.getOptions()[optionIdx];
		optionObject.text = option.label;
		optionObject.value = option.value;		
		selectField.options[selectField.length] = optionObject;
		if (!jsm.noud(this.selected) && (jsm.arrayContains(this.selected, option.value) || jsm.arrayContains(this.selected, option.label))) {			
			logger.debug("JsmSelectField: Option is selected: " + option.label); 
			optionObject.selected = true;
			jsmHangOntoSelectedWidgetNodes(optionObject);
		}			
  	}    	 	    	
  	return selectField;
  }    
  
  /** Clones this JsmSelectField
  * @return {JsmSelectField}
  */
  JsmSelectField.prototype.clone = function() {
	var clonedWidget = new JsmSelectField();
   	//TODO is it sufficient to copy the Array or do we have to iterate and copy each entry
   	clonedWidget._attributes = this._attributes;
   	clonedWidget._metaData = this._metaData;   
   	clonedWidget._options = this._options;   
  	clonedWidget._multi = this._multi;
  	clonedWidget._size = this._size;  	
  	clonedWidget._delimiter = this._delimiter;   			
   	return clonedWidget;	
  }      

jsm

Documentation generated by JSDoc on Tue Sep 26 08:42:57 2006