Search in sources :

Example 6 with WXAttr

use of com.taobao.weex.dom.WXAttr in project WeexErosFramework by bmfe.

the class HookWXText method updateFontSize.

private void updateFontSize() {
    if (getDomObject() != null && getDomObject().getStyles().get(Constants.Name.FONT_SIZE) == null) {
        WXStyle s = getDomObject().getStyles();
        s.put(Constants.Name.FONT_SIZE, 30);
        updateStyle(s);
        return;
    }
    if (mChangeFontSize == null) {
        return;
    }
    WXStyle styles = null;
    WXAttr attrs = null;
    if (getDomObject() != null) {
        styles = getDomObject().getStyles();
        attrs = getDomObject().getAttrs();
        if ((styles != null && "iconfont".equals(styles.get("fontFamily"))) || (attrs != null && attrs.get("changeFont") != null && !Boolean.valueOf((String) attrs.get("changeFont")))) {
            return;
        }
    }
    float scale = 0;
    // 获取fontScale字段
    if (attrs != null && attrs.get("fontScale") != null) {
        float fontScale = Float.valueOf((String) attrs.get("fontScale"));
        mCurrentScale = fontScale / mCurrentScale;
    }
    if (mChangeFontSize.equals(mCurrentFontSize) && mCurrentScale == 1) {
        return;
    }
    // 获取scale字段 在标准字体下不产生变化
    if (attrs != null && attrs.get("scale") != null && !(scale > 0)) {
        scale = Float.valueOf((String) attrs.get("scale"));
        float change = getFixedEnlarge(mChangeFontSize, scale);
        float current = getFixedEnlarge(mCurrentFontSize, scale);
        scale = change / current;
    }
    // 根据全局字体配置设置字体大小
    if (!(scale > 0)) {
        float current = getEnlarge(mCurrentFontSize);
        float change = getEnlarge(mChangeFontSize);
        scale = change / current * mCurrentScale;
    }
    if (getDomObject() != null && getDomObject().getStyles() != null) {
        WXStyle wxStyle = getDomObject().getStyles();
        Object object = wxStyle.get("fontSize");
        if (object instanceof Integer) {
            int fontSize = (int) object;
            int changeFontSize = Math.round(fontSize * (scale));
            wxStyle.put("fontSize", changeFontSize);
        }
        // 设置lineHeight
        Object lineHeight = wxStyle.get("lineHeight");
        if (lineHeight instanceof Integer) {
            int target = (int) lineHeight;
            wxStyle.put("lineHeight", Math.round(target * scale));
        }
        updateStyle(wxStyle);
    }
    mCurrentFontSize = mChangeFontSize;
}
Also used : WXStyle(com.taobao.weex.dom.WXStyle) WXDomObject(com.taobao.weex.dom.WXDomObject) JSONObject(com.alibaba.fastjson.JSONObject) WXAttr(com.taobao.weex.dom.WXAttr)

Example 7 with WXAttr

use of com.taobao.weex.dom.WXAttr 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 8 with WXAttr

use of com.taobao.weex.dom.WXAttr 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)

Example 9 with WXAttr

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

the class Statements method getComponentId.

public static String getComponentId(WXComponent component) {
    if (component instanceof WXCell || component == null) {
        return null;
    }
    WXDomObject domObject = (WXDomObject) component.getDomObject();
    WXAttr attr = domObject.getAttrs();
    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) {
            Object componentId = attr.get(CellDataManager.VIRTUAL_COMPONENT_ID);
            if (componentId == null) {
                return null;
            }
            return componentId.toString();
        }
    }
    return getComponentId(component.getParent());
}
Also used : WXDomObject(com.taobao.weex.dom.WXDomObject) JSONObject(com.alibaba.fastjson.JSONObject) WXDomObject(com.taobao.weex.dom.WXDomObject) JSONObject(com.alibaba.fastjson.JSONObject) WXCell(com.taobao.weex.ui.component.list.WXCell) WXAttr(com.taobao.weex.dom.WXAttr)

Example 10 with WXAttr

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

the class FlatGUIContext method checkComponent.

private boolean checkComponent(@NonNull WXComponent component) {
    boolean ret = false;
    ImmutableDomObject domObject = component.getDomObject();
    if (domObject != null) {
        WXStyle style = domObject.getStyles();
        WXAttr attr = domObject.getAttrs();
        if (style.containsKey(Name.OPACITY) || style.containsKey(Name.TRANSFORM) || style.containsKey(Name.VISIBILITY) || attr.containsKey(Name.ELEVATION) || attr.containsKey(Name.ARIA_HIDDEN) || attr.containsKey(Name.ARIA_LABEL) || attr.containsKey(WXComponent.PROP_FIXED_SIZE) || attr.containsKey(Name.DISABLED) || style.isFixed() || style.isSticky() || !style.getPesudoStyles().isEmpty() || domObject.getEvents().size() > 0) {
            ret = true;
        }
    }
    return ret;
}
Also used : WXStyle(com.taobao.weex.dom.WXStyle) ImmutableDomObject(com.taobao.weex.dom.ImmutableDomObject) WXAttr(com.taobao.weex.dom.WXAttr)

Aggregations

WXAttr (com.taobao.weex.dom.WXAttr)10 WXDomObject (com.taobao.weex.dom.WXDomObject)8 JSONObject (com.alibaba.fastjson.JSONObject)6 WXStyle (com.taobao.weex.dom.WXStyle)5 ArrayMap (android.support.v4.util.ArrayMap)3 WXComponent (com.taobao.weex.ui.component.WXComponent)3 WXCell (com.taobao.weex.ui.component.list.WXCell)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Point (android.graphics.Point)2 RecyclerView (android.support.v7.widget.RecyclerView)2 View (android.view.View)2 ImmutableDomObject (com.taobao.weex.dom.ImmutableDomObject)2 ArrayStack (com.taobao.weex.el.parse.ArrayStack)2 WXVContainer (com.taobao.weex.ui.component.WXVContainer)2 WXRecyclerView (com.taobao.weex.ui.view.listview.WXRecyclerView)2 BounceRecyclerView (com.taobao.weex.ui.view.refresh.wrapper.BounceRecyclerView)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 TargetApi (android.annotation.TargetApi)1