Search in sources :

Example 36 with WXDomObject

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

the class Layouts method setLayoutWaste.

private static final void setLayoutWaste(WXComponent component, boolean force) {
    WXDomObject domObject = (WXDomObject) component.getDomObject();
    if (domObject.hasUpdate() || force) {
        domObject.markUpdateSeen();
        if (domObject.hasUpdate()) {
            domObject.markLayoutStateUpdated();
        }
    }
    if (component instanceof WXVContainer) {
        WXVContainer container = (WXVContainer) component;
        int count = container.getChildCount();
        for (int i = 0; i < count; ++i) {
            WXComponent child = container.getChild(i);
            if (child != null) {
                setLayoutWaste(child, force);
            }
        }
    }
}
Also used : WXVContainer(com.taobao.weex.ui.component.WXVContainer) WXDomObject(com.taobao.weex.dom.WXDomObject) WXComponent(com.taobao.weex.ui.component.WXComponent)

Example 37 with WXDomObject

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

the class Layouts method doLayout.

private static void doLayout(WXComponent component, final CSSLayoutContext layoutContext) {
    WXDomObject domObject = (WXDomObject) component.getDomObject();
    final WXSDKInstance instance = component.getInstance();
    domObject.traverseUpdateTree(new WXDomObject.Consumer() {

        @Override
        public void accept(WXDomObject dom) {
            if (instance == null || instance.isDestroy()) {
                return;
            }
            if (!dom.hasUpdate()) {
                return;
            }
            if (!dom.isShow()) {
                // not show just skip
                return;
            }
            dom.layoutBefore();
        }
    });
    if (instance != null && !instance.isDestroy()) {
        domObject.calculateLayout(layoutContext);
    }
    domObject.traverseUpdateTree(new WXDomObject.Consumer() {

        @Override
        public void accept(WXDomObject dom) {
            if (instance == null || instance.isDestroy()) {
                return;
            }
            if (!dom.isShow()) {
                return;
            }
            if (dom.hasUpdate()) {
                dom.layoutAfter();
            }
        }
    });
}
Also used : WXSDKInstance(com.taobao.weex.WXSDKInstance) WXDomObject(com.taobao.weex.dom.WXDomObject)

Example 38 with WXDomObject

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

the class Layouts method setLayout.

/**
 * recursive set layout to component,
 * dom extra will also be updated from dom object to component.
 * if force is true, always set layout
 */
public static final void setLayout(WXComponent component, boolean force) {
    if (component.isWaste()) {
        return;
    }
    WXDomObject domObject = (WXDomObject) component.getDomObject();
    if (domObject.hasUpdate() || force) {
        domObject.markUpdateSeen();
        if (domObject.hasUpdate()) {
            domObject.markLayoutStateUpdated();
        }
        component.setLayout(component.getDomObject());
        if (component.getDomObject().getExtra() != null) {
            component.updateExtra(component.getDomObject().getExtra());
        }
    }
    if (component instanceof WXVContainer) {
        WXVContainer container = (WXVContainer) component;
        int count = container.getChildCount();
        for (int i = 0; i < count; ++i) {
            WXComponent child = container.getChild(i);
            if (child != null) {
                setLayout(child, force);
            }
        }
    }
}
Also used : WXVContainer(com.taobao.weex.ui.component.WXVContainer) WXDomObject(com.taobao.weex.dom.WXDomObject) WXComponent(com.taobao.weex.ui.component.WXComponent)

Example 39 with WXDomObject

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

the class Statements method doRenderBindingAttrsAndEvent.

/**
 * render dynamic binding attrs and bind them to component node.
 */
private static void doRenderBindingAttrsAndEvent(WXComponent component, WXDomObject domObject, CellRenderContext context) {
    ArrayStack stack = context.stack;
    component.setWaste(false);
    WXAttr attr = domObject.getAttrs();
    if (attr != null && attr.getBindingAttrs() != null && attr.getBindingAttrs().size() > 0) {
        ArrayMap<String, Object> bindAttrs = domObject.getAttrs().getBindingAttrs();
        Map<String, Object> dynamic = renderBindingAttrs(bindAttrs, stack);
        Set<Map.Entry<String, Object>> entries = dynamic.entrySet();
        /**
         * diff attrs, see attrs has update, remove none update attrs
         */
        Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            String key = entry.getKey();
            Object value = entry.getValue();
            Object oldValue = attr.get(key);
            if (value == null) {
                if (oldValue == null) {
                    iterator.remove();
                    continue;
                }
            } else {
                if (value.equals(oldValue)) {
                    iterator.remove();
                }
            }
        }
        if (dynamic.size() > 0) {
            if (dynamic.size() == 1 && dynamic.get(Constants.Name.SRC) != null && component instanceof WXImage) {
                // for image avoid dirty layout, only update src attrs
                domObject.getAttrs().put(Constants.Name.SRC, dynamic.get(Constants.Name.SRC));
            } else {
                // dirty layout
                domObject.updateAttr(dynamic);
            }
            if (isMainThread()) {
                component.updateProperties(dynamic);
            }
            dynamic.clear();
        }
    }
    WXStyle style = domObject.getStyles();
    if (style != null && style.getBindingStyle() != null) {
        ArrayMap<String, Object> bindStyle = style.getBindingStyle();
        Map<String, Object> dynamic = renderBindingAttrs(bindStyle, stack);
        Set<Map.Entry<String, Object>> entries = dynamic.entrySet();
        /**
         * diff attrs, see attrs has update, remove none update attrs
         */
        Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            String key = entry.getKey();
            Object value = entry.getValue();
            Object oldValue = style.get(key);
            if (value == null) {
                if (oldValue == null) {
                    iterator.remove();
                    continue;
                }
            } else {
                if (value.equals(oldValue)) {
                    iterator.remove();
                }
            }
        }
        if (dynamic.size() > 0) {
            domObject.updateStyle(dynamic, false);
            domObject.applyStyle(dynamic);
            if (isMainThread()) {
                component.updateProperties(dynamic);
            }
        }
    }
    WXEvent event = domObject.getEvents();
    if (event == null || event.getEventBindingArgs() == null) {
        return;
    }
    Set<Map.Entry<String, Object>> eventBindArgsEntrySet = event.getEventBindingArgs().entrySet();
    for (Map.Entry<String, Object> eventBindArgsEntry : eventBindArgsEntrySet) {
        List<Object> values = getBindingEventArgs(stack, eventBindArgsEntry.getValue());
        if (values != null) {
            event.putEventBindingArgsValue(eventBindArgsEntry.getKey(), values);
        }
    }
}
Also used : WXStyle(com.taobao.weex.dom.WXStyle) ArrayStack(com.taobao.weex.el.parse.ArrayStack) WXEvent(com.taobao.weex.dom.WXEvent) WXDomObject(com.taobao.weex.dom.WXDomObject) JSONObject(com.alibaba.fastjson.JSONObject) WXImage(com.taobao.weex.ui.component.WXImage) HashMap(java.util.HashMap) ArrayMap(android.support.v4.util.ArrayMap) Map(java.util.Map) WXAttr(com.taobao.weex.dom.WXAttr)

Example 40 with WXDomObject

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

the class Statements method doBindingAttrsEventAndRenderChildNode.

/**
 * bind attrs and doRender component child
 */
private static void doBindingAttrsEventAndRenderChildNode(WXComponent component, WXDomObject domObject, CellRenderContext context, List<WXComponent> updates) {
    WXAttr attr = component.getDomObject().getAttrs();
    /**
     * sub component supported, sub component new stack
     */
    ArrayStack stack = context.stack;
    if (attr.get(ELUtils.IS_COMPONENT_ROOT) != null && WXUtils.getBoolean(attr.get(ELUtils.IS_COMPONENT_ROOT), false)) {
        if (attr.get(ELUtils.COMPONENT_PROPS) != null && attr.get(ELUtils.COMPONENT_PROPS) instanceof JSONObject) {
            String compoentId = (String) attr.get(CellDataManager.SUB_COMPONENT_TEMPLATE_ID);
            Object compoentData = null;
            if (!TextUtils.isEmpty(compoentId)) {
                String virtualComponentId = context.getRenderState().getVirtualComponentIds().get(component.getViewTreeKey());
                if (virtualComponentId == null) {
                    // none virtualComponentId, create and do attach
                    virtualComponentId = CellDataManager.createVirtualComponentId(context.templateList.getRef(), component.getViewTreeKey(), context.templateList.getItemId(context.position));
                    Map<String, Object> props = renderProps((JSONObject) attr.get(ELUtils.COMPONENT_PROPS), context.stack);
                    EventResult result = WXBridgeManager.getInstance().syncCallJSEventWithResult(WXBridgeManager.METHD_COMPONENT_HOOK_SYNC, component.getInstanceId(), null, compoentId, VirtualComponentLifecycle.LIFECYCLE, VirtualComponentLifecycle.CREATE, new Object[] { virtualComponentId, props }, null);
                    if (result != null && result.getResult() != null && result.getResult() instanceof Map) {
                        props.putAll((Map<? extends String, ?>) result.getResult());
                    }
                    compoentData = props;
                    context.getRenderState().getVirtualComponentIds().put(component.getViewTreeKey(), virtualComponentId);
                    context.templateList.getCellDataManager().createVirtualComponentData(context.position, virtualComponentId, compoentData);
                    // create virtual componentId
                    WXBridgeManager.getInstance().asyncCallJSEventVoidResult(WXBridgeManager.METHD_COMPONENT_HOOK_SYNC, component.getInstanceId(), null, virtualComponentId, VirtualComponentLifecycle.LIFECYCLE, VirtualComponentLifecycle.ATTACH, null);
                } else {
                    // get virtual component data check has dirty's update
                    compoentData = context.getRenderState().getVirtualComponentDatas().get(virtualComponentId);
                    if (context.getRenderState().isHasDataUpdate()) {
                        Map<String, Object> props = renderProps((JSONObject) attr.get(ELUtils.COMPONENT_PROPS), context.stack);
                        EventResult result = WXBridgeManager.getInstance().syncCallJSEventWithResult(WXBridgeManager.METHD_COMPONENT_HOOK_SYNC, component.getInstanceId(), null, virtualComponentId, VirtualComponentLifecycle.LIFECYCLE, VirtualComponentLifecycle.SYNSTATE, new Object[] { virtualComponentId, props }, null);
                        if (result != null && result.getResult() != null && result.getResult() instanceof Map) {
                            props.putAll((Map<? extends String, ?>) result.getResult());
                            context.templateList.getCellDataManager().updateVirtualComponentData(virtualComponentId, props);
                            compoentData = props;
                        }
                    }
                }
                component.getDomObject().getAttrs().put(CellDataManager.VIRTUAL_COMPONENT_ID, virtualComponentId);
            } else {
                // stateless component
                Map<String, Object> props = renderProps((JSONObject) attr.get(ELUtils.COMPONENT_PROPS), context.stack);
                compoentData = props;
            }
            // virtual component is new context
            context.stack = new ArrayStack();
            if (compoentData != null) {
                context.stack.push(compoentData);
            }
        }
    }
    /**
     * check node is render only once, if render once, and has rendered, just return
     */
    Object vonce = null;
    if (attr.getStatement() != null) {
        vonce = attr.getStatement().get(WXStatement.WX_ONCE);
    }
    if (vonce != null) {
        ArrayStack onceStack = context.getRenderState().getOnceComponentStates().get(component.getViewTreeKey());
        if (onceStack == null) {
            onceStack = context.templateList.copyStack(context, stack);
            context.getRenderState().getOnceComponentStates().put(component.getViewTreeKey(), onceStack);
        }
        context.stack = onceStack;
    }
    doRenderBindingAttrsAndEvent(component, domObject, context);
    if (component instanceof WXVContainer) {
        if (!domObject.isShow()) {
            if (!(component instanceof WXCell)) {
                return;
            }
        }
        WXVContainer container = (WXVContainer) component;
        for (int k = 0; k < container.getChildCount(); ) {
            WXComponent next = container.getChild(k);
            k += doRenderComponent(next, context, updates);
        }
    }
    if (stack != context.stack) {
        context.stack = stack;
    }
}
Also used : WXVContainer(com.taobao.weex.ui.component.WXVContainer) EventResult(com.taobao.weex.bridge.EventResult) JSONObject(com.alibaba.fastjson.JSONObject) WXComponent(com.taobao.weex.ui.component.WXComponent) WXDomObject(com.taobao.weex.dom.WXDomObject) JSONObject(com.alibaba.fastjson.JSONObject) ArrayStack(com.taobao.weex.el.parse.ArrayStack) HashMap(java.util.HashMap) ArrayMap(android.support.v4.util.ArrayMap) Map(java.util.Map) WXCell(com.taobao.weex.ui.component.list.WXCell) 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