hedera-web/web/js/htk/repeater.js

207 lines
3.6 KiB
JavaScript
Raw Normal View History

2015-03-09 08:36:54 +00:00
Htk.Repeater = new Class
({
Extends: Htk.Widget
,Tag: 'htk-repeater'
2015-07-23 15:58:48 +00:00
,Child: 'model'
2015-03-09 08:36:54 +00:00
,Properties:
{
model:
{
type: Db.Model
,set: function (x)
{
this.link ({_model: x},
{
'status-changed': this.onModelChange
,'row-deleted': this.onRowDelete
,'row-updated': this.onRowUpdate
,'row-inserted': this.onRowInsert
});
this.onModelChange ();
}
,get: function ()
{
this._model;
}
}
,formId:
{
type: String
,set: function (x)
{
this._alias = x;
}
,get: function ()
{
this._alias;
}
2015-07-03 05:49:45 +00:00
}
,renderer:
{
type: Function
,set: function (x)
{
this._renderer = x;
}
,get: function ()
{
this._renderer;
}
}
,emptyMessage:
2015-03-15 12:44:57 +00:00
{
type: String
,value: _('NoData')
2015-03-09 08:36:54 +00:00
}
}
,xml: null
,parentBuilder: null
,_alias: 'form'
,initialize: function ()
{
this.createElement ('div');
2015-03-15 12:44:57 +00:00
this.node.className = 'htk-repeater';
2015-03-09 08:36:54 +00:00
}
,loadXml: function (builder, node)
{
this.parent (builder, node);
2015-08-17 18:02:14 +00:00
var template = node.getElementsByTagName ('template')[0];
//var template = node.querySelector ('template:first-of-type');
2015-07-23 15:58:48 +00:00
if (template)
{
this.xml = template.firstElementChild;
this.parentBuilder = builder;
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)
{
return this.node.childNodes[index];
}
2015-07-23 15:58:48 +00:00
,getBuilder: function (index)
{
return this.childsData[index].builder;
}
,getForm: function (index)
{
return this.childsData[index].form;
}
2015-03-15 12:44:57 +00:00
,buildBox: function (index)
{
var builder = new Vn.Builder ();
builder.setParent (this.parentBuilder);
2015-03-15 12:44:57 +00:00
var form = new Db.Form ();
form.model = this._model;
form.row = index;
builder.add (this._alias, form);
2015-07-23 15:58:48 +00:00
this.childsData.push ({
builder: builder,
form: form
2015-07-23 15:58:48 +00:00
});
2015-03-15 12:44:57 +00:00
var mainNode = builder.loadXmlFromNode (this.xml);
2015-07-03 05:49:45 +00:00
if (this._renderer)
2015-07-23 15:58:48 +00:00
this._renderer (builder, form);
2015-03-15 12:44:57 +00:00
this.node.appendChild (mainNode);
}
2015-03-09 08:36:54 +00:00
,onModelChange: function ()
{
if (!this._model || !this.xml)
return;
Vn.Node.removeChilds (this.node);
this.freeChildsData ();
2015-07-23 15:58:48 +00:00
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++)
this.buildBox (i);
2015-03-09 08:36:54 +00:00
2015-03-15 12:44:57 +00:00
this.showNoRecordsFound ();
break;
}
case Db.Model.Status.LOADING:
this.showMessage (_('Loading'), 'loader-black.gif');
break;
case Db.Model.Status.CLEAN:
this.showMessage (this.emptyMessage, 'refresh.svg');
break;
case Db.Model.Status.ERROR:
this.showMessage (_('ErrorLoadingData'), 'error.svg');
break;
2015-03-09 08:36:54 +00:00
}
2015-07-23 15:58:48 +00:00
this.signalEmit ('change');
2015-03-09 08:36:54 +00:00
}
2015-03-15 12:44:57 +00:00
,showNoRecordsFound: function (count)
2015-03-09 08:36:54 +00:00
{
2015-03-15 12:44:57 +00:00
if (this._model.numRows == 0)
this.showMessage (_('EmptyList'), 'clean.svg');
}
,showMessage: function (message, src)
{
var div = document.createElement ('div');
div.className = 'message';
this.node.appendChild (div);
var img = document.createElement ('img');
img.alt = '';
img.src = 'image/'+ src;
div.appendChild (img);
div.appendChild (document.createTextNode (message));
2015-03-09 08:36:54 +00:00
}
2015-03-15 12:44:57 +00:00
,onRowDelete: function (model, row)
2015-03-09 08:36:54 +00:00
{
2015-03-15 12:44:57 +00:00
Vn.Node.remove (this.node.childNodes[row]);
this.showNoRecordsFound ();
2015-03-09 08:36:54 +00:00
}
2015-03-15 12:44:57 +00:00
,onRowUpdate: function (model, row, columns)
2015-03-09 08:36:54 +00:00
{
2015-03-15 12:44:57 +00:00
// this.form[row].signalEmit ('iter-changed');
2015-03-09 08:36:54 +00:00
}
2015-03-15 12:44:57 +00:00
,onRowInsert: function (model, row)
2015-03-09 08:36:54 +00:00
{
2015-03-15 12:44:57 +00:00
this.buildBox (row);
2015-03-09 08:36:54 +00:00
}
,freeChildsData: function ()
{
if (this.childsData)
for (var i = 0; i < this.childsData.length; i++)
{
this.childsData[i].form.unref ();
this.childsData[i].builder.unref ();
}
}
,destroy: function ()
{
this.freeChildsData ();
this.parent ();
}
2015-03-09 08:36:54 +00:00
});