0
|
1 <%
|
|
2 package nabble.view.web;
|
|
3
|
|
4 import fschmidt.util.java.HtmlUtils;
|
|
5 import fschmidt.util.servlet.JtpContext;
|
|
6 import nabble.model.Site;
|
|
7 import nabble.model.SystemProperties;
|
|
8 import nabble.naml.compiler.Template;
|
|
9 import nabble.naml.compiler.TemplatePrintWriter;
|
|
10 import nabble.naml.namespaces.BasicNamespace;
|
|
11 import nabble.view.lib.Jtp;
|
|
12 import nabble.view.web.template.NabbleNamespace;
|
|
13 import nabble.view.web.template.ServletNamespace;
|
|
14
|
|
15 import javax.servlet.ServletException;
|
|
16 import javax.servlet.http.HttpServlet;
|
|
17 import javax.servlet.http.HttpServletRequest;
|
|
18 import javax.servlet.http.HttpServletResponse;
|
|
19 import java.io.IOException;
|
|
20 import java.io.PrintWriter;
|
|
21 import java.io.StringWriter;
|
|
22 import java.util.Collections;
|
|
23
|
|
24
|
|
25 public final class Javascript extends HttpServlet {
|
|
26
|
|
27 protected void service(HttpServletRequest request,HttpServletResponse response)
|
|
28 throws ServletException, IOException
|
|
29 {
|
|
30 JtpContext jtpContext = (JtpContext)getServletContext().getAttribute(JtpContext.attrName);
|
|
31 jtpContext.setEtag(request,response);
|
|
32 response.setHeader("Content-Type","application/x-javascript");
|
|
33 PrintWriter out = response.getWriter();
|
|
34
|
|
35 basicNabbleFunctions(out);
|
|
36 dateFunctionsJs(out);
|
|
37 }
|
|
38
|
|
39 public static void basicNabbleFunctions(PrintWriter out) {
|
|
40 %>
|
|
41 var Nabble = new Object();
|
|
42 Nabble.main = 1;
|
|
43
|
|
44 Nabble.getParent = function() {
|
|
45 if (typeof(customParent) != 'undefined')
|
|
46 return customParent();
|
|
47 try {
|
|
48 if (parent.Nabble.main == 1)
|
|
49 return parent.parent;
|
|
50 } catch(err) {}
|
|
51 return parent;
|
|
52 };
|
|
53
|
|
54 if (!Array.prototype.indexOf) {
|
|
55 Array.prototype.indexOf = function(e, start) {
|
|
56 start = start || 0;
|
|
57 if (start < 0)
|
|
58 start += this.length;
|
|
59 for (var i=start; i<this.length; i++)
|
|
60 if (this[i] == e)
|
|
61 return i;
|
|
62 return -1;
|
|
63 };
|
|
64 }
|
|
65
|
|
66 Nabble.get = function(id) {
|
|
67 return document.getElementById(id);
|
|
68 };
|
|
69
|
|
70 Nabble.loadScript = function(url) {
|
|
71 var e = document.createElement("script");
|
|
72 e.src = url;
|
|
73 e.type="text/javascript";
|
|
74 document.getElementsByTagName("head")[0].appendChild(e);
|
|
75 };
|
|
76
|
|
77 Nabble.escapeHTML = function(str) {
|
|
78 var div = document.createElement('div');
|
|
79 var text = document.createTextNode(str);
|
|
80 div.appendChild(text);
|
|
81 return div.innerHTML;
|
|
82 };
|
|
83
|
|
84 Nabble.escape = function(value) {
|
|
85 if (typeof value == 'string') {
|
|
86 var hasSpace = value.indexOf(' ') >= 0;
|
|
87 var hasQuote = value.indexOf('"') >= 0;
|
|
88
|
|
89 value = value.replace(/\;/g, '%3B');
|
|
90 value = value.replace(/"/g, '\\"');
|
|
91
|
|
92 if (hasSpace || hasQuote)
|
|
93 value = '"' + value + '"';
|
|
94 }
|
|
95 return value;
|
|
96 };
|
|
97
|
|
98 Nabble.unescape = function(value) {
|
|
99 if (value.charAt(0) == '"' && value.charAt(value.length-1) == '"')
|
|
100 value = value.substring(1, value.length-1);
|
|
101
|
|
102 value = value.replace(/\\"/g, '"');
|
|
103 value = value.replace(/%3B/g, ';');
|
|
104 return value;
|
|
105 };
|
|
106
|
|
107 Nabble.getCookie = function(name) {
|
|
108 var dc = document.cookie;
|
|
109 var prefix = name + "=";
|
|
110 var begin = dc.indexOf("; " + prefix);
|
|
111 if (begin == -1) {
|
|
112 begin = dc.indexOf(prefix);
|
|
113 if (begin != 0) return null;
|
|
114 } else
|
|
115 begin += 2;
|
|
116 var end = document.cookie.indexOf(";", begin);
|
|
117 if (end == -1)
|
|
118 end = dc.length;
|
|
119 return Nabble.unescape(dc.substring(begin + prefix.length, end));
|
|
120 };
|
|
121
|
|
122 Nabble.setCookie = function(name, value) {
|
|
123 var curCookie = name + "=" + Nabble.escape(value) + ";path=/";
|
|
124 document.cookie = curCookie;
|
|
125 };
|
|
126
|
|
127 Nabble.setPersistentCookie = function(name, value) {
|
|
128 var expires = new Date();
|
|
129 expires.setFullYear(expires.getFullYear()+10);
|
|
130 var curCookie = name + "=" + Nabble.escape(value) + "; expires=" + expires.toGMTString() + "; path=/";
|
|
131 document.cookie = curCookie;
|
|
132 };
|
|
133
|
|
134 Nabble.deleteCookie = function(name) {
|
|
135 if (this.getCookie(name)) {
|
|
136 document.cookie = name + "=" +
|
|
137 "; path=/" +
|
|
138 "; expires=Thu, 01-Jan-1970 00:00:01 GMT";
|
|
139 }
|
|
140 };
|
|
141
|
|
142 Nabble.vars = ["appnotice"];
|
|
143 Nabble.pvars = ["tview"]; /* persistent */
|
|
144
|
|
145 (function(){
|
|
146 for(var i=0;i<Nabble.vars.length;i++) {
|
|
147 var v = Nabble.vars[i];
|
|
148 Nabble[v] = Nabble.getCookie(v);
|
|
149 }
|
|
150 for(var i=0;i<Nabble.pvars.length;i++) {
|
|
151 var v = Nabble.pvars[i];
|
|
152 Nabble[v] = Nabble.getCookie(v);
|
|
153 }
|
|
154 })();
|
|
155
|
|
156 Nabble.handleVars = function() {
|
|
157 for( var i=0; i<Nabble.vars.length; i++ ) {
|
|
158 var v = Nabble.vars[i];
|
|
159 if( Nabble[v] != Nabble.getCookie(v) ) {
|
|
160 Nabble.setVar(v,Nabble[v]);
|
|
161 }
|
|
162 }
|
|
163 for( var i=0; i<Nabble.pvars.length; i++ ) {
|
|
164 var v = Nabble.pvars[i];
|
|
165 if( Nabble[v] != Nabble.getCookie(v) ) {
|
|
166 Nabble.setVar(v,this[v]);
|
|
167 }
|
|
168 }
|
|
169 };
|
|
170
|
|
171 Nabble.contains = function(a,v) {
|
|
172 for( var i=0; i<a.length; i++ ) {
|
|
173 if( a[i]==v )
|
|
174 return true;
|
|
175 }
|
|
176 return false;
|
|
177 };
|
|
178
|
|
179 Nabble.setVar = function(v,val) {
|
|
180 Nabble[v] = val;
|
|
181 try {
|
|
182 Nabble.getParent().Nabble[v] = val;
|
|
183 } catch(err) {}
|
|
184 if(val) {
|
|
185 if( this.contains(this.vars,v) ) {
|
|
186 this.setCookie(v,val);
|
|
187 } else if( this.contains(this.pvars,v) ) {
|
|
188 this.setPersistentCookie(v,val);
|
|
189 } else {
|
|
190 throw new Error("var not found: "+v);
|
|
191 }
|
|
192 } else {
|
|
193 this.deleteCookie(v);
|
|
194 }
|
|
195 };
|
|
196
|
|
197 Nabble.toggle = function(id, callback) {
|
|
198 $('#'+id).slideToggle('slow', function(){
|
|
199 if (callback) callback();
|
|
200 Nabble.resizeFrames();
|
|
201 });
|
|
202 };
|
|
203
|
|
204 Nabble.trim = function(s) {
|
|
205 return s.replace(/^\s+|\s+$/g,'');
|
|
206 };
|
|
207
|
|
208 /* overridden in javascript_library macro */
|
|
209 // Nabble.getClientID = function(){};
|
|
210
|
|
211 var userHeaderListeners = [];
|
|
212 var userHeaderReady = false;
|
|
213 Nabble.addUserHeaderListener = function(listener){
|
|
214 if (!userHeaderReady) userHeaderListeners.push(listener);
|
|
215 else listener();
|
|
216 };
|
|
217
|
|
218 Nabble.userHeader = function() {
|
|
219 $(document).ready(function(){
|
|
220 var s = '';
|
|
221 if (Nabble.siteHeader)
|
|
222 s += Nabble.siteHeader();
|
|
223 $("#nabble-user-header").html(s);
|
|
224 for(var i=0;i<userHeaderListeners.length;i++)
|
|
225 userHeaderListeners[i]();
|
|
226 userHeaderReady = true;
|
|
227 });
|
|
228 };
|
|
229
|
|
230 function notice(s, wait, fade) {
|
|
231 var $n = $('#notice');
|
|
232 $n.html(s);
|
|
233 var hw = $n.width()/2;
|
|
234 $n.css('margin-left', -hw + 'px');
|
|
235 $n.show();
|
|
236 if (wait && fade)
|
|
237 setTimeout(function() {
|
|
238 $n.fadeOut(fade);
|
|
239 }, wait);
|
|
240 };
|
|
241
|
|
242 function singleSubmit(f) {
|
|
243 if (f.done)
|
|
244 return false;
|
|
245 f.done = true;
|
|
246 return true;
|
|
247 };
|
|
248
|
|
249 Nabble.analytics = function() {
|
|
250 if (navigator.cookieEnabled && !Nabble.getCookie("v")) {
|
|
251 var visitCounter = "/util/VisitCounter.jtp?referrer=" + encodeURIComponent(document.referrer);
|
|
252 Nabble.loadScript(visitCounter);
|
|
253 }
|
|
254 var expires = new Date();
|
|
255 expires.setTime(expires.getTime()+30*60*1000);
|
|
256 document.cookie = "v=x; expires=" + expires.toGMTString() + "; path=/";
|
|
257 };
|
|
258 <%
|
|
259 } // end genericJs
|
|
260
|
|
261 private static void dateFunctionsJs(PrintWriter out) {
|
|
262 %>
|
|
263 Nabble.months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
|
|
264 Nabble.now = new Date();
|
|
265 Nabble.fmt2 = function(i) { return i <= 9? '0'+i:i; };
|
|
266
|
|
267 Nabble.isToday = function(date) {
|
|
268 return date.toDateString() == this.now.toDateString();
|
|
269 };
|
|
270
|
|
271 Nabble.isThisYear = function(date) {
|
|
272 return date.getYear() == this.now.getYear();
|
|
273 };
|
|
274
|
|
275 Nabble.dateFormatters = {
|
|
276 us: new (function(){
|
|
277 this.formatTime = function(date) {
|
|
278 var hours = date.getHours();
|
|
279 if (hours < 12) {
|
|
280 var xm = "am";
|
|
281 if (hours==0)
|
|
282 hours = 12;
|
|
283 } else {
|
|
284 var xm = "pm";
|
|
285 if (hours > 12)
|
|
286 hours -= 12;
|
|
287 }
|
|
288 return hours + ":" + Nabble.fmt2(date.getMinutes()) + xm;
|
|
289 };
|
|
290 this.formatDateOnly = function(date) {
|
|
291 return Nabble.months[date.getMonth()] + " " + Nabble.fmt2(date.getDate()) + ", " + date.getFullYear();
|
|
292 };
|
|
293 this.formatDateLong = function(date) {
|
|
294 return this.formatDateOnly(date) + "; " + this.formatTime(date);
|
|
295 };
|
|
296 this.formatDateShort = function(date) {
|
|
297 if( Nabble.isToday(date) )
|
|
298 return this.formatTime(date);
|
|
299 if( Nabble.isThisYear(date) )
|
|
300 return Nabble.months[date.getMonth()] + " " + Nabble.fmt2(date.getDate());
|
|
301 return this.formatDateOnly(date);
|
|
302 };
|
|
303 })()
|
|
304 ,
|
|
305 euro: new (function(){
|
|
306 this.formatTime = function(date) {
|
|
307 return Nabble.fmt2(date.getHours()) + ":" + Nabble.fmt2(date.getMinutes());
|
|
308 };
|
|
309 this.formatDateOnly = function(date) {
|
|
310 return Nabble.fmt2(date.getDate()) + "." + Nabble.months[date.getMonth()] + "." + date.getFullYear();
|
|
311 };
|
|
312 this.formatDateLong = function(date) {
|
|
313 return this.formatTime(date) + ", " + this.formatDateOnly(date);
|
|
314 };
|
|
315 this.formatDateShort = function(date) {
|
|
316 if( Nabble.isToday(date) )
|
|
317 return this.formatTime(date);
|
|
318 if( Nabble.isThisYear(date) )
|
|
319 return Nabble.fmt2(date.getDate()) + "." + Nabble.months[date.getMonth()];
|
|
320 return this.formatDateOnly(date);
|
|
321 };
|
|
322 })()
|
|
323 ,
|
|
324 tech: new (function(){
|
|
325 this.formatTime = function(date) {
|
|
326 return Nabble.fmt2(date.getHours()) + ":" + Nabble.fmt2(date.getMinutes());
|
|
327 };
|
|
328 this.formatDateOnly = function(date) {
|
|
329 return "" + date.getFullYear() + "-" + Nabble.fmt2(date.getMonth()+1) + "-" + Nabble.fmt2(date.getDate())
|
|
330 };
|
|
331 this.formatDateLong = function(date) {
|
|
332 return this.formatDateOnly(date) + " " + this.formatTime(date);
|
|
333 };
|
|
334 this.formatDateShort = function(date) {
|
|
335 if( Nabble.isToday(date) )
|
|
336 return this.formatTime(date);
|
|
337 if( Nabble.isThisYear(date) )
|
|
338 return Nabble.fmt2(date.getMonth()+1) + "-" + Nabble.fmt2(date.getDate());
|
|
339 return this.formatDateOnly(date);
|
|
340 };
|
|
341 })()
|
|
342 };
|
|
343
|
|
344 Nabble.getDateFmt = function() {
|
|
345 var dateFmt = Nabble.getCookie("date_fmt");
|
|
346 return dateFmt==null ? "us" : dateFmt;
|
|
347 };
|
|
348
|
|
349 Nabble.formatDateOnly = function(date) {
|
|
350 return Nabble.dateFormatters[Nabble.getDateFmt()].formatDateOnly(date);
|
|
351 };
|
|
352
|
|
353 Nabble.formatTimeOnly = function(date) {
|
|
354 return Nabble.dateFormatters[Nabble.getDateFmt()].formatTime(date);
|
|
355 };
|
|
356
|
|
357 Nabble.formatDateLong = function(date) {
|
|
358 return Nabble.dateFormatters[Nabble.getDateFmt()].formatDateLong(date);
|
|
359 };
|
|
360
|
|
361 Nabble.formatDateShort = function(date) {
|
|
362 var fmt = Nabble.dateFormatters[Nabble.getDateFmt()];
|
|
363 return '<span title="' + fmt.formatDateLong(date) + '">'
|
|
364 + fmt.formatDateShort(date) + '</span>';
|
|
365 };
|
|
366 <%
|
|
367 }
|
|
368 }
|
|
369 %>
|