Skip to content

Commit

Permalink
fix: <refactoring repeat item>
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake.Zheng authored and Jake.Zheng committed Sep 28, 2018
1 parent 8b18567 commit 94e7d6b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
5 changes: 3 additions & 2 deletions demo/components/comp7.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<uku-component>
<template>
<ul>
<li uku-repeat="num in cc.totalNumber" uku-class=" num === cc.currentNumber?'normal red':'normal'">
<a uku-onclick="cc.change(num)">{{num}}</a>
<li uku-repeat="num in cc.totalNumber" uku-class=" num === cc.currentNumber?'normal red':'normal'">
<a uku-onclick="cc.change(num)">{{ num }}</a>
<label>{{ num }}</label>
</li>
</ul>
</template>
Expand Down
2 changes: 1 addition & 1 deletion dist/uku.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/uku.js.map

Large diffs are not rendered by default.

37 changes: 26 additions & 11 deletions src/model/BoundItemRepeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@ export class BoundItemRepeat extends BoundItemBase{
this.endCommentString = undefined;
}

replaceVarible(element:HTMLElement, varName, alias){
let pattern = new RegExp("\\b"+ varName + "(?!\\w)","gm");
let attributes = element.attributes;
for(let i=0; i<attributes.length; i++){
let attr = attributes[i];
if(UkuleleUtil.isUkuAttrTag(attr.nodeName)){
attr.nodeValue = attr.nodeValue.replace(pattern, alias);
}
}
let childTag = element.innerHTML.match(new RegExp("\\<\\w*.*\\>","gm"))
let innerText = element.innerHTML.match(new RegExp("\\{{[\\w\\W]*\\}\\}","gm"));
if(childTag === null
&& innerText !== null
&& innerText.length === 1){
element.textContent = element.textContent.replace(pattern, alias);
}
if(element.children && element.children.length > 0){
for(let j=0; j<element.children.length; j++){
let child:HTMLElement = element.children[j] as HTMLElement;
this.replaceVarible(child, varName, alias);
}
}
}

render(controllers) {
let finalValue = UkuleleUtil.getFinalValue(controllers, this.attributeName);
if (!finalValue) {
Expand Down Expand Up @@ -57,13 +81,6 @@ export class BoundItemRepeat extends BoundItemBase{
NodeFilter.SHOW_COMMENT,
safeFilter,
false);

/*function filter(node:Node) :any{
if (node.nodeValue === self.beginCommentString) {
return (NodeFilter.FILTER_ACCEPT);
}
return (NodeFilter.FILTER_SKIP);
}*/

function generateTempContainer():HTMLElement{
let index = UkuleleUtil.searchHtmlTag(self.renderTemplate,"tr");
Expand Down Expand Up @@ -114,10 +131,8 @@ export class BoundItemRepeat extends BoundItemBase{
this.uku.registerController(alias, {'value':finalValue[j]});
alias = alias + ".value";
}
let pattern = new RegExp("\\b"+ this.expression + "(?!\\-|\\s|\\w|\\=)","gm");
//let pattern = new RegExp(this.expression+"(?!\\w)","gm");
let newOuterHtml = child.outerHTML.replace(pattern,alias);
child.insertAdjacentHTML('afterend',newOuterHtml);
this.replaceVarible(child,this.expression,alias);
child.insertAdjacentHTML('afterend',child.outerHTML);
let newItemDom:HTMLElement = child.nextSibling as HTMLElement;
child.parentNode.removeChild(child);
child = newItemDom;
Expand Down
7 changes: 7 additions & 0 deletions src/util/UkuleleUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ export class UkuleleUtil {
return index;
}

static isUkuAttrTag(tagName:string): boolean{
if(this.searchUkuAttrTag(tagName) > -1){
return true;
}
return false;
}

static getAttrFromUkuTag(ukuTag: string, camelCase: boolean = false) {
if (UkuleleUtil.searchUkuAttrTag(ukuTag) === 0) {
ukuTag = ukuTag.replace('uku-', '');
Expand Down

0 comments on commit 94e7d6b

Please sign in to comment.