Merge pull request #23 from PolymerLabs/set-value-justin

refactor NodePart::setValue
This commit is contained in:
Justin Fagnani 2017-08-02 01:30:40 -07:00 коммит произвёл GitHub
Родитель 595c2999bc d62f5bfd37
Коммит 6599e8fb26
1 изменённых файлов: 91 добавлений и 76 удалений

Просмотреть файл

@ -266,24 +266,12 @@ export class NodePart extends Part {
}
setValue(value: any): void {
let node: Node|undefined = undefined;
value = this._getValue(value);
if (value instanceof Node) {
this.clear();
node = value;
this._previousValue = value;
this._previousValue = this._setNodeValue(value);
} else if (value instanceof TemplateResult) {
let instance: TemplateInstance;
if (this._previousValue && this._previousValue._template === value.template) {
instance = this._previousValue;
} else {
this.clear();
instance = this.instance._createInstance(value.template);
node = instance._clone();
}
instance.update(value.values);
this._previousValue = instance;
this._previousValue = this._setTemplateResultValue(value);
} else if (value && value.then !== undefined) {
value.then((v: any) => {
if (this._previousValue === value) {
@ -292,6 +280,44 @@ export class NodePart extends Part {
});
this._previousValue = value;
} else if (value && typeof value !== 'string' && value[Symbol.iterator]) {
this._previousValue = this._setIterableValue(value);
} else if (this.startNode.nextSibling! === this.endNode.previousSibling! &&
this.startNode.nextSibling!.nodeType === Node.TEXT_NODE) {
this.startNode.nextSibling!.textContent = value;
this._previousValue = value;
} else {
this._previousValue = this._setTextValue(value);
}
}
private _insertNodeBeforeEndNode(node: Node) {
this.endNode.parentNode!.insertBefore(node, this.endNode);
}
private _setNodeValue(value: Node): Node {
this.clear();
this._insertNodeBeforeEndNode(value);
return value;
}
private _setTextValue(value: string): Node {
return this._setNodeValue(new Text(value));
}
private _setTemplateResultValue(value: TemplateResult): TemplateInstance {
let instance: TemplateInstance;
if (this._previousValue && this._previousValue._template === value.template) {
instance = this._previousValue;
} else {
instance = this.instance._createInstance(value.template);
this._setNodeValue(instance._clone());
}
instance.update(value.values);
return instance;
}
private _setIterableValue(value: any): NodePart[] {
// For an Iterable, we create a new InstancePart per item, then set its
// value to the item. This is a little bit of overhead for every item in
// an Iterable, but it lets us recurse easily and update Arrays of
@ -305,7 +331,8 @@ export class NodePart extends Part {
let itemEnd;
const values = value[Symbol.iterator]() as Iterator<any>;
const previousParts: NodePart[]|undefined = Array.isArray(this._previousValue) ? this._previousValue : undefined;
const previousParts: NodePart[]|undefined = Array.isArray(this._previousValue) ?
this._previousValue : undefined;
let previousPartsIndex = 0;
const itemParts = [];
let current = values.next();
@ -337,7 +364,7 @@ export class NodePart extends Part {
itemEnd = this.endNode;
} else {
itemEnd = new Text();
this.endNode.parentNode!.insertBefore(itemEnd, this.endNode);
this._insertNodeBeforeEndNode(itemEnd);
}
itemPart = new NodePart(this.instance, itemStart, itemEnd);
}
@ -349,19 +376,7 @@ export class NodePart extends Part {
next = values.next();
itemStart = itemEnd;
}
this._previousValue = itemParts;
} else if (this.startNode.nextSibling! === this.endNode.previousSibling! &&
this.startNode.nextSibling!.nodeType === Node.TEXT_NODE) {
this.startNode.nextSibling!.textContent = value;
this._previousValue = value;
} else {
this.clear();
node = new Text(value);
this._previousValue = value;
}
if (node !== undefined) {
this.endNode.parentNode!.insertBefore(node, this.endNode);
}
return itemParts;
}
clear(startNode: Node = this.startNode) {