prettify

baseline

Web Server in Go

package main

import (
	"log"
	"net/http"
	"strings"
)
func handler(w http.ResponseWriter, r *http.Request) {
	var sPath string
	if r.Method == "GET" {
		log.Println(r.URL.Path)
		if strings.HasPrefix(r.URL.Path, "/") {
			sPath = r.URL.Path[1:]
		} else {
			sPath = r.URL.Path
		}
		http.ServeFile(w, r, sPath)
	}
}
func main() {
	log.Printf("About to listen on localhost. Go to http://localhost")
	http.HandleFunc("/", handler)
	err := http.ListenAndServe(":80", nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}

Draggable IFRAME

Main.html
<html>
<head>
<title>Draggable IFrame</title>
<style>
iframe {
   position:absolute;
   left:300px;
   top:200px;
}
</style>
</head>
<body>
<iframe src="contents.html"></iframe>
</body></html>
Frame Content:contents.html
<html>
<head>
<title>Draggable IFrame</title>
<style>
#header {
   text-align:center;
   background-color:#0ff;
}
</style>
</head>
<body>
<div id="header" class="draggable">Title</div>
<div>this is content</div>
<script defer src="IFrame.js"></script>
</body></html>
IFrame.js
"use strict";
var addHandle = function(o){
 o.style.cursor="move";
 o.addEventListener("mousedown", mousedown, false)
};
var mousedown = function(evt){
 var oStyle = evt.target.ownerDocument.documentElement.style;
 oStyle.WebkitUserSelect = "none";
 oStyle.MozUserSelect = "none";
 oStyle.MsUserSelect = "none";
 oStyle.userSelect = "none";
 
 evt.target.nX = evt.pageX;
 evt.target.nY = evt.pageY;

 evt.target.addEventListener("mouseup", mouseup);
 evt.target.addEventListener("mousemove", mousemove);
};
var mouseup = function(evt){
 evt.target.style.cursor = "move";
 
 var oStyle = evt.target.ownerDocument.documentElement.style;
 oStyle.WebkitUserSelect = "all";
 oStyle.MozUserSelect = "all";
 oStyle.MsUserSelect = "all";
 oStyle.userSelect = "all";

 evt.target.removeEventListener("mousemove", mousemove);
 evt.target.removeEventListener("mouseup", mouseup);
};
var mousemove = function(evt){
 var oFrame = window.frameElement;
 var oRect = oFrame.getBoundingClientRect();
 var nX = oRect.left - (evt.target.nX - evt.pageX);
 var nY = oRect.top - (evt.target.nY - evt.pageY);
 nX = (nX >= 0? nX:0);
 nY = (nY >= 0? nY:0);
 
 oFrame.style.left = nX.toString()+"px";
 oFrame.style.top = nY.toString()+"px";
};
(function(){
 var Elems = document.getElementsByClassName("draggable");
 for (var nPos = 0; nPos < Elems.length; nPos++) {
  addHandle(Elems[nPos]);
 }
})();

Browser Privacy

Sites I visit get a lot of info about me that they shouldn't. There are 4 levels of privacy while browsing:
  1. Everything is in the open. This is the default for all browsers.
  2. Incognito-Privacy mode.
    Visits are not stored in browser history.
    Cookies are deleted
    Flash cookies may be deleted after Flash v10.3
  3. Minimum privacy.
    Only session cookies.
    Longer duration cookies deleted on browser close.
    Browser history is NOT deleted (do NOT use it for porn)
  4. Tor

HTML5 rating 1-5
















Javascript Unit Testing TDD

A simple HTML Page that runs all your unit tests (Only Is.Equal is implemented but more are coming).
The web app should be build as MVC or at least have a distinct Model (set of .js files).

Javascript Class Template

function Person(oInit) {
    this.Clear();
    if (typeof oInit === "object") { this.Init(oInit); }
    this.Init(oInit)
}
Person.sId = "Party";
Person.sColor = "#00f";
Person.sIcon = "../img/user_suit.png";

Person.prototype.Clear = function () {
    this.nkPer = 0;
    this.rvPer = 0;
    this.sPerLNm = "";
    this.sPerEml = "";
};

String.prototype javascript

String.prototype.PadLeft = function (nLen, sPad) {
    if (typeof sPad !== "string") sPad = " ";
    if (typeof nLen !== "number") nLen = 2;
    var sResult = this;
    while (sResult.length < nLen) sResult = sPad + sResult;
    return sResult;
};
String.prototype.PadRight = function(sPad, nLen) {
    if (typeof sPad !== "string") sPad = " ";
    if (typeof nLen !== "number") nLen = 2;
    var sResult = this;
    while (sResult.length < nLen) sResult += sPad;
    return sResult;
};
String.prototype.Trim = function () {
    return this.replace(/^\s+|\s+$/g, "");
};
String.prototype.TrimLeft = function () {
    return this.replace(/^\s+/, "");
};
String.prototype.TrimRight = function () {
    return this.replace(/\s+$/, "");
};

Date.prototype javascript

Date.prototype.Format = function (sFormat) {
    if (typeof sFormat !== "string") 
        return this.getFullYear().toString() + 
               (this.getMonth() + 1).toString().PadLeft("0", 2) +
               this.getDate().toString().PadLeft("0", 2);
    var that = this;
    return sFormat.replace(/dd?d?d?|MM?M?M?|yy?y?y?|hh?|HH?|mm?|ss?/g,
    function (sFormat) {
        switch (sFormat) {
        case "yyyy": return that.getFullYear().toString();
        case "yy": return that.getFullYear().toString().substring(2, 4);
        case "MM": return (that.getMonth() + 1).toString().PadLeft(2, "0");
        case "dd": return that.getDate().toString().PadLeft(2, "0");
        case "hh": return (that.getHours() <= 12 ? that.getHours() : 
                   (that.getHours() - 12)).toString().PadLeft(2, "0");
        case "HH": return that.getHours().toString().PadLeft(2, "0");
        case "mm": return that.getMinutes().toString().PadLeft(2, "0");
        case "ss": return that.getSeconds().toString().PadLeft(2, "0");
        case "M": return (that.getMonth() + 1).toString();
        case "d": return that.getDate().toString();
        case "h": return (that.getHours() <= 12 ? that.getHours() : 
                  (that.getHours() - 12)).toString();
        case "H": return that.getHours().toString();
        case "m": return tht.getMinutes().toString();
        case "s": return that.getSeconds().toString();
        }
    });
};

Color Wheels

Simplified Color Wheels for Natural Color System
f00 ff0 0f0 00f
Primary Colors (R-G, B-Y): 4

C# Collection Class Template

Use this class as a template for Collection classes.
This is the class Persons to accompany the class Person.

Javascript Collection Class

Javascript does not need a Template for Connection Classes.
One Class fits all!
Items.js:

C# class template

Template for a business class in C#.
The example is the "Party" from the "Party-Person-Organization" pattern.

CORS Cross Origin Resource Sharing Javascript

For many years we were stuck with JSONP if we needed to make a cross domain query.
JSONP was OK except for one major point: The amount of data to send where limited by max URL length and then again it had to be URI encoded.
Now we have a solution supported by all browsers except IE7 & Opera (for some reason).

Google Translate using JSONP

We have a web page.
In this page there is a set of Fields (input-text/ textarea) with Greek (or whatever google supports) text content.
There are some corresponding Fields where we like to have the english translation.
First get a key from Google API console
You have to remember that max URL length is 2038 characters for IE, that gives less than 1K for the string to translate. You can get more (a lot) from chrome, firefox, opera (>64K).
Google says that you can to it with POST instead of GET, but that is another post.

Special Characters for HTML

Special characters with their HTML code like:
½ ¼ ¾ © ® ™ ℗
Unicode characters that have not HTML codes but can be used in HTML Pages like:
☎ ✉ ✔ ☒ ✖

HTML5 Sticky Footer

Table layouts are fine for the nonconformists among us.
But there are cases we realy need a CSS layout. Like when we have to rearrange the layout depending on the window size of the browser, to accommodate everything from mobile browsers to extra wide monitors.
Here is a Sticky Footer CSS layout.

Reference

CSS drop caps
Various Font Stacks
and more…

Protect post from copy paste

Friends keep asking me how to protect their blog posts from being copied.
As you can see I have no protection for my blogs because I think that the effort is futile. I'v seen posts of mine plainly copied in other blogs. In some cases they give a reference to my blog, and for those I am really happy.
In other cases they have no reference even though they just copied and pasted without any work of their own.
Still, my friends want to restrict copy-paste, so I prepared a list of the available solutions:

Database Version Control

There are two types of projects I am involved in these days.
  1. Custom enterprise applications where we design the database and manage it.
  2. Web applications, that we host, where each customer has his own copy of the database.
In both cases there are many copies of the database installed.
  • Developer databases
  • Test database (both internal and customer)
  • Production database
It is obvious that these databases should be under some Version Control so that can be easily synchronized.
Usually when a database is upgraded, a new version of the application is issued. If only one application is using the database then application-database sync is easy.
When multiple applications are using the same database then this problem is quite difficult to solve.
This is my solution to database versioning problem: