summaryrefslogtreecommitdiff
path: root/framework/Web/Javascripts/base/focus.js
blob: 6c1359cdae475a593744e2e13348095bd655aad0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
Prado.Focus = Class.create();

Prado.Focus.setFocus = function(id)
{
	var target = document.getElementById ? document.getElementById(id) : document.all[id];
	if(target && !Prado.Focus.canFocusOn(target))
	{
		target = Prado.Focus.findTarget(target);
	}
	if(target)
	{
        try
		{
            target.focus();
			target.scrollIntoView(false);
            if (window.__smartNav)
			{
				window.__smartNav.ae = target.id;
			}
		}
        catch (e)
		{
		}
	}
}

Prado.Focus.canFocusOn = function(element)
{
	if(!element || !(element.tagName))
		return false;
	var tagName = element.tagName.toLowerCase();
	return !element.disabled && (!element.type || element.type.toLowerCase() != "hidden") && Prado.Focus.isFocusableTag(tagName) && Prado.Focus.isVisible(element);
}

Prado.Focus.isFocusableTag = function(tagName)
{
	return (tagName == "input" || tagName == "textarea" || tagName == "select" || tagName == "button" || tagName == "a");
}


Prado.Focus.findTarget = function(element)
{
	if(!element || !(element.tagName))
	{
		return null;
	}
	var tagName = element.tagName.toLowerCase();
	if (tagName == "undefined")
	{
		return null;
	}
	var children = element.childNodes;
	if (children)
	{
		for(var i=0;i<children.length;i++)
		{
			try
			{
				if(Prado.Focus.canFocusOn(children[i]))
				{
					return children[i];
				}
				else
				{
					var target = Prado.Focus.findTarget(children[i]);
					if(target)
					{
						return target;
					}
				}
			}
			catch (e)
			{
			}
		}
	}
	return null;
}

Prado.Focus.isVisible = function(element)
{
	var current = element;
	while((typeof(current) != "undefined") && (current != null))
	{
		if(current.disabled || (typeof(current.style) != "undefined" && ((typeof(current.style.display) != "undefined" && current.style.display == "none") || (typeof(current.style.visibility) != "undefined" && current.style.visibility == "hidden") )))
		{
			return false;
		}
		if(typeof(current.parentNode) != "undefined" &&	current.parentNode != null && current.parentNode != current && current.parentNode.tagName.toLowerCase() != "body")
		{
			current = current.parentNode;
		}
		else
		{
			return true;
		}
	}
    return true;
}