Mercurial Hosting > nabble
comparison src/nabble/view/naml/javascript_library.naml @ 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 <macro name="javascript_library" requires="servlet"> | |
2 <n.uncache_for/> | |
3 <n.javascript_response/> | |
4 <n.js_basic_nabble_functions/> | |
5 <n.js_basic_embedding_code/> | |
6 <n.js_basic_site_functions/> | |
7 <n.js_date_functions/> | |
8 <n.js_header_functions/> | |
9 <n.js_delete_functions/> | |
10 <n.js_topic_and_post_functions/> | |
11 <n.js_search_functions/> | |
12 </macro> | |
13 | |
14 <macro name="javascript_library_path"> | |
15 <n.encode_url.> | |
16 /template/NamlServlet.jtp?macro=javascript_library&v=<n.javascript_version/> | |
17 </n.encode_url.> | |
18 </macro> | |
19 | |
20 <macro name="js_basic_embedding_code"> | |
21 var host = "<n.get_request_header name="host"/>"; | |
22 <![CDATA[ | |
23 Nabble.embeddingUrl = null; | |
24 Nabble.embedForumID = 0; | |
25 Nabble.isEmbedded = false; | |
26 | |
27 try { | |
28 var info = Nabble.getParent().nabbleinfo.location.search.substring(6); | |
29 info = decodeURIComponent(info); | |
30 var pos = info.indexOf('&'); | |
31 Nabble.embedForumID = info.substring(0, pos); | |
32 var hash = Nabble.getParent().nabbleinfo.location.hash.substring(1).split('|'); | |
33 Nabble.embeddingUrl = hash[0]; | |
34 Nabble.isEmbedded = true; | |
35 } catch(err) {} | |
36 | |
37 Nabble.getClientID = function(c) { | |
38 return ''; | |
39 }; | |
40 | |
41 Nabble.embeddedTarget = function(defaultTarget) { | |
42 return Nabble.isEmbedded? ' target="nabbleiframe" ' : ' target="' + defaultTarget + '" '; | |
43 }; | |
44 | |
45 Nabble.height = function() { | |
46 if( typeof( window.innerHeight ) == 'number' ) { | |
47 return window.innerHeight; | |
48 } else if( document.documentElement && document.documentElement.clientHeight ) { | |
49 return document.documentElement.clientHeight; | |
50 } else if( document.body && document.body.clientHeight ) { | |
51 return document.body.clientHeight; | |
52 } | |
53 }; | |
54 | |
55 Nabble.knowsHeight = navigator.userAgent.toLowerCase().indexOf('safari') == -1; | |
56 | |
57 Nabble.canScroll = function() { | |
58 try{ | |
59 return Nabble.getParent().nabbleinfo.canScroll(); | |
60 } catch(er) {} | |
61 return true; | |
62 }; | |
63 | |
64 Nabble.hash = function() { | |
65 var _hash = location.hash; | |
66 if (Nabble.getParent().nabbleinfo && Nabble.getParent().nabbleinfo.hash) { | |
67 if (!_hash) | |
68 _hash = '#' + Nabble.getParent().nabbleinfo.hash; | |
69 Nabble.getParent().nabbleinfo.hash = null; | |
70 } | |
71 return _hash; | |
72 }; | |
73 | |
74 Nabble.setTop = function(url) { | |
75 if (Nabble.isEmbedded) Nabble.getParent().nabbleiframe.location=url; | |
76 else top.location=url; | |
77 }; | |
78 | |
79 Nabble.getMyHeight = function() { | |
80 try { | |
81 return $(document.body).outerHeight(); | |
82 } catch(err) {} | |
83 return 500; | |
84 }; | |
85 | |
86 Nabble.evalInTop = function(key, js) { | |
87 var p = ''; | |
88 for (var i = 0; i < js.length; i++) { | |
89 p += 'v=' + key[i] + '|' + encodeURIComponent(js[i]) + '&'; | |
90 } | |
91 if (p.length > 0) { | |
92 var url = "/util/SessionService.jtp?action=set&" + p + "cid=" + Nabble.getParent().nabbleinfo.clientID + "&_=" + new Date().getTime(); | |
93 $.get(url, function() { | |
94 Nabble.getParent().nabbleresize.location = "/util/Empty.jtp"; | |
95 }); | |
96 } | |
97 }; | |
98 | |
99 Nabble.quote = function(s) { | |
100 return '"' + s.replace(/\\/g,"\\\\").replace(/'/g,"\\'").replace(/"/g,'\\"') + '"'; | |
101 }; | |
102 | |
103 Nabble.heightLimit = 32767; | |
104 Nabble.resizeCount = 0; | |
105 | |
106 Nabble.isValidHeight = function(height) { | |
107 var isBadBrowser = $.browser.mozilla || $.browser.opera; | |
108 var exceeded = height > Nabble.heightLimit && isBadBrowser; | |
109 return !exceeded; | |
110 }; | |
111 | |
112 Nabble.resizeFrames = function(h, scroll) { | |
113 if (Nabble.getParent().nabbleresize) { | |
114 var height = h && typeof h == 'number'? h : Math.max(Nabble.getMyHeight() + 25, 600); | |
115 var validHeight = Nabble.isValidHeight(height); | |
116 height = validHeight? height : Nabble.heightLimit; | |
117 var js = [], key = []; | |
118 if (scroll) { | |
119 js.push("Nabble.scroll(" + scroll + ");"); | |
120 key.push("scrolljs"); | |
121 } | |
122 if (height != $(window).height() || Nabble.resizeCount++ == 0) { | |
123 js.push("Nabble.resizeFrames(" + height + "," + Nabble.quote(document.title) + "," + validHeight + ");"); | |
124 key.push("resizejs"); | |
125 } | |
126 if (js.length > 0) | |
127 Nabble.evalInTop(key, js); | |
128 } | |
129 }; | |
130 | |
131 Nabble.restartEmbedding = function(nodeId, baseUrl) { | |
132 if (Nabble.getParent().nabbleresize) { | |
133 var js = ['Nabble.restart(' + nodeId + ',"' + baseUrl + '");']; | |
134 Nabble.evalInTop(['others'], js); | |
135 } | |
136 }; | |
137 | |
138 $(document).ready(function(){ | |
139 if (Nabble.isEmbedded) { | |
140 Nabble.resizeFrames('', 1); | |
141 $(window).resize(Nabble.resizeFrames); | |
142 $(window).load(function() { | |
143 Nabble.resizeFrames(); | |
144 }); | |
145 } | |
146 }); | |
147 ]]> | |
148 </macro> | |
149 | |
150 <macro name="js_basic_site_functions" requires="servlet"> | |
151 <![CDATA[ | |
152 Nabble.online = function(id) { | |
153 $('img.online'+id).show(); | |
154 }; | |
155 | |
156 Nabble.openMsg = function(id) { | |
157 return Nabble.getCookie(id) == null; | |
158 }; | |
159 | |
160 Nabble.toggleMsg = function(id, hide) { | |
161 if (hide) | |
162 Nabble.setPersistentCookie(id, 'closed'); | |
163 else | |
164 Nabble.deleteCookie(id); | |
165 }; | |
166 ]]> | |
167 </macro> | |
168 | |
169 <macro name="js_date_functions" requires="servlet"> | |
170 Nabble.months = ["<t>Jan</t>","<t>Feb</t>","<t>Mar</t>","<t>Apr</t>","<t>May</t>","<t>Jun</t>","<t>Jul</t>","<t>Aug</t>","<t>Sep</t>","<t>Oct</t>","<t>Nov</t>","<t>Dec</t>"]; | |
171 <![CDATA[ | |
172 Nabble.now = new Date(); | |
173 Nabble.fmt2 = function(i) { return i <= 9? '0'+i:i; }; | |
174 | |
175 Nabble.isToday = function(date) { | |
176 return date.toDateString() == this.now.toDateString(); | |
177 }; | |
178 | |
179 Nabble.isThisYear = function(date) { | |
180 return date.getYear() == this.now.getYear(); | |
181 }; | |
182 | |
183 Nabble.dateFormatters = { | |
184 us: new (function(){ | |
185 this.formatTime = function(date) { | |
186 var hours = date.getHours(); | |
187 if (hours < 12) { | |
188 var xm = "am"; | |
189 if (hours==0) | |
190 hours = 12; | |
191 } else { | |
192 var xm = "pm"; | |
193 if (hours > 12) | |
194 hours -= 12; | |
195 } | |
196 return hours + ":" + Nabble.fmt2(date.getMinutes()) + xm; | |
197 }; | |
198 this.formatDateOnly = function(date) { | |
199 return Nabble.months[date.getMonth()] + " " + Nabble.fmt2(date.getDate()) + ", " + date.getFullYear(); | |
200 }; | |
201 this.formatDateLong = function(date) { | |
202 return this.formatDateOnly(date) + "; " + this.formatTime(date); | |
203 }; | |
204 this.formatDateShort = function(date) { | |
205 if( Nabble.isToday(date) ) | |
206 return this.formatTime(date); | |
207 if( Nabble.isThisYear(date) ) | |
208 return Nabble.months[date.getMonth()] + " " + Nabble.fmt2(date.getDate()); | |
209 return this.formatDateOnly(date); | |
210 }; | |
211 })() | |
212 , | |
213 euro: new (function(){ | |
214 this.formatTime = function(date) { | |
215 return Nabble.fmt2(date.getHours()) + ":" + Nabble.fmt2(date.getMinutes()); | |
216 }; | |
217 this.formatDateOnly = function(date) { | |
218 return Nabble.fmt2(date.getDate()) + "." + Nabble.months[date.getMonth()] + "." + date.getFullYear(); | |
219 }; | |
220 this.formatDateLong = function(date) { | |
221 return this.formatTime(date) + ", " + this.formatDateOnly(date); | |
222 }; | |
223 this.formatDateShort = function(date) { | |
224 if( Nabble.isToday(date) ) | |
225 return this.formatTime(date); | |
226 if( Nabble.isThisYear(date) ) | |
227 return Nabble.fmt2(date.getDate()) + "." + Nabble.months[date.getMonth()]; | |
228 return this.formatDateOnly(date); | |
229 }; | |
230 })() | |
231 , | |
232 tech: new (function(){ | |
233 this.formatTime = function(date) { | |
234 return Nabble.fmt2(date.getHours()) + ":" + Nabble.fmt2(date.getMinutes()); | |
235 }; | |
236 this.formatDateOnly = function(date) { | |
237 return "" + date.getFullYear() + "-" + Nabble.fmt2(date.getMonth()+1) + "-" + Nabble.fmt2(date.getDate()) | |
238 }; | |
239 this.formatDateLong = function(date) { | |
240 return this.formatDateOnly(date) + " " + this.formatTime(date); | |
241 }; | |
242 this.formatDateShort = function(date) { | |
243 if( Nabble.isToday(date) ) | |
244 return this.formatTime(date); | |
245 if( Nabble.isThisYear(date) ) | |
246 return Nabble.fmt2(date.getMonth()+1) + "-" + Nabble.fmt2(date.getDate()); | |
247 return this.formatDateOnly(date); | |
248 }; | |
249 })() | |
250 }; | |
251 | |
252 Nabble.getDateFmt = function() { | |
253 var dateFmt = Nabble.getCookie("date_fmt"); | |
254 return dateFmt==null ? "us" : dateFmt; | |
255 }; | |
256 | |
257 Nabble.formatDateOnly = function(date) { | |
258 return Nabble.dateFormatters[Nabble.getDateFmt()].formatDateOnly(date); | |
259 }; | |
260 | |
261 Nabble.formatTimeOnly = function(date) { | |
262 return Nabble.dateFormatters[Nabble.getDateFmt()].formatTime(date); | |
263 }; | |
264 | |
265 Nabble.formatDateLong = function(date) { | |
266 return Nabble.dateFormatters[Nabble.getDateFmt()].formatDateLong(date); | |
267 }; | |
268 | |
269 Nabble.formatDateShort = function(date) { | |
270 var fmt = Nabble.dateFormatters[Nabble.getDateFmt()]; | |
271 return '<span title="' + fmt.formatDateLong(date) + '">' | |
272 + fmt.formatDateShort(date) + '</span>'; | |
273 }; | |
274 ]]> | |
275 </macro> | |
276 | |
277 <macro name="js_topic_and_post_functions" requires="servlet"> | |
278 <![CDATA[ | |
279 Nabble.messageTextWidth = function() { | |
280 var maxWidth = Nabble.getCookie("max_width"); | |
281 if( maxWidth==null ) | |
282 return; | |
283 document.write("<style type='text/css'>.nabble .message-text {max-width: "+maxWidth+";}</style>"); | |
284 }; | |
285 | |
286 Nabble.setFontSize = function() { | |
287 var fontSize = Nabble.getCookie("font_size"); | |
288 if (fontSize) | |
289 document.write("<style type='text/css'>body, table .nabble {font-size: "+fontSize+";}</style>"); | |
290 }; | |
291 | |
292 Nabble.hideQuotes = function(context) { | |
293 $('div.shrinkable-quote,blockquote',context).each(function() { | |
294 var $this = $(this); | |
295 if ($this.outerHeight() > 300) { | |
296 $this.after("<div class='shrink-quote'><span></span> [<a href='javascript:void(0)'></a>]</div>"); | |
297 } | |
298 }); | |
299 $('div.shrink-quote a', context) | |
300 .click(function(){ | |
301 var $this = $(this); | |
302 var v = parseInt($this.attr('collapsed') || 1) % 2; | |
303 if (v == 0) { | |
304 $this.prev().html( "« " ); | |
305 $this.html( "hide part of quote" ); | |
306 var $text = $this.parent().prev(); | |
307 $text.css( 'height', 'auto' ); | |
308 $text.css( 'overflowY', 'auto' ); | |
309 } else { | |
310 $this.prev().html( "..." ); | |
311 $this.html( "show rest of quote" ); | |
312 var $text = $this.parent().prev(); | |
313 $text.css('height', '300px'); | |
314 $text.css('overflowY', 'hidden' ); | |
315 } | |
316 $this.attr('collapsed', v+1); | |
317 Nabble.resizeFrames(); | |
318 }) | |
319 .click() | |
320 ; | |
321 }; | |
322 ]]> | |
323 </macro> | |
324 | |
325 <macro name="js_header_functions" requires="servlet"> | |
326 var t_login = "<t>Login</t>"; | |
327 var t_register = "<t>Register</t>"; | |
328 var t_logout = "<t>Log out</t>"; | |
329 var t_my_posts = "<t>My posts</t>"; | |
330 var t_account_settings = "<t>Account settings</t>"; | |
331 var t_show_nabble_notice = "<t>Show Nabble notice</t>"; | |
332 var t_permalink = "<t>Permalink</t>"; | |
333 var t_refresh = "<t>Refresh</t>"; | |
334 var login_path = "<n.login_path/>"; | |
335 var logout_path = "<n.logout_path/>"; | |
336 var register_path = "<n.register_path/>"; | |
337 var user_profile_path = "<n.user_profile_path/>"; | |
338 <![CDATA[ | |
339 Nabble.userId = Nabble.getCookie("userId"); | |
340 var encodedUsername = Nabble.getCookie("username"); | |
341 if (encodedUsername) { | |
342 Nabble.username = decodeURIComponent(encodedUsername) | |
343 .replace(new RegExp('\\+','g'),' '); | |
344 } | |
345 | |
346 if (!Nabble.userId) { | |
347 Nabble.anonymousId = Nabble.getCookie("anonymousId"); | |
348 var encodedName = Nabble.getCookie("anonymousName"); | |
349 if (encodedName) { | |
350 Nabble.anonymousName = decodeURIComponent(encodedName) | |
351 .replace(new RegExp('\\+','g'),' ') | |
352 .replace(new RegExp('<','g'),'<') | |
353 .replace(new RegExp('>','g'),'>'); | |
354 } | |
355 } | |
356 | |
357 Nabble.getPermalink = function() { | |
358 return Nabble.getParent().nabbleiframe.location.href; | |
359 }; | |
360 | |
361 Nabble.permalinkLabel = function() { | |
362 if (!Nabble.isEmbedded) | |
363 return ''; | |
364 var p = "<script>function openPermalink() { prompt('Copy this:', Nabble.getPermalink()); };</script>"; | |
365 p += "<a href='javascript: void openPermalink();'>"+t_permalink+"</a> "; | |
366 return p; | |
367 }; | |
368 | |
369 Nabble.refreshLabel = function() { | |
370 if (Nabble.isEmbedded) | |
371 return "<a href='javascript: location.reload(true);'>"+t_refresh+"</a> "; | |
372 return ''; | |
373 }; | |
374 | |
375 Nabble.siteHeader = function() { | |
376 var s = Nabble.refreshLabel(); | |
377 s += Nabble.permalinkLabel(); | |
378 if (!Nabble.userId) { | |
379 var nextUrl = typeof loginNextUrl == 'undefined'? window.location.href : loginNextUrl; | |
380 s += '<a href="'+login_path+'&nextUrl='+encodeURIComponent(nextUrl)+'">'+t_login+'</a>'; | |
381 s += ' '; | |
382 s += '<a href="'+register_path+'&nextUrl='+encodeURIComponent(nextUrl)+'">'+t_register+'</a>'; | |
383 if (Nabble.anonymousId && Nabble.anonymousName) { | |
384 s += ' '; | |
385 s += '<a href="/template/NamlServlet.jtp?macro=user_nodes&user=' + Nabble.anonymousId + '~' + Nabble.anonymousName + '">'; | |
386 s += Nabble.anonymousName; | |
387 s += '</a>'; | |
388 } | |
389 } else | |
390 s += '<span id="user-dd"></span>'; | |
391 return s; | |
392 }; | |
393 | |
394 Nabble.userDropdown = function() { | |
395 var $t = $('#user-dd'); | |
396 if ($t.size() == 0) | |
397 return; | |
398 $t.empty(); | |
399 var dropdown = new NabbleDropdown('usrdd', Nabble.escapeHTML(Nabble.username)); | |
400 dropdown.add('myPosts','<a href="/template/NamlServlet.jtp?macro=user_nodes&user='+Nabble.userId + '">'+t_my_posts+'</a>'); | |
401 dropdown.add('personalSettings','<a href="'+user_profile_path+'">'+t_account_settings+'</a>'); | |
402 dropdown.add('adminNotice','<a href="javascript: void Nabble.showAdminNotice()">'+t_show_nabble_notice+'</a>','display:none'); | |
403 dropdown.add('logout','<a href="javascript: void Nabble.logout()">'+t_logout+'</a>'); | |
404 $t.html(dropdown.getHtml()); | |
405 dropdownInit('usrdd'); | |
406 }; | |
407 | |
408 Nabble.addUserHeaderListener(function() { | |
409 Nabble.userDropdown(); | |
410 }); | |
411 | |
412 Nabble.showAdminNotice = function() { | |
413 Nabble.deleteCookie('admin-notice'); | |
414 window.location.reload(); | |
415 }; | |
416 | |
417 Nabble.logout = function() { | |
418 window.location = '/template/NamlServlet.jtp?macro=logout_page'; | |
419 }; | |
420 ]]> | |
421 </macro> | |
422 | |
423 <macro name="js_delete_functions" requires="servlet"> | |
424 var t_confirm_delete_post = "<t>Do you really want to delete this post?</t>"; | |
425 var t_confirm_delete_post_recursively = "<t>Do you really want to permanently delete this message and all replies?</t>"; | |
426 var t_caution_cannot_revert = "<t>CAUTION: this action cannot be reverted.</t>"; | |
427 <![CDATA[ | |
428 Nabble.deletePost = function(postId) { | |
429 if (!confirm(t_confirm_delete_post)) | |
430 return; | |
431 var newLocation = "/template/NamlServlet.jtp?macro=delete_post&node="+postId; | |
432 Nabble.setTop(newLocation); | |
433 }; | |
434 | |
435 Nabble.deleteFromSite = function(nodeId) { | |
436 if( !confirm(t_confirm_delete_post_recursively+"\n\n"+t_caution_cannot_revert) ) | |
437 return; | |
438 var newLocation = "/template/NamlServlet.jtp?macro=delete_from_site&node="+nodeId; | |
439 Nabble.setTop(newLocation); | |
440 }; | |
441 ]]> | |
442 </macro> | |
443 | |
444 <macro name="js_search_functions" requires="servlet"> | |
445 <![CDATA[ | |
446 Nabble.highlightSearchTerms = function(searchterms, elem) { | |
447 if (elem.childNodes && elem.childNodes.length > 0) { | |
448 for (var i=0; i<elem.childNodes.length; i++) { | |
449 this.highlightSearchTerms(searchterms, elem.childNodes[i]); | |
450 } | |
451 } else if (elem.nodeType) { | |
452 if (elem.nodeType == document.TEXT_NODE || elem.nodeType == 3) { | |
453 var txt = elem.nodeValue; | |
454 var rgx = new RegExp("\\b("+searchterms+")\\w*\\b", "gi"); | |
455 var result; | |
456 var start = 0; | |
457 var newFragment = document.createElement("span"); | |
458 while ((result = rgx.exec(txt)) != null) { | |
459 var end = result.index; | |
460 var textNode = document.createTextNode(txt.slice(start, end)); | |
461 newFragment.appendChild(textNode); | |
462 var hlNode = document.createElement("span"); | |
463 hlNode.className = "bold highlight search-highlight"; | |
464 hlNode.appendChild(document.createTextNode(result[0])); | |
465 newFragment.appendChild(hlNode); | |
466 start = end + result[0].length; | |
467 Nabble.hasHighlightedTerms = true; | |
468 } | |
469 newFragment.appendChild(document.createTextNode(txt.slice(start))); | |
470 elem.parentNode.replaceChild(newFragment, elem); | |
471 } | |
472 } | |
473 }; | |
474 | |
475 Nabble.getSearchTerms = function() { | |
476 var searchterms = this.getCookie("searchterms"); | |
477 if (document.referrer) { | |
478 var result = Nabble.getSearchTerms2(document.referrer); | |
479 if (result!=null) { | |
480 var query = decodeURIComponent(result); | |
481 searchterms = query.replace(/\W+/g,"|").replace(/^\|/,"").replace(/\|$/,""); | |
482 this.setCookie("searchterms", searchterms); | |
483 Nabble.gquery = query.replace(/\+/g," "); | |
484 } | |
485 } | |
486 return searchterms; | |
487 }; | |
488 | |
489 /* Logic from _uOrg() in urchin.js */ | |
490 Nabble.getSearchTerms2 = function(referrer) { | |
491 if (typeof(_uOsr)=="undefined" || typeof(_uOkw)=="undefined") | |
492 return null; | |
493 var searchEngines = _uOsr; /* from urchin */ | |
494 var searchQueries = _uOkw; /* from urchin */ | |
495 var i; | |
496 if (referrer==null || (i=referrer.indexOf("://")) < 0) | |
497 return null; | |
498 var h = referrer.substring(i+3); | |
499 var p = h.indexOf("/"); | |
500 if (p > -1) | |
501 h = h.substring(0,p); | |
502 for (var j=0; j<searchEngines.length; j++) { | |
503 var engine = searchEngines[j]; | |
504 var query = searchQueries[j]; | |
505 if (h.toLowerCase().indexOf(engine.toLowerCase()) > -1) { | |
506 if ((i=referrer.indexOf("?"+query+"=")) > -1 || (i=referrer.indexOf("&"+query+"=")) > -1) { | |
507 var k = referrer.substring(i+query.length+2,referrer.length); | |
508 if ((i=k.indexOf("&")) > -1) | |
509 k = k.substring(0,i); | |
510 return k; | |
511 } | |
512 } | |
513 } | |
514 return null; | |
515 }; | |
516 ]]> | |
517 </macro> |