home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / res / html / utils.js < prev   
Encoding:
Text File  |  2009-10-12  |  5.6 KB  |  170 lines

  1.  
  2. function AssertionError(message) { this.message = message; }
  3. AssertionError.prototype.toString = function() { return 'AssertionError(' + this.message + ')'; };
  4. function assert(x, message) { if (!x) throw new AssertionError(message || ''); }
  5.  
  6. function urlQuery(url, args, do_escape) {
  7.     do_escape = (do_escape === undefined ? true : do_escape);
  8.     var pairs = [];
  9.     for (var k in args)
  10.         pairs.push(k + '=' + (do_escape ? escape(args[k]) : args[k]));
  11.  
  12.     var questionMark = url.search('?') == -1 ? '?' : '';
  13.     return url + questionMark + pairs.join('&');
  14. }
  15.  
  16. /*
  17.  * JavaScript Pretty Date
  18.  * Copyright (c) 2008 John Resig (jquery.com)
  19.  * Licensed under the MIT license.
  20.  */
  21. // Takes an ISO time and returns a string representing how
  22. // long ago the date represents.
  23. function dateDiff(time1, time2) {
  24.  
  25.     if (typeof(time1) == typeof("")){
  26.       var date = new Date((time1 || "").replace(/-/g,"/").replace(/[TZ]/g," "))
  27.     } else {
  28.       var date = new Date(time1);
  29.     }
  30.  
  31.     time2 = time2 || new Date();
  32.  
  33.     return ((time2.getTime() - date.getTime()) / 1000);
  34. }
  35.  
  36. function dayOfDate(date) {
  37.  var day = new Date(date);
  38.  day.setMilliseconds(0);
  39.  day.setSeconds(0);
  40.  day.setMinutes(0);
  41.  day.setHours(0);
  42.  return day;
  43. }
  44.  
  45. function prettyDate(time){
  46.     var diff = dateDiff(time),
  47.         day_diff = Math.floor(diff / 86400);
  48.  
  49.     if ( isNaN(day_diff) || day_diff < 0 )
  50.         return;
  51.  
  52.     return day_diff == 0 && (
  53.             diff < 60 && "just now" ||
  54.             diff < 120 && "1 minute ago" ||
  55.             diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
  56.             diff < 7200 && "1 hour ago" ||
  57.             diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
  58.         day_diff == 1 && "Yesterday" ||
  59.         day_diff < 7 && day_diff + " days ago" ||
  60.         day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago" ||
  61.                 day_diff < 365 && Math.ceil( day_diff / 30) + " months ago" ||
  62.                 Math.ceil( day_diff / 365) + " years ago";
  63. }
  64.  
  65. function linkify(string, options) {
  66.     if (!options) options = {};
  67.     if (!options.limit) options.limit = 100;
  68.     if (!options.tagFill) options.tagFill = '';
  69.  
  70.     var regex = /((http\:\/\/|https\:\/\/|ftp\:\/\/)|(www\.))+(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi;
  71.  
  72.     string = string.replace(regex, function(value) {
  73.         value = value.toLowerCase();
  74.         var m = value.match(/^([a-z]+:\/\/)/), nice, url;
  75.         if (m) {
  76.             nice = value.replace(m[1],'');
  77.             url = value;
  78.         } else {
  79.             nice = value;
  80.             url = 'http://' + nice;
  81.         }
  82.  
  83.         return '<a href="' + url + '"' + (options.tagFill != '' ? (' ' + options.tagFill) : '')+ '>' + linkifyLabel(/*nice*/url, options.limit) + '</a>';
  84.     });
  85.  
  86.     return string;
  87. }
  88.  
  89.  
  90. function linkifyLabel(text, limit) {
  91.     if (!limit) return text;
  92.  
  93.     if (text.length > limit) {
  94.         return text.substr(0, limit - 3) + '...';
  95.     }
  96.  
  97.     return text;
  98. }
  99.  
  100. /*
  101. //"AJAX from Scratch: Implementing Mutual Exclusion in JavaScript"
  102. //By Bruce Wallace
  103. //http://www.developer.com/lang/jscript/article.php/3592016
  104. //8/3/2009
  105.  
  106. var MUTEX_TIMESLICE = 0; //started as 10ms
  107.  
  108. function Mutex(cmdObject, methodName) {
  109.     // define instance method
  110.     this.attempt =
  111.                    function(start) {
  112. //                       console.log('attempt: ' + this.number);
  113.                        for ( var j = start; j; j = Mutex.Wait.next(j.c.id)) {
  114.                            if (j.enter
  115.                                || (j.number && (j.number < this.number || (j.number == this.number && j.c.id < this.c.id))))
  116.                                return setTimeout("Mutex.SLICE(" + this.c.id
  117.                                                  + "," + j.c.id + ")", MUTEX_TIMESLICE);
  118.                        }
  119. //                       console.log('running: ' + this.number);
  120.                        this.c[this.methodID](); // run with exclusive access
  121.                        var num = this.number;
  122.                        this.number = 0; // release exclusive access
  123.                        Mutex.Wait.remove(this.c.id);
  124. //                       console.log('ran: ' + num);
  125.                    }
  126.     // constructor logic
  127.     this.c = cmdObject;
  128.     this.methodID = methodName;
  129.     Mutex.Wait.add(this.c.id, this); //enter and number are
  130.     //"false"
  131.     this.enter = true;
  132.     this.number = ++Mutex.NextID;
  133.     this.enter = false;
  134.     this.attempt(Mutex.Wait.first());
  135. }
  136. // define static variable and method
  137. Mutex.Wait = new Map();
  138. Mutex.NextID = 0;
  139. Mutex.SLICE = function(cmdID, startID) {
  140.     Mutex.Wait.get(cmdID).attempt(Mutex.Wait.get(startID));
  141. }
  142.  
  143. function Map() {
  144.    this.map  = new Object();
  145.    // Map API
  146.   this.add     = function(k,o){ this.map[k] = o; }
  147.   this.remove  = function( k ){ delete this.map[k]; }
  148.   this.get     = function( k ){ return k==null ? null : this.map[k]; }
  149.   this.first   = function(   ){ return this.get( this.nextKey( ) ); }
  150.   this.next    = function( k ){ return this.get( this.nextKey(k) ); }
  151.   this.nextKey = function( k ){ for (i in this.map) {
  152.                                   if (!k) return i;
  153.                                   if (k==i) k=null;    //tricky
  154.                                 }
  155.                                 return null;
  156.                               }
  157. }
  158.  
  159. var NEXT_CMD_ID = 0;
  160.  
  161. function Command() {
  162.     this.id = ++NEXT_CMD_ID;              //define instance variable
  163.     // unsynchronized API
  164.     this.go = function(){ alert("DOIT called"); //override me
  165.     }
  166.     // synchronized API
  167.     this.syncGo = function(){ new Mutex(this,"go"); }
  168.   }
  169.   */
  170.