Initial side dependent elements handling, initial multi col/row objects support, lousy bugfix about hanging on stopiing a module

This commit is contained in:
Blue 2019-03-23 15:47:05 +03:00
parent 7f03a0bd75
commit 35fcc5c801
13 changed files with 760 additions and 259 deletions

View File

@ -16,10 +16,15 @@
#include <wSocket/socket.h> #include <wSocket/socket.h>
#include <wContainer/order.h> #include <wContainer/order.h>
namespace U {
class Connector;
};
namespace C { namespace C {
class Controller : public QObject class Controller : public QObject
{ {
Q_OBJECT Q_OBJECT
friend class U::Connector;
public: public:
enum ModelType { enum ModelType {
string, string,

View File

@ -43,6 +43,9 @@ U::Connector::~Connector()
ctrl->unregisterController(); ctrl->unregisterController();
} }
} }
disconnect(server, SIGNAL(newConnection(const W::Socket&)), this, SLOT(onNewConnection(const W::Socket&)));
disconnect(server, SIGNAL(closedConnection(const W::Socket&)), this, SLOT(onClosedConnection(const W::Socket&)));
} }
void U::Connector::addIgnoredNode(const W::String& name) void U::Connector::addIgnoredNode(const W::String& name)
@ -115,7 +118,7 @@ void U::Connector::onClosedConnection(const W::Socket& socket)
std::multimap<W::String, C::Controller*>::const_iterator end = controllers.upper_bound(name); std::multimap<W::String, C::Controller*>::const_iterator end = controllers.upper_bound(name);
for (; beg != end; ++beg) { for (; beg != end; ++beg) {
beg->second->unsubscribe(); beg->second->dropSubscribed();
beg->second->unregisterController(); beg->second->unregisterController();
} }

View File

@ -110,6 +110,10 @@ W::Socket * W::Server::createSocket(QWebSocket* socket)
} }
Socket *wSocket = new Socket(name, socket, connectionId, this); Socket *wSocket = new Socket(name, socket, connectionId, this);
if (connections.find(connectionId) != connections.end()) {
throw new SocketIdDuplectation(connectionId);
}
connections[connectionId] = wSocket; connections[connectionId] = wSocket;
connect(wSocket, SIGNAL(connected()), SLOT(onSocketConnected())); connect(wSocket, SIGNAL(connected()), SLOT(onSocketConnected()));
connect(wSocket, SIGNAL(disconnected()), SLOT(onSocketDisconnected())); connect(wSocket, SIGNAL(disconnected()), SLOT(onSocketDisconnected()));

View File

@ -73,6 +73,16 @@ namespace W
std::string getMessage() const{return "An attempt to access non existing socket";} std::string getMessage() const{return "An attempt to access non existing socket";}
}; };
class SocketIdDuplectation:
public Utils::Exception
{
public:
SocketIdDuplectation(uint64_t p_id):Exception(),id(p_id){}
uint64_t id;
std::string getMessage() const{return "An attempt to create a socket with duplicating id == " + std::to_string(id);}
};
}; };
} }

View File

@ -106,9 +106,11 @@ void W::Socket::onSocketConnected()
void W::Socket::onSocketDisconnected() void W::Socket::onSocketDisconnected()
{ {
if (state != disconnected_s) {
state = disconnected_s; state = disconnected_s;
emit disconnected(); emit disconnected();
} }
}
void W::Socket::onBinaryMessageReceived(const QByteArray& ba) void W::Socket::onBinaryMessageReceived(const QByteArray& ba)
{ {

View File

@ -1,7 +1,6 @@
"use strict"; "use strict";
global.W = { function extend () {
extend: function () {
var lTarget = arguments[0] || {}; var lTarget = arguments[0] || {};
var lIndex = 1; var lIndex = 1;
var lLength = arguments.length; var lLength = arguments.length;
@ -18,7 +17,7 @@ global.W = {
// Handle case when target is a string or something (possible in deep // Handle case when target is a string or something (possible in deep
// copy) // copy)
if (typeof lTarget !== "object" && typeof lTarget != "function") { if (typeof lTarget !== "object" && lTarget instanceof Function) {
lTarget = {}; lTarget = {};
} }
@ -41,17 +40,17 @@ global.W = {
} }
// Recurse if we're merging plain objects or arrays // Recurse if we're merging plain objects or arrays
if (lDeep && lCopy && (Object.isObject(lCopy) || (lCopyIsArray = Array.isArray(lCopy)))) { if (lDeep && lCopy && (typeof lCopy === "object" || (lCopyIsArray = lCopy instanceof Array))) {
if (lCopyIsArray) { if (lCopyIsArray) {
lCopyIsArray = false; lCopyIsArray = false;
lClone = lSrc && Array.isArray(lSrc) ? lSrc : []; lClone = lSrc && lSrc instanceof Array ? lSrc : [];
} else { } else {
lClone = lSrc && Object.isObject(lSrc) ? lSrc : {}; lClone = lSrc && typeof lCopy === "object" ? lSrc : {};
} }
// Never move original objects, clone them // Never move original objects, clone them
lTarget[lName] = Object.extend(lDeep, lClone, lCopy); lTarget[lName] = extend(lDeep, lClone, lCopy);
// Don't bring in undefined values // Don't bring in undefined values
} else { } else {
@ -65,7 +64,10 @@ global.W = {
// Return the modified object // Return the modified object
return lTarget; return lTarget;
}, }
global.W = {
extend: extend,
touchToMouse: function (e) { touchToMouse: function (e) {
e.preventDefault(); e.preventDefault();
if (e.touches.length > 1 || (e.type == "touchend" && e.touches.length > 0)) if (e.touches.length > 1 || (e.type == "touchend" && e.touches.length > 0))

View File

@ -19,13 +19,19 @@
W.extend(base, options); W.extend(base, options);
Layout.fn.constructor.call(this, controller, base); Layout.fn.constructor.call(this, controller, base);
this._rh = [];
this._rH = [];
this._rw = [];
this._rW = [];
this._lay = [[[]]]; this._lay = [[[]]];
this._cols = [{}]; this._cols = [{}];
this._rows = [{}]; this._rows = [{}];
this._sde = []; //size dependent elements
}, },
"append": function(child, row, col, rowSpan, colSpan, aligment) { "append": function(child, row, col, rowSpan, colSpan, aligment) {
aligment = aligment || 5; aligment = aligment || 5;
this._addChild(child, aligment); this._addChild(child, aligment);
child.on("sizeDependencyChange", this._onChildSizeDependencyChange.bind(this, child));
rowSpan = rowSpan || 1; rowSpan = rowSpan || 1;
colSpan = colSpan || 1; colSpan = colSpan || 1;
@ -44,10 +50,17 @@
} }
var obj = { var obj = {
child: child, child: child,
col: col,
row: row,
colspan: colSpan, colspan: colSpan,
rowspan: rowSpan, rowspan: rowSpan,
a: aligment a: aligment
} }
var item = this._c[this._c.length - 1];
item.col = col;
item.row = row;
item.colspan = colSpan;
item.rowspan = rowSpan;
for (i = row; i < tRow; ++i) { for (i = row; i < tRow; ++i) {
for (var j = col; j < tCol; ++j) { for (var j = col; j < tCol; ++j) {
@ -55,6 +68,17 @@
} }
} }
if (child._o.sizeDependency !== Layout.SizeDependency.straight.independent) {
this._sde.push(obj);
if (this._o.sizeDependency === Layout.SizeDependency.straight.independent) {
this.setSizeDependency(child._o.sizeDependency)
} else {
if (this._o.sizeDependency !== child._o.sizeDependency) {
this.setSizeDependency(Layout.SizeDependency.straight.undefined)
}
}
}
this._recountLimits(); this._recountLimits();
if (this._w !== undefined && this._h !== undefined) { if (this._w !== undefined && this._h !== undefined) {
this.refreshLay(); this.refreshLay();
@ -94,9 +118,14 @@
"clear": function() { "clear": function() {
Layout.fn.clear.call(this); Layout.fn.clear.call(this);
this._rh = [];
this._rH = [];
this._rw = [];
this._rW = [];
this._lay = [[[]]]; this._lay = [[[]]];
this._cols = [{}]; this._cols = [{}];
this._rows = [{}]; this._rows = [{}];
this._sde = [];
}, },
"_onChildChangeLimits": function() { "_onChildChangeLimits": function() {
var notification = this._recountLimits(); var notification = this._recountLimits();
@ -176,6 +205,10 @@
} }
}, },
"_recountLimits": function() { "_recountLimits": function() {
this._rh = [];
this._rH = [];
this._rw = [];
this._rW = [];
this._cols = []; this._cols = [];
this._rows = []; this._rows = [];
var i, j; var i, j;
@ -185,35 +218,38 @@
for (i = 0; i < this._lay.length; ++i) { for (i = 0; i < this._lay.length; ++i) {
while (!this._rows[i]) { while (!this._rows[i]) {
this._rows.push({}); this._rows.push({
count: 0,
min: 0,
max: Infinity
});
} }
for (j = 0; j < this._lay[i].length; ++j) { for (j = 0; j < this._lay[i].length; ++j) {
while (!this._cols[j]) { while (!this._cols[j]) {
this._cols.push({}); this._cols.push({
count: 0,
min: 0,
max: Infinity
});
} }
for (var k = 0; k < this._lay[i][j].length; ++k) { for (var k = 0; k < this._lay[i][j].length; ++k) {
var e = this._lay[i][j][k]; var e = this._lay[i][j][k];
++this._cols[j].count;
++this._rows[i].count;
if (proccessed[e.child._id]) { if (proccessed[e.child._id]) {
this._cols[j].min = this._cols[j].min || 0;
this._rows[i].min = this._rows[i].min || 0;
this._cols[j].max = this._cols[j].max || 0;
this._rows[i].max = this._rows[i].max || 0;
continue; continue;
} }
var colMinW = this._cols[j].min || 0; var colMinW = this._cols[j].min;
var rowMinH = this._rows[i].min || 0; var rowMinH = this._rows[i].min;
var colMaxW = this._cols[j].max || Infinity; var colMaxW = this._cols[j].max;
var rowMaxH = this._rows[i].max || Infinity; var rowMaxH = this._rows[i].max;
if (e.colspan === 1) { if (e.colspan === 1) {
this._cols[j].min = Math.max(colMinW, e.child._o.minWidth); this._cols[j].min = Math.max(colMinW, e.child._o.minWidth);
this._cols[j].max = Math.min(colMaxW, e.child._o.maxWidth); this._cols[j].max = Math.min(colMaxW, e.child._o.maxWidth);
} else { } else {
this._cols[j].min = colMinW;
this._cols[j].max = colMaxW;
multiCols.push({ multiCols.push({
p: j, p: j,
e: e e: e
@ -223,9 +259,6 @@
this._rows[i].min = Math.max(rowMinH, e.child._o.minHeight); this._rows[i].min = Math.max(rowMinH, e.child._o.minHeight);
this._rows[i].max = Math.min(rowMaxH, e.child._o.maxHeight); this._rows[i].max = Math.min(rowMaxH, e.child._o.maxHeight);
} else { } else {
this._rows[i].min = rowMinH;
this._rows[i].max = rowMaxH;
multiRows.push({ multiRows.push({
p: i, p: i,
e: e e: e
@ -234,148 +267,159 @@
proccessed[e.child._id] = true; proccessed[e.child._id] = true;
} }
if (!this._lay[i][j].length) {
this._cols[j].min = this._cols[j].min || 0;
this._rows[i].min = this._rows[i].min || 0;
this._cols[j].max = this._cols[j].max || 0;
this._rows[i].max = this._rows[i].max || 0;
}
}
}
for (i = 0; i < multiCols.length; ++i) {
var e = multiCols[i].e;
var pos = multiCols[i].p;
var span = e.colspan;
var target = pos + span;
var minSize = 0;
var maxSize = 0;
var flexibleColls = [];
for (j = pos; j < target; ++j) {
minSize += this._cols[j].min;
maxSize += this._cols[j].max;
if (this._cols[j].min < this._cols[j].max) {
flexibleColls.push(this._cols[j]);
} }
} }
var leftMin = e.child._o.minWidth - minSize; multiLimits(this._cols, multiCols, true, this._rw, this._rW);
if (leftMin > 0) { multiLimits(this._rows, multiRows, false, this._rh, this._rH);
while (leftMin > 0 && flexibleColls.length > 0) {
var portion = leftMin / flexibleColls.length;
for (j = 0; j < flexibleColls.length; ++j) { for (i = 0; i < this._sde.length; ++i) {
var lPortion = Math.min(flexibleColls[j].max, portion); var dep = this._sde[i];
flexibleColls[j].min += lPortion; var cols = [];
leftMin -= lPortion; var rows = [];
if (flexibleColls[j].min === flexibleColls[j].max) { var tdwMax = 0;
flexibleColls.splice(j, 1); var tdwMin = 0;
var tdhMax = 0;
var tdhMin = 0;
for (var c = 0; c < dep.colspan; ++c) {
var col = this._cols[dep.col + c];
tdwMax += col.max;
tdwMin += col.min;
if (col.min < col.max) {
cols.push(col);
}
}
for (var r = 0; r < dep.rowspan; ++r) {
var row = this._rows[dep.row + r];
tdhMax += row.max;
tdhMin += row.min;
if (row.min < row.max) {
rows.push(row);
}
}
var cMaxWidth, cMinWidth, cMaxHeight, cMinHeight;
var opts = dep.child._o;
var cmw = true;
var cMw = true;
var cmh = true;
var cMh = true;
while (cols.length || rows.length) {
switch (opts.sizeDependency) {
case Layout.SizeDependency.straight.straight:
if (cMh) {
cMaxWidth = dep.child.getMaxWidthForHeight(tdhMax);
cMh = false;
}
if (cmh) {
cMinWidth = dep.child.getMinWidthForHeight(tdhMin);
cmh = false;
}
if (cMw) {
cMaxHeight = dep.child.getMaxHeightForWidth(tdwMax);
cMw = false;
}
if (cmw) {
cMinHeight = dep.child.getMinHeightForWidth(tdwMin);
cmw = false;
}
break;
case Layout.SizeDependency.straight.reversed:
if (cMh) {
cMinWidth = dep.child.getMinWidthForHeight(tdhMax);
cMh = false;
}
if (cmh) {
cMaxWidth = dep.child.getMaxWidthForHeight(tdhMin);
cmh = false;
}
if (cMw) {
cMinHeight = dep.child.getMinHeightForWidth(tdwMax);
cMw = false;
}
if (cmw) {
cMaxHeight = dep.child.getMaxHeightForWidth(tdwMin);
cmw = false;
}
break;
}
var changed = false;
if (!changed && cMinWidth > tdwMin) {
var wDiff = cMinWidth - tdwMin;
var change = wDiff - adjust(wDiff, cols, true);
if (change > 0) {
changed = true;
cmw = true;
tdwMin += change;
}
}
if (!changed && cMinHeight > tdhMin) {
var hDiff = cMaxHeight - tdhMin;
var change = hDiff - adjust(hDiff, rows, true);
if (change > 0) {
changed = true;
cmh = true;
tdhMin += change;
}
}
if (!changed && cMaxWidth < tdwMax) {
var wDiff = tdwMax - cMaxWidth;
if (wDiff < Infinity) {
var change = wDiff - adjust(wDiff, cols, false);
if (change > 0) {
changed = true;
cMw = true;
tdwMax -= change;
}
} else {
constrain(cMaxWidth, cols, false);
changed = true;
cMw = true;
tdwMax = cMaxWidth;
}
}
if (!changed && cMaxHeight < tdhMax) {
var hDiff = tdhMax - cMaxHeight;
if (hDiff < Infinity) {
var change = hDiff - adjust(hDiff, rows, false);
if (change > 0) {
changed = true;
cMh = true;
tdhMax -= change;
}
} else {
constrain(cMaxHeight, rows, false);
changed = true;
cMh = true;
tdhMax = cMaxHeight;
}
}
if (!changed) {
break; break;
} }
} }
if (leftMin < 1) {
leftMin = 0;
}
}
}
var leftMax = maxSize - e.child._o.maxWidth
if (leftMax > 0) {
while (leftMax > 0 && flexibleColls.length > 0) {
var portion = leftMax / flexibleColls.length;
for (j = 0; j < flexibleColls.length; ++j) {
var lPortion = Math.max(flexibleColls[j].min, portion);
flexibleColls[j].max -= lPortion;
leftMax -= lPortion;
if (flexibleColls[j].min === flexibleColls[j].max) {
flexibleColls.splice(j, 1);
break;
}
} }
if (leftMax < 1) { var tw = total(this._cols, this._rw, this._rW);
leftMax = 0; var th = total(this._rows, this._rh, this._rH);
}
} this._storedCols = [];
} this._storedRows = [];
if (this._o.sizeDependency !== Layout.SizeDependency.straight.independent) {
W.extend(true, this._storedCols, this._cols);
W.extend(true, this._storedRows, this._rows);
} }
for (i = 0; i < multiRows.length; ++i) { return this._setLimits(tw.min, th.min, tw.max, th.max);
var e = multiRows[i].e;
var pos = multiRows[i].p;
var span = e.rowspan;
var target = pos + span;
var minSize = 0;
var maxSize = 0;
var flexibleRows = [];
for (j = pos; j < target; ++j) {
minSize += this._rows[j].min;
maxSize += this._rows[j].max;
if (this._rows[j].min < this._rows[j].max) {
flexibleRows.push(this._rows[j]);
}
}
var leftMin = e.child._o.minHeigh - minSize;
if (leftMin > 0) {
while (leftMin > 0 && flexibleRows.length > 0) {
var portion = leftMin / flexibleRows.length;
for (j = 0; j < flexibleRows.length; ++j) {
var lPortion = Math.min(flexibleRows[j].max, portion);
flexibleRows[j].min += lPortion;
leftMin -= lPortion;
if (flexibleRows[j].min === flexibleRows[j].max) {
flexibleRows.splice(j, 1);
break;
}
}
if (leftMin < 1) {
leftMin = 0;
}
}
}
var leftMax = maxSize - e.child._o.maxHeigh
if (leftMax > 0) {
while (leftMax > 0 && flexibleRows.length > 0) {
var portion = leftMax / flexibleRows.length;
for (j = 0; j < flexibleRows.length; ++j) {
var lPortion = Math.max(flexibleRows[j].min, portion);
flexibleRows[j].max -= lPortion;
leftMax -= lPortion;
if (flexibleRows[j].min === flexibleRows[j].max) {
flexibleRows.splice(j, 1);
break;
}
}
if (leftMax < 1) {
leftMax = 0;
}
}
}
}
var minWidth = 0;
var minHeight = 0;
var maxWidth = 0;
var maxHeight = 0;
for (i = 0; i < this._rows.length; ++i) {
minHeight += this._rows[i].min;
this._rows[i].max = Math.max(this._rows[i].max, this._rows[i].min);
maxHeight += this._rows[i].max;
}
for (i = 0; i < this._cols.length; ++i) {
minWidth += this._cols[i].min;
this._cols[i].max = Math.max(this._cols[i].max, this._cols[i].min);
maxWidth += this._cols[i].max;
}
return this._setLimits(minWidth, minHeight, maxWidth, maxHeight);
}, },
"refreshLay": function() { "refreshLay": function() {
if (this._o.sizeDependency !== Layout.SizeDependency.straight.independent) {
this._cols = [];
this._rows = [];
W.extend(true, this._cols, this._storedCols);
W.extend(true, this._rows, this._storedRows);
}
var totalMaxW = 0; var totalMaxW = 0;
var totalMaxH = 0; var totalMaxH = 0;
var totalMinW = 0; var totalMinW = 0;
@ -386,10 +430,6 @@
totalMaxW += this._cols[i].max; totalMaxW += this._cols[i].max;
totalMinW += this._cols[i].min totalMinW += this._cols[i].min
} }
for (i = 0; i < this._rows.length; ++i) {
totalMaxH += this._rows[i].max;
totalMinH += this._rows[i].min;
}
if (this._w <= totalMinW || this._w >= totalMaxW) { if (this._w <= totalMinW || this._w >= totalMaxW) {
var kW; var kW;
@ -406,7 +446,42 @@
this._cols[i].cur = this._cols[i][keyW] * kW; this._cols[i].cur = this._cols[i][keyW] * kW;
} }
} else { } else {
distribute(this._w, this._cols); distribute(this._w, this._cols, this._rw, this._rW);
}
for (i = 0; i < this._sde.length; ++i) {
var dep = this._sde[i];
var rows = [];
var tdw = 0;
var tdhMax = 0;
var tdhMin = 0;
for (var c = 0; c < dep.colspan; ++c) {
tdw += this._cols[dep.col + c].cur;
}
for (var r = 0; r < dep.rowspan; ++r) {
var row = this._rows[dep.row + r];
tdhMax += row.max;
tdhMin += row.min;
if (row.min < row.max) {
rows.push(row);
}
}
if (rows.length > 0) {
var cMaxHeight = dep.child.getMaxHeightForWidth(tdw);
var cMinHeight = dep.child.getMinHeightForWidth(tdw);
if (cMaxHeight < tdhMax) {
adjust(tdhMax - cMaxHeight, rows, false);
}
if (cMinHeight > tdhMin) {
adjust(cMinHeight - cMinHeight, rows, true);
}
}
}
for (i = 0; i < this._rows.length; ++i) {
totalMaxH += this._rows[i].max;
totalMinH += this._rows[i].min;
} }
if (this._h <= totalMinH || this._h >= totalMaxH) { if (this._h <= totalMinH || this._h >= totalMaxH) {
@ -424,26 +499,43 @@
this._rows[i].cur = this._rows[i][keyH] * kH; this._rows[i].cur = this._rows[i][keyH] * kH;
} }
} else { } else {
distribute(this._h, this._rows); distribute(this._h, this._rows, this._rh, this._rH);
} }
this._positionElements(); this._positionElements();
}, },
"removeChild": function(child) { "removeChild": function(child) {
Layout.fn.removeChild.call(this, child); var obj = Layout.fn.removeChild.call(this, child);
var found = obj !== undefined;
var layObject;
for (var i = 0; i < this._lay.length; ++i) { if (found) {
for (var j = 0; j < this._lay[i].length; ++j) { child.off("sizeDependencyChange") //assuming a child is supposed to have only one parent that should be okay
for (var k = 0; k < this._lay[i][j].length; ++k) { var cs = obj.colspan;
if (child === this._lay[i][j][k].child) { var rs = obj.rowspan;
this._lay[i][j].splice(k, 1); var row = obj.row;
var col = obj.col;
for (var i = 0; i < rs; ++ i) {
for (var j = 0; j < cs; ++j) {
var cell = this._lay[row + i][col + j];
for (var k = 0; k < cell.length; ++k) {
if (cell[k].child === child) {
layObject = cell[k];
cell.splice(k, 1);
} }
} }
} }
} }
var di = this._sde.indexOf(layObject);
if (di !== -1) {
this._sde.splice(di, 1);
this._recalculateSizeDependency(); //TODO need to recalculate the grid, but not sure about the order of triggers.
}
this._cleanupLay(); this._cleanupLay();
this._recountLimits(); this._recountLimits();
this.refreshLay(); this.refreshLay();
}
}, },
"setSize": function(w, h) { "setSize": function(w, h) {
View.fn.setSize.call(this, w, h); View.fn.setSize.call(this, w, h);
@ -451,11 +543,40 @@
if (this._c.length) { if (this._c.length) {
this.refreshLay(); this.refreshLay();
} }
},
"_recalculateSizeDependency": function() {
if (this._sde.length === 0) {
this.setSizeDependency(Layout.SizeDependency.straight.independent);
} else {
var sd = this._sde[0]._o.sizeDependency;
for (var d = 0; d < this._sde.length; ++d) {
var csd = this._sde[d]._o.sizeDependency
if (sd !== csd) {
sd = Layout.SizeDependency.straight.undefined;
}
if (sd === Layout.SizeDependency.straight.undefined) {
break;
}
}
if (sd !== this._o.sizeDependency) {
this.setSizeDependency(sd);
}
}
},
"_onChildSizeDependencyChange": function(child, d) {
if (d === Layout.SizeDependency.straight.independent) {
var index = this._getChildObjectIndex(child);
var obj = this._c[index]; //I assume it presents among children
var di = this._sde.indexOf(obj);
this._sde.splice(di, 1);
}
this._recalculateSizeDependency(); //TODO need to recalculate the grid, but not sure about the order of triggers.
} }
}); });
function distribute (size, array) { function distribute (size, array, rm, rM) {
var i, portion; var i;
var cMax = []; var cMax = [];
for (i = 0; i < array.length; ++i) { for (i = 0; i < array.length; ++i) {
array[i].cur = array[i].min; array[i].cur = array[i].min;
@ -465,30 +586,245 @@
cMax.push(array[i]); cMax.push(array[i]);
} }
} }
cMax.sort(GridLayout._candidatesSortMax);
for (i = 0; i < rm.length && size > 1; ++i) {
var rule = rm[i];
var trm = 0;
var mems = [];
for (var j = 0; j < rule.el.length; ++j) {
var mem = array[rule.el[j]];
trm += mem.cur;
mems.push(mem);
}
var c = rule.val - trm;
if (c > 1) {
size -= c;
mems.sort(GridLayout._candidatesSortMax);
c -= deliver(mems, c);
if (c > 1) {
var p = c / rule.el.length;
for (j = 0; j < rule.el.length; ++j) {
array[rule.el[j]].cur += p;
}
}
}
}
cMax.sort(GridLayout._candidatesSortMax);
size -= deliver(cMax, size);
for (i = 0; i < rM.length; ++i) {
var rule = rm[i];
var trm = 0;
var mems = [];
for (var j = 0; j < rule.el.length; ++j) {
var mem = array[rule.el[j]];
trm += mem.cur;
mems.push(mem);
}
var c = trm - rule.val;
if (c > 1) {
size += c;
var p = c / mems.length;
for (j = 0; j < mems.length; ++j) {
mems.cur -= p;
}
}
}
if (size) {
var portion = size / array.length;
for (i = 0; i < array.length; ++i) {
array.cur += portion;
}
}
}
function deliver(cMax, size) {
var oSize = size;
while (cMax.length && size) { while (cMax.length && size) {
portion = size / cMax.length; var portion = size / cMax.length;
var last = cMax[cMax.length -1]; var last = cMax[cMax.length -1];
if (portion >= last.max) { var avail = last.max - last.cur;
size -= last.max - last.cur; if (portion >= avail) {
size -= avail;
last.cur = last.max; last.cur = last.max;
cMax.pop(); cMax.pop();
} else { } else {
for (i = 0; i < cMax.length; ++i) { for (var i = 0; i < cMax.length; ++i) {
cMax[i].cur += portion; cMax[i].cur += portion;
} }
size = 0; size = 0;
} }
} }
if (size) { return oSize - size;
portion = size / array.length; }
for (i = 0; i < array.length; ++i) {
array.cur += portion; function multiLimits(arr, elements, w, rm, rM) {
var mind = "minWidth";
var maxd = "maxWidth";
var spand = "colspan";
if (!w) {
mind = "minHeight";
maxd = "maxHeight";
spand = "rowspan";
}
for (var i = 0; i < elements.length; ++i) {
var e = elements[i].e;
var pos = elements[i].p;
var span = e[spand];
var target = pos + span;
var minSize = 0;
var maxSize = 0;
var flexible = [];
var el = [];
for (var j = pos; j < target; ++j) {
minSize += arr[j].min;
maxSize += arr[j].max;
el.push(j);
if (arr[j].min < arr[j].max) {
flexible.push(arr[j]);
} }
} }
var leftMin = e.child._o[mind] - minSize;
var leftMax = maxSize - e.child._o[maxd];
var naMin = adjust(leftMin, flexible, true);
var naMax = adjust(leftMax, flexible, false);
if (naMin > 0) {
rm.push({
el: el,
val: e.child._o[mind]
});
}
if (naMax > 0) {
rM.push({
el: el,
val: e.child._o[maxd]
});
}
}
}
function adjust(space, arr, isMin) {
while (space > 0 && arr.length > 0) {
var portion = space / arr.length;
for (var j = 0; j < arr.length; ++j) {
var mem = arr[j];
var lPortion = Math.min(mem.max - mem.min, portion);
if (isMin === true) {
mem.min += lPortion;
} else {
mem.max -= lPortion;
}
space -= lPortion;
if (mem.min === mem.max) {
arr.splice(j, 1);
break;
}
}
if (space < 1) {
space = 0;
}
}
if (space < 0) {
space = 0;
}
return space;
}
function constrain(space, arr, isMin) {
while (space > 0 && arr.length > 0) {
var portion = space / arr.length;
for (var j = 0; j < arr.length; ++j) {
var mem = arr[j];
var lPortion
if (isMin === true) {
lPortion = Math.min(mem.max, portion);
mem.min = lPortion;
} else {
lPortion = Math.max(mem.min, portion);
mem.max = lPortion;
}
space -= lPortion;
if (mem.min === mem.max) {
arr.splice(j, 1);
break;
}
}
if (space < 1) {
space = 0;
}
}
}
function total(arr, rm, rM) {
var min = 0;
var max = 0;
var am = [];
for (var i = 0; i < arr.length; ++i) {
var el = arr[i];
if (el.count === 0) {
el.min = 0;
el.max = 0;
} else {
min += el.min;
el.max = Math.max(el.max, el.min);
max += el.max;
}
am.push({
min: el.min,
max: el.max
});
}
min += shove(rm, am, true);
max -= shove(rM, am, false);
return {
min: min,
max: Math.max(max, min)
}
}
function shove(rules, arr, isMin) {
var dim = "max";
if (isMin === true) {
dim = "min";
}
var change = 0;
for (var i = 0; i < rules.length; ++i) {
var rule = rules[i];
var val = rule.val;
var tr = 0;
for (var j = 0; j < rule.el.length; ++j) {
tr += arr[rule.el[j]][dim];
}
var c;
if (isMin) {
c = val - tr;
} else {
c = tr - val;
}
if (c > 0) {
var p = c / rule.el.length;
for (j = 0; j < rule.el.length; ++j) {
if (isMin) {
arr[rule.el[j]][dim] += p;
} else {
arr[rule.el[j]][dim] -= p; //TODO a possibility of going under zero
}
}
change += c;
}
}
return change;
} }
GridLayout._candidatesSortMax = function(a, b) { GridLayout._candidatesSortMax = function(a, b) {

View File

@ -9,27 +9,96 @@
var View = require("views/view"); var View = require("views/view");
var Image = View.inherit({ var Image = View.inherit({
"className": "Image", className: "Image",
"constructor": function(controller, options) { constructor: function(controller, options) {
var base = {}; var base = {
preserveAspectRatio: false,
aspectRatio: 1 // width / height
};
W.extend(base, options) W.extend(base, options)
if (base.preserveAspectRatio) {
base.sizeDependency = View.SizeDependency.straight.straight;
if (base.maxWidth || base.maxHeight) {
var max = closestSize(base.maxWidth || Infinity, base.maxHeight || Infinity, base.aspectRatio); //TODO really not sure about that
base.maxWidth = max.w;
base.maxHeight = max.h;
}
if (base.minWidth || base.minHeight) {
var min = closestSize(base.minWidth || 0, base.minHeight || 0, base.aspectRatio);
base.minWidth = min.w;
base.minHeight = min.h;
}
}
var el = document.createElement("img"); var el = document.createElement("img");
View.fn.constructor.call(this, controller, base, el); View.fn.constructor.call(this, controller, base, el);
controller.needData(); controller.needData();
}, },
"destructor": function() { destructor: function() {
this._f.dontNeedData(); this._f.dontNeedData();
View.fn.destructor.call(this); View.fn.destructor.call(this);
}, },
"_onData": function() { _onData: function() {
if (this._f.hasData()) { if (this._f.hasData()) {
this._e.src = "data:" + this._f.getMimeType() + ";base64," + this._f.data.base64(); this._e.src = "data:" + this._f.getMimeType() + ";base64," + this._f.data.base64(); //TODO figure out if that could be done with URLObject
}
},
_closestSize: function(w, h) {
if (this._o.preserveAspectRatio) {
return closestSize(w, h, this._o.aspectRatio);
} else {
return View.fn._closestSize.call(this, w, h);
}
},
"getMinWidthForHeight": function(h) {
if (this._o.preserveAspectRatio) {
return this.constrainWidth(h * this._o.aspectRatio);
} else {
return this._o.minWidth;
}
},
"getMaxWidthForHeight": function(h) {
if (this._o.preserveAspectRatio) {
return this.constrainWidth(h * this._o.aspectRatio);
} else {
return this._o.maxWidth;
}
},
"getMinHeightForWidth": function(w) {
if (this._o.preserveAspectRatio) {
return this.constrainHeight(w / this._o.aspectRatio);
} else {
return this._o.minHeight;
}
},
"getMaxHeightForWidth": function(w) {
if (this._o.preserveAspectRatio) {
return this.constrainHeight(w / this._o.aspectRatio);
} else {
return this._o.maxHeight;
} }
} }
}); });
function closestSize(w, h, ratio) {
var nh = w / ratio;
var nw = h * ratio;
var dh = Math.abs(h - nh);
var dw = Math.abs(w - nw);
if (dh > dw) {
return {
w: nw,
h: h
}
} else {
return {
w: w,
h: nh
}
}
}
return Image; return Image;
}) })
})(); })();

View File

@ -172,14 +172,26 @@
} }
}, },
"removeChild": function(child) { "removeChild": function(child) {
var i = this._getChildObjectIndex(child);
if (i !== -1) {
var obj = this._c[i];
this._removeChildByIndex(i);
return obj;
}
},
"_getChildObjectIndex": function(child) {
var obj;
var i; var i;
for (i = 0; i < this._c.length; ++i) { for (i = 0; i < this._c.length; ++i) {
if (this._c[i].c === child) { obj = this._c[i];
if (obj.c === child) {
break; break;
} }
} }
if (i !== this._c.length) { if (obj !== undefined) {
this._removeChildByIndex(i); return i;
} else {
return -1;
} }
}, },
"_removeChildByIndex": function(i) { "_removeChildByIndex": function(i) {

View File

@ -107,7 +107,9 @@
case ItemType.straight.picture: case ItemType.straight.picture:
this._picture = new Image(ctrl, { this._picture = new Image(ctrl, {
maxWidth: 100, maxWidth: 100,
maxHeight: 100 maxHeight: 100,
preserveAspectRatio: true,
aspectRatio: 1
}); });
this.append(this._picture, 0, 0, 3, 1); this.append(this._picture, 0, 0, 3, 1);
break; break;

View File

@ -5,11 +5,13 @@
var defineArray = []; var defineArray = [];
defineArray.push("lib/utils/subscribable"); defineArray.push("lib/utils/subscribable");
defineArray.push("views/helpers/draggable"); defineArray.push("views/helpers/draggable");
defineArray.push("lib/utils/enum");
define(moduleName, defineArray, function view_module() { define(moduleName, defineArray, function view_module() {
var counter = 0; var counter = 0;
var Subscribable = require("lib/utils/subscribable"); var Subscribable = require("lib/utils/subscribable");
var Draggable = require("views/helpers/draggable"); var Draggable = require("views/helpers/draggable");
var Enum = require("lib/utils/enum");
var View = Subscribable.inherit({ var View = Subscribable.inherit({
"className": "View", "className": "View",
@ -22,7 +24,8 @@
minHeight: 0, minHeight: 0,
maxWidth: Infinity, maxWidth: Infinity,
maxHeight: Infinity, maxHeight: Infinity,
draggable: false draggable: false,
sizeDependency: SizeDependency.straight.independent
}; };
W.extend(base, options); W.extend(base, options);
@ -86,6 +89,12 @@
this._onAddProperty(prop.k, prop.p); this._onAddProperty(prop.k, prop.p);
} }
}, },
_closestSize: function(w, h) {
return { //this is for cases when dimensions depend on each other, must be implemented in child classes
w: w, //supposed to return closest to given size basing on dependency
h: h
}
},
"constrainHeight": function(h) { "constrainHeight": function(h) {
h = Math.max(h, this._o.minHeight); h = Math.max(h, this._o.minHeight);
h = Math.min(h, this._o.maxHeight); h = Math.min(h, this._o.maxHeight);
@ -203,6 +212,7 @@
return needToTell && this._events.changeLimits && this._events.changeLimits.length; //to see if someone actually going to listen that event, if not - return result return needToTell && this._events.changeLimits && this._events.changeLimits.length; //to see if someone actually going to listen that event, if not - return result
}, },
"setMaxSize": function(w, h) { "setMaxSize": function(w, h) {
if (this._o.maxHeight !== h || this._o.maxWidth !== w) {
this._o.maxWidth = w; this._o.maxWidth = w;
this._o.maxHeight = h; this._o.maxHeight = h;
@ -211,8 +221,10 @@
} }
this.trigger("changeLimits", this); this.trigger("changeLimits", this);
}
}, },
"setMinSize": function(w, h) { "setMinSize": function(w, h) {
if (this._o.minHeight !== h || this._o.minWidth !== w) {
this._o.minWidth = w; this._o.minWidth = w;
this._o.minHeight = h; this._o.minHeight = h;
@ -221,15 +233,27 @@
} }
this.trigger("changeLimits", this); this.trigger("changeLimits", this);
}
}, },
"setSize": function(w, h) { "setSize": function(w, h) {
this._w = this.constrainWidth(w); var nw = this.constrainWidth(w);
this._h = this.constrainHeight(h); var nh = this.constrainHeight(h);
if (this._o.sizeDependency !== SizeDependency.straight.independent) {
var obj = this._closestSize(nw, nh);
nw = obj.w;
nh = obj.h;
}
if (nw !== this._w || nh !== this._h) {
this._w = nw;
this._h = nh;
this._e.style.width = this._w + "px"; this._e.style.width = this._w + "px";
this._e.style.height = this._h + "px"; this._e.style.height = this._h + "px";
this.trigger("resize", this._w, this._h); this.trigger("resize", this._w, this._h);
}
}, },
"setTop": function(t) { "setTop": function(t) {
this._y = t; this._y = t;
@ -237,6 +261,22 @@
}, },
"trySize": function(w, h) { "trySize": function(w, h) {
return !(w < this._o.minWidth || h < this._o.minHeight || w > this._o.maxWidth || h > this._o.maxHeight) return !(w < this._o.minWidth || h < this._o.minHeight || w > this._o.maxWidth || h > this._o.maxHeight)
},
"setSizeDependency": function(d) {
this._o.sizeDependency = d;
this.trigger("sizeDependencyChange", d);
},
"getMinWidthForHeight": function(h) {
return this._o.minWidth;
},
"getMaxWidthForHeight": function(h) {
return this._o.maxWidth;
},
"getMinHeightForWidth": function(w) {
return this._o.minHeight;
},
"getMaxHeightForWidth": function(h) {
return this._o.maxHeight;
} }
}); });
@ -331,6 +371,13 @@
View: View View: View
}; };
var SizeDependency = new Enum("SizeDependency");
SizeDependency.add("independent");
SizeDependency.add("straight");
SizeDependency.add("reversed");
SizeDependency.add("undefined");
View.SizeDependency = SizeDependency;
return View; return View;
}); });

View File

@ -12,6 +12,7 @@ var VCController = require("../lib/wController/vocabulary");
var Address = require("../lib/wType/address"); var Address = require("../lib/wType/address");
var Uint64 = require("../lib/wType/uint64"); var Uint64 = require("../lib/wType/uint64");
var Vocabulary = require("../lib/wType/vocabulary"); var Vocabulary = require("../lib/wType/vocabulary");
var Boolean = require("../lib/wType/boolean");
var AlbumPage = TempPage.inherit({ var AlbumPage = TempPage.inherit({
"className": "AlbumPage", "className": "AlbumPage",
@ -28,6 +29,8 @@ var AlbumPage = TempPage.inherit({
imageOptions.insert("maxWidth", new Uint64(200)); imageOptions.insert("maxWidth", new Uint64(200));
imageOptions.insert("minHeight", new Uint64(200)); imageOptions.insert("minHeight", new Uint64(200));
imageOptions.insert("maxHeight", new Uint64(200)); imageOptions.insert("maxHeight", new Uint64(200));
imageOptions.insert("aspectRatio", new Uint64(1));
imageOptions.insert("preserveAspectRatio", new Boolean(true));
this.addItem(this._image, 0, 0, 2, 1, TempPage.Aligment.CenterCenter, new Uint64(TempPage.getModelTypeId(this._image)), imageOptions); this.addItem(this._image, 0, 0, 2, 1, TempPage.Aligment.CenterCenter, new Uint64(TempPage.getModelTypeId(this._image)), imageOptions);
var header = this._header = new String(this._address["+"](new Address(["header"])), ""); var header = this._header = new String(this._address["+"](new Address(["header"])), "");

View File

@ -8,6 +8,8 @@ var VCController = require("../lib/wController/vocabulary");
var Address = require("../lib/wType/address"); var Address = require("../lib/wType/address");
var Uint64 = require("../lib/wType/uint64"); var Uint64 = require("../lib/wType/uint64");
var Boolean = require("../lib/wType/boolean");
var Vocabulary = require("../lib/wType/vocabulary");
var SongPage = TempPage.inherit({ var SongPage = TempPage.inherit({
"className": "SongPage", "className": "SongPage",
@ -30,8 +32,12 @@ var SongPage = TempPage.inherit({
artist.addProperty("fontFamily", "casualFont"); artist.addProperty("fontFamily", "casualFont");
this.addItem(artist, 2, 1, 1, 1, TempPage.Aligment.CenterTop); this.addItem(artist, 2, 1, 1, 1, TempPage.Aligment.CenterTop);
var io = new Vocabulary();
io.insert("aspectRatio", new Uint64(1));
io.insert("preserveAspectRatio", new Boolean(true));
var image = this._image = new Image(this._address["+"](new Address(["image"])), new Uint64(0)); var image = this._image = new Image(this._address["+"](new Address(["image"])), new Uint64(0));
this.addItem(image, 0, 0, 3, 1, TempPage.Aligment.CenterCenter); this.addItem(image, 0, 0, 3, 1, TempPage.Aligment.CenterCenter, new Uint64(TempPage.getModelTypeId(image)), io);
}, },
"destructor": function() { "destructor": function() {
this._clearCtrls(); this._clearCtrls();