comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:7ecd1a4ef557
1 <%
2 package nabble.view.web.embed;
3
4 import fschmidt.util.java.HtmlUtils;
5 import fschmidt.util.servlet.JtpContext;
6 import nabble.model.ModelHome;
7 import nabble.model.Node;
8 import nabble.model.Site;
9 import nabble.view.lib.Jtp;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 import javax.servlet.ServletException;
14 import javax.servlet.http.HttpServlet;
15 import javax.servlet.http.HttpServletRequest;
16 import javax.servlet.http.HttpServletResponse;
17 import java.io.IOException;
18 import java.io.PrintWriter;
19 import java.util.ArrayList;
20 import java.util.List;
21
22
23 public final class JsEmbed extends HttpServlet {
24
25 private static final Logger logger = LoggerFactory.getLogger(JsEmbed.class);
26
27 protected void service(HttpServletRequest request, HttpServletResponse response)
28 throws ServletException, IOException
29 {
30 response.setHeader("Content-Type","application/x-javascript");
31 PrintWriter out = response.getWriter();
32
33 long siteId = Jtp.getLong(request, "site");
34 Site site = ModelHome.getSite(siteId);
35 if (site == null) {
36 response.sendError(HttpServletResponse.SC_NOT_FOUND, "Site not found");
37 return;
38 }
39
40 long nodeId = Jtp.getLong(request, "node");
41 Node node = site.getNode(nodeId);
42
43 // cache the page
44 List<String> events = new ArrayList<String>();
45 Jtp.addBreadCrumbEvents( events, site.getRootNode() );
46 JtpContext jtpContext = (JtpContext)getServletContext().getAttribute(JtpContext.attrName);
47 jtpContext.setEtag(request,response, events.toArray( new String[events.size()] ) );
48
49 String baseUrl = Jtp.getBaseUrl(request);
50
51 String embedUrl = request.getParameter("url");
52 logger.info("URL=" + embedUrl);
53
54 embedUrl = HtmlUtils.urlDecode(embedUrl);
55 int pos = embedUrl.indexOf("#nabble-");
56 String defaultUrl = node == null ? null : baseUrl+Jtp.path(node);
57 if (pos > 0) {
58 String sufix = embedUrl.substring(pos+7);
59 // if sufix contains #a (with post id)
60 int posHash = sufix.indexOf('|');
61 if (posHash > 0) {
62 sufix = sufix.substring(0, posHash);
63 }
64 defaultUrl = baseUrl + '/' + sufix + ".html";
65 }
66 %>
67 var Nabble = new Object();
68 Nabble.defaultHeight = 700;
69 Nabble.currentHeight = 0;
70 Nabble.counter = 0;
71 Nabble.title = document.title == ""? "" : document.title + " - ";
72 Nabble.resizeTimeoutID;
73 Nabble.context = '<%=baseUrl%>';
74 Nabble.defaultUrl = '<%=defaultUrl%>';
75
76 Nabble.get = function(id) { return document.getElementById(id); };
77
78 Nabble.resizeTimeout = function() {
79 Nabble.resizeTimeoutID = setTimeout(Nabble.showMoreLink, 6000);
80 };
81
82 Nabble.cancelTimeout = function() {
83 if (Nabble.resizeTimeoutID) {
84 clearInterval(Nabble.resizeTimeoutID);
85 Nabble.get('nabblemore').style.display = 'none';
86 Nabble.resizeTimeoutID = null;
87 }
88 };
89
90 Nabble.showMoreLink = function() {
91 if (Nabble.resizeTimeoutID) {
92 Nabble.get('nabblemore').style.display = 'block';
93 }
94 };
95
96 Nabble.showMore = function() {
97 if (Nabble.currentHeight == 0)
98 Nabble.currentHeight = Nabble.defaultHeight;
99 Nabble.currentHeight += 300;
100 Nabble.get('nabbleiframe').style.height = Nabble.currentHeight + 'px';
101 };
102
103 Nabble.escape = function(value) {
104 if (typeof value == 'string') {
105 var hasSpace = value.indexOf(' ') >= 0;
106 var hasQuote = value.indexOf('"') >= 0;
107
108 value = value.replace(/\;/g, '%3B');
109 value = value.replace(/"/g, '\\"');
110
111 if (hasSpace || hasQuote)
112 value = '"' + value + '"';
113 }
114 return value;
115 };
116
117 Nabble.unescape = function(value) {
118 if (value.charAt(0) == '"' && value.charAt(value.length-1) == '"')
119 value = value.substring(1, value.length-1);
120
121 value = value.replace(/\\"/g, '"');
122 value = value.replace(/%3B/g, ';');
123 return value;
124 };
125
126 Nabble.setCookie = function(name, value) {
127 name = name+'<%=siteId%>';
128 document.cookie = name + "=" + Nabble.escape(value) + "; path=/";
129 };
130
131 Nabble.getCookie = function(name) {
132 name = name+'<%=siteId%>';
133 var dc = document.cookie;
134 var prefix = name + "=";
135 var begin = dc.indexOf("; " + prefix);
136 if (begin == -1) {
137 begin = dc.indexOf(prefix);
138 if (begin != 0) return null;
139 } else
140 begin += 2;
141 var end = document.cookie.indexOf(";", begin);
142 if (end == -1)
143 end = dc.length;
144 return Nabble.unescape(dc.substring(begin + prefix.length, end));
145 };
146
147 Nabble.setPersistentCookie = function(name, value) {
148 name = name+'<%=siteId%>';
149 var expires = new Date();
150 expires.setFullYear(expires.getFullYear()+10);
151 var curCookie = name + "=" + Nabble.escape(value) + "; expires=" + expires.toGMTString() + "; path=/";
152 document.cookie = curCookie;
153 };
154
155 Nabble.deleteCookie = function(name) {
156 name = name+'<%=siteId%>';
157 document.cookie = name + "=" +
158 "; path=/" +
159 "; expires=Thu, 01-Jan-1970 00:00:01 GMT";
160 };
161
162 Nabble.noHash = function(url) {
163 var pos = url.indexOf('#');
164 return (pos>-1)?url.substring(0, pos):url;
165 };
166
167 Nabble.debug = function(s) {
168 if (Nabble.debugElement == 0)
169 return;
170 if (!Nabble.debugElement) {
171 Nabble.debugElement = Nabble.get('debug');
172 if (!Nabble.debugElement) {
173 Nabble.debugElement = 0;
174 return;
175 }
176 }
177 Nabble.debugElement.innerHTML = Nabble.debugElement.innerHTML+s+'<br/>';
178 };
179
180 Nabble.loadScript = function(url) {
181 Nabble.debug('Loading script: ' + url);
182 var e = document.createElement("script");
183 e.src = url;
184 e.type="text/javascript";
185 document.getElementsByTagName("head")[0].appendChild(e);
186 };
187
188 Nabble.getJs = function(keys) {
189 if (!window.clientID)
190 return;
191 var p = '';
192 for (var i=0;i<keys.length;i++) {
193 p += '&key=' + keys[i];
194 }
195 var url = Nabble.context+"/util/SessionService.jtp?action=get" + p + "&cid=" + window.clientID + "&_=" + new Date().getTime();
196 Nabble.loadScript(url);
197 };
198
199 Nabble.scroll = function(y) {
200 Nabble.debug('[scroll] y=' + y);
201 if (y == 1 && window.nabble_scroll_top) {
202 scrollTo(0, 0);
203 } else if (y > 0 && !window.nabble_ignore_scroll) {
204 var obj = Nabble.get('nabbleiframe');
205 do {
206 y += obj.offsetTop;
207 } while (obj = obj.offsetParent);
208 scrollTo(0, y);
209 }
210 };
211
212 Nabble.resizeFrames = function(height,title,validHeight) {
213 if (document.title != title && !window.nabble_ignore_title)
214 document.title = title;
215 Nabble.debug('[resizeFrames] Counter = ' + (Nabble.counter++) + ' Height = ' + height + ' Title=[' + title + '] History=' + history.length + ' -- cid=' + window.clientID);
216
217 if (height != Nabble.currentHeight) {
218 Nabble.currentHeight = height;
219 var f = Nabble.get('nabbleiframe');
220 if (f) {
221 f.scrolling = validHeight? 'no' : 'auto';
222 Nabble.debug('Scrolling=' + f.scrolling);
223 f.style.height = height + 'px';
224 Nabble.cancelTimeout();
225 }
226 }
227 };
228
229 Nabble.getCurrentUrl = function() {
230 var currentUrl = Nabble.defaultUrl;
231 if (Nabble.hash.indexOf('#nabble+') == 0) {
232 var path = Nabble.hash.substring(8);
233 path = decodeURIComponent(path);
234 path = path
235 .replace(/</g,'%3C')
236 .replace(/>/g,'%3E')
237 .replace(/"/g,'%22')
238 .replace(/'/g,'%27');
239 currentUrl = Nabble.context+"/" + path;
240 }
241 currentUrl += Nabble.realHash == ''? '' : '#' + Nabble.realHash;
242 return currentUrl;
243 };
244
245 Nabble.getClientID = function() {
246 var clientID = Nabble.getCookie('clientID');
247 if (!clientID) {
248 clientID = new Date().getTime() + '-' + Math.ceil(Math.random() * 1000);
249 Nabble.setCookie('clientID', clientID);
250 }
251 return clientID;
252 };
253
254 Nabble.restart = function(nodeId, baseUrl) {
255 Nabble.debug('Restart -- baseUrl=' + baseUrl);
256 Nabble.context = baseUrl;
257 Nabble.defaultUrl = baseUrl+'/';
258 Nabble.start();
259 };
260
261 Nabble.getConf = function() {
262 return window.nabble_ignore_scroll? 'noscroll;':'';
263 };
264
265 Nabble.start = function() {
266 Nabble.infoLoaded = false;
267 window.clientID = Nabble.getClientID();
268
269 <% /* Hash processing */ %>
270 var hash = location.hash;
271 var pipe = hash.indexOf('|');
272 var realHash = '';
273 if (pipe > 0) {
274 <%/*
275 If there is a pipe in the hash, the value after it is the
276 hash of the original URL, which must be restored.
277 For Example:
278 #nabble-t100|a123
279 */%>
280 realHash = hash.substring(pipe);
281 hash = hash.replace(realHash, '');
282 realHash = realHash.substring(1);
283 }
284 <%/* save the hash and realHash variables because the getCurrentUrl() method will need them.. */%>
285 Nabble.hash = hash;
286 Nabble.realHash = realHash;
287 Nabble.infoUrl = Nabble.context+"/embed/EmbedInfo.jtp?node=<%=nodeId%>&cid=" + window.clientID + "&hash=" + realHash + '&conf=' + Nabble.getConf() + "&_=" + new Date().getTime() + "#" + Nabble.noHash(location.href);
288 var emptyUrl = Nabble.context+"/util/Empty.jtp";
289 var html = "<div id='nabblemain'><div style='height:700px'><img src='<%=baseUrl%>/images/loading.png' width=94 height=33 alt='Loading...'></div></div>";
290 html += "<div id='nabblemore' style='display:none'><a href=\"javascript: void Nabble.showMore()\">view more</a></div>";
291 html += "<iframe name='nabbleinfo' id='nabbleinfo' width='1' height='1' style='display:none' src=''></iframe>";
292 html += "<iframe name='nabbleresize' onload='Nabble.getJs([\"resizejs\", \"scrolljs\", \"others\"])' width='1' height='1' style='display:none' src='" + emptyUrl + "'></iframe>";
293 html += "<iframe name='nabbleready' onload='Nabble.loadMain()' width='1' height='1' style='display:none' src='" + emptyUrl + "'></iframe>";
294 var div = Nabble.get('nabbleforum');
295 div.innerHTML = html;
296 };
297
298 <%/*
299 1 - Loads the main page as the last page in order to allow the
300 embedding code to run before it.
301 2 - We must load the iframe using a string because IE (and some
302 other browsers) have problems to load an iframe through the DOM.
303 3 - The loadMain() function is called when the 'nabbleready' iframe is loaded.
304 Note that this iframe doesn't have an initial URL. The first URL is provided in EmbedInfo.jtp.
305 */%>
306 Nabble.loadMain = function() {
307 Nabble.debug('Loading main page...infoLoaded='+Nabble.infoLoaded);
308 if (!Nabble.infoLoaded) {
309 Nabble.debug('InfoUrl='+Nabble.infoUrl);
310 Nabble.get('nabbleinfo').setAttribute('src',Nabble.infoUrl);
311 Nabble.infoLoaded = true;
312 return;
313 }
314 var width = window.nabble_width? window.nabble_width : '100%';
315 var currentUrl = Nabble.getCurrentUrl();
316 Nabble.debug('CurrentUrl='+currentUrl);
317 var m = '<iframe name="nabbleiframe" id="nabbleiframe" src="' + currentUrl + '" width="' + width + '" height="' + Nabble.defaultHeight + '" frameBorder="0" scrolling="no" allowtransparency="true"></iframe>';
318 Nabble.get('nabblemain').innerHTML = m;
319 Nabble.resizeTimeout();
320 }
321
322 Nabble.start();
323 <%
324 }
325 }
326 %>