view src/nabble/view/web/Javascript.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 source

<%
package nabble.view.web;

import fschmidt.util.java.HtmlUtils;
import fschmidt.util.servlet.JtpContext;
import nabble.model.Site;
import nabble.model.SystemProperties;
import nabble.naml.compiler.Template;
import nabble.naml.compiler.TemplatePrintWriter;
import nabble.naml.namespaces.BasicNamespace;
import nabble.view.lib.Jtp;
import nabble.view.web.template.NabbleNamespace;
import nabble.view.web.template.ServletNamespace;

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.io.StringWriter;
import java.util.Collections;


public final class Javascript extends HttpServlet {

	protected void service(HttpServletRequest request,HttpServletResponse response)
		throws ServletException, IOException
	{
		JtpContext jtpContext = (JtpContext)getServletContext().getAttribute(JtpContext.attrName);
		jtpContext.setEtag(request,response);
		response.setHeader("Content-Type","application/x-javascript");
		PrintWriter out = response.getWriter();

		basicNabbleFunctions(out);
		dateFunctionsJs(out);
	}

	public static void basicNabbleFunctions(PrintWriter out) {
		%>
		var Nabble = new Object();
		Nabble.main = 1;

		Nabble.getParent = function() {
			if (typeof(customParent) != 'undefined')
				return customParent();
			try {
				if (parent.Nabble.main == 1)
					return parent.parent;
			} catch(err) {}
			return parent;
		};

		if (!Array.prototype.indexOf) {
			Array.prototype.indexOf = function(e, start) {
				start = start || 0;
				if (start < 0)
					start += this.length;
				for (var i=start; i<this.length; i++)
					if (this[i] == e)
						return i;
				return -1;
			};
		}

		Nabble.get = function(id) {
			return document.getElementById(id);
		};

		Nabble.loadScript = function(url) {
			var e = document.createElement("script");
			e.src = url;
			e.type="text/javascript";
			document.getElementsByTagName("head")[0].appendChild(e);
		};

		Nabble.escapeHTML = function(str) {
			var div = document.createElement('div');
			var text = document.createTextNode(str);
			div.appendChild(text);
			return div.innerHTML;
		};

		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.getCookie = function(name) {
			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.setCookie = function(name, value) {
			var curCookie = name + "=" + Nabble.escape(value) + ";path=/";
			document.cookie = curCookie;
		};

		Nabble.setPersistentCookie = function(name, value) {
			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) {
			if (this.getCookie(name)) {
				document.cookie = name + "=" +
					"; path=/"  +
					"; expires=Thu, 01-Jan-1970 00:00:01 GMT";
			}
		};

		Nabble.vars = ["appnotice"];
		Nabble.pvars = ["tview"];  /* persistent */

		(function(){
			for(var i=0;i<Nabble.vars.length;i++) {
				var v = Nabble.vars[i];
				Nabble[v] = Nabble.getCookie(v);
			}
			for(var i=0;i<Nabble.pvars.length;i++) {
				var v = Nabble.pvars[i];
				Nabble[v] = Nabble.getCookie(v);
			}
		})();

		Nabble.handleVars = function() {
			for( var i=0; i<Nabble.vars.length; i++ ) {
				var v = Nabble.vars[i];
				if( Nabble[v] != Nabble.getCookie(v) ) {
					Nabble.setVar(v,Nabble[v]);
				}
			}
			for( var i=0; i<Nabble.pvars.length; i++ ) {
				var v = Nabble.pvars[i];
				if( Nabble[v] != Nabble.getCookie(v) ) {
					Nabble.setVar(v,this[v]);
				}
			}
		};

		Nabble.contains = function(a,v) {
			for( var i=0; i<a.length; i++ ) {
				if( a[i]==v )
					return true;
			}
			return false;
		};

		Nabble.setVar = function(v,val) {
			Nabble[v] = val;
			try {
				Nabble.getParent().Nabble[v] = val;
			} catch(err) {}
			if(val) {
				if( this.contains(this.vars,v) ) {
					this.setCookie(v,val);
				} else if( this.contains(this.pvars,v) ) {
					this.setPersistentCookie(v,val);
				} else {
					throw new Error("var not found: "+v);
				}
			} else {
				this.deleteCookie(v);
			}
		};

		Nabble.toggle = function(id, callback) {
			$('#'+id).slideToggle('slow', function(){
				if (callback) callback();
				Nabble.resizeFrames();
			});
		};

		Nabble.trim = function(s) {
			return s.replace(/^\s+|\s+$/g,'');
		};

		/* overridden in javascript_library macro */
//		Nabble.getClientID = function(){};

		var userHeaderListeners = [];
		var userHeaderReady = false;
		Nabble.addUserHeaderListener = function(listener){
			if (!userHeaderReady) userHeaderListeners.push(listener);
			else listener();
		};

		Nabble.userHeader = function() {
			$(document).ready(function(){
				var s = '';
				if (Nabble.siteHeader)
					s += Nabble.siteHeader();
				$("#nabble-user-header").html(s);
				for(var i=0;i<userHeaderListeners.length;i++)
					userHeaderListeners[i]();
				userHeaderReady = true;
			});
		};

		function notice(s, wait, fade) {
			var $n = $('#notice');
			$n.html(s);
			var hw = $n.width()/2;
			$n.css('margin-left', -hw + 'px');
			$n.show();
			if (wait && fade)
				setTimeout(function() {
					$n.fadeOut(fade);
				}, wait);
		};

		function singleSubmit(f) {
			if (f.done)
				return false;
			f.done = true;
			return true;
		};

		Nabble.analytics = function() {
			if (navigator.cookieEnabled && !Nabble.getCookie("v")) {
				var visitCounter = "/util/VisitCounter.jtp?referrer=" + encodeURIComponent(document.referrer);
				Nabble.loadScript(visitCounter);
			}
			var expires = new Date();
			expires.setTime(expires.getTime()+30*60*1000);
			document.cookie = "v=x; expires=" + expires.toGMTString() + "; path=/";
		};
		<%
	} // end genericJs

	private static void dateFunctionsJs(PrintWriter out) {
		%>
		Nabble.months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
		Nabble.now = new Date();
		Nabble.fmt2 = function(i) { return i <= 9? '0'+i:i; };

		Nabble.isToday = function(date) {
			return date.toDateString() == this.now.toDateString();
		};

		Nabble.isThisYear = function(date) {
			return date.getYear() == this.now.getYear();
		};

		Nabble.dateFormatters = {
			us: new (function(){
				this.formatTime = function(date) {
					var hours = date.getHours();
					if (hours < 12) {
						var xm = "am";
						if (hours==0)
							hours = 12;
					} else {
						var xm = "pm";
						if (hours > 12)
							hours -= 12;
					}
					return hours + ":" + Nabble.fmt2(date.getMinutes()) + xm;
				};
				this.formatDateOnly = function(date) {
					return Nabble.months[date.getMonth()] + " " + Nabble.fmt2(date.getDate()) + ", " + date.getFullYear();
				};
				this.formatDateLong = function(date) {
					return this.formatDateOnly(date) + "; " + this.formatTime(date);
				};
				this.formatDateShort = function(date) {
					if( Nabble.isToday(date) )
						return this.formatTime(date);
					if( Nabble.isThisYear(date) )
						return Nabble.months[date.getMonth()] + " " + Nabble.fmt2(date.getDate());
					return this.formatDateOnly(date);
				};
			})()
			,
			euro: new (function(){
				this.formatTime = function(date) {
					return Nabble.fmt2(date.getHours()) + ":" + Nabble.fmt2(date.getMinutes());
				};
				this.formatDateOnly = function(date) {
					return Nabble.fmt2(date.getDate()) + "." + Nabble.months[date.getMonth()] + "." + date.getFullYear();
				};
				this.formatDateLong = function(date) {
					return this.formatTime(date) + ", " + this.formatDateOnly(date);
				};
				this.formatDateShort = function(date) {
					if( Nabble.isToday(date) )
						return this.formatTime(date);
					if( Nabble.isThisYear(date) )
						return Nabble.fmt2(date.getDate()) + "." + Nabble.months[date.getMonth()];
					return this.formatDateOnly(date);
				};
			})()
			,
			tech: new (function(){
				this.formatTime = function(date) {
					return Nabble.fmt2(date.getHours()) + ":" + Nabble.fmt2(date.getMinutes());
				};
				this.formatDateOnly = function(date) {
					return "" + date.getFullYear() + "-" + Nabble.fmt2(date.getMonth()+1) + "-" + Nabble.fmt2(date.getDate())
				};
				this.formatDateLong = function(date) {
					return this.formatDateOnly(date) + " " + this.formatTime(date);
				};
				this.formatDateShort = function(date) {
					if( Nabble.isToday(date) )
						return this.formatTime(date);
					if( Nabble.isThisYear(date) )
						return Nabble.fmt2(date.getMonth()+1) + "-" + Nabble.fmt2(date.getDate());
					return this.formatDateOnly(date);
				};
			})()
		};

		Nabble.getDateFmt = function() {
			var dateFmt = Nabble.getCookie("date_fmt");
			return dateFmt==null ? "us" : dateFmt;
		};

		Nabble.formatDateOnly = function(date) {
			return Nabble.dateFormatters[Nabble.getDateFmt()].formatDateOnly(date);
		};

		Nabble.formatTimeOnly = function(date) {
			return Nabble.dateFormatters[Nabble.getDateFmt()].formatTime(date);
		};

		Nabble.formatDateLong = function(date) {
			return Nabble.dateFormatters[Nabble.getDateFmt()].formatDateLong(date);
		};

		Nabble.formatDateShort = function(date) {
			var fmt = Nabble.dateFormatters[Nabble.getDateFmt()];
			return '<span title="' + fmt.formatDateLong(date) + '">'
				+ fmt.formatDateShort(date) + '</span>';
		};
		<%
	}
}
%>