Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Contributor
Wongadob
Posts: 40
Registered: ‎05-12-2012
My Device: 9800
My Carrier: Virgin mobile

Can't get focus.setFocus working in certain instance

 

I have an issue that has had me pulling my hair out all day.

 

I have this piece of code (note that I have tried to set focus using blackberry.focus.setFocus and via standard Javascript AND jquery methods - non are setting focus of the item to the correct id. The id exists beacuse I console out the id and it is as it should be. My code

		if(document.getElementById(lastFeedHeader))
		{
			console.log ("Look the ID exists. Here is the element",document.getElementById(lastFeedHeader));
//			blackberry.focus.setFocus(lastFeedHeader);
			var myid="#"+lastFeedHeader;
			$(myid).focus();
//			document.getElementById(lastFeedHeader).focus();
		}
		else
		{
			console.log("it would appear the id ", lastFeedHeader, " Does not yet exist on this page")
		}

		console.log("Pre-cached - I did get here and at this point ",blackberry.focus.getFocus()," should be in focus");

 

When it comes to the output blackberry.focus.getFocus()  it shows 'null' yet it is displaying the fact that the Id does exist as I say the correct HTML is shown. Initiallly the code was

 

blackberry.focus.setFocus(lastFeedHeader);

console.log(

"Pre-cached - I did get here and at this point ",blackberry.focus.getFocus()," should be in focus");

 

I added all the rest as debug code, but the bottom line is why is it not setting the focus?



Please use plain text.
Contributor
Wongadob
Posts: 40
Registered: ‎05-12-2012
My Device: 9800
My Carrier: Virgin mobile

Re: Can't get focus.setFocus working in certain instance

[ Edited ]

This is still not working I added a delay in there in case the screen was not full rendered (if that might make a difference) but I get the same result. I tried this.

 

		window.setTimeout(function () 
    	{ 
        	document.getElementById(lastFeedHeader).focus(); 
        	console.log ("just did it - set the focus");
    	}, 0); 

 

I get the message that the focus has been set, but it still has not. And lastFeedHeader is a global variable so it does contain the correct id?

 

Any ideas, this is preventing me from tidying up the nav and getting into QA.

Please use plain text.
Contributor
Wongadob
Posts: 40
Registered: ‎05-12-2012
My Device: 9800
My Carrier: Virgin mobile

Re: URGENT Can't get focus.setFocus working in certain instance

I have been able to narrow this down somewhat. Here is the latest info. I really could do with an answer on this one from you blackberry guys :smileyhappy: This is preventing me going to alpha now. Anyway where I am up to.

 

The issue seems to be when I change screen. I do not change screen in the normal way. As I have stated before I change screen by enabling and disabling a series of divs. Each major div is a screen. I use knockout js to create each page, but that does not appear to be the issue. Each 'screen' has a common icon bar at the top. if I set my focus to this titlebar it works when changing screens. Once the focus is on the status bar, when I change screen I can navigate everywhere correctly.

 

I had a feeling that the Dom has not been fully updated so I wrote a piece of code that tried to change the focus to the correct message item on the page and put it in a timed callback. That timed callback would set the focus then re-read the focus (an example from blackberry's demos and looks like this.

 

function setFocusToElement(/* string */parentDiv, /* string */elemntId)
{
	if (blackberry.focus && focusAttempts != 0)
	{
		try
		{
			focusAttempts--;
			var elmnt = document.getElementById(parentDiv);
			elmnt.appendChild(dummy);
			// Adding a dummy element
			elmnt.removeChild(dummy);
			// Removing the dummy element
		}
		catch (e)
		{
			alert(e);
		}

		try
		{
			blackberry.focus.setFocus(elemntId);
			if (blackberry.focus.getFocus() != elemntId)
			{
				console.log("try againt");
				setTimeout("setFocusToElement('" + parentDiv + "','" + elemntId + "');", 500);
			}
			else
			{
				setScrollTop(elemntId);
				focusAttempts = 0;
			}
		}
		catch (ex)
		{
			if (blackberry.focus.getFocus() != elemntId)
			{
				console.log("try againb");
				setTimeout("setFocusToElement('" + parentDiv + "','" + elemntId + "');", 500);
			}
			else
			{
				setScrollTop(elemntId);
				focusAttempts = 0;
			}
		}
	}
}

 

Even if I set the number of attempts to be 20 so therefore called for 10 seconds, it still will not set the foucs.

 

When it concludes and I go to the web inspector (BTW I am testing on simulators) I try to set it manually from there. It will not change. It stays on the icon I set it to on the icon bar. HOWEVER and I think this may be the biggest clue. If I move the scrollwheel or trackpad and move the focus just once to any other focusable element on the screen or icon bar and then manually try to set the focus in the web inspector it works fine and I can set it to any element I want on the screen.

 

So it seems that using the system to change focus just once will make the change focus work. So what is in your code to change focus from one element to another. Do you have any code that can be executed to recalculate all the focusable elements? Does that get called prior to a system movement of the focus.

 

I really need an answer to this as it really is delaying me completing this project.

 

Regards

 

 

Please use plain text.
Contributor
Wongadob
Posts: 40
Registered: ‎05-12-2012
My Device: 9800
My Carrier: Virgin mobile

Re: URGENT Can't get focus.setFocus working in certain instance

I really do need an urgent answer on this one. It is making me lookk stupid now and I have been trying to solve this problem for 2 weeks. It seems on DOM manipulation it gets confused where the focus is. So I need to tell it to re-calculate the focus nav points. I think, but how can I do this?

 

 

Please use plain text.
Contributor
Wongadob
Posts: 40
Registered: ‎05-12-2012
My Device: 9800
My Carrier: Virgin mobile

Re: URGENT Can't get focus.setFocus working in certain instance

[ Edited ]

Sorry to keep bumping this one, but I am hoping one of the blackberry guys will read it and give me some feedback. I know without a shadow of a doubt that blackberry.focus.setFocus is getting confused when I change pages by swapping page div's in and out.

 

The makeup of my page is as follows. I have a header bar that has a few icons, one for each page. As you select the icon the page will change. Below that I have a div for each page. Each page contains message items, in a typical e-mail/messenger way. You select a message and it brings up the conversation. This all works as when you select a message, you want to be at the top of the screen to read that message and all replies. So I can set the focus to the icon bar. However when you return back to your message headers you would want to go bacl to the orignal message you came from, That may be half way down the page. Now I know the ID of the message I want to go to is correct. I can also set the scroll position to the start of that message. Buit I CANNOT set the focus using blackberry.focus.setFocus(id).

 

So initially I thought that it must be becuase the page is not ready yet (even though setting scrollTop works). So I created a call back to set the focus a little bit later. There was an example of this in the Kitchen sink demo. So I implemented that. I tried setting the focus every half a second for 10 seconds. And it still did not set it correctly.

 

I then went into webInspector and tried to set it manually and it still would not set it. to the new ID. I did a console dump of the element with that id and sure enough it is there.

 

Now even more strange. When I move the cursor using the trackpad, the focus comes back (only if I do left or right), up or down crashes. It is like it is taking the screenpos of the last ID it was on and then moving to find a new ID on this screen. However going up or down from the screen is was on would crash it, as that screen does not exist any more and is hidden. The screen actually does not exist as the HTML for that page has also been removed. I use Knockoutjs and when it does a re-draw you can litterally turn the HTML off. It cannot remain on as it would otherwise refresh all its data (and that would apply for 6 other sccreens too) so by and large the old ID does not exist any more and nor do any of the ID's of messages above and below. But as I say if you move left or right if it is taking screen position of old ID then it will move to the ID on the new page left or right of that screen position so does not crash.

 

So I have tried setting the focus to the icon bar at the top prior to screen transition. Which works, but after screen transition it still will not set the focus to my new ID. Again with the 20 seconds of callbacks to set it. AND by trying to set it manually.

All I want to know is how I can reset it all, or re-calculate nav points or something like that so I can the re-start the navigation at my new point.

 

This is my only remaining Priority 1 bug (and I have no P2's) so it is preventing me from going Beta. It has been ongoing for 2+ weeks now and I need to resolve it.

 

Please use plain text.