Mercurial Hosting > nabble
diff src/nabble/view/web/embed/JsEmbed.jtp @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/nabble/view/web/embed/JsEmbed.jtp Thu Mar 21 19:15:52 2019 -0600 @@ -0,0 +1,326 @@ +<% +package nabble.view.web.embed; + +import fschmidt.util.java.HtmlUtils; +import fschmidt.util.servlet.JtpContext; +import nabble.model.ModelHome; +import nabble.model.Node; +import nabble.model.Site; +import nabble.view.lib.Jtp; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.List; + + +public final class JsEmbed extends HttpServlet { + + private static final Logger logger = LoggerFactory.getLogger(JsEmbed.class); + + protected void service(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException + { + response.setHeader("Content-Type","application/x-javascript"); + PrintWriter out = response.getWriter(); + + long siteId = Jtp.getLong(request, "site"); + Site site = ModelHome.getSite(siteId); + if (site == null) { + response.sendError(HttpServletResponse.SC_NOT_FOUND, "Site not found"); + return; + } + + long nodeId = Jtp.getLong(request, "node"); + Node node = site.getNode(nodeId); + + // cache the page + List<String> events = new ArrayList<String>(); + Jtp.addBreadCrumbEvents( events, site.getRootNode() ); + JtpContext jtpContext = (JtpContext)getServletContext().getAttribute(JtpContext.attrName); + jtpContext.setEtag(request,response, events.toArray( new String[events.size()] ) ); + + String baseUrl = Jtp.getBaseUrl(request); + + String embedUrl = request.getParameter("url"); + logger.info("URL=" + embedUrl); + + embedUrl = HtmlUtils.urlDecode(embedUrl); + int pos = embedUrl.indexOf("#nabble-"); + String defaultUrl = node == null ? null : baseUrl+Jtp.path(node); + if (pos > 0) { + String sufix = embedUrl.substring(pos+7); + // if sufix contains #a (with post id) + int posHash = sufix.indexOf('|'); + if (posHash > 0) { + sufix = sufix.substring(0, posHash); + } + defaultUrl = baseUrl + '/' + sufix + ".html"; + } + %> + var Nabble = new Object(); + Nabble.defaultHeight = 700; + Nabble.currentHeight = 0; + Nabble.counter = 0; + Nabble.title = document.title == ""? "" : document.title + " - "; + Nabble.resizeTimeoutID; + Nabble.context = '<%=baseUrl%>'; + Nabble.defaultUrl = '<%=defaultUrl%>'; + + Nabble.get = function(id) { return document.getElementById(id); }; + + Nabble.resizeTimeout = function() { + Nabble.resizeTimeoutID = setTimeout(Nabble.showMoreLink, 6000); + }; + + Nabble.cancelTimeout = function() { + if (Nabble.resizeTimeoutID) { + clearInterval(Nabble.resizeTimeoutID); + Nabble.get('nabblemore').style.display = 'none'; + Nabble.resizeTimeoutID = null; + } + }; + + Nabble.showMoreLink = function() { + if (Nabble.resizeTimeoutID) { + Nabble.get('nabblemore').style.display = 'block'; + } + }; + + Nabble.showMore = function() { + if (Nabble.currentHeight == 0) + Nabble.currentHeight = Nabble.defaultHeight; + Nabble.currentHeight += 300; + Nabble.get('nabbleiframe').style.height = Nabble.currentHeight + 'px'; + }; + + Nabble.escape = function(value) { + if (typeof value == 'string') { + var hasSpace = value.indexOf(' ') >= 0; + var hasQuote = value.indexOf('"') >= 0; + + value = value.replace(/\;/g, '%3B'); + value = value.replace(/"/g, '\\"'); + + if (hasSpace || hasQuote) + value = '"' + value + '"'; + } + return value; + }; + + Nabble.unescape = function(value) { + if (value.charAt(0) == '"' && value.charAt(value.length-1) == '"') + value = value.substring(1, value.length-1); + + value = value.replace(/\\"/g, '"'); + value = value.replace(/%3B/g, ';'); + return value; + }; + + Nabble.setCookie = function(name, value) { + name = name+'<%=siteId%>'; + document.cookie = name + "=" + Nabble.escape(value) + "; path=/"; + }; + + Nabble.getCookie = function(name) { + name = name+'<%=siteId%>'; + var dc = document.cookie; + var prefix = name + "="; + var begin = dc.indexOf("; " + prefix); + if (begin == -1) { + begin = dc.indexOf(prefix); + if (begin != 0) return null; + } else + begin += 2; + var end = document.cookie.indexOf(";", begin); + if (end == -1) + end = dc.length; + return Nabble.unescape(dc.substring(begin + prefix.length, end)); + }; + + Nabble.setPersistentCookie = function(name, value) { + name = name+'<%=siteId%>'; + var expires = new Date(); + expires.setFullYear(expires.getFullYear()+10); + var curCookie = name + "=" + Nabble.escape(value) + "; expires=" + expires.toGMTString() + "; path=/"; + document.cookie = curCookie; + }; + + Nabble.deleteCookie = function(name) { + name = name+'<%=siteId%>'; + document.cookie = name + "=" + + "; path=/" + + "; expires=Thu, 01-Jan-1970 00:00:01 GMT"; + }; + + Nabble.noHash = function(url) { + var pos = url.indexOf('#'); + return (pos>-1)?url.substring(0, pos):url; + }; + + Nabble.debug = function(s) { + if (Nabble.debugElement == 0) + return; + if (!Nabble.debugElement) { + Nabble.debugElement = Nabble.get('debug'); + if (!Nabble.debugElement) { + Nabble.debugElement = 0; + return; + } + } + Nabble.debugElement.innerHTML = Nabble.debugElement.innerHTML+s+'<br/>'; + }; + + Nabble.loadScript = function(url) { + Nabble.debug('Loading script: ' + url); + var e = document.createElement("script"); + e.src = url; + e.type="text/javascript"; + document.getElementsByTagName("head")[0].appendChild(e); + }; + + Nabble.getJs = function(keys) { + if (!window.clientID) + return; + var p = ''; + for (var i=0;i<keys.length;i++) { + p += '&key=' + keys[i]; + } + var url = Nabble.context+"/util/SessionService.jtp?action=get" + p + "&cid=" + window.clientID + "&_=" + new Date().getTime(); + Nabble.loadScript(url); + }; + + Nabble.scroll = function(y) { + Nabble.debug('[scroll] y=' + y); + if (y == 1 && window.nabble_scroll_top) { + scrollTo(0, 0); + } else if (y > 0 && !window.nabble_ignore_scroll) { + var obj = Nabble.get('nabbleiframe'); + do { + y += obj.offsetTop; + } while (obj = obj.offsetParent); + scrollTo(0, y); + } + }; + + Nabble.resizeFrames = function(height,title,validHeight) { + if (document.title != title && !window.nabble_ignore_title) + document.title = title; + Nabble.debug('[resizeFrames] Counter = ' + (Nabble.counter++) + ' Height = ' + height + ' Title=[' + title + '] History=' + history.length + ' -- cid=' + window.clientID); + + if (height != Nabble.currentHeight) { + Nabble.currentHeight = height; + var f = Nabble.get('nabbleiframe'); + if (f) { + f.scrolling = validHeight? 'no' : 'auto'; + Nabble.debug('Scrolling=' + f.scrolling); + f.style.height = height + 'px'; + Nabble.cancelTimeout(); + } + } + }; + + Nabble.getCurrentUrl = function() { + var currentUrl = Nabble.defaultUrl; + if (Nabble.hash.indexOf('#nabble+') == 0) { + var path = Nabble.hash.substring(8); + path = decodeURIComponent(path); + path = path + .replace(/</g,'%3C') + .replace(/>/g,'%3E') + .replace(/"/g,'%22') + .replace(/'/g,'%27'); + currentUrl = Nabble.context+"/" + path; + } + currentUrl += Nabble.realHash == ''? '' : '#' + Nabble.realHash; + return currentUrl; + }; + + Nabble.getClientID = function() { + var clientID = Nabble.getCookie('clientID'); + if (!clientID) { + clientID = new Date().getTime() + '-' + Math.ceil(Math.random() * 1000); + Nabble.setCookie('clientID', clientID); + } + return clientID; + }; + + Nabble.restart = function(nodeId, baseUrl) { + Nabble.debug('Restart -- baseUrl=' + baseUrl); + Nabble.context = baseUrl; + Nabble.defaultUrl = baseUrl+'/'; + Nabble.start(); + }; + + Nabble.getConf = function() { + return window.nabble_ignore_scroll? 'noscroll;':''; + }; + + Nabble.start = function() { + Nabble.infoLoaded = false; + window.clientID = Nabble.getClientID(); + + <% /* Hash processing */ %> + var hash = location.hash; + var pipe = hash.indexOf('|'); + var realHash = ''; + if (pipe > 0) { + <%/* + If there is a pipe in the hash, the value after it is the + hash of the original URL, which must be restored. + For Example: + #nabble-t100|a123 + */%> + realHash = hash.substring(pipe); + hash = hash.replace(realHash, ''); + realHash = realHash.substring(1); + } + <%/* save the hash and realHash variables because the getCurrentUrl() method will need them.. */%> + Nabble.hash = hash; + Nabble.realHash = realHash; + Nabble.infoUrl = Nabble.context+"/embed/EmbedInfo.jtp?node=<%=nodeId%>&cid=" + window.clientID + "&hash=" + realHash + '&conf=' + Nabble.getConf() + "&_=" + new Date().getTime() + "#" + Nabble.noHash(location.href); + var emptyUrl = Nabble.context+"/util/Empty.jtp"; + var html = "<div id='nabblemain'><div style='height:700px'><img src='<%=baseUrl%>/images/loading.png' width=94 height=33 alt='Loading...'></div></div>"; + html += "<div id='nabblemore' style='display:none'><a href=\"javascript: void Nabble.showMore()\">view more</a></div>"; + html += "<iframe name='nabbleinfo' id='nabbleinfo' width='1' height='1' style='display:none' src=''></iframe>"; + html += "<iframe name='nabbleresize' onload='Nabble.getJs([\"resizejs\", \"scrolljs\", \"others\"])' width='1' height='1' style='display:none' src='" + emptyUrl + "'></iframe>"; + html += "<iframe name='nabbleready' onload='Nabble.loadMain()' width='1' height='1' style='display:none' src='" + emptyUrl + "'></iframe>"; + var div = Nabble.get('nabbleforum'); + div.innerHTML = html; + }; + + <%/* + 1 - Loads the main page as the last page in order to allow the + embedding code to run before it. + 2 - We must load the iframe using a string because IE (and some + other browsers) have problems to load an iframe through the DOM. + 3 - The loadMain() function is called when the 'nabbleready' iframe is loaded. + Note that this iframe doesn't have an initial URL. The first URL is provided in EmbedInfo.jtp. + */%> + Nabble.loadMain = function() { + Nabble.debug('Loading main page...infoLoaded='+Nabble.infoLoaded); + if (!Nabble.infoLoaded) { + Nabble.debug('InfoUrl='+Nabble.infoUrl); + Nabble.get('nabbleinfo').setAttribute('src',Nabble.infoUrl); + Nabble.infoLoaded = true; + return; + } + var width = window.nabble_width? window.nabble_width : '100%'; + var currentUrl = Nabble.getCurrentUrl(); + Nabble.debug('CurrentUrl='+currentUrl); + var m = '<iframe name="nabbleiframe" id="nabbleiframe" src="' + currentUrl + '" width="' + width + '" height="' + Nabble.defaultHeight + '" frameBorder="0" scrolling="no" allowtransparency="true"></iframe>'; + Nabble.get('nabblemain').innerHTML = m; + Nabble.resizeTimeout(); + } + + Nabble.start(); + <% + } +} +%>