home *** CD-ROM | disk | FTP | other *** search
-
- function AssertionError(message) { this.message = message; }
- AssertionError.prototype.toString = function() { return 'AssertionError(' + this.message + ')'; };
- function assert(x, message) { if (!x) throw new AssertionError(message || ''); }
-
- function urlQuery(url, args, do_escape) {
- do_escape = (do_escape === undefined ? true : do_escape);
- var pairs = [];
- for (var k in args)
- pairs.push(k + '=' + (do_escape ? escape(args[k]) : args[k]));
-
- var questionMark = url.search('?') == -1 ? '?' : '';
- return url + questionMark + pairs.join('&');
- }
-
- /*
- * JavaScript Pretty Date
- * Copyright (c) 2008 John Resig (jquery.com)
- * Licensed under the MIT license.
- */
- // Takes an ISO time and returns a string representing how
- // long ago the date represents.
- function dateDiff(time1, time2) {
-
- if (typeof(time1) == typeof("")){
- var date = new Date((time1 || "").replace(/-/g,"/").replace(/[TZ]/g," "))
- } else {
- var date = new Date(time1);
- }
-
- time2 = time2 || new Date();
-
- return ((time2.getTime() - date.getTime()) / 1000);
- }
-
- function dayOfDate(date) {
- var day = new Date(date);
- day.setMilliseconds(0);
- day.setSeconds(0);
- day.setMinutes(0);
- day.setHours(0);
- return day;
- }
-
- function prettyDate(time){
- var diff = dateDiff(time),
- day_diff = Math.floor(diff / 86400);
-
- if ( isNaN(day_diff) || day_diff < 0 )
- return;
-
- return day_diff == 0 && (
- diff < 60 && "just now" ||
- diff < 120 && "1 minute ago" ||
- diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
- diff < 7200 && "1 hour ago" ||
- diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
- day_diff == 1 && "Yesterday" ||
- day_diff < 7 && day_diff + " days ago" ||
- day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago" ||
- day_diff < 365 && Math.ceil( day_diff / 30) + " months ago" ||
- Math.ceil( day_diff / 365) + " years ago";
- }
-
- function linkify(string, options) {
- if (!options) options = {};
- if (!options.limit) options.limit = 100;
- if (!options.tagFill) options.tagFill = '';
-
- var regex = /((http\:\/\/|https\:\/\/|ftp\:\/\/)|(www\.))+(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi;
-
- string = string.replace(regex, function(value) {
- value = value.toLowerCase();
- var m = value.match(/^([a-z]+:\/\/)/), nice, url;
- if (m) {
- nice = value.replace(m[1],'');
- url = value;
- } else {
- nice = value;
- url = 'http://' + nice;
- }
-
- return '<a href="' + url + '"' + (options.tagFill != '' ? (' ' + options.tagFill) : '')+ '>' + linkifyLabel(/*nice*/url, options.limit) + '</a>';
- });
-
- return string;
- }
-
-
- function linkifyLabel(text, limit) {
- if (!limit) return text;
-
- if (text.length > limit) {
- return text.substr(0, limit - 3) + '...';
- }
-
- return text;
- }
-
- /*
- //"AJAX from Scratch: Implementing Mutual Exclusion in JavaScript"
- //By Bruce Wallace
- //http://www.developer.com/lang/jscript/article.php/3592016
- //8/3/2009
-
- var MUTEX_TIMESLICE = 0; //started as 10ms
-
- function Mutex(cmdObject, methodName) {
- // define instance method
- this.attempt =
- function(start) {
- // console.log('attempt: ' + this.number);
- for ( var j = start; j; j = Mutex.Wait.next(j.c.id)) {
- if (j.enter
- || (j.number && (j.number < this.number || (j.number == this.number && j.c.id < this.c.id))))
- return setTimeout("Mutex.SLICE(" + this.c.id
- + "," + j.c.id + ")", MUTEX_TIMESLICE);
- }
- // console.log('running: ' + this.number);
- this.c[this.methodID](); // run with exclusive access
- var num = this.number;
- this.number = 0; // release exclusive access
- Mutex.Wait.remove(this.c.id);
- // console.log('ran: ' + num);
- }
- // constructor logic
- this.c = cmdObject;
- this.methodID = methodName;
- Mutex.Wait.add(this.c.id, this); //enter and number are
- //"false"
- this.enter = true;
- this.number = ++Mutex.NextID;
- this.enter = false;
- this.attempt(Mutex.Wait.first());
- }
- // define static variable and method
- Mutex.Wait = new Map();
- Mutex.NextID = 0;
- Mutex.SLICE = function(cmdID, startID) {
- Mutex.Wait.get(cmdID).attempt(Mutex.Wait.get(startID));
- }
-
- function Map() {
- this.map = new Object();
- // Map API
- this.add = function(k,o){ this.map[k] = o; }
- this.remove = function( k ){ delete this.map[k]; }
- this.get = function( k ){ return k==null ? null : this.map[k]; }
- this.first = function( ){ return this.get( this.nextKey( ) ); }
- this.next = function( k ){ return this.get( this.nextKey(k) ); }
- this.nextKey = function( k ){ for (i in this.map) {
- if (!k) return i;
- if (k==i) k=null; //tricky
- }
- return null;
- }
- }
-
- var NEXT_CMD_ID = 0;
-
- function Command() {
- this.id = ++NEXT_CMD_ID; //define instance variable
- // unsynchronized API
- this.go = function(){ alert("DOIT called"); //override me
- }
- // synchronized API
- this.syncGo = function(){ new Mutex(this,"go"); }
- }
- */
-