[ Index ]
 

Code source de Seagull 0.6.1

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/js/html_ajax/ -> util.js (source)

   1  /**
   2   * Utility methods
   3   *
   4   * @category    HTML
   5   * @package    Ajax
   6   * @license    http://www.opensource.org/licenses/lgpl-license.php  LGPL
   7   *
   8   * See Main.js for author/license details
   9   */
  10  // {{{ HTML_AJAX_Util
  11  /**
  12   * All the utilities we will be using thorough the classes
  13   */
  14  var HTML_AJAX_Util = {
  15      // Set the element event
  16      registerEvent: function(element, event, handler) 
  17      {
  18          element = this.getElement(element);
  19          if (typeof element.addEventListener != "undefined") {   //Dom2
  20              element.addEventListener(event, handler, false);
  21          } else if (typeof element.attachEvent != "undefined") { //IE 5+
  22              element.attachEvent("on" + event, handler);
  23          } else {
  24              if (element["on" + event] != null) {
  25                  var oldHandler = element["on" + event];
  26                  element["on" + event] = function(e) {
  27                      oldHander(e);
  28                      handler(e);
  29                  };
  30              } else {
  31                  element["on" + event] = handler;
  32              }
  33          }
  34      },
  35      // get the target of an event, automatically checks window.event for ie
  36      eventTarget: function(event) 
  37      {
  38          if (!event) var event = window.event;
  39          if (event.target) return event.target; // w3c
  40          if (event.srcElement) return event.srcElement; // ie 5
  41      },
  42      // gets the type of a variable or its primitive equivalent as a string
  43      getType: function(inp) 
  44      {
  45          var type = typeof inp, match;
  46          if(type == 'object' && !inp)
  47          {
  48              return 'null';
  49          }
  50          if (type == "object") {
  51              if(!inp.constructor)
  52              {
  53                  return 'object';
  54              }
  55              var cons = inp.constructor.toString();
  56              if (match = cons.match(/(\w+)\(/)) {
  57                  cons = match[1].toLowerCase();
  58              }
  59              var types = ["boolean", "number", "string", "array"];
  60              for (key in types) {
  61                  if (cons == types[key]) {
  62                      type = types[key];
  63                      break;
  64                  }
  65              }
  66          }
  67          return type;
  68      },
  69      // repeats the input string the number of times given by multiplier. exactly like PHP's str_repeat()
  70      strRepeat: function(inp, multiplier) {
  71          var ret = "";
  72          while (--multiplier > 0) ret += inp;
  73          return ret;
  74      },
  75      // encode a string allowing it to be used in a query string of a url
  76      encodeUrl: function(input) {
  77          return encodeURIComponent(input);
  78      },
  79      // decode a url encoded string
  80      decodeUrl: function(input) {
  81          return decodeURIComponent(input);
  82      },
  83      // recursive variable dumper similar in output to PHP's var_dump(), the differences being: this function displays JS types and type names; JS doesn't provide an object number like PHP does
  84      varDump: function(inp, printFuncs, _indent, _recursionLevel)
  85      {
  86          if (!_recursionLevel) _recursionLevel = 0;
  87          if (!_indent) _indent = 1;
  88          var tab = this.strRepeat("  ", ++_indent);    
  89          var type = this.getType(inp), out = type;
  90          var consrx = /(\w+)\(/;
  91          consrx.compile();
  92          if (++_recursionLevel > 6) {
  93              return tab + inp + "Loop Detected\n";
  94          }
  95          switch (type) {
  96              case "boolean":
  97              case "number":
  98                  out += "(" + inp.toString() + ")";
  99                  break;
 100              case "string":
 101                  out += "(" + inp.length + ") \"" + inp + "\"";
 102                  break;
 103              case "function":
 104                  if (printFuncs) {
 105                      out += inp.toString().replace(/\n/g, "\n" + tab);
 106                  }
 107                  break;
 108              case "array":
 109              case "object":
 110                  var atts = "", attc = 0;
 111                  try {
 112                      for (k in inp) {
 113                          atts += tab + "[" + (/\D/.test(k) ? "\"" + k + "\"" : k)
 114                              + "]=>\n" + tab + this.varDump(inp[k], printFuncs, _indent, _recursionLevel);
 115                          ++attc;
 116                      }
 117                  } catch (e) {}
 118                  if (type == "object") {
 119                      var objname, objstr = inp.toString();
 120                      if (objname = objstr.match(/^\[object (\w+)\]$/)) {
 121                          objname = objname[1];
 122                      } else {
 123                          try {
 124                              objname = inp.constructor.toString().match(consrx)[1];
 125                          } catch (e) {
 126                              objname = 'unknown';
 127                          }
 128                      }
 129                      out += "(" + objname + ") ";
 130                  }
 131                  out += "(" + attc + ") {\n" + atts + this.strRepeat("  ", _indent - 1) +"}";
 132                  break;
 133          }
 134          return out + "\n";
 135      },
 136      // non resursive simple debug printer
 137      quickPrint: function(input,sep) {
 138          if (!sep) {
 139              var sep = "\n";
 140          }
 141          var type = HTML_AJAX_Util.getType(input);
 142          switch (type) {
 143              case 'string':
 144                  return input;
 145              case 'array':
 146                  var ret = "";
 147                  for(var i = 0; i < input.length; i++) {
 148                      ret += i+':'+input[i]+sep;
 149                  }
 150                  return ret;
 151              default:
 152                  var ret = "";
 153                  for(var i in input) {
 154                      ret += i+':'+input[i]+sep;
 155                  }
 156                  return ret;
 157          }
 158      },
 159      //compat function for stupid browsers in which getElementsByTag with a * dunna work
 160      getAllElements: function(parentElement)
 161      {
 162          //check for idiot browsers
 163          if( document.all)
 164          {
 165              if(!parentElement) {
 166                  var allElements = document.all;
 167              }
 168              else
 169              {
 170                  var allElements = [], rightName = new RegExp( parentElement, 'i' ), i;
 171                  for( i=0; i<document.all.length; i++ ) {
 172                      if( rightName.test( document.all[i].parentElement ) )
 173                      allElements.push( document.all[i] );
 174                  }
 175              }
 176              return allElements;
 177          }
 178          //real browsers just do this
 179          else
 180          {
 181              if (!parentElement) { parentElement = document.body; }
 182              return parentElement.getElementsByTagName('*');
 183          }
 184      },
 185      getElementsByProperty: function(property, regex, parentElement) {
 186          var allElements = HTML_AJAX_Util.getAllElements(parentElement);
 187          var items = [];
 188          for(var i=0,j=allElements.length; i<j; i++)
 189          {
 190              if(regex.test(allElements[i][property]))
 191              {
 192                  items.push(allElements[i]);
 193              }
 194          }
 195          return items;
 196      },
 197      getElementsByClassName: function(className, parentElement) {
 198          return HTML_AJAX_Util.getElementsByProperty('className',new RegExp('(^| )' + className + '( |$)'),parentElement);
 199      },
 200      getElementsById: function(id, parentElement) {
 201          return HTML_AJAX_Util.getElementsByProperty('id',new RegExp(id),parentElement);
 202      },
 203      getElementsByCssSelector: function(selector,parentElement) {
 204          return cssQuery(selector,parentElement);
 205      },
 206      htmlEscape: function(inp) {
 207          var div = document.createElement('div');
 208          var text = document.createTextNode(inp);
 209          div.appendChild(text);
 210          return div.innerHTML;
 211      },
 212      // return the base of the given absolute url, or the filename if the second argument is true
 213      baseURL: function(absolute, filename) {
 214          var qPos = absolute.indexOf('?');
 215          if (qPos >= 0) {
 216              absolute = absolute.substr(0, qPos);
 217          }
 218          var slashPos = Math.max(absolute.lastIndexOf('/'), absolute.lastIndexOf('\\'));
 219          if (slashPos < 0) {
 220              return absolute;
 221          }
 222          return (filename ? absolute.substr(slashPos + 1) : absolute.substr(0, slashPos + 1));
 223      },
 224      // return the query string from a url
 225      queryString: function(url) {
 226          var qPos = url.indexOf('?');
 227          if (qPos >= 0) {
 228              return url.substr(qPos+1);
 229          }
 230      },
 231      // return the absolute path to the given relative url
 232      absoluteURL: function(rel, absolute) {
 233          if (/^https?:\/\//i.test(rel)) {
 234              return rel;
 235          }
 236          if (!absolute) {
 237              var bases = document.getElementsByTagName('base');
 238              for (i in bases) {
 239                  if (bases[i].href) {
 240                      absolute = bases[i].href;
 241                      break;
 242                  }
 243              }
 244              if (!absolute) {
 245                  absolute = window.location.href;
 246              }
 247          }
 248          if (rel == '') {
 249              return absolute;
 250          }
 251          if (rel.substr(0, 2) == '//') {
 252              // starts with '//', replace everything but the protocol
 253              var slashesPos = absolute.indexOf('//');
 254              if (slashesPos < 0) {
 255                  return 'http:' + rel;
 256              }
 257              return absolute.substr(0, slashesPos) + rel;
 258          }
 259          var base = this.baseURL(absolute);
 260          var absParts = base.substr(0, base.length - 1).split('/');
 261          var absHost = absParts.slice(0, 3).join('/') + '/';
 262          if (rel.substr(0, 1) == '/') {
 263              // starts with '/', append it to the host
 264              return absHost + rel;
 265          }
 266          if (rel.substr(0, 1) == '.' && rel.substr(1, 1) != '.') {
 267              // starts with '.', append it to the base
 268              return base + rel.substr(1);
 269          }
 270          // remove everything upto the path and beyond 
 271          absParts.splice(0, 3);
 272          var relParts = rel.split('/');
 273          var loopStart = relParts.length - 1;
 274          relParts = absParts.concat(relParts);
 275          for (i = loopStart; i < relParts.length;) {
 276              if (relParts[i] == '..') {
 277                  if (i == 0) {
 278                      return absolute;
 279                  }
 280                  relParts.splice(i - 1, 2);
 281                  --i;
 282                  continue;
 283              }
 284              i++;
 285          }
 286          return absHost + relParts.join('/');
 287      },
 288      // sets the innerHTML of an element. the third param decides how to write, it replaces by default, others are append|prepend
 289      setInnerHTML: function(node, innerHTML, type)
 290      {
 291          node = this.getElement(node);
 292  
 293          if (type != 'append') {
 294              if (type == 'prepend') {
 295                  var oldHtml = node.innerHTML;
 296              }
 297              node.innerHTML = '';
 298          }
 299          var good_browser = (window.opera || navigator.product == 'Gecko');
 300          var regex = /^([\s\S]*?)<script([\s\S]*?)>([\s\S]*?)<\/script>([\s\S]*)$/i;
 301          var regex_src = /src=["'](.*?)["']/i;
 302          var matches, id, script, output = '', subject = innerHTML;
 303          var scripts = [];
 304  
 305          while (true) {
 306              matches = regex.exec(subject);
 307              if (matches && matches[0]) {
 308                  subject = matches[4];
 309                  id = 'ih_' + Math.round(Math.random()*9999) + '_' + Math.round(Math.random()*9999);
 310  
 311                  var startLen = matches[3].length;
 312                  script = matches[3].replace(/document\.write\(([\s\S]*?)\)/ig, 
 313                      'document.getElementById("' + id + '").innerHTML+=$1');
 314  
 315                  output += matches[1];
 316                  if (startLen != script.length) {
 317                          output += '<span id="' + id + '"></span>';
 318                  }
 319                  
 320                  output += '<script' + matches[2] + '>' + script + '</script>';
 321                  if (good_browser) {
 322                      continue;
 323                  }
 324                  if (script) {
 325                      scripts.push(script);
 326                  }
 327                  if (regex_src.test(matches[2])) {
 328                      var script_el = document.createElement("SCRIPT");
 329                      var atts_regex = /(\w+)=["'](.*?)["']([\s\S]*)$/;
 330                      var atts = matches[2];
 331                      for (var i = 0; i < 5; i++) { 
 332                          var atts_matches = atts_regex.exec(atts);
 333                          if (atts_matches && atts_matches[0]) {
 334                              script_el.setAttribute(atts_matches[1], atts_matches[2]);
 335                              atts = atts_matches[3];
 336                          } else {
 337                              break;
 338                          }
 339                      }
 340                      scripts.push(script_el);
 341                  }
 342              } else {
 343                  output += subject;
 344                  break;
 345              }
 346          }
 347          innerHTML = output;
 348  
 349          if (good_browser) {
 350              var el = document.createElement('span');
 351              el.innerHTML = innerHTML;
 352  
 353              for(var i = 0; i < el.childNodes.length; i++) {
 354                  node.appendChild(el.childNodes[i].cloneNode(true));
 355              }
 356          }
 357          else {
 358              node.innerHTML += innerHTML;
 359          }
 360  
 361          if (oldHtml) {
 362              node.innerHTML += oldHtml;
 363          }
 364  
 365          if (!good_browser) {
 366              for(var i = 0; i < scripts.length; i++) {
 367                  if (HTML_AJAX_Util.getType(scripts[i]) == 'string') {
 368                      scripts[i] = scripts[i].replace(/^\s*<!(\[CDATA\[|--)|((\/\/)?--|\]\])>\s*$/g, '');
 369                      window.eval(scripts[i]);
 370                  }
 371                  else {
 372                      node.appendChild(scripts[i]);
 373                  }
 374              }
 375          }
 376          return;
 377      },
 378      classSep: '(^|$| )',
 379      hasClass: function(o, className) {
 380          var o = this.getElement(o);
 381          var regex = new RegExp(this.classSep + className + this.classSep);
 382          return regex.test(o.className);
 383      },
 384      addClass: function(o, className) {
 385          var o = this.getElement(o);
 386          if(!this.hasClass(o, className)) {
 387              o.className += " " + className;
 388          }
 389      },
 390      removeClass: function(o, className) {
 391          var o = this.getElement(o);
 392          var regex = new RegExp(this.classSep + className + this.classSep);
 393          o.className = o.className.replace(regex, " ");
 394      },
 395      replaceClass: function(o, oldClass, newClass) {
 396          var o = this.getElement(o);
 397          var regex = new RegExp(this.classSep + oldClass + this.classSep);
 398          o.className = o.className.replace(regex, newClass);
 399      },
 400      getElement: function(el) {
 401          if (typeof el == 'string') {
 402              return document.getElementById(el);
 403          }
 404          return el;
 405      }
 406  }
 407  // }}}


Généré le : Fri Mar 30 01:27:52 2007 par Balluche grâce à PHPXref 0.7