quicktags.js
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:13k
源码类别:

Ajax

开发平台:

Others

  1. // JS QuickTags version 1.3.1
  2. //
  3. // Copyright (c) 2002-2008 Alex King
  4. // http://alexking.org/projects/js-quicktags
  5. //
  6. // Thanks to Greg Heo <greg@node79.com> for his changes 
  7. // to support multiple toolbars per page.
  8. //
  9. // Licensed under the LGPL license
  10. // http://www.gnu.org/copyleft/lesser.html
  11. //
  12. // **********************************************************************
  13. // This program is distributed in the hope that it will be useful, but
  14. // WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
  16. // **********************************************************************
  17. //
  18. // This JavaScript will insert the tags below at the cursor position in IE and 
  19. // Gecko-based browsers (Mozilla, Camino, Firefox, Netscape). For browsers that 
  20. // do not support inserting at the cursor position (older versions of Safari, 
  21. // OmniWeb) it appends the tags to the end of the content.
  22. //
  23. // Pass the ID of the <textarea> element to the edToolbar and function.
  24. //
  25. // Example:
  26. //
  27. //  <script type="text/javascript">edToolbar('canvas');</script>
  28. //  <textarea id="canvas" rows="20" cols="50"></textarea>
  29. //
  30. var dictionaryUrl = 'http://www.ninjawords.com/';
  31. // other options include:
  32. //
  33. // var dictionaryUrl = 'http://www.answers.com/';
  34. // var dictionaryUrl = 'http://www.dictionary.com/';
  35. var edButtons = new Array();
  36. var edLinks = new Array();
  37. var edOpenTags = new Array();
  38. function edButton(id, display, tagStart, tagEnd, access, open) {
  39. this.id = id; // used to name the toolbar button
  40. this.display = display; // label on button
  41. this.tagStart = tagStart;  // open tag
  42. this.tagEnd = tagEnd; // close tag
  43. this.access = access; // set to -1 if tag does not need to be closed
  44. this.open = open; // set to -1 if tag does not need to be closed
  45. }
  46. edButtons.push(
  47. new edButton(
  48. 'ed_bold'
  49. ,'strong'
  50. ,'<strong>'
  51. ,'</strong>'
  52. ,'b'
  53. )
  54. );
  55. edButtons.push(
  56. new edButton(
  57. 'ed_italic'
  58. ,'italic'
  59. ,'<em>'
  60. ,'</em>'
  61. ,'i'
  62. )
  63. );
  64. edButtons.push(
  65. new edButton(
  66. 'ed_link'
  67. ,'link'
  68. ,''
  69. ,'</a>'
  70. ,'a'
  71. )
  72. ); // special case
  73. edButtons.push(
  74. new edButton(
  75. 'ed_img'
  76. ,'img'
  77. ,''
  78. ,''
  79. ,'m'
  80. ,-1
  81. )
  82. ); // special case
  83. edButtons.push(
  84. new edButton(
  85. 'ed_ul'
  86. ,'ul'
  87. ,'<ul>n'
  88. ,'</ul>nn'
  89. ,'u'
  90. )
  91. );
  92. edButtons.push(
  93. new edButton(
  94. 'ed_ol'
  95. ,'ol'
  96. ,'<ol>n'
  97. ,'</ol>nn'
  98. ,'o'
  99. )
  100. );
  101. edButtons.push(
  102. new edButton(
  103. 'ed_li'
  104. ,'li'
  105. ,'t<li>'
  106. ,'</li>n'
  107. ,'l'
  108. )
  109. );
  110. edButtons.push(
  111. new edButton(
  112. 'ed_block'
  113. ,'b-quote'
  114. ,'<blockquote>'
  115. ,'</blockquote>'
  116. ,'q'
  117. )
  118. );
  119. edButtons.push(
  120. new edButton(
  121. 'ed_del'
  122. ,'del'
  123. ,'<del>'
  124. ,'</del>'
  125. )
  126. );
  127. edButtons.push(
  128. new edButton(
  129. 'ed_code'
  130. ,'code'
  131. ,'<code>'
  132. ,'</code>'
  133. ,'c'
  134. )
  135. );
  136. edButtons.push(
  137. new edButton(
  138. 'ed_code'
  139. ,'more'
  140. ,'n<!--more-->n'
  141. ,''
  142. ,''
  143. )
  144. );
  145. edButtons.push(
  146. new edButton(
  147. 'ed_typocode'
  148. ,'typo:code'
  149. ,''
  150. ,'n</typo:code>nn'
  151. ,'typo:code'
  152. )
  153. );
  154. var extendedStart = edButtons.length;
  155. function edLink(display, URL, newWin) {
  156. this.display = display;
  157. this.URL = URL;
  158. if (!newWin) {
  159. newWin = 0;
  160. }
  161. this.newWin = newWin;
  162. }
  163. edLinks[edLinks.length] = new edLink('alexking.org'
  164.                                     ,'http://www.alexking.org/'
  165.                                     );
  166. function edShowButton(which, button, i) {
  167. if (button.access) {
  168. var accesskey = ' accesskey = "' + button.access + '"'
  169. }
  170. else {
  171. var accesskey = '';
  172. }
  173. switch (button.id) {
  174. case 'ed_img':
  175. document.write('<input type="button" id="' + button.id + '_' + which + '" ' + accesskey + ' class="ed_button" onclick="edInsertImage('' + which + '');" value="' + button.display + '" />');
  176. break;
  177. case 'ed_link':
  178. document.write('<input type="button" id="' + button.id + '_' + which + '" ' + accesskey + ' class="ed_button" onclick="edInsertLink('' + which + '', ' + i + ');" value="' + button.display + '" />');
  179. break;
  180. case 'ed_typocode':
  181. document.write('<input type="button" id="' + button.id + '_' + which + '" ' + accesskey + ' class="ed_button" onclick="edInsertTypoCode('' + which + '', ' + i + ');" value="' + button.display + '" />');
  182. break;
  183. default:
  184. document.write('<input type="button" id="' + button.id + '_' + which + '" ' + accesskey + ' class="ed_button" onclick="edInsertTag('' + which + '', ' + i + ');" value="' + button.display + '"  />');
  185. break;
  186. }
  187. }
  188. function edShowLinks() {
  189. var tempStr = '<select onchange="edQuickLink(this.options[this.selectedIndex].value, this);"><option value="-1" selected>(Quick Links)</option>';
  190. for (i = 0; i < edLinks.length; i++) {
  191. tempStr += '<option value="' + i + '">' + edLinks[i].display + '</option>';
  192. }
  193. tempStr += '</select>';
  194. document.write(tempStr);
  195. }
  196. function edAddTag(which, button) {
  197. if (edButtons[button].tagEnd != '') {
  198. edOpenTags[which][edOpenTags[which].length] = button;
  199. document.getElementById(edButtons[button].id + '_' + which).value = '/' + document.getElementById(edButtons[button].id + '_' + which).value;
  200. }
  201. }
  202. function edRemoveTag(which, button) {
  203. for (i = 0; i < edOpenTags[which].length; i++) {
  204. if (edOpenTags[which][i] == button) {
  205. edOpenTags[which].splice(i, 1);
  206. document.getElementById(edButtons[button].id + '_' + which).value = document.getElementById(edButtons[button].id + '_' + which).value.replace('/', '');
  207. }
  208. }
  209. }
  210. function edCheckOpenTags(which, button) {
  211. var tag = 0;
  212. for (i = 0; i < edOpenTags[which].length; i++) {
  213. if (edOpenTags[which][i] == button) {
  214. tag++;
  215. }
  216. }
  217. if (tag > 0) {
  218. return true; // tag found
  219. }
  220. else {
  221. return false; // tag not found
  222. }
  223. }
  224. function edCloseAllTags(which) {
  225. var count = edOpenTags[which].length;
  226. for (o = 0; o < count; o++) {
  227. edInsertTag(which, edOpenTags[which][edOpenTags[which].length - 1]);
  228. }
  229. }
  230. function edQuickLink(i, thisSelect) {
  231. if (i > -1) {
  232. var newWin = '';
  233. if (edLinks[i].newWin == 1) {
  234. newWin = ' target="_blank"';
  235. }
  236. var tempStr = '<a href="' + edLinks[i].URL + '"' + newWin + '>' 
  237.             + edLinks[i].display
  238.             + '</a>';
  239. thisSelect.selectedIndex = 0;
  240. edInsertContent(edCanvas, tempStr);
  241. }
  242. else {
  243. thisSelect.selectedIndex = 0;
  244. }
  245. }
  246. function edSpell(which) {
  247.     myField = document.getElementById(which);
  248. var word = '';
  249. if (document.selection) {
  250. myField.focus();
  251.     var sel = document.selection.createRange();
  252. if (sel.text.length > 0) {
  253. word = sel.text;
  254. }
  255. }
  256. else if (myField.selectionStart || myField.selectionStart == '0') {
  257. var startPos = myField.selectionStart;
  258. var endPos = myField.selectionEnd;
  259. if (startPos != endPos) {
  260. word = myField.value.substring(startPos, endPos);
  261. }
  262. }
  263. if (word == '') {
  264. word = prompt('Enter a word to look up:', '');
  265. }
  266. if (word != '') {
  267. window.open(dictionaryUrl + escape(word));
  268. }
  269. }
  270. function edToolbar(which) {
  271. document.write('<div id="ed_toolbar_' + which + '"><span>');
  272. for (i = 0; i < extendedStart; i++) {
  273. edShowButton(which, edButtons[i], i);
  274. }
  275. if (edShowExtraCookie()) {
  276. document.write(
  277. '<input type="button" id="ed_close_' + which + '" class="ed_button" onclick="edCloseAllTags('' + which + '');" value="Close Tags" />'
  278. + '<input type="button" id="ed_spell_' + which + '" class="ed_button" onclick="edSpell('' + which + '');" value="Dict" />'
  279. );
  280. }
  281. else {
  282. document.write(
  283. '<input type="button" id="ed_close_' + which + '" class="ed_button" onclick="edCloseAllTags('' + which + '');" value="Close Tags" />'
  284. + '<input type="button" id="ed_spell_' + which + '" class="ed_button" onclick="edSpell('' + which + '');" value="Dict" />'
  285. );
  286. }
  287. for (i = extendedStart; i < edButtons.length; i++) {
  288. edShowButton(which, edButtons[i], i);
  289. }
  290. document.write('</span>');
  291. // edShowLinks();
  292. document.write('</div>');
  293.     edOpenTags[which] = new Array();
  294. }
  295. // insertion code
  296. function edInsertTag(which, i) {
  297.     myField = document.getElementById(which);
  298. //IE support
  299. if (document.selection) {
  300. myField.focus();
  301.     sel = document.selection.createRange();
  302. if (sel.text.length > 0) {
  303. sel.text = edButtons[i].tagStart + sel.text + edButtons[i].tagEnd;
  304. }
  305. else {
  306. if (!edCheckOpenTags(which, i) || edButtons[i].tagEnd == '') {
  307. sel.text = edButtons[i].tagStart;
  308. edAddTag(which, i);
  309. }
  310. else {
  311. sel.text = edButtons[i].tagEnd;
  312. edRemoveTag(which, i);
  313. }
  314. }
  315. myField.focus();
  316. }
  317. //MOZILLA/NETSCAPE support
  318. else if (myField.selectionStart || myField.selectionStart == '0') {
  319. var startPos = myField.selectionStart;
  320. var endPos = myField.selectionEnd;
  321. var cursorPos = endPos;
  322. var scrollTop = myField.scrollTop;
  323. if (startPos != endPos) {
  324. myField.value = myField.value.substring(0, startPos)
  325.               + edButtons[i].tagStart
  326.               + myField.value.substring(startPos, endPos) 
  327.               + edButtons[i].tagEnd
  328.               + myField.value.substring(endPos, myField.value.length);
  329. cursorPos += edButtons[i].tagStart.length + edButtons[i].tagEnd.length;
  330. }
  331. else {
  332. if (!edCheckOpenTags(which, i) || edButtons[i].tagEnd == '') {
  333. myField.value = myField.value.substring(0, startPos) 
  334.               + edButtons[i].tagStart
  335.               + myField.value.substring(endPos, myField.value.length);
  336. edAddTag(which, i);
  337. cursorPos = startPos + edButtons[i].tagStart.length;
  338. }
  339. else {
  340. myField.value = myField.value.substring(0, startPos) 
  341.               + edButtons[i].tagEnd
  342.               + myField.value.substring(endPos, myField.value.length);
  343. edRemoveTag(which, i);
  344. cursorPos = startPos + edButtons[i].tagEnd.length;
  345. }
  346. }
  347. myField.focus();
  348. myField.selectionStart = cursorPos;
  349. myField.selectionEnd = cursorPos;
  350. myField.scrollTop = scrollTop;
  351. }
  352. else {
  353. if (!edCheckOpenTags(which, i) || edButtons[i].tagEnd == '') {
  354. myField.value += edButtons[i].tagStart;
  355. edAddTag(which, i);
  356. }
  357. else {
  358. myField.value += edButtons[i].tagEnd;
  359. edRemoveTag(which, i);
  360. }
  361. myField.focus();
  362. }
  363. }
  364. function edInsertContent(which, myValue) {
  365.     myField = document.getElementById(which);
  366. //IE support
  367. if (document.selection) {
  368. myField.focus();
  369. sel = document.selection.createRange();
  370. sel.text = myValue;
  371. myField.focus();
  372. }
  373. //MOZILLA/NETSCAPE support
  374. else if (myField.selectionStart || myField.selectionStart == '0') {
  375. var startPos = myField.selectionStart;
  376. var endPos = myField.selectionEnd;
  377. var scrollTop = myField.scrollTop;
  378. myField.value = myField.value.substring(0, startPos)
  379.               + myValue 
  380.                       + myField.value.substring(endPos, myField.value.length);
  381. myField.focus();
  382. myField.selectionStart = startPos + myValue.length;
  383. myField.selectionEnd = startPos + myValue.length;
  384. myField.scrollTop = scrollTop;
  385. } else {
  386. myField.value += myValue;
  387. myField.focus();
  388. }
  389. }
  390. function edInsertLink(which, i, defaultValue) {
  391.     myField = document.getElementById(which);
  392. if (!defaultValue) {
  393. defaultValue = 'http://';
  394. }
  395. if (!edCheckOpenTags(which, i)) {
  396. var URL = prompt('Enter the URL' ,defaultValue);
  397. if (URL) {
  398. edButtons[i].tagStart = '<a href="' + URL + '">';
  399. edInsertTag(which, i);
  400. }
  401. }
  402. else {
  403. edInsertTag(which, i);
  404. }
  405. }
  406. function edInsertTypoCode(which, i, defaultValue) {
  407.     myField = document.getElementById(which);
  408. if (!defaultValue) {
  409. defaultValue = '';
  410. }
  411. if (!edCheckOpenTags(which, i)) {
  412. var code = prompt('Choose language' ,defaultValue);
  413. if (code) {
  414. edButtons[i].tagStart = '<typo:code lang="' + code + '">n';
  415. edInsertTag(which, i);
  416. }
  417. }
  418. else {
  419. edInsertTag(which, i);
  420. }
  421. }
  422. function edInsertImage(which) {
  423.     myField = document.getElementById(which);
  424. var myValue = prompt('Enter the URL of the image', 'http://');
  425. if (myValue) {
  426. myValue = '<img src="' 
  427. + myValue 
  428. + '" alt="' + prompt('Enter a description of the image', '') 
  429. + '" />';
  430. edInsertContent(which, myValue);
  431. }
  432. }
  433. function edInsertImageFromCarousel(which, image) {
  434.     myField = document.getElementById(which);
  435. if (image) {
  436. myValue = '<img src="' 
  437. + image 
  438. + '" alt="' + prompt('Enter a description of the image', '') 
  439. + '" />';
  440. edInsertContent(which, myValue);
  441. }
  442. }
  443. function countInstances(string, substr) {
  444. var count = string.split(substr);
  445. return count.length - 1;
  446. }
  447. function edInsertVia(which) {
  448.     myField = document.getElementById(which);
  449. var myValue = prompt('Enter the URL of the source link', 'http://');
  450. if (myValue) {
  451. myValue = '(Thanks <a href="' + myValue + '" rel="external">'
  452. + prompt('Enter the name of the source', '') 
  453. + '</a>)';
  454. edInsertContent(which, myValue);
  455. }
  456. }
  457. function edSetCookie(name, value, expires, path, domain) {
  458. document.cookie= name + "=" + escape(value) +
  459. ((expires) ? "; expires=" + expires.toGMTString() : "") +
  460. ((path) ? "; path=" + path : "") +
  461. ((domain) ? "; domain=" + domain : "");
  462. }
  463. function edShowExtraCookie() {
  464. var cookies = document.cookie.split(';');
  465. for (var i=0;i < cookies.length; i++) {
  466. var cookieData = cookies[i];
  467. while (cookieData.charAt(0) ==' ') {
  468. cookieData = cookieData.substring(1, cookieData.length);
  469. }
  470. if (cookieData.indexOf('js_quicktags_extra') == 0) {
  471. if (cookieData.substring(19, cookieData.length) == 'show') {
  472. return true;
  473. }
  474. else {
  475. return false;
  476. }
  477. }
  478. }
  479. return false;
  480. }