view src/nabble/view/web/Javascript.java @ 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) {
		
		out.print( "\r\nvar Nabble = new Object();\r\nNabble.main = 1;\r\n\r\nNabble.getParent = function() {\r\n	if (typeof(customParent) != 'undefined')\r\n		return customParent();\r\n	try {\r\n		if (parent.Nabble.main == 1)\r\n			return parent.parent;\r\n	} catch(err) {}\r\n	return parent;\r\n};\r\n\r\nif (!Array.prototype.indexOf) {\r\n	Array.prototype.indexOf = function(e, start) {\r\n		start = start || 0;\r\n		if (start < 0)\r\n			start += this.length;\r\n		for (var i=start; i<this.length; i++)\r\n			if (this[i] == e)\r\n				return i;\r\n		return -1;\r\n	};\r\n}\r\n\r\nNabble.get = function(id) {\r\n	return document.getElementById(id);\r\n};\r\n\r\nNabble.loadScript = function(url) {\r\n	var e = document.createElement(\"script\");\r\n	e.src = url;\r\n	e.type=\"text/javascript\";\r\n	document.getElementsByTagName(\"head\")[0].appendChild(e);\r\n};\r\n\r\nNabble.escapeHTML = function(str) {\r\n	var div = document.createElement('div');\r\n	var text = document.createTextNode(str);\r\n	div.appendChild(text);\r\n	return div.innerHTML;\r\n};\r\n\r\nNabble.escape = function(value) {\r\n	if (typeof value == 'string') {\r\n		var hasSpace = value.indexOf(' ') >= 0;\r\n		var hasQuote = value.indexOf('\"') >= 0;\r\n\r\n		value = value.replace(/\\;/g, '%3B');\r\n		value = value.replace(/\"/g, '\\\\\"');\r\n\r\n		if (hasSpace || hasQuote)\r\n			value = '\"' + value + '\"';\r\n	}\r\n	return value;\r\n};\r\n\r\nNabble.unescape = function(value) {\r\n	if (value.charAt(0) == '\"' && value.charAt(value.length-1) == '\"')\r\n		value = value.substring(1, value.length-1);\r\n\r\n	value = value.replace(/\\\\\"/g, '\"');\r\n	value = value.replace(/%3B/g, ';');\r\n	return value;\r\n};\r\n\r\nNabble.getCookie = function(name) {\r\n	var dc = document.cookie;\r\n	var prefix = name + \"=\";\r\n	var begin = dc.indexOf(\"; \" + prefix);\r\n	if (begin == -1) {\r\n		begin = dc.indexOf(prefix);\r\n		if (begin != 0) return null;\r\n	} else\r\n		begin += 2;\r\n	var end = document.cookie.indexOf(\";\", begin);\r\n	if (end == -1)\r\n		end = dc.length;\r\n	return Nabble.unescape(dc.substring(begin + prefix.length, end));\r\n};\r\n\r\nNabble.setCookie = function(name, value) {\r\n	var curCookie = name + \"=\" + Nabble.escape(value) + \";path=/\";\r\n	document.cookie = curCookie;\r\n};\r\n\r\nNabble.setPersistentCookie = function(name, value) {\r\n	var expires = new Date();\r\n	expires.setFullYear(expires.getFullYear()+10);\r\n	var curCookie = name + \"=\" + Nabble.escape(value) + \"; expires=\" + expires.toGMTString() + \"; path=/\";\r\n	document.cookie = curCookie;\r\n};\r\n\r\nNabble.deleteCookie = function(name) {\r\n	if (this.getCookie(name)) {\r\n		document.cookie = name + \"=\" +\r\n			\"; path=/\"  +\r\n			\"; expires=Thu, 01-Jan-1970 00:00:01 GMT\";\r\n	}\r\n};\r\n\r\nNabble.vars = [\"appnotice\"];\r\nNabble.pvars = [\"tview\"];  /* persistent */\r\n\r\n(function(){\r\n	for(var i=0;i<Nabble.vars.length;i++) {\r\n		var v = Nabble.vars[i];\r\n		Nabble[v] = Nabble.getCookie(v);\r\n	}\r\n	for(var i=0;i<Nabble.pvars.length;i++) {\r\n		var v = Nabble.pvars[i];\r\n		Nabble[v] = Nabble.getCookie(v);\r\n	}\r\n})();\r\n\r\nNabble.handleVars = function() {\r\n	for( var i=0; i<Nabble.vars.length; i++ ) {\r\n		var v = Nabble.vars[i];\r\n		if( Nabble[v] != Nabble.getCookie(v) ) {\r\n			Nabble.setVar(v,Nabble[v]);\r\n		}\r\n	}\r\n	for( var i=0; i<Nabble.pvars.length; i++ ) {\r\n		var v = Nabble.pvars[i];\r\n		if( Nabble[v] != Nabble.getCookie(v) ) {\r\n			Nabble.setVar(v,this[v]);\r\n		}\r\n	}\r\n};\r\n\r\nNabble.contains = function(a,v) {\r\n	for( var i=0; i<a.length; i++ ) {\r\n		if( a[i]==v )\r\n			return true;\r\n	}\r\n	return false;\r\n};\r\n\r\nNabble.setVar = function(v,val) {\r\n	Nabble[v] = val;\r\n	try {\r\n		Nabble.getParent().Nabble[v] = val;\r\n	} catch(err) {}\r\n	if(val) {\r\n		if( this.contains(this.vars,v) ) {\r\n			this.setCookie(v,val);\r\n		} else if( this.contains(this.pvars,v) ) {\r\n			this.setPersistentCookie(v,val);\r\n		} else {\r\n			throw new Error(\"var not found: \"+v);\r\n		}\r\n	} else {\r\n		this.deleteCookie(v);\r\n	}\r\n};\r\n\r\nNabble.toggle = function(id, callback) {\r\n	$('#'+id).slideToggle('slow', function(){\r\n		if (callback) callback();\r\n		Nabble.resizeFrames();\r\n	});\r\n};\r\n\r\nNabble.trim = function(s) {\r\n	return s.replace(/^\\s+|\\s+$/g,'');\r\n};\r\n\r\n/* overridden in javascript_library macro */\r\n//		Nabble.getClientID = function(){};\r\n\r\nvar userHeaderListeners = [];\r\nvar userHeaderReady = false;\r\nNabble.addUserHeaderListener = function(listener){\r\n	if (!userHeaderReady) userHeaderListeners.push(listener);\r\n	else listener();\r\n};\r\n\r\nNabble.userHeader = function() {\r\n	$(document).ready(function(){\r\n		var s = '';\r\n		if (Nabble.siteHeader)\r\n			s += Nabble.siteHeader();\r\n		$(\"#nabble-user-header\").html(s);\r\n		for(var i=0;i<userHeaderListeners.length;i++)\r\n			userHeaderListeners[i]();\r\n		userHeaderReady = true;\r\n	});\r\n};\r\n\r\nfunction notice(s, wait, fade) {\r\n	var $n = $('#notice');\r\n	$n.html(s);\r\n	var hw = $n.width()/2;\r\n	$n.css('margin-left', -hw + 'px');\r\n	$n.show();\r\n	if (wait && fade)\r\n		setTimeout(function() {\r\n			$n.fadeOut(fade);\r\n		}, wait);\r\n};\r\n\r\nfunction singleSubmit(f) {\r\n	if (f.done)\r\n		return false;\r\n	f.done = true;\r\n	return true;\r\n};\r\n\r\nNabble.analytics = function() {\r\n	if (navigator.cookieEnabled && !Nabble.getCookie(\"v\")) {\r\n		var visitCounter = \"/util/VisitCounter.jtp?referrer=\" + encodeURIComponent(document.referrer);\r\n		Nabble.loadScript(visitCounter);\r\n	}\r\n	var expires = new Date();\r\n	expires.setTime(expires.getTime()+30*60*1000);\r\n	document.cookie = \"v=x; expires=\" + expires.toGMTString() + \"; path=/\";\r\n};\r\n" );

	} // end genericJs

	private static void dateFunctionsJs(PrintWriter out) {
		
		out.print( "\r\nNabble.months = [\"Jan\",\"Feb\",\"Mar\",\"Apr\",\"May\",\"Jun\",\"Jul\",\"Aug\",\"Sep\",\"Oct\",\"Nov\",\"Dec\"];\r\nNabble.now = new Date();\r\nNabble.fmt2 = function(i) { return i <= 9? '0'+i:i; };\r\n\r\nNabble.isToday = function(date) {\r\n	return date.toDateString() == this.now.toDateString();\r\n};\r\n\r\nNabble.isThisYear = function(date) {\r\n	return date.getYear() == this.now.getYear();\r\n};\r\n\r\nNabble.dateFormatters = {\r\n	us: new (function(){\r\n		this.formatTime = function(date) {\r\n			var hours = date.getHours();\r\n			if (hours < 12) {\r\n				var xm = \"am\";\r\n				if (hours==0)\r\n					hours = 12;\r\n			} else {\r\n				var xm = \"pm\";\r\n				if (hours > 12)\r\n					hours -= 12;\r\n			}\r\n			return hours + \":\" + Nabble.fmt2(date.getMinutes()) + xm;\r\n		};\r\n		this.formatDateOnly = function(date) {\r\n			return Nabble.months[date.getMonth()] + \" \" + Nabble.fmt2(date.getDate()) + \", \" + date.getFullYear();\r\n		};\r\n		this.formatDateLong = function(date) {\r\n			return this.formatDateOnly(date) + \"; \" + this.formatTime(date);\r\n		};\r\n		this.formatDateShort = function(date) {\r\n			if( Nabble.isToday(date) )\r\n				return this.formatTime(date);\r\n			if( Nabble.isThisYear(date) )\r\n				return Nabble.months[date.getMonth()] + \" \" + Nabble.fmt2(date.getDate());\r\n			return this.formatDateOnly(date);\r\n		};\r\n	})()\r\n	,\r\n	euro: new (function(){\r\n		this.formatTime = function(date) {\r\n			return Nabble.fmt2(date.getHours()) + \":\" + Nabble.fmt2(date.getMinutes());\r\n		};\r\n		this.formatDateOnly = function(date) {\r\n			return Nabble.fmt2(date.getDate()) + \".\" + Nabble.months[date.getMonth()] + \".\" + date.getFullYear();\r\n		};\r\n		this.formatDateLong = function(date) {\r\n			return this.formatTime(date) + \", \" + this.formatDateOnly(date);\r\n		};\r\n		this.formatDateShort = function(date) {\r\n			if( Nabble.isToday(date) )\r\n				return this.formatTime(date);\r\n			if( Nabble.isThisYear(date) )\r\n				return Nabble.fmt2(date.getDate()) + \".\" + Nabble.months[date.getMonth()];\r\n			return this.formatDateOnly(date);\r\n		};\r\n	})()\r\n	,\r\n	tech: new (function(){\r\n		this.formatTime = function(date) {\r\n			return Nabble.fmt2(date.getHours()) + \":\" + Nabble.fmt2(date.getMinutes());\r\n		};\r\n		this.formatDateOnly = function(date) {\r\n			return \"\" + date.getFullYear() + \"-\" + Nabble.fmt2(date.getMonth()+1) + \"-\" + Nabble.fmt2(date.getDate())\r\n		};\r\n		this.formatDateLong = function(date) {\r\n			return this.formatDateOnly(date) + \" \" + this.formatTime(date);\r\n		};\r\n		this.formatDateShort = function(date) {\r\n			if( Nabble.isToday(date) )\r\n				return this.formatTime(date);\r\n			if( Nabble.isThisYear(date) )\r\n				return Nabble.fmt2(date.getMonth()+1) + \"-\" + Nabble.fmt2(date.getDate());\r\n			return this.formatDateOnly(date);\r\n		};\r\n	})()\r\n};\r\n\r\nNabble.getDateFmt = function() {\r\n	var dateFmt = Nabble.getCookie(\"date_fmt\");\r\n	return dateFmt==null ? \"us\" : dateFmt;\r\n};\r\n\r\nNabble.formatDateOnly = function(date) {\r\n	return Nabble.dateFormatters[Nabble.getDateFmt()].formatDateOnly(date);\r\n};\r\n\r\nNabble.formatTimeOnly = function(date) {\r\n	return Nabble.dateFormatters[Nabble.getDateFmt()].formatTime(date);\r\n};\r\n\r\nNabble.formatDateLong = function(date) {\r\n	return Nabble.dateFormatters[Nabble.getDateFmt()].formatDateLong(date);\r\n};\r\n\r\nNabble.formatDateShort = function(date) {\r\n	var fmt = Nabble.dateFormatters[Nabble.getDateFmt()];\r\n	return '<span title=\"' + fmt.formatDateLong(date) + '\">'\r\n		+ fmt.formatDateShort(date) + '</span>';\r\n};\r\n" );

	}
}