2022-06-06 08:53:59 +00:00
|
|
|
require('./style.scss');
|
2022-06-06 12:49:18 +00:00
|
|
|
var ColumnText = require('../columns/text');
|
2016-09-26 09:28:47 +00:00
|
|
|
|
2022-05-25 18:04:16 +00:00
|
|
|
module.exports = new Class({
|
2015-01-23 13:09:30 +00:00
|
|
|
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'
|
2022-05-28 15:49:46 +00:00
|
|
|
,Properties: {
|
2015-01-23 13:09:30 +00:00
|
|
|
/**
|
|
|
|
* The model associated to this form.
|
2022-05-25 18:04:16 +00:00
|
|
|
*/
|
2022-05-28 15:49:46 +00:00
|
|
|
model: {
|
2015-01-23 13:09:30 +00:00
|
|
|
type: Db.Model
|
2022-11-16 01:46:44 +00:00
|
|
|
,set(x) {
|
2022-05-25 18:04:16 +00:00
|
|
|
this.link({_model: x}, {'status-changed-after': this._onModelChange});
|
|
|
|
this._onModelChange();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2022-11-16 01:46:44 +00:00
|
|
|
,get() {
|
2015-01-23 13:09:30 +00:00
|
|
|
return this._model;
|
|
|
|
}
|
2015-07-03 05:49:45 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* The row where the form positioned, has -1 if the row is unselected.
|
2022-05-25 18:04:16 +00:00
|
|
|
*/
|
2022-05-28 15:49:46 +00:00
|
|
|
row: {
|
2015-07-03 05:49:45 +00:00
|
|
|
type: Number
|
2022-11-16 01:46:44 +00:00
|
|
|
,set(x) {
|
2015-07-03 05:49:45 +00:00
|
|
|
if (!this._model || this._model.numRows <= x || x < -1)
|
|
|
|
x = -1;
|
|
|
|
if (x == this._row)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._row = x;
|
2022-05-25 18:04:16 +00:00
|
|
|
this.iterChanged();
|
2015-07-03 05:49:45 +00:00
|
|
|
}
|
2022-11-16 01:46:44 +00:00
|
|
|
,get() {
|
2015-07-03 05:49:45 +00:00
|
|
|
return this._row;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* The number of rows in the form.
|
2022-05-25 18:04:16 +00:00
|
|
|
*/
|
2022-06-06 12:49:18 +00:00
|
|
|
numRows:{
|
2015-07-03 05:49:45 +00:00
|
|
|
type: Number
|
2022-11-16 01:46:44 +00:00
|
|
|
,get() {
|
2015-07-03 05:49:45 +00:00
|
|
|
if (this._model)
|
|
|
|
return this._model.numRows;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Checks if the form data is ready.
|
2022-05-25 18:04:16 +00:00
|
|
|
*/
|
2022-06-06 12:49:18 +00:00
|
|
|
ready:{
|
2015-07-03 05:49:45 +00:00
|
|
|
type: Boolean
|
2022-11-16 01:46:44 +00:00
|
|
|
,get() {
|
2015-11-19 13:57:23 +00:00
|
|
|
return this._model && this._model.ready;
|
2015-07-03 05:49:45 +00:00
|
|
|
}
|
2015-09-28 11:37:31 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Checks if the form data is ready.
|
2022-05-25 18:04:16 +00:00
|
|
|
*/
|
2022-06-06 12:49:18 +00:00
|
|
|
placeholder:{
|
2015-09-28 11:37:31 +00:00
|
|
|
type: String
|
2022-11-16 01:46:44 +00:00
|
|
|
,set(x) {
|
2015-09-28 11:37:31 +00:00
|
|
|
this._placeholder = x;
|
2022-05-25 18:04:16 +00:00
|
|
|
this._refreshShowText(x);
|
2015-09-28 11:37:31 +00:00
|
|
|
}
|
2022-11-16 01:46:44 +00:00
|
|
|
,get() {
|
2015-09-28 11:37:31 +00:00
|
|
|
return this._placeholder;
|
|
|
|
}
|
2015-11-17 10:34:33 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Wether to allow null values.
|
2022-05-25 18:04:16 +00:00
|
|
|
*/
|
2022-06-06 12:49:18 +00:00
|
|
|
notNull:{
|
2015-11-17 10:34:33 +00:00
|
|
|
type: Boolean
|
2022-11-16 01:46:44 +00:00
|
|
|
,set(x) {
|
2015-11-17 10:34:33 +00:00
|
|
|
this._notNull = x;
|
|
|
|
}
|
2022-11-16 01:46:44 +00:00
|
|
|
,get() {
|
2015-11-17 10:34:33 +00:00
|
|
|
return this._notNull;
|
2015-07-03 05:49:45 +00:00
|
|
|
}
|
2022-05-28 15:49:46 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* The row object.
|
|
|
|
*/
|
|
|
|
$: {
|
|
|
|
type: Object
|
2022-11-16 01:46:44 +00:00
|
|
|
,get() {
|
2022-06-06 12:49:18 +00:00
|
|
|
return this._model.getObject(this._row) || {};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
params: {
|
|
|
|
type: Object
|
2022-11-16 01:46:44 +00:00
|
|
|
,get() {
|
2022-06-06 12:49:18 +00:00
|
|
|
return this.$;
|
2022-05-28 15:49:46 +00:00
|
|
|
}
|
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-12-10 13:48:43 +00:00
|
|
|
,_webkitRefresh: false
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,render() {
|
2022-05-25 18:04:16 +00:00
|
|
|
const button = this.createRoot('button');
|
2016-08-30 07:43:47 +00:00
|
|
|
button.type = 'button';
|
2022-06-06 17:13:57 +00:00
|
|
|
button.classList.add('input');
|
2022-06-06 08:53:59 +00:00
|
|
|
button.addEventListener('mousedown',
|
|
|
|
e => this._onButtonMouseDown(e));
|
2022-05-25 18:04:16 +00:00
|
|
|
|
|
|
|
this.label = this.createElement('span');
|
|
|
|
button.appendChild(this.label);
|
|
|
|
|
2022-06-06 08:53:59 +00:00
|
|
|
const erase = new Htk.Icon({
|
|
|
|
name: 'close',
|
|
|
|
title: _('Erase')
|
|
|
|
});
|
|
|
|
erase.classList.add('erase');
|
|
|
|
erase.addEventListener('mousedown',
|
|
|
|
e => this._onEraseMouseDown(e));
|
|
|
|
button.appendChild(erase.node);
|
|
|
|
|
2022-05-25 18:04:16 +00:00
|
|
|
const dropDown = new Htk.Icon({
|
|
|
|
name: 'expand_more'
|
|
|
|
});
|
|
|
|
button.appendChild(dropDown.node);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_setRow(row) {
|
2015-07-03 05:49:45 +00:00
|
|
|
this._row = row;
|
2022-05-25 18:04:16 +00:00
|
|
|
this._refreshShowText();
|
|
|
|
this.iterChanged();
|
2015-07-03 05:49:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_onButtonMouseDown(event) {
|
2022-05-25 18:04:16 +00:00
|
|
|
if (this._popup) {
|
|
|
|
this._popup.hide();
|
2015-12-15 15:22:46 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-06-06 08:53:59 +00:00
|
|
|
|
|
|
|
if (event.defaultPrevented) return;
|
|
|
|
event.preventDefault();
|
2015-12-15 15:22:46 +00:00
|
|
|
|
|
|
|
var model = this._model;
|
|
|
|
|
2022-05-25 18:04:16 +00:00
|
|
|
var menu = this.createElement('div');
|
2015-12-15 15:22:46 +00:00
|
|
|
menu.className = 'htk-select-menu';
|
|
|
|
|
2022-05-25 18:04:16 +00:00
|
|
|
var grid = new Htk.Grid({showHeader: false});
|
|
|
|
menu.appendChild(grid.node);
|
2015-12-15 15:22:46 +00:00
|
|
|
|
2016-10-16 14:16:08 +00:00
|
|
|
var gridNode = grid.node;
|
2022-05-25 18:04:16 +00:00
|
|
|
gridNode.addEventListener('click', this._onGridClicked.bind(this, grid));
|
2015-12-15 15:22:46 +00:00
|
|
|
|
2022-05-25 18:04:16 +00:00
|
|
|
var column = new ColumnText({columnIndex: this.showColumnIndex});
|
|
|
|
grid.appendColumn(column);
|
2015-12-15 15:22:46 +00:00
|
|
|
grid.model = model;
|
|
|
|
|
2022-05-25 18:04:16 +00:00
|
|
|
var popup = this._popup = new Htk.Popup({childNode: menu});
|
|
|
|
popup.on('closed', this._onPopupClose.bind(this));
|
2022-06-06 08:53:59 +00:00
|
|
|
popup.show(this.node, event);
|
2015-12-15 15:22:46 +00:00
|
|
|
|
2022-05-30 01:30:33 +00:00
|
|
|
this.emit('menu-show');
|
2015-12-15 15:22:46 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_onGridClicked(grid, e) {
|
2022-10-04 08:17:43 +00:00
|
|
|
let cell;
|
|
|
|
let target = e.target;
|
2015-12-15 15:22:46 +00:00
|
|
|
|
2022-10-04 08:17:43 +00:00
|
|
|
while (target !== grid.tbody) {
|
|
|
|
cell = target;
|
|
|
|
target = target.parentNode;
|
2015-12-15 15:22:46 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 08:17:43 +00:00
|
|
|
if (!cell) return;
|
|
|
|
|
|
|
|
let value;
|
|
|
|
const row = cell.rowIndex - 1;
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
if (row >= 0)
|
2022-05-25 18:04:16 +00:00
|
|
|
value = this._model.getByIndex(row, this.valueColumnIndex);
|
2015-01-23 13:09:30 +00:00
|
|
|
else
|
|
|
|
value = null;
|
|
|
|
|
2022-11-23 12:52:20 +00:00
|
|
|
this._setMyValue(row, value);
|
2022-05-25 18:04:16 +00:00
|
|
|
this._popup.hide();
|
2015-12-15 15:22:46 +00:00
|
|
|
|
2022-05-25 18:04:16 +00:00
|
|
|
e.stopPropagation();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2022-11-23 12:52:20 +00:00
|
|
|
|
|
|
|
,_onEraseMouseDown(event) {
|
|
|
|
if (event.defaultPrevented) return;
|
|
|
|
event.preventDefault();
|
|
|
|
this._setMyValue(-1, undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
,_setMyValue(row, value) {
|
|
|
|
this._myValue = value;
|
|
|
|
this._setRow(row);
|
|
|
|
this.valueChanged(value);
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_onPopupClose() {
|
2015-12-15 15:22:46 +00:00
|
|
|
this._popup = null;
|
2022-05-30 01:30:33 +00:00
|
|
|
this.emit('menu-hide');
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_refreshShowText() {
|
2022-11-23 12:52:20 +00:00
|
|
|
const model = this._model;
|
|
|
|
let showText = '';
|
|
|
|
|
2015-12-15 15:22:46 +00:00
|
|
|
if (this._row !== -1)
|
2022-11-23 12:52:20 +00:00
|
|
|
showText = model.getByIndex(this._row, this.showColumnIndex);
|
2016-01-07 12:58:29 +00:00
|
|
|
else if (model && model.status === Db.Model.Status.LOADING)
|
2022-11-23 12:52:20 +00:00
|
|
|
showText = _('Loading...');
|
|
|
|
else if (this._myValue != null)
|
|
|
|
showText = this._myValue;
|
2015-12-15 15:22:46 +00:00
|
|
|
else if (this._placeholder)
|
2022-11-23 12:52:20 +00:00
|
|
|
showText = this._placeholder;
|
2015-09-28 11:37:31 +00:00
|
|
|
|
2022-05-25 18:04:16 +00:00
|
|
|
Vn.Node.setText(this.label, showText);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_onModelChange() {
|
2015-01-23 13:09:30 +00:00
|
|
|
var model = this._model;
|
2022-05-30 01:30:33 +00:00
|
|
|
this.emit('status-changed');
|
2015-10-14 11:51:43 +00:00
|
|
|
|
2015-12-15 15:22:46 +00:00
|
|
|
if (this._popup)
|
2022-05-25 18:04:16 +00:00
|
|
|
this._popup.reset();
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-25 18:04:16 +00:00
|
|
|
if (model && model.ready) {
|
|
|
|
this._selectOption();
|
2022-05-30 01:30:33 +00:00
|
|
|
this.emit('ready');
|
2022-05-25 18:04:16 +00:00
|
|
|
} else
|
|
|
|
this._setRow(-1);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,setEditable(editable) {
|
2015-01-23 13:09:30 +00:00
|
|
|
this.node.disabled = !editable;
|
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_selectOption() {
|
2015-07-03 05:49:45 +00:00
|
|
|
var row;
|
|
|
|
|
2015-10-14 11:51:43 +00:00
|
|
|
if (this._model && this._model.ready)
|
2022-11-23 12:52:20 +00:00
|
|
|
row = this._model.searchByIndex(this.valueColumnIndex, this._myValue);
|
2015-07-03 05:49:45 +00:00
|
|
|
else
|
|
|
|
row = -1;
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-25 18:04:16 +00:00
|
|
|
this._setRow(row);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,putValue() {
|
2022-11-23 12:52:20 +00:00
|
|
|
this._myValue = this._value;
|
2022-05-25 18:04:16 +00:00
|
|
|
this._selectOption();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
});
|