/** * Clear the mapping relationship between Reference and {@link WXDomObject}. The mapping info is * stored in {@link #mRegistry}. * * @param domObject */ private void clearRegistryForDom(WXDomObject domObject) { int count = domObject.childCount(); mRegistry.remove(domObject.getRef()); for (int i = count - 1; i >= 0; --i) { clearRegistryForDom(domObject.getChild(i)); } }
/** * Creating the mapping between Reference to {@link WXDomObject} and store the mapping in {@link * #mRegistry}. Then, parse and copy style from DOM to {@link com.taobao.weex.dom.flex.CSSNode}. * Finally, DOM's children are also added to {@link com.taobao.weex.dom.flex.CSSNode#mChildren} if * added is true. The above procedure will be done recursively. * * @param dom the original DOM Object * @param isAdd true for adding children of {@link WXDomObject} {@link * com.taobao.weex.dom.flex.CSSNode#mChildren} and parsing style, false for only parsing * style. */ private void transformStyle(WXDomObject dom, boolean isAdd) { if (dom == null) { return; } if (isAdd) { dom.young(); mRegistry.put(dom.getRef(), dom); } WXStyle style = dom.getStyles(); /** merge default styles * */ Map<String, String> defaults = dom.getDefaultStyle(); if (defaults != null) { Iterator<Map.Entry<String, String>> it = defaults.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, String> entry = it.next(); if (!style.containsKey(entry.getKey())) { style.put(entry.getKey(), entry.getValue()); } } } if (dom.getStyles().size() > 0) { dom.applyStyleToNode(); } int count = dom.childCount(); WXDomObject child; for (int i = 0; i < count; ++i) { child = dom.getChild(i); transformStyle(child, isAdd); } }
/** * Find fixed node and tell root dom * * @param obj */ void findFixed(WXDomObject obj) { WXDomObject rootDom = mRegistry.get(WXDomObject.ROOT); if (rootDom == null) { return; } if (obj.isFixed()) { rootDom.add2FixedDomList(obj.getRef()); } int childrenCount = obj.childCount(); if (childrenCount > 0) { for (int i = 0; i < childrenCount; i++) { findFixed(obj.getChild(i)); } } }
/** * Create a command object for adding a dom node to its parent in a specific location. If dom's * parent doesn't exist or the dom has been added in current {@link WXSDKInstance}, this method * will return. If the above request is met, then put the command object in the queue. * * @param dom the dom object in the form of JSONObject * @param parentRef parent to which the dom is added. * @param index the location of which the dom is added. */ void addDom(JSONObject dom, final String parentRef, final int index) { if (mDestroy) { return; } WXDomObject parent = mRegistry.get(parentRef); WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(mInstanceId); if (parent == null) { if (instance != null) { instance.commitUTStab(IWXUserTrackAdapter.DOM_MODULE, WXErrorCode.WX_ERR_DOM_ADDELEMENT); } return; } WXDomObject domObject = parseInner(dom); if (domObject == null || mRegistry.containsKey(domObject.getRef())) { if (WXEnvironment.isApkDebugable()) { WXLogUtils.e("[WXDomStatement] addDom error!!"); } if (instance != null) { instance.commitUTStab(IWXUserTrackAdapter.DOM_MODULE, WXErrorCode.WX_ERR_DOM_ADDELEMENT); } return; } findFixed(domObject); parent.add(domObject, index); transformStyle(domObject, true); // Create component in dom thread final WXComponent component = mWXRenderManager.createComponentOnDomThread(mInstanceId, domObject, parentRef, index); if (component == null) { // stop redner, some fatal happened. return; } AddDomInfo addDomInfo = new AddDomInfo(); addDomInfo.component = component; mAddDom.put(domObject.getRef(), addDomInfo); mNormalTasks.add( new IWXRenderTask() { @Override public void execute() { WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(mInstanceId); if (instance == null || instance.getContext() == null) { WXLogUtils.e("instance is null or instance is destroy!"); return; } try { mWXRenderManager.addComponent(mInstanceId, component, parentRef, index); } catch (Exception e) { WXLogUtils.e("add component failed.", e); } } @Override public String toString() { return "AddDom"; } }); animations.add( new Pair<String, Map<String, Object>>(domObject.getRef(), domObject.getStyles())); mDirty = true; if (instance != null) { instance.commitUTStab(IWXUserTrackAdapter.DOM_MODULE, WXErrorCode.WX_SUCCESS); } }
/** * Create command object for creating body according to the JSONObject. And put the command object * in the queue. * * @param element the jsonObject according to which to create command object. */ void createBody(JSONObject element) { if (mDestroy) { return; } WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(mInstanceId); if (element == null) { if (instance != null) { instance.commitUTStab(IWXUserTrackAdapter.DOM_MODULE, WXErrorCode.WX_ERR_DOM_CREATEBODY); } return; } WXDomObject domObject = parseInner(element); if (domObject == null) { return; } Map<String, Object> style = new HashMap<>(5); if (!domObject.getStyles().containsKey(Constants.Name.FLEX_DIRECTION)) { style.put(Constants.Name.FLEX_DIRECTION, "column"); } if (!domObject.getStyles().containsKey(Constants.Name.BACKGROUND_COLOR)) { style.put(Constants.Name.BACKGROUND_COLOR, "#ffffff"); } // If there is height or width in JS, then that value will override value here. if (!domObject.getStyles().containsKey(Constants.Name.WIDTH)) { style.put( Constants.Name.WIDTH, WXViewUtils.getWebPxByWidth(WXViewUtils.getWeexWidth(mInstanceId))); domObject.setModifyWidth(true); } if (!domObject.getStyles().containsKey(Constants.Name.HEIGHT)) { style.put( Constants.Name.HEIGHT, WXViewUtils.getWebPxByWidth(WXViewUtils.getWeexHeight(mInstanceId))); domObject.setModifyHeight(true); } WXDomObject.prepareRoot(domObject); domObject.updateStyle(style); transformStyle(domObject, true); try { final WXComponent component = mWXRenderManager.createBodyOnDomThread(mInstanceId, domObject); AddDomInfo addDomInfo = new AddDomInfo(); addDomInfo.component = component; mAddDom.put(domObject.getRef(), addDomInfo); mNormalTasks.add( new IWXRenderTask() { @Override public void execute() { WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(mInstanceId); if (instance == null || instance.getContext() == null) { WXLogUtils.e("instance is null or instance is destroy!"); return; } try { mWXRenderManager.createBody(mInstanceId, component); } catch (Exception e) { WXLogUtils.e("create body failed.", e); } } @Override public String toString() { return "createBody"; } }); animations.add( new Pair<String, Map<String, Object>>(domObject.getRef(), domObject.getStyles())); mDirty = true; if (instance != null) { instance.commitUTStab(IWXUserTrackAdapter.DOM_MODULE, WXErrorCode.WX_SUCCESS); } } catch (Exception e) { WXLogUtils.e("create body in dom thread failed." + e.getMessage()); } }