PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/boinc-7.0.36/html/user/bbcode_toolbar.js

https://github.com/matszpk/native-boinc-for-android
JavaScript | 234 lines | 177 code | 32 blank | 25 comment | 60 complexity | 1d3d3608dd70f07f9e55a47be156bfe4 MD5 | raw file
  1. // bbCode control by
  2. // subBlue design
  3. // www.subBlue.com
  4. // Altered by John37309 for boinc.berkeley.edu 18th June 2009
  5. // This javascript adds a bbcode toolbar
  6. // functionallity to the posting page of the forums
  7. // Startup variables
  8. var imageTag = false;
  9. var theSelection = false;
  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. var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
  15. var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
  16. && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
  17. && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));
  18. var is_moz = 0;
  19. var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
  20. var is_mac = (clientPC.indexOf("mac")!=-1);
  21. // Helpline messages
  22. b_help = "Bold text: [b]text[/b] (alt+b)";
  23. i_help = "Italic text: [i]text[/i] (alt+i)";
  24. u_help = "Underline text: [u]text[/u] (alt+u)";
  25. q_help = "Quote text: [quote]text[/quote] (alt+q)";
  26. c_help = "Code display: [code]code[/code] (alt+c)";
  27. l_help = "List: [list]text[/list] (alt+l)";
  28. o_help = "Ordered list: [list=]text[/list] (alt+o)";
  29. p_help = "Insert image: [img]http://image_url[/img] (alt+p)";
  30. w_help = "Insert URL: [url]http://url[/url] or [url=http://url]URL text[/url] (alt+w)";
  31. a_help = "Close all open bbCode tags";
  32. s_help = "Font color: [color=red]text[/color] Tip: you can also use color=#FF0000";
  33. f_help = "Font size: [size=x-small]small text[/size]";
  34. y_help = "Youtube video: [youtube]URL of movie[/youtube]";
  35. k_help = "Strike text; [s]text[/s]";
  36. // Define the bbCode tags
  37. bbcode = new Array();
  38. bbtags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[quote]','[/quote]','[code]','[/code]','[list]','[/list]','[list=]','[/list]','[img]','[/img]','[url]','[/url]','[youtube]','[/youtube]','[s]','[/s]');
  39. imageTag = false;
  40. // Shows the help messages in the helpline window
  41. function helpline(help) {
  42. document.post.helpbox.value = eval(help + "_help");
  43. }
  44. // Replacement for arrayname.length property
  45. function getarraysize(thearray) {
  46. for (i = 0; i < thearray.length; i++) {
  47. if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))
  48. return i;
  49. }
  50. return thearray.length;
  51. }
  52. // Replacement for arrayname.push(value) not implemented in IE until version 5.5
  53. // Appends element to the array
  54. function arraypush(thearray,value) {
  55. thearray[ getarraysize(thearray) ] = value;
  56. }
  57. // Replacement for arrayname.pop() not implemented in IE until version 5.5
  58. // Removes and returns the last element of an array
  59. function arraypop(thearray) {
  60. thearraysize = getarraysize(thearray);
  61. retval = thearray[thearraysize - 1];
  62. delete thearray[thearraysize - 1];
  63. return retval;
  64. }
  65. function checkForm() {
  66. formErrors = false;
  67. if (document.post.content.value.length < 2) {
  68. formErrors = "You must enter a message when posting.";
  69. }
  70. if (formErrors) {
  71. alert(formErrors);
  72. return false;
  73. } else {
  74. bbstyle(-1);
  75. //formObj.preview.disabled = true;
  76. //formObj.submit.disabled = true;
  77. return true;
  78. }
  79. }
  80. function emoticon(text) {
  81. var txtarea = document.post.content;
  82. text = ' ' + text + ' ';
  83. if (txtarea.createTextRange && txtarea.caretPos) {
  84. var caretPos = txtarea.caretPos;
  85. caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
  86. txtarea.focus();
  87. } else {
  88. txtarea.value += text;
  89. txtarea.focus();
  90. }
  91. }
  92. function bbfontstyle(bbopen, bbclose) {
  93. var txtarea = document.post.content;
  94. if ((clientVer >= 4) && is_ie && is_win) {
  95. theSelection = document.selection.createRange().text;
  96. if (!theSelection) {
  97. txtarea.value += bbopen + bbclose;
  98. txtarea.focus();
  99. return;
  100. }
  101. document.selection.createRange().text = bbopen + theSelection + bbclose;
  102. txtarea.focus();
  103. return;
  104. }
  105. else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
  106. {
  107. mozWrap(txtarea, bbopen, bbclose);
  108. return;
  109. }
  110. else
  111. {
  112. txtarea.value += bbopen + bbclose;
  113. txtarea.focus();
  114. }
  115. storeCaret(txtarea);
  116. }
  117. function bbstyle(bbnumber) {
  118. var txtarea = document.post.content;
  119. txtarea.focus();
  120. donotinsert = false;
  121. theSelection = false;
  122. bblast = 0;
  123. if (bbnumber == -1) { // Close all open tags & default button names
  124. while (bbcode[0]) {
  125. butnumber = arraypop(bbcode) - 1;
  126. txtarea.value += bbtags[butnumber + 1];
  127. buttext = eval('document.post.addbbcode' + butnumber + '.value');
  128. eval('document.post.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
  129. }
  130. imageTag = false; // All tags are closed including image tags :D
  131. txtarea.focus();
  132. return;
  133. }
  134. if ((clientVer >= 4) && is_ie && is_win)
  135. {
  136. theSelection = document.selection.createRange().text; // Get text selection
  137. if (theSelection) {
  138. // Add tags around selection
  139. document.selection.createRange().text = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
  140. txtarea.focus();
  141. theSelection = '';
  142. return;
  143. }
  144. }
  145. else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
  146. {
  147. mozWrap(txtarea, bbtags[bbnumber], bbtags[bbnumber+1]);
  148. return;
  149. }
  150. // Find last occurance of an open tag the same as the one just clicked
  151. for (i = 0; i < bbcode.length; i++) {
  152. if (bbcode[i] == bbnumber+1) {
  153. bblast = i;
  154. donotinsert = true;
  155. }
  156. }
  157. if (donotinsert) { // Close all open tags up to the one just clicked & default button names
  158. while (bbcode[bblast]) {
  159. butnumber = arraypop(bbcode) - 1;
  160. txtarea.value += bbtags[butnumber + 1];
  161. buttext = eval('document.post.addbbcode' + butnumber + '.value');
  162. eval('document.post.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
  163. imageTag = false;
  164. }
  165. txtarea.focus();
  166. return;
  167. } else { // Open tags
  168. if (imageTag && (bbnumber != 14)) { // Close image tag before adding another
  169. txtarea.value += bbtags[15];
  170. lastValue = arraypop(bbcode) - 1; // Remove the close image tag from the list
  171. document.post.addbbcode14.value = "Img"; // Return button back to normal state
  172. imageTag = false;
  173. }
  174. // Open tag
  175. txtarea.value += bbtags[bbnumber];
  176. if ((bbnumber == 14) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag
  177. arraypush(bbcode,bbnumber+1);
  178. eval('document.post.addbbcode'+bbnumber+'.value += "*"');
  179. txtarea.focus();
  180. return;
  181. }
  182. storeCaret(txtarea);
  183. }
  184. // From http://www.massless.org/mozedit/
  185. function mozWrap(txtarea, open, close)
  186. {
  187. var selLength = txtarea.textLength;
  188. var selStart = txtarea.selectionStart;
  189. var selEnd = txtarea.selectionEnd;
  190. if (selEnd == 1 || selEnd == 2)
  191. selEnd = selLength;
  192. var s1 = (txtarea.value).substring(0,selStart);
  193. var s2 = (txtarea.value).substring(selStart, selEnd)
  194. var s3 = (txtarea.value).substring(selEnd, selLength);
  195. txtarea.value = s1 + open + s2 + close + s3;
  196. return;
  197. }
  198. // Insert at Claret position. Code from
  199. // http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
  200. function storeCaret(textEl) {
  201. if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
  202. }