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

com.dragome.web.html.dom.Window Maven / Gradle / Ivy

There is a newer version: 0.96-beta4
Show newest version
package com.dragome.web.html.dom;

import java.io.Serializable;

import org.w3c.dom.Element;
import org.w3c.dom.events.EventListener;

import com.dragome.commons.javascript.ScriptHelper;
import com.dragome.helpers.DragomeEntityManager;
import com.dragome.services.WebServiceLocator;
import com.dragome.services.interfaces.AsyncCallback;
import com.dragome.web.dispatcher.EventDispatcherImpl;
import com.dragome.web.services.RequestExecutorImpl;

public class Window
{
	private static Window instance;

	public Window()
	{
	}

	public static Window getInstance()
	{
		if (instance == null)
			instance= new Window();

		return instance;
	}

	public int getClientWidth()
	{
		return ScriptHelper.evalInt("window.innerWidth", this);
	}

	public int getClientHeight()
	{
		return ScriptHelper.evalInt("window.innerHeight", this);
	}

	public void requestAnimationFrame(Runnable aRunnable)
	{
		Runnable runnableForDebugging= wrapRunnableForDebugging(aRunnable);
		ScriptHelper.put("ra", runnableForDebugging, this);
		ScriptHelper.evalNoResult("requestAnimationFrame(function(time) {ra.$run$void();})", this);
	}

	public void addEventListener(EventListener aEventListener, String... aEvent)
	{
		Element theElement= WebServiceLocator.getInstance().getDomHandler().getElementBySelector("body");
		EventDispatcherImpl.setEventListener(theElement, aEventListener, aEvent);
	}

	public void onResize(Runnable aRunnable)
	{
		Runnable runnableForDebugging= wrapRunnableForDebugging(aRunnable);
		ScriptHelper.put("ra", runnableForDebugging, this);
		ScriptHelper.evalNoResult("window.addEventListener('resize', function() {ra.$run$void();})", this);
	}

	public void onHashChange(Runnable aRunnable)
	{
		Runnable runnableForDebugging= wrapRunnableForDebugging(aRunnable);
		ScriptHelper.put("ra", runnableForDebugging, this);
		ScriptHelper.evalNoResult("window.onhashchange= function() {ra.$run$void();}", this);
	}

	public static Runnable wrapRunnableForDebugging(final Runnable runnable)
	{
		AsyncCallback asyncCallback= new AsyncCallback()
		{
			public void onSuccess(String result)
			{
				runnable.run();
			}

			public void onError()
			{
			}
		};

		AsyncCallback wrappedCallback= RequestExecutorImpl.wrapCallback(Serializable.class, asyncCallback);

		Runnable runnable2= new Runnable()
		{
			public void run()
			{
				ScriptHelper.eval("dragomeJs['callback_'+this.javaId].$onSuccess___java_lang_Object$void(new String())", this);
			}
		};

		ScriptHelper.put("callback", wrappedCallback, null);
		ScriptHelper.put("javaId", DragomeEntityManager.getEntityId(runnable2), null);
		ScriptHelper.put("runnable2", runnable2, null);
		ScriptHelper.eval("runnable2.javaId= javaId", null);
		ScriptHelper.eval("dragomeJs['callback_'+javaId]= callback", null);
		return runnable2;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy