[ Index ]

PHP Cross Reference of phpwcms V1.5.0 _r431 (28.01.12)

title

Body

[close]

/include/inc_js/ -> util.js (source)

   1  /*===================================================================
   2   Author: Matt Kruse
   3   
   4   View documentation, examples, and source code at:
   5       http://www.JavascriptToolbox.com/
   6  
   7   NOTICE: You may use this code for any purpose, commercial or
   8   private, without any further permission from the author. You may
   9   remove this notice from your final code if you wish, however it is
  10   appreciated by the author if at least the web site address is kept.
  11  
  12   This code may NOT be distributed for download from script sites, 
  13   open source CDs or sites, or any other distribution method. If you
  14   wish you share this code with others, please direct them to the 
  15   web site above.
  16   
  17   Pleae do not link directly to the .js files on the server above. Copy
  18   the files to your own server for use with your site or webapp.
  19   ===================================================================*/
  20  /* ******************************************************************* */
  21  /*   UTIL FUNCTIONS                                                    */
  22  /* ******************************************************************* */
  23  var Util = {'$VERSION':1.06};
  24  
  25  // Util functions - these are GLOBAL so they
  26  // look like built-in functions.
  27  
  28  // Determine if an object is an array
  29  function isArray(o) {
  30      return (o!=null && typeof(o)=="object" && typeof(o.length)=="number" && (o.length==0 || defined(o[0])));
  31  };
  32  
  33  // Determine if an object is an Object
  34  function isObject(o) {
  35      return (o!=null && typeof(o)=="object" && defined(o.constructor) && o.constructor==Object && !defined(o.nodeName));
  36  };
  37  
  38  // Determine if a reference is defined
  39  function defined(o) {
  40      return (typeof(o)!="undefined");
  41  };
  42  
  43  // Iterate over an array, object, or list of items and run code against each item
  44  // Similar functionality to Perl's map() function
  45  function map(func) {
  46      var i,j,o;
  47      var results = [];
  48      if (typeof(func)=="string") {
  49          func = new Function('$_',func);
  50      }
  51      for (i=1; i<arguments.length; i++) {
  52          o = arguments[i];
  53          if (isArray(o)) {
  54              for (j=0; j<o.length; j++) {
  55                  results[results.length] = func(o[j]);
  56              }
  57          }
  58          else if (isObject(o)) {
  59              for (j in o) {
  60                  results[results.length] = func(o[j]);
  61              }
  62          }
  63          else {
  64              results[results.length] = func(o);
  65          }
  66      }
  67      return results;
  68  };
  69  
  70  // Set default values in an object if they are undefined
  71  function setDefaultValues(o,values) {
  72      if (!defined(o) || o==null) {
  73          o = {};
  74      }
  75      if (!defined(values) || values==null) {
  76          return o;
  77      }
  78      for (var val in values) {
  79          if (!defined(o[val])) {
  80              o[val] = values[val];
  81          }
  82      }
  83      return o;
  84  };
  85  
  86  /* ******************************************************************* */
  87  /*   DEFAULT OBJECT PROTOTYPE ENHANCEMENTS                             */
  88  /* ******************************************************************* */
  89  // These functions add useful functionality to built-in objects
  90  Array.prototype.contains = function(o) {
  91      var i,l;
  92      if (!(l = this.length)) { return false; }
  93      for (i=0; i<l; i++) {
  94          if (o==this[i]) {
  95              return true;
  96          }
  97      }
  98  }
  99  
 100  /* ******************************************************************* */
 101  /*   DOM FUNCTIONS                                                     */
 102  /* ******************************************************************* */
 103  var DOM = (function() { 
 104      var dom = {};
 105      
 106      // Get a parent tag with a given nodename
 107      dom.getParentByTagName = function(o,tagNames) {
 108          if(o==null) { return null; }
 109          if (isArray(tagNames)) {
 110              tagNames = map("return $_.toUpperCase()",tagNames);
 111              while (o=o.parentNode) {
 112                  if (o.nodeName && tagNames.contains(o.nodeName)) {
 113                      return o;
 114                  }
 115              }
 116          }
 117          else {
 118              tagNames = tagNames.toUpperCase();
 119              while (o=o.parentNode) {
 120                  if (o.nodeName && tagNames==o.nodeName) {
 121                      return o;
 122                  }
 123              }
 124          }
 125          return null;
 126      };
 127      
 128      // Remove a node from its parent
 129      dom.removeNode = function(o) {
 130          if (o!=null && o.parentNode && o.parentNode.removeChild) {
 131              // First remove all attributes which are func references, to avoid memory leaks
 132              for (var i in o) {
 133                  if (typeof(o[i])=="function") {
 134                      o[i] = null;
 135                  }
 136              }
 137              o.parentNode.removeChild(o);
 138              return true;
 139          }
 140          return false;
 141      };
 142  
 143      // Get the outer width in pixels of an object, including borders, padding, and margin
 144      dom.getOuterWidth = function(o) {
 145          if (defined(o.offsetWidth)) {
 146              return o.offsetWidth;
 147          }
 148          return null;
 149      };
 150  
 151      // Get the outer height in pixels of an object, including borders, padding, and margin
 152      dom.getOuterHeight = function(o) {
 153          if (defined(o.offsetHeight)) {
 154              return o.offsetHeight;
 155          }
 156          return null;
 157      };
 158  
 159      // Resolve an item, an array of items, or an object of items
 160      dom.resolve = function() {
 161          var results = new Array();
 162          var i,j,o;
 163          for (var i=0; i<arguments.length; i++) {
 164              var o = arguments[i];
 165              if (o==null) {
 166                  if (arguments.length==1) {
 167                      return null;
 168                  }
 169                  results[results.length] = null;
 170              }
 171              else if (typeof(o)=='string') {
 172                  if (document.getElementById) {
 173                      o = document.getElementById(o);
 174                  }
 175                  else if (document.all) {
 176                      o = document.all[o];
 177                  }
 178                  if (arguments.length==1) {
 179                      return o;
 180                  }
 181                  results[results.length] = o;
 182              }
 183              else if (isArray(o)) {
 184                  for (j=0; j<o.length; j++) {
 185                      results[results.length] = o[j];
 186                  }
 187              }
 188              else if (isObject(o)) {
 189                  for (j in o) {
 190                      results[results.length] = o[j];
 191                  }
 192              }
 193              else if (arguments.length==1) {
 194                  return o;
 195              }
 196              else {
 197                  results[results.length] = o;
 198              }
 199        }
 200        return results;
 201      };
 202      dom.$ = dom.resolve;
 203      
 204      return dom;
 205  })();
 206  
 207  /* ******************************************************************* */
 208  /*   CSS FUNCTIONS                                                     */
 209  /* ******************************************************************* */
 210  var CSS = (function(){
 211      var css = {};
 212  
 213      // Convert an RGB string in the form "rgb (255, 255, 255)" to "#ffffff"
 214      css.rgb2hex = function(rgbString) {
 215          if (typeof(rgbString)!="string" || !defined(rgbString.match)) { return null; }
 216          var result = rgbString.match(/^\s*rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*/);
 217          if (result==null) { return rgbString; }
 218          var rgb = +result[1] << 16 | +result[2] << 8 | +result[3]
 219          var hex = "";
 220          var digits = "0123456789abcdef";
 221          while(rgb!=0) { 
 222              hex = digits.charAt(rgb&0xf)+hex; 
 223              rgb>>>=4; 
 224          } 
 225          while(hex.length<6) { hex='0'+hex; }
 226          return "#" + hex;
 227      };
 228  
 229      // Convert hyphen style names like border-width to camel case like borderWidth
 230      css.hyphen2camel = function(property) {
 231          if (!defined(property) || property==null) { return null; }
 232          if (property.indexOf("-")<0) { return property; }
 233          var str = "";
 234          var c = null;
 235          var l = property.length;
 236          for (var i=0; i<l; i++) {
 237              c = property.charAt(i);
 238              str += (c!="-")?c:property.charAt(++i).toUpperCase();
 239          }
 240          return str;
 241      };
 242      
 243      // Determine if an object or class string contains a given class.
 244      css.hasClass = function(obj,className) {
 245          if (!defined(obj) || obj==null || !RegExp) { return false; }
 246          var re = new RegExp("(^|\\s)" + className + "(\\s|$)");
 247          if (typeof(obj)=="string") {
 248              return re.test(obj);
 249          }
 250          else if (typeof(obj)=="object" && obj.className) {
 251              return re.test(obj.className);
 252          }
 253          return false;
 254      };
 255      
 256      // Add a class to an object
 257      css.addClass = function(obj,className) {
 258          if (typeof(obj)!="object" || obj==null || !defined(obj.className)) { return false; }
 259          if (obj.className==null || obj.className=='') { 
 260              obj.className = className; 
 261              return true; 
 262          }
 263          if (css.hasClass(obj,className)) { return true; }
 264          obj.className = obj.className + " " + className;
 265          return true;
 266      };
 267      
 268      // Remove a class from an object
 269      css.removeClass = function(obj,className) {
 270          if (typeof(obj)!="object" || obj==null || !defined(obj.className) || obj.className==null) { return false; }
 271          if (!css.hasClass(obj,className)) { return false; }
 272          var re = new RegExp("(^|\\s+)" + className + "(\\s+|$)");
 273          obj.className = obj.className.replace(re,' ');
 274          return true;
 275      };
 276      
 277      // Fully replace a class with a new one
 278      css.replaceClass = function(obj,className,newClassName) {
 279          if (typeof(obj)!="object" || obj==null || !defined(obj.className) || obj.className==null) { return false; }
 280          css.removeClass(obj,className);
 281          css.addClass(obj,newClassName);
 282          return true;
 283      };
 284      
 285      // Get the currently-applied style of an object
 286      css.getStyle = function(o, property) {
 287          if (o==null) { return null; }
 288          var val = null;
 289          var camelProperty = css.hyphen2camel(property);
 290          // Handle "float" property as a special case
 291          if (property=="float") {
 292              val = css.getStyle(o,"cssFloat");
 293              if (val==null) { 
 294                  val = css.getStyle(o,"styleFloat"); 
 295              }
 296          }
 297          else if (o.currentStyle && defined(o.currentStyle[camelProperty])) {
 298              val = o.currentStyle[camelProperty];
 299          }
 300          else if (window.getComputedStyle) {
 301              val = window.getComputedStyle(o,null).getPropertyValue(property);
 302          }
 303          else if (o.style && defined(o.style[camelProperty])) {
 304              val = o.style[camelProperty];
 305          }
 306          // For color values, make the value consistent across browsers
 307          // Convert rgb() colors back to hex for consistency
 308          if (/^\s*rgb\s*\(/.test(val)) {
 309              val = css.rgb2hex(val);
 310          }
 311          // Lowercase all #hex values
 312          if (/^#/.test(val)) {
 313              val = val.toLowerCase();
 314          }
 315          return val;
 316      };
 317      css.get = css.getStyle;
 318  
 319      // Set a style on an object
 320      css.setStyle = function(o, property, value) {
 321          if (o==null || !defined(o.style) || !defined(property) || property==null || !defined(value)) { return false; }
 322          if (property=="float") {
 323              o.style["cssFloat"] = value;
 324              o.style["styleFloat"] = value;
 325          }
 326          else if (property=="opacity") {
 327              o.style['-moz-opacity'] = value;
 328              o.style['-khtml-opacity'] = value;
 329              o.style.opacity = value;
 330              if (defined(o.style.filter)) {
 331                  o.style.filter = "alpha(opacity=" + value*100 + ")";
 332              }
 333          }
 334          else {
 335              o.style[css.hyphen2camel(property)] = value;
 336          }
 337          return true;
 338      };
 339      css.set = css.setStyle;
 340      
 341      // Get a unique ID which doesn't already exist on the page
 342      css.uniqueIdNumber=1000;
 343      css.createId = function(o) {
 344          if (defined(o) && o!=null && defined(o.id) && o.id!=null && o.id!="") { 
 345              return o.id;
 346          }
 347          var id = null;
 348          while (id==null || document.getElementById(id)!=null) {
 349              id = "ID_"+(css.uniqueIdNumber++);
 350          }
 351          if (defined(o) && o!=null && (!defined(o.id)||o.id=="")) {
 352              o.id = id;
 353          }
 354          return id;
 355      };
 356      
 357      return css;
 358  })();
 359  
 360  /* ******************************************************************* */
 361  /*   EVENT FUNCTIONS                                                   */
 362  /* ******************************************************************* */
 363  
 364  var Event = (function(){
 365      var ev = {};
 366      
 367      // Resolve an event using IE's window.event if necessary
 368      // --------------------------------------------------------------------
 369      ev.resolve = function(e) {
 370          if (!defined(e) && defined(window.event)) {
 371              e = window.event;
 372          }
 373          return e;
 374      };
 375      
 376      // Add an event handler to a function
 377      // Note: Don't use 'this' within functions added using this method, since
 378      // the attachEvent and addEventListener models differ.
 379      // --------------------------------------------------------------------
 380      ev.add = function( obj, type, fn, capture ) {
 381          if (obj.addEventListener) {
 382              obj.addEventListener( type, fn, capture );
 383              return true;
 384          }
 385          else if (obj.attachEvent) {
 386              obj.attachEvent( "on"+type, fn );
 387              return true;
 388          }
 389          return false;
 390      };
 391  
 392      // Get the mouse position of an event
 393      // --------------------------------------------------------------------
 394      // PageX/Y, where they exist, are more reliable than ClientX/Y because 
 395      // of some browser bugs in Opera/Safari
 396      ev.getMouseX = function(e) {
 397          e = ev.resolve(e);
 398          if (defined(e.pageX)) {
 399              return e.pageX;
 400          }
 401          if (defined(e.clientX)) {
 402              return e.clientX+Screen.getScrollLeft();
 403          }
 404          return null;
 405      };
 406      ev.getMouseY = function(e) {
 407          e = ev.resolve(e);
 408          if (defined(e.pageY)) {
 409              return e.pageY;
 410          }
 411          if (defined(e.clientY)) {
 412              return e.clientY+Screen.getScrollTop();
 413          }
 414          return null;
 415      };
 416  
 417      // Stop the event from bubbling up to parent elements.
 418      // Two method names map to the same function
 419      // --------------------------------------------------------------------
 420      ev.cancelBubble = function(e) {
 421          e = ev.resolve(e);
 422          if (typeof(e.stopPropagation)=="function") { e.stopPropagation(); } 
 423          if (defined(e.cancelBubble)) { e.cancelBubble = true; }
 424      };
 425      ev.stopPropagation = ev.cancelBubble;
 426  
 427      // Prevent the default handling of the event to occur
 428      // --------------------------------------------------------------------
 429      ev.preventDefault = function(e) {
 430          e = ev.resolve(e);
 431          if (typeof(e.preventDefault)=="function") { e.preventDefault(); } 
 432          if (defined(e.returnValue)) { e.returnValue = false; }
 433      };
 434      
 435      return ev;
 436  })();
 437  
 438  /* ******************************************************************* */
 439  /*   SCREEN FUNCTIONS                                                  */
 440  /* ******************************************************************* */
 441  var Screen = (function() {
 442      var screen = {};
 443  
 444      // Get a reference to the body
 445      // --------------------------------------------------------------------
 446      screen.getBody = function() {
 447          if (document.body) {
 448              return document.body;
 449          }
 450          if (document.getElementsByTagName) {
 451              var bodies = document.getElementsByTagName("BODY");
 452              if (bodies!=null && bodies.length>0) {
 453                  return bodies[0];
 454              }
 455          }
 456          return null;
 457      };
 458  
 459      // Get the amount that the main document has scrolled from top
 460      // --------------------------------------------------------------------
 461      screen.getScrollTop = function() {
 462          if (document.documentElement && defined(document.documentElement.scrollTop) && document.documentElement.scrollTop>0) {
 463              return document.documentElement.scrollTop;
 464          }
 465          if (document.body && defined(document.body.scrollTop)) {
 466              return document.body.scrollTop;
 467          }
 468          return null;
 469      };
 470      
 471      // Get the amount that the main document has scrolled from left
 472      // --------------------------------------------------------------------
 473      screen.getScrollLeft = function() {
 474          if (document.documentElement && defined(document.documentElement.scrollLeft) && document.documentElement.scrollLeft>0) {
 475              return document.documentElement.scrollLeft;
 476          }
 477          if (document.body && defined(document.body.scrollLeft)) {
 478              return document.body.scrollLeft;
 479          }
 480          return null;
 481      };
 482      
 483      // Util function to default a bad number to 0
 484      // --------------------------------------------------------------------
 485      screen.zero = function(n) {
 486          return (!defined(n) || isNaN(n))?0:n;
 487      };
 488  
 489      // Get the width of the entire document
 490      // --------------------------------------------------------------------
 491      screen.getDocumentWidth = function() {
 492          var width = 0;
 493          var body = screen.getBody();
 494          if (document.documentElement && (!document.compatMode || document.compatMode=="CSS1Compat")) {
 495              var rightMargin = parseInt(CSS.get(body,'marginRight'),10) || 0;
 496              var leftMargin = parseInt(CSS.get(body,'marginLeft'), 10) || 0;
 497              width = Math.max(body.offsetWidth + leftMargin + rightMargin, document.documentElement.clientWidth);
 498          }
 499          else {
 500              width =  Math.max(body.clientWidth, body.scrollWidth);
 501          }
 502          if (isNaN(width) || width==0) {
 503              width = screen.zero(self.innerWidth);
 504          }
 505          return width;
 506      };
 507      
 508      // Get the height of the entire document
 509      // --------------------------------------------------------------------
 510      screen.getDocumentHeight = function() {
 511          var body = screen.getBody();
 512          var innerHeight = (defined(self.innerHeight)&&!isNaN(self.innerHeight))?self.innerHeight:0;
 513          if (document.documentElement && (!document.compatMode || document.compatMode=="CSS1Compat")) {
 514              var topMargin = parseInt(CSS.get(body,'marginTop'),10) || 0;
 515              var bottomMargin = parseInt(CSS.get(body,'marginBottom'), 10) || 0;
 516              return Math.max(body.offsetHeight + topMargin + bottomMargin, document.documentElement.clientHeight, document.documentElement.scrollHeight, screen.zero(self.innerHeight));
 517          }
 518          return Math.max(body.scrollHeight, body.clientHeight, screen.zero(self.innerHeight));
 519      };
 520      
 521      // Get the width of the viewport (viewable area) in the browser window
 522      // --------------------------------------------------------------------
 523      screen.getViewportWidth = function() {
 524          if (document.documentElement && (!document.compatMode || document.compatMode=="CSS1Compat")) {
 525              return document.documentElement.clientWidth;
 526          }
 527          else if (document.compatMode && document.body) {
 528              return document.body.clientWidth;
 529          }
 530          return screen.zero(self.innerWidth);
 531      };
 532      
 533      // Get the height of the viewport (viewable area) in the browser window
 534      // --------------------------------------------------------------------
 535      screen.getViewportHeight = function() {
 536          if (!window.opera && document.documentElement && (!document.compatMode || document.compatMode=="CSS1Compat")) {
 537              return document.documentElement.clientHeight;
 538          }
 539          else if (document.compatMode && !window.opera && document.body) {
 540              return document.body.clientHeight;
 541          }
 542          return screen.zero(self.innerHeight);
 543      };
 544  
 545      return screen;
 546  })();var Sort = (function(){
 547      var sort = {};
 548      sort.AlphaNumeric = function(a,b) {
 549          if (a==b) { return 0; }
 550          if (a<b) { return -1; }
 551          return 1;
 552      };
 553  
 554      sort.Default = sort.AlphaNumeric;
 555      
 556      sort.NumericConversion = function(val) {
 557          if (typeof(val)!="number") {
 558              if (typeof(val)=="string") {
 559                  val = parseFloat(val.replace(/,/g,''));
 560                  if (isNaN(val) || val==null) { val=0; }
 561              }
 562              else {
 563                  val = 0;
 564              }
 565          }
 566          return val;
 567      };
 568      
 569      sort.Numeric = function(a,b) {
 570          return sort.NumericConversion(a)-sort.NumericConversion(b);
 571      };
 572  
 573      sort.IgnoreCaseConversion = function(val) {
 574          if (val==null) { val=""; }
 575          return (""+val).toLowerCase();
 576      };
 577  
 578      sort.IgnoreCase = function(a,b) {
 579          return sort.AlphaNumeric(sort.IgnoreCaseConversion(a),sort.IgnoreCaseConversion(b));
 580      };
 581  
 582      sort.CurrencyConversion = function(val) {
 583          if (typeof(val)=="string") {
 584              val = val.replace(/^[^\d\.]/,'');
 585          }
 586          return sort.NumericConversion(val);
 587      };
 588      
 589      sort.Currency = function(a,b) {
 590          return sort.Numeric(sort.CurrencyConversion(a),sort.CurrencyConversion(b));
 591      };
 592      
 593      sort.DateConversion = function(val) {
 594          // inner util function to parse date formats
 595  		function getdate(str) {
 596              // inner util function to convert 2-digit years to 4
 597  			function fixYear(yr) {
 598                  yr = +yr;
 599                  if (yr<50) { yr += 2000; }
 600                  else if (yr<100) { yr += 1900; }
 601                  return yr;
 602              };
 603              var ret;
 604              // YYYY-MM-DD
 605              if (ret=str.match(/(\d{2,4})-(\d{1,2})-(\d{1,2})/)) {
 606                  return (fixYear(ret[1])*10000) + (ret[2]*100) + (+ret[3]);
 607              }
 608              // MM/DD/YY[YY] or MM-DD-YY[YY]
 609              if (ret=str.match(/(\d{1,2})[\/-](\d{1,2})[\/-](\d{2,4})/)) {
 610                  return (fixYear(ret[3])*10000) + (ret[1]*100) + (+ret[2]);
 611              }
 612              return 99999999; // So non-parsed dates will be last, not first
 613          };
 614          return getdate(val);
 615      };
 616  
 617      sort.Date = function(a,b) {
 618          return sort.Numeric(sort.DateConversion(a),sort.DateConversion(b));
 619      };
 620  
 621      return sort;
 622  })();
 623  


Generated: Sun Jan 29 16:31:14 2012 Cross-referenced by PHPXref 0.7.1