2022-06-06 08:53:59 +00:00
|
|
|
require('./style.scss');
|
2022-06-06 12:49:18 +00:00
|
|
|
var Component = require('vn/component');
|
2016-09-26 09:28:47 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
module.exports = new Class({
|
2022-06-06 12:49:18 +00:00
|
|
|
Extends: Component
|
2015-03-09 08:36:54 +00:00
|
|
|
,Tag: 'htk-repeater'
|
2015-07-23 15:58:48 +00:00
|
|
|
,Child: 'model'
|
2022-06-18 21:04:34 +00:00
|
|
|
,Properties: {
|
2015-11-19 13:57:23 +00:00
|
|
|
/**
|
|
|
|
* The source data model.
|
2022-05-24 21:11:12 +00:00
|
|
|
*/
|
2022-06-18 21:04:34 +00:00
|
|
|
model: {
|
2015-03-09 08:36:54 +00:00
|
|
|
type: Db.Model
|
2022-05-21 21:31:56 +00:00
|
|
|
,set: function(x) {
|
2022-05-24 21:11:12 +00:00
|
|
|
this.link({_model: x}, {
|
2015-11-09 17:11:51 +00:00
|
|
|
'status-changed': this._onModelChange
|
|
|
|
,'row-deleted': this._onRowDelete
|
|
|
|
,'row-updated': this._onRowUpdate
|
|
|
|
,'row-inserted': this._onRowInsert
|
2015-03-09 08:36:54 +00:00
|
|
|
});
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
this._onModelChange();
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
2022-05-21 21:31:56 +00:00
|
|
|
,get: function() {
|
2015-03-09 08:36:54 +00:00
|
|
|
this._model;
|
|
|
|
}
|
|
|
|
}
|
2015-11-19 13:57:23 +00:00
|
|
|
/**
|
|
|
|
* The identifier for internal iterator.
|
2022-05-24 21:11:12 +00:00
|
|
|
*/
|
2022-06-18 21:04:34 +00:00
|
|
|
,formId: {
|
2015-03-09 08:36:54 +00:00
|
|
|
type: String
|
2022-05-21 21:31:56 +00:00
|
|
|
,set: function(x) {
|
2015-11-09 08:14:33 +00:00
|
|
|
this._formId = x;
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
2022-05-21 21:31:56 +00:00
|
|
|
,get: function() {
|
2015-11-09 08:14:33 +00:00
|
|
|
this._formId;
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
2015-07-03 05:49:45 +00:00
|
|
|
}
|
2015-11-19 13:57:23 +00:00
|
|
|
/**
|
|
|
|
* {Function (Vn.BuilderResult, Db.Form)} Function to call after every
|
|
|
|
* box rendering.
|
2022-05-24 21:11:12 +00:00
|
|
|
*/
|
2022-06-18 21:04:34 +00:00
|
|
|
,renderer: {
|
2015-07-03 05:49:45 +00:00
|
|
|
type: Function
|
2022-05-21 21:31:56 +00:00
|
|
|
,set: function(x) {
|
2015-07-03 05:49:45 +00:00
|
|
|
this._renderer = x;
|
|
|
|
}
|
2022-05-21 21:31:56 +00:00
|
|
|
,get: function() {
|
2015-07-03 05:49:45 +00:00
|
|
|
this._renderer;
|
|
|
|
}
|
|
|
|
}
|
2018-03-26 16:35:02 +00:00
|
|
|
/**
|
|
|
|
* Wether to show the model status.
|
2022-05-24 21:11:12 +00:00
|
|
|
*/
|
2022-06-18 21:04:34 +00:00
|
|
|
,showStatus: {
|
2018-03-26 16:35:02 +00:00
|
|
|
type: Boolean
|
2022-05-21 21:31:56 +00:00
|
|
|
,set: function(x) {
|
2018-03-26 16:35:02 +00:00
|
|
|
this._showStatus = x;
|
|
|
|
this._onModelChange();
|
|
|
|
}
|
2022-05-21 21:31:56 +00:00
|
|
|
,get: function() {
|
2018-03-26 16:35:02 +00:00
|
|
|
this._showStatus;
|
|
|
|
}
|
|
|
|
}
|
2015-11-19 13:57:23 +00:00
|
|
|
/**
|
|
|
|
* Message that should be displayed when source model is not ready.
|
2022-05-24 21:11:12 +00:00
|
|
|
*/
|
2022-06-18 21:04:34 +00:00
|
|
|
,emptyMessage: {
|
2015-03-15 12:44:57 +00:00
|
|
|
type: String
|
2016-10-20 08:37:08 +00:00
|
|
|
,value: null
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 08:14:33 +00:00
|
|
|
,_builder: null
|
|
|
|
,_formId: 'form'
|
2018-03-26 16:35:02 +00:00
|
|
|
,_showStatus: true
|
2015-03-09 08:36:54 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,render: function() {
|
|
|
|
var div = this.createRoot('div');
|
2015-11-09 17:11:51 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
this._container = this.createElement('div');
|
2015-11-09 17:11:51 +00:00
|
|
|
this._container.className = 'htk-repeater';
|
2022-05-21 21:31:56 +00:00
|
|
|
div.appendChild(this._container);
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
,loadXml: function(scope, node) {
|
2022-06-06 16:02:17 +00:00
|
|
|
Component.prototype.loadXml.call(this, scope, node);
|
2022-05-24 21:11:12 +00:00
|
|
|
this._parentScope = scope;
|
2015-07-23 15:58:48 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
var builder = this._builder = new Vn.Builder();
|
2022-05-24 21:11:12 +00:00
|
|
|
builder.compileNode(node.firstElementChild, [this._formId]);
|
2015-11-09 08:14:33 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
this._onModelChange();
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
2015-03-15 12:44:57 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,getChild: function(index) {
|
2015-11-09 17:11:51 +00:00
|
|
|
return this._container.childNodes[index];
|
2015-07-10 12:30:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,getBuilder: function(index) {
|
2022-06-18 21:04:34 +00:00
|
|
|
return this._childsData[index].scope;
|
2015-07-23 15:58:48 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,getForm: function(index) {
|
2016-05-05 15:47:45 +00:00
|
|
|
return this._childsData[index].set;
|
2015-07-23 15:58:48 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,_buildBox: function(index) {
|
|
|
|
var set = new Db.SimpleIterator({
|
2015-11-09 08:14:33 +00:00
|
|
|
model: this._model,
|
|
|
|
row: index
|
|
|
|
});
|
2022-05-24 21:11:12 +00:00
|
|
|
|
|
|
|
var scope = this._builder.load(this.doc, null, this._parentScope);
|
2022-06-18 21:04:34 +00:00
|
|
|
scope.link({
|
|
|
|
$iter: set,
|
|
|
|
[this._formId]: set.getObject()
|
2022-05-24 21:11:12 +00:00
|
|
|
});
|
2015-07-23 15:58:48 +00:00
|
|
|
|
2022-06-18 21:04:34 +00:00
|
|
|
this._childsData.push({scope, set});
|
2015-07-03 05:49:45 +00:00
|
|
|
|
|
|
|
if (this._renderer)
|
2022-05-24 21:11:12 +00:00
|
|
|
this._renderer(scope, set);
|
2015-11-09 17:11:51 +00:00
|
|
|
|
2022-05-24 21:11:12 +00:00
|
|
|
return scope.getMain();
|
2015-03-15 12:44:57 +00:00
|
|
|
}
|
2015-03-09 08:36:54 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,_onModelChange: function() {
|
2015-11-09 08:14:33 +00:00
|
|
|
if (!this._model || !this._builder)
|
2015-03-09 08:36:54 +00:00
|
|
|
return;
|
2015-11-09 17:11:51 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
this.node.removeChild(this._container);
|
|
|
|
Vn.Node.removeChilds(this._container);
|
2015-11-09 17:11:51 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
this._freeChildsData();
|
2015-11-09 17:11:51 +00:00
|
|
|
this._childsData = [];
|
2015-03-09 08:36:54 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
switch (this._model.status) {
|
2015-03-15 12:44:57 +00:00
|
|
|
case Db.Model.Status.READY:
|
|
|
|
{
|
|
|
|
for (var i = 0; i < this._model.numRows; i++)
|
2022-05-21 21:31:56 +00:00
|
|
|
this._container.appendChild(this._buildBox(i));
|
2015-03-09 08:36:54 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
this._showNoRecordsFound();
|
2015-03-15 12:44:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Db.Model.Status.LOADING:
|
2022-05-21 21:31:56 +00:00
|
|
|
this._showMessage(_('Loading'), null);
|
2015-03-15 12:44:57 +00:00
|
|
|
break;
|
|
|
|
case Db.Model.Status.CLEAN:
|
2016-10-20 08:37:08 +00:00
|
|
|
{
|
|
|
|
var emptyMessage = this.emptyMessage ?
|
|
|
|
this.emptyMessage : _('NoData');
|
2022-05-21 21:31:56 +00:00
|
|
|
this._showMessage(emptyMessage, 'refresh');
|
2015-03-15 12:44:57 +00:00
|
|
|
break;
|
2016-10-20 08:37:08 +00:00
|
|
|
}
|
2015-03-15 12:44:57 +00:00
|
|
|
case Db.Model.Status.ERROR:
|
2022-05-21 21:31:56 +00:00
|
|
|
this._showMessage(_('ErrorLoadingData'), 'error');
|
2015-03-15 12:44:57 +00:00
|
|
|
break;
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
2015-11-19 13:57:23 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
this.node.appendChild(this._container);
|
2022-05-30 01:30:33 +00:00
|
|
|
this.emit('change');
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,_showNoRecordsFound: function() {
|
2015-11-09 17:11:51 +00:00
|
|
|
if (this._model.numRows === 0)
|
2022-05-21 21:31:56 +00:00
|
|
|
this._showMessage(_('EmptyList'), 'block');
|
2015-03-15 12:44:57 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,_showMessage: function(message, src) {
|
2018-03-26 16:35:02 +00:00
|
|
|
if (!this._showStatus)
|
|
|
|
return;
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
var div = this.createElement('div');
|
2015-03-15 12:44:57 +00:00
|
|
|
div.className = 'message';
|
2022-05-21 21:31:56 +00:00
|
|
|
this._container.appendChild(div);
|
2015-03-15 12:44:57 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
if (src) {
|
|
|
|
const icon = new Htk.Icon({
|
|
|
|
name: src
|
|
|
|
});
|
|
|
|
div.appendChild(icon.node);
|
|
|
|
} else {
|
|
|
|
var spinner = new Htk.Spinner();
|
|
|
|
spinner.start();
|
|
|
|
div.appendChild(spinner.node);
|
2015-11-19 13:57:23 +00:00
|
|
|
}
|
2015-03-15 12:44:57 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
div.appendChild(this.createTextNode(message));
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,_onRowDelete: function(model, row) {
|
|
|
|
Vn.Node.remove(this._container.childNodes[row]);
|
|
|
|
this._unrefChildData(row);
|
|
|
|
this._childsData.splice(row, 1);
|
2016-05-05 15:47:45 +00:00
|
|
|
|
|
|
|
for (var i = row; i < this._model.numRows; i++)
|
|
|
|
this._childsData[i].set.row = i;
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
this._showNoRecordsFound();
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,_onRowUpdate: function(model, row) {
|
|
|
|
this._childsData[row].set.iterChanged();
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,_onRowInsert: function(model, row) {
|
|
|
|
var box = this._buildBox(row);
|
|
|
|
this._container.appendChild(box);
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
2015-07-28 19:14:26 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,_freeChildsData: function() {
|
2015-11-09 17:11:51 +00:00
|
|
|
if (this._childsData)
|
|
|
|
for (var i = 0; i < this._childsData.length; i++)
|
2022-05-21 21:31:56 +00:00
|
|
|
this._unrefChildData(i);
|
2015-11-09 17:11:51 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,_unrefChildData: function(index) {
|
2015-11-09 17:11:51 +00:00
|
|
|
var childData = this._childsData[index];
|
2022-05-21 21:31:56 +00:00
|
|
|
childData.set.unref();
|
2022-06-18 21:04:34 +00:00
|
|
|
childData.scope.unref();
|
2015-07-28 19:14:26 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,destroy: function() {
|
|
|
|
this._freeChildsData();
|
2022-06-06 16:02:17 +00:00
|
|
|
Component.prototype.destroy.call(this);
|
2015-07-28 19:14:26 +00:00
|
|
|
}
|
2015-03-09 08:36:54 +00:00
|
|
|
});
|
2015-11-09 17:11:51 +00:00
|
|
|
|