Search in sources :

Example 16 with WXDomObject

use of com.taobao.weex.dom.WXDomObject in project incubator-weex by apache.

the class StatementTest method createVForNode.

private WXCell createVForNode() throws Exception {
    WXCell cell = new WXCell(WXSDKInstanceTest.createInstance(), new WXCellDomObject(), null, false);
    final WXDiv div = new WXDiv(WXSDKInstanceTest.createInstance(), new WXDomObject(), cell);
    cell.addChild(div);
    WXText text = new WXText(WXSDKInstanceTest.createInstance(), new WXTextDomObject(), div);
    WXStatement statement = new WXStatement();
    statement.put("[[repeat]]", ELUtils.vforBlock(JSON.parse("{\n" + "      '@expression': 'dataList',\n" + "      '@index': 'index',\n" + "      '@alias': 'item'\n" + "    }")));
    WXDomObject domObject = (WXDomObject) text.getDomObject();
    domObject.getAttrs().setStatement(statement);
    div.addChild(text);
    PowerMockito.mockStatic(WXComponentFactory.class, new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            WXText renderNode = new WXText(WXSDKInstanceTest.createInstance(), new WXTextDomObject(), div);
            return renderNode;
        }
    });
    return cell;
}
Also used : Answer(org.mockito.stubbing.Answer) WXDiv(com.taobao.weex.ui.component.WXDiv) WXDomObject(com.taobao.weex.dom.WXDomObject) InvocationOnMock(org.mockito.invocation.InvocationOnMock) WXTextDomObject(com.taobao.weex.dom.WXTextDomObject) WXDomObject(com.taobao.weex.dom.WXDomObject) TestDomObject(com.taobao.weex.dom.TestDomObject) WXCellDomObject(com.taobao.weex.dom.WXCellDomObject) WXTextDomObject(com.taobao.weex.dom.WXTextDomObject) JSONObject(com.alibaba.fastjson.JSONObject) WXText(com.taobao.weex.ui.component.WXText) WXCellDomObject(com.taobao.weex.dom.WXCellDomObject) WXCell(com.taobao.weex.ui.component.list.WXCell) WXStatement(com.taobao.weex.dom.binding.WXStatement)

Example 17 with WXDomObject

use of com.taobao.weex.dom.WXDomObject in project incubator-weex by apache.

the class StatementTest method testVFor.

@Test
public void testVFor() throws Exception {
    WXCell cell = createVForNode();
    int count = 3;
    Statements.doRender(cell, createContext(count));
    Assert.assertTrue(cell.getChildCount() == 1);
    WXDiv div = (WXDiv) cell.getChild(0);
    Assert.assertEquals(div.getChildCount(), count);
    Assert.assertNotNull(div.getChild(0).getDomObject());
    Assert.assertNotNull(((WXDomObject) div.getChild(0).getDomObject()).getAttrs().getStatement());
    Assert.assertNull(((WXDomObject) div.getChild(1).getDomObject()).getAttrs().getStatement());
    WXComponent childOne = div.getChild(0);
    WXComponent childTwo = div.getChild(1);
    WXComponent childThree = div.getChild(2);
    count = 4;
    Statements.doRender(cell, createContext(count));
    Assert.assertTrue(cell.getChildCount() == 1);
    div = (WXDiv) cell.getChild(0);
    Assert.assertTrue(div.getChildCount() == count);
    Assert.assertSame(childOne, div.getChild(0));
    Assert.assertSame(childTwo, div.getChild(1));
    Assert.assertSame(childThree, div.getChild(2));
    WXComponent childFour = div.getChild(3);
    count = 5;
    Statements.doRender(cell, createContext(count));
    Assert.assertTrue(cell.getChildCount() == 1);
    div = (WXDiv) cell.getChild(0);
    Assert.assertTrue(div.getChildCount() == count);
    Assert.assertSame(childOne, div.getChild(0));
    Assert.assertSame(childTwo, div.getChild(1));
    Assert.assertSame(childThree, div.getChild(2));
    Assert.assertSame(childFour, div.getChild(3));
    count = 3;
    Statements.doRender(cell, createContext(count));
    Assert.assertTrue(cell.getChildCount() == 1);
    div = (WXDiv) cell.getChild(0);
    Assert.assertTrue(div.getChildCount() == 5);
    Assert.assertSame(childOne, div.getChild(0));
    Assert.assertSame(childTwo, div.getChild(1));
    Assert.assertSame(childThree, div.getChild(2));
    for (int i = count; i < 5; i++) {
        Assert.assertTrue(div.getChild(i).isWaste());
    }
}
Also used : WXDiv(com.taobao.weex.ui.component.WXDiv) WXDomObject(com.taobao.weex.dom.WXDomObject) WXComponent(com.taobao.weex.ui.component.WXComponent) WXCell(com.taobao.weex.ui.component.list.WXCell) WXSDKInstanceTest(com.taobao.weex.WXSDKInstanceTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 18 with WXDomObject

use of com.taobao.weex.dom.WXDomObject in project incubator-weex by apache.

the class WXListComponentTest method testParseTransforms.

@Test
public void testParseTransforms() throws Exception {
    WXDiv div = WXDivTest.create();
    ComponentTest.create(div);
    WXDomObject dom = new WXListDomObject();
    dom.getAttrs().put(Constants.Name.TRANSFORM, "scale(0.9,0.8);translate(10,20);opacity(0.5);rotate(100)");
    component = create(div, dom);
    ComponentTest.create(component);
}
Also used : WXDiv(com.taobao.weex.ui.component.WXDiv) WXListDomObject(com.taobao.weex.dom.WXListDomObject) WXDomObject(com.taobao.weex.dom.WXDomObject) Test(org.junit.Test) ComponentTest(com.taobao.weex.ui.component.ComponentTest) WXSDKInstanceTest(com.taobao.weex.WXSDKInstanceTest) WXHeaderTest(com.taobao.weex.ui.component.WXHeaderTest) WXDivTest(com.taobao.weex.ui.component.WXDivTest)

Example 19 with WXDomObject

use of com.taobao.weex.dom.WXDomObject in project incubator-weex by apache.

the class WXTextTest method setUp.

@Before
public void setUp() throws Exception {
    WXEnvironment.sApplication = RuntimeEnvironment.application;
    WXSDKInstance instance = Mockito.mock(WXSDKInstance.class);
    Mockito.when(instance.getContext()).thenReturn(RuntimeEnvironment.application);
    Mockito.when(instance.getFlatUIContext()).thenReturn(new FlatGUIContext());
    mParentDomObj = Mockito.spy(new WXDomObject());
    Mockito.when(mParentDomObj.getPadding()).thenReturn(new Spacing());
    Mockito.when(mParentDomObj.getBorder()).thenReturn(new Spacing());
    Mockito.when(mParentDomObj.clone()).thenReturn(mParentDomObj);
    TestDomObject.setRef(mParentDomObj, WXDomObject.ROOT);
    mDomObject = Mockito.spy(new WXTextDomObject());
    TestDomObject.setRef(mDomObject, "1");
    mDomObject.addEvent(Constants.Event.CLICK);
    Mockito.when(mDomObject.clone()).thenReturn(mDomObject);
    Mockito.when(mDomObject.getPadding()).thenReturn(new Spacing());
    Mockito.when(mDomObject.getBorder()).thenReturn(new Spacing());
    Mockito.when(mDomObject.getMargin()).thenReturn(new Spacing());
    Mockito.when(mDomObject.getLayoutWidth()).thenReturn(100f);
    Mockito.when(mDomObject.getLayoutHeight()).thenReturn(100f);
    mParent = new WXDiv(instance, mParentDomObj, null);
    mParent.createView();
    mWXText = new WXText(instance, mDomObject, mParent);
    mWXText.bindHolder(new SimpleComponentHolder(WXText.class));
    assertNotNull(instance.getContext());
}
Also used : WXSDKInstance(com.taobao.weex.WXSDKInstance) FlatGUIContext(com.taobao.weex.ui.flat.FlatGUIContext) WXDomObject(com.taobao.weex.dom.WXDomObject) WXTextDomObject(com.taobao.weex.dom.WXTextDomObject) SimpleComponentHolder(com.taobao.weex.ui.SimpleComponentHolder) Spacing(com.taobao.weex.dom.flex.Spacing) Before(org.junit.Before)

Example 20 with WXDomObject

use of com.taobao.weex.dom.WXDomObject in project incubator-weex by apache.

the class Statements method doRenderComponent.

/**
 *  @param component component with v-for statement, v-if statement and bind attrs
 *  @param context   execute context
 *  render component in context, the function do the following  work.
 *  execute component's v-for statement, v-if statement in context,
 *  and rebuild component's tree with the statement, v-for reuse component execute by pre render.
 *  if executed, component will be removed, don't remove, just mark it waste;
 *  may be next render it can be used.
 *  after statement has executed, render component's binding attrs in context and bind it to component.
 */
private static final int doRenderComponent(WXComponent component, CellRenderContext context, List<WXComponent> updates) {
    WXVContainer parent = component.getParent();
    WXDomObject domObject = (WXDomObject) component.getDomObject();
    WXAttr attrs = domObject.getAttrs();
    WXStatement statement = attrs.getStatement();
    if (statement != null) {
        WXDomObject parentDomObject = (WXDomObject) parent.getDomObject();
        Token vif = null;
        JSONObject vfor = null;
        if (statement.get(WXStatement.WX_IF) instanceof Token) {
            vif = (Token) statement.get(WXStatement.WX_IF);
        }
        if (statement.get(WXStatement.WX_FOR) instanceof JSONObject) {
            vfor = (JSONObject) statement.get(WXStatement.WX_FOR);
        }
        // execute v-for content
        if (vfor != null) {
            int renderIndex = parent.indexOf(component);
            if (vfor.get(WXStatement.WX_FOR_LIST) instanceof Token) {
                Token listBlock = (Token) vfor.get(WXStatement.WX_FOR_LIST);
                String indexKey = vfor.getString(WXStatement.WX_FOR_INDEX);
                String itemKey = vfor.getString(WXStatement.WX_FOR_ITEM);
                Object data = null;
                if (listBlock != null) {
                    data = listBlock.execute(context.stack);
                }
                if ((data instanceof List || data instanceof Map)) {
                    Collection collection = null;
                    Map map = null;
                    if (data instanceof List) {
                        collection = (List) data;
                    } else {
                        map = (Map) data;
                        collection = map.keySet();
                    }
                    Map<String, Object> loop = new HashMap<>();
                    int index = 0;
                    for (Object item : collection) {
                        Object key = null;
                        Object value = item;
                        if (map == null) {
                            key = index;
                            value = item;
                        } else {
                            key = item;
                            value = map.get(item);
                        }
                        if (indexKey != null) {
                            loop.put(indexKey, key);
                        }
                        if (itemKey != null) {
                            loop.put(itemKey, value);
                        } else {
                            context.stack.push(value);
                        }
                        if (loop.size() > 0) {
                            context.stack.push(loop);
                        }
                        if (vif != null) {
                            if (!Operators.isTrue(vif.execute(context.stack))) {
                                continue;
                            }
                        }
                        // find resuable renderNode
                        WXComponent renderNode = null;
                        if (renderIndex < parent.getChildCount()) {
                            renderNode = parent.getChild(renderIndex);
                            // check is same statment, if true, it is usabled.
                            if (!isCreateFromNodeStatement(renderNode, component)) {
                                renderNode = null;
                            }
                            if (renderNode != null) {
                                if (renderNode.isWaste()) {
                                    renderNode.setWaste(false);
                                }
                            }
                        }
                        // none resuable render node, create node, add to parent, but clear node's statement
                        if (renderNode == null) {
                            long start = System.currentTimeMillis();
                            renderNode = copyComponentTree(component, parent);
                            renderNode.setWaste(false);
                            WXDomObject renderNodeDomObject = (WXDomObject) renderNode.getDomObject();
                            if (renderNodeDomObject.getAttrs().getStatement() != null) {
                                renderNodeDomObject.getAttrs().getStatement().remove(WXStatement.WX_FOR);
                                // clear node's statement
                                renderNodeDomObject.getAttrs().getStatement().remove(WXStatement.WX_IF);
                            }
                            parentDomObject.add(renderNodeDomObject, renderIndex);
                            parent.addChild(renderNode, renderIndex);
                            updates.add(renderNode);
                            if (WXEnvironment.isApkDebugable()) {
                                WXLogUtils.d(WXRecyclerTemplateList.TAG, Thread.currentThread().getName() + renderNode.getRef() + renderNode.getDomObject().getType() + "statements copy component tree used " + (System.currentTimeMillis() - start));
                            }
                        }
                        doBindingAttrsEventAndRenderChildNode(renderNode, (WXDomObject) renderNode.getDomObject(), context, updates);
                        renderIndex++;
                        if (loop.size() > 0) {
                            context.stack.push(loop);
                        }
                        if (itemKey == null) {
                            context.stack.pop();
                        }
                    }
                }
            } else {
                WXLogUtils.e(WXRecyclerTemplateList.TAG, vfor.toJSONString() + " not call vfor block, for pre compile");
            }
            // after v-for execute, remove component created pre v-for.
            for (; renderIndex < parent.getChildCount(); renderIndex++) {
                WXComponent wasteNode = parent.getChild(renderIndex);
                if (!isCreateFromNodeStatement(wasteNode, component)) {
                    break;
                }
                wasteNode.setWaste(true);
            }
            return renderIndex - parent.indexOf(component);
        }
        // execute v-if context
        if (vif != null) {
            if (!Operators.isTrue(vif.execute(context.stack))) {
                component.setWaste(true);
                return 1;
            } else {
                component.setWaste(false);
            }
        }
    }
    doBindingAttrsEventAndRenderChildNode(component, domObject, context, updates);
    return 1;
}
Also used : WXComponent(com.taobao.weex.ui.component.WXComponent) HashMap(java.util.HashMap) Token(com.taobao.weex.el.parse.Token) WXStatement(com.taobao.weex.dom.binding.WXStatement) WXVContainer(com.taobao.weex.ui.component.WXVContainer) WXDomObject(com.taobao.weex.dom.WXDomObject) JSONObject(com.alibaba.fastjson.JSONObject) Collection(java.util.Collection) WXDomObject(com.taobao.weex.dom.WXDomObject) JSONObject(com.alibaba.fastjson.JSONObject) ArrayList(java.util.ArrayList) WXRecyclerTemplateList(com.taobao.weex.ui.component.list.template.WXRecyclerTemplateList) List(java.util.List) HashMap(java.util.HashMap) ArrayMap(android.support.v4.util.ArrayMap) Map(java.util.Map) WXAttr(com.taobao.weex.dom.WXAttr)

Aggregations

WXDomObject (com.taobao.weex.dom.WXDomObject)42 WXSDKInstance (com.taobao.weex.WXSDKInstance)15 WXComponent (com.taobao.weex.ui.component.WXComponent)10 JSONObject (com.alibaba.fastjson.JSONObject)8 WXVContainer (com.taobao.weex.ui.component.WXVContainer)8 Spacing (com.taobao.weex.dom.flex.Spacing)7 WXEvent (com.taobao.weex.dom.WXEvent)6 WXTextDomObject (com.taobao.weex.dom.WXTextDomObject)6 Test (org.junit.Test)6 WXSDKInstanceTest (com.taobao.weex.WXSDKInstanceTest)5 Before (org.junit.Before)5 ArrayMap (android.support.v4.util.ArrayMap)4 WXAttr (com.taobao.weex.dom.WXAttr)4 WXDiv (com.taobao.weex.ui.component.WXDiv)4 WXCell (com.taobao.weex.ui.component.list.WXCell)4 HashMap (java.util.HashMap)4 WXStyle (com.taobao.weex.dom.WXStyle)3 FlatGUIContext (com.taobao.weex.ui.flat.FlatGUIContext)3 Map (java.util.Map)3 SpannableString (android.text.SpannableString)2