2016-09-26 09:28:47 +00:00
|
|
|
|
|
|
|
var Widget = require ('./widget');
|
|
|
|
|
|
|
|
module.exports = new Class
|
2015-03-09 08:36:54 +00:00
|
|
|
({
|
2016-09-26 09:28:47 +00:00
|
|
|
Extends: Widget
|
2015-03-09 08:36:54 +00:00
|
|
|
,Tag: 'htk-repeater'
|
2015-07-23 15:58:48 +00:00
|
|
|
,Child: 'model'
|
2015-03-09 08:36:54 +00:00
|
|
|
,Properties:
|
|
|
|
{
|
2015-11-19 13:57:23 +00:00
|
|
|
/**
|
|
|
|
* The source data model.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-03-09 08:36:54 +00:00
|
|
|
model:
|
|
|
|
{
|
|
|
|
type: Db.Model
|
|
|
|
,set: function (x)
|
|
|
|
{
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2015-11-09 17:11:51 +00:00
|
|
|
this._onModelChange ();
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
this._model;
|
|
|
|
}
|
|
|
|
}
|
2015-11-19 13:57:23 +00:00
|
|
|
/**
|
|
|
|
* The identifier for internal iterator.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-03-09 08:36:54 +00:00
|
|
|
,formId:
|
|
|
|
{
|
|
|
|
type: String
|
|
|
|
,set: function (x)
|
|
|
|
{
|
2015-11-09 08:14:33 +00:00
|
|
|
this._formId = x;
|
2015-03-09 08:36:54 +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.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-07-03 05:49:45 +00:00
|
|
|
,renderer:
|
|
|
|
{
|
|
|
|
type: Function
|
|
|
|
,set: function (x)
|
|
|
|
{
|
|
|
|
this._renderer = x;
|
|
|
|
}
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
this._renderer;
|
|
|
|
}
|
|
|
|
}
|
2015-11-19 13:57:23 +00:00
|
|
|
/**
|
|
|
|
* Message that should be displayed when source model is not ready.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-07-03 05:49:45 +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'
|
2015-03-09 08:36:54 +00:00
|
|
|
|
2016-10-16 14:16:08 +00:00
|
|
|
,render: function ()
|
2015-03-09 08:36:54 +00:00
|
|
|
{
|
2016-10-16 14:16:08 +00:00
|
|
|
var div = this.createRoot ('div');
|
2015-11-09 17:11:51 +00:00
|
|
|
|
2016-10-16 14:16:08 +00:00
|
|
|
this._container = this.createElement ('div');
|
2015-11-09 17:11:51 +00:00
|
|
|
this._container.className = 'htk-repeater';
|
|
|
|
div.appendChild (this._container);
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 08:14:33 +00:00
|
|
|
,loadXml: function (builderResult, node)
|
2015-03-09 08:36:54 +00:00
|
|
|
{
|
2015-11-09 08:14:33 +00:00
|
|
|
this.parent (builderResult, node);
|
2015-07-23 15:58:48 +00:00
|
|
|
|
2015-11-09 08:14:33 +00:00
|
|
|
var builder = this._builder = new Vn.Builder ();
|
|
|
|
builder.setParent (builderResult);
|
|
|
|
builder.loadXmlFromNode (node.firstElementChild);
|
|
|
|
|
2015-11-09 17:11:51 +00:00
|
|
|
this._onModelChange ();
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
2015-03-15 12:44:57 +00:00
|
|
|
|
2015-07-10 12:30:08 +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
|
|
|
}
|
|
|
|
|
2015-07-23 15:58:48 +00:00
|
|
|
,getBuilder: function (index)
|
|
|
|
{
|
2015-11-09 17:11:51 +00:00
|
|
|
return this._childsData[index].builder;
|
2015-07-23 15:58:48 +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
|
|
|
}
|
|
|
|
|
2015-11-09 17:11:51 +00:00
|
|
|
,_buildBox: function (index)
|
2015-03-15 12:44:57 +00:00
|
|
|
{
|
2016-05-05 15:47:45 +00:00
|
|
|
var set = new Db.SimpleIterator ({
|
2015-11-09 08:14:33 +00:00
|
|
|
model: this._model,
|
|
|
|
row: index
|
|
|
|
});
|
|
|
|
|
2016-05-05 15:47:45 +00:00
|
|
|
this._builder.add (this._formId, set);
|
2015-11-09 08:14:33 +00:00
|
|
|
var res = this._builder.load ();
|
2015-11-17 10:34:33 +00:00
|
|
|
res.link ();
|
2015-07-23 15:58:48 +00:00
|
|
|
|
2015-11-09 17:11:51 +00:00
|
|
|
this._childsData.push ({
|
2015-11-09 08:14:33 +00:00
|
|
|
builder: res,
|
2016-05-05 15:47:45 +00:00
|
|
|
set: set
|
2015-07-23 15:58:48 +00:00
|
|
|
});
|
2015-07-03 05:49:45 +00:00
|
|
|
|
|
|
|
if (this._renderer)
|
2016-05-05 15:47:45 +00:00
|
|
|
this._renderer (res, set);
|
2015-11-09 17:11:51 +00:00
|
|
|
|
|
|
|
return res.getMain ();
|
2015-03-15 12:44:57 +00:00
|
|
|
}
|
2015-03-09 08:36:54 +00:00
|
|
|
|
2015-11-09 17:11:51 +00:00
|
|
|
,_onModelChange: function ()
|
2015-03-09 08:36:54 +00:00
|
|
|
{
|
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
|
|
|
|
|
|
|
this.node.removeChild (this._container);
|
|
|
|
Vn.Node.removeChilds (this._container);
|
|
|
|
|
|
|
|
this._freeChildsData ();
|
|
|
|
this._childsData = [];
|
2015-03-09 08:36:54 +00:00
|
|
|
|
2015-03-15 12:44:57 +00:00
|
|
|
switch (this._model.status)
|
2015-03-09 08:36:54 +00:00
|
|
|
{
|
2015-03-15 12:44:57 +00:00
|
|
|
case Db.Model.Status.READY:
|
|
|
|
{
|
|
|
|
for (var i = 0; i < this._model.numRows; i++)
|
2015-11-09 17:11:51 +00:00
|
|
|
this._container.appendChild (this._buildBox (i));
|
2015-03-09 08:36:54 +00:00
|
|
|
|
2015-11-09 17:11:51 +00:00
|
|
|
this._showNoRecordsFound ();
|
2015-03-15 12:44:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Db.Model.Status.LOADING:
|
2015-11-19 13:57:23 +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');
|
|
|
|
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:
|
2016-09-26 09:28:47 +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
|
|
|
|
2015-11-09 17:11:51 +00:00
|
|
|
this.node.appendChild (this._container);
|
2015-07-23 15:58:48 +00:00
|
|
|
this.signalEmit ('change');
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 17:11:51 +00:00
|
|
|
,_showNoRecordsFound: function (count)
|
2015-03-09 08:36:54 +00:00
|
|
|
{
|
2015-11-09 17:11:51 +00:00
|
|
|
if (this._model.numRows === 0)
|
2016-09-26 09:28:47 +00:00
|
|
|
this._showMessage (_('EmptyList'), 'clean');
|
2015-03-15 12:44:57 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 17:11:51 +00:00
|
|
|
,_showMessage: function (message, src)
|
2015-03-15 12:44:57 +00:00
|
|
|
{
|
2016-10-16 14:16:08 +00:00
|
|
|
var div = this.createElement ('div');
|
2015-03-15 12:44:57 +00:00
|
|
|
div.className = 'message';
|
2015-11-09 17:11:51 +00:00
|
|
|
this._container.appendChild (div);
|
2015-03-15 12:44:57 +00:00
|
|
|
|
2015-11-19 13:57:23 +00:00
|
|
|
if (src)
|
|
|
|
{
|
2016-10-16 14:16:08 +00:00
|
|
|
var img = this.createElement ('img');
|
2015-11-19 13:57:23 +00:00
|
|
|
img.alt = '';
|
2016-09-26 09:28:47 +00:00
|
|
|
img.src = 'image/icon/light/'+ src +'.svg';
|
2015-11-19 13:57:23 +00:00
|
|
|
div.appendChild (img);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var spinner = new Htk.Spinner ();
|
|
|
|
spinner.start ();
|
2016-10-16 14:16:08 +00:00
|
|
|
div.appendChild (spinner.node);
|
2015-11-19 13:57:23 +00:00
|
|
|
}
|
2015-03-15 12:44:57 +00:00
|
|
|
|
2016-10-16 14:16:08 +00:00
|
|
|
div.appendChild (this.createTextNode (message));
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 17:11:51 +00:00
|
|
|
,_onRowDelete: function (model, row)
|
2015-03-09 08:36:54 +00:00
|
|
|
{
|
2015-11-09 17:11:51 +00:00
|
|
|
Vn.Node.remove (this._container.childNodes[row]);
|
|
|
|
this._unrefChildData (row);
|
2016-05-05 15:47:45 +00:00
|
|
|
this._childsData.splice (row, 1);
|
|
|
|
|
|
|
|
for (var i = row; i < this._model.numRows; i++)
|
|
|
|
this._childsData[i].set.row = i;
|
|
|
|
|
2015-11-09 17:11:51 +00:00
|
|
|
this._showNoRecordsFound ();
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 17:11:51 +00:00
|
|
|
,_onRowUpdate: function (model, row, columns)
|
2015-03-09 08:36:54 +00:00
|
|
|
{
|
2017-03-30 11:44:53 +00:00
|
|
|
this._childsData[row].set.changed ();
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 17:11:51 +00:00
|
|
|
,_onRowInsert: function (model, row)
|
2015-03-09 08:36:54 +00:00
|
|
|
{
|
2015-11-09 17:11:51 +00:00
|
|
|
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
|
|
|
|
2015-11-09 17:11:51 +00:00
|
|
|
,_freeChildsData: function ()
|
2015-07-28 19:14:26 +00:00
|
|
|
{
|
2015-11-09 17:11:51 +00:00
|
|
|
if (this._childsData)
|
|
|
|
for (var i = 0; i < this._childsData.length; i++)
|
|
|
|
this._unrefChildData (i);
|
|
|
|
}
|
|
|
|
|
|
|
|
,_unrefChildData: function (index)
|
|
|
|
{
|
|
|
|
var childData = this._childsData[index];
|
2016-05-05 15:47:45 +00:00
|
|
|
childData.set.unref ();
|
2015-11-09 17:11:51 +00:00
|
|
|
childData.builder.unref ();
|
2015-07-28 19:14:26 +00:00
|
|
|
}
|
2017-04-10 15:17:56 +00:00
|
|
|
|
|
|
|
,_destroy: function ()
|
2015-07-28 19:14:26 +00:00
|
|
|
{
|
2015-11-09 17:11:51 +00:00
|
|
|
this._freeChildsData ();
|
2015-07-28 19:14:26 +00:00
|
|
|
this.parent ();
|
|
|
|
}
|
2015-03-09 08:36:54 +00:00
|
|
|
});
|
2015-11-09 17:11:51 +00:00
|
|
|
|