Commit da584506e562951f64faae8ecafc09abbbc53f88

Authored by bernard
1 parent c02520de

Modified JavaScript so that focus can happen even after dom is loaded.

1 1 if (!window.Richfaces) window.Richfaces = {};
2 2
3   -Richfaces.FocusManager = (function(){
  3 +Richfaces.FocusManager = (function() {
4 4
5   - var m_focus;
6   - var m_priority = 999999;
7   - var eventAttached = false;
  5 + var m_focus;
  6 + var m_priority = 999999;
  7 + var m_domLoaded = false;
8 8
9   - var attachEvent = function() {
10   - if(!eventAttached) {
11   - document.observe('dom:loaded',function(){
12   - var element = $(m_focus);
13   - element.focus();
14   - element.select(element);
15   - Richfaces.FocusManager.clearFocus();
16   - });
17   - }
18   - eventAttached = true;
19   - };
  9 + var focus = function() {
  10 + if (m_focus == null) {
  11 + return;
  12 + }
  13 + try {
  14 + var element = $(m_focus);
  15 + if (element != null) {
  16 + element.focus();
  17 + element.select(element);
  18 + }
  19 + } finally {
  20 + Richfaces.FocusManager.clearFocus();
  21 + }
  22 + };
20 23
21   - return {
22   - getFocus : function() {
23   - return m_focus;
24   - },
25   - setFocus : function(id,priority) {
26   - if(priority == null) {
27   - priority = 99999;
28   - }
29   - if(m_focus == null || priority < m_priority) {
30   - m_focus = id;
31   - m_priority = priority == null ? 0 : priority;
32   - attachEvent();
33   - }
34   - },
35   - clearFocus : function() {
36   - m_focus = null;
37   - m_priority = 999999;
38   - }
39   - };
40   - })();
  24 + document.observe('dom:loaded', function() {
  25 + focus();
  26 + m_domLoaded = true;
  27 + });
  28 +
  29 + return {
  30 + getFocus : function() {
  31 + return m_focus;
  32 + },
  33 + setFocus : function(id, priority) {
  34 + if (priority == null) {
  35 + priority = 99999;
  36 + }
  37 + if (m_focus == null || priority < m_priority) {
  38 + m_focus = id;
  39 + m_priority = priority == null ? 0 : priority;
  40 + if (m_domLoaded) {
  41 + focus();
  42 + }
  43 + }
  44 + },
  45 + clearFocus : function() {
  46 + m_focus = null;
  47 + m_priority = 999999;
  48 + }
  49 + };
  50 +})();
... ...
Please register or login to post a comment