File: modal.js

package info (click to toggle)
webcit 8.24-dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,888 kB
  • ctags: 4,214
  • sloc: ansic: 33,336; sh: 4,468; makefile: 340; xml: 90; sed: 9
file content (89 lines) | stat: -rw-r--r-- 2,028 bytes parent folder | download | duplicates (3)
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
var focusedElement = null;

var modal = document.getElementById('modal');
var dialog = document.getElementById('dialog');
var body = document.getElementById('global');
var html = document.documentElement;

var modalShowing = (html.className === 'modal');


// Have to hack for Safari, due to poor support for the focus() function.
try {
	var isSafari = window.navigator.vendor.match(/Apple/);
} catch (ex) {
	isSafari = false;
}
if ( isSafari ) {
	var dialogFocuser = document.createElement('a');
	dialogFocuser.href="#";
	dialogFocuser.style.display='block';
	dialogFocuser.style.height='0';
	dialogFocuser.style.width='0';
	dialogFocuser.style.position = 'absolute';
	dialog.insertBefore(dialogFocuser, dialog.firstChild);
} else {
	dialogFocuser = dialog;
}

window.onunload = function () {
	dialogFocuser = focusedElement = modal = dialog = body = html = null;
};

var onfocus = function (e) {
	e = e || window.event;
	var el = e.target || e.srcElement;

	// save the last focused element when the modal is hidden.
	if ( !modalShowing ) {
		focusedElement = el;
		return;
	}
	
	// if we're focusing the dialog, then just clear the blurring flag.
	// else, focus the dialog and prevent the other event.
	var p = el.parentNode;
	while ( p && p.parentNode && p !== dialog ) {
		p=p.parentNode;
	}
	if ( p !== dialog ) {
		dialogFocuser.focus();
	}
};



var onblur = function () {
	if ( !modalShowing ) {
		focusedElement = body;
	}
};

html.onfocus = html.onfocusin = onfocus;
html.onblur = html.onfocusout = onblur;
if ( isSafari ) {
	html.addEventListener('DOMFocusIn',onfocus);
	html.addEventListener('DOMFocusOut',onblur);
}
// focus and blur events are tricky to bubble.
// need to do some special stuff to handle MSIE.


function toggleModal (b) {

	if (modalShowing && b) return;
	if (!modalShowing && !b) return;
	
	html.className=modalShowing?'':'modal';

	modalShowing = !modalShowing;

	if (modalShowing) {
		dialog.focus();
	} else if (focusedElement) {
		try {
			focusedElement.focus();
		} catch(ex) {}
	}
	
};