[ Index ]

PHP Cross Reference of phpBB 3.0 Beta 3

title

Body

[close]

/styles/subSilver/template/ -> editor.js (source)

   1  /**
   2  * bbCode control by subBlue design [ www.subBlue.com ]
   3  * Includes unixsafe colour palette selector by SHS`
   4  */
   5  
   6  // Startup variables
   7  var imageTag = false;
   8  var theSelection = false;
   9  
  10  // Check for Browser & Platform for PC & IE specific bits
  11  // More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
  12  var clientPC = navigator.userAgent.toLowerCase(); // Get client info
  13  var clientVer = parseInt(navigator.appVersion); // Get browser version
  14  
  15  var is_ie = ((clientPC.indexOf('msie') != -1) && (clientPC.indexOf('opera') == -1));
  16  var is_nav = ((clientPC.indexOf('mozilla') != -1) && (clientPC.indexOf('spoofer') == -1) && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera') == -1) && (clientPC.indexOf('webtv') == -1) && (clientPC.indexOf('hotjava') == -1));
  17  
  18  var is_win = ((clientPC.indexOf('win') != -1) || (clientPC.indexOf('16bit') != -1));
  19  var is_mac = (clientPC.indexOf('mac') != -1);
  20  
  21  /**
  22  * Shows the help messages in the helpline window
  23  */
  24  function helpline(help)
  25  {
  26      document.forms[form_name].helpbox.value = help_line[help];
  27  }
  28  
  29  /**
  30  * Replacement for arrayname.length property
  31  */
  32  function getarraysize(thearray)
  33  {
  34      for (i = 0; i < thearray.length; i++)
  35      {
  36          if (typeof thearray[i] == 'undefined' || thearray[i] == '' || thearray[i] == null)
  37          {
  38              return i;
  39          }
  40      }
  41  
  42      return thearray.length;
  43  }
  44  
  45  /**
  46  * Replacement for arrayname.push(value) not implemented in IE until version 5.5
  47  * Appends element to the array
  48  */
  49  function arraypush(thearray,value)
  50  {
  51      thearray[getarraysize(thearray)] = value;
  52  }
  53  
  54  /**
  55  * Replacement for arrayname.pop() not implemented in IE until version 5.5
  56  * Removes and returns the last element of an array
  57  */
  58  function arraypop(thearray)
  59  {
  60      thearraysize = getarraysize(thearray);
  61      retval = thearray[thearraysize - 1];
  62      delete thearray[thearraysize - 1];
  63  
  64      return retval;
  65  }
  66  
  67  /**
  68  * Apply bbcodes
  69  */
  70  function bbfontstyle(bbopen, bbclose)
  71  {
  72      theSelection = false;
  73      document.forms[form_name].elements[text_name].focus();
  74  
  75      if ((clientVer >= 4) && is_ie && is_win)
  76      {
  77          // Get text selection
  78          theSelection = document.selection.createRange().text;
  79  
  80          if (theSelection)
  81          {
  82              // Add tags around selection
  83              document.selection.createRange().text = bbopen + theSelection + bbclose;
  84              document.forms[form_name].elements[text_name].focus();
  85              theSelection = '';
  86              return;
  87          }
  88      }
  89      else if (document.forms[form_name].elements[text_name].selectionEnd && (document.forms[form_name].elements[text_name].selectionEnd - document.forms[form_name].elements[text_name].selectionStart > 0))
  90      {
  91          mozWrap(document.forms[form_name].elements[text_name], bbopen, bbclose);
  92          document.forms[form_name].elements[text_name].focus();
  93          theSelection = '';
  94          return;
  95      }
  96  
  97      // Close image tag before adding
  98      if (imageTag)
  99      {
 100          insert_text(bbtags[15]);
 101  
 102          // Remove the close image tag from the list
 103          lastValue = arraypop(bbcode) - 1;
 104  
 105          // Return button back to normal state
 106          document.forms[form_name].addbbcode14.value = 'Img';
 107          imageTag = false;
 108      }
 109  
 110      // Open tag
 111      insert_text(bbopen + bbclose);
 112  
 113      document.forms[form_name].elements[text_name].focus();
 114  
 115      storeCaret(document.forms[form_name].elements[text_name]);
 116      return;
 117  }
 118  
 119  /**
 120  * Insert text at position
 121  */
 122  function insert_text(text, spaces, popup)
 123  {
 124      var textarea;
 125      
 126      if (!popup) 
 127      {
 128          textarea = document.forms[form_name].elements[text_name];
 129      } else 
 130      {
 131          textarea = opener.document.forms[form_name].elements[text_name];
 132      }
 133      if (spaces) 
 134      {
 135          text = ' ' + text + ' ';
 136      }
 137      if (textarea.createTextRange && !isNaN(textarea.caretPos))
 138      {
 139          var caret_pos = textarea.caretPos;
 140          caret_pos.text = caret_pos.text.charAt(caret_pos.text.length - 1) == ' ' ? caret_pos.text + text + ' ' : caret_pos.text + text;
 141      }
 142      else if (!isNaN(textarea.selectionStart))
 143      {
 144          var sel_start = textarea.selectionStart;
 145          var sel_end = textarea.selectionEnd;
 146  
 147          mozWrap(textarea, text, '')
 148          textarea.selectionStart = sel_start + text.length;
 149          textarea.selectionEnd = sel_end + text.length;
 150      }
 151      else
 152      {
 153          textarea.value = textarea.value + text;
 154      }
 155  }
 156  
 157  /**
 158  * Add inline attachment at position
 159  */
 160  function attach_inline()
 161  {
 162      insert_text('[attachment=' + document.forms[form_name].elements['attachments'].value + ']' + document.forms[form_name].elements['attachments'].options[document.forms[form_name].elements['attachments'].selectedIndex].text + '[/attachment]');
 163      document.forms[form_name].elements[text_name].focus();
 164  }
 165  
 166  /**
 167  * Add quote text to message
 168  */
 169  function addquote(post_id, username)
 170  {
 171      var message_name = 'message_' + post_id;
 172      var theSelection = '';
 173      var divarea = false;
 174  
 175      if (document.all)
 176      {
 177          divarea = document.all[message_name];
 178      }
 179      else
 180      {
 181          divarea = document.getElementById(message_name);
 182      }
 183  
 184      // Get text selection - not only the post content :(
 185      if (window.getSelection)
 186      {
 187          theSelection = window.getSelection().toString();
 188      }
 189      else if (document.getSelection)
 190      {
 191          theSelection = document.getSelection();
 192      }
 193      else if (document.selection)
 194      {
 195          theSelection = document.selection.createRange().text;
 196      }
 197  
 198      if (theSelection == '' || typeof theSelection == 'undefined' || theSelection == null)
 199      {
 200          if (divarea.innerHTML)
 201          {
 202              theSelection = divarea.innerHTML.replace(/<br>/ig, '\n');
 203              theSelection = theSelection.replace(/<br\/>/ig, '\n');
 204              theSelection = theSelection.replace(/&lt\;/ig, '<');
 205              theSelection = theSelection.replace(/&gt\;/ig, '>');
 206              theSelection = theSelection.replace(/&amp\;/ig, '&');            
 207          }
 208          else if (document.all)
 209          {
 210              theSelection = divarea.innerText;
 211          }
 212          else if (divarea.textContent)
 213          {
 214              theSelection = divarea.textContent;
 215          }
 216          else if (divarea.firstChild.nodeValue)
 217          {
 218              theSelection = divarea.firstChild.nodeValue;
 219          }
 220      }
 221  
 222      if (theSelection)
 223      {
 224          insert_text('[quote="' + username + '"]' + theSelection + '[/quote]');
 225      }
 226  
 227      return;
 228  }
 229  
 230  /**
 231  * bbstyle
 232  */
 233  function bbstyle(bbnumber)
 234  {
 235      donotinsert = false;
 236      theSelection = false;
 237      bblast = 0;
 238      document.forms[form_name].elements[text_name].focus();
 239  
 240      // Close all open tags & default button names
 241      if (bbnumber == -1)
 242      {
 243          while (bbcode[0])
 244          {
 245              butnumber = arraypop(bbcode) - 1;
 246              document.forms[form_name].elements[text_name].value += bbtags[butnumber + 1];
 247              buttext = document.forms[form_name]['addbbcode' + butnumber].value;
 248          
 249              if (buttext != '[*]')
 250              {
 251                  document.forms[form_name]['addbbcode' + butnumber].value = buttext.substr(0,(buttext.length - 1));                
 252              }
 253          }
 254  
 255          document.forms[form_name].addbbcode10.value = 'List';
 256          bbtags[10] = '[list]';
 257  
 258          document.forms[form_name].addbbcode12.value = 'List=';
 259          bbtags[12] = '[list=]';
 260          
 261          // All tags are closed including image tags :D
 262          imageTag = false;
 263          document.forms[form_name].elements[text_name].focus();
 264  
 265          return;
 266      }
 267  
 268      // [*] doesn't have an end tag
 269      noEndTag = (bbtags[bbnumber] == '[*]')
 270  
 271      if ((clientVer >= 4) && is_ie && is_win)
 272      {
 273          // Get text selection
 274          theSelection = document.selection.createRange().text;
 275  
 276          if (theSelection) 
 277          {
 278              // Add tags around selection
 279              document.selection.createRange().text = bbtags[bbnumber] + theSelection + ((!noEndTag) ? bbtags[bbnumber+1] : '');
 280              document.forms[form_name].elements[text_name].focus();
 281              theSelection = '';
 282              return;
 283          }
 284      }
 285      else if (document.forms[form_name].elements[text_name].selectionEnd && (document.forms[form_name].elements[text_name].selectionEnd - document.forms[form_name].elements[text_name].selectionStart > 0))
 286      {
 287          mozWrap(document.forms[form_name].elements[text_name], bbtags[bbnumber], ((!noEndTag) ? bbtags[bbnumber+1] : ''));
 288          document.forms[form_name].elements[text_name].focus();
 289          theSelection = '';
 290          return;
 291      }
 292  
 293      // Find last occurance of an open tag the same as the one just clicked
 294      for (i = 0; i < bbcode.length; i++)
 295      {
 296          if (bbcode[i] == bbnumber+1)
 297          {
 298              bblast = i;
 299              donotinsert = true;
 300          }
 301      }
 302  
 303      if (bbnumber == 10 && bbtags[10] != '[*]')
 304      {
 305          if (donotinsert)
 306          {
 307              document.forms[form_name].addbbcode12.value = 'List=';
 308              tmp_help = o_help;
 309              o_help = e_help;
 310              e_help = tmp_help;
 311              bbtags[12] = '[list=]';
 312          }
 313          else
 314          {
 315              document.forms[form_name].addbbcode12.value = '[*]';
 316              tmp_help = o_help;
 317              o_help = e_help;
 318              e_help = tmp_help;
 319              bbtags[12] = '[*]';
 320          }
 321      }
 322  
 323      if (bbnumber == 12 && bbtags[12] != '[*]')
 324      {
 325          if (donotinsert)
 326          {
 327              document.forms[form_name].addbbcode10.value = 'List';
 328              tmp_help = l_help;
 329              l_help = e_help;
 330              e_help = tmp_help;
 331              bbtags[10] = '[list]';
 332          }
 333          else
 334          {
 335              document.forms[form_name].addbbcode10.value = '[*]';
 336              tmp_help = l_help;
 337              l_help = e_help;
 338              e_help = tmp_help;
 339              bbtags[10] = '[*]';
 340          }
 341      }
 342  
 343      // Close all open tags up to the one just clicked & default button names
 344      if (donotinsert)
 345      {
 346          while (bbcode[bblast])
 347          {
 348              butnumber = arraypop(bbcode) - 1;
 349  
 350              if (bbtags[butnumber] != '[*]')
 351              {
 352                  insert_text(bbtags[butnumber + 1]);
 353              }
 354              else
 355              {
 356                  insert_text(bbtags[butnumber]);
 357              }
 358  
 359              buttext = document.forms[form_name]['addbbcode' + butnumber].value;
 360  
 361              if (bbtags[butnumber] != '[*]')
 362              {
 363                  document.forms[form_name]['addbbcode' + butnumber].value = buttext.substr(0,(buttext.length - 1));
 364              }
 365              imageTag = false;
 366          }
 367          document.forms[form_name].elements[text_name].focus();
 368          return;
 369      }
 370      else
 371      {
 372          // Open tags
 373  
 374          // Close image tag before adding another
 375          if (imageTag && (bbnumber != 14))
 376          {
 377              insert_text(bbtags[15]);
 378  
 379              // Remove the close image tag from the list
 380              lastValue = arraypop(bbcode) - 1;
 381  
 382              // Return button back to normal state
 383              document.forms[form_name].addbbcode14.value = 'Img';
 384              imageTag = false;
 385          }
 386  
 387          // Open tag
 388          insert_text(bbtags[bbnumber]);
 389  
 390          // Check to stop additional tags after an unclosed image tag
 391          if (bbnumber == 14 && imageTag == false)
 392          {
 393              imageTag = 1; 
 394          }
 395  
 396          if (bbtags[bbnumber] != '[*]')
 397          {
 398              arraypush(bbcode, bbnumber + 1);
 399              document.forms[form_name]['addbbcode' + bbnumber].value += "*";
 400          }
 401  
 402          document.forms[form_name].elements[text_name].focus();
 403          return;
 404      }
 405  
 406      storeCaret(document.forms[form_name].elements[text_name]);
 407  }
 408  
 409  /**
 410  * From http://www.massless.org/mozedit/
 411  */
 412  function mozWrap(txtarea, open, close)
 413  {
 414      var selLength = txtarea.textLength;
 415      var selStart = txtarea.selectionStart;
 416      var selEnd = txtarea.selectionEnd;
 417      var scrollTop = txtarea.scrollTop;
 418  
 419      if (selEnd == 1 || selEnd == 2) 
 420      {
 421          selEnd = selLength;
 422      }
 423  
 424      var s1 = (txtarea.value).substring(0,selStart);
 425      var s2 = (txtarea.value).substring(selStart, selEnd)
 426      var s3 = (txtarea.value).substring(selEnd, selLength);
 427  
 428      txtarea.value = s1 + open + s2 + close + s3;
 429      txtarea.selectionStart = selEnd + open.length + close.length;
 430      txtarea.selectionEnd = txtarea.selectionStart;
 431      txtarea.focus();
 432      txtarea.scrollTop = scrollTop;
 433  
 434      return;
 435  }
 436  
 437  /**
 438  * Insert at Claret position. Code from
 439  * http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
 440  */
 441  function storeCaret(textEl)
 442  {
 443      if (textEl.createTextRange)
 444      {
 445          textEl.caretPos = document.selection.createRange().duplicate();
 446      }
 447  }
 448  
 449  /**
 450  * Color pallette
 451  */
 452  function colorPalette(dir, width, height)
 453  {
 454      var r = 0, g = 0, b = 0;
 455      var numberList = new Array(6);
 456  
 457      numberList[0] = '00';
 458      numberList[1] = '40';
 459      numberList[2] = '80';
 460      numberList[3] = 'BF';
 461      numberList[4] = 'FF';
 462  
 463      document.writeln('<table cellspacing="1" cellpadding="0" border="0">');
 464  
 465      for (r = 0; r < 5; r++)
 466      {
 467          if (dir == 'h')
 468          {
 469              document.writeln('<tr>');
 470          }
 471  
 472          for (g = 0; g < 5; g++)
 473          {
 474              if (dir == 'v')
 475              {
 476                  document.writeln('<tr>');
 477              }
 478              
 479              for (b = 0; b < 5; b++)
 480              {
 481                  color = String(numberList[r]) + String(numberList[g]) + String(numberList[b]);
 482                  document.write('<td bgcolor="#' + color + '">');
 483                  document.write('<a href="#" onclick="bbfontstyle(\'[color=#' + color + ']\', \'[/color]\'); return false;" onmouseover="helpline(\'s\');"><img src="images/spacer.gif" width="' + width + '" height="' + height + '" alt="#' + color + '" title="#' + color + '" /></a>');
 484                  document.writeln('</td>');
 485              }
 486  
 487              if (dir == 'v')
 488              {
 489                  document.writeln('</tr>');
 490              }
 491          }
 492  
 493          if (dir == 'h')
 494          {
 495              document.writeln('</tr>');
 496          }
 497      }
 498      document.writeln('</table>');
 499  }


Generated: Wed Nov 22 00:35:05 2006 Cross-referenced by PHPXref 0.6