Browse Source

整理代码

es6
guy 3 years ago
parent
commit
6704e10365
  1. 46
      src/core/wrapper/layout.js

46
src/core/wrapper/layout.js

@ -198,6 +198,21 @@ BI.Layout = BI.inherit(BI.Widget, {
return this.element; return this.element;
}, },
// 不依赖于this.options.items进行更新
_updateItemAt: function (index, item) {
var del = this._children[this._getChildName(index)];
delete this._children[this._getChildName(index)];
var w = this._addElement(index, item);
this._children[this._getChildName(index)] = w;
if (index > 0) {
this._children[this._getChildName(index - 1)].element.after(w.element);
} else {
w.element.prependTo(this._getWrapper());
}
del._destroy();
w._mount();
},
_addItemAt: function (index, item) { _addItemAt: function (index, item) {
for (var i = this.options.items.length; i > index; i--) { for (var i = this.options.items.length; i > index; i--) {
this._children[this._getChildName(i)] = this._children[this._getChildName(i - 1)]; this._children[this._getChildName(i)] = this._children[this._getChildName(i - 1)];
@ -286,35 +301,13 @@ BI.Layout = BI.inherit(BI.Widget, {
}, },
shouldUpdateItem: function (index, item) { shouldUpdateItem: function (index, item) {
if (index < 0 || index > this.options.items.length - 1) {
return false;
}
var child = this._children[this._getChildName(index)]; var child = this._children[this._getChildName(index)];
if (!child.shouldUpdate) { if (!child || !child.shouldUpdate) {
return null; return null;
} }
return child.shouldUpdate(this._getOptions(item)); return child.shouldUpdate(this._getOptions(item));
}, },
updateItemAt: function (index, item) {
if (index < 0 || index > this.options.items.length - 1) {
return;
}
var del = this._children[this._getChildName(index)];
delete this._children[this._getChildName(index)];
this.options.items.splice(index, 1);
var w = this._addElement(index, item);
this.options.items.splice(index, 0, item);
this._children[this._getChildName(index)] = w;
if (index > 0) {
this._children[this._getChildName(index - 1)].element.after(w.element);
} else {
w.element.prependTo(this._getWrapper());
}
del._destroy();
w._mount();
},
addItems: function (items, context) { addItems: function (items, context) {
var self = this, o = this.options; var self = this, o = this.options;
var fragment = BI.Widget._renderEngine.createFragment(); var fragment = BI.Widget._renderEngine.createFragment();
@ -394,8 +387,9 @@ BI.Layout = BI.inherit(BI.Widget, {
// if (child.update) { // if (child.update) {
// return child.update(this._getOptions(vnode)); // return child.update(this._getOptions(vnode));
// } // }
return this.updateItemAt(index, vnode); return this._updateItemAt(index, vnode);
} }
}, },
updateChildren: function (oldCh, newCh) { updateChildren: function (oldCh, newCh) {
@ -564,9 +558,9 @@ BI.Layout = BI.inherit(BI.Widget, {
update: function (opt) { update: function (opt) {
var o = this.options; var o = this.options;
var items = opt.items || []; var items = opt.items || [];
var updated = this.updateChildren(o.items, items); var oldItems = o.items;
this.options.items = items; this.options.items = items;
return updated; return this.updateChildren(oldItems, items);
}, },
stroke: function (items) { stroke: function (items) {

Loading…
Cancel
Save