forked from verdnatura/hedera-web
108 lines
1.9 KiB
JavaScript
108 lines
1.9 KiB
JavaScript
|
Htk.Select = new Class
|
||
|
({
|
||
|
Extends: Htk.Field
|
||
|
,Tag: 'htk-combo'
|
||
|
,Properties:
|
||
|
{
|
||
|
/**
|
||
|
* The model associated to this form.
|
||
|
**/
|
||
|
model:
|
||
|
{
|
||
|
type: Db.Model
|
||
|
,set: function (x)
|
||
|
{
|
||
|
this.link ({_model: x}, {'status-changed': this.onModelChange});
|
||
|
this.onModelChange ();
|
||
|
}
|
||
|
,get: function ()
|
||
|
{
|
||
|
return this._model;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
,_model: null
|
||
|
,valueColumnIndex: 0
|
||
|
,valueColumnName: null
|
||
|
,showColumnIndex: 1
|
||
|
,showColumnName: null
|
||
|
|
||
|
,initialize: function (props)
|
||
|
{
|
||
|
this.parent (props);
|
||
|
this.createElement ('select');
|
||
|
this.node.addEventListener ('change', this.changed.bind (this));
|
||
|
}
|
||
|
|
||
|
,changed: function (event)
|
||
|
{
|
||
|
var value;
|
||
|
var row = this.node.selectedIndex - 1;
|
||
|
|
||
|
if (row >= 0)
|
||
|
value = this._model.getByIndex (row, this.valueColumnIndex);
|
||
|
else
|
||
|
value = null;
|
||
|
|
||
|
this.valueChanged (value);
|
||
|
}
|
||
|
|
||
|
,addOption: function (value, text)
|
||
|
{
|
||
|
var option = document.createElement ('option');
|
||
|
option.value = value;
|
||
|
option.appendChild (document.createTextNode (text));
|
||
|
this.node.appendChild (option);
|
||
|
}
|
||
|
|
||
|
,onModelChange: function ()
|
||
|
{
|
||
|
var model = this._model;
|
||
|
|
||
|
if (model.status == Db.Model.Status.LOADING)
|
||
|
return;
|
||
|
|
||
|
Vn.Node.removeChilds (this.node);
|
||
|
|
||
|
switch (model.status)
|
||
|
{
|
||
|
case Db.Model.Status.READY:
|
||
|
{
|
||
|
var data = model.data;
|
||
|
this.addOption (null, '');
|
||
|
|
||
|
for (var i = 0; i < data.length; i++)
|
||
|
this.addOption (data[i][this.showColumnIndex], data[i][1]);
|
||
|
|
||
|
this.selectOption ();
|
||
|
break;
|
||
|
}
|
||
|
case Db.Model.Status.ERROR:
|
||
|
this.addOption (null, _('Error'));
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
,setEditable: function (editable)
|
||
|
{
|
||
|
this.node.disabled = !editable;
|
||
|
}
|
||
|
|
||
|
,selectOption: function ()
|
||
|
{
|
||
|
if (!this._model || this._model.status != Db.Model.Status.READY)
|
||
|
return;
|
||
|
|
||
|
var row = this._model.searchByIndex (this.valueColumnIndex, this._value);
|
||
|
|
||
|
if (row != -1)
|
||
|
this.node.selectedIndex = row + 1;
|
||
|
}
|
||
|
|
||
|
,putValue: function (value)
|
||
|
{
|
||
|
this.selectOption ();
|
||
|
}
|
||
|
});
|