2015-01-23 13:09:30 +00:00
|
|
|
Htk.Select = new Class
|
|
|
|
({
|
|
|
|
Extends: Htk.Field
|
2015-07-03 05:49:45 +00:00
|
|
|
,Implements: Db.Iterator
|
2015-01-23 13:09:30 +00:00
|
|
|
,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;
|
|
|
|
}
|
2015-07-03 05:49:45 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* The row where the form positioned, has -1 if the row is unselected.
|
|
|
|
**/
|
|
|
|
row:
|
|
|
|
{
|
|
|
|
type: Number
|
|
|
|
,set: function (x)
|
|
|
|
{
|
|
|
|
if (!this._model || this._model.numRows <= x || x < -1)
|
|
|
|
x = -1;
|
|
|
|
if (x == this._row)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._row = x;
|
|
|
|
this.iterChanged ();
|
|
|
|
}
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
return this._row;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* The number of rows in the form.
|
|
|
|
**/
|
|
|
|
numRows:
|
|
|
|
{
|
|
|
|
type: Number
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
if (this._model)
|
|
|
|
return this._model.numRows;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Checks if the form data is ready.
|
|
|
|
**/
|
|
|
|
ready:
|
|
|
|
{
|
|
|
|
type: Boolean
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
return this._ready;
|
|
|
|
}
|
2015-09-28 11:37:31 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Checks if the form data is ready.
|
|
|
|
**/
|
|
|
|
placeholder:
|
|
|
|
{
|
|
|
|
type: String
|
|
|
|
,set: function (x)
|
|
|
|
{
|
|
|
|
this._placeholder = x;
|
2015-10-14 11:51:43 +00:00
|
|
|
|
2015-09-28 11:37:31 +00:00
|
|
|
if (this._placeholderNode)
|
|
|
|
Vn.Node.setText (this._placeholderNode, x);
|
|
|
|
}
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
return this._placeholder;
|
|
|
|
}
|
2015-11-17 10:34:33 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Wether to allow null values.
|
|
|
|
**/
|
|
|
|
notNull:
|
|
|
|
{
|
|
|
|
type: Boolean
|
|
|
|
,set: function (x)
|
|
|
|
{
|
|
|
|
this._notNull = x;
|
|
|
|
}
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
return this._notNull;
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 05:49:45 +00:00
|
|
|
,_row: -1
|
2015-01-23 13:09:30 +00:00
|
|
|
,_model: null
|
|
|
|
,valueColumnIndex: 0
|
|
|
|
,valueColumnName: null
|
|
|
|
,showColumnIndex: 1
|
|
|
|
,showColumnName: null
|
2015-11-17 10:34:33 +00:00
|
|
|
,_notNull: true
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
,initialize: function (props)
|
|
|
|
{
|
|
|
|
this.createElement ('select');
|
2015-10-14 11:51:43 +00:00
|
|
|
this.node.addEventListener ('change', this._onChange.bind (this));
|
|
|
|
this._addPlaceholder (this._placeholder);
|
2015-11-09 08:14:33 +00:00
|
|
|
|
|
|
|
this.parent (props);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-09-28 14:05:54 +00:00
|
|
|
|
|
|
|
,on: function (id, callback, instance)
|
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case 'click':
|
2015-10-14 11:51:43 +00:00
|
|
|
case 'mousedown':
|
|
|
|
case 'focusout':
|
2015-09-28 14:05:54 +00:00
|
|
|
this.node.addEventListener (id,
|
|
|
|
callback.bind (instance, this));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this.parent (id, callback, instance);
|
|
|
|
}
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-07-03 05:49:45 +00:00
|
|
|
,setRow: function (row)
|
|
|
|
{
|
2015-10-14 11:51:43 +00:00
|
|
|
if (row != -1)
|
2015-10-23 23:23:19 +00:00
|
|
|
this.node.selectedIndex = row + this.getIndexIncrement ();
|
|
|
|
else if (this._placeholder)
|
2015-10-14 11:51:43 +00:00
|
|
|
this.node.selectedIndex = 0;
|
2015-10-23 23:23:19 +00:00
|
|
|
else
|
|
|
|
this.node.selectedIndex = -1;
|
2015-10-14 11:51:43 +00:00
|
|
|
|
2015-07-03 05:49:45 +00:00
|
|
|
this._row = row;
|
|
|
|
this.iterChanged ();
|
|
|
|
}
|
|
|
|
|
2015-10-14 11:51:43 +00:00
|
|
|
,_onChange: function (event)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
|
|
|
var value;
|
2015-10-23 23:23:19 +00:00
|
|
|
var row = this.node.selectedIndex - this.getIndexIncrement ();
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
if (row >= 0)
|
|
|
|
value = this._model.getByIndex (row, this.valueColumnIndex);
|
|
|
|
else
|
|
|
|
value = null;
|
|
|
|
|
2015-07-03 05:49:45 +00:00
|
|
|
this.setRow (row);
|
2015-10-14 11:51:43 +00:00
|
|
|
this.valueChanged (value);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2015-10-23 23:23:19 +00:00
|
|
|
,getIndexIncrement: function ()
|
|
|
|
{
|
|
|
|
var inc = 0;
|
|
|
|
|
|
|
|
if (this._placeholder)
|
|
|
|
inc++;
|
2015-11-17 10:34:33 +00:00
|
|
|
if (!this._notNull)
|
|
|
|
inc++;
|
2015-10-23 23:23:19 +00:00
|
|
|
|
|
|
|
return inc;
|
|
|
|
}
|
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
,addOption: function (value, text)
|
|
|
|
{
|
|
|
|
var option = document.createElement ('option');
|
|
|
|
option.value = value;
|
2015-09-28 11:37:31 +00:00
|
|
|
|
|
|
|
if (text)
|
|
|
|
option.appendChild (document.createTextNode (text));
|
|
|
|
|
|
|
|
this.node.appendChild (option);
|
|
|
|
}
|
|
|
|
|
2015-10-14 11:51:43 +00:00
|
|
|
,_addPlaceholder: function (text)
|
2015-09-28 11:37:31 +00:00
|
|
|
{
|
|
|
|
var option = document.createElement ('option');
|
2015-10-14 11:51:43 +00:00
|
|
|
option.className = 'htk-option';
|
2015-09-28 11:37:31 +00:00
|
|
|
option.disabled = true;
|
|
|
|
option.selected = true;
|
|
|
|
option.value = null;
|
2015-10-14 11:51:43 +00:00
|
|
|
Vn.Node.setText (option, text ? text : '');
|
2015-01-23 13:09:30 +00:00
|
|
|
this.node.appendChild (option);
|
2015-09-28 11:37:31 +00:00
|
|
|
this._placeholderNode = option;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,onModelChange: function ()
|
|
|
|
{
|
2015-10-14 11:51:43 +00:00
|
|
|
var placeholder = null;
|
2015-01-23 13:09:30 +00:00
|
|
|
var model = this._model;
|
2015-07-03 05:49:45 +00:00
|
|
|
this.signalEmit ('status-changed');
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
Vn.Node.removeChilds (this.node);
|
|
|
|
|
|
|
|
switch (model.status)
|
|
|
|
{
|
|
|
|
case Db.Model.Status.ERROR:
|
2015-10-14 11:51:43 +00:00
|
|
|
placeholder = _('Error');
|
2015-09-28 11:37:31 +00:00
|
|
|
break;
|
2015-07-10 12:30:08 +00:00
|
|
|
case Db.Model.Status.LOADING:
|
2015-10-14 11:51:43 +00:00
|
|
|
placeholder = _('Loading...');
|
|
|
|
break;
|
2015-07-03 05:49:45 +00:00
|
|
|
default:
|
2015-10-14 11:51:43 +00:00
|
|
|
placeholder = this._placeholder;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-10-14 11:51:43 +00:00
|
|
|
|
|
|
|
if (placeholder)
|
|
|
|
this._addPlaceholder (placeholder);
|
|
|
|
|
|
|
|
if (model.ready)
|
|
|
|
{
|
|
|
|
var data = model.data;
|
|
|
|
|
2015-11-17 10:34:33 +00:00
|
|
|
if (!this.notNull)
|
|
|
|
this.addOption (null, null);
|
|
|
|
|
2015-10-14 11:51:43 +00:00
|
|
|
for (var i = 0; i < data.length; i++)
|
|
|
|
this.addOption (data[i][this.showColumnIndex], data[i][1]);
|
|
|
|
|
|
|
|
this.selectOption ();
|
|
|
|
this._onTimeout ();
|
2015-10-23 23:23:19 +00:00
|
|
|
this.signalEmit ('ready');
|
2015-10-14 11:51:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
this.setRow (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
,_onTimeout: function ()
|
|
|
|
{
|
|
|
|
if (this._blockMouseDown || !navigator.userAgent.match (/WebKit/))
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._blockMouseDown = true;
|
|
|
|
|
|
|
|
this.node.blur();
|
|
|
|
|
|
|
|
var e = document.createEvent('MouseEvents');
|
|
|
|
e.initMouseEvent ('mousedown',
|
|
|
|
true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
|
|
|
|
this.node.dispatchEvent(e);
|
|
|
|
|
|
|
|
this._blockMouseDown = false;
|
|
|
|
this.node.focus ();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,setEditable: function (editable)
|
|
|
|
{
|
|
|
|
this.node.disabled = !editable;
|
|
|
|
}
|
|
|
|
|
|
|
|
,selectOption: function ()
|
|
|
|
{
|
2015-07-03 05:49:45 +00:00
|
|
|
var row;
|
|
|
|
|
2015-10-14 11:51:43 +00:00
|
|
|
if (this._model && this._model.ready)
|
2015-07-03 05:49:45 +00:00
|
|
|
row = this._model.searchByIndex (this.valueColumnIndex, this._value);
|
|
|
|
else
|
|
|
|
row = -1;
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-07-03 05:49:45 +00:00
|
|
|
this.setRow (row);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,putValue: function (value)
|
|
|
|
{
|
|
|
|
this.selectOption ();
|
|
|
|
}
|
|
|
|
});
|