<?php

/**
 * Sets focus on given form element
 *
 * @version DR1.7
 */
class FocusFilter implements InterceptingFilter {



	protected static $focus;



	/**
	 * Sets focus on given view
	 *
	 * @param View, view to be focused
	 */
	public static function setFocus(View $v) {
		if ($id = $v->getId()) {
			self::$focus = $id;
		}
	}



	/**
	 * Returns if focus is already set
	 */
	public function isFocusSet() {
		return (boolean)self::$focus;
	}



	public function preProcess(RequestContext $request, ResponseContext $response) {

	}



	/**
	 * Adds focus procedure in Javascript to response
	 */
	public function postProcess(RequestContext $request, ResponseContext $response) {
		if ($id = self::$focus) {
			$response->eval("document.getElementById('$id').focus()");
		}
	}



} // class FocusFilter

?>