0
1
Fork 0
hedera-web-mindshore/js/htk/repeater/index.js

232 lines
4.4 KiB
JavaScript
Raw Normal View History

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-11-16 01:46:44 +00:00
,set(x) {
2022-05-24 21:11:12 +00:00
this.link({_model: x}, {
'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-11-16 01:46:44 +00:00
,get() {
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-11-16 01:46:44 +00:00
,set(x) {
2015-11-09 08:14:33 +00:00
this._formId = x;
2015-03-09 08:36:54 +00:00
}
2022-11-16 01:46:44 +00:00
,get() {
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-11-16 01:46:44 +00:00
,set(x) {
2015-07-03 05:49:45 +00:00
this._renderer = x;
}
2022-11-16 01:46:44 +00:00
,get() {
2015-07-03 05:49:45 +00:00
this._renderer;
}
}
/**
* Wether to show the model status.
2022-05-24 21:11:12 +00:00
*/
2022-06-18 21:04:34 +00:00
,showStatus: {
type: Boolean
2022-11-16 01:46:44 +00:00
,set(x) {
this._showStatus = x;
this._onModelChange();
}
2022-11-16 01:46:44 +00:00
,get() {
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
,value: null
2015-03-09 08:36:54 +00:00
}
}
2015-11-09 08:14:33 +00:00
,_builder: null
,_formId: 'form'
,_showStatus: true
2015-03-09 08:36:54 +00:00
2022-11-16 01:46:44 +00:00
,render() {
2022-05-21 21:31:56 +00:00
var div = this.createRoot('div');
2022-05-21 21:31:56 +00:00
this._container = this.createElement('div');
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-11-16 01:46:44 +00:00
,loadXml(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-11-16 01:46:44 +00:00
,getChild(index) {
return this._container.childNodes[index];
2015-07-10 12:30:08 +00:00
}
2022-11-16 01:46:44 +00:00
,getBuilder(index) {
2022-06-18 21:04:34 +00:00
return this._childsData[index].scope;
2015-07-23 15:58:48 +00:00
}
2022-11-16 01:46:44 +00:00
,getForm(index) {
return this._childsData[index].set;
2015-07-23 15:58:48 +00:00
}
2022-11-16 01:46:44 +00:00
,_buildBox(index) {
2022-05-21 21:31:56 +00:00
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);
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-11-16 01:46:44 +00:00
,_onModelChange() {
2015-11-09 08:14:33 +00:00
if (!this._model || !this._builder)
2015-03-09 08:36:54 +00:00
return;
2022-05-21 21:31:56 +00:00
this.node.removeChild(this._container);
Vn.Node.removeChilds(this._container);
2022-05-21 21:31:56 +00:00
this._freeChildsData();
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:
{
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;
}
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-11-16 01:46:44 +00:00
,_showNoRecordsFound() {
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-11-16 01:46:44 +00:00
,_showMessage(message, src) {
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-11-16 01:46:44 +00:00
,_onRowDelete(model, row) {
2022-05-21 21:31:56 +00:00
Vn.Node.remove(this._container.childNodes[row]);
this._unrefChildData(row);
this._childsData.splice(row, 1);
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-11-16 01:46:44 +00:00
,_onRowUpdate(model, row) {
2022-05-21 21:31:56 +00:00
this._childsData[row].set.iterChanged();
2015-03-09 08:36:54 +00:00
}
2022-11-16 01:46:44 +00:00
,_onRowInsert(model, row) {
2022-05-21 21:31:56 +00:00
var box = this._buildBox(row);
this._container.appendChild(box);
2015-03-09 08:36:54 +00:00
}
2022-11-16 01:46:44 +00:00
,_freeChildsData() {
if (this._childsData)
for (var i = 0; i < this._childsData.length; i++)
2022-05-21 21:31:56 +00:00
this._unrefChildData(i);
}
2022-11-16 01:46:44 +00:00
,_unrefChildData(index) {
var childData = this._childsData[index];
childData.set._destroy();
childData.scope._destroy();
}
,_destroy() {
2022-05-21 21:31:56 +00:00
this._freeChildsData();
Component.prototype._destroy.call(this);
}
2015-03-09 08:36:54 +00:00
});