diff options
Diffstat (limited to 'js/globals.js')
-rw-r--r-- | js/globals.js | 55 |
1 files changed, 51 insertions, 4 deletions
diff --git a/js/globals.js b/js/globals.js index 1dad0dc..29f62c5 100644 --- a/js/globals.js +++ b/js/globals.js @@ -4,11 +4,58 @@ function _(str) { /* getText */ } if (typeof String.prototype.endsWith !== 'function') { - String.prototype.endsWith = function(suffix) { - return this.indexOf(suffix, this.length - suffix.length) !== -1; - }; + 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 = {}; +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); } + } +} + +window.onbeforeunload = function(e) { + return ""; /* some browsers will show this text, some won't. */ +} |