Browse Source

Merge pull request #141 in FUI/fineui from ~GUY/fineui:master to master

* commit 'dfe5f49660a2e92388ec1d71a976aca9b1453de3':
  update
  update
es6
guy 7 years ago
parent
commit
f459426214
  1. 199
      bi/core.js
  2. 33
      demo/js/core/abstract/demo.virtual_group.js
  3. 199
      dist/bundle.js
  4. 80
      dist/bundle.min.js
  5. 199
      dist/core.js
  6. 33
      dist/demo.js
  7. 199
      src/core/wrapper/layout.js

199
bi/core.js

@ -11527,33 +11527,194 @@ BI.Layout = BI.inherit(BI.Widget, {
}) })
}, },
update: function (opt) { patchItem: function (oldVnode, vnode, index) {
var o = this.options; if (!this._compare(oldVnode, vnode)) {
var items = opt.items || []; return this.updateItemAt(index, vnode);
var updated, i, len; }
for (i = 0, len = Math.min(o.items.length, items.length); i < len; i++) { },
if (!this._compare(o.items[i], items[i])) {
updated = this.updateItemAt(i, items[i]) || updated; updateChildren: function (oldCh, newCh) {
var self = this;
var oldStartIdx = 0, newStartIdx = 0;
var oldEndIdx = oldCh.length - 1;
var oldStartVnode = oldCh[0];
var oldEndVnode = oldCh[oldEndIdx];
var newEndIdx = newCh.length - 1;
var newStartVnode = newCh[0];
var newEndVnode = newCh[newEndIdx];
var oldKeyToIdx;
var idxInOld;
var elmToMove;
var before;
var updated;
var children = {};
BI.each(oldCh, function (i, child) {
child = self._getOptions(child);
var key = child.key == null ? i : child.key;
if (BI.isKey(key)) {
children[key] = self._children[self._getChildName(i)];
}
});
while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx) {
if (BI.isNull(oldStartVnode)) {
oldStartVnode = oldCh[++oldStartIdx];
} else if (BI.isNull(oldEndVnode)) {
oldEndVnode = oldCh[--oldEndIdx];
} else if (sameVnode(oldStartVnode, newStartVnode, oldStartIdx, newStartIdx)) {
updated = this.patchItem(oldStartVnode, newStartVnode, oldStartIdx) || updated;
oldStartVnode = oldCh[++oldStartIdx];
newStartVnode = newCh[++newStartIdx];
} else if (sameVnode(oldEndVnode, newEndVnode, oldEndIdx, newEndIdx)) {
updated = this.patchItem(oldEndVnode, newEndVnode, oldEndIdx) || updated;
oldEndVnode = oldCh[--oldEndIdx];
newEndVnode = newCh[--newEndIdx];
} else if (sameVnode(oldStartVnode, newEndVnode)) {
updated = this.patchItem(oldStartVnode, newEndVnode, oldStartIdx) || updated;
insertBefore(oldStartVnode, oldEndVnode, true);
oldStartVnode = oldCh[++oldStartIdx];
newEndVnode = newCh[--newEndIdx];
} else if (sameVnode(oldEndVnode, newStartVnode)) {
updated = this.patchItem(oldEndVnode, newStartVnode, oldEndIdx) || updated;
insertBefore(oldEndVnode, oldStartVnode);
oldEndVnode = oldCh[--oldEndIdx];
newStartVnode = newCh[++newStartIdx];
} else {
if (oldKeyToIdx === undefined) {
oldKeyToIdx = createKeyToOldIdx(oldCh, oldStartIdx, oldEndIdx);
}
idxInOld = oldKeyToIdx[newStartVnode.key];
if (BI.isNull(idxInOld)) {
var node = addNode(newStartVnode);
insertBefore(node, oldStartVnode);
newStartVnode = newCh[++newStartIdx];
} else {
elmToMove = oldCh[idxInOld];
var node = addNode(newStartVnode);
insertBefore(node, oldStartVnode);
// if (elmToMove.sel !== newStartVnode.sel) {
// api.insertBefore(parentElm, createElm(newStartVnode), oldStartVnode.elm);
// } else {
// updated = this.patchItem(elmToMove, newStartVnode, idxInOld) || updated;
// oldCh[idxInOld] = undefined;
// api.insertBefore(parentElm, (elmToMove.elm), oldStartVnode.elm);
// }
newStartVnode = newCh[++newStartIdx];
} }
} }
if (o.items.length > items.length) {
var deleted = [];
for (i = items.length; i < o.items.length; i++) {
deleted.push(this._children[this._getChildName(i)]);
delete this._children[this._getChildName(i)];
} }
o.items.splice(items.length); if (oldStartIdx > oldEndIdx) {
BI.each(deleted, function (i, w) { before = BI.isNull(newCh[newEndIdx + 1]) ? null : newCh[newEndIdx + 1].elm;
w._destroy(); addVnodes(before, newCh, newStartIdx, newEndIdx);
}) } else if (newStartIdx > newEndIdx) {
} else if (items.length > o.items.length) { removeVnodes(oldCh, oldStartIdx, oldEndIdx);
for (i = o.items.length; i < items.length; i++) { }
this.addItemAt(i, items[i]);
this._children = {};
BI.each(newCh, function (i, child) {
var node = self._getOptions(child);
var key = node.key == null ? i : node.key;
self._children[self._getChildName(i)] = children[key];
});
function sameVnode(vnode1, vnode2, oldIndex, newIndex) {
vnode1 = self._getOptions(vnode1);
vnode2 = self._getOptions(vnode2);
if (BI.isKey(vnode1.key)) {
return vnode1.key === vnode2.key;
}
if (oldIndex >= 0) {
return oldIndex === newIndex
}
}
function addNode(vnode, index) {
var opt = self._getOptions(vnode);
var key = opt.key == null ? i : opt.key;
return children[key] = self._addElement(key, vnode);
}
function addVnodes(before, vnodes, startIdx, endIdx) {
for (; startIdx <= endIdx; ++startIdx) {
var node = addNode(vnodes[startIdx], startIdx);
insertBefore(node, before);
}
}
function removeVnodes(vnodes, startIdx, endIdx) {
for (; startIdx <= endIdx; ++startIdx) {
var node = self._getOptions(vnodes[startIdx]);
var key = node.key == null ? startIdx : node.key;
children[key]._destroy();
} }
} }
function insertBefore(insert, before, isNext) {
insert = self._getOptions(insert);
before = before && self._getOptions(before);
if (BI.isKey(insert.key)) {
if (before && children[before.key]) {
var next;
if (isNext) {
next = children[before.key].element.next();
} else {
next = children[before.key].element;
}
if (next.length > 0) {
next.before(children[insert.key].element);
} else {
self._getWrapper().append(children[insert.key].element);
}
} else {
self._getWrapper().append(children[insert.key].element);
}
} else {
throw "key is not defined";
}
}
function createKeyToOldIdx(children, beginIdx, endIdx) {
var i, map = {}, key;
for (i = beginIdx; i <= endIdx; ++i) {
key = children[i].key;
if (key !== undefined) map[key] = i;
}
return map;
}
return updated; return updated;
}, },
update: function (opt) {
var o = this.options;
var items = opt.items || [];
var updated = this.updateChildren(o.items, items);
this.options.items = items;
return updated;
// var updated, i, len;
// for (i = 0, len = Math.min(o.items.length, items.length); i < len; i++) {
// if (!this._compare(o.items[i], items[i])) {
// updated = this.updateItemAt(i, items[i]) || updated;
// }
// }
// if (o.items.length > items.length) {
// var deleted = [];
// for (i = items.length; i < o.items.length; i++) {
// deleted.push(this._children[this._getChildName(i)]);
// delete this._children[this._getChildName(i)];
// }
// o.items.splice(items.length);
// BI.each(deleted, function (i, w) {
// w._destroy();
// })
// } else if (items.length > o.items.length) {
// for (i = o.items.length; i < items.length; i++) {
// this.addItemAt(i, items[i]);
// }
// }
// return updated;
},
stroke: function (items) { stroke: function (items) {
var self = this; var self = this;
BI.each(items, function (i, item) { BI.each(items, function (i, item) {

33
demo/js/core/abstract/demo.virtual_group.js

@ -4,19 +4,28 @@ Demo.Func = BI.inherit(BI.Widget, {
}, },
_createItems: function () { _createItems: function () {
var items = BI.makeArray(1000, { var items = BI.map(BI.range(1000), function (i) {
type: "demo.virtual_group_item" return {
type: "demo.virtual_group_item",
key: i + 1
}
}); });
items[0].value = BI.UUID();
return items; return items;
}, },
render: function () { render: function () {
var self = this; var self = this;
var buttonGroupItems = self._createItems();
var virtualGroupItems = self._createItems();
return { return {
type: "bi.vertical", type: "bi.vertical",
vgap: 20, vgap: 20,
items: [{ items: [{
type: "bi.label",
cls: "layout-bg5",
height: 50,
text: "共1000个元素,演示button_group和virtual_group每次删除第一个元素"
}, {
type: "bi.button_group", type: "bi.button_group",
width: 500, width: 500,
height: 300, height: 300,
@ -26,17 +35,14 @@ Demo.Func = BI.inherit(BI.Widget, {
chooseType: BI.ButtonGroup.CHOOSE_TYPE_MULTI, chooseType: BI.ButtonGroup.CHOOSE_TYPE_MULTI,
layouts: [{ layouts: [{
type: "bi.vertical" type: "bi.vertical"
}, {
type: "bi.center_adapt",
}], }],
items: this._createItems() items: this._createItems()
}, { }, {
type: "bi.button", type: "bi.button",
text: "演示button_group的刷新", text: "演示button_group的刷新",
handler: function () { handler: function () {
var items = self._createItems(); buttonGroupItems.shift();
items.pop(); self.buttonGroup.populate(BI.deepClone(buttonGroupItems));
self.buttonGroup.populate(items);
} }
}, { }, {
type: "bi.virtual_group", type: "bi.virtual_group",
@ -48,17 +54,14 @@ Demo.Func = BI.inherit(BI.Widget, {
chooseType: BI.ButtonGroup.CHOOSE_TYPE_MULTI, chooseType: BI.ButtonGroup.CHOOSE_TYPE_MULTI,
layouts: [{ layouts: [{
type: "bi.vertical" type: "bi.vertical"
}, {
type: "bi.center_adapt",
}], }],
items: this._createItems() items: this._createItems()
}, { }, {
type: "bi.button", type: "bi.button",
text: "演示virtual_group的刷新", text: "演示virtual_group的刷新",
handler: function () { handler: function () {
var items = self._createItems(); virtualGroupItems.shift();
items.pop(); self.virtualGroup.populate(BI.deepClone(virtualGroupItems));
self.virtualGroup.populate(items);
} }
}] }]
@ -74,14 +77,14 @@ Demo.Item = BI.inherit(BI.Widget, {
}, },
render: function () { render: function () {
var self = this; var self = this, o = this.options;
return { return {
type: "bi.label", type: "bi.label",
ref: function () { ref: function () {
self.label = this; self.label = this;
}, },
height: this.options.height, height: this.options.height,
text: "这是一个测试项" + BI.UUID() text: "key:" + o.key + ",随机数" + BI.UUID()
} }
}, },

199
dist/bundle.js vendored

@ -19883,33 +19883,194 @@ BI.Layout = BI.inherit(BI.Widget, {
}) })
}, },
update: function (opt) { patchItem: function (oldVnode, vnode, index) {
var o = this.options; if (!this._compare(oldVnode, vnode)) {
var items = opt.items || []; return this.updateItemAt(index, vnode);
var updated, i, len; }
for (i = 0, len = Math.min(o.items.length, items.length); i < len; i++) { },
if (!this._compare(o.items[i], items[i])) {
updated = this.updateItemAt(i, items[i]) || updated; updateChildren: function (oldCh, newCh) {
var self = this;
var oldStartIdx = 0, newStartIdx = 0;
var oldEndIdx = oldCh.length - 1;
var oldStartVnode = oldCh[0];
var oldEndVnode = oldCh[oldEndIdx];
var newEndIdx = newCh.length - 1;
var newStartVnode = newCh[0];
var newEndVnode = newCh[newEndIdx];
var oldKeyToIdx;
var idxInOld;
var elmToMove;
var before;
var updated;
var children = {};
BI.each(oldCh, function (i, child) {
child = self._getOptions(child);
var key = child.key == null ? i : child.key;
if (BI.isKey(key)) {
children[key] = self._children[self._getChildName(i)];
}
});
while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx) {
if (BI.isNull(oldStartVnode)) {
oldStartVnode = oldCh[++oldStartIdx];
} else if (BI.isNull(oldEndVnode)) {
oldEndVnode = oldCh[--oldEndIdx];
} else if (sameVnode(oldStartVnode, newStartVnode, oldStartIdx, newStartIdx)) {
updated = this.patchItem(oldStartVnode, newStartVnode, oldStartIdx) || updated;
oldStartVnode = oldCh[++oldStartIdx];
newStartVnode = newCh[++newStartIdx];
} else if (sameVnode(oldEndVnode, newEndVnode, oldEndIdx, newEndIdx)) {
updated = this.patchItem(oldEndVnode, newEndVnode, oldEndIdx) || updated;
oldEndVnode = oldCh[--oldEndIdx];
newEndVnode = newCh[--newEndIdx];
} else if (sameVnode(oldStartVnode, newEndVnode)) {
updated = this.patchItem(oldStartVnode, newEndVnode, oldStartIdx) || updated;
insertBefore(oldStartVnode, oldEndVnode, true);
oldStartVnode = oldCh[++oldStartIdx];
newEndVnode = newCh[--newEndIdx];
} else if (sameVnode(oldEndVnode, newStartVnode)) {
updated = this.patchItem(oldEndVnode, newStartVnode, oldEndIdx) || updated;
insertBefore(oldEndVnode, oldStartVnode);
oldEndVnode = oldCh[--oldEndIdx];
newStartVnode = newCh[++newStartIdx];
} else {
if (oldKeyToIdx === undefined) {
oldKeyToIdx = createKeyToOldIdx(oldCh, oldStartIdx, oldEndIdx);
}
idxInOld = oldKeyToIdx[newStartVnode.key];
if (BI.isNull(idxInOld)) {
var node = addNode(newStartVnode);
insertBefore(node, oldStartVnode);
newStartVnode = newCh[++newStartIdx];
} else {
elmToMove = oldCh[idxInOld];
var node = addNode(newStartVnode);
insertBefore(node, oldStartVnode);
// if (elmToMove.sel !== newStartVnode.sel) {
// api.insertBefore(parentElm, createElm(newStartVnode), oldStartVnode.elm);
// } else {
// updated = this.patchItem(elmToMove, newStartVnode, idxInOld) || updated;
// oldCh[idxInOld] = undefined;
// api.insertBefore(parentElm, (elmToMove.elm), oldStartVnode.elm);
// }
newStartVnode = newCh[++newStartIdx];
} }
} }
if (o.items.length > items.length) {
var deleted = [];
for (i = items.length; i < o.items.length; i++) {
deleted.push(this._children[this._getChildName(i)]);
delete this._children[this._getChildName(i)];
} }
o.items.splice(items.length); if (oldStartIdx > oldEndIdx) {
BI.each(deleted, function (i, w) { before = BI.isNull(newCh[newEndIdx + 1]) ? null : newCh[newEndIdx + 1].elm;
w._destroy(); addVnodes(before, newCh, newStartIdx, newEndIdx);
}) } else if (newStartIdx > newEndIdx) {
} else if (items.length > o.items.length) { removeVnodes(oldCh, oldStartIdx, oldEndIdx);
for (i = o.items.length; i < items.length; i++) { }
this.addItemAt(i, items[i]);
this._children = {};
BI.each(newCh, function (i, child) {
var node = self._getOptions(child);
var key = node.key == null ? i : node.key;
self._children[self._getChildName(i)] = children[key];
});
function sameVnode(vnode1, vnode2, oldIndex, newIndex) {
vnode1 = self._getOptions(vnode1);
vnode2 = self._getOptions(vnode2);
if (BI.isKey(vnode1.key)) {
return vnode1.key === vnode2.key;
}
if (oldIndex >= 0) {
return oldIndex === newIndex
}
}
function addNode(vnode, index) {
var opt = self._getOptions(vnode);
var key = opt.key == null ? i : opt.key;
return children[key] = self._addElement(key, vnode);
} }
function addVnodes(before, vnodes, startIdx, endIdx) {
for (; startIdx <= endIdx; ++startIdx) {
var node = addNode(vnodes[startIdx], startIdx);
insertBefore(node, before);
}
}
function removeVnodes(vnodes, startIdx, endIdx) {
for (; startIdx <= endIdx; ++startIdx) {
var node = self._getOptions(vnodes[startIdx]);
var key = node.key == null ? startIdx : node.key;
children[key]._destroy();
} }
}
function insertBefore(insert, before, isNext) {
insert = self._getOptions(insert);
before = before && self._getOptions(before);
if (BI.isKey(insert.key)) {
if (before && children[before.key]) {
var next;
if (isNext) {
next = children[before.key].element.next();
} else {
next = children[before.key].element;
}
if (next.length > 0) {
next.before(children[insert.key].element);
} else {
self._getWrapper().append(children[insert.key].element);
}
} else {
self._getWrapper().append(children[insert.key].element);
}
} else {
throw "key is not defined";
}
}
function createKeyToOldIdx(children, beginIdx, endIdx) {
var i, map = {}, key;
for (i = beginIdx; i <= endIdx; ++i) {
key = children[i].key;
if (key !== undefined) map[key] = i;
}
return map;
}
return updated; return updated;
}, },
update: function (opt) {
var o = this.options;
var items = opt.items || [];
var updated = this.updateChildren(o.items, items);
this.options.items = items;
return updated;
// var updated, i, len;
// for (i = 0, len = Math.min(o.items.length, items.length); i < len; i++) {
// if (!this._compare(o.items[i], items[i])) {
// updated = this.updateItemAt(i, items[i]) || updated;
// }
// }
// if (o.items.length > items.length) {
// var deleted = [];
// for (i = items.length; i < o.items.length; i++) {
// deleted.push(this._children[this._getChildName(i)]);
// delete this._children[this._getChildName(i)];
// }
// o.items.splice(items.length);
// BI.each(deleted, function (i, w) {
// w._destroy();
// })
// } else if (items.length > o.items.length) {
// for (i = o.items.length; i < items.length; i++) {
// this.addItemAt(i, items[i]);
// }
// }
// return updated;
},
stroke: function (items) { stroke: function (items) {
var self = this; var self = this;
BI.each(items, function (i, item) { BI.each(items, function (i, item) {

80
dist/bundle.min.js vendored

File diff suppressed because one or more lines are too long

199
dist/core.js vendored

@ -19832,33 +19832,194 @@ BI.Layout = BI.inherit(BI.Widget, {
}) })
}, },
update: function (opt) { patchItem: function (oldVnode, vnode, index) {
var o = this.options; if (!this._compare(oldVnode, vnode)) {
var items = opt.items || []; return this.updateItemAt(index, vnode);
var updated, i, len;
for (i = 0, len = Math.min(o.items.length, items.length); i < len; i++) {
if (!this._compare(o.items[i], items[i])) {
updated = this.updateItemAt(i, items[i]) || updated;
} }
},
updateChildren: function (oldCh, newCh) {
var self = this;
var oldStartIdx = 0, newStartIdx = 0;
var oldEndIdx = oldCh.length - 1;
var oldStartVnode = oldCh[0];
var oldEndVnode = oldCh[oldEndIdx];
var newEndIdx = newCh.length - 1;
var newStartVnode = newCh[0];
var newEndVnode = newCh[newEndIdx];
var oldKeyToIdx;
var idxInOld;
var elmToMove;
var before;
var updated;
var children = {};
BI.each(oldCh, function (i, child) {
child = self._getOptions(child);
var key = child.key == null ? i : child.key;
if (BI.isKey(key)) {
children[key] = self._children[self._getChildName(i)];
}
});
while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx) {
if (BI.isNull(oldStartVnode)) {
oldStartVnode = oldCh[++oldStartIdx];
} else if (BI.isNull(oldEndVnode)) {
oldEndVnode = oldCh[--oldEndIdx];
} else if (sameVnode(oldStartVnode, newStartVnode, oldStartIdx, newStartIdx)) {
updated = this.patchItem(oldStartVnode, newStartVnode, oldStartIdx) || updated;
oldStartVnode = oldCh[++oldStartIdx];
newStartVnode = newCh[++newStartIdx];
} else if (sameVnode(oldEndVnode, newEndVnode, oldEndIdx, newEndIdx)) {
updated = this.patchItem(oldEndVnode, newEndVnode, oldEndIdx) || updated;
oldEndVnode = oldCh[--oldEndIdx];
newEndVnode = newCh[--newEndIdx];
} else if (sameVnode(oldStartVnode, newEndVnode)) {
updated = this.patchItem(oldStartVnode, newEndVnode, oldStartIdx) || updated;
insertBefore(oldStartVnode, oldEndVnode, true);
oldStartVnode = oldCh[++oldStartIdx];
newEndVnode = newCh[--newEndIdx];
} else if (sameVnode(oldEndVnode, newStartVnode)) {
updated = this.patchItem(oldEndVnode, newStartVnode, oldEndIdx) || updated;
insertBefore(oldEndVnode, oldStartVnode);
oldEndVnode = oldCh[--oldEndIdx];
newStartVnode = newCh[++newStartIdx];
} else {
if (oldKeyToIdx === undefined) {
oldKeyToIdx = createKeyToOldIdx(oldCh, oldStartIdx, oldEndIdx);
}
idxInOld = oldKeyToIdx[newStartVnode.key];
if (BI.isNull(idxInOld)) {
var node = addNode(newStartVnode);
insertBefore(node, oldStartVnode);
newStartVnode = newCh[++newStartIdx];
} else {
elmToMove = oldCh[idxInOld];
var node = addNode(newStartVnode);
insertBefore(node, oldStartVnode);
// if (elmToMove.sel !== newStartVnode.sel) {
// api.insertBefore(parentElm, createElm(newStartVnode), oldStartVnode.elm);
// } else {
// updated = this.patchItem(elmToMove, newStartVnode, idxInOld) || updated;
// oldCh[idxInOld] = undefined;
// api.insertBefore(parentElm, (elmToMove.elm), oldStartVnode.elm);
// }
newStartVnode = newCh[++newStartIdx];
} }
if (o.items.length > items.length) {
var deleted = [];
for (i = items.length; i < o.items.length; i++) {
deleted.push(this._children[this._getChildName(i)]);
delete this._children[this._getChildName(i)];
} }
o.items.splice(items.length);
BI.each(deleted, function (i, w) {
w._destroy();
})
} else if (items.length > o.items.length) {
for (i = o.items.length; i < items.length; i++) {
this.addItemAt(i, items[i]);
} }
if (oldStartIdx > oldEndIdx) {
before = BI.isNull(newCh[newEndIdx + 1]) ? null : newCh[newEndIdx + 1].elm;
addVnodes(before, newCh, newStartIdx, newEndIdx);
} else if (newStartIdx > newEndIdx) {
removeVnodes(oldCh, oldStartIdx, oldEndIdx);
}
this._children = {};
BI.each(newCh, function (i, child) {
var node = self._getOptions(child);
var key = node.key == null ? i : node.key;
self._children[self._getChildName(i)] = children[key];
});
function sameVnode(vnode1, vnode2, oldIndex, newIndex) {
vnode1 = self._getOptions(vnode1);
vnode2 = self._getOptions(vnode2);
if (BI.isKey(vnode1.key)) {
return vnode1.key === vnode2.key;
}
if (oldIndex >= 0) {
return oldIndex === newIndex
}
}
function addNode(vnode, index) {
var opt = self._getOptions(vnode);
var key = opt.key == null ? i : opt.key;
return children[key] = self._addElement(key, vnode);
}
function addVnodes(before, vnodes, startIdx, endIdx) {
for (; startIdx <= endIdx; ++startIdx) {
var node = addNode(vnodes[startIdx], startIdx);
insertBefore(node, before);
}
}
function removeVnodes(vnodes, startIdx, endIdx) {
for (; startIdx <= endIdx; ++startIdx) {
var node = self._getOptions(vnodes[startIdx]);
var key = node.key == null ? startIdx : node.key;
children[key]._destroy();
}
}
function insertBefore(insert, before, isNext) {
insert = self._getOptions(insert);
before = before && self._getOptions(before);
if (BI.isKey(insert.key)) {
if (before && children[before.key]) {
var next;
if (isNext) {
next = children[before.key].element.next();
} else {
next = children[before.key].element;
}
if (next.length > 0) {
next.before(children[insert.key].element);
} else {
self._getWrapper().append(children[insert.key].element);
}
} else {
self._getWrapper().append(children[insert.key].element);
}
} else {
throw "key is not defined";
}
}
function createKeyToOldIdx(children, beginIdx, endIdx) {
var i, map = {}, key;
for (i = beginIdx; i <= endIdx; ++i) {
key = children[i].key;
if (key !== undefined) map[key] = i;
}
return map;
} }
return updated; return updated;
}, },
update: function (opt) {
var o = this.options;
var items = opt.items || [];
var updated = this.updateChildren(o.items, items);
this.options.items = items;
return updated;
// var updated, i, len;
// for (i = 0, len = Math.min(o.items.length, items.length); i < len; i++) {
// if (!this._compare(o.items[i], items[i])) {
// updated = this.updateItemAt(i, items[i]) || updated;
// }
// }
// if (o.items.length > items.length) {
// var deleted = [];
// for (i = items.length; i < o.items.length; i++) {
// deleted.push(this._children[this._getChildName(i)]);
// delete this._children[this._getChildName(i)];
// }
// o.items.splice(items.length);
// BI.each(deleted, function (i, w) {
// w._destroy();
// })
// } else if (items.length > o.items.length) {
// for (i = o.items.length; i < items.length; i++) {
// this.addItemAt(i, items[i]);
// }
// }
// return updated;
},
stroke: function (items) { stroke: function (items) {
var self = this; var self = this;
BI.each(items, function (i, item) { BI.each(items, function (i, item) {

33
dist/demo.js vendored

@ -6872,19 +6872,28 @@ BI.shortcut("demo.list_view", Demo.Func);Demo.Func = BI.inherit(BI.Widget, {
}, },
_createItems: function () { _createItems: function () {
var items = BI.makeArray(1000, { var items = BI.map(BI.range(1000), function (i) {
type: "demo.virtual_group_item" return {
type: "demo.virtual_group_item",
key: i + 1
}
}); });
items[0].value = BI.UUID();
return items; return items;
}, },
render: function () { render: function () {
var self = this; var self = this;
var buttonGroupItems = self._createItems();
var virtualGroupItems = self._createItems();
return { return {
type: "bi.vertical", type: "bi.vertical",
vgap: 20, vgap: 20,
items: [{ items: [{
type: "bi.label",
cls: "layout-bg5",
height: 50,
text: "共1000个元素,演示button_group和virtual_group每次删除第一个元素"
}, {
type: "bi.button_group", type: "bi.button_group",
width: 500, width: 500,
height: 300, height: 300,
@ -6894,17 +6903,14 @@ BI.shortcut("demo.list_view", Demo.Func);Demo.Func = BI.inherit(BI.Widget, {
chooseType: BI.ButtonGroup.CHOOSE_TYPE_MULTI, chooseType: BI.ButtonGroup.CHOOSE_TYPE_MULTI,
layouts: [{ layouts: [{
type: "bi.vertical" type: "bi.vertical"
}, {
type: "bi.center_adapt",
}], }],
items: this._createItems() items: this._createItems()
}, { }, {
type: "bi.button", type: "bi.button",
text: "演示button_group的刷新", text: "演示button_group的刷新",
handler: function () { handler: function () {
var items = self._createItems(); buttonGroupItems.shift();
items.pop(); self.buttonGroup.populate(BI.deepClone(buttonGroupItems));
self.buttonGroup.populate(items);
} }
}, { }, {
type: "bi.virtual_group", type: "bi.virtual_group",
@ -6916,17 +6922,14 @@ BI.shortcut("demo.list_view", Demo.Func);Demo.Func = BI.inherit(BI.Widget, {
chooseType: BI.ButtonGroup.CHOOSE_TYPE_MULTI, chooseType: BI.ButtonGroup.CHOOSE_TYPE_MULTI,
layouts: [{ layouts: [{
type: "bi.vertical" type: "bi.vertical"
}, {
type: "bi.center_adapt",
}], }],
items: this._createItems() items: this._createItems()
}, { }, {
type: "bi.button", type: "bi.button",
text: "演示virtual_group的刷新", text: "演示virtual_group的刷新",
handler: function () { handler: function () {
var items = self._createItems(); virtualGroupItems.shift();
items.pop(); self.virtualGroup.populate(BI.deepClone(virtualGroupItems));
self.virtualGroup.populate(items);
} }
}] }]
@ -6942,14 +6945,14 @@ Demo.Item = BI.inherit(BI.Widget, {
}, },
render: function () { render: function () {
var self = this; var self = this, o = this.options;
return { return {
type: "bi.label", type: "bi.label",
ref: function () { ref: function () {
self.label = this; self.label = this;
}, },
height: this.options.height, height: this.options.height,
text: "这是一个测试项" + BI.UUID() text: "key:" + o.key + ",随机数" + BI.UUID()
} }
}, },

199
src/core/wrapper/layout.js

@ -347,31 +347,192 @@ BI.Layout = BI.inherit(BI.Widget, {
}) })
}, },
update: function (opt) { patchItem: function (oldVnode, vnode, index) {
var o = this.options; if (!this._compare(oldVnode, vnode)) {
var items = opt.items || []; return this.updateItemAt(index, vnode);
var updated, i, len;
for (i = 0, len = Math.min(o.items.length, items.length); i < len; i++) {
if (!this._compare(o.items[i], items[i])) {
updated = this.updateItemAt(i, items[i]) || updated;
} }
},
updateChildren: function (oldCh, newCh) {
var self = this;
var oldStartIdx = 0, newStartIdx = 0;
var oldEndIdx = oldCh.length - 1;
var oldStartVnode = oldCh[0];
var oldEndVnode = oldCh[oldEndIdx];
var newEndIdx = newCh.length - 1;
var newStartVnode = newCh[0];
var newEndVnode = newCh[newEndIdx];
var oldKeyToIdx;
var idxInOld;
var elmToMove;
var before;
var updated;
var children = {};
BI.each(oldCh, function (i, child) {
child = self._getOptions(child);
var key = child.key == null ? i : child.key;
if (BI.isKey(key)) {
children[key] = self._children[self._getChildName(i)];
} }
if (o.items.length > items.length) { });
var deleted = [];
for (i = items.length; i < o.items.length; i++) { while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx) {
deleted.push(this._children[this._getChildName(i)]); if (BI.isNull(oldStartVnode)) {
delete this._children[this._getChildName(i)]; oldStartVnode = oldCh[++oldStartIdx];
} else if (BI.isNull(oldEndVnode)) {
oldEndVnode = oldCh[--oldEndIdx];
} else if (sameVnode(oldStartVnode, newStartVnode, oldStartIdx, newStartIdx)) {
updated = this.patchItem(oldStartVnode, newStartVnode, oldStartIdx) || updated;
oldStartVnode = oldCh[++oldStartIdx];
newStartVnode = newCh[++newStartIdx];
} else if (sameVnode(oldEndVnode, newEndVnode, oldEndIdx, newEndIdx)) {
updated = this.patchItem(oldEndVnode, newEndVnode, oldEndIdx) || updated;
oldEndVnode = oldCh[--oldEndIdx];
newEndVnode = newCh[--newEndIdx];
} else if (sameVnode(oldStartVnode, newEndVnode)) {
updated = this.patchItem(oldStartVnode, newEndVnode, oldStartIdx) || updated;
insertBefore(oldStartVnode, oldEndVnode, true);
oldStartVnode = oldCh[++oldStartIdx];
newEndVnode = newCh[--newEndIdx];
} else if (sameVnode(oldEndVnode, newStartVnode)) {
updated = this.patchItem(oldEndVnode, newStartVnode, oldEndIdx) || updated;
insertBefore(oldEndVnode, oldStartVnode);
oldEndVnode = oldCh[--oldEndIdx];
newStartVnode = newCh[++newStartIdx];
} else {
if (oldKeyToIdx === undefined) {
oldKeyToIdx = createKeyToOldIdx(oldCh, oldStartIdx, oldEndIdx);
}
idxInOld = oldKeyToIdx[newStartVnode.key];
if (BI.isNull(idxInOld)) {
var node = addNode(newStartVnode);
insertBefore(node, oldStartVnode);
newStartVnode = newCh[++newStartIdx];
} else {
elmToMove = oldCh[idxInOld];
var node = addNode(newStartVnode);
insertBefore(node, oldStartVnode);
// if (elmToMove.sel !== newStartVnode.sel) {
// api.insertBefore(parentElm, createElm(newStartVnode), oldStartVnode.elm);
// } else {
// updated = this.patchItem(elmToMove, newStartVnode, idxInOld) || updated;
// oldCh[idxInOld] = undefined;
// api.insertBefore(parentElm, (elmToMove.elm), oldStartVnode.elm);
// }
newStartVnode = newCh[++newStartIdx];
}
}
}
if (oldStartIdx > oldEndIdx) {
before = BI.isNull(newCh[newEndIdx + 1]) ? null : newCh[newEndIdx + 1].elm;
addVnodes(before, newCh, newStartIdx, newEndIdx);
} else if (newStartIdx > newEndIdx) {
removeVnodes(oldCh, oldStartIdx, oldEndIdx);
}
this._children = {};
BI.each(newCh, function (i, child) {
var node = self._getOptions(child);
var key = node.key == null ? i : node.key;
self._children[self._getChildName(i)] = children[key];
});
function sameVnode(vnode1, vnode2, oldIndex, newIndex) {
vnode1 = self._getOptions(vnode1);
vnode2 = self._getOptions(vnode2);
if (BI.isKey(vnode1.key)) {
return vnode1.key === vnode2.key;
} }
o.items.splice(items.length); if (oldIndex >= 0) {
BI.each(deleted, function (i, w) { return oldIndex === newIndex
w._destroy(); }
}) }
} else if (items.length > o.items.length) {
for (i = o.items.length; i < items.length; i++) { function addNode(vnode, index) {
this.addItemAt(i, items[i]); var opt = self._getOptions(vnode);
var key = opt.key == null ? i : opt.key;
return children[key] = self._addElement(key, vnode);
}
function addVnodes(before, vnodes, startIdx, endIdx) {
for (; startIdx <= endIdx; ++startIdx) {
var node = addNode(vnodes[startIdx], startIdx);
insertBefore(node, before);
}
}
function removeVnodes(vnodes, startIdx, endIdx) {
for (; startIdx <= endIdx; ++startIdx) {
var node = self._getOptions(vnodes[startIdx]);
var key = node.key == null ? startIdx : node.key;
children[key]._destroy();
}
}
function insertBefore(insert, before, isNext) {
insert = self._getOptions(insert);
before = before && self._getOptions(before);
if (BI.isKey(insert.key)) {
if (before && children[before.key]) {
var next;
if (isNext) {
next = children[before.key].element.next();
} else {
next = children[before.key].element;
}
if (next.length > 0) {
next.before(children[insert.key].element);
} else {
self._getWrapper().append(children[insert.key].element);
}
} else {
self._getWrapper().append(children[insert.key].element);
}
} else {
throw "key is not defined";
} }
} }
function createKeyToOldIdx(children, beginIdx, endIdx) {
var i, map = {}, key;
for (i = beginIdx; i <= endIdx; ++i) {
key = children[i].key;
if (key !== undefined) map[key] = i;
}
return map;
}
return updated;
},
update: function (opt) {
var o = this.options;
var items = opt.items || [];
var updated = this.updateChildren(o.items, items);
this.options.items = items;
return updated; return updated;
// var updated, i, len;
// for (i = 0, len = Math.min(o.items.length, items.length); i < len; i++) {
// if (!this._compare(o.items[i], items[i])) {
// updated = this.updateItemAt(i, items[i]) || updated;
// }
// }
// if (o.items.length > items.length) {
// var deleted = [];
// for (i = items.length; i < o.items.length; i++) {
// deleted.push(this._children[this._getChildName(i)]);
// delete this._children[this._getChildName(i)];
// }
// o.items.splice(items.length);
// BI.each(deleted, function (i, w) {
// w._destroy();
// })
// } else if (items.length > o.items.length) {
// for (i = o.items.length; i < items.length; i++) {
// this.addItemAt(i, items[i]);
// }
// }
// return updated;
}, },
stroke: function (items) { stroke: function (items) {

Loading…
Cancel
Save