editor.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Startup variables
  2. var imageTag = false;
  3. var mystored_selection = '';
  4. // Check for Browser & Platform for PC & IE specific bits
  5. // More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
  6. var clientPC = navigator.userAgent.toLowerCase(); // Get client info
  7. var clientVer = parseInt(navigator.appVersion); // Get browser version
  8. var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
  9. var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
  10. && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
  11. && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));
  12. var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
  13. var is_mac = (clientPC.indexOf("mac")!=-1);
  14. // Replacement for arrayname.length property
  15. function getarraysize(thearray) {
  16. for (i = 0; i < thearray.length; i++) {
  17. if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))
  18. return i;
  19. }
  20. return thearray.length;
  21. }
  22. // Replacement for arrayname.push(value) not implemented in IE until version 5.5
  23. // Appends element to the array
  24. function arraypush(thearray,value) {
  25. thearray[ getarraysize(thearray) ] = value;
  26. }
  27. // Replacement for arrayname.pop() not implemented in IE until version 5.5
  28. // Removes and returns the last element of an array
  29. function arraypop(thearray) {
  30. thearraysize = getarraysize(thearray);
  31. retval = thearray[thearraysize - 1];
  32. delete thearray[thearraysize - 1];
  33. return retval;
  34. }
  35. function insert_text(textarea, text) {
  36. if (textarea.createTextRange && textarea.caretPos) {
  37. var caretPos = textarea.caretPos;
  38. caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
  39. } else {
  40. var selStart = textarea.selectionStart;
  41. var selEnd = textarea.selectionEnd;
  42. mozWrap(textarea, text, '')
  43. textarea.selectionStart = selStart + text.length;
  44. textarea.selectionEnd = selEnd + text.length;
  45. }
  46. }
  47. function checkselection() {
  48. var myselection = '';
  49. if ( window.getSelection ) {
  50. myselection = window.getSelection();
  51. } else if ( document.selection ) {
  52. myselection = document.selection.createRange().text;
  53. } else if ( document.getSelection ) {
  54. myselection = document.getSelection();
  55. }
  56. if ( myselection != '' && myselection != null ) {
  57. if ( myselection != mystored_selection ) {
  58. mystored_selection = (myselection.toString() != '') ? myselection.toString() : null;
  59. }
  60. } else {
  61. mystored_selection = null;
  62. }
  63. }
  64. function addquote(textarea) {
  65. if ( mystored_selection != '' && mystored_selection != null ) {
  66. if (textarea){
  67. insert_text(textarea, '[quote]' + mystored_selection + '[/quote]\n');
  68. }
  69. }
  70. return false;
  71. }
  72. function bbstyle(textarea, bbnumber, id) {
  73. donotinsert = new Array();
  74. if(!bbcode[id]) {
  75. bbcode[id] = new Array();
  76. }
  77. donotinsert[id] = false;
  78. theSelection = false;
  79. bblast[id] = 0;
  80. textarea.focus();
  81. if (bbnumber == -1) { // Close all open tags & default button names
  82. while (bbcode[id][0]) {
  83. butnumber = arraypop(bbcode[id]) - 1;
  84. textarea.value += bbtags[butnumber + 1];
  85. }
  86. imageTag = false; // All tags are closed including image tags :D
  87. textarea.focus();
  88. return;
  89. }
  90. if ((clientVer >= 4) && is_ie && is_win) {
  91. theSelection = document.selection.createRange().text; // Get text selection
  92. if (theSelection) {
  93. // Add tags around selection
  94. document.selection.createRange().text = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
  95. textarea.focus();
  96. theSelection = '';
  97. return;
  98. }
  99. } else if (textarea.selectionEnd && (textarea.selectionEnd - textarea.selectionStart > 0)) {
  100. mozWrap(textarea, bbtags[bbnumber], bbtags[bbnumber+1]);
  101. textarea.focus();
  102. theSelection = '';
  103. return;
  104. }
  105. // Find last occurance of an open tag the same as the one just clicked
  106. for (i = 0; i < bbcode[id].length; i++) {
  107. if (bbcode[id][i] == bbnumber+1) {
  108. bblast[id] = i;
  109. donotinsert[id] = true;
  110. }
  111. }
  112. if (donotinsert[id]) { // Close all open tags up to the one just clicked & default button names
  113. while (bbcode[id][bblast[id]]) {
  114. butnumber = arraypop(bbcode[id]) - 1;
  115. insert_text(textarea, bbtags[butnumber + 1]);
  116. imageTag = false;
  117. }
  118. textarea.focus();
  119. return;
  120. } else { // Open tags
  121. if (imageTag && (bbnumber != 10)) { // Close image tag before adding another
  122. insert_text(textarea, bbtags[11]);
  123. lastValue = arraypop(bbcode[id]) - 1; // Remove the close image tag from the list
  124. imageTag = false;
  125. }
  126. // Open tag
  127. insert_text(textarea, bbtags[bbnumber]);
  128. if ((bbnumber == 10) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag
  129. arraypush(bbcode[id],bbnumber+1);
  130. textarea.focus();
  131. return;
  132. }
  133. storeCaret(textarea);
  134. }
  135. // From http://www.massless.org/mozedit/
  136. function mozWrap(txtarea, open, close)
  137. {
  138. var selLength = txtarea.textLength;
  139. var selStart = txtarea.selectionStart;
  140. var selEnd = txtarea.selectionEnd;
  141. if (selEnd == 1 || selEnd == 2)
  142. selEnd = selLength;
  143. var s1 = (txtarea.value).substring(0,selStart);
  144. var s2 = (txtarea.value).substring(selStart, selEnd)
  145. var s3 = (txtarea.value).substring(selEnd, selLength);
  146. txtarea.value = s1 + open + s2 + close + s3;
  147. return;
  148. }
  149. // Insert at Claret position. Code from
  150. // http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
  151. function storeCaret(textEl) {
  152. if (textEl.createTextRange) {
  153. textEl.caretPos = document.selection.createRange().duplicate();
  154. }
  155. }