/* Event binding for browser using duktape and libdom
 *
 * Copyright 2015 Vincent Sanders <vince@netsurf-browser.org>
 * Copyright 2015 Daniel Silverstone <dsilvers@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * Released under the terms of the MIT License,
 *         http://www.opensource.org/licenses/mit-license
 */

class Event {
      private dom_event *evt;
};

init Event (struct dom_event *evt)
%{
	priv->evt = evt;
	dom_event_ref(evt);
%}

fini Event ()
%{
	dom_event_unref(priv->evt);
%}

/* Note: many of these could be automatics once nsgenbind gets there. */

getter Event::type ()
%{
	dom_string *ret;
	dom_exception exc;

	exc = dom_event_get_type(priv->evt, &ret);
	if (exc != DOM_NO_ERR) return 0;
	if (ret == NULL) {
		duk_push_lstring(ctx, "", 0);
	} else {
		duk_push_lstring(ctx, dom_string_data(ret),
				dom_string_length(ret));
		dom_string_unref(ret);
	}

	return 1;
%}

getter Event::target ()
%{
	/** @todo Decide HTF this works wrt. Window as an event target */
	dom_node *et;
	dom_exception exc;

	exc = dom_event_get_target(priv->evt, &et);
	if (exc != DOM_NO_ERR) return 0;

	dukky_push_node(ctx, et);
	return 1;
%}

getter Event::currentTarget ()
%{
	/** @todo Decide HTF this works wrt. Window as an event target */
	dom_node *et;
	dom_exception exc;

	exc = dom_event_get_current_target(priv->evt, &et);
	if (exc != DOM_NO_ERR) return 0;

	dukky_push_node(ctx, et);
	return 1;
%}

getter Event::eventPhase ()
%{
	dom_exception exc;
	dom_event_flow_phase phase;

	exc = dom_event_get_event_phase(priv->evt, &phase);
	if (exc != DOM_NO_ERR) return 0;

	duk_push_uint(ctx, phase);
	return 1;
%}

method Event::stopPropagation ()
%{
	dom_exception exc;

	exc = dom_event_stop_propagation(priv->evt);
	if (exc != DOM_NO_ERR) return 0;

	return 0;
%}

method Event::stopImmediatePropagation ()
%{
	dom_exception exc;

	exc = dom_event_stop_immediate_propagation(priv->evt);
	if (exc != DOM_NO_ERR) return 0;

	return 0;
%}

getter Event::bubbles ()
%{
	dom_exception exc;
	bool ret;

	exc = dom_event_get_bubbles(priv->evt, &ret);
	if (exc != DOM_NO_ERR) return 0;

	duk_push_boolean(ctx, ret);
	return 1;
%}

getter Event::cancelable ()
%{
	dom_exception exc;
	bool ret;

	exc = dom_event_get_cancelable(priv->evt, &ret);
	if (exc != DOM_NO_ERR) return 0;

	duk_push_boolean(ctx, ret);
	return 1;
%}

method Event::preventDefault ()
%{
	dom_exception exc;

	exc = dom_event_prevent_default(priv->evt);
	if (exc != DOM_NO_ERR) return 0;

	return 0;
%}

getter Event::defaultPrevented ()
%{
	dom_exception exc;
	bool ret;

	exc = dom_event_is_default_prevented(priv->evt, &ret);
	if (exc != DOM_NO_ERR) return 0;

	duk_push_boolean(ctx, ret);
	return 1;
%}

