/*
* Executes JavaScript in the Browser to navigate between News.
*/
void navigate(boolean next, boolean unread) {
CBrowser browser = fViewer.getBrowser();
if (browser.shouldDisableScript())
browser.setScriptDisabled(false);
/* Create JavaScript to Execute */
StringBuffer js = new StringBuffer();
if (browser.isIE())
js.append("var scrollPosY = document.body.scrollTop; "); //$NON-NLS-1$
else
js.append("var scrollPosY = window.pageYOffset; "); //$NON-NLS-1$
js.append("var body = document.getElementById(\"owlbody\"); "); //$NON-NLS-1$
js.append("var divs = body.childNodes; "); //$NON-NLS-1$
/* Next News */
if (next) {
js.append(" for (var i = 1; i < divs.length; i++) { "); //$NON-NLS-1$
js.append(" if (divs[i].nodeType != 1) { "); //$NON-NLS-1$
js.append(" continue; "); //$NON-NLS-1$
js.append(" } "); //$NON-NLS-1$
js.append(" var divPosY = divs[i].offsetTop; "); //$NON-NLS-1$
if (unread) {
js.append(" if (divPosY > scrollPosY && divs[i].className == \"newsitemUnread\") { "); //$NON-NLS-1$
} else
js.append(" if (divPosY > scrollPosY) { "); //$NON-NLS-1$
js.append(" divs[i].scrollIntoView(); "); //$NON-NLS-1$
js.append(" break; "); //$NON-NLS-1$
js.append(" } "); //$NON-NLS-1$
js.append(" } "); //$NON-NLS-1$
}
/* Previous News */
else {
js.append(" for (var i = divs.length - 1; i >= 0; i--) { "); //$NON-NLS-1$
js.append(" if (divs[i].nodeType != 1) { "); //$NON-NLS-1$
js.append(" continue; "); //$NON-NLS-1$
js.append(" } "); //$NON-NLS-1$
js.append(" var divPosY = divs[i].offsetTop; "); //$NON-NLS-1$
if (unread) {
js.append(" if (divPosY < scrollPosY - 10 && divs[i].className == \"newsitemUnread\") { "); //$NON-NLS-1$
} else
js.append(" if (divPosY < scrollPosY - 10) { "); //$NON-NLS-1$
js.append(" divs[i].scrollIntoView(); "); //$NON-NLS-1$
js.append(" break; "); //$NON-NLS-1$
js.append(" } "); //$NON-NLS-1$
js.append(" } "); //$NON-NLS-1$
}
/* See if the Scroll Position Changed at all and handle */
String actionId;
if (next) {
if (unread)
actionId = NewsBrowserViewer.NEXT_UNREAD_NEWS_HANDLER_ID;
else
actionId = NewsBrowserViewer.NEXT_NEWS_HANDLER_ID;
} else {
if (unread)
actionId = NewsBrowserViewer.PREVIOUS_UNREAD_NEWS_HANDLER_ID;
else
actionId = NewsBrowserViewer.PREVIOUS_NEWS_HANDLER_ID;
}
if (browser.isIE())
js.append("var newScrollPosY = document.body.scrollTop; "); //$NON-NLS-1$
else
js.append("var newScrollPosY = window.pageYOffset; "); //$NON-NLS-1$
js.append("if (scrollPosY == newScrollPosY) { "); //$NON-NLS-1$
js.append(" window.location.href = \"").append(ILinkHandler.HANDLER_PROTOCOL + actionId).append("\"; "); //$NON-NLS-1$ //$NON-NLS-2$
js.append("} "); //$NON-NLS-1$
try {
browser.getControl().execute(js.toString());
} finally {
if (browser.shouldDisableScript())
browser.setScriptDisabled(true);
}
}