summaryrefslogtreecommitdiffstats
path: root/js/globals.js
blob: 46c769921ae437b632c05ea9997850389d7a2437 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function _(str) { /* getText */
	if (!(str in window.LOCALE)) { return str; }
	return window.LOCALE[str];
}

if (typeof String.prototype.endsWith !== 'function') {
	String.prototype.endsWith = function(suffix) {
		return this.indexOf(suffix, this.length - suffix.length) !== -1;
	};
}

if (!String.prototype.trim) {
	String.prototype.trim = function() {
		return this.match(/^\s*([\s\S]*?)\s*$/)[1];
	}
}

if (!String.trim) {
	String.trim = function(obj) { return String.prototype.trim.call(obj);}
}

if (!Object.create) {
	Object.create = function (o) {
		if (arguments.length > 1) { throw new Error("Object.create polyfill only accepts the first parameter"); }
		var tmp = function() {};
		tmp.prototype = o;
		return new tmp();
	};
}

var DATATYPES = false;
var LOCALE = {};
var SQL = {
	_subscribers: {},
	
	publish: function(message, publisher, data) {
		var subscribers = this._subscribers[message] || [];
		var obj = {
			target: publisher,
			data: data
		}
		subscribers.forEach(function(subscriber) { subscriber(obj); });
	},
	
	subscribe: function(message, subscriber) {
		if (!(message in this._subscribers)) {
			this._subscribers[message] = [];
		}
		var index = this._subscribers[message].indexOf(subscriber);
		if (index == -1) { this._subscribers[message].push(subscriber); }
	},
	
	unsubscribe: function(message, subscriber) {
		var index = this._subscribers[message].indexOf(subscriber);
		if (index > -1) { this._subscribers[message].splice(index, 1); }
	},

	escape: function(str) {
		return str.replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;");
	}
}

window.onbeforeunload = function(e) {
	return ""; /* some browsers will show this text, some won't. */
}