Ejemplo n.º 1
0
  /**
   * 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());
    }
  }