From da584506e562951f64faae8ecafc09abbbc53f88 Mon Sep 17 00:00:00 2001 From: Bernard Labno Date: Mon, 15 Nov 2010 09:06:29 +0000 Subject: [PATCH] Modified JavaScript so that focus can happen even after dom is loaded. --- src/main/resources/org/richfaces/renderkit/html/scripts/focus.js | 80 +++++++++++++++++++++++++++++++++++++++++++++----------------------------------- 1 file changed, 45 insertions(+), 35 deletions(-) diff --git a/src/main/resources/org/richfaces/renderkit/html/scripts/focus.js b/src/main/resources/org/richfaces/renderkit/html/scripts/focus.js index 4cce423..4b94291 100644 --- a/src/main/resources/org/richfaces/renderkit/html/scripts/focus.js +++ b/src/main/resources/org/richfaces/renderkit/html/scripts/focus.js @@ -1,40 +1,50 @@ if (!window.Richfaces) window.Richfaces = {}; -Richfaces.FocusManager = (function(){ +Richfaces.FocusManager = (function() { - var m_focus; - var m_priority = 999999; - var eventAttached = false; + var m_focus; + var m_priority = 999999; + var m_domLoaded = false; - var attachEvent = function() { - if(!eventAttached) { - document.observe('dom:loaded',function(){ - var element = $(m_focus); - element.focus(); - element.select(element); - Richfaces.FocusManager.clearFocus(); - }); - } - eventAttached = true; - }; + var focus = function() { + if (m_focus == null) { + return; + } + try { + var element = $(m_focus); + if (element != null) { + element.focus(); + element.select(element); + } + } finally { + Richfaces.FocusManager.clearFocus(); + } + }; - return { - getFocus : function() { - return m_focus; - }, - setFocus : function(id,priority) { - if(priority == null) { - priority = 99999; - } - if(m_focus == null || priority < m_priority) { - m_focus = id; - m_priority = priority == null ? 0 : priority; - attachEvent(); - } - }, - clearFocus : function() { - m_focus = null; - m_priority = 999999; - } - }; - })(); + document.observe('dom:loaded', function() { + focus(); + m_domLoaded = true; + }); + + return { + getFocus : function() { + return m_focus; + }, + setFocus : function(id, priority) { + if (priority == null) { + priority = 99999; + } + if (m_focus == null || priority < m_priority) { + m_focus = id; + m_priority = priority == null ? 0 : priority; + if (m_domLoaded) { + focus(); + } + } + }, + clearFocus : function() { + m_focus = null; + m_priority = 999999; + } + }; +})(); -- libgit2 0.24.0