radio/libjs/wController/panesList.js

119 lines
4.4 KiB
JavaScript

"use strict";
var List = require("./list");
var ImagePane = require("./imagePane");
var Address = require("../wType/address");
var PanesList = List.inherit({
"className": "PanesList",
"constructor": function PanesListModel(addr) {
List.fn.constructor.call(this, addr);
this.actions = null;
this._subscriptionStart = 0;
this._subscriptionEnd = Infinity;
},
"destructor": function() {
if (this.initialized) {
this.actions.destructor();
}
List.fn.destructor.call(this);
},
"addElement": function(element) {
var size = this.data.length();
List.fn.addElement.call(this, element);
if (size >= this._subscriptionStart && size < this._subscriptionEnd) {
var controller = new ImagePane(this._pairAddress["+"](new Address([element.toString()])));
var size = this.actions.length();
for (var i = 0; i < size; ++i) {
controller.addAction(this.actions.at(i));
}
this.addController(controller);
}
},
"clear": function() {
List.fn.clear.call(this);
this.clearChildren();
},
"_h_get": function(ev) {
this.clear();
var root = ev.getData();
var data = root.at("data");
if (this.initialized) {
this.actions.destructor();
}
this.actions = root.at("actions").clone();
var size = data.length();
for (var i = 0; i < size; ++i) {
this.addElement(data.at(i).clone());
}
this.initialized = true;
this.trigger("data");
},
"setSubscriptionRange": function(s, e) {
var needStart = s !== this._subscriptionStart;
var needEnd = e !== this._subscriptionEnd;
if (needStart || needEnd) {
var os = this._subscriptionStart;
var oe = this._subscriptionEnd;
this._subscriptionStart = s;
this._subscriptionEnd = e;
if (this._subscribed && this.initialized) {
var size = this.actions.length();
this.trigger("rangeStart");
if (needStart) {
if (s > os) {
var limit = Math.min(s - os, this._controllers.length);
for (var i = 0; i < limit; ++i) {
var ctrl = this._controllers[0];
this._removeControllerByIndex(0);
ctrl.destructor();
}
} else {
var limit = Math.min(os, e) - s;
for (var i = 0; i < limit; ++i) {
var ctrl = new ImagePane(this._pairAddress["+"](new Address([this.data.at(i + s).toString()])));
for (var a = 0; a < size; ++a) {
ctrl.addAction(this.actions.at(a));
}
this.addController(ctrl, i);
}
}
}
if (needEnd) {
var ce = Math.min(this.data.length(), e);
var coe = Math.min(this.data.length(), oe);
if (ce > coe) {
var start = Math.max(s, oe);
var amount = ce - start; //it can be negative, it's fine
for (var i = 0; i < amount; ++i) {
var ctrl = new ImagePane(this._pairAddress["+"](new Address([this.data.at(start + i).toString()])));
for (var a = 0; a < size; ++a) {
ctrl.addAction(this.actions.at(a));
}
this.addController(ctrl);
}
} else if (ce < coe) {
var amount = Math.min(coe - ce, coe - os);
for (var i = 0; i < amount; ++i) {
var index = this._controllers.length - 1;
var ctrl = this._controllers[index];
this._removeControllerByIndex(index);
ctrl.destructor();
}
}
}
this.trigger("rangeEnd");
}
}
}
});
module.exports = PanesList;