[ Index ]
 

Code source de b2evolution 2.1.0-beta

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/blogs/rsc/js/ -> styleswitcher.js (source)

   1  /**

   2   * This file is public domain.

   3   *

   4   * Source: http://www.alistapart.com/articles/alternate/

   5   *

   6   * dh> TODO: could be optimized probably a bit

   7   *

   8   * Changes:

   9   *  2006-10-17, http://daniel.hahler.de/:

  10   *  - fix/optimization regarding disabling of chosen stylesheet(-set)

  11   *  - only use selected style from cookie, if available

  12   *  - Transformed into prototyped object (encapsulation)

  13   *  - setActiveStyleSheet(): do not disable all stylesheets in case there's

  14   *    an invalid one provided as param (e.g. from obsolete cookie);

  15   *    This would disable all styles..!

  16   *  - Use addEvent() and do not overwrite window.on(un)load

  17   */
  18  
  19  function StyleSwitcher()
  20  {
  21      /**

  22       * Cross browser event handling for IE5+, NS6+ an Mozilla/Gecko

  23       * NOTE: taken/copied here from /rsc/js/functions.js

  24       * @author Scott Andrew

  25       */
  26      var addEvent = function( elm, evType, fn, useCapture )
  27      {
  28          if( elm.addEventListener )
  29          { // Standard & Mozilla way:
  30              elm.addEventListener( evType, fn, useCapture );
  31              return true;
  32          }
  33          else if( elm.attachEvent )
  34          { // IE way:
  35              var r = elm.attachEvent( 'on'+evType, fn );
  36              return r;
  37          }
  38          else
  39          { // "dirty" way (IE Mac for example):
  40              // Will overwrite any previous handler! :((

  41              elm['on'+evType] = fn;
  42              return false;
  43          }
  44      };
  45  
  46      var oThis = this;
  47  
  48      this.onload = function(e)
  49      {
  50          var cookie = oThis.readCookie("evo_style");
  51          var title = cookie ? cookie : oThis.getPreferredStyleSheet();
  52          oThis.setActiveStyleSheet(title);
  53      };
  54  
  55      addEvent( window, 'load', this.onload, false );
  56  
  57      this.onload(); // do it now already, so there's no "skin changing" after load on "larger" pages

  58  };
  59  
  60  
  61  StyleSwitcher.prototype.setActiveStyleSheet = function (title)
  62  {
  63      var i, a;
  64  
  65      var valid = false;
  66  
  67      // test if it's a valid one:

  68      for(var i=0; (a = document.getElementsByTagName("link")[i]); i++)
  69      {
  70          if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title"))
  71          {
  72              if(a.getAttribute("title") == title)
  73              {
  74                  valid = true;
  75                  break;
  76              }
  77          }
  78      }
  79  
  80      if( ! valid )
  81          return false;
  82  
  83      // IE6 and IE7 need to disable all stylesheets first and then re-enable it..

  84      // ..at least when we call the onload event directly, too.

  85      // There is a bug with Konqueror (http://bugs.kde.org/135849) but it does not seem to apply here anymore?!

  86      for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
  87      {
  88          if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title"))
  89          {
  90              a.disabled = true;
  91              if(a.getAttribute("title") == title)
  92              {
  93                  a.disabled = false;
  94              }
  95          }
  96      }
  97  
  98      this.createCookie("evo_style", title, 365);
  99  
 100      return true;
 101  };
 102  
 103  StyleSwitcher.prototype.getActiveStyleSheet = function()
 104  {
 105      var i, a;
 106      for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
 107      {
 108          if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled)
 109          {
 110              return a.getAttribute("title");
 111          }
 112      }
 113      return null;
 114  };
 115  
 116  StyleSwitcher.prototype.getPreferredStyleSheet = function()
 117  {
 118      var i, a;
 119      for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
 120      {
 121          if(a.getAttribute("rel").indexOf("style") != -1
 122              && a.getAttribute("rel").indexOf("alt") == -1
 123              && a.getAttribute("title") )
 124          {
 125              return a.getAttribute("title");
 126          }
 127      }
 128      return null;
 129  };
 130  
 131  StyleSwitcher.prototype.createCookie = function(name,value,days)
 132  {
 133      if (days)
 134      {
 135          var date = new Date();
 136          date.setTime(date.getTime()+(days*24*60*60*1000));
 137          var expires = "; expires="+date.toGMTString();
 138      }
 139      else expires = "";
 140      document.cookie = name+"="+value+expires+"; path=/";
 141  };
 142  
 143  StyleSwitcher.prototype.readCookie = function(name)
 144  {
 145      var nameEQ = name + "=";
 146      var ca = document.cookie.split(';');
 147      for(var i=0;i < ca.length;i++)
 148      {
 149          var c = ca[i];
 150          while (c.charAt(0)==' ')
 151          {
 152              c = c.substring(1,c.length);
 153          }
 154          if (c.indexOf(nameEQ) == 0)
 155          {
 156              return c.substring(nameEQ.length,c.length);
 157          }
 158      }
 159  
 160      return null;
 161  };
 162  
 163  
 164  var StyleSwitcher = new StyleSwitcher();


Généré le : Thu Nov 29 23:58:50 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics