home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2007 April / PCpro_2007_04.ISO / files / dsl / NVinst.exe / Scripts / HTTP / 127.0.0.3 / ReloadCMS / editor.js < prev    next >
Encoding:
JavaScript  |  2007-02-10  |  5.9 KB  |  212 lines

  1. // bbcode[id] control by subBlue design [ www.subBlue.com ]
  2. // Includes unixsafe colour palette selector by SHS`
  3.  
  4. // Startup variables
  5. var imageTag = false;
  6. var theSelection = false;
  7.  
  8. // Check for Browser & Platform for PC & IE specific bits
  9. // More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
  10. var clientPC = navigator.userAgent.toLowerCase(); // Get client info
  11. var clientVer = parseInt(navigator.appVersion); // Get browser version
  12.  
  13. var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
  14. var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
  15.                 && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
  16.                 && (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. // Replacement for arrayname.length property
  22. function getarraysize(thearray) {
  23.     for (i = 0; i < thearray.length; i++) {
  24.         if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))
  25.             return i;
  26.         }
  27.     return thearray.length;
  28. }
  29.  
  30. // Replacement for arrayname.push(value) not implemented in IE until version 5.5
  31. // Appends element to the array
  32. function arraypush(thearray,value) {
  33.     thearray[ getarraysize(thearray) ] = value;
  34. }
  35.  
  36. // Replacement for arrayname.pop() not implemented in IE until version 5.5
  37. // Removes and returns the last element of an array
  38. function arraypop(thearray) {
  39.     thearraysize = getarraysize(thearray);
  40.     retval = thearray[thearraysize - 1];
  41.     delete thearray[thearraysize - 1];
  42.     return retval;
  43. }
  44.  
  45.  
  46. function insert_text(textarea, text) {
  47.     if (textarea.createTextRange && textarea.caretPos) {
  48.         var caretPos = textarea.caretPos;
  49.         caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
  50.     } else {
  51.         var selStart = textarea.selectionStart;
  52.         var selEnd = textarea.selectionEnd;
  53.         mozWrap(textarea, text, '')
  54.         textarea.selectionStart = selStart + text.length;
  55.         textarea.selectionEnd = selEnd + text.length;
  56.     }
  57. }
  58.  
  59. function addquote(textarea, post_id, username) {
  60.  
  61.     var message_name = 'message_' + post_id;
  62.     var theSelection = '';
  63.     var divarea = false;
  64.  
  65.     if (document.all)
  66.     {
  67.         eval("divarea = document.all." + message_name + ";");
  68.     }
  69.     else
  70.     {
  71.         eval("divarea = document.getElementById('" + message_name + "');");
  72.     }
  73.  
  74.     // Get text selection - not only the post content :(
  75.     if (window.getSelection)
  76.     {
  77.         theSelection = window.getSelection().toString();
  78.     }
  79.     else if (document.getSelection)
  80.     {
  81.         theSelection = document.getSelection();
  82.     }
  83.     else if (document.selection)
  84.     {
  85.         theSelection = document.selection.createRange().text;
  86.     }
  87.  
  88.     if (theSelection == '')
  89.     {
  90.         if (document.all)
  91.         {
  92.             theSelection = divarea.innerText;
  93.         }
  94.         else if (divarea.textContent)
  95.         {
  96.             theSelection = divarea.textContent;
  97.         }
  98.         else if (divarea.firstChild.nodeValue)
  99.         {
  100.             theSelection = divarea.firstChild.nodeValue;
  101.         }
  102.     }
  103.     
  104.     if (theSelection)
  105.     {
  106.         if(username){
  107.             insert_text(textarea, '[quote="' + username + '"]' + theSelection + '[/quote]');
  108.         } else {
  109.             insert_text(textarea, '[quote]' + theSelection + '[/quote]');
  110.         }
  111.     }
  112.  
  113.     return;
  114. }
  115.  
  116. function bbstyle(textarea, bbnumber, id) {
  117.     donotinsert = new Array();
  118.     if(!bbcode[id]) {
  119.         bbcode[id] = new Array();
  120.     }
  121.     donotinsert[id] = false;
  122.     theSelection = false;
  123.     bblast[id] = 0;
  124.     textarea.focus();
  125.  
  126.     if (bbnumber == -1) { // Close all open tags & default button names
  127.         while (bbcode[id][0]) {
  128.             butnumber = arraypop(bbcode[id]) - 1;
  129.             textarea.value += bbtags[butnumber + 1];
  130.         }
  131.         imageTag = false; // All tags are closed including image tags :D
  132.         textarea.focus();
  133.         return;
  134.     }
  135.  
  136.     if ((clientVer >= 4) && is_ie && is_win)
  137.     {
  138.         theSelection = document.selection.createRange().text; // Get text selection
  139.         if (theSelection) {
  140.             // Add tags around selection
  141.             document.selection.createRange().text = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
  142.             textarea.focus();
  143.             theSelection = '';
  144.             return;
  145.         }
  146.     }
  147.     else if (textarea.selectionEnd && (textarea.selectionEnd - textarea.selectionStart > 0))
  148.     {
  149.         mozWrap(textarea, bbtags[bbnumber], bbtags[bbnumber+1]);
  150.         textarea.focus();
  151.         theSelection = '';
  152.         return;
  153.     }
  154.  
  155.     // Find last occurance of an open tag the same as the one just clicked
  156.     for (i = 0; i < bbcode[id].length; i++) {
  157.         if (bbcode[id][i] == bbnumber+1) {
  158.             bblast[id] = i;
  159.             donotinsert[id] = true;
  160.         }
  161.     }
  162.     if (donotinsert[id]) {        // Close all open tags up to the one just clicked & default button names
  163.         while (bbcode[id][bblast[id]]) {
  164.             butnumber = arraypop(bbcode[id]) - 1;
  165.             insert_text(textarea, bbtags[butnumber + 1]);
  166.             imageTag = false;
  167.         }
  168.         textarea.focus();
  169.         return;
  170.     } else { // Open tags
  171.  
  172.         if (imageTag && (bbnumber != 10)) {        // Close image tag before adding another
  173.             insert_text(textarea, bbtags[11]);
  174.  
  175.             lastValue = arraypop(bbcode[id]) - 1;    // Remove the close image tag from the list
  176.             imageTag = false;
  177.         }
  178.  
  179.         // Open tag
  180.         insert_text(textarea, bbtags[bbnumber]);
  181.  
  182.         if ((bbnumber == 10) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag
  183.         arraypush(bbcode[id],bbnumber+1);
  184.         textarea.focus();
  185.         return;
  186.     }
  187.  
  188.     storeCaret(textarea);
  189. }
  190.  
  191. // From http://www.massless.org/mozedit/
  192. function mozWrap(txtarea, open, close)
  193. {
  194.     var selLength = txtarea.textLength;
  195.     var selStart = txtarea.selectionStart;
  196.     var selEnd = txtarea.selectionEnd;
  197.     if (selEnd == 1 || selEnd == 2) 
  198.         selEnd = selLength;
  199.  
  200.     var s1 = (txtarea.value).substring(0,selStart);
  201.     var s2 = (txtarea.value).substring(selStart, selEnd)
  202.     var s3 = (txtarea.value).substring(selEnd, selLength);
  203.     txtarea.value = s1 + open + s2 + close + s3;
  204.     return;
  205. }
  206.  
  207. // Insert at Claret position. Code from
  208. // http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
  209. function storeCaret(textEl) {
  210.     if (textEl.createTextRange) { textEl.caretPos = document.selection.createRange().duplicate(); }
  211. }
  212.