All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.terapico.caf.wxappviewcomponent.BaseWxappViewComponent Maven / Gradle / Ivy

package com.terapico.caf.wxappviewcomponent;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.terapico.caf.viewcomponent.BaseViewComponent;

public abstract class BaseWxappViewComponent extends BaseViewComponent{
	protected String id;
	protected Map className;
	protected List kids;
	protected Map customStyle;
	protected Object flex;
	protected String linkToUrl;
	
	public Map getCustomStyle() {
		return customStyle;
	}

	
	public Object getFlex() {
		return flex;
	}


	public void setFlex(Object flex) {
		this.flex = flex;
	}


	public String getLinkToUrl() {
		return linkToUrl;
	}


	public void setLinkToUrl(String linkToUrl) {
		this.linkToUrl = linkToUrl;
	}


	public void setCustomStyle(Map customStyle) {
		this.customStyle = customStyle;
	}


	public List getKids() {
		return kids;
	}


	public void setKids(List kids) {
		this.kids = kids;
	}


	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public Map getClassName() {
		return className;
	}
	public void setClassName(Map className) {
		this.className = className;
	}
	
	public T withId(String id) {
		this.setId(id);
		return (T) this;
	}
	public T withClassName(String clazzName) {
		ensureClassName();
		this.className.put(clazzName, true);
		return (T) this;
	}
	public T withCustomStyle(String name, Object value) {
		ensuerCustomStyle();
		customStyle.put(name, value);
		return (T) this;
	}
	public T withFlex(Object value) {
		this.setFlex(value);
		return (T) this;
	}
	protected void ensuerCustomStyle() {
		if (customStyle == null) {
			customStyle = new HashMap<>();
		}
	}


	public  T kid(BaseWxappViewComponent kid) {
		ensureKids();
		kids.add(kid);
		if (kid.getId() == null) {
			kid.setId(String.format("%s-%04d", kid.getType(), kids.size()));
		}
		return (T) this;
	}
	
	protected void ensureKids() {
		if (kids == null) {
			kids = new ArrayList<>();
		}
	}


	protected void ensureClassName() {
		if (className == null) {
			className = new HashMap<>();
		}
	}

	@Override
	public Map toMap(){
		Map resultMap = new HashMap<>();
		putInMap(resultMap, "id", getId());
		putInMap(resultMap, "className", getClassName());
		putInMap(resultMap, "linkToUrl", getLinkToUrl());
		putInMap(resultMap, "type", getType());
		putInMap(resultMap, "flex", getFlex());
		putInMap(resultMap, "customStyle", getCustomStyle());
		mapFieldsToMap(resultMap);
		if (kids != null) {
			List> kidsList = new ArrayList<>(kids.size());
			for(BaseWxappViewComponent kid: kids) {
				Map kidMap = kid.toMap();
				kidsList.add(kidMap);
			}
			putInMap(resultMap, "kids", kidsList);
		}
		return resultMap;
	}


	public abstract String getType();
	protected abstract void mapFieldsToMap(Map resultMap) ;


	protected void putInMap(Map resultMap, String name, Object value) {
		if (value == null) {
			return;
		}
		if (value instanceof Collection) {
			if (((Collection) value).isEmpty()) {
				return;
			}
		}
		if (value instanceof Map) {
			if (((Map) value).isEmpty()) {
				return;
			}
		}
		resultMap.put(name, value);
	}


	public T flexFull() {
		this.withClassName("flex-full");
		return (T)this;
	}
	public T flexRowCenter() {
		this.withClassName("flex-row-center");
		return (T)this;
	}
	public T flexColumnCenter() {
		this.withClassName("flex-column-center");
		return (T)this;
	}
	
	
}