PageSourceSearch

https://www.cnphi-rcrsp.ca/cnphi/faces/javax.faces.resource/template/js/focusManager.js

js cnphi-rcrsp.ca collected 2026-10-04 01:57:35 UTC 3,299 bytes, 107 lines download raw bytes

1//Utility to manage focus. Developers can specify an id, or expression to set focus to on following
2//the next post or ajax request. If focus has been lost, generally because the request refreshed the 
3//field currently focused on, the default will be to reset to the last focused element. Stores key 
4//val pairs in HTML sessionStorage in order to retain these values across posts. Also stores these key/values
5//on the FocusManager object in case sessionStorage is unavailable, to retain as much of this functionality as possible.
6var FocusManager = {};
7
8//detect if sessionStorage is available in the most foolproof manner, to avoid js errors, as done by Modernizer.
9var hasStorage = (function() {
10	try {
11		sessionStorage.setItem('foo', 'bar');
12		sessionStorage.removeItem('foo');
13		return true;
14	} catch (exception) {
15		return false;
16	}
17}());
18
19//Store a given key value pair in session storage and FocusManager object, so that it can be retrieved after a post.
20FocusManager.setItem = function(key, val){
21	if(hasStorage){
22		sessionStorage.setItem(key, val);
23	}
24	
25	FocusManager[key] = val;
26}
27
28//Retrieve a given key value pair from session storage or FocusManager object after a post or ajax request.
29FocusManager.getItem = function(key){
30	var item = FocusManager[key];
31	
32	if(hasStorage){
33		item = sessionStorage.getItem(key);
34	}
35
36	return item;
37}
38
39//Remove a given value from session storage or FocusManager object, by specifying its key.
40FocusManager.removeItem = function(key){
41	if(hasStorage){
42		sessionStorage.removeItem(key);
43	}
44	
45	FocusManager[key] = null;
46}
47
48//to set focus after ajax or form submission to a specified id, or partial ending portion of an id
49FocusManager.setFocus = function(id) {
50	FocusManager.setItem('focusExp', "[id$='" + id + "']");
51};
52
53//developer defined expression in string form
54FocusManager.setFocusExp = function(exp) {
55	FocusManager.setItem('focusExp', exp);
56};
57
58//default is to set focus back to last focused element
59FocusManager.getDefault = function(){
60	var focused = $(document.activeElement);
61	if(focused.length !== 0 && focused.attr('id')) {
62		FocusManager.defaultFocus = focused.attr('id');
63	}
64};
65
66FocusManager.focusListener = function(data){
67	//reset focus to chosen id or previously focused id
68	if(data.status === 'begin') {
69		FocusManager.getDefault();
70	}
71	//try to get a the very last element focused before the ajax request
72	if(data.status === 'complete') {
73		FocusManager.getDefault();
74	}
75	if(data.status === 'success') {
76		if($(":focus").length !== 0){
77			FocusManager.defaultFocus = null;
78		}
79		
80		FocusManager.focusComplete();
81	}
82};
83
84FocusManager.focusComplete = function(){
85	
86	var focusExp = FocusManager.getItem('focusExp'),
87		defaultFocus = FocusManager.defaultFocus;
88	
89	//try matching by expression
90	if(focusExp){
91		$(focusExp).focus();
92	}
93	//fallback to previously selected element
94	if(!focusExp && defaultFocus){
95		var focusTarget = $("#" + wb.jqEscape(defaultFocus))
96		if(!$(focusTarget).hasClass('fm-ignore'))	
97			focusTarget.focus();
98	}
99	
100	FocusManager.removeItem('focusExp');
101	FocusManager.defaultFocus = null;
102};
103
104$(document).ready(function(){
105	FocusManager.focusComplete();
106	jsf.ajax.addOnEvent(FocusManager.focusListener);
107});	

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.