PageRenderTime 66ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/www/lib/jquery/jquery.bw.bbcode.js

http://firmkernel.googlecode.com/
JavaScript | 345 lines | 303 code | 18 blank | 24 comment | 58 complexity | 2af6230cd0aeac32caabb7de72145171 MD5 | raw file
Possible License(s): LGPL-3.0
  1. /*
  2. +-----------------------------------------------------------------------------+
  3. | $Id: jquery.bw.bbcode.js 2009-10-01 04:20:57Z Bleakwind $
  4. | Build BBCode
  5. | Copyright (c) 2003-2010 Bleakwind (www.weaverdream.com)
  6. | http://www.weaverdream.com/
  7. | Release under the GNU Lesser General Public License Version 3 (LGPLv3):
  8. | http://www.gnu.org/licenses/lgpl.html
  9. +-----------------------------------------------------------------------------+
  10. */
  11. (function($){
  12. $.fn.extend({
  13. bwbbcode:function(option){
  14. var font_default = ["Arial", "Arial Black", "Arial Narrow", "Book Antiqua", "Century Gothic",
  15. "Comic Sans MS", "Courier New", "Franklin Gothic Medium", "Garamond", "Georgia",
  16. "Impact", "Lucida Console", "Lucida Sans Unicode", "Microsoft Sans Serif", "Palatino Linotype",
  17. "Tahoma", "Times New Roman", "Trebuchet MS", "Verdana"];
  18. var size_default = ["9", "10", "12", "14", "18", "24", "36"];
  19. var color_default = ["#002060", "#0070c0", "#c00000", "#00b050", "#8064a2", "#ff0000", "#ffc000",
  20. "#000000", "#0f243e", "#632423", "#4f6128", "#3f3151", "#a40035", "#974806",
  21. "#262626", "#17365d", "#953734", "#76923c", "#5f497a", "#ff0000", "#e36c09",
  22. "#595959", "#1f497d", "#c0504d", "#9bbb59", "#8064a2", "#fa4251", "#f79646",
  23. "#7f7f7f", "#548dd4", "#d99694", "#c3d69b", "#b2a2c7", "#ff909b", "#fac08f",
  24. "#bfbfbf", "#8db3e2", "#e5b9b7", "#d7e3bc", "#ccc1d9", "#ffbfc6", "#fbd5b5",
  25. "#ffffff", "#c6d9f0", "#f2dcdb", "#ebf1dd", "#e5e0ec", "#ffdee1", "#fdeada"];
  26. var bgcolor_default = ["#002060", "#0070c0", "#c00000", "#00b050", "#8064a2", "#ff0000", "#ffc000",
  27. "#000000", "#0f243e", "#632423", "#4f6128", "#3f3151", "#a40035", "#974806",
  28. "#262626", "#17365d", "#953734", "#76923c", "#5f497a", "#ff0000", "#e36c09",
  29. "#595959", "#1f497d", "#c0504d", "#9bbb59", "#8064a2", "#fa4251", "#f79646",
  30. "#7f7f7f", "#548dd4", "#d99694", "#c3d69b", "#b2a2c7", "#ff909b", "#fac08f",
  31. "#bfbfbf", "#8db3e2", "#e5b9b7", "#d7e3bc", "#ccc1d9", "#ffbfc6", "#fbd5b5",
  32. "#ffffff", "#c6d9f0", "#f2dcdb", "#ebf1dd", "#e5e0ec", "#ffdee1", "#fdeada"];
  33. var font_button;
  34. var font_selector_ifshow = false;
  35. var size_button;
  36. var size_selector_ifshow = false;
  37. var color_button;
  38. var color_selector_ifshow = false;
  39. var bgcolor_button;
  40. var bgcolor_selector_ifshow = false;
  41. $('#bwbbcode_bold').click(function() { selectInsert('[b]', '[/b]'); });
  42. $('#bwbbcode_italic').click(function() { selectInsert('[i]', '[/i]'); });
  43. $('#bwbbcode_underline').click(function() { selectInsert('[u]', '[/u]'); });
  44. $('#bwbbcode_line-through').click(function() { selectInsert('[s]', '[/s]'); });
  45. $('#bwbbcode_font').change(function() { selectInsert('[font='+$(this).val()+']', '[/font]'); $(this).attr("value",''); });
  46. $('#bwbbcode_size').change(function() { selectInsert('[size='+$(this).val()+']', '[/size]'); $(this).attr("value",''); });
  47. $('#bwbbcode_color').change(function() { selectInsert('[color='+$(this).val()+']', '[/color]'); $(this).attr("value",''); });
  48. $('#bwbbcode_bgcolor').change(function() { selectInsert('[bgcolor='+$(this).val()+']', '[/bgcolor]'); $(this).attr("value",''); });
  49. $('#bwbbcode_alignleft').click(function() { selectInsert('[align=left]', '[/align]'); });
  50. $('#bwbbcode_aligncenter').click(function() { selectInsert('[align=center]', '[/align]'); });
  51. $('#bwbbcode_alignright').click(function() { selectInsert('[align=right]', '[/align]'); });
  52. $('#bwbbcode_quote').click(function() { selectInsert('[quote]', '[/quote]'); });
  53. $('#bwbbcode_code').click(function() { selectInsert('[code]', '[/code]'); });
  54. $('#bwbbcode_url').click(function() {
  55. //var p1 = prompt("显示链接的文本, 留空将显示链接地址...","");
  56. var p1 = prompt("显示链接的文本...","");
  57. if (p1 != null) {
  58. var p2 = prompt("链接地址...","http://");
  59. if (p2 != '' && p2 != 'http://') {
  60. if (p1 != '') {
  61. $('#content').insertIn('[url='+p2+']'+p1+'[/url]');
  62. } else {
  63. $('#content').insertIn('[url]'+p2+'[/url]');
  64. }
  65. }
  66. }
  67. });
  68. $('#bwbbcode_mail').click(function() {
  69. //var p1 = prompt("显示链接的文本, 留空将显示E-mail地址...","");
  70. var p1 = prompt("显示链接的文本...","");
  71. if (p1 != null) {
  72. var p2 = prompt("E-mail地址...","xxx@xxx.com");
  73. if (p2 != '') {
  74. if (p1 != '') {
  75. $('#content').insertIn('[email='+p2+']'+p1+'[/email]');
  76. } else {
  77. $('#content').insertIn('[email]'+p2+'[/email]');
  78. }
  79. }
  80. }
  81. });
  82. $('#bwbbcode_image').click(function() {
  83. var p = prompt('图片地址...', 'http://');
  84. if (p == null || $.trim(p) == '' || p.toLowerCase() == 'http://') {
  85. return;
  86. }
  87. $('#content').insertIn('[img]' + p + '[/img]');
  88. });
  89. // font
  90. font_selector = $("<div id='bwbbcode_font_selector' unselectable='on'></div>");
  91. $.each(font_default, function(i){
  92. selector_content = $("<div class='bwbbcode_font_palette' unselectable='on'>"+this.toString()+"</div>")
  93. selector_content.css("font-family", this.toString());
  94. selector_content.bind("click", function(e){
  95. selectInsert('[font='+font_default[i]+']', '[/font]');
  96. font_selector_hide();
  97. });
  98. selector_content.bind("mouseover", function(e){
  99. $(this).removeClass("bwbbcode_font_palette");
  100. $(this).addClass("bwbbcode_font_palette_mouseover");
  101. });
  102. selector_content.bind("mouseout", function(e){
  103. $(this).removeClass("bwbbcode_font_palette_mouseover");
  104. $(this).addClass("bwbbcode_font_palette");
  105. });
  106. selector_content.appendTo(font_selector);
  107. });
  108. $("body").append(font_selector);
  109. font_selector.hide();
  110. $("#bwbbcode_font").bind("click", function(event){
  111. font_button = this;
  112. font_selector_ifshow ? font_selector_hide() : font_selector_show();
  113. });
  114. font_check_mouse = function(event){
  115. var font_selector_this = "div#bwbbcode_font_selector";
  116. var font_selector_parent = $(event.target).parents(font_selector_this).length;
  117. if(event.target == $(font_selector_this)[0] || event.target == font_button || font_selector_parent > 0) { return; }
  118. font_selector_hide();
  119. }
  120. font_selector_hide = function(){
  121. var font_selector_this = $("div#bwbbcode_font_selector");
  122. $(document).unbind("mousedown", font_check_mouse);
  123. font_selector_this.hide();
  124. font_selector_ifshow = false;
  125. }
  126. font_selector_show = function(){
  127. var font_selector_this = $("div#bwbbcode_font_selector");
  128. font_selector_this.css({
  129. top: $(font_button).offset().top + ($(font_button).outerHeight()),
  130. left: $(font_button).offset().left
  131. });
  132. font_selector_this.show();
  133. $(document).bind("mousedown", font_check_mouse);
  134. font_selector_ifshow = true;
  135. }
  136. //////
  137. // size
  138. size_selector = $("<div id='bwbbcode_size_selector' unselectable='on'></div>");
  139. $.each(size_default, function(i){
  140. selector_content = $("<div class='bwbbcode_size_palette' unselectable='on'>"+this.toString()+"</div>")
  141. selector_content.css("font-size", this.toString()+"px");
  142. selector_content.bind("click", function(e){
  143. selectInsert('[size='+size_default[i]+']', '[/size]');
  144. size_selector_hide();
  145. });
  146. selector_content.bind("mouseover", function(e){
  147. $(this).removeClass("bwbbcode_size_palette");
  148. $(this).addClass("bwbbcode_size_palette_mouseover");
  149. });
  150. selector_content.bind("mouseout", function(e){
  151. $(this).removeClass("bwbbcode_size_palette_mouseover");
  152. $(this).addClass("bwbbcode_size_palette");
  153. });
  154. selector_content.appendTo(size_selector);
  155. });
  156. $("body").append(size_selector);
  157. size_selector.hide();
  158. $("#bwbbcode_size").bind("click", function(event){
  159. size_button = this;
  160. size_selector_ifshow ? size_selector_hide() : size_selector_show();
  161. });
  162. size_check_mouse = function(event){
  163. var size_selector_this = "div#bwbbcode_size_selector";
  164. var size_selector_parent = $(event.target).parents(size_selector_this).length;
  165. if(event.target == $(size_selector_this)[0] || event.target == size_button || size_selector_parent > 0) { return; }
  166. size_selector_hide();
  167. }
  168. size_selector_hide = function(){
  169. var size_selector_this = $("div#bwbbcode_size_selector");
  170. $(document).unbind("mousedown", size_check_mouse);
  171. size_selector_this.hide();
  172. size_selector_ifshow = false;
  173. }
  174. size_selector_show = function(){
  175. var size_selector_this = $("div#bwbbcode_size_selector");
  176. size_selector_this.css({
  177. top: $(size_button).offset().top + ($(size_button).outerHeight()),
  178. left: $(size_button).offset().left
  179. });
  180. size_selector_this.show();
  181. $(document).bind("mousedown", size_check_mouse);
  182. size_selector_ifshow = true;
  183. }
  184. //////
  185. // color
  186. color_selector = $("<div id='bwbbcode_color_selector' unselectable='on'></div>");
  187. $.each(color_default, function(i){
  188. selector_content = $("<div class='bwbbcode_color_palette' unselectable='on'>&nbsp;</div>")
  189. selector_content.css("background-color", this.toString());
  190. selector_content.bind("click", function(e){
  191. selectInsert('[color='+color_default[i]+']', '[/color]');
  192. color_selector_hide();
  193. });
  194. selector_content.bind("mouseover", function(e){
  195. $(this).removeClass("bwbbcode_color_palette");
  196. $(this).addClass("bwbbcode_color_palette_mouseover");
  197. });
  198. selector_content.bind("mouseout", function(e){
  199. $(this).removeClass("bwbbcode_color_palette_mouseover");
  200. $(this).addClass("bwbbcode_color_palette");
  201. });
  202. selector_content.appendTo(color_selector);
  203. });
  204. $("body").append(color_selector);
  205. color_selector.hide();
  206. $("#bwbbcode_color").bind("click", function(event){
  207. color_button = this;
  208. color_selector_ifshow ? color_selector_hide() : color_selector_show();
  209. });
  210. color_check_mouse = function(event){
  211. var color_selector_this = "div#bwbbcode_color_selector";
  212. var color_selector_parent = $(event.target).parents(color_selector_this).length;
  213. if(event.target == $(color_selector_this)[0] || event.target == color_button || color_selector_parent > 0) { return; }
  214. color_selector_hide();
  215. }
  216. color_selector_hide = function(){
  217. var color_selector_this = $("div#bwbbcode_color_selector");
  218. $(document).unbind("mousedown", color_check_mouse);
  219. color_selector_this.hide();
  220. color_selector_ifshow = false;
  221. }
  222. color_selector_show = function(){
  223. var color_selector_this = $("div#bwbbcode_color_selector");
  224. color_selector_this.css({
  225. top: $(color_button).offset().top + ($(color_button).outerHeight()),
  226. left: $(color_button).offset().left
  227. });
  228. color_selector_this.show();
  229. $(document).bind("mousedown", color_check_mouse);
  230. color_selector_ifshow = true;
  231. }
  232. //////
  233. // bgcolor
  234. bgcolor_selector = $("<div id='bwbbcode_bgcolor_selector' unselectable='on'></div>");
  235. $.each(bgcolor_default, function(i){
  236. selector_content = $("<div class='bwbbcode_bgcolor_palette' unselectable='on'>&nbsp;</div>")
  237. selector_content.css("background-color", this.toString());
  238. selector_content.bind("click", function(e){
  239. selectInsert('[bgcolor='+bgcolor_default[i]+']', '[/bgcolor]');
  240. bgcolor_selector_hide();
  241. });
  242. selector_content.bind("mouseover", function(e){
  243. $(this).removeClass("bwbbcode_bgcolor_palette");
  244. $(this).addClass("bwbbcode_bgcolor_palette_mouseover");
  245. });
  246. selector_content.bind("mouseout", function(e){
  247. $(this).removeClass("bwbbcode_bgcolor_palette_mouseover");
  248. $(this).addClass("bwbbcode_bgcolor_palette");
  249. });
  250. selector_content.appendTo(bgcolor_selector);
  251. });
  252. $("body").append(bgcolor_selector);
  253. bgcolor_selector.hide();
  254. $("#bwbbcode_bgcolor").bind("click", function(event){
  255. bgcolor_button = this;
  256. bgcolor_selector_ifshow ? bgcolor_selector_hide() : bgcolor_selector_show();
  257. });
  258. bgcolor_check_mouse = function(event){
  259. var bgcolor_selector_this = "div#bwbbcode_bgcolor_selector";
  260. var bgcolor_selector_parent = $(event.target).parents(bgcolor_selector_this).length;
  261. if(event.target == $(bgcolor_selector_this)[0] || event.target == bgcolor_button || bgcolor_selector_parent > 0) { return; }
  262. bgcolor_selector_hide();
  263. }
  264. bgcolor_selector_hide = function(){
  265. var bgcolor_selector_this = $("div#bwbbcode_bgcolor_selector");
  266. $(document).unbind("mousedown", bgcolor_check_mouse);
  267. bgcolor_selector_this.hide();
  268. bgcolor_selector_ifshow = false;
  269. }
  270. bgcolor_selector_show = function(){
  271. var bgcolor_selector_this = $("div#bwbbcode_bgcolor_selector");
  272. bgcolor_selector_this.css({
  273. top: $(bgcolor_button).offset().top + ($(bgcolor_button).outerHeight()),
  274. left: $(bgcolor_button).offset().left
  275. });
  276. bgcolor_selector_this.show();
  277. $(document).bind("mousedown", bgcolor_check_mouse);
  278. bgcolor_selector_ifshow = true;
  279. }
  280. //////
  281. selectInsert = function (code_begin, code_end) {
  282. var val = $("#content").selected();
  283. //var mytest = val;
  284. //var s="";for(i in mytest){s+=i.toString()+"\n";}alert(mytest);alert(s);
  285. if (val == '') {
  286. //alert('请选择文字后进行此操作...');
  287. //return false;
  288. $('#content').insertIn(code_begin + code_end);
  289. } else {
  290. $('#content').insertIn(code_begin + val + code_end);
  291. }
  292. }
  293. $.fn.selected = function() {
  294. var txt = '';
  295. var doc = this.get(0).document;
  296. if (doc) {
  297. var sel = doc.selection.createRange();
  298. if (sel.text.length > 0) {
  299. txt = sel.text;
  300. }
  301. } else if (this.get(0).selectionStart || this.get(0).selectionStart == '0') {
  302. var s = this.get(0).selectionStart;
  303. var e = this.get(0).selectionEnd;
  304. if (s != e) {
  305. txt = this.get(0).value.substring(s, e);
  306. }
  307. }
  308. return $.trim(txt);
  309. }
  310. $.fn.insertIn = function(t) {
  311. var doc = this.get(0).document;
  312. if (doc) {
  313. var sel = doc.selection.createRange();
  314. this.get(0).focus();
  315. sel.text = t;
  316. } else if (this.get(0).selectionStart || this.get(0).selectionStart == '0') {
  317. var s = this.get(0).selectionStart;
  318. var e = this.get(0).selectionEnd;
  319. var val = this.get(0).value;
  320. var start = val.substring(0,s);
  321. var end = val.substring(e);
  322. this.get(0).value = start + t + end;
  323. }
  324. }
  325. }
  326. });
  327. })(jQuery);