[ 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/ -> functions.js (source)

   1  /**

   2   * This file implements general Javascript functions.

   3   *

   4   * This file is part of the evoCore framework - {@link http://evocore.net/}

   5   * See also {@link http://sourceforge.net/projects/evocms/}.

   6   *

   7   * @copyright (c)2003-2006 by Francois PLANQUE - {@link http://fplanque.net/}

   8   * Parts of this file are copyright (c)2004-2006 by Daniel HAHLER - {@link http://thequod.de/contact}.

   9   *

  10   * {@internal License choice

  11   * - If you have received this file as part of a package, please find the license.txt file in

  12   *   the same folder or the closest folder above for complete license terms.

  13   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)

  14   *   then you must choose one of the following licenses before using the file:

  15   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php

  16   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php

  17   * }}

  18   *

  19   * {@internal Open Source relicensing agreement:

  20   * Daniel HAHLER grants Francois PLANQUE the right to license

  21   * Daniel HAHLER's contributions to this file and the b2evolution project

  22   * under any OSI approved OSS license (http://www.opensource.org/licenses/).

  23   * }}

  24   *

  25   * @package main

  26   *

  27   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}

  28   * @author blueyed: Daniel HAHLER

  29   * @author fplanque: Francois PLANQUE

  30   *

  31   * @version $Id: functions.js,v 1.30 2007/09/16 22:06:37 fplanque Exp $

  32   */
  33  
  34  
  35  /**

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

  37   * @author Scott Andrew

  38   */
  39  function addEvent( elm, evType, fn, useCapture )
  40  {
  41      if( elm.addEventListener )
  42      { // Standard & Mozilla way:
  43          elm.addEventListener( evType, fn, useCapture );
  44          return true;
  45      }
  46      else if( elm.attachEvent )
  47      { // IE way:
  48          var r = elm.attachEvent( 'on'+evType, fn );
  49          return r;
  50      }
  51      else
  52      { // "dirty" way (IE Mac for example):
  53          // Will overwrite any previous handler! :((

  54          elm['on'+evType] = fn;
  55          return false;
  56      }
  57  }
  58  
  59  
  60  /**

  61   * Browser status changed. 

  62   * Warning: This is disabled in modern browsers.

  63   */
  64  function setstatus( message )
  65  {
  66      window.status = message;
  67      return true;
  68  }
  69  function resetstatus()
  70  {
  71      window.status = 'Done';
  72  }
  73  
  74  /**

  75   * Opens a window, centers it and makes sure it gets focus.

  76   */
  77  function pop_up_window( href, target, width, height, params )
  78  {
  79       if( typeof(width) == 'undefined' )
  80       {
  81          width = 750;
  82      }
  83  
  84       if( typeof(height) == 'undefined' )
  85       {
  86          height = 550;
  87      }
  88  
  89      var left = (screen.width - width) / 2;
  90      var top = (screen.height - height) / 2;
  91  
  92       if( typeof(params) == 'undefined' )
  93      {
  94          params = 'scrollbars=yes, status=yes, resizable=yes, menubar=yes';
  95      }
  96  
  97      params = 'width=' + width + ', height=' + height + ', ' + 'left=' + left + ', top=' + top + ', ' + params;
  98  
  99      // Open window:

 100      opened = window.open( href, target, params );
 101  
 102      // Bring to front!

 103      opened.focus();
 104  
 105      if( typeof(openedWindows) == 'undefined' )
 106      {
 107          openedWindows = new Array(opened);
 108      }
 109      else
 110      {
 111          openedWindows.push(opened);
 112      }
 113  
 114      // Tell the caller there is no need to process href="" :

 115      return false;
 116  }
 117  
 118  
 119  /**

 120   * Shows/Hides target_id, and updates text_id object with either

 121   * text_when_displayed or text_when_hidden.

 122   *

 123   * It simply uses the value of the elements display attribute and toggles it.

 124   *

 125   * @return false

 126   */
 127  function toggle_display_by_id( text_id, target_id, text_when_displayed, text_when_hidden )
 128  {
 129      if( document.getElementById(target_id).style.display=="" )
 130      {
 131          document.getElementById( text_id ).innerHTML = text_when_hidden;
 132          document.getElementById( target_id ).style.display="none";
 133      }
 134      else
 135      {
 136          document.getElementById( text_id ).innerHTML = text_when_displayed;
 137          document.getElementById( target_id ).style.display="";
 138      }
 139      return false;
 140  }
 141  
 142  
 143  /**

 144   * Open or close a clickopen area (by use of CSS style).

 145   *

 146   * You have to define a div with id clickdiv_<ID> and a img with clickimg_<ID>,

 147   * where <ID> is the first param to the function.

 148   *

 149   * @param string html id of the element to toggle

 150   * @param string CSS display property to use when visible ('inline', 'block')

 151   * @return false

 152   */
 153  function toggle_clickopen( id, hide, displayVisible )
 154  {
 155      if( !( clickdiv = document.getElementById( 'clickdiv_'+id ) )
 156              || !( clickimg = document.getElementById( 'clickimg_'+id ) ) )
 157      {
 158          alert( 'ID '+id+' not found!' );
 159          return false;
 160      }
 161  
 162      if( typeof(hide) == 'undefined' )
 163      {
 164          hide = document.getElementById( 'clickdiv_'+id ).style.display != 'none';
 165      }
 166  
 167      if( typeof(displayVisible) == 'undefined' )
 168      {
 169          displayVisible = ''; // setting it to "empty" is the default for an element's display CSS attribute

 170      }
 171  
 172      if( hide )
 173      {
 174          clickdiv.style.display = 'none';
 175          clickimg.src = imgpath_expand;
 176  
 177          return false;
 178      }
 179      else
 180      {
 181          clickdiv.style.display = displayVisible;
 182          clickimg.src = imgpath_collapse;
 183  
 184          return false;
 185      }
 186  }
 187  
 188  
 189  // deprecated but left for old plugins:

 190  function textarea_replace_selection( myField, snippet, target_document )
 191  {
 192      textarea_wrap_selection( myField, snippet, '', 1, target_document );
 193  }
 194  
 195  /**

 196   * Textarea insertion code.

 197   *

 198   * @var element

 199   * @var text

 200   * @var text

 201   * @var boolean

 202   * @var document (needs only be passed from a popup window as window.opener.document)

 203   */
 204  function textarea_wrap_selection( myField, before, after, replace, target_document )
 205  {
 206      target_document = target_document || document;
 207  
 208      var hook_params = {
 209          'element': myField,
 210          'before': before,
 211          'after': after,
 212          'replace': replace,
 213          'target_document': target_document
 214      };
 215  
 216      // First try, if a JavaScript callback is registered to handle this.

 217      // E.g. the tinymce_plugin uses registers "wrap_selection_for_itemform_post_content"

 218      //      to replace the (non-)selection

 219      if( b2evo_Callbacks.trigger_callback( "wrap_selection_for_"+myField.id, hook_params ) )
 220      {
 221          return;
 222      }
 223      if( window.opener
 224          && window.opener.b2evo_Callbacks
 225          && ( typeof window.opener.b2evo_Callbacks != "undefined" ) )
 226      { // callback in parent document (e.g. "Files" popup)
 227          if( window.opener.b2evo_Callbacks.trigger_callback( "wrap_selection_for_"+myField.id, hook_params ) )
 228          {
 229              return;
 230          }
 231      }
 232  
 233      // Basic handling:

 234      if(target_document.selection)
 235      { // IE support:
 236          myField.focus();
 237          sel = target_document.selection.createRange();
 238          if( replace )
 239          {
 240              sel.text = before + after;
 241          }
 242          else
 243          {
 244              sel.text = before + sel.text + after;
 245          }
 246          myField.focus();
 247      }
 248      else if (myField.selectionStart || myField.selectionStart == '0')
 249      { // MOZILLA/NETSCAPE support:
 250          var startPos = myField.selectionStart;
 251          var endPos = myField.selectionEnd;
 252          var cursorPos;
 253  
 254          var scrollTop, scrollLeft;
 255          if( myField.type == 'textarea' && typeof myField.scrollTop != 'undefined' )
 256          { // remember old position
 257              scrollTop = myField.scrollTop;
 258              scrollLeft = myField.scrollLeft;
 259          }
 260  
 261          if( replace )
 262          {
 263              myField.value = myField.value.substring( 0, startPos)
 264                  + before
 265                  + after
 266                  + myField.value.substring( endPos, myField.value.length);
 267              cursorPos = startPos + before.length + after.length;
 268          }
 269          else
 270          {
 271              myField.value = myField.value.substring( 0, startPos)
 272                  + before
 273                  + myField.value.substring(startPos, endPos)
 274                  + after
 275                  + myField.value.substring( endPos, myField.value.length);
 276              cursorPos = endPos + before.length + after.length;
 277          }
 278  
 279          if( typeof scrollTop != 'undefined' )
 280          { // scroll to old position
 281              myField.scrollTop = scrollTop;
 282              myField.scrollLeft = scrollLeft;
 283          }
 284  
 285          myField.focus();
 286          myField.selectionStart = cursorPos;
 287          myField.selectionEnd = cursorPos;
 288      }
 289      else
 290      { // Default browser support:
 291          myField.value += before + after;
 292          myField.focus();
 293      }
 294  }
 295  
 296  /**

 297   * Open or close a filter area (by use of CSS style).

 298   *

 299   * You have to define a div with id clickdiv_<ID> and a img with clickimg_<ID>,

 300   * where <ID> is the first param to the function.

 301   *

 302   * @param string html id of the element to toggle

 303   * @return false

 304   */
 305  function toggle_filter_area( filter_name )
 306  {
 307      // Find objects to toggle:

 308      if( !( clickdiv = document.getElementById( 'clickdiv_'+filter_name ) )
 309              || !( clickimg = document.getElementById( 'clickimg_'+filter_name ) ) )
 310      {
 311          alert( 'ID '+filter_name+' not found!' );
 312          return false;
 313      }
 314  
 315      // Determine if we want to show or to hide (based on current state).

 316      hide = document.getElementById( 'clickdiv_'+filter_name ).style.display != 'none';
 317  
 318      if( hide )
 319      {    // Hide/collapse filters:
 320          clickdiv.style.display = 'none';
 321          clickimg.src = imgpath_expand;
 322          asyncRequest( htsrv_url+'async.php?collapse='+filter_name );
 323      }
 324      else
 325      {    // Show/expand filters
 326          clickdiv.style.display = 'block';
 327          clickimg.src = imgpath_collapse;
 328          asyncRequest( htsrv_url+'async.php?expand='+filter_name );
 329      }
 330  
 331      return false;
 332  }
 333  
 334  
 335  /**

 336   * "AJAX" wrapper

 337   *

 338   * What this really is actually, is just a function to perform an asynchronous Request to the server.

 339   * There is no need to have any XML involved.

 340   *

 341   * @param string url urlencoded

 342   */
 343  function asyncRequest( url )
 344  {
 345      if (window.XMLHttpRequest)
 346      { // browser has native support for XMLHttpRequest object
 347          req = new XMLHttpRequest();
 348      }
 349      else if (window.ActiveXObject)
 350      { // try XMLHTTP ActiveX (Internet Explorer) version
 351          req = new ActiveXObject("Microsoft.XMLHTTP");
 352      }
 353  
 354      if(req)
 355      {
 356          swapSection( '...' );
 357          //req.onreadystatechange = responseHandler;

 358      req.onreadystatechange = asyncResponseHandler;
 359          req.open( 'GET', url, true );
 360          req.setRequestHeader("content-type","application/x-www-form-urlencoded");
 361          req.send('dummy');
 362      }
 363      else
 364      {
 365          swapSection('Your browser does not seem to support XMLHttpRequest.');
 366      }
 367  
 368      return false;
 369  }
 370  
 371  function asyncResponseHandler()
 372  {
 373      if( req.readyState == 4 )
 374      {    // Request has been loaded (readyState = 4)
 375          if( req.status == 200 )
 376          {    // Status is 200 OK:
 377              swapSection( req.responseText );
 378          }
 379          else
 380          {
 381              swapSection("There was a problem retrieving the XML data:\n" + req.statusText);
 382          }
 383      }
 384  }
 385  
 386  function swapSection( data )
 387  {
 388      var swappableSection = document.getElementById('asyncResponse');
 389      if( swappableSection )
 390      {
 391          swappableSection.innerHTML = data;
 392      }
 393  }
 394  
 395  
 396  /*

 397   * Javascript callback handling, for helping plugins to interact in Javascript.

 398   *

 399   * This is, so one plugin (e.g. the tinymce_plugin) can say that it handles insertion of raw

 400   * content into a specific element ("itemform_post_content" in this case):

 401   *

 402   * <code>

 403   * if( typeof b2evo_Callbacks == "object" )

 404   * { // add a callback, that lets us insert the

 405   *   b2evo_Callbacks.register_callback( "insert_raw_into_itemform_post_content", function(value) {

 406   *       tinyMCE.execCommand( "mceInsertRawHTML", false, value );

 407   *       return true;

 408   *     } );

 409   * }

 410   * </code>

 411   *

 412   * and others (e.g. the smilies_plugin or the youtube_plugin) should first try to use this

 413   * callback to insert the HTML:

 414   *

 415   * if( typeof b2evo_Callbacks == 'object' )

 416   * { // see if there's a callback registered that should handle this:

 417   *   if( b2evo_Callbacks.trigger_callback("insert_raw_into_"+b2evoCanvas.id, tag) )

 418   *   {

 419   *     return;

 420   *   }

 421   * }

 422   */
 423  function b2evo_Callbacks() {
 424      this.eventHandlers = new Array();
 425  };
 426  
 427  b2evo_Callbacks.prototype = {
 428      register_callback : function(event, f) {
 429          if( typeof this.eventHandlers[event] == "undefined" )
 430          {
 431              this.eventHandlers[event] = new Array();
 432          }
 433          this.eventHandlers[event][this.eventHandlers[event].length] = f;
 434      },
 435  
 436      /**

 437       * @param String event name

 438       * @param mixed argument1

 439       * @param mixed argument2

 440       * ...

 441       * @return boolean true, if any callback returned true

 442       *                 null, if no callback registered

 443       */
 444      trigger_callback : function(event, args) {
 445  
 446          if( typeof this.eventHandlers[event] == "undefined" )
 447          {
 448              return null;
 449          }
 450  
 451          var r = false;
 452  
 453          // copy arguments and build function param string for eval():

 454          var cb_args = '';
 455          var cb_arguments = arguments;
 456          for( var i = 1; i < arguments.length; i++ ) {
 457              cb_args += "cb_arguments[" + i + "], ";
 458          }
 459          if( cb_args.length )
 460          { // remove last ", ":
 461              cb_args = cb_args.substring( 0, cb_args.length - 2 );
 462          }
 463  
 464          // eval() for each registered callback:

 465          for( var i = 0; i < this.eventHandlers[event].length; i++ )
 466          {
 467              var f = this.eventHandlers[event][i];
 468              r = eval( "f("+cb_args+");" ) || r;
 469          }
 470  
 471          return r;
 472      }
 473  };
 474  
 475  var b2evo_Callbacks = new b2evo_Callbacks();
 476  
 477  /*

 478   * $Log: functions.js,v $

 479   * Revision 1.30  2007/09/16 22:06:37  fplanque

 480   * minor

 481   *

 482   * Revision 1.29  2007/06/30 21:05:17  fplanque

 483   * fixed for FF; I hope.

 484   *

 485   * Revision 1.28  2007/06/29 23:42:30  blueyed

 486   * - Fixed b2evoCallback in textarea_wrap_selection: uses "wrap_selection_for_" callback now instead of "insert_raw_into_"

 487   * - trigger_callback now returns null if no callback is registered

 488   *

 489   * Revision 1.27  2007/04/20 01:42:32  fplanque

 490   * removed excess javascript

 491   *

 492   * Revision 1.26  2007/03/07 19:26:20  blueyed

 493   * Fix; for IE IIRC

 494   *

 495   * Revision 1.25  2007/01/26 02:12:09  fplanque

 496   * cleaner popup windows

 497   *

 498   * Revision 1.24  2006/12/10 03:05:02  blueyed

 499   * cleanup

 500   *

 501   * Revision 1.23  2006/11/28 19:44:51  blueyed

 502   * b2evo_Callbacks.trigger_callback() now supports variable number of arguments; textarea_replace_selection(): default to document for target_document arg

 503   *

 504   * Revision 1.22  2006/11/26 01:42:10  fplanque

 505   * doc

 506   *

 507   */


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