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

net.abstractfactory.plum.view.web.WebPageView Maven / Gradle / Ivy

There is a newer version: 0.3.21
Show newest version
package net.abstractfactory.plum.view.web;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import net.abstractfactory.plum.view.component.Component;
import net.abstractfactory.plum.view.web.component.WebComponent;
import net.abstractfactory.plum.view.web.component.input.WebCheckBox;

import org.jsoup.nodes.DataNode;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class WebPageView {
	static final String START_JS = "_plum_start_js";

	// index for all web components
	private Map webComponentMap = new HashMap();
	// index for all check boxes
	private Map webCheckBoxMap = new HashMap();

	private Map componentToWebComponentMap = new HashMap();

	private Map nodeHashes = new HashMap();

	private WebComponent rootContentWebComponent;

	private Document doc;
	private String sessionId;

	Element head;
	Element body;
	Element form;
	Element startJs;
	/**
	 * used for view pushing
	 */
	private String version;

	public WebPageView(Document document, String pushTopic) {
		this.doc = document;
		this.sessionId = sessionId;

		head = doc.getElementsByTag("head").get(0);
		body = doc.getElementsByTag("body").get(0);
		form = body.getElementsByTag("form").get(0);
		startJs = body.getElementById(START_JS);

		// body.empty();

		// remove all except scripts
		for (Element e : body.children()) {
			if (!e.tagName().equalsIgnoreCase("script")
					&& !e.attr("keep").equals("true"))
				e.remove();
		}

		// replace JS code
		String jsData = startJs.data().replaceAll("#topic#", pushTopic);
		startJs.empty();
		startJs.appendChild(new DataNode(jsData, ""));

	}

	public Map getWebComponentMap() {
		return webComponentMap;
	}

	public Map getWebCheckBoxMap() {
		return webCheckBoxMap;
	}

	public Map getComponentToWebComponentMap() {
		return componentToWebComponentMap;
	}

	public Map getNodeHashes() {
		return nodeHashes;
	}

	public void setRootContentWebComponent(
			WebComponent rootContentWebComponent, String title) {
		this.rootContentWebComponent = rootContentWebComponent;

		if (title != null)
			doc.title(title);

		// Element form = formTemplate.clone();
		body.prependChild(form);
		form.prependChild(rootContentWebComponent.getHtmlOuterElement());
	}

	public WebComponent getRootContentWebComponent() {
		return rootContentWebComponent;
	}

	public Document getDoc() {
		return doc;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;

		form.getElementById("_plum_view_version").val(version);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy